|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.core.jdk; |
| 26 | + |
| 27 | +import com.oracle.svm.core.SubstrateUtil; |
| 28 | +import com.oracle.svm.core.jdk.management.SubstrateThreadMXBean; |
| 29 | +import com.oracle.svm.core.locks.Target_java_util_concurrent_locks_AbstractOwnableSynchronizer; |
| 30 | +import com.oracle.svm.core.monitor.JavaMonitor; |
| 31 | +import com.oracle.svm.core.thread.JavaThreads.JMXMonitoring; |
| 32 | +import com.oracle.svm.core.thread.PlatformThreads; |
| 33 | + |
| 34 | +import java.lang.management.ThreadInfo; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.List; |
| 38 | +import java.util.concurrent.locks.AbstractOwnableSynchronizer; |
| 39 | +import java.util.concurrent.locks.LockSupport; |
| 40 | + |
| 41 | +/** |
| 42 | + * Utils to support {@link SubstrateThreadMXBean} for providing threading information for JMX |
| 43 | + * support. Include the {@link ThreadInfo} constructing utils, and deadlock detection utils. |
| 44 | + */ |
| 45 | +public class ThreadMXUtils { |
| 46 | + |
| 47 | + public static class ThreadInfoConstructionUtils { |
| 48 | + |
| 49 | + private static StackTraceElement[] getStackTrace(Thread thread, int maxDepth) { |
| 50 | + StackTraceElement[] stackTrace = thread.getStackTrace(); |
| 51 | + return maxDepth == -1 || maxDepth >= stackTrace.length ? stackTrace : Arrays.copyOfRange(stackTrace, 0, maxDepth); |
| 52 | + } |
| 53 | + |
| 54 | + private record Blocker(Object blockObject, Thread ownerThread) { |
| 55 | + } |
| 56 | + |
| 57 | + private static Blocker getBlockerInfo(Thread thread) { |
| 58 | + Object blocker = LockSupport.getBlocker(thread); |
| 59 | + |
| 60 | + if (blocker instanceof JavaMonitor javaMonitor) { |
| 61 | + return new Blocker( |
| 62 | + javaMonitor.getBlockedObject(), |
| 63 | + SubstrateThreadMXBean.getThreadById(javaMonitor.getOwnerThreadId())); |
| 64 | + } else if (blocker instanceof AbstractOwnableSynchronizer synchronizer) { |
| 65 | + return new Blocker(synchronizer, |
| 66 | + SubstrateUtil.cast(synchronizer, Target_java_util_concurrent_locks_AbstractOwnableSynchronizer.class) |
| 67 | + .getExclusiveOwnerThread()); |
| 68 | + } |
| 69 | + return new Blocker(blocker, null); |
| 70 | + } |
| 71 | + |
| 72 | + private static Object[] getLockedSynchronizers(Thread thread) { |
| 73 | + return JMXMonitoring.getThreadLocks(thread); |
| 74 | + } |
| 75 | + |
| 76 | + private record LockedMonitors(Object[] monitorObjects, int[] monitorDepths) { |
| 77 | + } |
| 78 | + |
| 79 | + private static LockedMonitors getLockedMonitors(Thread thread, int stacktraceLength) { |
| 80 | + List<JMXMonitoring.MonitorInfo> monitors = JMXMonitoring.getThreadMonitors(thread); |
| 81 | + Object[] monitorObjects = monitors.stream().map(JMXMonitoring.MonitorInfo::originalObject).toArray(); |
| 82 | + int[] monitorDepths = monitors.stream().mapToInt(monitorInfo -> stacktraceLength < 0 ? -1 : stacktraceLength - monitorInfo.stacksize()).toArray(); |
| 83 | + return new LockedMonitors(monitorObjects, monitorDepths); |
| 84 | + } |
| 85 | + |
| 86 | + private static int getThreadState(Thread thread, boolean inNative) { |
| 87 | + int state = PlatformThreads.getThreadStatus(thread); |
| 88 | + if (inNative) { |
| 89 | + // set the JMM thread state native flag to true: |
| 90 | + state |= 0x00400000; |
| 91 | + } |
| 92 | + return state; |
| 93 | + } |
| 94 | + |
| 95 | + public static ThreadInfo getThreadInfo(Thread thread, int maxDepth, |
| 96 | + boolean withLockedMonitors, boolean withLockedSynchronizers) { |
| 97 | + Blocker blocker = getBlockerInfo(thread); |
| 98 | + StackTraceElement[] stackTrace = getStackTrace(thread, maxDepth); |
| 99 | + LockedMonitors lockedMonitors = getLockedMonitors(thread, stackTrace.length); |
| 100 | + boolean inNative = stackTrace.length > 0 && stackTrace[0].isNativeMethod(); |
| 101 | + Target_java_lang_management_ThreadInfo targetThreadInfo = new Target_java_lang_management_ThreadInfo( |
| 102 | + thread, |
| 103 | + getThreadState(thread, inNative), |
| 104 | + blocker.blockObject, |
| 105 | + blocker.ownerThread, |
| 106 | + JMXMonitoring.getThreadTotalBlockedCount(thread), |
| 107 | + JMXMonitoring.getThreadTotalBlockedTime(thread), |
| 108 | + JMXMonitoring.getThreadTotalWaitedCount(thread), |
| 109 | + JMXMonitoring.getThreadTotalWaitedTime(thread), |
| 110 | + stackTrace, |
| 111 | + withLockedMonitors ? lockedMonitors.monitorObjects : new Object[0], |
| 112 | + withLockedMonitors ? lockedMonitors.monitorDepths : new int[0], |
| 113 | + withLockedSynchronizers ? getLockedSynchronizers(thread) : new Object[0]); |
| 114 | + return SubstrateUtil.cast(targetThreadInfo, ThreadInfo.class); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static class DeadlockDetectionUtils { |
| 119 | + /** |
| 120 | + * Returns an array of thread ids of blocked threads within some given array of ThreadInfo. |
| 121 | + * |
| 122 | + * @param threadInfos array of ThreadInfo for the threads among which the deadlocks should |
| 123 | + * be detected |
| 124 | + * @param byMonitorOnly true if we are interested only in the deadlocks blocked exclusively |
| 125 | + * on monitors |
| 126 | + * @return array containing thread ids of deadlocked threads |
| 127 | + */ |
| 128 | + public static long[] findDeadlockedThreads(ThreadInfo[] threadInfos, boolean byMonitorOnly) { |
| 129 | + HashSet<Long> deadlocked = new HashSet<>(); |
| 130 | + for (ThreadInfo threadInfo : threadInfos) { |
| 131 | + HashSet<Long> chain = new HashSet<>(); |
| 132 | + ThreadInfo current = threadInfo; |
| 133 | + while (current != null && current.getLockInfo() != null && !deadlocked.contains(current.getThreadId())) { |
| 134 | + if (!chain.add(current.getThreadId())) { |
| 135 | + if (!byMonitorOnly || chain.stream().allMatch(DeadlockDetectionUtils::isBlockedByMonitor)) { |
| 136 | + deadlocked.addAll(chain); |
| 137 | + } |
| 138 | + chain.clear(); |
| 139 | + break; |
| 140 | + } |
| 141 | + long currentLockOwnerId = current.getLockOwnerId(); |
| 142 | + current = Arrays.stream(threadInfos).filter(ti -> ti.getThreadId() == currentLockOwnerId).findAny().orElse(null); |
| 143 | + } |
| 144 | + } |
| 145 | + return deadlocked.stream().mapToLong(threadId -> threadId).toArray(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Anything that is deadlocked can be blocked either by monitor (the object related to |
| 150 | + * JavaMonitor), or a lock (anything under AbstractOwnableSynchronizer). |
| 151 | + * |
| 152 | + * @return true if provided thread is blocked by a monitor |
| 153 | + */ |
| 154 | + private static boolean isBlockedByMonitor(long threadId) { |
| 155 | + return LockSupport.getBlocker(SubstrateThreadMXBean.getThreadById(threadId)) instanceof JavaMonitor; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments