Search code examples
javajava-8jvm-hotspot

Standard JIT compilation instead of On Stack Replacement in HotSpot


I am trying to see the result of standard JIT compilation instead of OSR using C1 in java HotSpot VM. I have turned off OSR using -XX:-UseOnStackReplacement and restricted compilation to C1 using -XX:TieredStopAtLevel=1. But now my method is not getting compiled at all. I have Print Compilation turned on, which logs compilation just fine if I let it use OSR. Also none of my breakpoints are getting hit in C1 files without OSR.

I am using a very simple code snippet to test this

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;
  }
}

Solution

  • The problem is that you invoke workload only once and execute that loop many times; you are not executing workload many times; and that is the main problem you have here. JIT can optimize methods, but here you have a single loop - so unless OSR is active there is not much to optimize.

    This is fairly easy to prove, you can run your method with:

    -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
    

    In the output that you will get, you will see a lot of Installing method.

    But if you enable back the 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
    

    you will get a lot of Installing method, but also one line:

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