8289284: jdk.tracePinnedThreads output confusing when pinned due to native frame

Reviewed-by: jpai, mchung
This commit is contained in:
Alan Bateman 2022-07-13 15:03:37 +00:00
parent 572c14efc6
commit f528124f57
4 changed files with 108 additions and 24 deletions

View File

@ -77,6 +77,7 @@ ifeq ($(call isTargetOs, windows), true)
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerUnnamed := $(LIBCXX)
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := $(LIBCXX)
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := $(LIBCXX)
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTracePinnedThreads := jvm.lib
else
BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava
BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava
@ -89,6 +90,7 @@ else
BUILD_JDK_JTREG_LIBRARIES_LIBS_libExplicitAttach := -ljvm
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libExplicitAttach := -pthread
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libImplicitAttach := -pthread
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTracePinnedThreads := -ljvm
BUILD_JDK_JTREG_EXCLUDE += exerevokeall.c
ifeq ($(call isTargetOs, linux), true)
BUILD_JDK_JTREG_LIBRARIES_LIBS_libInheritedChannel := -ljava

View File

@ -86,7 +86,7 @@ class PinnedThreadPrinter {
}
/**
* Prints the stack trace of the current mounted vthread continuation.
* Prints the continuation stack trace.
*
* @param printAll true to print all stack frames, false to only print the
* frames that are native or holding a monitor
@ -103,7 +103,7 @@ class PinnedThreadPrinter {
.filter(f -> (f.isNativeMethod() || f.getMonitors().length > 0))
.map(LiveStackFrame::getDeclaringClass)
.findFirst()
.ifPresent(klass -> {
.ifPresentOrElse(klass -> {
int hash = hash(stack);
Hashes hashes = HASHES.get(klass);
synchronized (hashes) {
@ -112,7 +112,7 @@ class PinnedThreadPrinter {
printStackTrace(stack, out, printAll);
}
}
});
}, () -> printStackTrace(stack, out, true)); // not found
}
private static void printStackTrace(List<LiveStackFrame> stack,
@ -122,9 +122,9 @@ class PinnedThreadPrinter {
for (LiveStackFrame frame : stack) {
var ste = frame.toStackTraceElement();
int monitorCount = frame.getMonitors().length;
if (monitorCount > 0 || frame.isNativeMethod()) {
if (monitorCount > 0) {
out.format(" %s <== monitors:%d%n", ste, monitorCount);
} else if (printAll) {
} else if (frame.isNativeMethod() || printAll) {
out.format(" %s%n", ste);
}
}

View File

@ -23,11 +23,13 @@
/**
* @test
* @bug 8284161 8289284
* @summary Basic test of debugging option to trace pinned threads
* @requires vm.continuations
* @compile --enable-preview -source ${jdk.version} TracePinnedThreads.java
* @run main/othervm --enable-preview -Djdk.tracePinnedThreads=full TracePinnedThreads
* @run main/othervm --enable-preview -Djdk.tracePinnedThreads=short TracePinnedThreads
* @enablePreview
* @library /test/lib
* @run testng/othervm -Djdk.tracePinnedThreads=full TracePinnedThreads
* @run testng/othervm -Djdk.tracePinnedThreads=short TracePinnedThreads
*/
import java.io.ByteArrayOutputStream;
@ -35,32 +37,81 @@ import java.io.PrintStream;
import java.time.Duration;
import java.util.concurrent.locks.LockSupport;
import jdk.test.lib.thread.VThreadRunner;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class TracePinnedThreads {
static final Object lock = new Object();
public static void main(String[] args) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream original = System.out;
System.setOut(new PrintStream(baos));
try {
Thread.ofVirtual().start(() -> {
synchronized (lock) {
/**
* Parks current thread for 1 second.
*/
private static void park() {
long nanos = Duration.ofSeconds(1).toNanos();
LockSupport.parkNanos(nanos);
}
}).join();
System.out.flush();
/**
* Invokes the park method through a native frame to park the current
* thread for 1 second.
*/
private static native void invokePark();
/**
* Test parking inside synchronized block.
*/
@Test
public void testPinnedCausedBySynchronizedBlock() throws Exception {
String output = run(() -> {
synchronized (lock) {
park();
}
});
assertContains(output, "<== monitors:1");
assertDoesNotContain(output, "(Native Method)");
}
/**
* Test parking with native frame on stack.
*/
@Test
public void testPinnedCausedByNativeMethod() throws Exception {
System.loadLibrary("TracePinnedThreads");
String output = run(() -> invokePark());
assertContains(output, "(Native Method)");
assertDoesNotContain(output, "<== monitors");
}
/**
* Runs a task in a virutal thread, returning a String with any output printed
* to standard output.
*/
private static String run(Runnable task) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream original = System.out;
System.setOut(new PrintStream(baos, true));
try {
VThreadRunner.run(task::run);
} finally {
System.setOut(original);
}
String output = new String(baos.toByteArray()); // default charset
String output = new String(baos.toByteArray());
System.out.println(output);
return output;
}
String expected = "<== monitors:1";
if (!output.contains(expected)) {
throw new RuntimeException("expected: \"" + expected + "\"");
}
/**
* Tests that s1 contains s2.
*/
private static void assertContains(String s1, String s2) {
assertTrue(s1.contains(s2), s2 + " not found!!!");
}
/**
* Tests that s1 does not contain s2.
*/
private static void assertDoesNotContain(String s1, String s2) {
assertFalse(s1.contains(s2), s2 + " found!!");
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2022, 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.
*/
#include "jni.h"
JNIEXPORT void JNICALL Java_TracePinnedThreads_invokePark(JNIEnv *env, jclass clazz) {
jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "park", "()V");
if (mid != NULL) {
(*env)->CallStaticVoidMethod(env, clazz, mid);
}
}