9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.core.test.debug; 24 25 import java.io.PrintStream; 26 import java.lang.reflect.Field; 27 import java.lang.reflect.Method; 28 import java.util.ArrayList; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.ListIterator; 32 import java.util.Map; 33 import java.util.stream.Collectors; 34 35 import org.junit.After; 36 import org.junit.Assert; 37 import org.junit.Before; 38 import org.junit.Test; 39 40 import org.graalvm.compiler.core.test.GraalCompilerTest; 41 import org.graalvm.compiler.debug.Debug; 42 import org.graalvm.compiler.debug.DebugCloseable; 43 import org.graalvm.compiler.debug.DebugConfig; 44 import org.graalvm.compiler.debug.DebugConfigScope; 45 import org.graalvm.compiler.debug.DebugCounter; 46 import org.graalvm.compiler.debug.DebugDumpHandler; 47 import org.graalvm.compiler.debug.DebugMethodMetrics; 48 import org.graalvm.compiler.debug.DebugTimer; 49 import org.graalvm.compiler.debug.DebugVerifyHandler; 50 import org.graalvm.compiler.debug.DelegatingDebugConfig; 51 import org.graalvm.compiler.debug.DelegatingDebugConfig.Feature; 52 import org.graalvm.compiler.debug.GraalDebugConfig; 53 import org.graalvm.compiler.debug.internal.DebugScope; 54 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl; 55 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl.CompilationData; 56 import org.graalvm.compiler.debug.internal.method.MethodMetricsInlineeScopeInfo; 57 import org.graalvm.compiler.debug.internal.method.MethodMetricsPrinter; 58 import org.graalvm.compiler.nodes.InvokeNode; 59 import org.graalvm.compiler.nodes.StructuredGraph; 60 import org.graalvm.compiler.nodes.calc.BinaryNode; 61 import org.graalvm.compiler.nodes.calc.FixedBinaryNode; 62 import org.graalvm.compiler.nodes.calc.MulNode; 63 import org.graalvm.compiler.nodes.calc.ShiftNode; 64 import org.graalvm.compiler.nodes.calc.SignedDivNode; 65 import org.graalvm.compiler.nodes.calc.SubNode; 66 import org.graalvm.compiler.options.OptionValue; 67 import org.graalvm.compiler.options.OptionValue.OverrideScope; 68 import org.graalvm.compiler.phases.BasePhase; 69 import org.graalvm.compiler.phases.Phase; 70 import org.graalvm.compiler.phases.PhaseSuite; 71 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 72 import org.graalvm.compiler.phases.common.ConvertDeoptimizeToGuardPhase; 73 import org.graalvm.compiler.phases.schedule.SchedulePhase; 74 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy; 75 import org.graalvm.compiler.phases.tiers.HighTierContext; 76 import org.graalvm.compiler.phases.tiers.Suites; 77 78 import jdk.vm.ci.meta.ResolvedJavaMethod; 79 80 public abstract class MethodMetricsTest extends GraalCompilerTest { 81 static class TestApplication { 82 public static int m01(int x, int y) { 83 return x + y; 84 } 85 86 public static int m02(int x, int y) { 87 return x * y; 224 try (DebugCloseable c2 = scopedTimer1.start()) { 225 try (DebugCloseable c3 = scopedScopedTimer1.start()) { 226 // do sth unnecessary long allocating many inlinee scopes 227 for (int i = 0; i < 50; i++) { 228 try (DebugCloseable c4 = scopedScopedScopeTimer1.start()) { 229 new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS).apply(graph); 230 new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS).apply(graph); 231 } 232 } 233 } 234 } 235 } 236 } 237 238 } 239 } 240 241 static DebugConfig overrideGraalDebugConfig(PrintStream log, String methodFilter, String methodMeter) { 242 List<DebugDumpHandler> dumpHandlers = new ArrayList<>(); 243 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>(); 244 GraalDebugConfig debugConfig = new GraalDebugConfig( 245 GraalDebugConfig.Options.Log.getValue(), 246 GraalDebugConfig.Options.Count.getValue(), 247 GraalDebugConfig.Options.TrackMemUse.getValue(), 248 GraalDebugConfig.Options.Time.getValue(), 249 GraalDebugConfig.Options.Dump.getValue(), 250 GraalDebugConfig.Options.Verify.getValue(), 251 methodFilter, 252 methodMeter, 253 log, dumpHandlers, verifyHandlers); 254 return debugConfig; 255 } 256 257 private static OverrideScope overrideMetricPrinterConfig() { 258 Map<OptionValue<?>, Object> mapping = new HashMap<>(); 259 mapping.put(MethodMetricsPrinter.Options.MethodMeterPrintAscii, true); 260 return OptionValue.override(mapping); 261 } 262 263 abstract Phase additionalPhase(); 264 265 @Override 266 protected Suites createSuites() { 267 Suites ret = super.createSuites(); 268 ListIterator<BasePhase<? super HighTierContext>> iter = ret.getHighTier().findPhase(ConvertDeoptimizeToGuardPhase.class, true); 269 PhaseSuite.findNextPhase(iter, CanonicalizerPhase.class); 270 iter.add(additionalPhase()); 271 return ret; 272 } 273 274 @Test 275 @SuppressWarnings("try") 276 public void test() throws Throwable { 277 try (DebugConfigScope s = Debug.setConfig(getConfig()); OverrideScope o = getOScope();) { 278 executeMethod(TestApplication.class.getMethod("m01", testSignature), null, testArgs); 279 executeMethod(TestApplication.class.getMethod("m02", testSignature), null, testArgs); 280 executeMethod(TestApplication.class.getMethod("m03", testSignature), null, testArgs); 281 executeMethod(TestApplication.class.getMethod("m04", testSignature), null, testArgs); 282 executeMethod(TestApplication.class.getMethod("m05", testSignature), null, testArgs); 283 executeMethod(TestApplication.class.getMethod("m06", testSignature), null, testArgs); 284 executeMethod(TestApplication.class.getMethod("m07", testSignature), null, testArgs); 285 executeMethod(TestApplication.class.getMethod("m08", testSignature), null, testArgs); 286 executeMethod(TestApplication.class.getMethod("m09", testSignature), null, testArgs); 287 executeMethod(TestApplication.class.getMethod("m10", testSignature), null, testArgs); 288 assertValues(); 289 } 290 } 291 292 @Before 293 public void rememberScopeId() { 294 scopeIdBeforeAccess = DebugScope.getCurrentGlobalScopeId(); 295 } 296 297 @After 298 public void clearMMCache() { 299 MethodMetricsImpl.clearMM(); 300 } 301 302 abstract DebugConfig getConfig(); 303 304 OverrideScope getOScope() { 305 return overrideMetricPrinterConfig(); 306 } 307 308 abstract void assertValues() throws Throwable; 309 310 @SuppressWarnings("unchecked") 311 private static Map<ResolvedJavaMethod, CompilationData> readMethodMetricsImplData() { 312 Map<ResolvedJavaMethod, CompilationData> threadLocalMap = null; 313 for (Field f : MethodMetricsImpl.class.getDeclaredFields()) { 314 if (f.getName().equals("threadEntries")) { 315 f.setAccessible(true); 316 Object map; 317 try { 318 map = ((ThreadLocal<?>) f.get(null)).get(); 319 } catch (Throwable t) { 320 throw new RuntimeException(t); 321 } 322 threadLocalMap = (Map<ResolvedJavaMethod, CompilationData>) map; 323 break; 324 } 325 } 326 return threadLocalMap; 327 } 328 329 private long scopeIdBeforeAccess; 330 private long scopeIdAfterAccess; 331 332 protected long readValFromCurrThread(ResolvedJavaMethod method, String metricName) { 333 334 Map<ResolvedJavaMethod, CompilationData> threadLocalMap = readMethodMetricsImplData(); 335 assert threadLocalMap != null; 356 return res != null ? res : 0; 357 } 358 359 @SuppressWarnings("try") 360 void assertValues(String metricName, long[] vals) { 361 scopeIdAfterAccess = DebugScope.getCurrentGlobalScopeId(); 362 try (DebugConfigScope s = Debug.setConfig(new DelegatingDebugConfig().enable(Feature.METHOD_METRICS))) { 363 Assert.assertEquals(vals[0], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m01", testSignature)), metricName)); 364 Assert.assertEquals(vals[1], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m02", testSignature)), metricName)); 365 Assert.assertEquals(vals[2], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m03", testSignature)), metricName)); 366 Assert.assertEquals(vals[3], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m04", testSignature)), metricName)); 367 Assert.assertEquals(vals[4], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m05", testSignature)), metricName)); 368 Assert.assertEquals(vals[5], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m06", testSignature)), metricName)); 369 Assert.assertEquals(vals[6], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m07", testSignature)), metricName)); 370 Assert.assertEquals(vals[7], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m08", testSignature)), metricName)); 371 Assert.assertEquals(vals[8], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m09", testSignature)), metricName)); 372 } catch (Throwable t) { 373 throw new RuntimeException(t); 374 } 375 } 376 377 void executeMethod(Method m, Object receiver, Object... args) { 378 test(asResolvedJavaMethod(m), receiver, args); 379 } 380 381 } | 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.core.test.debug; 24 25 import java.io.PrintStream; 26 import java.lang.reflect.Field; 27 import java.lang.reflect.Method; 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.ListIterator; 31 import java.util.Map; 32 import java.util.stream.Collectors; 33 34 import org.junit.After; 35 import org.junit.Assert; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.graalvm.compiler.core.common.util.Util; 39 import org.graalvm.compiler.core.test.GraalCompilerTest; 40 import org.graalvm.compiler.debug.Debug; 41 import org.graalvm.compiler.debug.DebugCloseable; 42 import org.graalvm.compiler.debug.DebugConfig; 43 import org.graalvm.compiler.debug.DebugConfigScope; 44 import org.graalvm.compiler.debug.DebugCounter; 45 import org.graalvm.compiler.debug.DebugDumpHandler; 46 import org.graalvm.compiler.debug.DebugMethodMetrics; 47 import org.graalvm.compiler.debug.DebugTimer; 48 import org.graalvm.compiler.debug.DebugVerifyHandler; 49 import org.graalvm.compiler.debug.DelegatingDebugConfig; 50 import org.graalvm.compiler.debug.DelegatingDebugConfig.Feature; 51 import org.graalvm.compiler.debug.GraalDebugConfig; 52 import org.graalvm.compiler.debug.internal.DebugScope; 53 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl; 54 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl.CompilationData; 55 import org.graalvm.compiler.debug.internal.method.MethodMetricsInlineeScopeInfo; 56 import org.graalvm.compiler.debug.internal.method.MethodMetricsPrinter; 57 import org.graalvm.compiler.nodes.InvokeNode; 58 import org.graalvm.compiler.nodes.StructuredGraph; 59 import org.graalvm.compiler.nodes.calc.BinaryNode; 60 import org.graalvm.compiler.nodes.calc.FixedBinaryNode; 61 import org.graalvm.compiler.nodes.calc.MulNode; 62 import org.graalvm.compiler.nodes.calc.ShiftNode; 63 import org.graalvm.compiler.nodes.calc.SignedDivNode; 64 import org.graalvm.compiler.nodes.calc.SubNode; 65 import org.graalvm.compiler.options.OptionValues; 66 import org.graalvm.compiler.phases.BasePhase; 67 import org.graalvm.compiler.phases.Phase; 68 import org.graalvm.compiler.phases.PhaseSuite; 69 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 70 import org.graalvm.compiler.phases.common.ConvertDeoptimizeToGuardPhase; 71 import org.graalvm.compiler.phases.schedule.SchedulePhase; 72 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy; 73 import org.graalvm.compiler.phases.tiers.HighTierContext; 74 import org.graalvm.compiler.phases.tiers.Suites; 75 76 import jdk.vm.ci.meta.ResolvedJavaMethod; 77 78 public abstract class MethodMetricsTest extends GraalCompilerTest { 79 static class TestApplication { 80 public static int m01(int x, int y) { 81 return x + y; 82 } 83 84 public static int m02(int x, int y) { 85 return x * y; 222 try (DebugCloseable c2 = scopedTimer1.start()) { 223 try (DebugCloseable c3 = scopedScopedTimer1.start()) { 224 // do sth unnecessary long allocating many inlinee scopes 225 for (int i = 0; i < 50; i++) { 226 try (DebugCloseable c4 = scopedScopedScopeTimer1.start()) { 227 new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS).apply(graph); 228 new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS).apply(graph); 229 } 230 } 231 } 232 } 233 } 234 } 235 236 } 237 } 238 239 static DebugConfig overrideGraalDebugConfig(PrintStream log, String methodFilter, String methodMeter) { 240 List<DebugDumpHandler> dumpHandlers = new ArrayList<>(); 241 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>(); 242 OptionValues options = getInitialOptions(); 243 GraalDebugConfig debugConfig = new GraalDebugConfig( 244 options, 245 GraalDebugConfig.Options.Log.getValue(options), 246 GraalDebugConfig.Options.Count.getValue(options), 247 GraalDebugConfig.Options.TrackMemUse.getValue(options), 248 GraalDebugConfig.Options.Time.getValue(options), 249 GraalDebugConfig.Options.Dump.getValue(options), 250 GraalDebugConfig.Options.Verify.getValue(options), 251 methodFilter, 252 methodMeter, 253 log, dumpHandlers, verifyHandlers); 254 return debugConfig; 255 } 256 257 abstract Phase additionalPhase(); 258 259 @Override 260 protected Suites createSuites(OptionValues options) { 261 Suites ret = super.createSuites(options); 262 ListIterator<BasePhase<? super HighTierContext>> iter = ret.getHighTier().findPhase(ConvertDeoptimizeToGuardPhase.class, true); 263 PhaseSuite.findNextPhase(iter, CanonicalizerPhase.class); 264 iter.add(additionalPhase()); 265 return ret; 266 } 267 268 @Test 269 @SuppressWarnings("try") 270 public void test() throws Throwable { 271 try (DebugConfigScope s = Debug.setConfig(getConfig())) { 272 executeMethod(TestApplication.class.getMethod("m01", testSignature), null, testArgs); 273 executeMethod(TestApplication.class.getMethod("m02", testSignature), null, testArgs); 274 executeMethod(TestApplication.class.getMethod("m03", testSignature), null, testArgs); 275 executeMethod(TestApplication.class.getMethod("m04", testSignature), null, testArgs); 276 executeMethod(TestApplication.class.getMethod("m05", testSignature), null, testArgs); 277 executeMethod(TestApplication.class.getMethod("m06", testSignature), null, testArgs); 278 executeMethod(TestApplication.class.getMethod("m07", testSignature), null, testArgs); 279 executeMethod(TestApplication.class.getMethod("m08", testSignature), null, testArgs); 280 executeMethod(TestApplication.class.getMethod("m09", testSignature), null, testArgs); 281 executeMethod(TestApplication.class.getMethod("m10", testSignature), null, testArgs); 282 assertValues(); 283 } 284 } 285 286 void executeMethod(Method m, Object receiver, Object... args) { 287 OptionValues options = new OptionValues(getInitialOptions(), MethodMetricsPrinter.Options.MethodMeterPrintAscii, true); 288 test(options, asResolvedJavaMethod(m), receiver, args); 289 } 290 291 @Before 292 public void rememberScopeId() { 293 scopeIdBeforeAccess = DebugScope.getCurrentGlobalScopeId(); 294 } 295 296 @After 297 public void clearMMCache() { 298 MethodMetricsImpl.clearMM(); 299 } 300 301 abstract DebugConfig getConfig(); 302 303 abstract void assertValues() throws Throwable; 304 305 @SuppressWarnings("unchecked") 306 private static Map<ResolvedJavaMethod, CompilationData> readMethodMetricsImplData() { 307 Map<ResolvedJavaMethod, CompilationData> threadLocalMap = null; 308 for (Field f : MethodMetricsImpl.class.getDeclaredFields()) { 309 if (f.getName().equals("threadEntries")) { 310 Util.setAccessible(f, true); 311 Object map; 312 try { 313 map = ((ThreadLocal<?>) f.get(null)).get(); 314 } catch (Throwable t) { 315 throw new RuntimeException(t); 316 } 317 threadLocalMap = (Map<ResolvedJavaMethod, CompilationData>) map; 318 break; 319 } 320 } 321 return threadLocalMap; 322 } 323 324 private long scopeIdBeforeAccess; 325 private long scopeIdAfterAccess; 326 327 protected long readValFromCurrThread(ResolvedJavaMethod method, String metricName) { 328 329 Map<ResolvedJavaMethod, CompilationData> threadLocalMap = readMethodMetricsImplData(); 330 assert threadLocalMap != null; 351 return res != null ? res : 0; 352 } 353 354 @SuppressWarnings("try") 355 void assertValues(String metricName, long[] vals) { 356 scopeIdAfterAccess = DebugScope.getCurrentGlobalScopeId(); 357 try (DebugConfigScope s = Debug.setConfig(new DelegatingDebugConfig().enable(Feature.METHOD_METRICS))) { 358 Assert.assertEquals(vals[0], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m01", testSignature)), metricName)); 359 Assert.assertEquals(vals[1], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m02", testSignature)), metricName)); 360 Assert.assertEquals(vals[2], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m03", testSignature)), metricName)); 361 Assert.assertEquals(vals[3], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m04", testSignature)), metricName)); 362 Assert.assertEquals(vals[4], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m05", testSignature)), metricName)); 363 Assert.assertEquals(vals[5], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m06", testSignature)), metricName)); 364 Assert.assertEquals(vals[6], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m07", testSignature)), metricName)); 365 Assert.assertEquals(vals[7], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m08", testSignature)), metricName)); 366 Assert.assertEquals(vals[8], readValFromCurrThread(asResolvedJavaMethod(TestApplication.class.getMethod("m09", testSignature)), metricName)); 367 } catch (Throwable t) { 368 throw new RuntimeException(t); 369 } 370 } 371 } |