1 /* VMThreadMXBeanImpl.java - VM impl. of a thread bean 2 Copyright (C) 2006 Free Software Foundation 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 package gnu.java.lang.management; 39 40 import java.lang.management.ThreadInfo; 41 42 /** 43 * Provides access to information about the threads 44 * of the virtual machine. An instance of this bean is 45 * obtained by calling 46 * {@link ManagementFactory#getThreadMXBean()}. 47 * See {@link java.lang.management.ThreadMXBean} for 48 * full documentation. 49 * 50 * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 51 * @since 1.5 52 */ 53 final class VMThreadMXBeanImpl 54 { 55 56 /** 57 * Cache of how many threads were found. 58 */ 59 private static int filled; 60 61 /** 62 * Returns the ids of cycles of deadlocked threads, occurring 63 * due to monitor ownership or ownable synchronizer ownership. 64 * This will only be called if ownable synchronizer monitoring 65 * is supported. 66 * 67 * @return the ids of the deadlocked threads. 68 */ findDeadlockedThreads()69 static native long[] findDeadlockedThreads(); 70 71 /** 72 * Returns the ids of cycles of deadlocked threads, occurring 73 * due to monitor ownership. 74 * 75 * @return the ids of the deadlocked threads. 76 */ findMonitorDeadlockedThreads()77 static native long[] findMonitorDeadlockedThreads(); 78 79 /* This is the same as in Thread.getAllStackTraces() */ getAllThreads()80 static Thread[] getAllThreads() 81 { 82 ThreadGroup group = Thread.currentThread().getThreadGroup(); 83 while (group.getParent() != null) 84 group = group.getParent(); 85 int arraySize = group.activeCount(); 86 Thread[] threadList = new Thread[arraySize]; 87 filled = group.enumerate(threadList); 88 while (filled == arraySize) 89 { 90 arraySize *= 2; 91 threadList = new Thread[arraySize]; 92 filled = group.enumerate(threadList); 93 } 94 return threadList; 95 } 96 97 /** 98 * Returns the id of all live threads at the time of execution. 99 * 100 * @return the live thread ids. 101 */ getAllThreadIds()102 static long[] getAllThreadIds() 103 { 104 Thread[] threadList = getAllThreads(); 105 long[] ids = new long[filled]; 106 for (int a = 0; a < filled; ++a) 107 ids[a] = threadList[a].getId(); 108 return ids; 109 } 110 111 /** 112 * Returns the number of nanoseconds of CPU time 113 * the current thread has used in total. This is 114 * only called if this feature is enabled and 115 * supported. 116 * 117 * @return the nanoseconds of CPU time used by 118 * the current thread. 119 */ getCurrentThreadCpuTime()120 static native long getCurrentThreadCpuTime(); 121 122 /** 123 * Returns the number of nanoseconds of user time 124 * the current thread has used in total. This is 125 * only called if this feature is enabled and 126 * supported. 127 * 128 * @return the nanoseconds of user time used by 129 * the current thread. 130 */ getCurrentThreadUserTime()131 static native long getCurrentThreadUserTime(); 132 133 /** 134 * Returns the number of live daemon threads. 135 * 136 * @return the number of live daemon threads. 137 */ getDaemonThreadCount()138 static int getDaemonThreadCount() 139 { 140 Thread[] threadList = getAllThreads(); 141 int daemonCount = 0; 142 for (int a = 0; a < filled; ++a) 143 { 144 if (threadList[a].isDaemon()) 145 ++daemonCount; 146 } 147 return daemonCount; 148 } 149 150 /** 151 * Fill out the given {@link ThreadInfo} object 152 * with ownable synchronizer usage information. 153 * This is only called if ownable synchronizer 154 * usage monitoring is supported. 155 * 156 * @param info the {@link ThreadInfo} object to modify. 157 */ getLockInfo(ThreadInfo info)158 static native void getLockInfo(ThreadInfo info); 159 160 /** 161 * Fill out the given {@link ThreadInfo} object 162 * with monitor usage information. This is only 163 * called if monitor usage monitoring is supported. 164 * 165 * @param info the {@link ThreadInfo} object to modify. 166 */ getMonitorInfo(ThreadInfo info)167 static native void getMonitorInfo(ThreadInfo info); 168 169 /** 170 * Returns the current peak number of live threads. 171 * 172 * @return the peak number of live threads. 173 */ getPeakThreadCount()174 static native int getPeakThreadCount(); 175 176 /** 177 * Returns the number of live threads. 178 * 179 * @return the number of live threads. 180 */ getThreadCount()181 static int getThreadCount() 182 { 183 getAllThreads(); 184 return filled; 185 } 186 187 /** 188 * Returns the number of nanoseconds of CPU time 189 * the specified thread has used in total. This is 190 * only called if this feature is enabled and 191 * supported. 192 * 193 * @param id the thread to obtain statistics on. 194 * @return the nanoseconds of CPU time used by 195 * the thread. 196 */ getThreadCpuTime(long id)197 static native long getThreadCpuTime(long id); 198 199 /** 200 * Returns the {@link java.lang.management.ThreadInfo} 201 * which corresponds to the specified id. 202 * 203 * @param id the id of the thread. 204 * @param maxDepth the depth of the stack trace. 205 * @return the corresponding <code>ThreadInfo</code>. 206 */ getThreadInfoForId(long id, int maxDepth)207 static native ThreadInfo getThreadInfoForId(long id, int maxDepth); 208 209 /** 210 * Returns the number of nanoseconds of user time 211 * the specified thread has used in total. This is 212 * only called if this feature is enabled and 213 * supported. 214 * 215 * @param id the thread to obtain statistics on. 216 * @return the nanoseconds of user time used by 217 * the thread. 218 */ getThreadUserTime(long id)219 static native long getThreadUserTime(long id); 220 221 /** 222 * Returns the total number of threads that have 223 * been started over the lifetime of the virtual 224 * machine. 225 * 226 * @return the total number of threads started. 227 */ getTotalStartedThreadCount()228 static native long getTotalStartedThreadCount(); 229 230 /** 231 * Resets the peak thread count to the current 232 * number of live threads. 233 */ resetPeakThreadCount()234 static native void resetPeakThreadCount(); 235 236 } 237