http://blog.csdn.net/unei66/article/details/26477629

JVM 有 HotSpot引擎可以对热代码路径进行有效的 JIT优化,大幅度提升计算密集代码的性能。默认一个方法至少被调用10k次以上才可能被JIT优化。

查看JIT工作情况

Java代码

  1. public class VolatileBarrierExample {
  2. long a;
  3. volatile long v1=1;
  4. volatile long v2=1;
  5. void readAndWrite(){
  6. long j=v1;
  7. long i=v2;
  8. a=i+j;
  9. v1=i+1;
  10. long v=v1;
  11. v2=j*2;
  12. }
  13. public static void main(String[] args){
  14. final VolatileBarrierExample ex=new VolatileBarrierExample();
  15. for(int i=0;i<50000;i++)
  16. ex.readAndWrite();
  17. }
  18. }

运行命令:

  1. java -XX:+PrintCompilation VolatileBarrierExample

输出:

  1. VM option '+PrintCompilation'
  2. 242    1             VolatileBarrierExample::readAndWrite (40 bytes)
  3. 249    1 %           VolatileBarrierExample::main @ 10 (27 bytes)

用如下命令行可以更多地了解内联优化的实际情况以及优化发生的级别:

  1. java -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining -XX:+TieredCompilation VolatileBarrierExample

查看JIT生成的汇编代码

环境说明:

1.      CPU:Intel i7

2.      操作系统:Ubuntu 12.04-amd64

3.      JDK:openjdkversion "1.7.0-internal-fastdebug",我使用的是fastdebug版本,可以直接使用openjdk。

4.      hsdis  下载地址:https://kenai.com/projects/base-hsdis/downloads,也可自行下载源代码编译,将hsdis-*.so放在目录$JAVA_HOME/jre/lib/amd64/server下.

运行命令:

  1. java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly VolatileBarrierExample

产生的汇编代码

  1. VM option '+UnlockDiagnosticVMOptions'
  2. VM option '+PrintAssembly'
  3. OpenJDK 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output
  4. Loaded disassembler from hsdis-amd64.so
  5. Decoding compiled method 0x00007fae110f5890:
  6. Code:
  7. [Disassembling for mach='i386:x86-64']
  8. [Entry Point]
  9. [Constants]
  10. # {method} 'readAndWrite' '()V' in 'VolatileBarrierExample'
  11. #           [sp+0x20]  (sp of caller)
  12. 0x00007fae110f59c0: mov    0x8(%rsi),%r10d
  13. 0x00007fae110f59c4: cmp    %r10,%rax
  14. 0x00007fae110f59c7: jne    0x00007fae110cd920  ;   {runtime_call}
  15. 0x00007fae110f59cd: nop
  16. 0x00007fae110f59ce: nop
  17. 0x00007fae110f59cf: nop
  18. [Verified Entry Point]
  19. 0x00007fae110f59d0: push   %rbp
  20. 0x00007fae110f59d1: sub    $0x10,%rsp
  21. 0x00007fae110f59d5: nop                       ;*synchronization entry
  22. ; - VolatileBarrierExample::readAndWrite@-1 (line 13)
  23. 0x00007fae110f59d6: mov    0x18(%rsi),%r10    ;*getfield v1
  24. ; - VolatileBarrierExample::readAndWrite@1 (line 13)
  25. 0x00007fae110f59da: mov    0x20(%rsi),%r11    ;*getfield v2
  26. ; - VolatileBarrierExample::readAndWrite@6 (line 14)
  27. 0x00007fae110f59de: mov    %r10,%r8
  28. 0x00007fae110f59e1: add    %r11,%r8
  29. 0x00007fae110f59e4: mov    %r8,0x10(%rsi)
  30. 0x00007fae110f59e8: shl    %r10
  31. 0x00007fae110f59eb: add    $0x1,%r11
  32. 0x00007fae110f59ef: mov    %r11,0x18(%rsi)
  33. 0x00007fae110f59f3: mov    %r10,0x20(%rsi)    ;*putfield v2
  34. ; - VolatileBarrierExample::readAndWrite@36 (line 18)
  35. 0x00007fae110f59f7: add    $0x10,%rsp
  36. 0x00007fae110f59fb: pop    %rbp
  37. 0x00007fae110f59fc: test   %eax,0xa6875fe(%rip)        # 0x00007fae1b77d000
  38. ;   {poll_return}
  39. ……….

如果只查看某个方法产生的汇编代码,可以使用命令:

  1. java -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,*VolatileBarrierExample.readAndWrite VolatileBarrierExample

如果运行命令是出现此错误:

Could not load hsdis-amd64.so; library notloadable; PrintAssembly is disabled

可以设置

  1. export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/server/

参考资料:

1. http://chaoslawful.info/archives/387

2. https://wikis.oracle.com/display/HotSpotInternals/PrintAssembly

使用hsdis查看jit生成的汇编代码的更多相关文章

  1. GCC生成的汇编代码

    假设我们写了一个C代码文件 code.c包含下面代码: int accum = 0; int sum(int x, int y){ int t = x + y; accum += t; return ...

  2. 利用hsdis和JITWatch查看分析HotSpot JIT compiler生成的汇编代码

    http://blog.csdn.net/hengyunabc/article/details/26898657

  3. 如何在windows平台下使用hsdis与jitwatch查看JIT后的汇编码

    1. 安装hsids 这一步比较麻烦,需要提前安装cygwin,以及下载openjdk的源码 具体步骤请参考下面的两篇文章 How to build hsdis-amd64.dll and hsdis ...

  4. 【JVM】-NO.110.JVM.1 -【hsdis jitwatch 生成查看汇编代码】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  5. 使用gcc不同选项来编译查看中间生成文件

    gcc编译C程序的总体流程如下图 用到的命令如下: .c---> .i gcc -E hello.c .c--->.s gcc -S hello.c .c--->.o gcc -c ...

  6. 分析一个C语言程序生成的汇编代码-《Linux内核分析》Week1作业

    署名信息 郭春阳 原创作品转载请注明出处 :<Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 C源码 这 ...

  7. 解析c语言背后的汇编代码

    源码 很简单的c语言代码,作用是交换两个数: #include <stdio.h> void swap(int * a, int * b) { *a = *a + *b - (*b = * ...

  8. Keil 中关于C语言编译生成汇编代码函数名规则

    在keil 中 C语言的函数有带参数和不带参数之分. 一般的资料里说fun(void)类型的函数不带参数,所以,keil编译器生成的汇编的调用地址(函数名) 为fun.这没有错.事实上,不管C语言的函 ...

  9. 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的

    实验一:通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 学号:20135114 姓名:王朝宪 注: 原创作品转载请注明出处   <Linux内核分析>MOOC课程http: ...

随机推荐

  1. tr/td

    在HTML中,tr代表行,td代表列. 说明: 1.tr与td必须一起使用,并且输入的内容必须在td里面: 2.td必须在tr里面,表示在一行中的列: 3.在一个tr里面,有x个td,就表示在这一行里 ...

  2. 【BZOJ2253】纸箱堆叠 [CDQ分治]

    纸箱堆叠 Time Limit: 30 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description P 工厂是一个生产纸箱的工厂. 纸 ...

  3. 单源最短路模板_SPFA_Dijkstra(堆优化)_C++

    随手一打就是标准的SPFA,默认1号节点为出发点,当然不用 f 判断是否在队里也可以,只是这样更优化一点 void spfa() { int i,x,k; ;i<=n;i++) { d[i]=o ...

  4. bzoj 3190 维护栈

    我们可以将每一辆赛车看成一条直线,斜率为速度,纵截距为初始位置,那么问题就转化为求这n条直线处于最上面的直线.最上面是指在坐标系中,假设从x轴向下看,能看到的直线,只露一个点也算能看见.那么就类似水平 ...

  5. bootstrap-table不分页时对数值类型数据的排序

    html中的代码 <table id="table"></table> sortData.json的数据如下 [ {"name":&qu ...

  6. jetty bleed漏洞利用工具

    两个exp: https://github.com/AppSecConsulting/Pentest-Tools/blob/master/jetty-bleed.py https://github.c ...

  7. mongodb的数据库操作

    1.创建数据库 语法 MongoDB 创建数据库的语法格式如下: use DATABASE_NAME 如果数据库不存在,则创建数据库,否则切换到指定数据库. 1.创建数据库 > show dbs ...

  8. MSP432P401R时钟入门

    拿到msp432的板子差不多一年了,刚刚进行了开机点亮LED工程:         首先是msp432的时钟模块(CS),个人理解msp432最特色的功能应该是超低功耗和高性能的组合.432系列的时钟 ...

  9. oracle 一行转多行

    比如sql: select zyxdm from table where bindid=2265254 查询结果为:1|4|8|9|10 将这个查询结果转成多行,结果如下: ID 1 4 8 9 10 ...

  10. HUST-1350 Trie

    1350 - Trie 时间限制:1秒 内存限制:128兆 104 次提交 35 次通过 题目描述 In computer science, a trie, is an ordered tree da ...