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