Java Code Examples for java.lang.management.ThreadInfo#getLockInfo()

The following examples show how to use java.lang.management.ThreadInfo#getLockInfo() . 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: ThreadPrinter.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public StackTraceElement[] printSingleThread(ThreadInfo info) {
	StackTraceElement[] trace = info.getStackTrace();
	StringBuilder sb = new StringBuilder(512);

	sb.append(" ").append(info.getThreadId()).append(": \"").append(info.getThreadName()).append("\"");

	if (view.vmInfo.threadContentionMonitoringSupported) {
		sb.append(" (blocked:").append(info.getBlockedCount()).append("/").append(info.getBlockedTime())
				.append("ms, wait:").append(info.getWaitedCount()).append("/").append(info.getWaitedTime())
				.append("ms");
	} else {
		sb.append(" (blocked:").append(info.getBlockedCount()).append(" times, wait:").append(info.getWaitedCount())
				.append(" times");
	}

	if (info.isSuspended()) {
		sb.append(" ,suspended");
	}
	if (info.isInNative()) {
		sb.append(" ,in native");
	}
	sb.append(")\n");

	sb.append("   java.lang.Thread.State: " + info.getThreadState().toString());
	LockInfo lockInfo = info.getLockInfo();
	if (lockInfo != null) {
		sb.append("(on " + lockInfo + ")");
	}
	if (info.getLockOwnerName() != null) {
		sb.append(" owned by " + info.getLockOwnerId() + ":\"" + info.getLockOwnerName() + "\"");
	}
	sb.append("\n");
	for (StackTraceElement traceElement : trace) {
		sb.append("\tat ").append(traceElement).append("\n");
	}

	System.out.print(sb.toString());

	return trace;
}
 
Example 2
Source File: CurrentRequestsHandler.java    From styx with Apache License 2.0 5 votes vote down vote up
private String getThreadState(ThreadInfo t) {
    StringBuilder sb = new StringBuilder();

    LockInfo lock = t.getLockInfo();
    if (lock != null && t.getThreadState() != BLOCKED) {
        sb.append(format("%n    - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()));
        sb.append(format("%n    - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()));
    } else if (lock != null && t.getThreadState() == BLOCKED) {
        sb.append(format("%n    - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName()));
    }

    return sb.toString();
}
 
Example 3
Source File: DeadlockInfo.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Get a string identifying the lock that this thread is waiting on.
 * @param threadInfo
 * @return A string identifying the lock that this thread is waiting on,
 * or null if the thread is not waiting on a lock
 */
@Nullable
public String getWaitingLockString(final ThreadInfo threadInfo) {
  if (null == threadInfo.getLockInfo()) {
    return null;
  } else {
    return threadInfo.getLockName() + " held by " + threadInfo.getLockOwnerName();
  }
}
 
Example 4
Source File: CF6Impl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public LI getLockInfo(ThreadInfo threadInfo) {
  LockInfo lockInfo= threadInfo.getLockInfo();
  if(lockInfo == null) {
    return null;
  }
  
  return new LI(lockInfo.getClassName(), lockInfo.getIdentityHashCode());
}
 
Example 5
Source File: LongGCDisruption.java    From crate with Apache License 2.0 5 votes vote down vote up
protected void onBlockDetected(ThreadInfo blockedThread, @Nullable ThreadInfo blockingThread) {
    String blockedThreadStackTrace = stackTrace(blockedThread.getStackTrace());
    String blockingThreadStackTrace = blockingThread != null ?
        stackTrace(blockingThread.getStackTrace()) : "not available";
    throw new AssertionError("Thread [" + blockedThread.getThreadName() + "] is blocked waiting on the resource [" +
        blockedThread.getLockInfo() + "] held by the suspended thread [" + blockedThread.getLockOwnerName() +
        "] of the disrupted node [" + disruptedNode + "].\n" +
        "Please add this occurrence to the unsafeClasses list in [" + LongGCDisruption.class.getName() + "].\n" +
        "Stack trace of blocked thread: " + blockedThreadStackTrace + "\n" +
        "Stack trace of blocking thread: " + blockingThreadStackTrace);
}
 
Example 6
Source File: OSProcess.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static void formatThreadInfo(ThreadInfo t, PrintWriter pw) {
    // this is largely copied from the JDK's ThreadInfo.java, but it limits the
    // stacks to 8 elements
    pw.append("\"" + t.getThreadName() + "\"" +
        " tid=0x" + Long.toHexString(t.getThreadId()));
// this is in the stack trace elements so we don't need to add it
//    if (t.getLockName() != null) {
//      pw.append(" ");
//      pw.append(StringUtils.toLowerCase(t.getThreadState().toString()));
//      pw.append(" on " + t.getLockName());
//    }
    // priority is not known
    // daemon status is not known
    if (t.isSuspended()) {
      pw.append(" (suspended)");
    }
    if (t.isInNative()) {
      pw.append(" (in native)");
    }
    if (t.getLockOwnerName() != null) {
      pw.append(" owned by \"" + t.getLockOwnerName() +
          "\" tid=0x" + Long.toHexString(t.getLockOwnerId()));
    }
    pw.append('\n');
    pw.append("    java.lang.Thread.State: " + t.getThreadState() + "\n");
    int i = 0;
    StackTraceElement[] stackTrace = t.getStackTrace();
    for (; i < stackTrace.length && i < MAX_STACK_FRAMES; i++) {
      StackTraceElement ste = stackTrace[i];
      pw.append("\tat " + ste.toString());
      pw.append('\n');
      if (i == 0 && t.getLockInfo() != null) {
        Thread.State ts = t.getThreadState();
        switch (ts) {
        case BLOCKED: 
          pw.append("\t-  blocked on " + t.getLockInfo());
          pw.append('\n');
          break;
        case WAITING:
          pw.append("\t-  waiting on " + t.getLockInfo());
          pw.append('\n');
          break;
        case TIMED_WAITING:
          pw.append("\t-  waiting on " + t.getLockInfo());
          pw.append('\n');
          break;
        default:
        }
      }

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

    LockInfo[] locks = t.getLockedSynchronizers();
    if (locks.length > 0) {
      pw.append("\n\tNumber of locked synchronizers = " + locks.length);
      pw.append('\n');
      for (LockInfo li : locks) {
        pw.append("\t- " + li);
        pw.append('\n');
      }
    }
    pw.append('\n');
  }
 
Example 7
Source File: ThreadUtil.java    From samza with Apache License 2.0 4 votes vote down vote up
/**
 * Copy of ThreadInfo#toString() without the hardcoded MAX_FRAMES = 8 restriction on thread stack depth.
 *
 * Returns a string representation of this thread info.
 * The format of this string depends on the implementation.
 * The returned string will typically include
 * the thread name, the getThreadId thread ID, its state,
 * and a stack trace if any.
 *
 * @return a string representation of this thread info.
 */
private static String toString(ThreadInfo info) {
  StringBuilder sb = new StringBuilder("\"" + info.getThreadName() + "\""
      + " Id=" + info.getThreadId() + " " + info.getThreadState());
  if (info.getLockName() != null) {
    sb.append(" on " + info.getLockName());
  }
  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 (; i < info.getStackTrace().length; i++) {
    StackTraceElement ste = info.getStackTrace()[i];
    sb.append("\tat " + ste.toString());
    sb.append('\n');
    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:
      }
    }

    for (MonitorInfo mi : info.getLockedMonitors()) {
      if (mi.getLockedStackDepth() == i) {
        sb.append("\t-  locked " + mi);
        sb.append('\n');
      }
    }
  }
  if (i < info.getStackTrace().length) {
    sb.append("\t...");
    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');
  return sb.toString();
}
 
Example 8
Source File: OSProcess.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static void formatThreadInfo(ThreadInfo t, PrintWriter pw) {
    // this is largely copied from the JDK's ThreadInfo.java, but it limits the
    // stacks to 8 elements
    pw.append("\"" + t.getThreadName() + "\"" +
        " tid=0x" + Long.toHexString(t.getThreadId()));
// this is in the stack trace elements so we don't need to add it
//    if (t.getLockName() != null) {
//      pw.append(" ");
//      pw.append(StringUtils.toLowerCase(t.getThreadState().toString()));
//      pw.append(" on " + t.getLockName());
//    }
    // priority is not known
    // daemon status is not known
    if (t.isSuspended()) {
      pw.append(" (suspended)");
    }
    if (t.isInNative()) {
      pw.append(" (in native)");
    }
    if (t.getLockOwnerName() != null) {
      pw.append(" owned by \"" + t.getLockOwnerName() +
          "\" tid=0x" + Long.toHexString(t.getLockOwnerId()));
    }
    pw.append('\n');
    pw.append("    java.lang.Thread.State: " + t.getThreadState() + "\n");
    int i = 0;
    StackTraceElement[] stackTrace = t.getStackTrace();
    for (; i < stackTrace.length && i < MAX_STACK_FRAMES; i++) {
      StackTraceElement ste = stackTrace[i];
      pw.append("\tat " + ste.toString());
      pw.append('\n');
      if (i == 0 && t.getLockInfo() != null) {
        Thread.State ts = t.getThreadState();
        switch (ts) {
        case BLOCKED: 
          pw.append("\t-  blocked on " + t.getLockInfo());
          pw.append('\n');
          break;
        case WAITING:
          pw.append("\t-  waiting on " + t.getLockInfo());
          pw.append('\n');
          break;
        case TIMED_WAITING:
          pw.append("\t-  waiting on " + t.getLockInfo());
          pw.append('\n');
          break;
        default:
        }
      }

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

    LockInfo[] locks = t.getLockedSynchronizers();
    if (locks.length > 0) {
      pw.append("\n\tNumber of locked synchronizers = " + locks.length);
      pw.append('\n');
      for (LockInfo li : locks) {
        pw.append("\t- " + li);
        pw.append('\n');
      }
    }
    pw.append('\n');
  }
 
Example 9
Source File: GfxdLocalLockService.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void generateThreadDump(StringBuilder msg) {
  ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  for (ThreadInfo tInfo : mbean.dumpAllThreads(true, true)) {
    msg.append('"').append(tInfo.getThreadName()).append('"').append(" Id=")
        .append(tInfo.getThreadId()).append(' ')
        .append(tInfo.getThreadState());
    if (tInfo.getLockName() != null) {
      msg.append(" on ").append(tInfo.getLockName());
    }
    if (tInfo.getLockOwnerName() != null) {
      msg.append(" owned by \"").append(tInfo.getLockOwnerName())
          .append("\" Id=").append(tInfo.getLockOwnerId());
    }
    if (tInfo.isSuspended()) {
      msg.append(" (suspended)");
    }
    if (tInfo.isInNative()) {
      msg.append(" (in native)");
    }
    msg.append(SanityManager.lineSeparator);
    final StackTraceElement[] stackTrace = tInfo.getStackTrace();
    for (int index = 0; index < stackTrace.length; ++index) {
      msg.append("\tat ").append(stackTrace[index].toString())
          .append(SanityManager.lineSeparator);
      if (index == 0 && tInfo.getLockInfo() != null) {
        final Thread.State ts = tInfo.getThreadState();
        switch (ts) {
          case BLOCKED:
            msg.append("\t-  blocked on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          case WAITING:
            msg.append("\t-  waiting on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          case TIMED_WAITING:
            msg.append("\t-  waiting on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          default:
        }
      }

      for (MonitorInfo mi : tInfo.getLockedMonitors()) {
        if (mi.getLockedStackDepth() == index) {
          msg.append("\t-  locked ").append(mi)
              .append(SanityManager.lineSeparator);
        }
      }
    }

    final LockInfo[] locks = tInfo.getLockedSynchronizers();
    if (locks.length > 0) {
      msg.append(SanityManager.lineSeparator)
          .append("\tNumber of locked synchronizers = ").append(locks.length)
          .append(SanityManager.lineSeparator);
      for (LockInfo li : locks) {
        msg.append("\t- ").append(li).append(SanityManager.lineSeparator);
      }
    }
    msg.append(SanityManager.lineSeparator);
  }
}
 
Example 10
Source File: SdcInfoContentGenerator.java    From datacollector with Apache License 2.0 4 votes vote down vote up
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());
    }
    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();
}
 
Example 11
Source File: ThreadDumpingWatchdog.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("incomplete-switch")
private static void dumpThreadInfo(StringBuilder sb, String prefix, ThreadInfo tinfo) {
  sb.append('\n').append(prefix);
  dumpThreadHeader(sb, tinfo);

  StackTraceElement[] trace = tinfo.getStackTrace();
  if (trace.length > 0) {
    sb.append('\n').append(prefix).append("    at ").append(trace[0]);
    if (tinfo.getLockInfo() != null) {
      sb.append('\n').append(prefix).append("    - ");
      switch (tinfo.getThreadState()) {
        case BLOCKED:
          sb.append("blocked on ");
          break;
        case TIMED_WAITING:
        case WAITING:
          sb.append("waiting on ");
          break;
      }
      sb.append(tinfo.getLockInfo());
    }
    MonitorInfo[] lockedMonitors = tinfo.getLockedMonitors();
    for (int i = 1; i < trace.length; i++) {
      sb.append("\n").append(prefix).append("    at ").append(trace[i]);
      for (MonitorInfo minfo : lockedMonitors) {
        if (minfo.getLockedStackDepth() == i) {
          sb.append("\n").append(prefix).append("    - locked ").append(minfo);
        }
      }
    }
  }
  LockInfo[] lockedSynchronizers = tinfo.getLockedSynchronizers();
  if (lockedSynchronizers.length > 0) {
    sb.append("\n").append(prefix).append("    Locked synchronizers:");
    for (int i = 0; i < lockedSynchronizers.length; i++) {
      sb.append("\n").append(prefix).append("      ").append(i).append(". ")
          .append(lockedSynchronizers[i]);
    }
  }
  sb.append("\n").append(prefix);
}
 
Example 12
Source File: SampledCPUSnapshot.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
private void print16Thread(final StringBuilder sb, final ThreadInfo thread, boolean goToSourceAvailable) {
    MonitorInfo[] monitors = thread.getLockedMonitors();
    sb.append("&nbsp;<b>");   // NOI18N
    sb.append("\"").append(thread.getThreadName()).append("\" - Thread t@").append(thread.getThreadId()).append("<br>");    // NOI18N
    sb.append("    java.lang.Thread.State: ").append(thread.getThreadState()); // NOI18N
    sb.append("</b><br>");   // NOI18N
    int index = 0;
    for (StackTraceElement st : thread.getStackTrace()) {
        LockInfo lock = thread.getLockInfo();
        String stackElementText = htmlize(st.toString());
        String lockOwner = thread.getLockOwnerName();
        String className = st.getClassName();
        String method = st.getMethodName();
        int lineNo = st.getLineNumber();
        
        String stackEl = stackElementText;
        if (goToSourceAvailable) {
            String stackUrl = OPEN_THREADS_URL+className+"|"+method+"|"+lineNo; // NOI18N
            stackEl = "<a href=\""+stackUrl+"\">"+stackElementText+"</a>";    // NOI18N
        }

        sb.append("\tat ").append(stackEl).append("<br>");    // NOI18N
        if (index == 0) {
            if ("java.lang.Object".equals(st.getClassName()) &&     // NOI18N
                    "wait".equals(st.getMethodName())) {                // NOI18N
                if (lock != null) {
                    sb.append("\t- waiting on ");    // NOI18N
                    printLock(sb,lock);
                    sb.append("<br>");    // NOI18N
                }
            } else if (lock != null) {
                if (lockOwner == null) {
                    sb.append("\t- parking to wait for ");      // NOI18N
                    printLock(sb,lock);
                    sb.append("<br>");            // NOI18N
                } else {
                    sb.append("\t- waiting to lock ");      // NOI18N
                    printLock(sb,lock);
                    sb.append(" owned by \"").append(lockOwner).append("\" t@").append(thread.getLockOwnerId()).append("<br>");   // NOI18N
                }
            }
        }
        printMonitors(sb, monitors, index);
        index++;
    }
    StringBuilder jnisb = new StringBuilder();
    printMonitors(jnisb, monitors, -1);
    if (jnisb.length() > 0) {
        sb.append("   JNI locked monitors:<br>");
        sb.append(jnisb);
    }
    LockInfo[] synchronizers = thread.getLockedSynchronizers();
    if (synchronizers != null) {
        sb.append("<br>   Locked ownable synchronizers:");    // NOI18N
        if (synchronizers.length == 0) {
            sb.append("<br>\t- None\n");  // NOI18N
        } else {
            for (LockInfo li : synchronizers) {
                sb.append("<br>\t- locked ");         // NOI18N
                printLock(sb,li);
                sb.append("<br>");  // NOI18N
            }
        }
    }
    sb.append("<br>");
}
 
Example 13
Source File: ThreadDumpWindow.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
private void print16Thread(final StringBuilder sb, final ThreadInfo thread, boolean goToSourceAvailable) {
    MonitorInfo[] monitors = thread.getLockedMonitors();
    sb.append("&nbsp;<b>");   // NOI18N
    sb.append("\"").append(thread.getThreadName()).append("\" - Thread t@").append(thread.getThreadId()).append("<br>");    // NOI18N
    sb.append("    java.lang.Thread.State: ").append(thread.getThreadState()); // NOI18N
    sb.append("</b><br>");   // NOI18N
    int index = 0;
    for (StackTraceElement st : thread.getStackTrace()) {
        LockInfo lock = thread.getLockInfo();
        String stackElementText = htmlize(st.toString());
        String lockOwner = thread.getLockOwnerName();

        String stackEl = stackElementText;
        if (goToSourceAvailable) {
            String className = st.getClassName();
            String method = st.getMethodName();
            int lineNo = st.getLineNumber();
            String stackUrl = OPEN_THREADS_URL + className + "|" + method + "|" + lineNo; // NOI18N
            stackEl = "<a href=\"" + stackUrl + "\">" + stackElementText + "</a>";    // NOI18N
        }

        sb.append("    at ").append(stackEl).append("<br>");    // NOI18N
        if (index == 0) {
            if ("java.lang.Object".equals(st.getClassName()) && // NOI18N
                    "wait".equals(st.getMethodName())) {                // NOI18N
                if (lock != null) {
                    sb.append("    - waiting on ");    // NOI18N
                    printLock(sb, lock);
                    sb.append("<br>");    // NOI18N
                }
            } else if (lock != null) {
                if (lockOwner == null) {
                    sb.append("    - parking to wait for ");      // NOI18N
                    printLock(sb, lock);
                    sb.append("<br>");            // NOI18N
                } else {
                    sb.append("    - waiting to lock ");      // NOI18N
                    printLock(sb, lock);
                    sb.append(" owned by \"").append(lockOwner).append("\" t@").append(thread.getLockOwnerId()).append("<br>");   // NOI18N
                }
            }
        }
        printMonitors(sb, monitors, index);
        index++;
    }
    StringBuilder jnisb = new StringBuilder();
    printMonitors(jnisb, monitors, -1);
    if (jnisb.length() > 0) {
        sb.append("   JNI locked monitors:<br>");
        sb.append(jnisb);
    }
    LockInfo[] synchronizers = thread.getLockedSynchronizers();
    if (synchronizers != null) {
        sb.append("<br>   Locked ownable synchronizers:");    // NOI18N
        if (synchronizers.length == 0) {
            sb.append("<br>    - None\n");  // NOI18N
        } else {
            for (LockInfo li : synchronizers) {
                sb.append("<br>    - locked ");         // NOI18N
                printLock(sb, li);
                sb.append("<br>");  // NOI18N
            }
        }
    }
    sb.append("<br>");
}
 
Example 14
Source File: ThreadDumpUtil.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private static String threadInfoToString(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;
   for (; i < threadInfo.getStackTrace().length; i++) {
      StackTraceElement ste = threadInfo.getStackTrace()[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 : threadInfo.getLockedMonitors()) {
         if (mi.getLockedStackDepth() == i) {
            sb.append("\t-  locked " + mi);
            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();
}
 
Example 15
Source File: GfxdLocalLockService.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void generateThreadDump(StringBuilder msg) {
  ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  for (ThreadInfo tInfo : mbean.dumpAllThreads(true, true)) {
    msg.append('"').append(tInfo.getThreadName()).append('"').append(" Id=")
        .append(tInfo.getThreadId()).append(' ')
        .append(tInfo.getThreadState());
    if (tInfo.getLockName() != null) {
      msg.append(" on ").append(tInfo.getLockName());
    }
    if (tInfo.getLockOwnerName() != null) {
      msg.append(" owned by \"").append(tInfo.getLockOwnerName())
          .append("\" Id=").append(tInfo.getLockOwnerId());
    }
    if (tInfo.isSuspended()) {
      msg.append(" (suspended)");
    }
    if (tInfo.isInNative()) {
      msg.append(" (in native)");
    }
    msg.append(SanityManager.lineSeparator);
    final StackTraceElement[] stackTrace = tInfo.getStackTrace();
    for (int index = 0; index < stackTrace.length; ++index) {
      msg.append("\tat ").append(stackTrace[index].toString())
          .append(SanityManager.lineSeparator);
      if (index == 0 && tInfo.getLockInfo() != null) {
        final Thread.State ts = tInfo.getThreadState();
        switch (ts) {
          case BLOCKED:
            msg.append("\t-  blocked on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          case WAITING:
            msg.append("\t-  waiting on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          case TIMED_WAITING:
            msg.append("\t-  waiting on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          default:
        }
      }

      for (MonitorInfo mi : tInfo.getLockedMonitors()) {
        if (mi.getLockedStackDepth() == index) {
          msg.append("\t-  locked ").append(mi)
              .append(SanityManager.lineSeparator);
        }
      }
    }

    final LockInfo[] locks = tInfo.getLockedSynchronizers();
    if (locks.length > 0) {
      msg.append(SanityManager.lineSeparator)
          .append("\tNumber of locked synchronizers = ").append(locks.length)
          .append(SanityManager.lineSeparator);
      for (LockInfo li : locks) {
        msg.append("\t- ").append(li).append(SanityManager.lineSeparator);
      }
    }
    msg.append(SanityManager.lineSeparator);
  }
}
 
Example 16
Source File: AbstractContainer.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
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();
}
 
Example 17
Source File: DebugUtils.java    From trufflesqueak with MIT License 4 votes vote down vote up
public static void dumpThreads(final StringBuilder sb) {
    CompilerAsserts.neverPartOfCompilation("For debugging purposes only");
    sb.append("\r\n\r\n\r\n").append("Total number of threads started: ").append(ManagementFactory.getThreadMXBean().getTotalStartedThreadCount()).append("\r\n\r\n");

    final Runtime r = Runtime.getRuntime();
    sb.append("Total Memory : ").append(r.totalMemory()).append("\r\n").append("Max Memory   : ").append(r.maxMemory()).append("\r\n").append("Free Memory  : ").append(r.freeMemory()).append(
                    "\r\n\r\n");

    final ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
    for (final ThreadInfo info : threads) {
        sb.append('"').append(info.getThreadName()).append("\" Id=").append(info.getThreadId()).append(' ').append(info.getThreadState());
        if (info.getLockName() != null) {
            sb.append(" on ").append(info.getLockName());
        }
        if (info.getLockOwnerName() != null) {
            sb.append(" owned by \"").append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
        }
        if (info.isSuspended()) {
            sb.append(" (suspended)");
        }
        if (info.isInNative()) {
            sb.append(" (in native)");
        }
        sb.append("\r\n");
        int i = 0;
        for (; i < info.getStackTrace().length; i++) {
            final StackTraceElement ste = info.getStackTrace()[i];
            sb.append("\tat ").append(ste.toString());
            sb.append("\r\n");
            if (i == 0 && info.getLockInfo() != null) {
                final Thread.State ts = info.getThreadState();
                switch (ts) {
                    case BLOCKED:
                        sb.append("\t-  blocked on ").append(info.getLockInfo()).append("\r\n");
                        break;
                    case WAITING:
                        sb.append("\t-  waiting on ").append(info.getLockInfo()).append("\r\n");
                        break;
                    case TIMED_WAITING:
                        sb.append("\t-  waiting on ").append(info.getLockInfo()).append("\r\n");
                        break;
                    default:
                }
            }

            for (final MonitorInfo mi : info.getLockedMonitors()) {
                if (mi.getLockedStackDepth() == i) {
                    sb.append("\t-  locked ").append(mi).append("\r\n");
                }
            }
        }
        if (i < info.getStackTrace().length) {
            sb.append("\t...");
            sb.append("\r\n");
        }

        final LockInfo[] locks = info.getLockedSynchronizers();
        if (locks.length > 0) {
            sb.append("\r\n\tNumber of locked synchronizers = ").append(locks.length).append("\r\n");
            for (final LockInfo li : locks) {
                sb.append("\t- ").append(li).append("\r\n");
            }
        }

        sb.append("\r\n\r\n");
    }
}
 
Example 18
Source File: SampledCPUSnapshot.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void print16Thread(final StringBuilder sb, final ThreadInfo thread, boolean goToSourceAvailable) {
    MonitorInfo[] monitors = thread.getLockedMonitors();
    sb.append("&nbsp;<b>");   // NOI18N
    sb.append("\"").append(thread.getThreadName()).append("\" - Thread t@").append(thread.getThreadId()).append("<br>");    // NOI18N
    sb.append("    java.lang.Thread.State: ").append(thread.getThreadState()); // NOI18N
    sb.append("</b><br>");   // NOI18N
    int index = 0;
    for (StackTraceElement st : thread.getStackTrace()) {
        LockInfo lock = thread.getLockInfo();
        String stackElementText = htmlize(st.toString());
        String lockOwner = thread.getLockOwnerName();
        String className = st.getClassName();
        String method = st.getMethodName();
        int lineNo = st.getLineNumber();
        
        String stackEl = stackElementText;
        if (goToSourceAvailable) {
            String stackUrl = OPEN_THREADS_URL+className+"|"+method+"|"+lineNo; // NOI18N
            stackEl = "<a href=\""+stackUrl+"\">"+stackElementText+"</a>";    // NOI18N
        }

        sb.append("\tat ").append(stackEl).append("<br>");    // NOI18N
        if (index == 0) {
            if ("java.lang.Object".equals(st.getClassName()) &&     // NOI18N
                    "wait".equals(st.getMethodName())) {                // NOI18N
                if (lock != null) {
                    sb.append("\t- waiting on ");    // NOI18N
                    printLock(sb,lock);
                    sb.append("<br>");    // NOI18N
                }
            } else if (lock != null) {
                if (lockOwner == null) {
                    sb.append("\t- parking to wait for ");      // NOI18N
                    printLock(sb,lock);
                    sb.append("<br>");            // NOI18N
                } else {
                    sb.append("\t- waiting to lock ");      // NOI18N
                    printLock(sb,lock);
                    sb.append(" owned by \"").append(lockOwner).append("\" t@").append(thread.getLockOwnerId()).append("<br>");   // NOI18N
                }
            }
        }
        printMonitors(sb, monitors, index);
        index++;
    }
    StringBuilder jnisb = new StringBuilder();
    printMonitors(jnisb, monitors, -1);
    if (jnisb.length() > 0) {
        sb.append("   JNI locked monitors:<br>");
        sb.append(jnisb);
    }
    LockInfo[] synchronizers = thread.getLockedSynchronizers();
    if (synchronizers != null) {
        sb.append("<br>   Locked ownable synchronizers:");    // NOI18N
        if (synchronizers.length == 0) {
            sb.append("<br>\t- None\n");  // NOI18N
        } else {
            for (LockInfo li : synchronizers) {
                sb.append("<br>\t- locked ");         // NOI18N
                printLock(sb,li);
                sb.append("<br>");  // NOI18N
            }
        }
    }
    sb.append("<br>");
}
 
Example 19
Source File: ThreadDumpWindow.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void print16Thread(final StringBuilder sb, final ThreadInfo thread, boolean goToSourceAvailable) {
    MonitorInfo[] monitors = thread.getLockedMonitors();
    sb.append("&nbsp;<b>");   // NOI18N
    sb.append("\"").append(thread.getThreadName()).append("\" - Thread t@").append(thread.getThreadId()).append("<br>");    // NOI18N
    sb.append("    java.lang.Thread.State: ").append(thread.getThreadState()); // NOI18N
    sb.append("</b><br>");   // NOI18N
    int index = 0;
    for (StackTraceElement st : thread.getStackTrace()) {
        LockInfo lock = thread.getLockInfo();
        String stackElementText = htmlize(st.toString());
        String lockOwner = thread.getLockOwnerName();

        String stackEl = stackElementText;
        if (goToSourceAvailable) {
            String className = st.getClassName();
            String method = st.getMethodName();
            int lineNo = st.getLineNumber();
            String stackUrl = OPEN_THREADS_URL + className + "|" + method + "|" + lineNo; // NOI18N
            stackEl = "<a href=\"" + stackUrl + "\">" + stackElementText + "</a>";    // NOI18N
        }

        sb.append("    at ").append(stackEl).append("<br>");    // NOI18N
        if (index == 0) {
            if ("java.lang.Object".equals(st.getClassName()) && // NOI18N
                    "wait".equals(st.getMethodName())) {                // NOI18N
                if (lock != null) {
                    sb.append("    - waiting on ");    // NOI18N
                    printLock(sb, lock);
                    sb.append("<br>");    // NOI18N
                }
            } else if (lock != null) {
                if (lockOwner == null) {
                    sb.append("    - parking to wait for ");      // NOI18N
                    printLock(sb, lock);
                    sb.append("<br>");            // NOI18N
                } else {
                    sb.append("    - waiting to lock ");      // NOI18N
                    printLock(sb, lock);
                    sb.append(" owned by \"").append(lockOwner).append("\" t@").append(thread.getLockOwnerId()).append("<br>");   // NOI18N
                }
            }
        }
        printMonitors(sb, monitors, index);
        index++;
    }
    StringBuilder jnisb = new StringBuilder();
    printMonitors(jnisb, monitors, -1);
    if (jnisb.length() > 0) {
        sb.append("   JNI locked monitors:<br>");
        sb.append(jnisb);
    }
    LockInfo[] synchronizers = thread.getLockedSynchronizers();
    if (synchronizers != null) {
        sb.append("<br>   Locked ownable synchronizers:");    // NOI18N
        if (synchronizers.length == 0) {
            sb.append("<br>    - None\n");  // NOI18N
        } else {
            for (LockInfo li : synchronizers) {
                sb.append("<br>    - locked ");         // NOI18N
                printLock(sb, li);
                sb.append("<br>");  // NOI18N
            }
        }
    }
    sb.append("<br>");
}
 
Example 20
Source File: ThreadDump.java    From metrics with Apache License 2.0 4 votes vote down vote up
/**
 * Dumps all of the threads' current information to an output stream.
 *
 * @param out an output stream
 */
public void dump(OutputStream out) {
    final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(true, true);
    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();
}