43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.hotspot.test;
|
|
26 |
|
46640
|
27 |
import static org.graalvm.compiler.debug.MemUseTrackerKey.getCurrentThreadAllocatedBytes;
|
43972
|
28 |
|
|
29 |
import org.graalvm.compiler.api.test.Graal;
|
|
30 |
import org.graalvm.compiler.core.test.AllocSpy;
|
|
31 |
import org.graalvm.compiler.hotspot.CompilationTask;
|
|
32 |
import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
|
|
33 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
46393
|
34 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
35 |
|
|
36 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
|
|
37 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
|
|
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 |
*
|
|
50 |
* Memory analysis for a {@link CompileTheWorld} execution can also be performed. For example:
|
|
51 |
*
|
|
52 |
* <pre>
|
46393
|
53 |
* mx vm -XX:-UseJVMCIClassLoader -DCompileTheWorld.Classpath=$HOME/SPECjvm2008/SPECjvm2008.jar -cp @org.graalvm.compiler.hotspot.test org.graalvm.compiler.hotspot.test.MemoryUsageBenchmark
|
43972
|
54 |
* </pre>
|
|
55 |
*/
|
|
56 |
public class MemoryUsageBenchmark extends HotSpotGraalCompilerTest {
|
|
57 |
|
|
58 |
public static int simple(int a, int b) {
|
|
59 |
return a + b;
|
|
60 |
}
|
|
61 |
|
|
62 |
public static synchronized int complex(CharSequence cs) {
|
|
63 |
if (cs instanceof String) {
|
|
64 |
return cs.hashCode();
|
|
65 |
}
|
|
66 |
|
|
67 |
if (cs instanceof StringBuilder) {
|
|
68 |
int[] hash = {0};
|
|
69 |
cs.chars().forEach(c -> hash[0] += c);
|
|
70 |
return hash[0];
|
|
71 |
}
|
|
72 |
|
|
73 |
int res = 0;
|
|
74 |
|
|
75 |
// Exercise lock elimination
|
|
76 |
synchronized (cs) {
|
|
77 |
res = cs.length();
|
|
78 |
}
|
|
79 |
synchronized (cs) {
|
|
80 |
res = cs.hashCode() ^ 31;
|
|
81 |
}
|
|
82 |
|
|
83 |
for (int i = 0; i < cs.length(); i++) {
|
|
84 |
res *= cs.charAt(i);
|
|
85 |
}
|
|
86 |
|
|
87 |
// A fixed length loop with some canonicalizable arithmetics will
|
|
88 |
// activate loop unrolling and more canonicalization
|
|
89 |
int sum = 0;
|
|
90 |
for (int i = 0; i < 5; i++) {
|
|
91 |
sum += i * 2;
|
|
92 |
}
|
|
93 |
res += sum;
|
|
94 |
|
|
95 |
// Activates escape-analysis
|
|
96 |
res += new String("asdf").length();
|
|
97 |
|
|
98 |
return res;
|
|
99 |
}
|
|
100 |
|
|
101 |
static class MemoryUsageCloseable implements AutoCloseable {
|
|
102 |
|
|
103 |
private final long start;
|
|
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 |
new MemoryUsageBenchmark().run();
|
|
125 |
}
|
|
126 |
|
|
127 |
@SuppressWarnings("try")
|
|
128 |
private void doCompilation(String methodName, String label) {
|
|
129 |
HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
|
|
130 |
|
|
131 |
// invalidate any existing compiled code
|
|
132 |
method.reprofile();
|
|
133 |
|
|
134 |
long jvmciEnv = 0L;
|
|
135 |
|
|
136 |
try (MemoryUsageCloseable c = label == null ? null : new MemoryUsageCloseable(label)) {
|
50858
|
137 |
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
|
43972
|
138 |
int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
|
|
139 |
HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, jvmciEnv);
|
54601
|
140 |
CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false);
|
|
141 |
task.runCompilation(getInitialOptions());
|
43972
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
@SuppressWarnings("try")
|
|
146 |
private void allocSpyCompilation(String methodName) {
|
|
147 |
if (AllocSpy.isEnabled()) {
|
|
148 |
HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
|
|
149 |
|
|
150 |
// invalidate any existing compiled code
|
|
151 |
method.reprofile();
|
|
152 |
|
|
153 |
long jvmciEnv = 0L;
|
|
154 |
try (AllocSpy as = AllocSpy.open(methodName)) {
|
50858
|
155 |
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
|
43972
|
156 |
HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, JVMCICompiler.INVOCATION_ENTRY_BCI, jvmciEnv);
|
46640
|
157 |
HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
|
|
158 |
OptionValues options = getInitialOptions();
|
54601
|
159 |
CompilationTask task = new CompilationTask(runtime, compiler, request, true, false);
|
|
160 |
task.runCompilation(options);
|
43972
|
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 |
|
|
180 |
public void run() {
|
|
181 |
compileAndTime("simple");
|
|
182 |
compileAndTime("complex");
|
54601
|
183 |
OptionValues harnessOptions = CompileTheWorld.loadHarnessOptions();
|
|
184 |
if (CompileTheWorld.Options.Classpath.getValue(harnessOptions) != CompileTheWorld.SUN_BOOT_CLASS_PATH) {
|
50858
|
185 |
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
|
54601
|
186 |
CompileTheWorld ctw = new CompileTheWorld(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), harnessOptions, getInitialOptions());
|
43972
|
187 |
try {
|
|
188 |
ctw.compile();
|
|
189 |
} catch (Throwable e) {
|
|
190 |
e.printStackTrace();
|
|
191 |
}
|
|
192 |
}
|
|
193 |
allocSpyCompilation("simple");
|
|
194 |
allocSpyCompilation("complex");
|
|
195 |
}
|
|
196 |
}
|