|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 2017, 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.jdk.management.SubstrateThreadMXBean; |
| 28 | +import com.oracle.svm.core.locks.Target_java_util_concurrent_locks_AbstractOwnableSynchronizer; |
| 29 | +import com.oracle.svm.core.monitor.JavaMonitor; |
| 30 | +import com.oracle.svm.core.thread.JavaThreads.JMXMonitoring; |
| 31 | +import com.oracle.svm.core.thread.PlatformThreads; |
| 32 | +import com.oracle.svm.core.thread.VMThreads; |
| 33 | +import org.graalvm.collections.EconomicSet; |
| 34 | + |
| 35 | +import java.lang.management.ThreadInfo; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.concurrent.locks.AbstractOwnableSynchronizer; |
| 39 | +import java.util.concurrent.locks.LockSupport; |
| 40 | +import java.util.concurrent.locks.ReentrantLock; |
| 41 | + |
| 42 | +/** |
| 43 | + * Utils to support {@link SubstrateThreadMXBean} for providing threading information for JMX |
| 44 | + * support. Include the {@link ThreadInfo} constructing utils, and deadlock detection utils. |
| 45 | + */ |
| 46 | +public class ThreadMXUtils { |
| 47 | + public static class ThreadInfoConstructionUtils { |
| 48 | + |
| 49 | + private static StackTraceElement[] getStackTrace(Thread thread, int maxDepth) { |
| 50 | + return maxDepth == -1 || maxDepth >= thread.getStackTrace().length ? thread.getStackTrace() : Arrays.copyOfRange(thread.getStackTrace(), 0, maxDepth); |
| 51 | + } |
| 52 | + |
| 53 | + private static int getThreadState(Thread thread) { |
| 54 | + int state = PlatformThreads.getThreadStatus(thread); |
| 55 | + boolean inNative = VMThreads.StatusSupport |
| 56 | + .getStatusVolatile(PlatformThreads.getIsolateThreadUnsafe(thread)) == VMThreads.StatusSupport.STATUS_IN_NATIVE; |
| 57 | + if (inNative) { |
| 58 | + // set the JMM thread state native flag to true: |
| 59 | + state |= 0x00400000; |
| 60 | + } |
| 61 | + return state; |
| 62 | + } |
| 63 | + |
| 64 | + private record Blocker(Object blockObject, Thread ownerThread) { |
| 65 | + } |
| 66 | + |
| 67 | + private static Blocker getBlockerInfo(Thread thread) { |
| 68 | + Object blocker = LockSupport.getBlocker(thread); |
| 69 | + |
| 70 | + if (blocker instanceof JavaMonitor javaMonitor) { |
| 71 | + return new Blocker( |
| 72 | + javaMonitor.getBlockedObject(), |
| 73 | + SubstrateThreadMXBean.getThreadById(javaMonitor.getOwnerThreadId())); |
| 74 | + } else if (blocker instanceof AbstractOwnableSynchronizer synchronizer) { |
| 75 | + return new Blocker(synchronizer, Target_java_util_concurrent_locks_AbstractOwnableSynchronizer.class |
| 76 | + .cast(synchronizer).getExclusiveOwnerThread()); |
| 77 | + } |
| 78 | + return new Blocker(blocker, null); |
| 79 | + } |
| 80 | + |
| 81 | + private static Object[] getLockedSynchronizers(Thread thread) { |
| 82 | + EconomicSet<AbstractOwnableSynchronizer> locks = JMXMonitoring.getThreadLocks(thread); |
| 83 | + Object[] lockObjects = new Object[locks != null ? locks.size() : 0]; |
| 84 | + for (int i = 0; i < lockObjects.length; i++) { |
| 85 | + lockObjects[i] = locks.iterator().next(); |
| 86 | + } |
| 87 | + return lockObjects; |
| 88 | + } |
| 89 | + |
| 90 | + private record LockedMonitors(Object[] monitorObjects, int[] monitorDepths) { |
| 91 | + } |
| 92 | + |
| 93 | + private static LockedMonitors getLockedMonitors(Thread thread) { |
| 94 | + EconomicSet<JavaMonitor> monitors = JMXMonitoring.getThreadMonitors(thread); |
| 95 | + Object[] monitorObjects = new Object[monitors != null ? monitors.size() : 0]; |
| 96 | + int[] monitorDepths = new int[monitorObjects.length]; |
| 97 | + for (int i = 0; i < monitorObjects.length; i++) { |
| 98 | + JavaMonitor javaMonitor = monitors.iterator().next(); |
| 99 | + monitorObjects[i] = javaMonitor.getBlockedObject(); |
| 100 | + monitorDepths[i] = javaMonitor.getDepth(); |
| 101 | + } |
| 102 | + return new LockedMonitors(monitorObjects, monitorDepths); |
| 103 | + } |
| 104 | + |
| 105 | + public static ThreadInfo getThreadInfo(Thread thread, int maxDepth, |
| 106 | + boolean withLockedMonitors, boolean withLockedSynchronizers) { |
| 107 | + assert thread != null; |
| 108 | + Blocker blocker = getBlockerInfo(thread); |
| 109 | + LockedMonitors lockedMonitors = getLockedMonitors(thread); |
| 110 | + |
| 111 | + Target_java_lang_management_ThreadInfo targetThreadInfo = new Target_java_lang_management_ThreadInfo( |
| 112 | + thread, |
| 113 | + getThreadState(thread), |
| 114 | + blocker.blockObject, |
| 115 | + blocker.ownerThread, |
| 116 | + JMXMonitoring.getThreadTotalBlockedCount(thread), |
| 117 | + JMXMonitoring.getThreadTotalBlockedTime(thread), |
| 118 | + JMXMonitoring.getThreadTotalWaitedCount(thread), |
| 119 | + JMXMonitoring.getThreadTotalWaitedTime(thread), |
| 120 | + getStackTrace(thread, maxDepth), |
| 121 | + withLockedMonitors ? lockedMonitors.monitorObjects : new Object[0], |
| 122 | + withLockedMonitors ? lockedMonitors.monitorDepths : new int[0], |
| 123 | + withLockedSynchronizers ? getLockedSynchronizers(thread) : new Object[0]); |
| 124 | + return ThreadInfo.class.cast(targetThreadInfo); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public static class DeadlockDetectionUtils { |
| 129 | + private static final ReentrantLock lock = new ReentrantLock(); |
| 130 | + private static final HashSet<Long> deadlocked = new HashSet<>(); |
| 131 | + private static final HashSet<Long> chain = new HashSet<>(); |
| 132 | + private static ThreadInfo[] allThreadInfos = new ThreadInfo[0]; |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns an array of thread ids of blocked threads within some given array of ThreadInfo. |
| 136 | + * |
| 137 | + * @param threadInfos array of ThreadInfo for the threads among which the deadlocks should |
| 138 | + * be detected |
| 139 | + * @param byMonitorOnly true if we are interested only in the deadlocks blocked exclusively |
| 140 | + * on monitors |
| 141 | + * @return array containing thread ids of deadlocked threads |
| 142 | + */ |
| 143 | + public static long[] findDeadlockedThreads(ThreadInfo[] threadInfos, boolean byMonitorOnly) { |
| 144 | + lock.lock(); |
| 145 | + try { |
| 146 | + allThreadInfos = threadInfos; |
| 147 | + deadlocked.clear(); |
| 148 | + chain.clear(); |
| 149 | + |
| 150 | + Arrays.stream(allThreadInfos) |
| 151 | + .filter(threadInfo -> !deadlocked.contains(threadInfo.getThreadId())) |
| 152 | + .forEach(threadInfo -> checkBlocker(threadInfo, byMonitorOnly)); |
| 153 | + return deadlocked.stream().mapToLong(threadId -> threadId).toArray(); |
| 154 | + } finally { |
| 155 | + lock.unlock(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static void checkBlocker(ThreadInfo currentThreadInfo, boolean byMonitorOnly) { |
| 160 | + if (chain.contains(currentThreadInfo.getThreadId())) { |
| 161 | + releaseCurrentChain(byMonitorOnly); |
| 162 | + } else { |
| 163 | + chain.add(currentThreadInfo.getThreadId()); |
| 164 | + Arrays.stream(allThreadInfos) |
| 165 | + .filter(threadInfo -> threadInfo.getThreadId() == currentThreadInfo.getLockOwnerId() && |
| 166 | + threadInfo.getLockInfo() != null) |
| 167 | + .findAny() |
| 168 | + .ifPresentOrElse(threadInfo -> checkBlocker(threadInfo, byMonitorOnly), |
| 169 | + chain::clear); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static void releaseCurrentChain(boolean byMonitorOnly) { |
| 174 | + if (!byMonitorOnly || chain.stream().allMatch(DeadlockDetectionUtils::isBlockedByMonitor)) { |
| 175 | + deadlocked.addAll(chain); |
| 176 | + } |
| 177 | + chain.clear(); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Anything that is deadlocked can be blocked either by monitor (the object related to |
| 182 | + * JavaMonitor), or a lock (anything under AbstractOwnableSynchronizer). |
| 183 | + * |
| 184 | + * @return true if provided thread is blocked by a monitor |
| 185 | + */ |
| 186 | + private static boolean isBlockedByMonitor(long threadId) { |
| 187 | + return LockSupport.getBlocker(SubstrateThreadMXBean.getThreadById(threadId)) instanceof JavaMonitor; |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments