-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GR-44559] Finish ThreadMXBean implementation for Native Image. #8430
Open
graalvmbot
wants to merge
2
commits into
master
Choose a base branch
from
fniephaus/GR-44559/jmx-thread-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...m.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_management_ThreadInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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 com.oracle.svm.core.jdk; | ||
|
||
import com.oracle.svm.core.annotate.Alias; | ||
import com.oracle.svm.core.annotate.TargetClass; | ||
|
||
@SuppressWarnings({"unused"}) | ||
@TargetClass(java.lang.management.ThreadInfo.class) | ||
final class Target_java_lang_management_ThreadInfo { | ||
|
||
@Alias | ||
Target_java_lang_management_ThreadInfo(Thread t, int state, Object lockObj, Thread lockOwner, | ||
long blockedCount, long blockedTime, | ||
long waitedCount, long waitedTime, | ||
StackTraceElement[] stackTrace, | ||
Object[] monitors, | ||
int[] stackDepths, | ||
Object[] synchronizers) { | ||
} | ||
} |
158 changes: 158 additions & 0 deletions
158
substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ThreadMXUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* 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 com.oracle.svm.core.jdk; | ||
|
||
import com.oracle.svm.core.SubstrateUtil; | ||
import com.oracle.svm.core.jdk.management.SubstrateThreadMXBean; | ||
import com.oracle.svm.core.locks.Target_java_util_concurrent_locks_AbstractOwnableSynchronizer; | ||
import com.oracle.svm.core.monitor.JavaMonitor; | ||
import com.oracle.svm.core.thread.JavaThreads.JMXMonitoring; | ||
import com.oracle.svm.core.thread.PlatformThreads; | ||
|
||
import java.lang.management.ThreadInfo; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.concurrent.locks.AbstractOwnableSynchronizer; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
/** | ||
* Utils to support {@link SubstrateThreadMXBean} for providing threading information for JMX | ||
* support. Include the {@link ThreadInfo} constructing utils, and deadlock detection utils. | ||
*/ | ||
public class ThreadMXUtils { | ||
|
||
public static class ThreadInfoConstructionUtils { | ||
|
||
private static StackTraceElement[] getStackTrace(Thread thread, int maxDepth) { | ||
StackTraceElement[] stackTrace = thread.getStackTrace(); | ||
return maxDepth == -1 || maxDepth >= stackTrace.length ? stackTrace : Arrays.copyOfRange(stackTrace, 0, maxDepth); | ||
} | ||
|
||
private record Blocker(Object blockObject, Thread ownerThread) { | ||
} | ||
|
||
private static Blocker getBlockerInfo(Thread thread) { | ||
Object blocker = LockSupport.getBlocker(thread); | ||
|
||
if (blocker instanceof JavaMonitor javaMonitor) { | ||
return new Blocker( | ||
javaMonitor.getBlockedObject(), | ||
SubstrateThreadMXBean.getThreadById(javaMonitor.getOwnerThreadId())); | ||
} else if (blocker instanceof AbstractOwnableSynchronizer synchronizer) { | ||
return new Blocker(synchronizer, | ||
SubstrateUtil.cast(synchronizer, Target_java_util_concurrent_locks_AbstractOwnableSynchronizer.class) | ||
.getExclusiveOwnerThread()); | ||
} | ||
return new Blocker(blocker, null); | ||
} | ||
|
||
private static Object[] getLockedSynchronizers(Thread thread) { | ||
return JMXMonitoring.getThreadLocks(thread); | ||
} | ||
|
||
private record LockedMonitors(Object[] monitorObjects, int[] monitorDepths) { | ||
} | ||
|
||
private static LockedMonitors getLockedMonitors(Thread thread, int stacktraceLength) { | ||
List<JMXMonitoring.MonitorInfo> monitors = JMXMonitoring.getThreadMonitors(thread); | ||
Object[] monitorObjects = monitors.stream().map(JMXMonitoring.MonitorInfo::originalObject).toArray(); | ||
int[] monitorDepths = monitors.stream().mapToInt(monitorInfo -> stacktraceLength < 0 ? -1 : stacktraceLength - monitorInfo.stacksize()).toArray(); | ||
return new LockedMonitors(monitorObjects, monitorDepths); | ||
} | ||
|
||
private static int getThreadState(Thread thread, boolean inNative) { | ||
int state = PlatformThreads.getThreadStatus(thread); | ||
if (inNative) { | ||
// set the JMM thread state native flag to true: | ||
state |= 0x00400000; | ||
} | ||
return state; | ||
} | ||
|
||
public static ThreadInfo getThreadInfo(Thread thread, int maxDepth, | ||
boolean withLockedMonitors, boolean withLockedSynchronizers) { | ||
Blocker blocker = getBlockerInfo(thread); | ||
StackTraceElement[] stackTrace = getStackTrace(thread, maxDepth); | ||
LockedMonitors lockedMonitors = getLockedMonitors(thread, stackTrace.length); | ||
boolean inNative = stackTrace.length > 0 && stackTrace[0].isNativeMethod(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct - you would need to access the VM-internal thread state of the isolate thread but this isn't accessible easily for threads other than the current thread. |
||
Target_java_lang_management_ThreadInfo targetThreadInfo = new Target_java_lang_management_ThreadInfo( | ||
thread, | ||
getThreadState(thread, inNative), | ||
blocker.blockObject, | ||
blocker.ownerThread, | ||
JMXMonitoring.getThreadTotalBlockedCount(thread), | ||
JMXMonitoring.getThreadTotalBlockedTime(thread), | ||
JMXMonitoring.getThreadTotalWaitedCount(thread), | ||
JMXMonitoring.getThreadTotalWaitedTime(thread), | ||
stackTrace, | ||
withLockedMonitors ? lockedMonitors.monitorObjects : new Object[0], | ||
withLockedMonitors ? lockedMonitors.monitorDepths : new int[0], | ||
withLockedSynchronizers ? getLockedSynchronizers(thread) : new Object[0]); | ||
return SubstrateUtil.cast(targetThreadInfo, ThreadInfo.class); | ||
} | ||
} | ||
|
||
public static class DeadlockDetectionUtils { | ||
/** | ||
* Returns an array of thread ids of blocked threads within some given array of ThreadInfo. | ||
* | ||
* @param threadInfos array of ThreadInfo for the threads among which the deadlocks should | ||
* be detected | ||
* @param byMonitorOnly true if we are interested only in the deadlocks blocked exclusively | ||
* on monitors | ||
* @return array containing thread ids of deadlocked threads | ||
*/ | ||
public static long[] findDeadlockedThreads(ThreadInfo[] threadInfos, boolean byMonitorOnly) { | ||
HashSet<Long> deadlocked = new HashSet<>(); | ||
for (ThreadInfo threadInfo : threadInfos) { | ||
HashSet<Long> chain = new HashSet<>(); | ||
ThreadInfo current = threadInfo; | ||
while (current != null && current.getLockInfo() != null && !deadlocked.contains(current.getThreadId())) { | ||
if (!chain.add(current.getThreadId())) { | ||
if (!byMonitorOnly || chain.stream().allMatch(DeadlockDetectionUtils::isBlockedByMonitor)) { | ||
deadlocked.addAll(chain); | ||
} | ||
chain.clear(); | ||
break; | ||
} | ||
long currentLockOwnerId = current.getLockOwnerId(); | ||
current = Arrays.stream(threadInfos).filter(ti -> ti.getThreadId() == currentLockOwnerId).findAny().orElse(null); | ||
} | ||
} | ||
return deadlocked.stream().mapToLong(threadId -> threadId).toArray(); | ||
} | ||
|
||
/** | ||
* Anything that is deadlocked can be blocked either by monitor (the object related to | ||
* JavaMonitor), or a lock (anything under AbstractOwnableSynchronizer). | ||
* | ||
* @return true if provided thread is blocked by a monitor | ||
*/ | ||
private static boolean isBlockedByMonitor(long threadId) { | ||
return LockSupport.getBlocker(SubstrateThreadMXBean.getThreadById(threadId)) instanceof JavaMonitor; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should this method return if the thread is no longer
a. alive
b. blocked
I think a lot of corner cases are missing here.