2012-02-18 247 views
18

我試圖按照this post中所述使用-XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,*MyClass.myMethod命令行。如何使用-XX:+ UnlockDiagnosticVMOptions -XX:CompileCommand =使用JVM HotSpot打印選項

它似乎可以用於open-jdkhttps://wikis.oracle.com/display/HotSpotInternals/PrintAssembly)。

如何在Oracle JDK7和JVM HotSpot中使用這些選項(或類似的等價物)?

+0

它看起來不太難看 – 2012-02-18 13:30:55

+0

Ofc我試過但它不起作用,我的問題是如何使它們工作?我會編輯。 – 2012-02-18 13:54:49

+0

您是否嘗試了「如果您沒有輸出,請使用-XX:+ PrintCompilation驗證您的方法是否已編譯完畢。「 – 2012-02-18 15:52:14

回答

26

這些說明適用於Linux(Ubuntu 10.04.4 LTS),但應適用於您的操作系統。下載Oracle JDK 7u3並相應地設置您的JAVA_HOMEPATH環境變量後,執行以下命令檢查可用的選項:

java -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal -version 

您應該看到UnlockDiagnosticVMOptionsCompileCommandPrintAssembly可供選擇。使用CompileCommand選項也將啓用PrintAssembly選項。但是,您需要使用PrintAssembly的HotSpot反彙編程序插件才能正常工作;沒有它,你可能會看到類似以下內容:

$ java -version 
java version "1.7.0_03" 
Java(TM) SE Runtime Environment (build 1.7.0_03-b04) 
Java HotSpot(TM) Server VM (build 22.1-b02, mixed mode) 
$ java -server -XX:+UnlockDiagnosticVMOptions '-XX:CompileCommand=print,*Main.main' Main 
CompilerOracle: print *Main.main 
Java HotSpot(TM) Server VM warning: printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output 
Compiled method (c2)  68 1 %   Main::main @ 4 (49 bytes) 
total in heap [0xb3a97548,0xb3a979ec] = 1188 
relocation  [0xb3a97610,0xb3a97624] = 20 
main code  [0xb3a97640,0xb3a97840] = 512 
stub code  [0xb3a97840,0xb3a97850] = 16 
oops   [0xb3a97850,0xb3a97858] = 8 
scopes data [0xb3a97858,0xb3a97898] = 64 
scopes pcs  [0xb3a97898,0xb3a979e8] = 336 
dependencies [0xb3a979e8,0xb3a979ec] = 4 
Could not load hsdis-i386.so; library not loadable; PrintAssembly is disabled 
OopMapSet contains 1 OopMaps 

要獲取熱點反彙編器插件,你將需要建立它。縱觀OpenJDK的7U2來源,hsdis plugin readme說:

To use the plugin with a JVM, you need a new version that can load it. If the product mode of your JVM does not accept -XX:+PrintAssembly, you do not have a version that is new enough.

To build this project you [need] a copy of GNU binutils to build against.

In theory this should be buildable on Windows but getting a working GNU build environment on Windows has proven difficult.

我們,甲骨文JDK 7U3上述已確認支持PrintAssembly。我跟着hsdis插件自述說明,下載了GNU binutils 2.22,將它放在hsdis build/binutils目錄中並運行make。這最終將產生以下錯誤:

hsdis.c:32:20: error: sysdep.h: No such file or directory 

爲了解決這個問題,我改變hsdis.c使用下面的補丁:

diff -r 6259c6d3bbb7 src/share/tools/hsdis/hsdis.c 
--- a/src/share/tools/hsdis/hsdis.c Mon Dec 12 23:08:01 2011 -0800 
+++ b/src/share/tools/hsdis/hsdis.c Thu Feb 23 09:26:37 2012 -0500 
@@ -29,7 +29,7 @@ 

#include "hsdis.h" 

-#include <sysdep.h> 
+#include <errno.h> 
#include <libiberty.h> 
#include <bfd.h> 
#include <dis-asm.h> 

運行make當時成功。現在只需將hsdis build目錄中的hsdis-i386.so插件複製到Oracle JDK 7u3 jre/lib/i386目錄。

現在你可以看到反彙編編譯代碼:

$ java -server -XX:+UnlockDiagnosticVMOptions '-XX:CompileCommand=print,*Main.main' Main 
CompilerOracle: print *Main.main 
Java HotSpot(TM) Server VM warning: printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output 
Compiled method (c2)  68 1 %   Main::main @ 4 (49 bytes) 
total in heap [0xb3999548,0xb39999ec] = 1188 
relocation  [0xb3999610,0xb3999624] = 20 
main code  [0xb3999640,0xb3999840] = 512 
stub code  [0xb3999840,0xb3999850] = 16 
oops   [0xb3999850,0xb3999858] = 8 
scopes data [0xb3999858,0xb3999898] = 64 
scopes pcs  [0xb3999898,0xb39999e8] = 336 
dependencies [0xb39999e8,0xb39999ec] = 4 
Loaded disassembler from [snip]/jdk1.7.0_03/jre/lib/i386/hsdis-i386.so 
Decoding compiled method 0xb3999548: 
Code: 
[Disassembling for mach='i386'] 
[Entry Point] 
[Verified Entry Point] 
[Constants] 
    # {method} 'main' '([Ljava/lang/String;)V' in 'Main' 
    0xb3999640: call 0xb6ff8510   ; {runtime_call} 
    0xb3999645: data32 xchg %ax,%ax 
    0xb3999648: mov %eax,-0x3000(%esp) 
    0xb399964f: push %ebp 
    0xb3999650: sub $0x38,%esp 
    0xb3999656: mov %ecx,%esi 
    0xb3999658: mov 0x4(%esi),%ebp 
    0xb399965b: mov 0x8(%esi),%edi 
    0xb399965e: mov (%ecx),%esi 
    0xb3999660: mov %ecx,(%esp) 
    0xb3999663: call 0xb7078cf0   ;*iload_3 
[snip] 
    0xb399983e: hlt  
    0xb399983f: hlt  
[Exception Handler] 
[Stub Code] 
    0xb3999840: jmp 0xb39981e0   ; {no_reloc} 
[Deopt Handler Code] 
    0xb3999845: push $0xb3999845  ; {section_word} 
    0xb399984a: jmp 0xb397e220   ; {runtime_call} 
    0xb399984f: .byte 0x0 
OopMapSet contains 1 OopMaps 

#0 
OopMap{off=468} 

我使用的測試類是:

public class Main { 
    public static void main(final String[] args) { 
     long x = 0; 
     for (int i = 0; i < 1000000; i++) { 
      x += calculate(i); 
     } 
     System.out.println("x=" + x); 
    } 

    private static long calculate(final int i) { 
     return (long)i * (long)i; 
    } 
} 
+0

非常感謝答覆,我有一個問題,理解我在哪裏找到hsdis以及如何安裝它?我不明白我應該在哪裏創建這個構建目錄。從現在開始,我已經在這裏找到了這些文件http://hg.openjdk.java.net/ jdk7/hotspot/hotspot/file/tip/src/share/tools/hsdis /我已經下載binutils-2.22。當你說到'binutils'時,它的整個目錄就是名爲binutils的子目錄? – 2012-02-23 16:28:16

+0

@ alain.janinm從hsdis自述文件可以看出:'makefile在build/binutils中查找源代碼,或者可以使用BINTUILS = path指定它的位置。'build/binutils'與hsdis文件所在目錄相關。從此目錄,你可以嘗試像'mkdir build; cd build; wget -O - http://ftp.gnu.or g/gnu/binutils/binutils-2.22.tar.bz2 | tar xjvf - ; ln -s binutils-2.2.22 binutils'。或者嘗試'BINUTILS = path_to_where_you_extracted_binutils make'。 – 2012-02-23 16:51:28

+0

你真棒,它完美的作品!非常感謝! – 2012-02-23 17:28:10

1

在我的情況看反彙編編譯代碼:

$ java -XX:CompileThreshold=1 -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand="compileonly pac/kage/MyClass myMethod" MyClass 

在上面的例子中有周期:for (int i = 0; i < 1 000 000; i++) {...}, 這就是爲什麼在沒有1 000 000次迭代的情況下,我們需要-XX:CompileThreshold=1選項(對於-server,默認爲10 000)來查看我們反彙編的編譯代碼。