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.HashMap;
  27 import java.util.List;
  28 import java.util.Map;
  29 
  30 import org.junit.Test;
  31 
  32 import org.graalvm.compiler.debug.Debug;
  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.CounterImpl;
  43 import org.graalvm.compiler.debug.internal.MemUseTrackerImpl;
  44 import org.graalvm.compiler.debug.internal.TimerImpl;
  45 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl;
  46 import org.graalvm.compiler.debug.internal.method.MethodMetricsPrinter;
  47 import org.graalvm.compiler.options.OptionValue;
  48 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  49 import org.graalvm.compiler.phases.Phase;
  50 
  51 import jdk.vm.ci.meta.ResolvedJavaMethod;
  52 
  53 // intercepting metrics
  54 public class MethodMetricsTestInterception01 extends MethodMetricsTest {
  55 
  56     @Override
  57     protected Phase additionalPhase() {
  58         return new MethodMetricPhases.CountingAddPhase();
  59     }
  60 
  61     @Override
  62     DebugConfig getConfig() {
  63         List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
  64         List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
  65         GraalDebugConfig debugConfig = new GraalDebugConfig(
  66                         GraalDebugConfig.Options.Log.getValue(),
  67                         "CountingAddPhase",
  68                         GraalDebugConfig.Options.TrackMemUse.getValue(),
  69                         "CountingAddPhase",
  70                         GraalDebugConfig.Options.Dump.getValue(),
  71                         GraalDebugConfig.Options.Verify.getValue(),
  72                         "MethodMetricsTest$TestApplication.*",
  73                         "CountingAddPhase",
  74                         System.out, dumpHandlers, verifyHandlers);
  75         return debugConfig;
  76     }
  77 
  78     @Override
  79     protected OverrideScope getOScope() {
  80         Map<OptionValue<?>, Object> mapping = new HashMap<>();
  81         mapping.put(MethodMetricsPrinter.Options.MethodMeterPrintAscii, true);
  82         return OptionValue.override(mapping);
  83     }
  84 
  85     private DebugValueFactory factory;
  86 
  87     @Test
  88     @Override
  89     @SuppressWarnings("try")
  90     public void test() throws Throwable {
  91         factory = Debug.getDebugValueFactory();
  92         Debug.setDebugValueFactory(new DebugValueFactory() {
  93             @Override
  94             public DebugTimer createTimer(String name, boolean conditional) {
  95                 return new TimerImpl(name, conditional, true);
  96             }
  97 
  98             @Override
  99             public DebugCounter createCounter(String name, boolean conditional) {
 100                 return CounterImpl.create(name, conditional, true);
 101             }
 102 
 103             @Override
 104             public DebugMethodMetrics createMethodMetrics(ResolvedJavaMethod method) {
 105                 return MethodMetricsImpl.getMethodMetrics(method);
 106             }
 107 
 108             @Override
 109             public DebugMemUseTracker createMemUseTracker(String name, boolean conditional) {
 110                 return new MemUseTrackerImpl(name, conditional, true);
 111             }
 112         });
 113         super.test();
 114 
 115     }
 116 
 117     @Override
 118     public void afterTest() {
 119         super.afterTest();
 120         Debug.setDebugValueFactory(factory);
 121     }
 122 
 123     @Override
 124     @SuppressWarnings("try")
 125     void assertValues() throws Throwable {
 126         assertValues("GlobalMetric", new long[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
 127     }
 128 }