View Javadoc

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