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