test/compiler/whitebox/CompilerWhiteBoxTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff test/compiler/whitebox

test/compiler/whitebox/CompilerWhiteBoxTest.java

Print this page




  52     /** Maximal value for CompLevel */
  53     protected static int COMP_LEVEL_MAX = COMP_LEVEL_FULL_OPTIMIZATION;
  54 
  55     /** Instance of WhiteBox */
  56     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  57     /** Value of {@code -XX:CompileThreshold} */
  58     protected static final int COMPILE_THRESHOLD
  59             = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
  60     /** Value of {@code -XX:BackgroundCompilation} */
  61     protected static final boolean BACKGROUND_COMPILATION
  62             = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
  63     /** Value of {@code -XX:TieredCompilation} */
  64     protected static final boolean TIERED_COMPILATION
  65             = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
  66     /** Value of {@code -XX:TieredStopAtLevel} */
  67     protected static final int TIERED_STOP_AT_LEVEL
  68             = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
  69     /** Flag for verbose output, true if {@code -Dverbose} specified */
  70     protected static final boolean IS_VERBOSE
  71             = System.getProperty("verbose") != null;
  72     /** count of invocation to triger compilation */
  73     protected static final int THRESHOLD;
  74     /** count of invocation to triger OSR compilation */
  75     protected static final long BACKEDGE_THRESHOLD;


  76     /** Value of {@code java.vm.info} (interpreted|mixed|comp mode) */
  77     protected static final String MODE = System.getProperty("java.vm.info");
  78 
  79     static {
  80         if (TIERED_COMPILATION) {

  81             BACKEDGE_THRESHOLD = THRESHOLD = 150000;
  82         } else {

  83             THRESHOLD = COMPILE_THRESHOLD;
  84             BACKEDGE_THRESHOLD = COMPILE_THRESHOLD * Long.parseLong(getVMOption(
  85                     "OnStackReplacePercentage"));
  86         }
  87     }
  88 
  89     /**
  90      * Returns value of VM option.
  91      *
  92      * @param name option's name
  93      * @return value of option or {@code null}, if option doesn't exist
  94      * @throws NullPointerException if name is null
  95      */
  96     protected static String getVMOption(String name) {
  97         Objects.requireNonNull(name);
  98         return Objects.toString(WHITE_BOX.getVMFlag(name), null);
  99     }
 100 
 101     /**
 102      * Returns value of VM option or default value.


 466             private final Helper helper = new Helper();
 467 
 468             @Override
 469             public Integer call() throws Exception {
 470                 return helper.method();
 471             }
 472         };
 473 
 474         private static final Callable<Integer> STATIC_CALLABLE
 475                 = new Callable<Integer>() {
 476             @Override
 477             public Integer call() throws Exception {
 478                 return staticMethod();
 479             }
 480         };
 481 
 482         private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
 483                 = new Callable<Integer>() {
 484             @Override
 485             public Integer call() throws Exception {
 486                 return new Helper(null).hashCode();

 487             }
 488         };
 489 
 490         private static final Callable<Integer> OSR_METHOD_CALLABLE
 491                 = new Callable<Integer>() {
 492             private final Helper helper = new Helper();
 493 
 494             @Override
 495             public Integer call() throws Exception {
 496                 return helper.osrMethod();

 497             }
 498         };
 499 
 500         private static final Callable<Integer> OSR_STATIC_CALLABLE
 501                 = new Callable<Integer>() {
 502             @Override
 503             public Integer call() throws Exception {
 504                 return osrStaticMethod();

 505             }
 506         };
 507 







































 508         private static final Constructor CONSTRUCTOR;
 509         private static final Constructor OSR_CONSTRUCTOR;
 510         private static final Method METHOD;
 511         private static final Method STATIC;
 512         private static final Method OSR_METHOD;
 513         private static final Method OSR_STATIC;
 514 
 515         static {
 516             try {
 517                 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
 518             } catch (NoSuchMethodException | SecurityException e) {
 519                 throw new RuntimeException(
 520                         "exception on getting method Helper.<init>(int)", e);
 521             }
 522             try {
 523                 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
 524                         Object.class);
 525             } catch (NoSuchMethodException | SecurityException e) {
 526                 throw new RuntimeException(
 527                         "exception on getting method Helper.<init>(Object)", e);
 528             }
 529             METHOD = getMethod("method");
 530             STATIC = getMethod("staticMethod");
 531             OSR_METHOD = getMethod("osrMethod");
 532             OSR_STATIC = getMethod("osrStaticMethod");
 533         }
 534 
 535         private static Method getMethod(String name) {
 536             try {
 537                 return Helper.class.getDeclaredMethod(name);
 538             } catch (NoSuchMethodException | SecurityException e) {
 539                 throw new RuntimeException(
 540                         "exception on getting method Helper." + name, e);
 541             }
 542 
 543         }
 544 
 545         private static int staticMethod() {
 546             return 1138;
 547         }
 548 
 549         private int method() {
 550             return 42;
 551         }
 552 
 553         private static int osrStaticMethod() {
 554             int result = 0;
 555             for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
 556                 result += staticMethod();
 557             }
 558             return result;
 559         }
 560 
 561         private int osrMethod() {
 562             int result = 0;
 563             for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
 564                 result += method();
 565             }
 566             return result;
 567         }
 568 
 569         private final int x;
 570 
 571         // for method and OSR method test case
 572         public Helper() {
 573             x = 0;
 574         }
 575 
 576         // for OSR constructor test case
 577         private Helper(Object o) {
 578             int result = 0;
 579             for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
 580                 result += method();
 581             }
 582             x = result;
 583         }
 584 
 585         // for constructor test case
 586         private Helper(int x) {
 587             this.x = x;
 588         }
 589 
 590         @Override
 591         public int hashCode() {
 592             return x;
 593         }
 594     }
 595 }


  52     /** Maximal value for CompLevel */
  53     protected static int COMP_LEVEL_MAX = COMP_LEVEL_FULL_OPTIMIZATION;
  54 
  55     /** Instance of WhiteBox */
  56     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  57     /** Value of {@code -XX:CompileThreshold} */
  58     protected static final int COMPILE_THRESHOLD
  59             = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
  60     /** Value of {@code -XX:BackgroundCompilation} */
  61     protected static final boolean BACKGROUND_COMPILATION
  62             = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
  63     /** Value of {@code -XX:TieredCompilation} */
  64     protected static final boolean TIERED_COMPILATION
  65             = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
  66     /** Value of {@code -XX:TieredStopAtLevel} */
  67     protected static final int TIERED_STOP_AT_LEVEL
  68             = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
  69     /** Flag for verbose output, true if {@code -Dverbose} specified */
  70     protected static final boolean IS_VERBOSE
  71             = System.getProperty("verbose") != null;
  72     /** invocation count to trigger compilation */
  73     protected static final int THRESHOLD;
  74     /** invocation count to trigger OSR compilation */
  75     protected static final long BACKEDGE_THRESHOLD;
  76     /** invocation count to warm up method before triggering OSR compilation */
  77     protected static final long OSR_WARMUP;
  78     /** Value of {@code java.vm.info} (interpreted|mixed|comp mode) */
  79     protected static final String MODE = System.getProperty("java.vm.info");
  80 
  81     static {
  82         if (TIERED_COMPILATION) {
  83             OSR_WARMUP = 200;
  84             BACKEDGE_THRESHOLD = THRESHOLD = 150000;
  85         } else {
  86             OSR_WARMUP = 2000;
  87             THRESHOLD = COMPILE_THRESHOLD;
  88             BACKEDGE_THRESHOLD = COMPILE_THRESHOLD * Long.parseLong(getVMOption(
  89                     "OnStackReplacePercentage"));
  90         }
  91     }
  92 
  93     /**
  94      * Returns value of VM option.
  95      *
  96      * @param name option's name
  97      * @return value of option or {@code null}, if option doesn't exist
  98      * @throws NullPointerException if name is null
  99      */
 100     protected static String getVMOption(String name) {
 101         Objects.requireNonNull(name);
 102         return Objects.toString(WHITE_BOX.getVMFlag(name), null);
 103     }
 104 
 105     /**
 106      * Returns value of VM option or default value.


 470             private final Helper helper = new Helper();
 471 
 472             @Override
 473             public Integer call() throws Exception {
 474                 return helper.method();
 475             }
 476         };
 477 
 478         private static final Callable<Integer> STATIC_CALLABLE
 479                 = new Callable<Integer>() {
 480             @Override
 481             public Integer call() throws Exception {
 482                 return staticMethod();
 483             }
 484         };
 485 
 486         private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
 487                 = new Callable<Integer>() {
 488             @Override
 489             public Integer call() throws Exception {
 490                 int result = warmup(OSR_CONSTRUCTOR);
 491                 return result + new Helper(null, CompilerWhiteBoxTest.BACKEDGE_THRESHOLD).hashCode();
 492             }
 493         };
 494 
 495         private static final Callable<Integer> OSR_METHOD_CALLABLE
 496                 = new Callable<Integer>() {
 497             private final Helper helper = new Helper();
 498 
 499             @Override
 500             public Integer call() throws Exception {
 501                 int result = warmup(OSR_METHOD);
 502                 return result + helper.osrMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
 503             }
 504         };
 505 
 506         private static final Callable<Integer> OSR_STATIC_CALLABLE
 507                 = new Callable<Integer>() {
 508             @Override
 509             public Integer call() throws Exception {
 510                 int result = warmup(OSR_STATIC);
 511                 return result + osrStaticMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
 512             }
 513         };
 514 
 515         /**
 516          * Executes the method multiple times to make sure we have
 517          * enough profiling information before triggering an OSR
 518          * compilation. Otherwise the C2 compiler may add uncommon traps.
 519          *
 520          * @param m Method to be executed
 521          * @return Number of times the method was executed
 522          * @throws Exception
 523          */
 524         private static int warmup(Method m) throws Exception {
 525             Helper helper = new Helper();
 526             int result = 0;
 527             for (long i = 0; i < CompilerWhiteBoxTest.OSR_WARMUP; ++i) {
 528                 result += (int)m.invoke(helper, 1);
 529             }
 530             // Make sure method is not (yet) compiled
 531             WhiteBox.getWhiteBox().deoptimizeMethod(m, false);
 532             return result;
 533         }
 534 
 535         /**
 536          * Executes the constructor multiple times to make sure we
 537          * have enough profiling information before triggering an OSR
 538          * compilation. Otherwise the C2 compiler may add uncommon traps.
 539          *
 540          * @param c Constructor to be executed
 541          * @return Number of times the constructor was executed
 542          * @throws Exception
 543          */
 544         private static int warmup(Constructor c) throws Exception {
 545             int result = 0;
 546             for (long i = 0; i < CompilerWhiteBoxTest.OSR_WARMUP; ++i) {
 547                 result += c.newInstance(null, 1).hashCode();
 548             }
 549             // Make sure method is not (yet) compiled
 550             WhiteBox.getWhiteBox().deoptimizeMethod(c, false);
 551             return result;
 552         }
 553 
 554         private static final Constructor CONSTRUCTOR;
 555         private static final Constructor OSR_CONSTRUCTOR;
 556         private static final Method METHOD;
 557         private static final Method STATIC;
 558         private static final Method OSR_METHOD;
 559         private static final Method OSR_STATIC;
 560 
 561         static {
 562             try {
 563                 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
 564             } catch (NoSuchMethodException | SecurityException e) {
 565                 throw new RuntimeException(
 566                         "exception on getting method Helper.<init>(int)", e);
 567             }
 568             try {
 569                 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
 570                         Object.class, long.class);
 571             } catch (NoSuchMethodException | SecurityException e) {
 572                 throw new RuntimeException(
 573                         "exception on getting method Helper.<init>(Object, long)", e);
 574             }
 575             METHOD = getMethod("method");
 576             STATIC = getMethod("staticMethod");
 577             OSR_METHOD = getMethod("osrMethod", long.class);
 578             OSR_STATIC = getMethod("osrStaticMethod", long.class);
 579         }
 580 
 581         private static Method getMethod(String name, Class<?>... parameterTypes) {
 582             try {
 583                 return Helper.class.getDeclaredMethod(name, parameterTypes);
 584             } catch (NoSuchMethodException | SecurityException e) {
 585                 throw new RuntimeException(
 586                         "exception on getting method Helper." + name, e);
 587             }

 588         }
 589 
 590         private static int staticMethod() {
 591             return 1138;
 592         }
 593 
 594         private int method() {
 595             return 42;
 596         }
 597 
 598         private static int osrStaticMethod(long limit) {
 599             int result = 0;
 600             for (long i = 0; i < limit; ++i) {
 601                 result += staticMethod();
 602             }
 603             return result;
 604         }
 605 
 606         private int osrMethod(long limit) {
 607             int result = 0;
 608             for (long i = 0; i < limit; ++i) {
 609                 result += method();
 610             }
 611             return result;
 612         }
 613 
 614         private final int x;
 615 
 616         // for method and OSR method test case
 617         public Helper() {
 618             x = 0;
 619         }
 620 
 621         // for OSR constructor test case
 622         private Helper(Object o, long limit) {
 623             int result = 0;
 624             for (long i = 0; i < limit; ++i) {
 625                 result += method();
 626             }
 627             x = result;
 628         }
 629 
 630         // for constructor test case
 631         private Helper(int x) {
 632             this.x = x;
 633         }
 634 
 635         @Override
 636         public int hashCode() {
 637             return x;
 638         }
 639     }
 640 }
test/compiler/whitebox/CompilerWhiteBoxTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File