Skip to content

Commit fcedde8

Browse files
txominpeluDavid Holmes
authored andcommitted
8330846: Add stacks of mounted virtual threads to the HotSpot thread dump
Reviewed-by: dholmes, alanb
1 parent 74468bc commit fcedde8

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/hotspot/share/runtime/threads.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,15 @@ void Threads::print_on(outputStream* st, bool print_stacks,
13211321
p->trace_stack();
13221322
} else {
13231323
p->print_stack_on(st);
1324+
const oop thread_oop = p->threadObj();
1325+
if (thread_oop != nullptr) {
1326+
if (p->is_vthread_mounted()) {
1327+
const oop vt = p->vthread();
1328+
assert(vt != nullptr, "vthread should not be null when vthread is mounted");
1329+
st->print_cr(" Mounted virtual thread \"%s\" #" INT64_FORMAT, JavaThread::name_for(vt), (int64_t)java_lang_Thread::thread_id(vt));
1330+
p->print_vthread_stack_on(st);
1331+
}
1332+
}
13241333
}
13251334
}
13261335
st->cr();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import jdk.test.lib.dcmd.CommandExecutor;
25+
import jdk.test.lib.dcmd.JMXExecutor;
26+
import jdk.test.lib.process.OutputAnalyzer;
27+
import org.junit.Test;
28+
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.atomic.AtomicBoolean;
31+
import java.util.regex.Pattern;
32+
33+
/*
34+
* @test
35+
* @summary Test of diagnostic command Thread.print with virtual threads
36+
* @requires vm.continuations
37+
* @library /test/lib
38+
* @run junit PrintMountedVirtualThread
39+
*/
40+
public class PrintMountedVirtualThread {
41+
42+
public void run(CommandExecutor executor) throws InterruptedException {
43+
var shouldFinish = new AtomicBoolean(false);
44+
var started = new CountDownLatch(1);
45+
final Runnable runnable = new DummyRunnable(shouldFinish, started);
46+
try {
47+
Thread vthread = Thread.ofVirtual().name("Dummy Vthread").start(runnable);
48+
started.await();
49+
/* Execute */
50+
OutputAnalyzer output = executor.execute("Thread.print");
51+
output.shouldMatch(".*at " + Pattern.quote(DummyRunnable.class.getName()) + "\\.run.*");
52+
output.shouldMatch(".*at " + Pattern.quote(DummyRunnable.class.getName()) + "\\.compute.*");
53+
output.shouldMatch("Mounted virtual thread " + "\"Dummy Vthread\"" + " #" + vthread.threadId());
54+
55+
} finally {
56+
shouldFinish.set(true);
57+
}
58+
}
59+
60+
@Test
61+
public void jmx() throws InterruptedException {
62+
run(new JMXExecutor());
63+
}
64+
65+
static class DummyRunnable implements Runnable {
66+
67+
private final AtomicBoolean shouldFinish;
68+
private final CountDownLatch started;
69+
70+
public DummyRunnable(AtomicBoolean shouldFinish, CountDownLatch started) {
71+
this.shouldFinish = shouldFinish;
72+
this.started = started;
73+
}
74+
75+
public void run() {
76+
compute();
77+
}
78+
79+
void compute() {
80+
started.countDown();
81+
while (!shouldFinish.get()) {
82+
Thread.onSpinWait();
83+
}
84+
}
85+
}
86+
87+
88+
}

0 commit comments

Comments
 (0)