Skip to content
Permalink
Browse files
Split out thread tracker to allow use by debugger
  • Loading branch information
AlanBateman committed Mar 26, 2021
1 parent 1aff49f commit 2ae17c0a6436f2ad191146527dc36c5866c6d8f0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 44 deletions.
@@ -49,7 +49,7 @@
import jdk.internal.event.VirtualThreadSubmitRejectedEvent;
import jdk.internal.misc.InnocuousThread;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.ThreadDumper;
import jdk.internal.vm.ThreadTracker;
import jdk.internal.vm.annotation.ChangesCurrentThread;
import jdk.internal.vm.annotation.JvmtiMountTransition;
import sun.nio.ch.Interruptible;
@@ -438,7 +438,7 @@ private void afterTerminate(boolean executed) {
}

// notify thread dumper, no-op if not tracking threads
ThreadDumper.notifyTerminate(this);
ThreadTracker.notifyTerminate(this);

// clear references to thread locals, this method is assumed to be
// called on its carrier thread on which it terminated.
@@ -502,7 +502,7 @@ public void start() {
if (!compareAndSetState(NEW, STARTED)) {
throw new IllegalThreadStateException("Already started");
}
ThreadDumper.notifyStart(this); // no-op if threads not tracked
ThreadTracker.notifyStart(this); // no-op if threads not tracked
try {
submitRunContinuation();
} catch (RejectedExecutionException ree) {
@@ -46,57 +46,19 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import sun.nio.ch.Poller;
import sun.security.action.GetPropertyAction;

/**
* Thread dump support.
*
* This class defines methods to dump threads to an output stream or file in
* plain text or JSON format. Virtual threads are located if they are created in
* a structured way with a ThreadExecutor. This class optionally support tracking
* the start and termination of all virtual threads.
* a structured way with a ThreadExecutor.
*/
public class ThreadDumper {
private ThreadDumper() { }

// the set of all virtual threads when tracking is enabled, otherwise null
private static final Set<Thread> VIRTUAL_THREADS;
static {
String s = GetPropertyAction.privilegedGetProperty("jdk.trackAllVirtualThreads");
if (s != null && (s.isEmpty() || s.equalsIgnoreCase("true"))) {
VIRTUAL_THREADS = ConcurrentHashMap.newKeySet();
} else {
VIRTUAL_THREADS = null;
}
}

/**
* Notifies the thread dumper of new virtual thread. A no-op if tracking of
* virtual threads is not enabled.
*/
public static void notifyStart(Thread thread) {
Set<Thread> threads = VIRTUAL_THREADS;
if (threads != null) {
assert thread.isVirtual();
threads.add(thread);
}
}

/**
* Notifies the thread dumper that a virtual thread has virtual. A no-op if
* tracking of virtual threads is not enabled.
*/
public static void notifyTerminate(Thread thread) {
Set<Thread> threads = VIRTUAL_THREADS;
if (threads != null) {
assert thread.isVirtual();
threads.remove(thread);
}
}

/**
* A container of threads, backed by a ThreadExecutor.
*/
@@ -250,7 +212,7 @@ public static void dumpThreads(OutputStream out) {
}

// virtual threads
Set<Thread> threads = VIRTUAL_THREADS;
Set<Thread> threads = ThreadTracker.virtualThreads().orElse(null);
if (threads != null) {
threads.forEach(t -> dumpThread(t, t.getStackTrace(), ps));
} else {
@@ -356,7 +318,7 @@ private static void dumpThreadsToJson(PrintStream out) {
out.println(" \"threads\": [");

// virtual threads
Set<Thread> threads = VIRTUAL_THREADS;
Set<Thread> threads = ThreadTracker.virtualThreads().orElse(null);;
if (threads != null) {
threads.forEach(t -> dumpThreadToJson(t, t.getStackTrace(), out, true));
} else {
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2020, 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.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import sun.security.action.GetPropertyAction;

/**
* Thread tracking, for debugging/monitoring purposes.
*/
public class ThreadTracker {
// the set of all virtual threads when tracking is enabled, otherwise null
private static final Set<Thread> VIRTUAL_THREADS;
static {
String s = GetPropertyAction.privilegedGetProperty("jdk.trackAllVirtualThreads");
if (s != null && (s.isEmpty() || s.equalsIgnoreCase("true"))) {
VIRTUAL_THREADS = ConcurrentHashMap.newKeySet();
} else {
VIRTUAL_THREADS = null;
}
}

private ThreadTracker() { }

/**
* Notifies the thread dumper of new virtual thread. A no-op if tracking of
* virtual threads is not enabled.
*/
public static void notifyStart(Thread thread) {
Set<Thread> threads = VIRTUAL_THREADS;
if (threads != null) {
assert thread.isVirtual();
threads.add(thread);
}
}

/**
* Notifies the thread dumper that a virtual thread has virtual. A no-op if
* tracking of virtual threads is not enabled.
*/
public static void notifyTerminate(Thread thread) {
Set<Thread> threads = VIRTUAL_THREADS;
if (threads != null) {
assert thread.isVirtual();
threads.remove(thread);
}
}

/**
* Returns the set of virtual threads, if tracked, or an empty {@code Optional}
* if not tracked.
*/
public static Optional<Set<Thread>> virtualThreads() {
if (VIRTUAL_THREADS == null) {
return Optional.empty();
} else {
return Optional.of(Collections.unmodifiableSet(VIRTUAL_THREADS));
}
}
}

0 comments on commit 2ae17c0

Please sign in to comment.