Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.mprew.commons;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Code which writes a stack dump for all threads to a file in /var/tmp.
 */
public class DumpStack {

  public static void dumpStacks() throws IOException {
    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0);
    Map<Long, ThreadInfo> threadInfoMap = new HashMap<Long, ThreadInfo>();
    for (ThreadInfo threadInfo : threadInfos) {
      threadInfoMap.put(threadInfo.getThreadId(), threadInfo);
    }

    // choose our dump-file
    File dumpFile = new File("/var/tmp/stacks.", Long.toString(System.currentTimeMillis()));
    Writer writer = new BufferedWriter(new FileWriter(dumpFile));
    try {
      dumpTraces(mxBean, threadInfoMap, writer);
    } finally {
      writer.close();
    }
  }

  private void dumpTraces(ThreadMXBean mxBean, Map<Long, ThreadInfo> threadInfoMap, Writer writer) throws IOException {
    Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();
    writer.write("Dump of " + stacks.size() + " thread at "
        + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(System.currentTimeMillis()))
        + "\n\n");
    for (Map.Entry<Thread, StackTraceElement[]> entry : stacks.entrySet()) {
      Thread thread = entry.getKey();
      writer.write("\"" + thread.getName() + "\" prio=" + thread.getPriority() + " tid=" + thread.getId()
          + " " + thread.getState() + " " + (thread.isDaemon() ? "deamon" : "worker") + "\n");
      ThreadInfo threadInfo = threadInfoMap.get(thread.getId());
      if (threadInfo != null) {
        writer.write("    native=" + threadInfo.isInNative() + ", suspended=" + threadInfo.isSuspended()
            + ", block=" + threadInfo.getBlockedCount() + ", wait=" + threadInfo.getWaitedCount()
            + "\n");
        writer.write("    lock="
            + threadInfo.getLockName()
            + " owned by "
            + threadInfo.getLockOwnerName()
            + " ("
            + threadInfo.getLockOwnerId()
            + "), cpu="
            + (mxBean.getThreadCpuTime(threadInfo.getThreadId()) / 1000000L)
            + ", user="
            + (mxBean.getThreadUserTime(threadInfo.getThreadId()) / 1000000L)
            + "\n");
      }
      for (StackTraceElement element : entry.getValue()) {
        writer.write("        ");
        writer.write(element.toString());
        writer.write("\n");
      }
      writer.write("\n");
    }
  }
}