jdk/test/java/lang/management/ThreadMXBean/ThreadDump.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 5506 202f599c92aa
child 29101 55f7a91aaa32
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * Thread Dump utility class for printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class ThreadDump {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private static final String INDENT = "   ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void printThreadInfo(ThreadInfo ti) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                                             " Id=" + ti.getThreadId() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                                             " in " + ti.getThreadState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        if (ti.getLockName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            sb.append(" on lock=" + ti.getLockName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        if (ti.isSuspended()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            sb.append(" (suspended)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (ti.isInNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            sb.append(" (running in native)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.out.println(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (ti.getLockOwnerName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
             System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                " Id=" + ti.getLockOwnerId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        StackTraceElement[] stacktrace = ti.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        MonitorInfo[] monitors = ti.getLockedMonitors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for (int i = 0; i < stacktrace.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            StackTraceElement ste = stacktrace[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.out.println(INDENT + "at " + ste.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            for (MonitorInfo mi : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                if (mi.getLockedStackDepth() == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    System.out.println(INDENT + "  - locked " + mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void printStack(StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println(INDENT + "Stack: (length = " + stack.length + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        for (int j = 0; j < stack.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.out.println(INDENT + INDENT + stack[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static void dumpStacks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // Get stack traces of all Threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Map m = Thread.getAllStackTraces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Set s = m.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Iterator iter = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Map.Entry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            entry = (Map.Entry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            Thread t = (Thread) entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            StackTraceElement[] stack = (StackTraceElement[]) entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            printStack(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static void printLockInfo(LockInfo[] locks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
       System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
       for (LockInfo li : locks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           System.out.println(INDENT + "  - " + li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static void threadDump() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
       System.out.println("Full Java thread dump");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
       ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
       for (ThreadInfo ti : tinfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
           printThreadInfo(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
           LockInfo[] syncs = ti.getLockedSynchronizers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
           printLockInfo(syncs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
           System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}