查看Java代碼對應的匯編指令又一利器,JITWatch 轉
阿新 • • 發佈:2017-05-26
又一 size watch tope -s down rtt diag ini
http://www.tuicool.com/articles/IRrIRb3
原文 http://javagoo.tk/java/jitwatch_code.html 主題 JDK查看Java代碼對應的匯編指令又一利器,JITWatch
糾錯 13 May 2015
接著上一篇文章 查看Java代碼對應的匯編指令利器,hsdis 。JITWatch提供了更好的顯示方式,還有各種圖表,稱得上又一利器。
github地址: JITWatch
git clone git@github.com:AdoptOpenJDK/jitwatch.git
cd jitwatch
mvn clean install -DskipTests=true
./launchUI.sh
我們通過一個簡單的例子來看下如何使用(例子稍微復雜些,為了了解JDK8對AtomicInteger.getAndIncrement()方法做的優化),基於JDK8(jdk7的getAndIncrement()方法實現不同)
首先給出java代碼,AtomicInteger_jdk8.java
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicInteger_jdk8 {
private final static int TEST_SIZE = 100000000;
private final static int THREAD_COUNT = 10;
private CountDownLatch cdl = new CountDownLatch(THREAD_COUNT + 1);
private AtomicInteger ai = new AtomicInteger(0);
private long startTime;
public void init() {
startTime = System.nanoTime();
}
public class MyTask implements Runnable {
@Override
public void run() {
while (true) {
if(ai.getAndIncrement() == TEST_SIZE) {
System.out.println(System.nanoTime() - startTime);
cdl.countDown();
System.exit(0);
}
}
}
}
public static void main(String[] args) {
AtomicInteger_jdk8 at = new AtomicInteger_jdk8();
at.init();
for (int n = 0; n < THREAD_COUNT; n++)
new Thread(at.new MyTask()).start();
System.out.println("start");
at.cdl.countDown();
}
}
編譯執行,並輸出日誌(提示:需要hsdis)
javac AtomicInteger_jdk8.java
java -server -XX:+UnlockDiagnosticVMOptions -XX:+TraceClassLoading -XX:+PrintAssembly -XX:+LogCompilation -XX:LogFile=jit.log AtomicInteger_jdk8
如圖所示:
- 點擊Open Log選擇jit.log文件
- 點擊Start
- 如圖,右擊run()方法,點擊TriView
AtomicInteger.getAndIncrement()方法對應的匯編指令callq。
通過JITWatch發現,getAndAddInt()已經被編譯為特殊的機器指令xadd(這就是為啥jdk8比jdk7快的原因,讀者可以自己看下jdk7是啥)
--------------------------------------------補充下JDK7的------------------------------------
查看Java代碼對應的匯編指令又一利器,JITWatch 轉