View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.monitoring;
21  
22  import java.lang.management.ManagementFactory;
23  import java.lang.management.ThreadInfo;
24  import java.lang.management.ThreadMXBean;
25  
26  public abstract class ThreadMonitoring {
27  
28    private static final ThreadMXBean threadBean = 
29      ManagementFactory.getThreadMXBean();
30    private static final int STACK_DEPTH = 20;
31  
32    public static ThreadInfo getThreadInfo(Thread t) {
33      long tid = t.getId();
34      return threadBean.getThreadInfo(tid, STACK_DEPTH);
35    }
36      
37  
38    /**
39     * Format the given ThreadInfo object as a String.
40     * @param indent a prefix for each line, used for nested indentation
41     */
42    public static String formatThreadInfo(ThreadInfo threadInfo, String indent) {
43      StringBuilder sb = new StringBuilder();
44      appendThreadInfo(sb, threadInfo, indent);
45      return sb.toString();
46    }
47  
48    /**
49     * Print all of the thread's information and stack traces.
50     * 
51     * @param sb
52     * @param info
53     * @param indent
54     */
55    public static void appendThreadInfo(StringBuilder sb,
56                                        ThreadInfo info,
57                                        String indent) {
58      boolean contention = threadBean.isThreadContentionMonitoringEnabled();
59  
60      if (info == null) {
61        sb.append(indent).append("Inactive (perhaps exited while monitoring was done)\n");
62        return;
63      }
64      String taskName = getTaskName(info.getThreadId(), info.getThreadName());
65      sb.append(indent).append("Thread ").append(taskName).append(":\n");
66      
67      Thread.State state = info.getThreadState();
68      sb.append(indent).append("  State: ").append(state).append("\n");
69      sb.append(indent).append("  Blocked count: ").append(info.getBlockedCount()).append("\n");
70      sb.append(indent).append("  Waited count: ").append(info.getWaitedCount()).append("\n");
71      if (contention) {
72        sb.append(indent).append("  Blocked time: " + info.getBlockedTime()).append("\n");
73        sb.append(indent).append("  Waited time: " + info.getWaitedTime()).append("\n");
74      }
75      if (state == Thread.State.WAITING) {
76        sb.append(indent).append("  Waiting on ").append(info.getLockName()).append("\n");
77      } else  if (state == Thread.State.BLOCKED) {
78        sb.append(indent).append("  Blocked on ").append(info.getLockName()).append("\n");
79        sb.append(indent).append("  Blocked by ").append(
80          getTaskName(info.getLockOwnerId(), info.getLockOwnerName())).append("\n");
81      }
82      sb.append(indent).append("  Stack:").append("\n");
83      for (StackTraceElement frame: info.getStackTrace()) {
84        sb.append(indent).append("    ").append(frame.toString()).append("\n");
85      }
86    }
87    
88    private static String getTaskName(long id, String name) {
89      if (name == null) {
90        return Long.toString(id);
91      }
92      return id + " (" + name + ")";
93    }
94  
95  
96  }