| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class Main { |
| 18 |
public static void main(String[] args) throws Exception { |
| 19 |
System.loadLibrary(args[0]); |
| 20 |
if (isAotCompiled(Main.class, "hasJit")) { |
| 21 |
throw new Error("This test must be run with --no-prebuild!"); |
| 22 |
} |
| 23 |
if (!hasJit()) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
testCompilationUseAndCollection(); |
| 28 |
testMixedFramesOnStack(); |
| 29 |
} |
| 30 |
|
| 31 |
public static void testCompilationUseAndCollection() { |
| 32 |
|
| 33 |
assertFalse(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 34 |
assertFalse(hasJitCompiledCode(Main.class, "callThrough")); |
| 35 |
ensureCompiledCallThroughEntrypoint( true); |
| 36 |
assertTrue(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 37 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 38 |
|
| 39 |
|
| 40 |
callThrough(Main.class, "doNothing"); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
callThrough(Main.class, "testGcWithCallThroughStubOnStack"); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
testSingleInvocationTriggersRecompilation(); |
| 49 |
|
| 50 |
|
| 51 |
testStubCanBeCollected(); |
| 52 |
} |
| 53 |
|
| 54 |
public static void testGcWithCallThroughStubOnStack() { |
| 55 |
|
| 56 |
assertTrue(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 57 |
|
| 58 |
assertTrue(new Throwable().getStackTrace()[1].getMethodName().equals("callThrough")); |
| 59 |
|
| 60 |
doJitGcsUntilFullJitGcIsScheduled(); |
| 61 |
|
| 62 |
|
| 63 |
jitGc(); |
| 64 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 65 |
} |
| 66 |
|
| 67 |
public static void testSingleInvocationTriggersRecompilation() { |
| 68 |
|
| 69 |
|
| 70 |
doJitGcsUntilFullJitGcIsScheduled(); |
| 71 |
callThrough(Main.class, "doNothing"); |
| 72 |
ensureCompiledCallThroughEntrypoint( false); |
| 73 |
assertTrue(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 74 |
jitGc(); |
| 75 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 76 |
} |
| 77 |
|
| 78 |
public static void testMixedFramesOnStack() { |
| 79 |
|
| 80 |
assertFalse(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 81 |
assertFalse(hasJitCompiledCode(Main.class, "callThrough")); |
| 82 |
callThrough(Main.class, "testMixedFramesOnStackStage2"); |
| 83 |
|
| 84 |
|
| 85 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 86 |
|
| 87 |
|
| 88 |
testStubCanBeCollected(); |
| 89 |
} |
| 90 |
|
| 91 |
public static void testMixedFramesOnStackStage2() { |
| 92 |
|
| 93 |
|
| 94 |
assertTrue(new Throwable().getStackTrace()[1].getMethodName().equals("callThrough")); |
| 95 |
|
| 96 |
ensureCompiledCallThroughEntrypoint( true); |
| 97 |
callThrough(Main.class, "testMixedFramesOnStackStage3"); |
| 98 |
} |
| 99 |
|
| 100 |
public static void testMixedFramesOnStackStage3() { |
| 101 |
|
| 102 |
assertTrue(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 103 |
|
| 104 |
assertTrue(new Throwable().getStackTrace()[1].getMethodName().equals("callThrough")); |
| 105 |
|
| 106 |
jitGc(); |
| 107 |
} |
| 108 |
|
| 109 |
public static void testStubCanBeCollected() { |
| 110 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 111 |
doJitGcsUntilFullJitGcIsScheduled(); |
| 112 |
assertFalse(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 113 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 114 |
jitGc(); |
| 115 |
assertFalse(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 116 |
assertFalse(hasJitCompiledCode(Main.class, "callThrough")); |
| 117 |
} |
| 118 |
|
| 119 |
public static void doJitGcsUntilFullJitGcIsScheduled() { |
| 120 |
|
| 121 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 122 |
ensureCompiledCallThroughEntrypoint( true); |
| 123 |
|
| 124 |
do { |
| 125 |
assertTrue(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 126 |
callThrough(Main.class, "jitGc"); |
| 127 |
} while (!isNextJitGcFull()); |
| 128 |
|
| 129 |
|
| 130 |
assertFalse(hasJitCompiledEntrypoint(Main.class, "callThrough")); |
| 131 |
assertTrue(hasJitCompiledCode(Main.class, "callThrough")); |
| 132 |
} |
| 133 |
|
| 134 |
public static void ensureCompiledCallThroughEntrypoint(boolean call) { |
| 135 |
int count = 0; |
| 136 |
while (!hasJitCompiledEntrypoint(Main.class, "callThrough")) { |
| 137 |
|
| 138 |
|
| 139 |
final int rampUpCutOff = 12; |
| 140 |
int limit = call ? 1 << Math.min(count, rampUpCutOff) : 0; |
| 141 |
for (int i = 0; i < limit; ++i) { |
| 142 |
callThrough(Main.class, "doNothing"); |
| 143 |
} |
| 144 |
try { |
| 145 |
|
| 146 |
|
| 147 |
Thread.sleep(count >= rampUpCutOff ? 200 : 100); |
| 148 |
} catch (Exception e) { |
| 149 |
|
| 150 |
} |
| 151 |
if (++count == 50) { |
| 152 |
throw new Error("TIMEOUT"); |
| 153 |
} |
| 154 |
}; |
| 155 |
} |
| 156 |
|
| 157 |
public static void assertTrue(boolean value) { |
| 158 |
if (!value) { |
| 159 |
throw new AssertionError("Expected true!"); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public static void assertFalse(boolean value) { |
| 164 |
if (value) { |
| 165 |
throw new AssertionError("Expected false!"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public static void doNothing() { } |
| 170 |
public static void throwError() { throw new Error(); } |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public native static void callThrough(Class<?> cls, String methodName); |
| 175 |
|
| 176 |
public native static void jitGc(); |
| 177 |
public native static boolean isNextJitGcFull(); |
| 178 |
|
| 179 |
public native static boolean isAotCompiled(Class<?> cls, String methodName); |
| 180 |
public native static boolean hasJitCompiledEntrypoint(Class<?> cls, String methodName); |
| 181 |
public native static boolean hasJitCompiledCode(Class<?> cls, String methodName); |
| 182 |
private native static boolean hasJit(); |
| 183 |
} |