1/*
2 * Copyright (c) 2015, 2015, 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.microbenchmarks.graal.util;
24
25import static org.graalvm.compiler.microbenchmarks.graal.util.GraalUtil.getGraph;
26import static org.graalvm.compiler.microbenchmarks.graal.util.GraalUtil.getMethodFromMethodSpec;
27import jdk.vm.ci.meta.ResolvedJavaMethod;
28
29import org.openjdk.jmh.annotations.Level;
30import org.openjdk.jmh.annotations.Scope;
31import org.openjdk.jmh.annotations.Setup;
32import org.openjdk.jmh.annotations.State;
33
34import org.graalvm.compiler.debug.Debug;
35import org.graalvm.compiler.debug.DebugEnvironment;
36import org.graalvm.compiler.debug.internal.DebugScope;
37import org.graalvm.compiler.nodes.StructuredGraph;
38
39/**
40 * State providing a new copy of a graph for each invocation of a benchmark. Subclasses of this
41 * class are annotated with {@link MethodSpec} to specify the Java method that will be parsed to
42 * obtain the original graph.
43 */
44@State(Scope.Thread)
45public abstract class GraphState {
46
47    @SuppressWarnings("try")
48    public GraphState() {
49        // Ensure a debug configuration for this thread is initialized
50        if (Debug.isEnabled() && DebugScope.getConfig() == null) {
51            DebugEnvironment.initialize(System.out);
52        }
53
54        GraalState graal = new GraalState();
55        ResolvedJavaMethod method = graal.metaAccess.lookupJavaMethod(getMethodFromMethodSpec(getClass()));
56        StructuredGraph structuredGraph = null;
57        try (Debug.Scope s = Debug.scope("GraphState", method)) {
58            structuredGraph = preprocessOriginal(getGraph(graal, method));
59        } catch (Throwable t) {
60            Debug.handle(t);
61        }
62        this.originalGraph = structuredGraph;
63    }
64
65    protected StructuredGraph preprocessOriginal(StructuredGraph structuredGraph) {
66        return structuredGraph;
67    }
68
69    /**
70     * Original graph from which the per-benchmark invocation {@link #graph} is cloned.
71     */
72    private final StructuredGraph originalGraph;
73
74    /**
75     * The graph processed by the benchmark.
76     */
77    public StructuredGraph graph;
78
79    @Setup(Level.Invocation)
80    public void beforeInvocation() {
81        graph = (StructuredGraph) originalGraph.copy();
82    }
83}
84