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 */
23package org.graalvm.compiler.core.test.debug;
24
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.List;
28import java.util.Map;
29import java.util.concurrent.TimeUnit;
30
31import org.junit.Test;
32
33import org.graalvm.compiler.debug.Debug;
34import org.graalvm.compiler.debug.DebugCloseable;
35import org.graalvm.compiler.debug.DebugConfig;
36import org.graalvm.compiler.debug.DebugCounter;
37import org.graalvm.compiler.debug.DebugDumpHandler;
38import org.graalvm.compiler.debug.DebugMemUseTracker;
39import org.graalvm.compiler.debug.DebugMethodMetrics;
40import org.graalvm.compiler.debug.DebugTimer;
41import org.graalvm.compiler.debug.DebugValueFactory;
42import org.graalvm.compiler.debug.DebugVerifyHandler;
43import org.graalvm.compiler.debug.GraalDebugConfig;
44import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl;
45import org.graalvm.compiler.debug.internal.method.MethodMetricsPrinter;
46import org.graalvm.compiler.options.OptionValue;
47import org.graalvm.compiler.options.OptionValue.OverrideScope;
48import org.graalvm.compiler.phases.Phase;
49
50import jdk.vm.ci.meta.ResolvedJavaMethod;
51
52// intercepting metrics
53public class MethodMetricsTestInterception02 extends MethodMetricsTest {
54
55    @Override
56    protected Phase additionalPhase() {
57        return new MethodMetricPhases.ScopeTestPhase();
58    }
59
60    private DebugValueFactory factory;
61
62    public void setFactory() {
63        /*
64         * setting a custom debug value factory creating a constant timer for checking scope
65         * creation and inlining scopes with metric intercepting works
66         */
67        factory = Debug.getDebugValueFactory();
68        Debug.setDebugValueFactory(new DebugValueFactory() {
69            @Override
70            public DebugTimer createTimer(String name, boolean conditional) {
71                // can still use together with real timer
72                // TimerImpl realTimer = new TimerImpl(name, conditional, true);
73                return new DebugTimer() {
74                    int runs = 0;
75
76                    // private DebugCloseable t;
77
78                    @Override
79                    public DebugCloseable start() {
80                        // t = realTimer.start();
81                        return new DebugCloseable() {
82                            @Override
83                            public void close() {
84                                // t.close();
85                                runs++;
86                                MethodMetricsImpl.addToCurrentScopeMethodMetrics(name, 1);
87                            }
88                        };
89                    }
90
91                    @Override
92                    public void setConditional(boolean flag) {
93
94                    }
95
96                    @Override
97                    public boolean isConditional() {
98                        return false;
99                    }
100
101                    @Override
102                    public TimeUnit getTimeUnit() {
103                        return TimeUnit.MILLISECONDS;
104                    }
105
106                    @Override
107                    public long getCurrentValue() {
108                        return runs;
109                    }
110                };
111            }
112
113            @Override
114            public DebugCounter createCounter(String name, boolean conditional) {
115                return factory.createCounter(name, conditional);
116            }
117
118            @Override
119            public DebugMethodMetrics createMethodMetrics(ResolvedJavaMethod method) {
120                return factory.createMethodMetrics(method);
121            }
122
123            @Override
124            public DebugMemUseTracker createMemUseTracker(String name, boolean conditional) {
125                return factory.createMemUseTracker(name, conditional);
126            }
127        });
128    }
129
130    @Override
131    protected OverrideScope getOScope() {
132        Map<OptionValue<?>, Object> mapping = new HashMap<>();
133        mapping.put(MethodMetricsPrinter.Options.MethodMeterPrintAscii, true);
134        return OptionValue.override(mapping);
135    }
136
137    @Test
138    @Override
139    public void test() throws Throwable {
140        setFactory();
141        super.test();
142    }
143
144    @Override
145    public void afterTest() {
146        super.afterTest();
147        Debug.setDebugValueFactory(factory);
148    }
149
150    @Override
151    DebugConfig getConfig() {
152        List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
153        List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
154        GraalDebugConfig debugConfig = new GraalDebugConfig(
155                        GraalDebugConfig.Options.Log.getValue(),
156                        ""/* unscoped meter */,
157                        GraalDebugConfig.Options.TrackMemUse.getValue(),
158                        ""/* unscoped time */,
159                        GraalDebugConfig.Options.Dump.getValue(),
160                        GraalDebugConfig.Options.Verify.getValue(),
161                        null /* no method filter */,
162                        "" /* unscoped method metering */,
163                        System.out, dumpHandlers, verifyHandlers);
164        return debugConfig;
165    }
166
167    @Override
168    @SuppressWarnings("try")
169    void assertValues() throws Throwable {
170        assertValues("GlobalTimer4_WithoutInlineEnhancement", new long[]{50, 50, 50, 50, 50, 50, 50, 50, 50, 50});
171    }
172
173}
174