Search in sources :

Example 31 with MonitorInfo

use of java.lang.management.MonitorInfo in project dubbo by alibaba.

the class JVMUtil method getThreadDumpString.

private static String getThreadDumpString(ThreadInfo threadInfo) {
    StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        sb.append(" on " + threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null) {
        sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');
    int i = 0;
    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    for (; i < stackTrace.length && i < 32; i++) {
        StackTraceElement ste = stackTrace[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch(ts) {
                case BLOCKED:
                    sb.append("\t-  blocked on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                case WAITING:
                    sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                case TIMED_WAITING:
                    sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                default:
            }
        }
        for (MonitorInfo mi : lockedMonitors) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }
    if (i < stackTrace.length) {
        sb.append("\t...");
        sb.append('\n');
    }
    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + locks.length);
        sb.append('\n');
        for (LockInfo li : locks) {
            sb.append("\t- " + li);
            sb.append('\n');
        }
    }
    sb.append('\n');
    return sb.toString();
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo)

Example 32 with MonitorInfo

use of java.lang.management.MonitorInfo in project ignite by apache.

the class IgniteUtils method printThreadInfo.

/**
 * Prints single thread info to a buffer.
 *
 * @param threadInfo Thread info.
 * @param sb Buffer.
 */
private static void printThreadInfo(ThreadInfo threadInfo, GridStringBuilder sb, Set<Long> deadlockedIdSet) {
    final long id = threadInfo.getThreadId();
    if (deadlockedIdSet.contains(id))
        sb.a("##### DEADLOCKED ");
    sb.a("Thread [name=\"").a(threadInfo.getThreadName()).a("\", id=").a(threadInfo.getThreadId()).a(", state=").a(threadInfo.getThreadState()).a(", blockCnt=").a(threadInfo.getBlockedCount()).a(", waitCnt=").a(threadInfo.getWaitedCount()).a("]").a(NL);
    LockInfo lockInfo = threadInfo.getLockInfo();
    if (lockInfo != null) {
        sb.a("    Lock [object=").a(lockInfo).a(", ownerName=").a(threadInfo.getLockOwnerName()).a(", ownerId=").a(threadInfo.getLockOwnerId()).a("]").a(NL);
    }
    MonitorInfo[] monitors = threadInfo.getLockedMonitors();
    StackTraceElement[] elements = threadInfo.getStackTrace();
    for (int i = 0; i < elements.length; i++) {
        StackTraceElement e = elements[i];
        sb.a("        at ").a(e.toString());
        for (MonitorInfo monitor : monitors) {
            if (monitor.getLockedStackDepth() == i)
                sb.a(NL).a("        - locked ").a(monitor);
        }
        sb.a(NL);
    }
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo)

Example 33 with MonitorInfo

use of java.lang.management.MonitorInfo in project metrics by dropwizard.

the class ThreadDump method dump.

/**
 * Dumps all of the threads' current information, optionally including synchronization, to an output stream.
 *
 * Having control over including synchronization info allows using this method (and its wrappers, i.e.
 * ThreadDumpServlet) in environments where getting object monitor and/or ownable synchronizer usage is not
 * supported. It can also speed things up.
 *
 * See {@link ThreadMXBean#dumpAllThreads(boolean, boolean)}
 *
 * @param lockedMonitors dump all locked monitors if true
 * @param lockedSynchronizers dump all locked ownable synchronizers if true
 * @param out an output stream
 */
public void dump(boolean lockedMonitors, boolean lockedSynchronizers, OutputStream out) {
    final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers);
    final PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, UTF_8));
    for (int ti = threads.length - 1; ti >= 0; ti--) {
        final ThreadInfo t = threads[ti];
        writer.printf("\"%s\" id=%d state=%s", t.getThreadName(), t.getThreadId(), t.getThreadState());
        final LockInfo lock = t.getLockInfo();
        if (lock != null && t.getThreadState() != Thread.State.BLOCKED) {
            writer.printf("%n    - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
            writer.printf("%n    - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
        } else if (lock != null && t.getThreadState() == Thread.State.BLOCKED) {
            writer.printf("%n    - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
        }
        if (t.isSuspended()) {
            writer.print(" (suspended)");
        }
        if (t.isInNative()) {
            writer.print(" (running in native)");
        }
        writer.println();
        if (t.getLockOwnerName() != null) {
            writer.printf("     owned by %s id=%d%n", t.getLockOwnerName(), t.getLockOwnerId());
        }
        final StackTraceElement[] elements = t.getStackTrace();
        final MonitorInfo[] monitors = t.getLockedMonitors();
        for (int i = 0; i < elements.length; i++) {
            final StackTraceElement element = elements[i];
            writer.printf("    at %s%n", element);
            for (int j = 1; j < monitors.length; j++) {
                final MonitorInfo monitor = monitors[j];
                if (monitor.getLockedStackDepth() == i) {
                    writer.printf("      - locked %s%n", monitor);
                }
            }
        }
        writer.println();
        final LockInfo[] locks = t.getLockedSynchronizers();
        if (locks.length > 0) {
            writer.printf("    Locked synchronizers: count = %d%n", locks.length);
            for (LockInfo l : locks) {
                writer.printf("      - %s%n", l);
            }
            writer.println();
        }
    }
    writer.println();
    writer.flush();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo) OutputStreamWriter(java.io.OutputStreamWriter) LockInfo(java.lang.management.LockInfo) PrintWriter(java.io.PrintWriter)

Example 34 with MonitorInfo

use of java.lang.management.MonitorInfo in project qpid-broker-j by apache.

the class AbstractContainer method getThreadStackTraces.

private String getThreadStackTraces(final ThreadInfo threadInfo) {
    String lineSeparator = System.lineSeparator();
    StringBuilder dump = new StringBuilder();
    dump.append("\"").append(threadInfo.getThreadName()).append("\"").append(" Id=").append(threadInfo.getThreadId()).append(" ").append(threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        dump.append(" on ").append(threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null) {
        dump.append(" owned by \"").append(threadInfo.getLockOwnerName()).append("\" Id=").append(threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended()) {
        dump.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        dump.append(" (in native)");
    }
    dump.append(lineSeparator);
    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement stackTraceElement = stackTrace[i];
        dump.append("    at ").append(stackTraceElement.toString()).append(lineSeparator);
        LockInfo lockInfo = threadInfo.getLockInfo();
        if (i == 0 && lockInfo != null) {
            Thread.State threadState = threadInfo.getThreadState();
            switch(threadState) {
                case BLOCKED:
                    dump.append("    -  blocked on ").append(lockInfo).append(lineSeparator);
                    break;
                case WAITING:
                    dump.append("    -  waiting on ").append(lockInfo).append(lineSeparator);
                    break;
                case TIMED_WAITING:
                    dump.append("    -  waiting on ").append(lockInfo).append(lineSeparator);
                    break;
                default:
            }
        }
        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                dump.append("    -  locked ").append(mi).append(lineSeparator);
            }
        }
    }
    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        dump.append(lineSeparator).append("    Number of locked synchronizers = ").append(locks.length);
        dump.append(lineSeparator);
        for (LockInfo li : locks) {
            dump.append("    - " + li);
            dump.append(lineSeparator);
        }
    }
    dump.append(lineSeparator);
    return dump.toString();
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo)

Example 35 with MonitorInfo

use of java.lang.management.MonitorInfo in project elastic-core-maven by OrdinaryDude.

the class GetStackTraces method processRequest.

/**
 * Process the GetStackTraces API request
 *
 * @param   req                 API request
 * @return                      API response
 */
@Override
protected JSONStreamAware processRequest(HttpServletRequest req) {
    String value;
    // 
    // Get the number of trace lines to return
    // 
    int depth;
    value = req.getParameter("depth");
    if (value != null)
        depth = Math.max(Integer.valueOf(value), 1);
    else
        depth = Integer.MAX_VALUE;
    // 
    // Get the thread information
    // 
    JSONArray threadsJSON = new JSONArray();
    JSONArray locksJSON = new JSONArray();
    ThreadMXBean tmxBean = ManagementFactory.getThreadMXBean();
    boolean tmxMI = tmxBean.isObjectMonitorUsageSupported();
    ThreadInfo[] tList = tmxBean.dumpAllThreads(tmxMI, false);
    // 
    for (ThreadInfo tInfo : tList) {
        JSONObject threadJSON = new JSONObject();
        // 
        // General thread information
        // 
        threadJSON.put("id", tInfo.getThreadId());
        threadJSON.put("name", tInfo.getThreadName());
        threadJSON.put("state", tInfo.getThreadState().toString());
        // 
        if (tmxMI) {
            MonitorInfo[] mList = tInfo.getLockedMonitors();
            if (mList.length > 0) {
                JSONArray monitorsJSON = new JSONArray();
                for (MonitorInfo mInfo : mList) {
                    JSONObject lockJSON = new JSONObject();
                    lockJSON.put("name", mInfo.getClassName());
                    lockJSON.put("hash", mInfo.getIdentityHashCode());
                    lockJSON.put("depth", mInfo.getLockedStackDepth());
                    lockJSON.put("trace", mInfo.getLockedStackFrame().toString());
                    monitorsJSON.add(lockJSON);
                }
                threadJSON.put("locks", monitorsJSON);
            }
            if (tInfo.getThreadState() == Thread.State.BLOCKED) {
                LockInfo lInfo = tInfo.getLockInfo();
                if (lInfo != null) {
                    JSONObject lockJSON = new JSONObject();
                    lockJSON.put("name", lInfo.getClassName());
                    lockJSON.put("hash", lInfo.getIdentityHashCode());
                    lockJSON.put("thread", tInfo.getLockOwnerId());
                    threadJSON.put("blocked", lockJSON);
                    boolean addLock = true;
                    for (Object lock : locksJSON) {
                        if (((JSONObject) lock).get("name").equals(lInfo.getClassName())) {
                            addLock = false;
                            break;
                        }
                    }
                    if (addLock)
                        locksJSON.add(lockJSON);
                }
            }
        }
        // 
        // Add the stack trace
        // 
        StackTraceElement[] elements = tInfo.getStackTrace();
        JSONArray traceJSON = new JSONArray();
        int ix = 0;
        for (StackTraceElement element : elements) {
            traceJSON.add(element.toString());
            if (++ix == depth)
                break;
        }
        threadJSON.put("trace", traceJSON);
        // 
        // Add the thread to the response
        // 
        threadsJSON.add(threadJSON);
    }
    // 
    // Return the response
    // 
    JSONObject response = new JSONObject();
    response.put("threads", threadsJSON);
    response.put("locks", locksJSON);
    return response;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MonitorInfo(java.lang.management.MonitorInfo) JSONArray(org.json.simple.JSONArray) ThreadInfo(java.lang.management.ThreadInfo) JSONObject(org.json.simple.JSONObject) LockInfo(java.lang.management.LockInfo) JSONObject(org.json.simple.JSONObject)

Aggregations

MonitorInfo (java.lang.management.MonitorInfo)77 LockInfo (java.lang.management.LockInfo)41 ThreadInfo (java.lang.management.ThreadInfo)18 ThreadMXBean (java.lang.management.ThreadMXBean)10 Test (org.testng.annotations.Test)8 CompositeData (javax.management.openmbean.CompositeData)7 OutputStreamWriter (java.io.OutputStreamWriter)6 TMonitorInfo (com.navercorp.pinpoint.thrift.dto.command.TMonitorInfo)3 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 TThreadDump (com.navercorp.pinpoint.thrift.dto.command.TThreadDump)2 State (java.lang.Thread.State)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 OpenDataException (javax.management.openmbean.OpenDataException)2 MonitorInfoMetricSnapshot (com.navercorp.pinpoint.profiler.monitor.metric.deadlock.MonitorInfoMetricSnapshot)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CompositeType (javax.management.openmbean.CompositeType)1 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)1