Skip to content

Commit

Permalink
8306446: java/lang/management/ThreadMXBean/Locks.java transient failures
Browse files Browse the repository at this point in the history
Reviewed-by: cjplummer, sspitsyn
  • Loading branch information
kevinjwalls committed Jul 28, 2023
1 parent cad6114 commit 4ae5a3e
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions test/jdk/java/lang/management/ThreadMXBean/Locks.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
Expand Down Expand Up @@ -43,8 +43,11 @@

public class Locks {

private static final Object OBJA = new Object();
private static final Object OBJB = new Object();
private static class ObjectA { }
private static class ObjectB { }

private static final Object OBJA = new ObjectA();
private static final Object OBJB = new ObjectB();
private static final EnhancedWaiter OBJC = new EnhancedWaiter();
private static final ThreadMXBean TM = ManagementFactory.getThreadMXBean();
private static final LockFreeLogger LOGGER = new LockFreeLogger();
Expand All @@ -65,6 +68,8 @@ private static void assertNoLock(Thread t) {
TM.getThreadInfo(TM.getAllThreadIds(), true, true))
.filter(Objects::nonNull)
.filter(i -> name.equals(i.getLockOwnerName()))
/* Carrier Thread can hold a lock on a VirtualThread, which we ignore: */
.filter(i -> !i.getLockName().contains("java.lang.VirtualThread"))
.findAny();
if (result.isPresent()) {
throw new RuntimeException("Thread " + t.getName() + " is not "
Expand Down Expand Up @@ -117,34 +122,34 @@ private static void assertThreadState(Thread t, Thread.State expectedState) {
*/
private static void checkBlockedObject(Thread t, Object lock, Thread owner) {
long tid = t.getId();
String result = TM.getThreadInfo(tid).getLockName();
String lockName = TM.getThreadInfo(tid).getLockName();
final String expectedLock = (lock != null ? getLockName(lock) : null);
Predicate<String> p = (res) -> ((res != null && !res.equals(expectedLock))
|| (res == null && expectedLock != null));

if (p.test(result)) {
if (p.test(lockName)) {
printStackTrace(t);
int retryCount = 0;
while (p.test(result)) {
while (p.test(lockName)) {
if (retryCount++ > 500) {
printStackTrace(t);
throw new RuntimeException("Thread " + t.getName() + " is blocked on "
+ expectedLock + " but got " + result);
+ expectedLock + " but got " + lockName);
}
goSleep(100);
result = TM.getThreadInfo(tid).getLockName();
lockName = TM.getThreadInfo(tid).getLockName();
}
}

result = TM.getThreadInfo(tid).getLockOwnerName();
final String expectedOwner = (owner != null ? owner.getName() : null);
String lockOwnerName = TM.getThreadInfo(tid).getLockOwnerName();
final String expectedOwnerName = (owner != null ? owner.getName() : null);

p = (res) -> ((res != null && !res.equals(expectedOwner))
|| (res == null && expectedOwner != null));
if (p.test(result)) {
p = (res) -> ((res != null && !res.equals(expectedOwnerName))
|| (res == null && expectedOwnerName != null));
if (p.test(lockOwnerName)) {
printStackTrace(t);
throw new RuntimeException("Owner of " + lock + " should be "
+ expectedOwner + " but got " + result);
+ expectedOwnerName + " but got " + lockOwnerName);
}
}

Expand Down Expand Up @@ -342,14 +347,13 @@ Exception result() {
public static void main(String args[]) throws Exception {
try {
Thread mainThread = Thread.currentThread();

// Test uncontested case
LockAThread t1;
LockBThread t2;

Phaser p = new Phaser(3);
synchronized(OBJC) {
// Make sure the main thread is not holding any lock
// Make sure the main thread is not holding any lock (except possibly a VirtualThread)
assertNoLock(mainThread);

// Test deadlock case
Expand All @@ -362,15 +366,22 @@ public static void main(String args[]) throws Exception {
t2.start();

p.arriveAndAwaitAdvance(); // Phase 1 (blocking)

assertThreadState(t2, Thread.State.BLOCKED);
checkBlockedObject(t2, OBJC, mainThread);
if (!mainThread.isVirtual()) {
// ThreadInfo not available for Virtual Threads.
checkBlockedObject(t2, OBJC, mainThread);
}
assertThreadState(t1, Thread.State.BLOCKED);
checkBlockedObject(t1, OBJB, t2);

long[] expectedThreads = new long[3];
long[] expectedThreads = new long[mainThread.isVirtual() ? 2: 3];
expectedThreads[0] = t1.getId(); // blocked on lockB
expectedThreads[1] = t2.getId(); // owner of lockB blocking on lockC
expectedThreads[2] = mainThread.getId(); // owner of lockC
if (!mainThread.isVirtual()) {
// ThreadInfo not available for Virtual Threads.
expectedThreads[2] = mainThread.getId(); // owner of lockC
}
findThreadsBlockedOn(OBJB, expectedThreads);
}
p.arriveAndAwaitAdvance(); // Phase 2 (blocking)
Expand Down Expand Up @@ -400,6 +411,9 @@ private static ThreadInfo findOwnerInfo(ThreadInfo[] infos, String lock)
throws Exception {
ThreadInfo ownerInfo = null;
for (ThreadInfo info : infos) {
if (info == null) {
continue; // Missing thread, e.g. completed. Ignore.
}
String blockedLock = info.getLockName();
if (lock.equals(blockedLock)) {
long threadId = info.getLockOwnerId();
Expand All @@ -421,19 +435,22 @@ private static void findThreadsBlockedOn(Object o, long[] expectedThreads)
throws Exception {
String lock = getLockName(o);
// Check with ThreadInfo with no stack trace (i.e. no safepoint)
ThreadInfo[] infos = TM.getThreadInfo(TM.getAllThreadIds());
doCheck(infos, lock, expectedThreads);
ThreadInfo[] allThreadInfos = TM.getThreadInfo(TM.getAllThreadIds());
doCheck(allThreadInfos, lock, expectedThreads);

// Check with ThreadInfo with stack trace
infos = TM.getThreadInfo(TM.getAllThreadIds(), 1);
doCheck(infos, lock, expectedThreads);
allThreadInfos = TM.getThreadInfo(TM.getAllThreadIds(), 1);
doCheck(allThreadInfos, lock, expectedThreads);
}

private static void doCheck(ThreadInfo[] infos, String lock, long[] expectedThreads)
throws Exception {
ThreadInfo ownerInfo = null;
// Find the thread who is blocking on lock
for (ThreadInfo info : infos) {
if (info == null) {
continue; // Missing thread, e.g. completed. Ignore.
}
String blockedLock = info.getLockName();
if (lock.equals(blockedLock)) {
log("%s blocked on %s", info.getThreadName(), blockedLock);
Expand All @@ -445,11 +462,19 @@ private static void doCheck(ThreadInfo[] infos, String lock, long[] expectedThre
"Can't retrieve ThreadInfo for the blocked thread");
}

// Follow chain of locks:
long[] threads = new long[10];
int count = 0;
threads[count++] = ownerInfo.getThreadId();
while (ownerInfo.getThreadState() == Thread.State.BLOCKED) {
ownerInfo = findOwnerInfo(infos, lock);
log("ownerInfo = %s", ownerInfo);
if (ownerInfo.getThreadName().contains("ForkJoinPool")) {
// Ignore e.g. "ForkJoinPool-1-worker-1" waiting on a VirtualThread
log ("skipping %s", ownerInfo);
lock = ownerInfo.getLockName();
continue;
}
threads[count++] = ownerInfo.getThreadId();
log(" Owner = %s id = %d",
ownerInfo.getThreadName(),
Expand All @@ -468,14 +493,19 @@ private static void doCheck(ThreadInfo[] infos, String lock, long[] expectedThre
throw new RuntimeException("TEST FAILED: " +
"Expected chain of threads not matched; current count =" + count);
}
int failures = 0;
for (int i = 0; i < count; i++) {
if (threads[i] != expectedThreads[i]) {
log("TEST FAILED: Unexpected thread in the chain %s expected to be %s",
threads[i],
expectedThreads[i]
);
failures++;
}
}
if (failures > 0) {
throw new RuntimeException("TEST FAILED: " + failures + " unexpected thread(s).");
}
}

private static void log(String format, Object ... args) {
Expand Down

1 comment on commit 4ae5a3e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.