< prev index next >

test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/Compiler.java

Print this page

 24 package sun.hotspot.tools.ctw;
 25 
 26 import jdk.internal.access.SharedSecrets;
 27 import jdk.internal.misc.Unsafe;
 28 import jdk.internal.reflect.ConstantPool;
 29 import jdk.test.whitebox.WhiteBox;
 30 
 31 import java.lang.reflect.Executable;
 32 import java.util.Arrays;
 33 import java.util.Objects;
 34 import java.util.concurrent.Executor;
 35 import java.util.concurrent.atomic.AtomicLong;
 36 import java.util.stream.Collectors;
 37 
 38 /**
 39  * Provide method to compile whole class.
 40  * Also contains compiled methods and classes counters.
 41  */
 42 public class Compiler {
 43 





 44     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 45     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 46     private static final AtomicLong METHOD_COUNT = new AtomicLong(0L);

 47 
 48     private Compiler() { }
 49 
 50     /**
 51      * @return count of processed methods
 52      */
 53     public static long getMethodCount() {
 54         return METHOD_COUNT.get();
 55     }
 56 
 57     /**
 58      * Compiles all methods and constructors.
 59      *
 60      * @param aClass class to compile
 61      * @param id an id of the class
 62      * @param executor executor used for compile task invocation
 63      * @throws NullPointerException if {@code class} or {@code executor}
 64      *                              is {@code null}
 65      */
 66     public static void compileClass(Class<?> aClass, long id, Executor executor) {
 67         Objects.requireNonNull(aClass);
 68         Objects.requireNonNull(executor);
 69         ConstantPool constantPool = SharedSecrets.getJavaLangAccess().
 70                 getConstantPool(aClass);
 71         if (Utils.COMPILE_THE_WORLD_PRELOAD_CLASSES) {
 72             preloadClasses(aClass.getName(), id, constantPool);
 73         }
 74         UNSAFE.ensureClassInitialized(aClass);
 75         compileClinit(aClass, id);
 76         long methodCount = 0;
 77         for (Executable e : aClass.getDeclaredConstructors()) {
 78             ++methodCount;
 79             executor.execute(new CompileMethodCommand(id, e));
 80         }
 81         for (Executable e : aClass.getDeclaredMethods()) {
 82             ++methodCount;
 83             executor.execute(new CompileMethodCommand(id, e));
 84         }
 85         METHOD_COUNT.addAndGet(methodCount);















 86     }
 87 
 88     private static void preloadClasses(String className, long id,
 89             ConstantPool constantPool) {
 90         try {
 91             for (int i = 0, n = constantPool.getSize(); i < n; ++i) {
 92                 try {
 93                     constantPool.getClassAt(i);
 94                 } catch (IllegalArgumentException ignore) {
 95                 }
 96             }
 97         } catch (Throwable t) {
 98             CompileTheWorld.OUT.println(String.format("[%d]\t%s\tWARNING preloading failed : %s",
 99                     id, className, t));
100             t.printStackTrace(CompileTheWorld.ERR);
101         }
102     }
103 
104     private static void compileClinit(Class<?> aClass, long id) {
105         int startLevel = Utils.INITIAL_COMP_LEVEL;

 24 package sun.hotspot.tools.ctw;
 25 
 26 import jdk.internal.access.SharedSecrets;
 27 import jdk.internal.misc.Unsafe;
 28 import jdk.internal.reflect.ConstantPool;
 29 import jdk.test.whitebox.WhiteBox;
 30 
 31 import java.lang.reflect.Executable;
 32 import java.util.Arrays;
 33 import java.util.Objects;
 34 import java.util.concurrent.Executor;
 35 import java.util.concurrent.atomic.AtomicLong;
 36 import java.util.stream.Collectors;
 37 
 38 /**
 39  * Provide method to compile whole class.
 40  * Also contains compiled methods and classes counters.
 41  */
 42 public class Compiler {
 43 
 44     // Call GC after compiling as many methods. This would remove the stale methods.
 45     // This threshold should balance the GC overhead and the cost of keeping lots
 46     // of stale methods around.
 47     private static final long GC_METHOD_THRESHOLD = Long.getLong("gcMethodThreshold", 100);
 48 
 49     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 50     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 51     private static final AtomicLong METHOD_COUNT = new AtomicLong();
 52     private static final AtomicLong METHODS_SINCE_LAST_GC = new AtomicLong();
 53 
 54     private Compiler() { }
 55 
 56     /**
 57      * @return count of processed methods
 58      */
 59     public static long getMethodCount() {
 60         return METHOD_COUNT.get();
 61     }
 62 
 63     /**
 64      * Compiles all methods and constructors.
 65      *
 66      * @param aClass class to compile
 67      * @param id an id of the class
 68      * @param executor executor used for compile task invocation
 69      * @throws NullPointerException if {@code class} or {@code executor}
 70      *                              is {@code null}
 71      */
 72     public static void compileClass(Class<?> aClass, long id, Executor executor) {
 73         Objects.requireNonNull(aClass);
 74         Objects.requireNonNull(executor);
 75         ConstantPool constantPool = SharedSecrets.getJavaLangAccess().
 76                 getConstantPool(aClass);
 77         if (Utils.COMPILE_THE_WORLD_PRELOAD_CLASSES) {
 78             preloadClasses(aClass.getName(), id, constantPool);
 79         }
 80         UNSAFE.ensureClassInitialized(aClass);
 81         compileClinit(aClass, id);
 82         long methodCount = 0;
 83         for (Executable e : aClass.getDeclaredConstructors()) {
 84             ++methodCount;
 85             executor.execute(new CompileMethodCommand(id, e));
 86         }
 87         for (Executable e : aClass.getDeclaredMethods()) {
 88             ++methodCount;
 89             executor.execute(new CompileMethodCommand(id, e));
 90         }
 91         METHOD_COUNT.addAndGet(methodCount);
 92 
 93         // See if we need to schedule a GC
 94         while (true) {
 95             long current = METHODS_SINCE_LAST_GC.get();
 96             long update = current + methodCount;
 97             if (update >= GC_METHOD_THRESHOLD) {
 98                 update = 0;
 99             }
100             if (METHODS_SINCE_LAST_GC.compareAndSet(current, update)) {
101                 if (update == 0) {
102                     executor.execute(() -> System.gc());
103                 }
104                 break;
105             }
106         }
107     }
108 
109     private static void preloadClasses(String className, long id,
110             ConstantPool constantPool) {
111         try {
112             for (int i = 0, n = constantPool.getSize(); i < n; ++i) {
113                 try {
114                     constantPool.getClassAt(i);
115                 } catch (IllegalArgumentException ignore) {
116                 }
117             }
118         } catch (Throwable t) {
119             CompileTheWorld.OUT.println(String.format("[%d]\t%s\tWARNING preloading failed : %s",
120                     id, className, t));
121             t.printStackTrace(CompileTheWorld.ERR);
122         }
123     }
124 
125     private static void compileClinit(Class<?> aClass, long id) {
126         int startLevel = Utils.INITIAL_COMP_LEVEL;
< prev index next >