1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Test JFR jdk.VirtualThreadPinned event recorded for contended monitor enter
* and Object.wait when pinned
* @requires vm.continuations & vm.hasJFR
* @modules jdk.jfr jdk.management
* @library /test/lib
* @run junit/othervm --enable-native-access=ALL-UNNAMED MonitorPinnedEvents
*/
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import jdk.jfr.consumer.RecordedMethod;
import jdk.jfr.consumer.RecordedThread;
import jdk.jfr.consumer.RecordingFile;
import jdk.test.lib.thread.VThreadPinner;
import jdk.test.lib.thread.VThreadRunner; // ensureParallelism requires jdk.management
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
class MonitorPinnedEvents {
// log to System.err so inlined with JUnit output
private static final PrintStream log = System.err;
// expected values for "blockingOperation" field in event
private static final String CONTENDED_MONITOR_ENTER = "Contended monitor enter";
private static final String OBJECT_WAIT = "Object.wait";
// expected value for "pinnedReason" field in event
private static final String NATIVE_FRAME = "Native or VM frame on stack";
// block or wait for 2s to allow minimum event duration be tested
private static final int DELAY = 2000;
@BeforeAll
static void setup() {
// need at least two carriers to test pinning
VThreadRunner.ensureParallelism(2);
}
/**
* Test jdk.VirtualThreadPinned recorded for a contended monitor enter.
*/
@Test
void testContentedMonitorEnter() throws Exception {
try (Recording recording = new Recording()) {
recording.enable("jdk.VirtualThreadPinned");
recording.start();
Thread vthread;
try {
Object lock = new Object();
synchronized (lock) {
// start virtual thread that blocks trying to acquire monitor while pinned
var ready = new AtomicBoolean();
vthread = Thread.ofVirtual().start(() -> {
VThreadPinner.runPinned(() -> {
ready.set(true);
synchronized (lock) { // <--- blocks here while pinned
}
});
});
await(ready, vthread, Thread.State.BLOCKED);
// sleep before releasing to ensure virtual thread is blocked for >= 2s
Thread.sleep(DELAY);
}
vthread.join();
} finally {
recording.stop();
}
// jdk.VirtualThreadPinned event should be recorded
RecordedEvent event = findPinnedEvent(recording);
testEvent(event, vthread, CONTENDED_MONITOR_ENTER, NATIVE_FRAME, DELAY);
}
}
/**
* Test jdk.VirtualThreadPinned recorded for a contended monitor enter where
* another thread may acquire the monitor when the main thread releases it. If
* the other thread acquires the monitor before the virtual thread then the event
* duration should include the time blocked until the other thread releases it.
*/
@Test
void testContentedMonitorEnter2() throws Exception {
try (Recording recording = new Recording()) {
recording.enable("jdk.VirtualThreadPinned");
recording.start();
// the thread that acquires the monitor after the main thread releases it
var nextOwner = new AtomicReference<Thread>();
Thread vthread;
try {
Object lock = new Object();
Thread thread2;
synchronized (lock) {
// start virtual thread that blocks trying to acquire monitor while pinned
var ready1 = new AtomicBoolean();
vthread = Thread.ofVirtual().start(() -> {
VThreadPinner.runPinned(() -> {
ready1.set(true);
synchronized (lock) { // <--- blocks here while pinned
nextOwner.compareAndSet(null, Thread.currentThread());
}
});
});
await(ready1, vthread, Thread.State.BLOCKED);
// start platform thread that blocks trying to acquire monitor
thread2 = Thread.ofPlatform().start(() -> {
synchronized (lock) {
if (nextOwner.compareAndSet(null, Thread.currentThread())) {
// delayed release of monitor if acquired before virtual thread
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
}
}
}
});
} // release lock, vthread or thread2 will acquire
vthread.join();
thread2.join();
} finally {
recording.stop();
}
// jdk.VirtualThreadPinned event should be recorded. If the platform thread
// acquired the monitor before the virtual thread then the event duration
// should be >= DELAY.
RecordedEvent event = findPinnedEvent(recording);
Thread winner = nextOwner.get();
assertNotNull(winner);
int minDuration = winner.isVirtual() ? 0 : DELAY;
testEvent(event, vthread, CONTENDED_MONITOR_ENTER, NATIVE_FRAME, minDuration);
}
}
/**
* Test jdk.VirtualThreadPinned recorded for Object.wait when notified.
*/
@ParameterizedTest
@ValueSource(booleans = { true, false })
void testObjectWaitNotify(boolean timed) throws Exception {
testObjectWait(timed, false);
}
/**
* Test jdk.VirtualThreadPinned recorded for Object.wait when interrupted.
*/
@ParameterizedTest
@ValueSource(booleans = { true, false })
void testObjectWaitInterrupt(boolean timed) throws Exception {
testObjectWait(timed, true);
}
/**
* Test jdk.VirtualThreadPinned recorded for Object.wait.
* @param timed true for a timed-wait, false for an untimed-wait
* @param interrupt true to interrupt the thread, false to notify
*/
private void testObjectWait(boolean timed, boolean interrupt) throws Exception {
try (Recording recording = new Recording()) {
recording.enable("jdk.VirtualThreadPinned");
recording.start();
Thread vthread;
try {
Object lock = new Object();
// start virtual thread that waits in Object.wait while pinned
var ready = new AtomicBoolean();
vthread = Thread.ofVirtual().start(() -> {
VThreadPinner.runPinned(() -> {
synchronized (lock) {
ready.set(true);
try {
// wait while pinned
if (timed) {
lock.wait(Long.MAX_VALUE);
} else {
lock.wait();
}
} catch (InterruptedException e) {
}
}
});
});
await(ready, vthread, timed ? Thread.State.TIMED_WAITING : Thread.State.WAITING);
// sleep to ensure virtual thread waits for >= 2s
Thread.sleep(DELAY);
// interrupt or notify thread so it resumes execution
if (interrupt) {
vthread.interrupt();
} else {
synchronized (lock) {
lock.notify();
}
}
vthread.join();
} finally {
recording.stop();
}
// jdk.VirtualThreadPinned event should be recorded
RecordedEvent event = findPinnedEvent(recording);
testEvent(event, vthread, OBJECT_WAIT, NATIVE_FRAME, DELAY);
}
}
/**
* Test jdk.VirtualThreadPinned recorded for Object.wait where the virtual thread
* is notified but may block when attempting to reenter. One event should be
* recorded. The event duration should include both the waiting time and the time
* blocked to reenter.
*/
@ParameterizedTest
@ValueSource(booleans = { true, false })
void testObjectWaitNotify2(boolean timed) throws Exception {
testObjectWait2(timed, false);
}
/**
* Test jdk.VirtualThreadPinned recorded for Object.wait where the virtual thread
* is interrupted but may block when attempting to reenter. One event should be
* recorded. The event duration should include both the waiting time and the time
* blocked to reenter.
*/
@ParameterizedTest
@ValueSource(booleans = { true, false })
void testObjectWaitInterrupt2(boolean timed) throws Exception {
testObjectWait2(timed, true);
}
/**
* Test jdk.VirtualThreadPinned recorded for Object.wait where the virtual thread
* blocks when attempting to reenter.
* @param timed true for a timed-wait, false for an untimed-wait
* @param interrupt true to interrupt the thread, false to notify
*/
private void testObjectWait2(boolean timed, boolean interrupt) throws Exception {
try (Recording recording = new Recording()) {
recording.enable("jdk.VirtualThreadPinned");
recording.start();
// the thread that acquires the monitor after the main thread releases it
var nextOwner = new AtomicReference<Thread>();
Thread vthread;
try {
Object lock = new Object();
// start virtual thread that waits in Object.wait while pinned
var ready1 = new AtomicBoolean();
vthread = Thread.ofVirtual().start(() -> {
VThreadPinner.runPinned(() -> {
boolean notify = !interrupt;
synchronized (lock) {
ready1.set(true);
try {
// wait while pinned
if (timed) {
lock.wait(Long.MAX_VALUE);
} else {
lock.wait();
}
if (notify) {
nextOwner.compareAndSet(null, Thread.currentThread());
}
} catch (InterruptedException e) {
if (interrupt) {
nextOwner.compareAndSet(null, Thread.currentThread());
}
}
}
});
});
await(ready1, vthread, timed ? Thread.State.TIMED_WAITING : Thread.State.WAITING);
// platform thread that blocks on monitor enter
Thread thread2;
synchronized (lock) {
thread2 = Thread.ofPlatform().start(() -> {
synchronized (lock) { // <--- blocks here
if (nextOwner.compareAndSet(null, Thread.currentThread())) {
// delayed release of monitor if acquired before virtual thread
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
}
}
}
});
// interrupt/notify and release monitor to allow one of the threads to acquire
if (interrupt) {
vthread.interrupt();
} else {
lock.notify();
}
}
vthread.join();
thread2.join();
} finally {
recording.stop();
}
// jdk.VirtualThreadPinned event should be recorded. If the platform thread
// acquired the monitor before the virtual thread re-entered then the event
// duration should be >= DELAY.
RecordedEvent event = findPinnedEvent(recording);
Thread winner = nextOwner.get();
assertNotNull(winner);
int minDuration = winner.isVirtual() ? 0 : DELAY;
testEvent(event, vthread, OBJECT_WAIT, NATIVE_FRAME, minDuration);
}
}
/**
* Test that the event was recorded by the expected virtual thread, the event has a
* "carrierThread", the "blockingOperation", and "pinnedReason" fields have the
* expected values, and the event duration is >= minDuration.
*/
private void testEvent(RecordedEvent event,
Thread vthread,
String expectedBlockingOp,
String expectedPinnedReason,
int minDuration) {
assertTrue(vthread.isVirtual());
assertEquals(vthread.threadId(), event.getThread().getId());
RecordedThread carrier = event.getValue("carrierThread");
assertFalse(carrier.isVirtual());
assertEquals(expectedBlockingOp, event.getString("blockingOperation"));
assertEquals(expectedPinnedReason, event.getString("pinnedReason"));
long duration = event.getDuration().toMillis();
assertTrue(duration >= (minDuration - 100),
"Duration " + duration + "ms, expected >= " + minDuration + "ms");
}
/**
* Find the expected jdk.VirtualThreadPinned event in the recording. There may be
* several pinned events recorded by other parts of the system that need to be
* filtered out.
*/
private RecordedEvent findPinnedEvent(Recording recording) throws IOException {
Map<Boolean, List<RecordedEvent>> events = find(recording, "jdk.VirtualThreadPinned")
.collect(Collectors.partitioningBy(e -> filtered(topFrameMethod(e)), Collectors.toList()));
List<RecordedEvent> filteredEvents = events.get(Boolean.TRUE);
List<RecordedEvent> pinnedEvents = events.get(Boolean.FALSE);
log.format("%d event(s) recorded%n", filteredEvents.size() + pinnedEvents.size());
log.println("-- filtered events --");
log.println(filteredEvents);
log.println("-- remaining pinned events --");
log.println(pinnedEvents);
assertEquals(1, pinnedEvents.size());
return pinnedEvents.get(0);
}
/**
* Pinned events recorded at the following classes/methods should be ignored.
*/
private final Set<String> FILTERED = Set.of(
"java.lang.VirtualThread.*",
"java.lang.ref.ReferenceQueue.poll",
"java.lang.invoke.*",
"jdk.internal.ref.*"
);
/**
* Returns true if the given top-frame {@code class.method} should be ignored.
*/
private boolean filtered(String topFrameMethod) {
for (String s : FILTERED) {
if (s.endsWith("*")) {
String prefix = s.substring(0, s.length()-1);
if (topFrameMethod.startsWith(prefix)) {
return true;
}
} else if (s.equals(topFrameMethod)) {
return true;
}
}
return false;
}
/**
* Returns the {@code class.method} of the top frame in the event's stack trace.
*/
private String topFrameMethod(RecordedEvent event) {
RecordedFrame topFrame = event.getStackTrace().getFrames().get(0);
RecordedMethod method = topFrame.getMethod();
return method.getType().getName() + "." + method.getName();
}
/**
* Returns the events with the given name in the recording.
*/
private Stream<RecordedEvent> find(Recording recording, String name) throws IOException {
Path recordingFile = recording.getDestination();
if (recordingFile == null) {
ProcessHandle h = ProcessHandle.current();
recordingFile = Path.of("recording-" + recording.getId() + "-pid" + h.pid() + ".jfr");
recording.dump(recordingFile);
}
return RecordingFile.readAllEvents(recordingFile)
.stream()
.filter(e -> e.getEventType().getName().equals(name));
}
/**
* Waits for ready to become true, then waits for the thread to get to the given state.
*/
private void await(AtomicBoolean ready,
Thread thread,
Thread.State expectedState) throws InterruptedException {
while (!ready.get()) {
Thread.sleep(10);
}
Thread.State state = thread.getState();
while (state != expectedState) {
Thread.sleep(10);
state = thread.getState();
}
}
}
|