java.lang.management.ThreadInfo Java Examples

The following examples show how to use java.lang.management.ThreadInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JvmThreadInstanceEntryImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static Byte[] getState(ThreadInfo info) {
    byte[] bitmap = new byte[] {(byte)0x00, (byte)0x00};
    try {
        final Thread.State state = info.getThreadState();
        final boolean inNative  = info.isInNative();
        final boolean suspended = info.isSuspended();
        log.debug("getJvmThreadInstState",
                  "[State=" + state +
                  ",isInNative=" + inNative +
                  ",isSuspended=" + suspended + "]");
        setState(bitmap,state);
        if (inNative)  setNative(bitmap);
        if (suspended) setSuspended(bitmap);
        checkOther(bitmap);
    } catch (RuntimeException r) {
        bitmap[0]=(byte)0x00;
        bitmap[1]=Byte1.other;
        log.trace("getJvmThreadInstState",
                  "Unexpected exception: " + r);
        log.debug("getJvmThreadInstState",r);
    }
    Byte[] result = { new Byte(bitmap[0]), new Byte(bitmap[1]) };
    return result;
}
 
Example #2
Source File: ThreadMXBeanStateTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) {
    ThreadInfo info = getThreadInfo(t, state);
    if (info == null) {
        throw new RuntimeException(t.getName() +
           " expected to have ThreadInfo " +
           " but got null.");
    }

    if (info.getThreadState() != state) {
        throw new RuntimeException(t.getName() + " expected to be in " +
            state + " state but got " + info.getThreadState());
    }

    if (!info.isSuspended()) {
        throw new RuntimeException(t.getName() + " expected to be suspended " +
            " but isSuspended() returns " + info.isSuspended());
    }
}
 
Example #3
Source File: ThreadInfoCompositeData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void badTypeCompositeData() throws Exception {
    CompositeType ct =
        new CompositeType("MyCompositeType",
                          "CompositeType for ThreadInfo",
                          validItemNames,
                          validItemNames,
                          badItemTypes);

    // patch values[STACK_TRACE] to Long
    values[STACK_TRACE] = new Long(1000);
    values[LOCK_INFO] = new Long(1000);
    CompositeData cd =
        new CompositeDataSupport(ct,
                                 validItemNames,
                                 values);

    try {
        ThreadInfo info = ThreadInfo.from(cd);
    } catch (IllegalArgumentException e) {
        System.out.println("Expected exception: " +
            e.getMessage());
        return;
    }
    throw new RuntimeException(
        "IllegalArgumentException not thrown");
}
 
Example #4
Source File: TimedOutTestsListener.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private static void printThread(ThreadInfo ti, PrintWriter out) {
  out.print("\"" + ti.getThreadName() + "\"" + " Id="
      + ti.getThreadId() + " in " + ti.getThreadState());
  if (ti.getLockName() != null) {
    out.print(" on lock=" + ti.getLockName());
  }
  if (ti.isSuspended()) {
    out.print(" (suspended)");
  }
  if (ti.isInNative()) {
    out.print(" (running in native)");
  }
  out.println();
  if (ti.getLockOwnerName() != null) {
    out.println(INDENT + " owned by " + ti.getLockOwnerName() + " Id="
        + ti.getLockOwnerId());
  }
}
 
Example #5
Source File: TimedOutTestsListener.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private static void printThreadInfo(ThreadInfo ti, PrintWriter out) {
  // print thread information
  printThread(ti, out);

  // 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];
    out.println(INDENT + "at " + ste.toString());
    for (MonitorInfo mi : monitors) {
      if (mi.getLockedStackDepth() == i) {
        out.println(INDENT + "  - locked " + mi);
      }
    }
  }
  out.println();
}
 
Example #6
Source File: TestLogConfigurationDeadLockWithConf.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    while(goOn) {
        try {
            long[] ids = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
            checkCount.incrementAndGet();
            ids = ids == null ? new long[0] : ids;
            if (ids.length > 0) {
                deadlocked.addAll(asList(ids));
            }
            if (ids.length == 1) {
                throw new RuntimeException("Found 1 deadlocked thread: "+ids[0]);
            } else if (ids.length > 0) {
                ThreadInfo[] infos = ManagementFactory.getThreadMXBean().getThreadInfo(ids, Integer.MAX_VALUE);
                System.err.println("Found "+ids.length+" deadlocked threads: ");
                for (ThreadInfo inf : infos) {
                    System.err.println(inf.toString());
                }
                throw new RuntimeException("Found "+ids.length+" deadlocked threads");
            }
            Thread.sleep(100);
        } catch(InterruptedException | RuntimeException x) {
            fail(x);
        }
    }
}
 
Example #7
Source File: TestThread.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Waits until {@link TestThread} is in the certain {@link State}
 * and blocking on {@code object}.
 *
 * @param state The thread state
 * @param object The object to block on
 */
public void waitUntilBlockingOnObject(Thread.State state, Object object) {
    String want = object == null ? null : object.getClass().getName() + '@'
            + Integer.toHexString(System.identityHashCode(object));
    ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
    while (isAlive()) {
        ThreadInfo ti = tmx.getThreadInfo(getId());
        if (ti.getThreadState() == state
                && (want == null || want.equals(ti.getLockName()))) {
            return;
        }
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
        }
    }
}
 
Example #8
Source File: TimedOutTestsListener.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private static void printThread(ThreadInfo ti, PrintWriter out) {
    out.print("\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " in " + ti.getThreadState());
    if (ti.getLockName() != null) {
        out.print(" on lock=" + ti.getLockName());
    }
    if (ti.isSuspended()) {
        out.print(" (suspended)");
    }
    if (ti.isInNative()) {
        out.print(" (running in native)");
    }
    out.println();
    if (ti.getLockOwnerName() != null) {
        out.println(indent + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId());
    }
}
 
Example #9
Source File: TestLogConfigurationDeadLockWithConf.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    while(goOn) {
        try {
            long[] ids = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
            checkCount.incrementAndGet();
            ids = ids == null ? new long[0] : ids;
            if (ids.length > 0) {
                deadlocked.addAll(asList(ids));
            }
            if (ids.length == 1) {
                throw new RuntimeException("Found 1 deadlocked thread: "+ids[0]);
            } else if (ids.length > 0) {
                ThreadInfo[] infos = ManagementFactory.getThreadMXBean().getThreadInfo(ids, Integer.MAX_VALUE);
                System.err.println("Found "+ids.length+" deadlocked threads: ");
                for (ThreadInfo inf : infos) {
                    System.err.println(inf.toString());
                }
                throw new RuntimeException("Found "+ids.length+" deadlocked threads");
            }
            Thread.sleep(100);
        } catch(InterruptedException | RuntimeException x) {
            fail(x);
        }
    }
}
 
Example #10
Source File: TestLogConfigurationDeadLock.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    while(goOn) {
        try {
            long[] ids = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
            checkCount.incrementAndGet();
            ids = ids == null ? new long[0] : ids;
            if (ids.length == 1) {
                throw new RuntimeException("Found 1 deadlocked thread: "+ids[0]);
            } else if (ids.length > 0) {
                ThreadInfo[] infos = ManagementFactory.getThreadMXBean()
                    .getThreadInfo(ids, Integer.MAX_VALUE);
                System.err.println("Found "+ids.length+" deadlocked threads: ");
                for (ThreadInfo inf : infos) {
                    System.err.println(inf.toString());
                }
                throw new RuntimeException("Found "+ids.length+" deadlocked threads");
            }
            Thread.sleep(100);
        } catch(InterruptedException | RuntimeException x) {
            fail(x);
        }
    }
}
 
Example #11
Source File: DeadlockDetector.java    From mapleLemon with GNU General Public License v2.0 6 votes vote down vote up
private void printThread(ThreadInfo ti) {
    this.sb.append("\nPrintThread\n");
    this.sb.append("\"").append(ti.getThreadName()).append("\" Id=").append(ti.getThreadId()).append(" in ").append(ti.getThreadState()).append("\n");
    if (ti.getLockName() != null) {
        this.sb.append(" on lock=").append(ti.getLockName()).append("\n");
    }
    if (ti.isSuspended()) {
        this.sb.append(" (suspended)\n");
    }
    if (ti.isInNative()) {
        this.sb.append(" (running in native)\n");
    }
    if (ti.getLockOwnerName() != null) {
        this.sb.append(INDENT).append(" owned by ").append(ti.getLockOwnerName()).append(" Id=").append(ti.getLockOwnerId()).append("\n");
    }
}
 
Example #12
Source File: ThreadMXBeanStateTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) {
    ThreadInfo info = getThreadInfo(t, state);
    if (info == null) {
        throw new RuntimeException(t.getName() +
           " expected to have ThreadInfo " +
           " but got null.");
    }

    if (info.getThreadState() != state) {
        throw new RuntimeException(t.getName() + " expected to be in " +
            state + " state but got " + info.getThreadState());
    }

    if (!info.isSuspended()) {
        throw new RuntimeException(t.getName() + " expected to be suspended " +
            " but isSuspended() returns " + info.isSuspended());
    }
}
 
Example #13
Source File: ThreadMXBeanStateTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) {
    ThreadInfo info = getThreadInfo(t, state);
    if (info == null) {
        throw new RuntimeException(t.getName() +
           " expected to have ThreadInfo " +
           " but got null.");
    }

    if (info.getThreadState() != state) {
        throw new RuntimeException(t.getName() + " expected to be in " +
            state + " state but got " + info.getThreadState());
    }

    if (!info.isSuspended()) {
        throw new RuntimeException(t.getName() + " expected to be suspended " +
            " but isSuspended() returns " + info.isSuspended());
    }
}
 
Example #14
Source File: ThreadImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
    verifyThreadIds(ids);

    if (maxDepth < 0) {
        throw new IllegalArgumentException(
            "Invalid maxDepth parameter: " + maxDepth);
    }

    // ids has been verified to be non-null
    // an empty array of ids should return an empty array of ThreadInfos
    if (ids.length == 0) return new ThreadInfo[0];

    Util.checkMonitorAccess();

    ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
    if (maxDepth == Integer.MAX_VALUE) {
        getThreadInfo1(ids, -1, infos);
    } else {
        getThreadInfo1(ids, maxDepth, infos);
    }
    return infos;
}
 
Example #15
Source File: ThreadMain.java    From jdk-source-analysis with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
    for (ThreadInfo info : threadInfos) {
        System.out.printf("[%d]%s\n", info.getThreadId(), info.getThreadName());
    }
}
 
Example #16
Source File: ThreadInfoCompositeData.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void createGoodCompositeData() throws Exception {
     CompositeType ct =
         new CompositeType("MyCompositeType",
                           "CompositeType for ThreadInfo",
                           validItemNames,
                           validItemNames,
                           validItemTypes);
     CompositeData cd =
         new CompositeDataSupport(ct,
                                  validItemNames,
                                  values);
     ThreadInfo info = ThreadInfo.from(cd);
     checkThreadInfo(info);
}
 
Example #17
Source File: SampledCPUSnapshot.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public String getThreadDump(int sampleIndex) throws IOException {
    StringBuilder sb = new StringBuilder(4096);
    SamplesInputStream stream = seek(sampleIndex);
    ThreadsSample _sample = stream.readSample();
    ThreadInfo[] threads = _sample.getTinfos();

    stream.close();
    stream = null;
    printThreads(sb, threads);
    return sb.toString();
}
 
Example #18
Source File: ThreadDump.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public ThreadInfo[] getThreads() {
    synchronized (tinfoLock) {
        if (tinfos == null) {
            int i = 0;
            tinfos = new ThreadInfo[cdThreads.length];
            for (Object cd : cdThreads) {
                tinfos[i++] = ThreadInfo.from((CompositeData) cd);
            }
            cdThreads = null;
        }
        return tinfos;
    }
}
 
Example #19
Source File: Utils.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static String getAllThreadDumps() {
    ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
    StringBuilder builder = new StringBuilder();
    for (ThreadInfo info : threads) {
        builder.append('\n').append(info);
    }
    return builder.toString();
}
 
Example #20
Source File: ThreadMonitor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prints the thread dump information with locks info to System.out.
 */
private void dumpThreadInfoWithLocks() {
   System.out.println("Full Java thread dump with locks info");

   ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
   for (ThreadInfo ti : tinfos) {
       printThreadInfo(ti);
       LockInfo[] syncs = ti.getLockedSynchronizers();
       printLockInfo(syncs);
   }
   System.out.println();
}
 
Example #21
Source File: RedefineMethodInBacktraceApp.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void doMethodInBacktraceTestB() throws Exception {
    // Start a thread which blocks in method
    Thread t = new Thread(RedefineMethodInBacktraceTargetB::methodToRedefine);
    t.setDaemon(true);
    t.start();

    // Wait here until the new thread is in the method we want to redefine
    called.await();

    // Now redefine the class while the method is still on the stack of the new thread
    doRedefine(RedefineMethodInBacktraceTargetB.class);

    // Do thread dumps in two different ways (to exercise different code paths)
    // while the old class is still on the stack

    ThreadInfo[] tis = ManagementFactory.getThreadMXBean().dumpAllThreads(false, false);
    for(ThreadInfo ti : tis) {
        System.out.println(ti);
    }

    String[] threadPrintArgs = {};
    Object[] dcmdArgs = {threadPrintArgs};
    String[] signature = {String[].class.getName()};
    DiagnosticCommandMBean dcmd = ManagementFactoryHelper.getDiagnosticCommandMBean();
    System.out.println(dcmd.invoke("threadPrint", dcmdArgs, signature));

    // release the thread
    stop.countDown();
}
 
Example #22
Source File: ThreadMXBeanUtils.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public static ThreadInfo getThreadInfo(long id, int stackTraceMaxDepth) {
    if (stackTraceMaxDepth <= 0) {
        return THREAD_MX_BEAN.getThreadInfo(id);
    } else {
        return THREAD_MX_BEAN.getThreadInfo(id, stackTraceMaxDepth);
    }
}
 
Example #23
Source File: Diagnostics.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a JVM thread dump formatted
 * using the given StringManager.
 *
 * @param requestedSm the StringManager to use
 * @return the formatted JVM thread dump
 */
private static String getThreadDump(StringManager requestedSm) {
    StringBuilder sb = new StringBuilder();

    synchronized(timeformat) {
        sb.append(timeformat.format(new Date()));
    }
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.threadDumpTitle"));
    sb.append(" ");
    sb.append(runtimeMXBean.getVmName());
    sb.append(" (");
    sb.append(runtimeMXBean.getVmVersion());
    String vminfo = System.getProperty(vminfoSystemProperty);
    if (vminfo != null) {
        sb.append(" " + vminfo);
    }
    sb.append("):" + CRLF);
    sb.append(CRLF);

    ThreadInfo[] tis = threadMXBean.dumpAllThreads(true, true);
    sb.append(getThreadDump(tis));

    sb.append(findDeadlock());
    return sb.toString();
}
 
Example #24
Source File: ThreadMXBeanStateTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static ThreadInfo getThreadInfo(ThreadStateController t, Thread.State expected) {
    // wait for the thread to transition to the expected state.
    // There is a small window between the thread checking the state
    // and the thread actual entering that state.
    int retryCount=0;
    ThreadInfo info = tm.getThreadInfo(t.getId());
    while (info.getThreadState() != expected && retryCount < MAX_RETRY) {
        ThreadStateController.pause(10);
        retryCount++;
        info = tm.getThreadInfo(t.getId());
    }
    return info;
}
 
Example #25
Source File: ThreadDumpUtils.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private static void setThreadInfo(ThreadDumpMetricSnapshot threadDump, ThreadInfo threadInfo) {
    threadDump.setThreadName(threadInfo.getThreadName());
    threadDump.setThreadId(threadInfo.getThreadId());
    threadDump.setBlockedTime(threadInfo.getBlockedTime());
    threadDump.setBlockedCount(threadInfo.getBlockedCount());
    threadDump.setWaitedTime(threadInfo.getWaitedTime());
    threadDump.setWaitedCount(threadInfo.getWaitedCount());
}
 
Example #26
Source File: DeadlockDetector.java    From mapleLemon with GNU General Public License v2.0 5 votes vote down vote up
private void printMonitorInfo(ThreadInfo threadInfo, MonitorInfo[] monitorInfos) {
    this.sb.append(INDENT).append("Locked monitors: count = ").append(monitorInfos.length).append("\n");
    for (MonitorInfo monitorInfo : monitorInfos) {
        this.sb.append(INDENT).append("  - ").append(monitorInfo).append(" locked at \n");
        this.sb.append(INDENT).append("      ").append(monitorInfo.getLockedStackDepth()).append(" ").append(monitorInfo.getLockedStackFrame()).append("\n");
    }
}
 
Example #27
Source File: DeadlockAnalyzer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Deadlock[] createDeadlockDescriptions(Set<LinkedHashSet<ThreadInfo>> cycles) {
    Deadlock result[] = new Deadlock[cycles.size()];
    int count = 0;
    for (LinkedHashSet<ThreadInfo> cycle : cycles) {
        ThreadInfo asArray[] = cycle.toArray(new ThreadInfo[cycle.size()]);
        Deadlock d = new Deadlock(asArray);
        result[count++] = d;
    }
    return result;
}
 
Example #28
Source File: Diagnostics.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Formats the thread dump for a list of threads.
 *
 * @param tinfos the ThreadInfo array describing the thread list
 * @return the formatted thread dump
 */
private static String getThreadDump(ThreadInfo[] tinfos) {
    StringBuilder sb = new StringBuilder();
    for (ThreadInfo tinfo : tinfos) {
        sb.append(getThreadDump(tinfo));
        sb.append(CRLF);
    }
    return sb.toString();
}
 
Example #29
Source File: ThreadMonitor.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void dumpThreadInfo() {
   System.out.println("Full Java thread dump");
   long[] tids = tmbean.getAllThreadIds();
   ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
   for (ThreadInfo ti : tinfos) {
       printThreadInfo(ti);
   }
}
 
Example #30
Source File: ThreadDump.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ThreadInfo[] getThreads() {
    synchronized (tinfoLock) {
        if (tinfos == null) {
            int i = 0;
            tinfos = new ThreadInfo[cdThreads.length];
            for (Object cd : cdThreads) {
                tinfos[i++] = ThreadInfo.from((CompositeData) cd);
            }
            cdThreads = null;
        }
        return tinfos;
    }
}