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_libLinkerInvokerUnnamed := $(LIBCXX)
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := $(LIBCXX) BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := $(LIBCXX)
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := $(LIBCXX) BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := $(LIBCXX)
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTracePinnedThreads := jvm.lib
else else
BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava
BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava
@ -89,6 +90,7 @@ else
BUILD_JDK_JTREG_LIBRARIES_LIBS_libExplicitAttach := -ljvm BUILD_JDK_JTREG_LIBRARIES_LIBS_libExplicitAttach := -ljvm
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libExplicitAttach := -pthread BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libExplicitAttach := -pthread
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libImplicitAttach := -pthread BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libImplicitAttach := -pthread
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTracePinnedThreads := -ljvm
BUILD_JDK_JTREG_EXCLUDE += exerevokeall.c BUILD_JDK_JTREG_EXCLUDE += exerevokeall.c
ifeq ($(call isTargetOs, linux), true) ifeq ($(call isTargetOs, linux), true)
BUILD_JDK_JTREG_LIBRARIES_LIBS_libInheritedChannel := -ljava 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 * @param printAll true to print all stack frames, false to only print the
* frames that are native or holding a monitor * frames that are native or holding a monitor
@ -103,7 +103,7 @@ class PinnedThreadPrinter {
.filter(f -> (f.isNativeMethod() || f.getMonitors().length > 0)) .filter(f -> (f.isNativeMethod() || f.getMonitors().length > 0))
.map(LiveStackFrame::getDeclaringClass) .map(LiveStackFrame::getDeclaringClass)
.findFirst() .findFirst()
.ifPresent(klass -> { .ifPresentOrElse(klass -> {
int hash = hash(stack); int hash = hash(stack);
Hashes hashes = HASHES.get(klass); Hashes hashes = HASHES.get(klass);
synchronized (hashes) { synchronized (hashes) {
@ -112,7 +112,7 @@ class PinnedThreadPrinter {
printStackTrace(stack, out, printAll); printStackTrace(stack, out, printAll);
} }
} }
}); }, () -> printStackTrace(stack, out, true)); // not found
} }
private static void printStackTrace(List<LiveStackFrame> stack, private static void printStackTrace(List<LiveStackFrame> stack,
@ -122,9 +122,9 @@ class PinnedThreadPrinter {
for (LiveStackFrame frame : stack) { for (LiveStackFrame frame : stack) {
var ste = frame.toStackTraceElement(); var ste = frame.toStackTraceElement();
int monitorCount = frame.getMonitors().length; int monitorCount = frame.getMonitors().length;
if (monitorCount > 0 || frame.isNativeMethod()) { if (monitorCount > 0) {
out.format(" %s <== monitors:%d%n", ste, monitorCount); out.format(" %s <== monitors:%d%n", ste, monitorCount);
} else if (printAll) { } else if (frame.isNativeMethod() || printAll) {
out.format(" %s%n", ste); out.format(" %s%n", ste);
} }
} }

View file

@ -23,11 +23,13 @@
/** /**
* @test * @test
* @bug 8284161 8289284
* @summary Basic test of debugging option to trace pinned threads * @summary Basic test of debugging option to trace pinned threads
* @requires vm.continuations * @requires vm.continuations
* @compile --enable-preview -source ${jdk.version} TracePinnedThreads.java * @enablePreview
* @run main/othervm --enable-preview -Djdk.tracePinnedThreads=full TracePinnedThreads * @library /test/lib
* @run main/othervm --enable-preview -Djdk.tracePinnedThreads=short TracePinnedThreads * @run testng/othervm -Djdk.tracePinnedThreads=full TracePinnedThreads
* @run testng/othervm -Djdk.tracePinnedThreads=short TracePinnedThreads
*/ */
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -35,32 +37,81 @@ import java.io.PrintStream;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.locks.LockSupport; 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 { public class TracePinnedThreads {
static final Object lock = new Object(); static final Object lock = new Object();
public static void main(String[] args) throws Exception { /**
ByteArrayOutputStream baos = new ByteArrayOutputStream(); * Parks current thread for 1 second.
*/
private static void park() {
long nanos = Duration.ofSeconds(1).toNanos();
LockSupport.parkNanos(nanos);
}
/**
* 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; PrintStream original = System.out;
System.setOut(new PrintStream(baos)); System.setOut(new PrintStream(baos, true));
try { try {
Thread.ofVirtual().start(() -> { VThreadRunner.run(task::run);
synchronized (lock) {
long nanos = Duration.ofSeconds(1).toNanos();
LockSupport.parkNanos(nanos);
}
}).join();
System.out.flush();
} finally { } finally {
System.setOut(original); System.setOut(original);
} }
String output = new String(baos.toByteArray());
String output = new String(baos.toByteArray()); // default charset
System.out.println(output); System.out.println(output);
return output;
}
String expected = "<== monitors:1"; /**
if (!output.contains(expected)) { * Tests that s1 contains s2.
throw new RuntimeException("expected: \"" + expected + "\""); */
} 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);
}
}