标准 JIT 编译而不是 HotSpot 中的 On Stack Replacement

Standard JIT compilation instead of On Stack Replacement in HotSpot

我试图在 java HotSpot VM 中使用 C1 查看标准 JIT 编译而不是 OSR 的结果。我已经使用 -XX:-UseOnStackReplacement 关闭了 OSR,并使用 -XX:TieredStopAtLevel=1 将编译限制为 C1。但是现在我的方法根本没有被编译。我打开了 Print Compilation,如果我让它使用 OSR,它会很好地记录编译。另外 none 我的断点在没有 OSR 的 C1 文件中被击中。

我正在使用一个非常简单的代码片段来测试这个

class Demo {
  public static void main(String[] args) {
      int a = workload();
    System.out.println("Calculated answer is: " + a);
  }

  private static int workload() {
    int a = 14;
    for (int i = 0; i<100000; i++) {
      a = a + i;
    }
    return a;
  }
}

问题是您只调用了一次 workload 并多次执行该循环;你没有执行workload很多次;这是你在这里遇到的主要问题。 JIT 可以优化方法,但这里只有一个循环 - 因此除非 OSR 处于活动状态,否则没有太多需要优化的地方。

这很容易证明,你可以运行你的方法:

-XX:+UnlockDiagnosticVMOptions  
-XX:TieredStopAtLevel=1 
-XX:+TraceNMethodInstalls // this is to track the compiled methods
-XX:-UseOnStackReplacement  
   com.so.jit.OSRCompilation // this is the classname I've used

在您将获得的输出中,您会看到很多 Installing method

但是如果你重新启用 OSR:

-XX:+UnlockDiagnosticVMOptions  
-XX:TieredStopAtLevel=1 
-XX:+TraceNMethodInstalls // this is to track the compiled methods
-XX:+UseOnStackReplacement  
   com.so.jit.OSRCompilation // this is the classname I've used

你会得到很多Installing method还有一行:

 Installing osr method (1) com.so.jit.OSRCompilation.workload()I @ 5