| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.io.PrintStream; |
| 9 |
import java.io.PrintWriter; |
| 10 |
import java.io.StringWriter; |
| 11 |
import java.lang.management.LockInfo; |
| 12 |
import java.lang.management.ManagementFactory; |
| 13 |
import java.lang.management.MonitorInfo; |
| 14 |
import java.lang.management.ThreadInfo; |
| 15 |
import java.lang.management.ThreadMXBean; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.Timer; |
| 19 |
import java.util.TimerTask; |
| 20 |
import org.h2.engine.SysProperties; |
| 21 |
import org.h2.mvstore.db.MVTable; |
| 22 |
|
| 23 |
/** |
| 24 |
* Detects deadlocks between threads. Prints out data in the same format as the |
| 25 |
* CTRL-BREAK handler, but includes information about table locks. |
| 26 |
*/ |
| 27 |
public class ThreadDeadlockDetector { |
| 28 |
|
| 29 |
private static final String INDENT = " "; |
| 30 |
|
| 31 |
private static ThreadDeadlockDetector detector; |
| 32 |
|
| 33 |
private final ThreadMXBean threadBean; |
| 34 |
|
| 35 |
private ThreadDeadlockDetector() { |
| 36 |
this.threadBean = ManagementFactory.getThreadMXBean(); |
| 37 |
|
| 38 |
// a daemon thread |
| 39 |
// delay: 10 ms |
| 40 |
// period: 10000 ms (100 seconds) |
| 41 |
Timer threadCheck = new Timer("ThreadDeadlockDetector", true); |
| 42 |
threadCheck.schedule(new TimerTask() { |
| 43 |
@Override |
| 44 |
public void run() { |
| 45 |
checkForDeadlocks(); |
| 46 |
} |
| 47 |
}, 10, 10_000); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Initialize the detector. |
| 52 |
*/ |
| 53 |
public static synchronized void init() { |
| 54 |
if (detector == null) { |
| 55 |
detector = new ThreadDeadlockDetector(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Checks if any threads are deadlocked. If any, print the thread dump |
| 61 |
* information. |
| 62 |
*/ |
| 63 |
void checkForDeadlocks() { |
| 64 |
long[] deadlockedThreadIds = threadBean.findDeadlockedThreads(); |
| 65 |
if (deadlockedThreadIds == null) { |
| 66 |
return; |
| 67 |
} |
| 68 |
dumpThreadsAndLocks("ThreadDeadlockDetector - deadlock found :", |
| 69 |
threadBean, deadlockedThreadIds, System.out); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Dump all deadlocks (if any). |
| 74 |
* |
| 75 |
* @param msg the message |
| 76 |
*/ |
| 77 |
public static void dumpAllThreadsAndLocks(String msg) { |
| 78 |
dumpAllThreadsAndLocks(msg, System.out); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Dump all deadlocks (if any). |
| 83 |
* |
| 84 |
* @param msg the message |
| 85 |
* @param out the output |
| 86 |
*/ |
| 87 |
public static void dumpAllThreadsAndLocks(String msg, PrintStream out) { |
| 88 |
final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); |
| 89 |
final long[] allThreadIds = threadBean.getAllThreadIds(); |
| 90 |
dumpThreadsAndLocks(msg, threadBean, allThreadIds, out); |
| 91 |
} |
| 92 |
|
| 93 |
private static void dumpThreadsAndLocks(String msg, ThreadMXBean threadBean, |
| 94 |
long[] threadIds, PrintStream out) { |
| 95 |
final StringWriter stringWriter = new StringWriter(); |
| 96 |
final PrintWriter print = new PrintWriter(stringWriter); |
| 97 |
|
| 98 |
print.println(msg); |
| 99 |
|
| 100 |
final HashMap<Long, String> tableWaitingForLockMap; |
| 101 |
final HashMap<Long, ArrayList<String>> tableExclusiveLocksMap; |
| 102 |
final HashMap<Long, ArrayList<String>> tableSharedLocksMap; |
| 103 |
if (SysProperties.THREAD_DEADLOCK_DETECTOR) { |
| 104 |
tableWaitingForLockMap = MVTable.WAITING_FOR_LOCK |
| 105 |
.getSnapshotOfAllThreads(); |
| 106 |
tableExclusiveLocksMap = MVTable.EXCLUSIVE_LOCKS |
| 107 |
.getSnapshotOfAllThreads(); |
| 108 |
tableSharedLocksMap = MVTable.SHARED_LOCKS |
| 109 |
.getSnapshotOfAllThreads(); |
| 110 |
} else { |
| 111 |
tableWaitingForLockMap = new HashMap<>(); |
| 112 |
tableExclusiveLocksMap = new HashMap<>(); |
| 113 |
tableSharedLocksMap = new HashMap<>(); |
| 114 |
} |
| 115 |
|
| 116 |
final ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, |
| 117 |
true); |
| 118 |
for (ThreadInfo ti : infos) { |
| 119 |
printThreadInfo(print, ti); |
| 120 |
printLockInfo(print, ti.getLockedSynchronizers(), |
| 121 |
tableWaitingForLockMap.get(ti.getThreadId()), |
| 122 |
tableExclusiveLocksMap.get(ti.getThreadId()), |
| 123 |
tableSharedLocksMap.get(ti.getThreadId())); |
| 124 |
} |
| 125 |
|
| 126 |
print.flush(); |
| 127 |
// Dump it to system.out in one block, so it doesn't get mixed up with |
| 128 |
// other stuff when we're using a logging subsystem. |
| 129 |
out.println(stringWriter.getBuffer()); |
| 130 |
out.flush(); |
| 131 |
} |
| 132 |
|
| 133 |
private static void printThreadInfo(PrintWriter print, ThreadInfo ti) { |
| 134 |
// print thread information |
| 135 |
printThread(print, ti); |
| 136 |
|
| 137 |
// print stack trace with locks |
| 138 |
StackTraceElement[] stackTrace = ti.getStackTrace(); |
| 139 |
MonitorInfo[] monitors = ti.getLockedMonitors(); |
| 140 |
for (int i = 0; i < stackTrace.length; i++) { |
| 141 |
StackTraceElement e = stackTrace[i]; |
| 142 |
print.println(INDENT + "at " + e.toString()); |
| 143 |
for (MonitorInfo mi : monitors) { |
| 144 |
if (mi.getLockedStackDepth() == i) { |
| 145 |
print.println(INDENT + " - locked " + mi); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
print.println(); |
| 150 |
} |
| 151 |
|
| 152 |
private static void printThread(PrintWriter print, ThreadInfo ti) { |
| 153 |
print.print("\"" + ti.getThreadName() + "\"" + " Id=" |
| 154 |
+ ti.getThreadId() + " in " + ti.getThreadState()); |
| 155 |
if (ti.getLockName() != null) { |
| 156 |
print.append(" on lock=").append(ti.getLockName()); |
| 157 |
} |
| 158 |
if (ti.isSuspended()) { |
| 159 |
print.append(" (suspended)"); |
| 160 |
} |
| 161 |
if (ti.isInNative()) { |
| 162 |
print.append(" (running in native)"); |
| 163 |
} |
| 164 |
print.println(); |
| 165 |
if (ti.getLockOwnerName() != null) { |
| 166 |
print.println(INDENT + " owned by " + ti.getLockOwnerName() + " Id=" |
| 167 |
+ ti.getLockOwnerId()); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private static void printLockInfo(PrintWriter print, LockInfo[] locks, |
| 172 |
String tableWaitingForLock, |
| 173 |
ArrayList<String> tableExclusiveLocks, |
| 174 |
ArrayList<String> tableSharedLocksMap) { |
| 175 |
print.println(INDENT + "Locked synchronizers: count = " + locks.length); |
| 176 |
for (LockInfo li : locks) { |
| 177 |
print.println(INDENT + " - " + li); |
| 178 |
} |
| 179 |
if (tableWaitingForLock != null) { |
| 180 |
print.println(INDENT + "Waiting for table: " + tableWaitingForLock); |
| 181 |
} |
| 182 |
if (tableExclusiveLocks != null) { |
| 183 |
print.println(INDENT + "Exclusive table locks: count = " + tableExclusiveLocks.size()); |
| 184 |
for (String name : tableExclusiveLocks) { |
| 185 |
print.println(INDENT + " - " + name); |
| 186 |
} |
| 187 |
} |
| 188 |
if (tableSharedLocksMap != null) { |
| 189 |
print.println(INDENT + "Shared table locks: count = " + tableSharedLocksMap.size()); |
| 190 |
for (String name : tableSharedLocksMap) { |
| 191 |
print.println(INDENT + " - " + name); |
| 192 |
} |
| 193 |
} |
| 194 |
print.println(); |
| 195 |
} |
| 196 |
|
| 197 |
} |