Search in sources :

Example 21 with LockInfo

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
    }
}
Also used : LockInfo(java.lang.management.LockInfo) Test(org.testng.annotations.Test)

Example 22 with LockInfo

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());
}
Also used : LockInfo(java.lang.management.LockInfo) Test(org.testng.annotations.Test)

Example 23 with LockInfo

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());
    }
}
Also used : LockInfo(java.lang.management.LockInfo) Test(org.testng.annotations.Test)

Example 24 with LockInfo

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();
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) LockInfo(java.lang.management.LockInfo) Test(org.testng.annotations.Test)

Example 25 with LockInfo

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();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MonitorInfo(java.lang.management.MonitorInfo) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) ThreadInfo(java.lang.management.ThreadInfo) OutputStreamWriter(java.io.OutputStreamWriter) LockInfo(java.lang.management.LockInfo)

Aggregations

LockInfo (java.lang.management.LockInfo)69 MonitorInfo (java.lang.management.MonitorInfo)41 ThreadInfo (java.lang.management.ThreadInfo)17 ThreadMXBean (java.lang.management.ThreadMXBean)9 OutputStreamWriter (java.io.OutputStreamWriter)6 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)4 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 CompositeData (javax.management.openmbean.CompositeData)3 TMonitorInfo (com.navercorp.pinpoint.thrift.dto.command.TMonitorInfo)2 TThreadDump (com.navercorp.pinpoint.thrift.dto.command.TThreadDump)2 State (java.lang.Thread.State)2 HashSet (java.util.HashSet)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 OpenDataException (javax.management.openmbean.OpenDataException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1