Example usage for java.lang.management MonitorInfo getLockedStackDepth

List of usage examples for java.lang.management MonitorInfo getLockedStackDepth

Introduction

In this page you can find the example usage for java.lang.management MonitorInfo getLockedStackDepth.

Prototype

public int getLockedStackDepth() 

Source Link

Document

Returns the depth in the stack trace where the object monitor was locked.

Usage

From source file:com.workplacesystems.utilsj.ThreadDumperJdk16.java

@Override
int outputMonitors(ThreadInfo info, int stack_depth, int monitor_pointer, StringBuffer buffer) {
    MonitorInfo[] locked_monitors = info.getLockedMonitors();
    boolean check_next_monitor;
    do {//from w  w  w.j  ava 2  s . c  o m
        check_next_monitor = false;
        if (monitor_pointer < locked_monitors.length) {
            MonitorInfo monitor_info = locked_monitors[monitor_pointer];
            if (monitor_info.getLockedStackDepth() == stack_depth) {
                formatLock(monitor_info, "locked", buffer);
                buffer.append("\n");
                monitor_pointer++;
                check_next_monitor = true;
            }
        }
    } while (check_next_monitor);

    return monitor_pointer;
}

From source file:DeadlockDetector.java

private void printMonitorInfo(ThreadInfo threadInfo, MonitorInfo[] monitorInfos) {
    sb.append(INDENT + "Locked monitors: count = " + monitorInfos.length + "\n");
    for (MonitorInfo monitorInfo : monitorInfos) {
        sb.append(INDENT + "  - " + monitorInfo + " locked at " + "\n");
        sb.append(INDENT + "      " + monitorInfo.getLockedStackDepth() + " "
                + monitorInfo.getLockedStackFrame() + "\n");
    }/*w  w  w  . ja  v a 2s .c  o m*/
}

From source file:hudson.Functions.java

@IgnoreJRERequirement
public static String dumpThreadInfo(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder(
            "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " " + ti.getThreadState());
    if (ti.getLockName() != null) {
        sb.append(" on " + ti.getLockName());
    }/*from  ww  w .j a  va2 s  .c o  m*/
    if (ti.getLockOwnerName() != null) {
        sb.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId());
    }
    if (ti.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');
    StackTraceElement[] stackTrace = ti.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        if (i == 0 && ti.getLockInfo() != null) {
            Thread.State ts = ti.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on " + ti.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on " + ti.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on " + ti.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

        for (MonitorInfo mi : ti.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }

    LockInfo[] locks = ti.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();
}

From source file:FullThreadDump.java

private void printThreadInfo(ThreadInfo ti) {
    // print thread information
    printThread(ti);/*w  w  w .j av a  2 s.c o m*/

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        System.out.println(INDENT + "at " + ste.toString());
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                System.out.println(INDENT + "  - locked " + mi);
            }
        }
    }
    System.out.println();
}

From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java

public void threadDump(BundleWriter writer) throws IOException {
    writer.markStartOfFile("runtime/threads.txt");

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threads = threadMXBean.dumpAllThreads(true, true);

    // Sadly we can't easily do info.toString() as the implementation is hardcoded to cut the stack trace only to 8
    // items which does not serve our purpose well. Hence we have custom implementation that prints entire stack trace
    // for all threads.
    for (ThreadInfo info : threads) {
        StringBuilder sb = new StringBuilder(
                "\"" + info.getThreadName() + "\"" + " Id=" + info.getThreadId() + " " + info.getThreadState());
        if (info.getLockName() != null) {
            sb.append(" on " + info.getLockName());
        }/*  w w w .  j  a  v  a  2  s  . c om*/
        if (info.getLockOwnerName() != null) {
            sb.append(" owned by \"" + info.getLockOwnerName() + "\" Id=" + info.getLockOwnerId());
        }
        if (info.isSuspended()) {
            sb.append(" (suspended)");
        }
        if (info.isInNative()) {
            sb.append(" (in native)");
        }
        sb.append('\n');
        int i = 0;
        for (StackTraceElement ste : info.getStackTrace()) {
            if (i == 0 && info.getLockInfo() != null) {
                Thread.State ts = info.getThreadState();
                switch (ts) {
                case BLOCKED:
                    sb.append("\t-  blocked on " + info.getLockInfo());
                    sb.append('\n');
                    break;
                case WAITING:
                    sb.append("\t-  waiting on " + info.getLockInfo());
                    sb.append('\n');
                    break;
                case TIMED_WAITING:
                    sb.append("\t-  waiting on " + info.getLockInfo());
                    sb.append('\n');
                    break;
                default:
                }
            }
            sb.append("\tat " + ste.toString());
            sb.append('\n');

            i++;

            for (MonitorInfo mi : info.getLockedMonitors()) {
                if (mi.getLockedStackDepth() == i) {
                    sb.append("\t-  locked " + mi);
                    sb.append('\n');
                }
            }
        }

        LockInfo[] locks = info.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');

        writer.write(sb.toString());
    }

    writer.markEndOfFile();
}

From source file:FullThreadDump.java

private void printMonitorInfo(ThreadInfo ti, MonitorInfo[] monitors) {
    System.out.println(INDENT + "Locked monitors: count = " + monitors.length);
    for (MonitorInfo mi : monitors) {
        System.out.println(INDENT + "  - " + mi + " locked at ");
        System.out.println(INDENT + "      " + mi.getLockedStackDepth() + " " + mi.getLockedStackFrame());
    }/*from   w  w w.j  a v a 2  s  . co  m*/
}

From source file:fullThreadDump.java

private String printThreadInfo(ThreadInfo ti) {
    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    currentThreadInfo result = new currentThreadInfo(ti);
    StringBuilder threadOutput = new StringBuilder(result.getThreadName());
    threadOutput.append(result.getThreadStateDesc());
    if (stats == true) {
        threadOutput.append(result.getThreadStats());
    }//from  ww  w  .j a  v a  2  s.  c o m
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        if (i == 0) {
            threadOutput.append("\n    java.lang.Thread.State: " + result.getThreadState());
            threadOutput.append("\n" + INDENT + "at " + ste.toString());
            if (ste.toString().contains("java.lang.Object.wait(Native Method)")
                    && result.getLockName() != null) {
                threadOutput.append("\n" + INDENT + "- waiting on " + result.getLockName());
            }
            if (ste.toString().contains("sun.misc.Unsafe.park(Native Method)")
                    && result.getLockName() != null) {
                threadOutput.append("\n" + INDENT + "- parking to wait for " + result.getLockName());
            }
            if (result.getThreadStateDesc().contains("BLOCKED") && result.getLockName() != null) {
                threadOutput.append("\n" + INDENT + "- waiting to lock " + result.getLockName());
            }
        } else {
            threadOutput.append("\n" + INDENT + "at " + ste.toString());
        }
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                threadOutput.append("\n" + INDENT + " - locked " + mi);
            }
        }
    }
    threadOutput.append("\n");
    //System.out.print(threadOutput.toString());
    //printMonitorInfo(ti, monitors);
    return threadOutput.toString();
}

From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java

private TreeMap<Long, Map<String, Object>> getStackTraceInformation(ThreadMXBean threadMXBean) {
    TreeMap<Long, Map<String, Object>> traces = new TreeMap<>();
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    for (ThreadInfo threadInfo : threadInfos) {
        LinkedHashMap<String, Object> threadStackTrace = new LinkedHashMap<>();
        threadStackTrace.put("Id", threadInfo.getThreadId());
        threadStackTrace.put("Name", threadInfo.getThreadName());
        threadStackTrace.put("State", threadInfo.getThreadState());

        LinkedHashMap<String, Object> lockMonitorInfo = new LinkedHashMap<>();
        MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
        ArrayList<Map<String, Object>> lockedMonitorsJson = new ArrayList<>();

        for (MonitorInfo lockedMonitor : lockedMonitors) {
            LinkedHashMap<String, Object> lockedMonitorJson = new LinkedHashMap<>();
            lockedMonitorJson.put("Class", lockedMonitor.getClassName());
            lockedMonitorJson.put("IdentityHashCode", lockedMonitor.getIdentityHashCode());
            lockedMonitorJson.put("LockedStackDepth", lockedMonitor.getLockedStackDepth());
            lockedMonitorJson.put("StackFrame", lockedMonitor.getLockedStackFrame().toString());
            lockedMonitorsJson.add(lockedMonitorJson);
        }//from   w  w  w  .j av a  2  s  .  com

        lockMonitorInfo.put("Locked Monitors", lockedMonitorsJson);
        lockMonitorInfo.put("Locked Synchronizers", asJSON(threadInfo.getLockedSynchronizers()));
        threadStackTrace.put("Lock Monitor Info", lockMonitorInfo);

        LinkedHashMap<String, Object> blockedInfo = new LinkedHashMap<>();
        blockedInfo.put("Blocked Time", threadInfo.getBlockedTime() == -1 ? null : threadInfo.getBlockedTime());
        blockedInfo.put("Blocked Count", threadInfo.getBlockedCount());
        threadStackTrace.put("Blocked Info", blockedInfo);

        LinkedHashMap<String, Object> timeInfo = new LinkedHashMap<>();
        timeInfo.put("Waited Time", threadInfo.getWaitedTime() == -1 ? null : threadInfo.getWaitedTime());
        timeInfo.put("Waited Count", threadInfo.getWaitedCount());
        threadStackTrace.put("Time Info", timeInfo);

        LinkedHashMap<String, Object> lockInfoMap = new LinkedHashMap<>();
        LockInfo lockInfo = threadInfo.getLockInfo();
        lockInfoMap.put("Locked On", asJSON(lockInfo));
        lockInfoMap.put("Lock Owner Thread Id",
                threadInfo.getLockOwnerId() == -1 ? null : threadInfo.getLockOwnerId());
        lockInfoMap.put("Lock Owner Thread Name", threadInfo.getLockOwnerName());
        threadStackTrace.put("Lock Info", lockInfoMap);

        LinkedHashMap<String, Object> stateInfo = new LinkedHashMap<>();
        stateInfo.put("Suspended", threadInfo.isSuspended());
        stateInfo.put("InNative", threadInfo.isInNative());
        threadStackTrace.put("State Info", stateInfo);

        threadStackTrace.put("Stack Trace", asJSON(threadInfo.getStackTrace()));
        traces.put(threadInfo.getThreadId(), threadStackTrace);
    }
    return traces;
}

From source file:fullThreadDump.java

private void printMonitorInfo(ThreadInfo ti, MonitorInfo[] monitors) {
    System.out.println(INDENT + "Locked monitors: count = " + monitors.length);
    for (MonitorInfo mi : monitors) {
        System.out.println(INDENT + " - " + mi + " locked at ");
        System.out.println(INDENT + " " + mi.getLockedStackDepth() + " " + mi.getLockedStackFrame());
    }/* w  ww .  ja va 2s  .co  m*/
}

From source file:org.ScripterRon.JavaBitcoin.RpcHandler.java

/**
 * Process 'getstacktraces' request/*from w  w w .  ja v  a  2s . co  m*/
 *
 * Response parameters:
 *   locks   - An array of lock objects for locks with waiters
 *   threads - An array of thread objects
 *
 * Lock object:
 *   name   - Lock class name
 *   hash   - Lock identity hash code
 *   thread - Identifier of thread holding the lock
 *
 * Monitor object:
 *   name    - Monitor class name
 *   hash    - Monitor identity hash
 *   depth   - Stack depth where monitor locked
 *   trace   - Stack element where monitor locked
 *
 * Thread object:
 *   blocked - Lock object if thread is waiting on a lock
 *   id      - Thread identifier
 *   locks   - Array of monitor objects for locks held by this thread
 *   name    - Thread name
 *   state   - Thread state
 *   trace   - Array of stack trace elements
 *
 * @return                              Response as a JSONObject
 */
private JSONObject getStackTraces() {
    JSONArray threadsJSON = new JSONArray();
    JSONArray locksJSON = new JSONArray();
    ThreadMXBean tmxBean = ManagementFactory.getThreadMXBean();
    boolean tmxMI = tmxBean.isObjectMonitorUsageSupported();
    ThreadInfo[] tList = tmxBean.dumpAllThreads(tmxMI, false);
    //
    // Generate the response
    //
    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());
        //
        // Gather lock usage
        //
        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 (((String) ((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();
        for (StackTraceElement element : elements)
            traceJSON.add(element.toString());
        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;
}