1 /* 2 * Copyright (c) 2011, 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 */ 23 package org.graalvm.compiler.hotspot.test; 24 25 import static org.graalvm.compiler.debug.internal.MemUseTrackerImpl.getCurrentThreadAllocatedBytes; 26 27 import org.graalvm.compiler.api.test.Graal; 28 import org.graalvm.compiler.core.test.AllocSpy; 29 import org.graalvm.compiler.debug.Debug; 30 import org.graalvm.compiler.debug.DebugEnvironment; 31 import org.graalvm.compiler.debug.internal.DebugScope; 32 import org.graalvm.compiler.hotspot.CompilationTask; 33 import org.graalvm.compiler.hotspot.CompileTheWorld; 34 import org.graalvm.compiler.hotspot.CompileTheWorldOptions; 35 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler; 36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 37 38 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 39 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 40 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider; 41 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 42 import jdk.vm.ci.runtime.JVMCICompiler; 43 44 /** 45 * Used to benchmark memory usage during Graal compilation. 46 * 47 * To benchmark: 48 * 49 * <pre> 50 * mx vm -XX:-UseJVMCIClassLoader -cp @org.graalvm.compiler.hotspot.test org.graalvm.compiler.hotspot.test.MemoryUsageBenchmark 51 * </pre> 52 * 53 * Memory analysis for a {@link CompileTheWorld} execution can also be performed. For example: 54 * 55 * <pre> 56 * mx --vm server vm -XX:-UseJVMCIClassLoader -Dgraal.CompileTheWorldClasspath=$HOME/SPECjvm2008/SPECjvm2008.jar -cp @org.graalvm.compiler.hotspot.test org.graalvm.compiler.hotspot.test.MemoryUsageBenchmark 57 * </pre> 58 */ 59 public class MemoryUsageBenchmark extends HotSpotGraalCompilerTest { 60 61 public static int simple(int a, int b) { 62 return a + b; 63 } 64 65 public static synchronized int complex(CharSequence cs) { 66 if (cs instanceof String) { 67 return cs.hashCode(); 68 } 69 70 if (cs instanceof StringBuilder) { 71 int[] hash = {0}; 72 cs.chars().forEach(c -> hash[0] += c); 73 return hash[0]; 74 } 75 76 int res = 0; 77 78 // Exercise lock elimination 79 synchronized (cs) { 80 res = cs.length(); 81 } 82 synchronized (cs) { 83 res = cs.hashCode() ^ 31; 84 } 85 86 for (int i = 0; i < cs.length(); i++) { 87 res *= cs.charAt(i); 88 } 89 90 // A fixed length loop with some canonicalizable arithmetics will 91 // activate loop unrolling and more canonicalization 92 int sum = 0; 93 for (int i = 0; i < 5; i++) { 94 sum += i * 2; 95 } 96 res += sum; 97 98 // Activates escape-analysis 99 res += new String("asdf").length(); 100 101 return res; 102 } 103 104 static class MemoryUsageCloseable implements AutoCloseable { 105 106 private final long start; 107 private final String name; 108 109 MemoryUsageCloseable(String name) { 110 this.name = name; 111 this.start = getCurrentThreadAllocatedBytes(); 112 } 113 114 @Override 115 public void close() { 116 long end = getCurrentThreadAllocatedBytes(); 117 long allocated = end - start; 118 System.out.println(name + ": " + allocated); 119 } 120 } 121 122 public static void main(String[] args) { 123 // Ensure a Graal runtime is initialized prior to Debug being initialized as the former 124 // may include processing command line options used by the latter. 125 Graal.getRuntime(); 126 127 // Ensure a debug configuration for this thread is initialized 128 if (Debug.isEnabled() && DebugScope.getConfig() == null) { 129 DebugEnvironment.initialize(System.out); 130 } 131 new MemoryUsageBenchmark().run(); 132 } 133 134 @SuppressWarnings("try") 135 private void doCompilation(String methodName, String label) { 136 HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName); 137 138 // invalidate any existing compiled code 139 method.reprofile(); 140 141 long jvmciEnv = 0L; 142 143 try (MemoryUsageCloseable c = label == null ? null : new MemoryUsageCloseable(label)) { 144 HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime(); 145 int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI; 146 HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, jvmciEnv); 147 CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false); 148 task.runCompilation(); 149 } 150 } 151 152 @SuppressWarnings("try") 153 private void allocSpyCompilation(String methodName) { 154 if (AllocSpy.isEnabled()) { 155 HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName); 156 157 // invalidate any existing compiled code 158 method.reprofile(); 159 160 long jvmciEnv = 0L; 161 try (AllocSpy as = AllocSpy.open(methodName)) { 162 HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime(); 163 HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, JVMCICompiler.INVOCATION_ENTRY_BCI, jvmciEnv); 164 CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false); 165 task.runCompilation(); 166 } 167 } 168 } 169 170 private static final boolean verbose = Boolean.getBoolean("verbose"); 171 172 private void compileAndTime(String methodName) { 173 174 // Parse in eager mode to resolve methods/fields/classes 175 parseEager(methodName, AllowAssumptions.YES); 176 177 // Warm up and initialize compiler phases used by this compilation 178 for (int i = 0; i < 10; i++) { 179 doCompilation(methodName, verbose ? methodName + "[warmup-" + i + "]" : null); 180 } 181 182 doCompilation(methodName, methodName); 183 } 184 185 public void run() { 186 compileAndTime("simple"); 187 compileAndTime("complex"); 188 if (CompileTheWorldOptions.CompileTheWorldClasspath.getValue() != CompileTheWorld.SUN_BOOT_CLASS_PATH) { 189 HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime(); 190 CompileTheWorld ctw = new CompileTheWorld(runtime, (HotSpotGraalCompiler) runtime.getCompiler()); 191 try { 192 ctw.compile(); 193 } catch (Throwable e) { 194 e.printStackTrace(); 195 } 196 } 197 allocSpyCompilation("simple"); 198 allocSpyCompilation("complex"); 199 } 200 }