use of java.lang.management.LockInfo in project openj9 by eclipse.
the class TestLockInfo method testLockInfo.
/*
* Test method for 'java.lang.management.LockInfo.LockInfo(String, int)'
*/
@Test
public void testLockInfo() throws Exception {
AssertJUnit.assertEquals(lockName, goodLI.getClassName());
AssertJUnit.assertEquals(idHashCode, goodLI.getIdentityHashCode());
// The bad case ...
try {
LockInfo li = new LockInfo(null, System.identityHashCode(null));
Assert.fail("Should have thrown NPE");
} catch (NullPointerException e) {
// expected
}
}
use of java.lang.management.LockInfo in project openj9 by eclipse.
the class TestThreadInfo method testGetLockInfo.
/*
* Test method for 'java.lang.management.ThreadInfo.getLockInfo()'
*/
@Test
public void testGetLockInfo() {
LockInfo info = goodThreadInfo.getLockInfo();
AssertJUnit.assertEquals(TestLockInfo.GOOD_CD_CLASSNAME, info.getClassName());
AssertJUnit.assertEquals(TestLockInfo.GOOD_CD_IDHASHCODE, info.getIdentityHashCode());
String lockName = TestLockInfo.GOOD_CD_CLASSNAME + "@" + Integer.toHexString(TestLockInfo.GOOD_CD_IDHASHCODE);
AssertJUnit.assertEquals(lockName, info.toString());
// if the CompositeData does not contains any information about
// LockInfo, use LockName to build LockInfo instead
goodThreadInfo = ThreadInfo.from(createGoodCompositeDataNoLockInfo());
AssertJUnit.assertNotNull(goodThreadInfo);
info = goodThreadInfo.getLockInfo();
AssertJUnit.assertEquals(GOOD_LOCK_CLASSNAME, info.getClassName());
AssertJUnit.assertEquals(Integer.parseInt(GOOD_LOCK_HASH), info.getIdentityHashCode());
AssertJUnit.assertEquals(GOOD_LOCK_CLASSNAME + "@" + Integer.toHexString(Integer.parseInt(GOOD_LOCK_HASH)), info.toString());
}
use of java.lang.management.LockInfo in project openj9 by eclipse.
the class TestThreadInfo method testGetLockedSynchronizers.
/*
* Test method for
* 'java.lang.management.ThreadInfo.getLockedSynchronizers()'
*/
@Test
public void testGetLockedSynchronizers() {
LockInfo[] infos = goodThreadInfo.getLockedSynchronizers();
AssertJUnit.assertEquals(3, infos.length);
for (LockInfo info : infos) {
AssertJUnit.assertEquals(TestLockInfo.GOOD_CD_CLASSNAME, info.getClassName());
AssertJUnit.assertEquals(TestLockInfo.GOOD_CD_IDHASHCODE, info.getIdentityHashCode());
String lockName = TestLockInfo.GOOD_CD_CLASSNAME + "@" + Integer.toHexString(TestLockInfo.GOOD_CD_IDHASHCODE);
AssertJUnit.assertEquals(lockName, info.toString());
}
}
use of java.lang.management.LockInfo in project openj9 by eclipse.
the class TestThreadInfo method testGetLockInfo_RealThread.
/*
* Test method for 'java.lang.management.ThreadInfo.getLockInfo()' in real
* thread
*/
@Test
public void testGetLockInfo_RealThread() throws Exception {
// current thread
ThreadMXBean mb = ManagementFactory.getThreadMXBean();
ThreadInfo ti = mb.getThreadInfo(Thread.currentThread().getId());
AssertJUnit.assertEquals(-1, ti.getLockOwnerId());
AssertJUnit.assertNull(ti.getLockName());
AssertJUnit.assertNull(ti.getLockOwnerName());
AssertJUnit.assertEquals(Thread.State.RUNNABLE, ti.getThreadState());
logger.debug("Current thread name: " + ti.getThreadName());
AssertJUnit.assertEquals(false, ti.isSuspended());
AssertJUnit.assertEquals(0, ti.getStackTrace().length);
AssertJUnit.assertEquals(0, ti.getLockedMonitors().length);
AssertJUnit.assertEquals(0, ti.getLockedSynchronizers().length);
AssertJUnit.assertNull(ti.getLockInfo());
// a waiting thread
final Object lock = new Object();
class Waiting extends Thread {
public void run() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
// ignore
}
}
}
}
Thread thread = new Waiting();
thread.start();
Thread.sleep(1000);
ti = mb.getThreadInfo(thread.getId());
String className = "java.lang.Object";
int hashCode = System.identityHashCode(lock);
String lockName = className + "@" + Integer.toHexString(hashCode);
AssertJUnit.assertEquals(-1, ti.getLockOwnerId());
AssertJUnit.assertEquals(lockName, ti.getLockName());
AssertJUnit.assertNull(ti.getLockOwnerName());
AssertJUnit.assertEquals(Thread.State.WAITING, ti.getThreadState());
AssertJUnit.assertEquals(false, ti.isSuspended());
AssertJUnit.assertEquals(0, ti.getStackTrace().length);
AssertJUnit.assertEquals(0, ti.getLockedMonitors().length);
AssertJUnit.assertEquals(0, ti.getLockedSynchronizers().length);
LockInfo lockInfo = ti.getLockInfo();
AssertJUnit.assertEquals(className, lockInfo.getClassName());
AssertJUnit.assertEquals(hashCode, lockInfo.getIdentityHashCode());
AssertJUnit.assertEquals(lockName, lockInfo.toString());
synchronized (lock) {
lock.notifyAll();
}
}
use of java.lang.management.LockInfo in project nifi-minifi by apache.
the class BootstrapListener method writeDump.
private static void writeDump(final OutputStream out) throws IOException {
final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
final ThreadInfo[] infos = mbean.dumpAllThreads(true, true);
final long[] deadlockedThreadIds = mbean.findDeadlockedThreads();
final long[] monitorDeadlockThreadIds = mbean.findMonitorDeadlockedThreads();
final List<ThreadInfo> sortedInfos = new ArrayList<>(infos.length);
for (final ThreadInfo info : infos) {
sortedInfos.add(info);
}
Collections.sort(sortedInfos, new Comparator<ThreadInfo>() {
@Override
public int compare(ThreadInfo o1, ThreadInfo o2) {
return o1.getThreadName().toLowerCase().compareTo(o2.getThreadName().toLowerCase());
}
});
final StringBuilder sb = new StringBuilder();
for (final ThreadInfo info : sortedInfos) {
sb.append("\n");
sb.append("\"").append(info.getThreadName()).append("\" Id=");
sb.append(info.getThreadId()).append(" ");
sb.append(info.getThreadState().toString()).append(" ");
switch(info.getThreadState()) {
case BLOCKED:
case TIMED_WAITING:
case WAITING:
sb.append(" on ");
sb.append(info.getLockInfo());
break;
default:
break;
}
if (info.isSuspended()) {
sb.append(" (suspended)");
}
if (info.isInNative()) {
sb.append(" (in native code)");
}
if (deadlockedThreadIds != null && deadlockedThreadIds.length > 0) {
for (final long id : deadlockedThreadIds) {
if (id == info.getThreadId()) {
sb.append(" ** DEADLOCKED THREAD **");
}
}
}
if (monitorDeadlockThreadIds != null && monitorDeadlockThreadIds.length > 0) {
for (final long id : monitorDeadlockThreadIds) {
if (id == info.getThreadId()) {
sb.append(" ** MONITOR-DEADLOCKED THREAD **");
}
}
}
final StackTraceElement[] stackTraces = info.getStackTrace();
for (final StackTraceElement element : stackTraces) {
sb.append("\n\tat ").append(element);
final MonitorInfo[] monitors = info.getLockedMonitors();
for (final MonitorInfo monitor : monitors) {
if (monitor.getLockedStackFrame().equals(element)) {
sb.append("\n\t- waiting on ").append(monitor);
}
}
}
final LockInfo[] lockInfos = info.getLockedSynchronizers();
if (lockInfos.length > 0) {
sb.append("\n\t");
sb.append("Number of Locked Synchronizers: ").append(lockInfos.length);
for (final LockInfo lockInfo : lockInfos) {
sb.append("\n\t- ").append(lockInfo.toString());
}
}
sb.append("\n");
}
if (deadlockedThreadIds != null && deadlockedThreadIds.length > 0) {
sb.append("\n\nDEADLOCK DETECTED!");
sb.append("\nThe following thread IDs are deadlocked:");
for (final long id : deadlockedThreadIds) {
sb.append("\n").append(id);
}
}
if (monitorDeadlockThreadIds != null && monitorDeadlockThreadIds.length > 0) {
sb.append("\n\nMONITOR DEADLOCK DETECTED!");
sb.append("\nThe following thread IDs are deadlocked:");
for (final long id : monitorDeadlockThreadIds) {
sb.append("\n").append(id);
}
}
writer.write(sb.toString());
writer.flush();
}
Aggregations