Skip to content

Commit

Permalink
8289284: jdk.tracePinnedThreads output confusing when pinned due to n…
Browse files Browse the repository at this point in the history
…ative frame

Reviewed-by: jpai, mchung
  • Loading branch information
Alan Bateman committed Jul 13, 2022
1 parent 572c14e commit f528124
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 24 deletions.
2 changes: 2 additions & 0 deletions make/test/JtregNativeJdk.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/java.base/share/classes/java/lang/PinnedThreadPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static int hash(List<LiveStackFrame> stack) {
}

/**
* 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
Expand All @@ -103,7 +103,7 @@ static void printStackTrace(PrintStream out, boolean printAll) {
.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) {
Expand All @@ -112,7 +112,7 @@ static void printStackTrace(PrintStream out, boolean printAll) {
printStackTrace(stack, out, printAll);
}
}
});
}, () -> printStackTrace(stack, out, true)); // not found
}

private static void printStackTrace(List<LiveStackFrame> stack,
Expand All @@ -122,9 +122,9 @@ private static void printStackTrace(List<LiveStackFrame> stack,
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);
}
}
Expand Down
89 changes: 70 additions & 19 deletions test/jdk/java/lang/Thread/virtual/TracePinnedThreads.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,95 @@

/**
* @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;
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();
/**
* 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;
System.setOut(new PrintStream(baos));
System.setOut(new PrintStream(baos, true));
try {
Thread.ofVirtual().start(() -> {
synchronized (lock) {
long nanos = Duration.ofSeconds(1).toNanos();
LockSupport.parkNanos(nanos);
}
}).join();
System.out.flush();
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!!");
}
}
31 changes: 31 additions & 0 deletions test/jdk/java/lang/Thread/virtual/libTracePinnedThreads.c
Original file line number Diff line number Diff line change
@@ -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);
}
}

1 comment on commit f528124

@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.