8337199: Add jcmd Thread.vthread_scheduler and Thread.vthread_pollers diagnostic commands

Reviewed-by: dholmes, kevinw
This commit is contained in:
Alan Bateman 2024-12-03 07:24:46 +00:00
parent 3eb5461578
commit 5c8cb2edcb
11 changed files with 383 additions and 12 deletions

View file

@ -55,6 +55,7 @@ import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Supplier;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
@ -2304,6 +2305,10 @@ public final class System {
return VirtualThread.defaultScheduler();
}
public Stream<ScheduledExecutorService> virtualThreadDelayedTaskSchedulers() {
return VirtualThread.delayedTaskSchedulers();
}
public StackWalker newStackWalkerInstance(Set<StackWalker.Option> options,
ContinuationScope contScope,
Continuation continuation) {

View file

@ -24,6 +24,7 @@
*/
package java.lang;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
@ -32,12 +33,12 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import jdk.internal.event.VirtualThreadEndEvent;
import jdk.internal.event.VirtualThreadStartEvent;
import jdk.internal.event.VirtualThreadSubmitFailedEvent;
@ -192,6 +193,13 @@ final class VirtualThread extends BaseVirtualThread {
return DEFAULT_SCHEDULER;
}
/**
* Returns a stream of the delayed task schedulers used to support timed operations.
*/
static Stream<ScheduledExecutorService> delayedTaskSchedulers() {
return Arrays.stream(DELAYED_TASK_SCHEDULERS);
}
/**
* Returns the continuation scope used for virtual threads.
*/

View file

@ -45,6 +45,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Stream;
import jdk.internal.loader.NativeLibraries;
@ -595,6 +596,11 @@ public interface JavaLangAccess {
*/
Executor virtualThreadDefaultScheduler();
/**
* Returns a stream of the delayed task schedulers used for virtual threads.
*/
Stream<ScheduledExecutorService> virtualThreadDelayedTaskSchedulers();
/**
* Creates a new StackWalker
*/

View file

@ -0,0 +1,112 @@
/*
* Copyright (c) 2023, 2024, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
package jdk.internal.vm;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.IntStream;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import sun.nio.ch.Poller;
/**
* The implementation for the jcmd Thread.vthread_* diagnostic commands. These methods are
* called from the "Attach Listener" thread.
*/
public class JcmdVThreadCommands {
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
private JcmdVThreadCommands() { }
/**
* Invoked by the VM to print the virtual scheduler to a byte[].
*/
private static byte[] printScheduler() {
StringBuilder sb = new StringBuilder();
// virtual thread scheduler
sb.append(JLA.virtualThreadDefaultScheduler())
.append(System.lineSeparator());
// break
sb.append(System.lineSeparator());
// delayed task schedulers
sb.append("Delayed task schedulers:").append(System.lineSeparator());
var delayedTaskSchedulers = JLA.virtualThreadDelayedTaskSchedulers().toList();
IntStream.range(0, delayedTaskSchedulers.size())
.forEach(i -> sb.append('[')
.append(i)
.append("] ")
.append(delayedTaskSchedulers.get(i))
.append(System.lineSeparator()));
return sb.toString().getBytes(StandardCharsets.UTF_8);
}
/**
* Invoked by the VM to print the I/O pollers to a byte[].
*/
private static byte[] printPollers() {
StringBuilder sb = new StringBuilder();
Poller masterPoller = Poller.masterPoller();
List<Poller> readPollers = Poller.readPollers();
List<Poller> writePollers = Poller.writePollers();
if (masterPoller != null) {
sb.append("Master I/O poller:")
.append(System.lineSeparator())
.append(masterPoller)
.append(System.lineSeparator());
// break
sb.append(System.lineSeparator());
}
sb.append("Read I/O pollers:");
sb.append(System.lineSeparator());
IntStream.range(0, readPollers.size())
.forEach(i -> sb.append('[')
.append(i)
.append("] ")
.append(readPollers.get(i))
.append(System.lineSeparator()));
// break
sb.append(System.lineSeparator());
sb.append("Write I/O pollers:");
sb.append(System.lineSeparator());
IntStream.range(0, writePollers.size())
.forEach(i -> sb.append('[')
.append(i)
.append("] ")
.append(writePollers.get(i))
.append(System.lineSeparator()));
return sb.toString().getBytes(StandardCharsets.UTF_8);
}
}

View file

@ -36,6 +36,7 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.locks.LockSupport;
import java.util.function.BooleanSupplier;
import jdk.internal.misc.InnocuousThread;
import jdk.internal.vm.annotation.Stable;
/**
* Polls file descriptors. Virtual threads invoke the poll method to park
@ -53,6 +54,9 @@ public abstract class Poller {
}
}
// the poller or sub-poller thread
private @Stable Thread owner;
// maps file descriptors to parked Thread
private final Map<Integer, Thread> map = new ConcurrentHashMap<>();
@ -238,6 +242,7 @@ public abstract class Poller {
* descriptor that is polled.
*/
private void pollerLoop() {
owner = Thread.currentThread();
try {
for (;;) {
poll(-1);
@ -258,6 +263,7 @@ public abstract class Poller {
*/
private void subPollerLoop(Poller masterPoller) {
assert Thread.currentThread().isVirtual();
owner = Thread.currentThread();
try {
int polled = 0;
for (;;) {
@ -282,7 +288,8 @@ public abstract class Poller {
@Override
public String toString() {
return Objects.toIdentityString(this) + " [registered = " + registered() + "]";
return String.format("%s [registered = %d, owner = %s]",
Objects.toIdentityString(this), registered(), owner);
}
/**
@ -442,4 +449,25 @@ public abstract class Poller {
}
}
}
/**
* Return the master poller or null if there is no master poller.
*/
public static Poller masterPoller() {
return POLLERS.masterPoller();
}
/**
* Return the list of read pollers.
*/
public static List<Poller> readPollers() {
return POLLERS.readPollers();
}
/**
* Return the list of write pollers.
*/
public static List<Poller> writePollers() {
return POLLERS.writePollers();
}
}