src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/MemoryUsageBenchmark.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/MemoryUsageBenchmark.java

Print this page




   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.DebugEnvironment;
  30 import org.graalvm.compiler.hotspot.CompilationTask;
  31 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
  32 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  33 import org.graalvm.compiler.options.OptionValues;
  34 
  35 import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
  36 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  37 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  38 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  39 import jdk.vm.ci.runtime.JVMCICompiler;
  40 
  41 /**
  42  * Used to benchmark memory usage during Graal compilation.
  43  *
  44  * To benchmark:
  45  *
  46  * <pre>
  47  *     mx vm -XX:-UseJVMCIClassLoader -cp @org.graalvm.compiler.hotspot.test org.graalvm.compiler.hotspot.test.MemoryUsageBenchmark
  48  * </pre>
  49  *


 104         private final String name;
 105 
 106         MemoryUsageCloseable(String name) {
 107             this.name = name;
 108             this.start = getCurrentThreadAllocatedBytes();
 109         }
 110 
 111         @Override
 112         public void close() {
 113             long end = getCurrentThreadAllocatedBytes();
 114             long allocated = end - start;
 115             System.out.println(name + ": " + allocated);
 116         }
 117     }
 118 
 119     public static void main(String[] args) {
 120         // Ensure a Graal runtime is initialized prior to Debug being initialized as the former
 121         // may include processing command line options used by the latter.
 122         Graal.getRuntime();
 123 
 124         // Ensure a debug configuration for this thread is initialized
 125         DebugEnvironment.ensureInitialized(getInitialOptions());
 126         new MemoryUsageBenchmark().run();
 127     }
 128 
 129     @SuppressWarnings("try")
 130     private void doCompilation(String methodName, String label) {
 131         HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
 132 
 133         // invalidate any existing compiled code
 134         method.reprofile();
 135 
 136         long jvmciEnv = 0L;
 137 
 138         try (MemoryUsageCloseable c = label == null ? null : new MemoryUsageCloseable(label)) {
 139             HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
 140             int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
 141             HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, jvmciEnv);
 142             CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false, getInitialOptions());
 143             task.runCompilation();
 144         }
 145     }
 146 
 147     @SuppressWarnings("try")
 148     private void allocSpyCompilation(String methodName) {
 149         if (AllocSpy.isEnabled()) {
 150             HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
 151 
 152             // invalidate any existing compiled code
 153             method.reprofile();
 154 
 155             long jvmciEnv = 0L;
 156             try (AllocSpy as = AllocSpy.open(methodName)) {
 157                 HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
 158                 HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, JVMCICompiler.INVOCATION_ENTRY_BCI, jvmciEnv);
 159                 CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false, getInitialOptions());


 160                 task.runCompilation();
 161             }
 162         }
 163     }
 164 
 165     private static final boolean verbose = Boolean.getBoolean("verbose");
 166 
 167     private void compileAndTime(String methodName) {
 168 
 169         // Parse in eager mode to resolve methods/fields/classes
 170         parseEager(methodName, AllowAssumptions.YES);
 171 
 172         // Warm up and initialize compiler phases used by this compilation
 173         for (int i = 0; i < 10; i++) {
 174             doCompilation(methodName, verbose ? methodName + "[warmup-" + i + "]" : null);
 175         }
 176 
 177         doCompilation(methodName, methodName);
 178     }
 179 


   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.MemUseTrackerKey.getCurrentThreadAllocatedBytes;
  26 
  27 import org.graalvm.compiler.api.test.Graal;
  28 import org.graalvm.compiler.core.test.AllocSpy;

  29 import org.graalvm.compiler.hotspot.CompilationTask;
  30 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
  31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  32 import org.graalvm.compiler.options.OptionValues;
  33 
  34 import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
  35 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  36 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  37 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  38 import jdk.vm.ci.runtime.JVMCICompiler;
  39 
  40 /**
  41  * Used to benchmark memory usage during Graal compilation.
  42  *
  43  * To benchmark:
  44  *
  45  * <pre>
  46  *     mx vm -XX:-UseJVMCIClassLoader -cp @org.graalvm.compiler.hotspot.test org.graalvm.compiler.hotspot.test.MemoryUsageBenchmark
  47  * </pre>
  48  *


 103         private final String name;
 104 
 105         MemoryUsageCloseable(String name) {
 106             this.name = name;
 107             this.start = getCurrentThreadAllocatedBytes();
 108         }
 109 
 110         @Override
 111         public void close() {
 112             long end = getCurrentThreadAllocatedBytes();
 113             long allocated = end - start;
 114             System.out.println(name + ": " + allocated);
 115         }
 116     }
 117 
 118     public static void main(String[] args) {
 119         // Ensure a Graal runtime is initialized prior to Debug being initialized as the former
 120         // may include processing command line options used by the latter.
 121         Graal.getRuntime();
 122 


 123         new MemoryUsageBenchmark().run();
 124     }
 125 
 126     @SuppressWarnings("try")
 127     private void doCompilation(String methodName, String label) {
 128         HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
 129 
 130         // invalidate any existing compiled code
 131         method.reprofile();
 132 
 133         long jvmciEnv = 0L;
 134 
 135         try (MemoryUsageCloseable c = label == null ? null : new MemoryUsageCloseable(label)) {
 136             HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
 137             int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
 138             HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, jvmciEnv);
 139             CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false, getInitialOptions());
 140             task.runCompilation();
 141         }
 142     }
 143 
 144     @SuppressWarnings("try")
 145     private void allocSpyCompilation(String methodName) {
 146         if (AllocSpy.isEnabled()) {
 147             HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
 148 
 149             // invalidate any existing compiled code
 150             method.reprofile();
 151 
 152             long jvmciEnv = 0L;
 153             try (AllocSpy as = AllocSpy.open(methodName)) {
 154                 HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
 155                 HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, JVMCICompiler.INVOCATION_ENTRY_BCI, jvmciEnv);
 156                 HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
 157                 OptionValues options = getInitialOptions();
 158                 CompilationTask task = new CompilationTask(runtime, compiler, request, true, false, options);
 159                 task.runCompilation();
 160             }
 161         }
 162     }
 163 
 164     private static final boolean verbose = Boolean.getBoolean("verbose");
 165 
 166     private void compileAndTime(String methodName) {
 167 
 168         // Parse in eager mode to resolve methods/fields/classes
 169         parseEager(methodName, AllowAssumptions.YES);
 170 
 171         // Warm up and initialize compiler phases used by this compilation
 172         for (int i = 0; i < 10; i++) {
 173             doCompilation(methodName, verbose ? methodName + "[warmup-" + i + "]" : null);
 174         }
 175 
 176         doCompilation(methodName, methodName);
 177     }
 178 
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/MemoryUsageBenchmark.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File