Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorStackDepthInfo/GetOwnedMonitorStackDepthInfoWithEATest.java
41153 views
1
/*
2
* Copyright (c) 2019 SAP SE. 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
24
/**
25
* @test
26
* @bug 8230677
27
* @summary Test JVMTI's GetOwnedMonitorStackDepthInfo with scalar replaced objects and eliminated locks on stack (optimizations based on escape analysis).
28
* @comment Without RFE 8227745 escape analysis needs to be switched off to pass the test. For the implementation of RFE 8227745 it serves as a regression test.
29
* @requires (vm.compMode != "Xcomp" & vm.compiler2.enabled)
30
* @requires vm.jvmti
31
* @library /test/lib
32
* @compile GetOwnedMonitorStackDepthInfoWithEATest.java
33
* @run main/othervm/native
34
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
35
* -XX:+UnlockDiagnosticVMOptions
36
* -Xms128m -Xmx128m
37
* -XX:CompileCommand=dontinline,*::dontinline_*
38
* -XX:+PrintCompilation
39
* -XX:+PrintInlining
40
* -XX:-TieredCompilation
41
* -Xbatch
42
* -XX:CICompilerCount=1
43
* -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking
44
* GetOwnedMonitorStackDepthInfoWithEATest
45
* @run main/othervm/native
46
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
47
* -XX:+UnlockDiagnosticVMOptions
48
* -Xms128m -Xmx128m
49
* -XX:CompileCommand=dontinline,*::dontinline_*
50
* -XX:+PrintCompilation
51
* -XX:+PrintInlining
52
* -XX:-TieredCompilation
53
* -Xbatch
54
* -XX:CICompilerCount=1
55
* -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:-EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking -XX:-UseOptoBiasInlining
56
* GetOwnedMonitorStackDepthInfoWithEATest
57
* @run main/othervm/native
58
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
59
* -XX:+UnlockDiagnosticVMOptions
60
* -Xms128m -Xmx128m
61
* -XX:CompileCommand=dontinline,*::dontinline_*
62
* -XX:+PrintCompilation
63
* -XX:+PrintInlining
64
* -XX:-TieredCompilation
65
* -Xbatch
66
* -XX:CICompilerCount=1
67
* -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking
68
* GetOwnedMonitorStackDepthInfoWithEATest
69
* @run main/othervm/native
70
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
71
* -XX:+UnlockDiagnosticVMOptions
72
* -Xms128m -Xmx128m
73
* -XX:CompileCommand=dontinline,*::dontinline_*
74
* -XX:+PrintCompilation
75
* -XX:+PrintInlining
76
* -XX:-TieredCompilation
77
* -Xbatch
78
* -XX:CICompilerCount=1
79
* -XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking
80
* GetOwnedMonitorStackDepthInfoWithEATest
81
* @run main/othervm/native
82
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
83
* -XX:+UnlockDiagnosticVMOptions
84
* -Xms128m -Xmx128m
85
* -XX:CompileCommand=dontinline,*::dontinline_*
86
* -XX:+PrintCompilation
87
* -XX:+PrintInlining
88
* -XX:-TieredCompilation
89
* -Xbatch
90
* -XX:CICompilerCount=1
91
* -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:-UseBiasedLocking
92
* GetOwnedMonitorStackDepthInfoWithEATest
93
* @run main/othervm/native
94
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
95
* -XX:+UnlockDiagnosticVMOptions
96
* -Xms128m -Xmx128m
97
* -XX:CompileCommand=dontinline,*::dontinline_*
98
* -XX:+PrintCompilation
99
* -XX:+PrintInlining
100
* -XX:-TieredCompilation
101
* -Xbatch
102
* -XX:CICompilerCount=1
103
* -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:-UseBiasedLocking
104
* GetOwnedMonitorStackDepthInfoWithEATest
105
* @run main/othervm/native
106
* -agentlib:GetOwnedMonitorStackDepthInfoWithEATest
107
* -XX:+UnlockDiagnosticVMOptions
108
* -Xms128m -Xmx128m
109
* -XX:CompileCommand=dontinline,*::dontinline_*
110
* -XX:+PrintCompilation
111
* -XX:+PrintInlining
112
* -XX:-TieredCompilation
113
* -Xbatch
114
* -XX:CICompilerCount=1
115
* -XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:-UseBiasedLocking
116
* GetOwnedMonitorStackDepthInfoWithEATest
117
*/
118
119
import jdk.test.lib.Asserts;
120
121
public class GetOwnedMonitorStackDepthInfoWithEATest {
122
123
public static final int COMPILE_THRESHOLD = 20000;
124
125
/**
126
* Native wrapper arround JVMTI's GetOwnedMonitorStackDepthInfo().
127
* @param t The thread for which the owned monitors information should be retrieved.
128
* @param ownedMonitors Array filled in by the call with the objects associated
129
* with the monitors owned by the given thread.
130
* @param depths Per owned monitor the depth of the frame were it was locked.
131
* Filled in by the call
132
* @return Number of monitors owned by the given thread.
133
*/
134
public static native int getOwnedMonitorStackDepthInfo(Thread t, Object[] ownedMonitors, int[] depths);
135
136
public static void main(String[] args) throws Exception {
137
new GetOwnedMonitorStackDepthInfoWithEATest().runTest();
138
}
139
140
public void runTest() throws Exception {
141
new TestCase_1().run();
142
new TestCase_2().run();
143
}
144
145
public static abstract class TestCaseBase implements Runnable {
146
147
public long checkSum;
148
public boolean doLoop;
149
public volatile long loopCount;
150
public volatile boolean targetIsInLoop;
151
152
public void run() {
153
try {
154
msgHL("Executing test case " + getClass().getName());
155
warmUp();
156
runTest();
157
} catch (Exception e) {
158
Asserts.fail("Unexpected Exception", e);
159
}
160
}
161
162
public void warmUp() {
163
int callCount = COMPILE_THRESHOLD + 1000;
164
doLoop = true;
165
while (callCount-- > 0) {
166
dontinline_testMethod();
167
}
168
}
169
170
public abstract void runTest() throws Exception;
171
public abstract void dontinline_testMethod();
172
173
public long dontinline_endlessLoop() {
174
long cs = checkSum;
175
while (doLoop && loopCount-- > 0) {
176
targetIsInLoop = true;
177
checkSum += checkSum % ++cs;
178
}
179
loopCount = 3;
180
targetIsInLoop = false;
181
return checkSum;
182
}
183
184
public void waitUntilTargetThreadHasEnteredEndlessLoop() throws Exception {
185
while(!targetIsInLoop) {
186
msg("Target has not yet entered the loop. Sleep 200ms.");
187
try { Thread.sleep(200); } catch (InterruptedException e) { /*ignore */ }
188
}
189
msg("Target has entered the loop.");
190
}
191
192
public void terminateEndlessLoop() throws Exception {
193
msg("Terminate endless loop");
194
do {
195
doLoop = false;
196
} while(targetIsInLoop);
197
}
198
199
public void msg(String m) {
200
System.out.println();
201
System.out.println("### " + m);
202
System.out.println();
203
}
204
205
public void msgHL(String m) {
206
System.out.println();
207
System.out.println("#####################################################");
208
System.out.println("### " + m);
209
System.out.println("###");
210
System.out.println();
211
}
212
}
213
214
/**
215
* Starts target thread T and then queries monitor information for T using JVMTI's GetOwnedMonitorStackDepthInfo().
216
* With escape analysis enabled the jit compiled method {@link #dontinline_testMethod()} has
217
* scalar replaced objects with eliminated (nested) locking in scope when the monitor
218
* information is retrieved. Effectively the objects escape through the JVMTI call. This works
219
* only with RFE 8227745. Without it escape analysis needs to be disabled.
220
*/
221
public static class TestCase_1 extends TestCaseBase {
222
223
public void runTest() throws Exception {
224
loopCount = 1L << 62; // endless loop
225
Thread t1 = new Thread(() -> dontinline_testMethod(), "Target Thread");
226
try {
227
t1.start();
228
waitUntilTargetThreadHasEnteredEndlessLoop();
229
int expectedMonitorCount = 1;
230
int resultSize = expectedMonitorCount + 3;
231
Object[] ownedMonitors = new Object[resultSize];
232
int[] depths = new int[resultSize];
233
msg("Get monitor info");
234
int monitorCount = getOwnedMonitorStackDepthInfo(t1, ownedMonitors, depths);
235
Asserts.assertGreaterThanOrEqual(monitorCount, 0, "getOwnedMonitorsFor() call failed");
236
msg("Monitor info:");
237
for (int i = 0; i < monitorCount; i++) {
238
System.out.println(i + ": cls=" + (ownedMonitors[i] != null ? ownedMonitors[i].getClass() : null) + " depth=" + depths[i]);
239
}
240
Asserts.assertEQ(monitorCount, expectedMonitorCount, "unexpected monitor count returned by getOwnedMonitorsFor()");
241
Asserts.assertNotNull(ownedMonitors[0]);
242
Asserts.assertSame(ownedMonitors[0].getClass(), LockCls.class);
243
Asserts.assertEQ(depths[0], 1, "unexpected depth for owned monitor at index 0");
244
} finally {
245
terminateEndlessLoop();
246
t1.join();
247
}
248
}
249
250
public void dontinline_testMethod() {
251
LockCls l1 = new LockCls(); // to be scalar replaced
252
synchronized (l1) {
253
inlinedTestMethodWithNestedLocking(l1);
254
}
255
}
256
257
public void inlinedTestMethodWithNestedLocking(LockCls l1) {
258
synchronized (l1) { // nested
259
dontinline_endlessLoop();
260
}
261
}
262
}
263
264
/**
265
* Similar to {@link TestCase_1}. Additionally the target thread T has got eliminated locking
266
* for a synchronized method of a different type {@linkplain LockCls2}.
267
*/
268
public static class TestCase_2 extends TestCaseBase {
269
270
public void runTest() throws Exception {
271
loopCount = 1L << 62; // endless loop
272
Thread t1 = new Thread(() -> dontinline_testMethod(), "Target Thread");
273
t1.start();
274
try {
275
waitUntilTargetThreadHasEnteredEndlessLoop();
276
int expectedMonitorCount = 2;
277
int resultSize = expectedMonitorCount + 3;
278
Object[] ownedMonitors = new Object[resultSize];
279
int[] depths = new int[resultSize];
280
msg("Get monitor info");
281
int monitorCount = getOwnedMonitorStackDepthInfo(t1, ownedMonitors, depths);
282
terminateEndlessLoop();
283
t1.join();
284
Asserts.assertGreaterThanOrEqual(monitorCount, 0, "getOwnedMonitorsFor() call failed");
285
msg("Monitor info:");
286
for (int i = 0; i < monitorCount; i++) {
287
System.out.println(i + ": cls=" + (ownedMonitors[i] != null ? ownedMonitors[i].getClass() : null) + " depth=" + depths[i]);
288
}
289
Asserts.assertEQ(monitorCount, expectedMonitorCount, "unexpected monitor count returned by getOwnedMonitorsFor()");
290
Asserts.assertNotNull(ownedMonitors[0]);
291
Asserts.assertSame(ownedMonitors[0].getClass(), LockCls2.class);
292
Asserts.assertEQ(depths[0], 1, "unexpected depth for owned monitor at index 0");
293
294
Asserts.assertNotNull(ownedMonitors[1]);
295
Asserts.assertSame(ownedMonitors[1].getClass(), LockCls.class);
296
Asserts.assertEQ(depths[1], 3, "unexpected depth for owned monitor at index 1");
297
} finally {
298
terminateEndlessLoop();
299
t1.join();
300
}
301
}
302
303
public void dontinline_testMethod() {
304
LockCls l1 = new LockCls();
305
synchronized (l1) {
306
inlinedTestMethodWithNestedLocking(l1);
307
}
308
}
309
310
public void inlinedTestMethodWithNestedLocking(LockCls l1) {
311
synchronized (l1) {
312
dontinline_testMethod2();
313
}
314
}
315
316
public void dontinline_testMethod2() {
317
// Call synchronized method. Receiver of the call will be scalar replaced,
318
// and locking will be eliminated. Here we use a different type.
319
new LockCls2().inline_synchronized_testMethod(this);
320
}
321
}
322
323
public static class LockCls {
324
}
325
326
public static class LockCls2 {
327
public synchronized void inline_synchronized_testMethod(TestCaseBase testCase) {
328
testCase.dontinline_endlessLoop();
329
}
330
}
331
}
332
333