1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 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.util.ArrayList; 26 import java.util.List; 27 import java.util.concurrent.TimeUnit; 28 29 import org.junit.Test; 30 31 import org.graalvm.compiler.debug.Debug; 32 import org.graalvm.compiler.debug.DebugCloseable; 33 import org.graalvm.compiler.debug.DebugConfig; 34 import org.graalvm.compiler.debug.DebugCounter; 35 import org.graalvm.compiler.debug.DebugDumpHandler; 36 import org.graalvm.compiler.debug.DebugMemUseTracker; 37 import org.graalvm.compiler.debug.DebugMethodMetrics; 38 import org.graalvm.compiler.debug.DebugTimer; 39 import org.graalvm.compiler.debug.DebugValueFactory; 40 import org.graalvm.compiler.debug.DebugVerifyHandler; 41 import org.graalvm.compiler.debug.GraalDebugConfig; 42 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl; 43 import org.graalvm.compiler.options.OptionValues; 44 import org.graalvm.compiler.phases.Phase; 45 46 import jdk.vm.ci.meta.ResolvedJavaMethod; 47 48 // intercepting metrics 49 public class MethodMetricsTestInterception02 extends MethodMetricsTest { 50 51 @Override 52 protected Phase additionalPhase() { 53 return new MethodMetricPhases.ScopeTestPhase(); 54 } 55 56 private DebugValueFactory factory; 57 58 public void setFactory() { 59 /* 60 * setting a custom debug value factory creating a constant timer for checking scope 61 * creation and inlining scopes with metric intercepting works 62 */ 63 factory = Debug.getDebugValueFactory(); 64 Debug.setDebugValueFactory(new DebugValueFactory() { 65 @Override 66 public DebugTimer createTimer(String name, boolean conditional) { 67 // can still use together with real timer 68 // TimerImpl realTimer = new TimerImpl(name, conditional, true); 69 return new DebugTimer() { 70 int runs = 0; 71 72 // private DebugCloseable t; 73 74 @Override 75 public DebugCloseable start() { 76 // t = realTimer.start(); 77 return new DebugCloseable() { 78 @Override 79 public void close() { 80 // t.close(); 81 runs++; 82 MethodMetricsImpl.addToCurrentScopeMethodMetrics(name, 1); 83 } 84 }; 85 } 86 87 @Override 88 public void setConditional(boolean flag) { 89 90 } 91 92 @Override 93 public boolean isConditional() { 94 return false; 95 } 96 97 @Override 98 public TimeUnit getTimeUnit() { 99 return TimeUnit.MILLISECONDS; 100 } 101 102 @Override 103 public long getCurrentValue() { 104 return runs; 105 } 106 }; 107 } 108 109 @Override 110 public DebugCounter createCounter(String name, boolean conditional) { 111 return factory.createCounter(name, conditional); 112 } 113 114 @Override 115 public DebugMethodMetrics createMethodMetrics(ResolvedJavaMethod method) { 116 return factory.createMethodMetrics(method); 117 } 118 119 @Override 120 public DebugMemUseTracker createMemUseTracker(String name, boolean conditional) { 121 return factory.createMemUseTracker(name, conditional); 122 } 123 }); 124 } 125 126 @Test 127 @Override 128 public void test() throws Throwable { 129 setFactory(); 130 super.test(); 131 } 132 133 @Override 134 public void afterTest() { 135 super.afterTest(); 136 Debug.setDebugValueFactory(factory); 137 } 138 139 @Override 140 DebugConfig getConfig() { 141 List<DebugDumpHandler> dumpHandlers = new ArrayList<>(); 142 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>(); 143 OptionValues options = getInitialOptions(); 144 GraalDebugConfig debugConfig = new GraalDebugConfig( 145 options, 146 GraalDebugConfig.Options.Log.getValue(options), 147 ""/* unscoped meter */, 148 GraalDebugConfig.Options.TrackMemUse.getValue(options), 149 ""/* unscoped time */, 150 GraalDebugConfig.Options.Dump.getValue(options), 151 GraalDebugConfig.Options.Verify.getValue(options), 152 null /* no method filter */, 153 "" /* unscoped method metering */, 154 System.out, dumpHandlers, verifyHandlers); 155 return debugConfig; 156 } 157 158 @Override 159 @SuppressWarnings("try") 160 void assertValues() throws Throwable { 161 assertValues("GlobalTimer4_WithoutInlineEnhancement", new long[]{50, 50, 50, 50, 50, 50, 50, 50, 50, 50}); 162 } 163 164 }