Skip to content

Commit

Permalink
vjtop 打印线程时输出更多信息 #127
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Sep 21, 2018
1 parent 34f3ea0 commit 37fd9db
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void printView() throws Exception {

private boolean checkState() {
if (vmInfo.state != VMInfo.VMInfoState.ATTACHED && vmInfo.state != VMInfo.VMInfoState.ATTACHED_UPDATE_ERROR) {
System.out.println(System.lineSeparator() + Formats.red("ERROR: Could not attach to process, exit now."));
System.out.println("\n" + Formats.red("ERROR: Could not attach to process, exit now."));
shoulExit();
return false;
}
Expand Down Expand Up @@ -439,7 +439,7 @@ private void printWelcome() {

private void printJmxError() {
if (!vmInfo.currentGcCause.equals("No GC")) {
System.out.println(System.lineSeparator() + Formats.red(
System.out.println("\n" + Formats.red(
"ERROR: Could not fetch data via JMX - Process is doing GC, cause is " + vmInfo.currentGcCause));
} else {
System.out.println(
Expand Down Expand Up @@ -494,33 +494,52 @@ public void printTopStack() throws IOException {

public StackTraceElement[] printSingleThread(ThreadInfo info) {
StackTraceElement[] trace = info.getStackTrace();
String blockedMsg;
StringBuilder sb = new StringBuilder(512);

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

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

System.out.println(" " + info.getThreadId() + ": \"" + info.getThreadName() + "\"" + blockedMsg
+ System.lineSeparator() + " java.lang.Thread.State: " + info.getThreadState().toString());
if (info.getLockName() != null) {
sb.append(" blocked on " + info.getLockName());
}
if (info.getLockOwnerName() != null) {
sb.append(" owned by " + info.getLockOwnerId() + ":\"" + info.getLockOwnerName() + "\"");
}
if (info.isSuspended()) {
sb.append(" (suspended)");
}
if (info.isInNative()) {
sb.append(" (in native)");
}
sb.append('\n');
sb.append("\n java.lang.Thread.State: " + info.getThreadState().toString()).append("\n");

for (StackTraceElement traceElement : trace) {
System.out.println("\tat " + traceElement);
sb.append("\tat ").append(traceElement).append("\n");
}

if (info.getLockOwnerId() != -1) {
System.out.println(" blocked by thread:" + info.getLockOwnerId() + ":" + info.getLockOwnerName());
sb.append(" blocked by thread:" + info.getLockOwnerId() + ":" + info.getLockOwnerName() + "\n");
}

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

return trace;
}

/**
* 打印所有线程,只获取名称不获取stack,不造成停顿
*/
public void printAllThreads() throws IOException {
System.out.println(System.lineSeparator() + " Thread Id and name of all live threads:");
System.out.println("\n Thread Id and name of all live threads:");

long tids[] = vmInfo.getAllThreadIds();
ThreadInfo[] threadInfos = vmInfo.getThreadInfo(tids);
Expand All @@ -544,7 +563,7 @@ public void printAllThreads() throws IOException {
}

public void printBlockedThreads() throws IOException {
System.out.println(System.lineSeparator() + " Stack trace of blocked threads:");
System.out.println("\n Stack trace of blocked threads:");
int counter = 0;
ThreadInfo[] threadInfos = vmInfo.getAllThreadInfo();
for (ThreadInfo info : threadInfos) {
Expand Down

0 comments on commit 37fd9db

Please sign in to comment.