author | pliden |
Wed, 20 Nov 2019 10:37:46 +0100 | |
changeset 59152 | 59272e9e0635 |
parent 59005 | 01d47d511f5f |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
55625
f7e8dbb77156
8191890: Biased locking still uses the inferior stop the world safepoint for revocation
pchilanomate
parents:
52672
diff
changeset
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.event.runtime; |
|
27 |
||
28 |
import jdk.jfr.Recording; |
|
29 |
import jdk.jfr.consumer.*; |
|
30 |
import jdk.test.lib.Asserts; |
|
31 |
import jdk.test.lib.dcmd.PidJcmdExecutor; |
|
32 |
import jdk.test.lib.jfr.EventNames; |
|
33 |
import jdk.test.lib.jfr.Events; |
|
34 |
import jdk.test.lib.process.OutputAnalyzer; |
|
35 |
||
50391
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
36 |
import java.util.*; |
50113 | 37 |
import java.util.concurrent.FutureTask; |
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
38 |
import java.util.stream.Collectors; |
50113 | 39 |
|
51214 | 40 |
/** |
50113 | 41 |
* @test |
42 |
* @key jfr |
|
51214 | 43 |
* @requires vm.hasJFR |
50113 | 44 |
* @library /test/lib |
45 |
* |
|
59152
59272e9e0635
8234383: Test TestBiasedLockRevocationEvents.java assumes -XX:UseBiasedLocking is enabled
pliden
parents:
59005
diff
changeset
|
46 |
* @run main/othervm -XX:+UseBiasedLocking jdk.jfr.event.runtime.TestBiasedLockRevocationEvents |
50113 | 47 |
*/ |
48 |
public class TestBiasedLockRevocationEvents { |
|
49 |
||
50 |
public static void main(String[] args) throws Throwable { |
|
51 |
testSingleRevocation(); |
|
52 |
testBulkRevocation(); |
|
53 |
testSelfRevocation(); |
|
54 |
testExitedThreadRevocation(); |
|
55 |
testBulkRevocationNoRebias(); |
|
56 |
testRevocationSafepointIdCorrelation(); |
|
57 |
} |
|
58 |
||
59 |
// Default value of BiasedLockingBulkRebiasThreshold is 20, and BiasedLockingBulkRevokeTreshold is 40. |
|
60 |
// Using a value that will hit the first threshold once, and the second one the next time. |
|
61 |
private static final int BULK_REVOKE_THRESHOLD = 25; |
|
62 |
||
63 |
static void touch(Object lock) { |
|
64 |
synchronized(lock) { |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
static Thread triggerRevocation(int numRevokes, Class<?> lockClass) throws Throwable { |
|
69 |
Object[] locks = new Object[numRevokes]; |
|
70 |
for (int i = 0; i < locks.length; ++i) { |
|
71 |
locks[i] = lockClass.getDeclaredConstructor().newInstance(); |
|
72 |
touch(locks[i]); |
|
73 |
} |
|
74 |
||
75 |
Thread biasBreaker = new Thread("BiasBreaker") { |
|
76 |
@Override |
|
77 |
public void run() { |
|
78 |
for (Object lock : locks) { |
|
79 |
touch(lock); |
|
80 |
} |
|
81 |
} |
|
82 |
}; |
|
83 |
||
84 |
biasBreaker.start(); |
|
85 |
biasBreaker.join(); |
|
86 |
||
87 |
return biasBreaker; |
|
88 |
} |
|
89 |
||
90 |
// Basic stack trace validation, checking the name of the leaf method |
|
91 |
static void validateStackTrace(RecordedStackTrace stackTrace, String leafMethodName) { |
|
92 |
List<RecordedFrame> frames = stackTrace.getFrames(); |
|
93 |
Asserts.assertFalse(frames.isEmpty()); |
|
94 |
String name = frames.get(0).getMethod().getName(); |
|
95 |
Asserts.assertEquals(name, leafMethodName); |
|
96 |
} |
|
97 |
||
98 |
// Validates that the given stack trace refers to lock.touch(); in triggerRevocation |
|
99 |
static void validateStackTrace(RecordedStackTrace stackTrace) { |
|
100 |
validateStackTrace(stackTrace, "touch"); |
|
101 |
} |
|
102 |
||
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
103 |
// Retrieve all biased lock revocation events related to the provided lock class, sorted by start time |
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
104 |
static List<RecordedEvent> getRevocationEvents(Recording recording, String eventTypeName, String fieldName, Class<?> lockClass) throws Throwable { |
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
105 |
return Events.fromRecording(recording).stream() |
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
106 |
.filter(e -> e.getEventType().getName().equals(eventTypeName)) |
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
107 |
.filter(e -> ((RecordedClass)e.getValue(fieldName)).getName().equals(lockClass.getName())) |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
108 |
.sorted(Comparator.comparing(RecordedEvent::getStartTime)) |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
109 |
.collect(Collectors.toList()); |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
110 |
} |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
111 |
|
50113 | 112 |
static void testSingleRevocation() throws Throwable { |
113 |
class MyLock {}; |
|
114 |
||
115 |
Recording recording = new Recording(); |
|
116 |
||
117 |
recording.enable(EventNames.BiasedLockRevocation); |
|
118 |
recording.start(); |
|
119 |
||
120 |
Thread biasBreaker = triggerRevocation(1, MyLock.class); |
|
121 |
||
122 |
recording.stop(); |
|
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
123 |
List<RecordedEvent> events = getRevocationEvents(recording, EventNames.BiasedLockRevocation, "lockClass", MyLock.class); |
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
124 |
Asserts.assertEQ(events.size(), 1); |
50113 | 125 |
|
126 |
RecordedEvent event = events.get(0); |
|
127 |
Events.assertEventThread(event, biasBreaker); |
|
128 |
Events.assertEventThread(event, "previousOwner", Thread.currentThread()); |
|
129 |
||
130 |
RecordedClass lockClass = event.getValue("lockClass"); |
|
131 |
Asserts.assertEquals(lockClass.getName(), MyLock.class.getName()); |
|
132 |
||
133 |
validateStackTrace(event.getStackTrace()); |
|
134 |
} |
|
135 |
||
136 |
static void testBulkRevocation() throws Throwable { |
|
137 |
class MyLock {}; |
|
138 |
||
139 |
Recording recording = new Recording(); |
|
140 |
||
141 |
recording.enable(EventNames.BiasedLockClassRevocation); |
|
142 |
recording.start(); |
|
143 |
||
144 |
Thread biasBreaker = triggerRevocation(BULK_REVOKE_THRESHOLD, MyLock.class); |
|
145 |
||
146 |
recording.stop(); |
|
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
147 |
List<RecordedEvent> events = getRevocationEvents(recording, EventNames.BiasedLockClassRevocation, "revokedClass", MyLock.class); |
50113 | 148 |
Asserts.assertEQ(events.size(), 1); |
149 |
||
150 |
RecordedEvent event = events.get(0); |
|
151 |
Events.assertEventThread(event, biasBreaker); |
|
152 |
Events.assertField(event, "disableBiasing").equal(false); |
|
153 |
||
154 |
RecordedClass lockClass = event.getValue("revokedClass"); |
|
155 |
Asserts.assertEquals(lockClass.getName(), MyLock.class.getName()); |
|
156 |
||
157 |
validateStackTrace(event.getStackTrace()); |
|
158 |
} |
|
159 |
||
160 |
static void testSelfRevocation() throws Throwable { |
|
161 |
class MyLock {}; |
|
162 |
||
163 |
Recording recording = new Recording(); |
|
164 |
||
165 |
recording.enable(EventNames.BiasedLockSelfRevocation); |
|
166 |
recording.start(); |
|
167 |
||
168 |
MyLock l = new MyLock(); |
|
169 |
touch(l); |
|
170 |
Thread.holdsLock(l); |
|
171 |
||
172 |
recording.stop(); |
|
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
173 |
List<RecordedEvent> events = getRevocationEvents(recording, EventNames.BiasedLockSelfRevocation, "lockClass", MyLock.class); |
50113 | 174 |
Asserts.assertEQ(events.size(), 1); |
175 |
||
176 |
RecordedEvent event = events.get(0); |
|
177 |
Events.assertEventThread(event, Thread.currentThread()); |
|
178 |
||
179 |
validateStackTrace(event.getStackTrace(), "holdsLock"); |
|
180 |
} |
|
181 |
||
182 |
static void testExitedThreadRevocation() throws Throwable { |
|
183 |
class MyLock {}; |
|
184 |
||
185 |
Recording recording = new Recording(); |
|
186 |
||
187 |
recording.enable(EventNames.BiasedLockRevocation); |
|
188 |
recording.start(); |
|
189 |
||
190 |
FutureTask<MyLock> lockerTask = new FutureTask<>(() -> { |
|
191 |
MyLock l = new MyLock(); |
|
192 |
touch(l); |
|
193 |
return l; |
|
194 |
}); |
|
195 |
||
196 |
Thread locker = new Thread(lockerTask, "BiasLocker"); |
|
197 |
locker.start(); |
|
198 |
locker.join(); |
|
199 |
||
200 |
// Even after joining, the VM has a bit more work to do before the thread is actually removed |
|
201 |
// from the threads list. Ensure that this has happened before proceeding. |
|
202 |
while (true) { |
|
203 |
PidJcmdExecutor jcmd = new PidJcmdExecutor(); |
|
204 |
OutputAnalyzer oa = jcmd.execute("Thread.print", true); |
|
205 |
String lockerThreadFound = oa.firstMatch("BiasLocker"); |
|
206 |
if (lockerThreadFound == null) { |
|
207 |
break; |
|
208 |
} |
|
209 |
}; |
|
210 |
||
211 |
MyLock l = lockerTask.get(); |
|
212 |
touch(l); |
|
213 |
||
214 |
recording.stop(); |
|
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
215 |
List<RecordedEvent> events = getRevocationEvents(recording, EventNames.BiasedLockRevocation, "lockClass", MyLock.class); |
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
216 |
Asserts.assertEQ(events.size(), 1); |
50113 | 217 |
|
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
218 |
RecordedEvent event = events.get(0); |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
219 |
Events.assertEventThread(event, Thread.currentThread()); |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
220 |
// Previous owner will usually be null, but can also be a thread that |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
221 |
// was created after the BiasLocker thread exited due to address reuse. |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
222 |
RecordedThread prevOwner = event.getValue("previousOwner"); |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
223 |
if (prevOwner != null) { |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
224 |
Asserts.assertNE(prevOwner.getJavaName(), "BiasLocker"); |
50113 | 225 |
} |
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
226 |
validateStackTrace(event.getStackTrace()); |
50113 | 227 |
} |
228 |
||
229 |
static void testBulkRevocationNoRebias() throws Throwable { |
|
230 |
class MyLock {}; |
|
231 |
||
232 |
Recording recording = new Recording(); |
|
233 |
||
234 |
recording.enable(EventNames.BiasedLockClassRevocation); |
|
235 |
recording.start(); |
|
236 |
||
237 |
Thread biasBreaker0 = triggerRevocation(BULK_REVOKE_THRESHOLD, MyLock.class); |
|
238 |
Thread biasBreaker1 = triggerRevocation(BULK_REVOKE_THRESHOLD, MyLock.class); |
|
239 |
||
240 |
recording.stop(); |
|
59005
01d47d511f5f
8231922: Could not find field with name revokedClass
mgronlun
parents:
55625
diff
changeset
|
241 |
List<RecordedEvent> events = getRevocationEvents(recording, EventNames.BiasedLockClassRevocation, "revokedClass", MyLock.class); |
50113 | 242 |
Asserts.assertEQ(events.size(), 2); |
243 |
||
244 |
// The rebias event should occur before the noRebias one |
|
50336
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
245 |
RecordedEvent eventRebias = events.get(0); |
1b6ea6bcd21a
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
rwestberg
parents:
50113
diff
changeset
|
246 |
RecordedEvent eventNoRebias = events.get(1); |
50113 | 247 |
|
248 |
Events.assertEventThread(eventRebias, biasBreaker0); |
|
249 |
Events.assertField(eventRebias, "disableBiasing").equal(false); |
|
250 |
||
251 |
Events.assertEventThread(eventNoRebias, biasBreaker1); |
|
252 |
Events.assertField(eventNoRebias, "disableBiasing").equal(true); |
|
253 |
||
254 |
RecordedClass lockClassRebias = eventRebias.getValue("revokedClass"); |
|
255 |
Asserts.assertEquals(lockClassRebias.getName(), MyLock.class.getName()); |
|
256 |
RecordedClass lockClassNoRebias = eventNoRebias.getValue("revokedClass"); |
|
257 |
Asserts.assertEquals(lockClassNoRebias.getName(), MyLock.class.getName()); |
|
258 |
||
259 |
validateStackTrace(eventRebias.getStackTrace()); |
|
260 |
validateStackTrace(eventNoRebias.getStackTrace()); |
|
261 |
} |
|
262 |
||
263 |
static void testRevocationSafepointIdCorrelation() throws Throwable { |
|
264 |
class MyLock {}; |
|
265 |
||
266 |
Recording recording = new Recording(); |
|
267 |
||
268 |
recording.enable(EventNames.BiasedLockClassRevocation); |
|
269 |
recording.enable(EventNames.ExecuteVMOperation); |
|
270 |
recording.start(); |
|
271 |
||
272 |
triggerRevocation(BULK_REVOKE_THRESHOLD, MyLock.class); |
|
273 |
||
274 |
recording.stop(); |
|
275 |
List<RecordedEvent> events = Events.fromRecording(recording); |
|
276 |
||
55625
f7e8dbb77156
8191890: Biased locking still uses the inferior stop the world safepoint for revocation
pchilanomate
parents:
52672
diff
changeset
|
277 |
// Determine which safepoints included bulk revocation VM operations |
52672
bbfa1b3aaf7e
8212108: SafepointSynchronizer never ending counter (big enough)
rehn
parents:
51214
diff
changeset
|
278 |
Set<Long> vmOperationsBulk = new HashSet<>(); |
50391
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
279 |
|
50113 | 280 |
for (RecordedEvent event : events) { |
50391
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
281 |
if (event.getEventType().getName().equals(EventNames.ExecuteVMOperation)) { |
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
282 |
String operation = event.getValue("operation"); |
52672
bbfa1b3aaf7e
8212108: SafepointSynchronizer never ending counter (big enough)
rehn
parents:
51214
diff
changeset
|
283 |
Long safepointId = event.getValue("safepointId"); |
50391
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
284 |
|
55625
f7e8dbb77156
8191890: Biased locking still uses the inferior stop the world safepoint for revocation
pchilanomate
parents:
52672
diff
changeset
|
285 |
if (operation.equals("BulkRevokeBias")) { |
50391
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
286 |
vmOperationsBulk.add(safepointId); |
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
287 |
} |
50113 | 288 |
} |
289 |
} |
|
290 |
||
291 |
int bulkRevokeCount = 0; |
|
292 |
||
293 |
// Match all revoke events to a corresponding VMOperation event |
|
294 |
for (RecordedEvent event : events) { |
|
55625
f7e8dbb77156
8191890: Biased locking still uses the inferior stop the world safepoint for revocation
pchilanomate
parents:
52672
diff
changeset
|
295 |
if (event.getEventType().getName().equals(EventNames.BiasedLockClassRevocation)) { |
52672
bbfa1b3aaf7e
8212108: SafepointSynchronizer never ending counter (big enough)
rehn
parents:
51214
diff
changeset
|
296 |
Long safepointId = event.getValue("safepointId"); |
50391
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
297 |
String lockClass = ((RecordedClass)event.getValue("revokedClass")).getName(); |
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
298 |
if (lockClass.toString().equals(MyLock.class.getName())) { |
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
299 |
Asserts.assertTrue(vmOperationsBulk.contains(safepointId)); |
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
300 |
bulkRevokeCount++; |
2bea53d7a27b
8202095: JFR TestBiasedLockRevocationEvents should cope with multiple events during a single safepoint
rwestberg
parents:
50336
diff
changeset
|
301 |
} |
50113 | 302 |
} |
303 |
} |
|
304 |
||
305 |
Asserts.assertGT(bulkRevokeCount, 0); |
|
306 |
} |
|
307 |
} |