从Hotspot JIT编译器打印生成的汇编代码
有時,在對Java應用程序進行性能分析時,有必要了解Hotspot JIT編譯器生成的匯編代碼。 這對于確定已做出的優化決策以及我們的代碼更改如何影響生成的匯編代碼非常有用。 在調試并行算法以確保已按預期應用可見性規則時,知道何時發出什么指令也很有用。 通過這種方式,我在各種JVM中發現了很多錯誤。
該博客說明了如何安裝反匯編程序插件,并提供了針對特定方法的命令行選項。
安裝
以前,有必要獲取調試版本以打印由Hotspot JIT為Oracle / SUN JVM生成的匯編代碼。 從Java 7開始,如果在標準Oracle Hotspot JVM中安裝了反匯編程序插件 ,則可以打印生成的匯編代碼。 要為64位Linux安裝插件,請按照以下步驟操作:
您現在已經安裝了插件!
測試程序
為了測試插件,我們需要一些代碼,這些代碼既對程序員很有趣,又執行得足夠熱,可以被JIT優化。 JIT何時進行優化的一些細節可以在這里找到。 下面的代碼可用于通過讀寫易失字段來測量兩個線程之間的平均延遲。 這些易失字段很有趣,因為它們需要關聯的硬件籬笆來遵守Java內存模型 。
import static java.lang.System.out;public class InterThreadLatency {private static final int REPETITIONS = 100 * 1000 * 1000;private static volatile int ping = -1;private static volatile int pong = -1;public static void main(final String[] args)throws Exception{for (int i = 0; i < 5; i++){final long duration = runTest();out.printf("%d - %dns avg latency - ping=%d pong=%d\n",i,duration / (REPETITIONS * 2),ping,pong);}}private static long runTest() throws InterruptedException{final Thread pongThread = new Thread(new PongRunner());final Thread pingThread = new Thread(new PingRunner());pongThread.start();pingThread.start();final long start = System.nanoTime();pongThread.join();return System.nanoTime() - start;}public static class PingRunner implements Runnable{public void run(){for (int i = 0; i < REPETITIONS; i++){ping = i;while (i != pong){// busy spin}}}}public static class PongRunner implements Runnable{public void run(){for (int i = 0; i < REPETITIONS; i++){while (i != ping){// busy spin}pong = i;}}} }印刷匯編代碼
使用以下語句可以打印所有生成的匯編代碼。
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly InterThreadLatency但是,這會使您處于無法看到樹木的森林的情況。 通常,針對特定方法更有用。 對于此測試,Hotspot將優化run()方法并生成兩次。 一次用于OSR版本,然后一次用于標準JIT版本。 標準的JIT版本如下。
java -XX:+UnlockDiagnosticVMOptions '-XX:CompileCommand=print,*PongRunner.run' InterThreadLatencyCompiled method (c2) 10531 5 InterThreadLatency$PongRunner::run (30 bytes)total in heap [0x00007fed81060850,0x00007fed81060b30] = 736relocation [0x00007fed81060970,0x00007fed81060980] = 16main code [0x00007fed81060980,0x00007fed81060a00] = 128stub code [0x00007fed81060a00,0x00007fed81060a18] = 24oops [0x00007fed81060a18,0x00007fed81060a30] = 24scopes data [0x00007fed81060a30,0x00007fed81060a78] = 72scopes pcs [0x00007fed81060a78,0x00007fed81060b28] = 176dependencies [0x00007fed81060b28,0x00007fed81060b30] = 8 Decoding compiled method 0x00007fed81060850: Code: [Entry Point] [Constants]# {method} 'run' '()V' in 'InterThreadLatency$PongRunner'# [sp+0x20] (sp of caller)0x00007fed81060980: mov 0x8(%rsi),%r10d0x00007fed81060984: shl $0x3,%r100x00007fed81060988: cmp %r10,%rax0x00007fed8106098b: jne 0x00007fed81037a60 ; {runtime_call}0x00007fed81060991: xchg %ax,%ax0x00007fed81060994: nopl 0x0(%rax,%rax,1)0x00007fed8106099c: xchg %ax,%ax [Verified Entry Point]0x00007fed810609a0: sub $0x18,%rsp0x00007fed810609a7: mov %rbp,0x10(%rsp) ;*synchronization entry; - InterThreadLatency$PongRunner::run@-1 (line 58)0x00007fed810609ac: xor %r11d,%r11d0x00007fed810609af: mov $0x7ad0fcbf0,%r10 ; {oop(a 'java/lang/Class' = 'InterThreadLatency')}0x00007fed810609b9: jmp 0x00007fed810609d00x00007fed810609bb: nopl 0x0(%rax,%rax,1) ; OopMap{r10=Oop off=64};*goto; - InterThreadLatency$PongRunner::run@15 (line 60)0x00007fed810609c0: test %eax,0xaa1663a(%rip) # 0x00007fed8ba77000;*goto; - InterThreadLatency$PongRunner::run@15 (line 60); {poll}0x00007fed810609c6: nopw 0x0(%rax,%rax,1) ;*iload_1; - InterThreadLatency$PongRunner::run@8 (line 60)0x00007fed810609d0: mov 0x74(%r10),%r9d ;*getstatic ping; - InterThreadLatency::access$000@0 (line 3); - InterThreadLatency$PongRunner::run@9 (line 60)0x00007fed810609d4: cmp %r9d,%r11d0x00007fed810609d7: jne 0x00007fed810609c00x00007fed810609d9: mov %r11d,0x78(%r10)0x00007fed810609dd: lock addl $0x0,(%rsp) ;*putstatic pong; - InterThreadLatency::access$102@2 (line 3); - InterThreadLatency$PongRunner::run@19 (line 65)0x00007fed810609e2: inc %r11d ;*iinc; - InterThreadLatency$PongRunner::run@23 (line 58)0x00007fed810609e5: cmp $0x5f5e100,%r11d0x00007fed810609ec: jl 0x00007fed810609d0 ;*if_icmpeq; - InterThreadLatency$PongRunner::run@12 (line 60)0x00007fed810609ee: add $0x10,%rsp0x00007fed810609f2: pop %rbp0x00007fed810609f3: test %eax,0xaa16607(%rip) # 0x00007fed8ba77000; {poll_return}0x00007fed810609f9: retq ;*iload_1; - InterThreadLatency$PongRunner::run@8 (line 60)0x00007fed810609fa: hlt 0x00007fed810609fb: hlt 0x00007fed810609fc: hlt 0x00007fed810609fd: hlt 0x00007fed810609fe: hlt 0x00007fed810609ff: hlt [Exception Handler] [Stub Code]0x00007fed81060a00: jmpq 0x00007fed8105eaa0 ; {no_reloc} [Deopt Handler Code]0x00007fed81060a05: callq 0x00007fed81060a0a0x00007fed81060a0a: subq $0x5,(%rsp)0x00007fed81060a0f: jmpq 0x00007fed81038c00 ; {runtime_call}0x00007fed81060a14: hlt 0x00007fed81060a15: hlt 0x00007fed81060a16: hlt 0x00007fed81060a17: hlt OopMapSet contains 1 OopMaps#0 OopMap{r10=Oop off=64}有趣的觀察
上面的匯編代碼用紅色突出顯示的行非常有趣。 當一個
寫入volatile字段后,在Java內存模型下,寫入必須順序一致 ,即,由于通常應用的優化(例如將寫入暫存到存儲緩沖區)而不會重新排序。 這可以通過插入適當的內存屏障來實現。 在上述情況下,Hotspot選擇通過執行MOV指令(寄存器到內存地址-即寫操作),然后發出具有排序語義的LOCK ADD指令(不使用棧指針作為圍籬慣用語)來強制執行排序。 這在x86處理器上并不理想。 使用單個LOCK XCHG指令進行寫入,可以更有效,更正確地執行相同的操作。 這使我想知道JVM是否存在一些重大的折衷,以使其可以跨許多體系結構移植,而不是在x86上可以做到的最好。
翻譯自: https://www.javacodegeeks.com/2013/07/printing-generated-assembly-code-from-the-hotspot-jit-compiler.html
總結
以上是生活随笔為你收集整理的从Hotspot JIT编译器打印生成的汇编代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 索尼 PS Plus 九月免费阵容公布:
- 下一篇: 丰田埃尔法的衰退进行时