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