2006-08-14 Mark Wielaard <mark@klomp.org>
[official-gcc.git] / libjava / classpath / vm / reference / gnu / java / lang / management / VMThreadMXBeanImpl.java
blobc5bcb631055631b97548a545dcc9934701700d0a
1 /* VMThreadMXBeanImpl.java - VM impl. of a thread bean
2 Copyright (C) 2006 Free Software Foundation
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.java.lang.management;
40 import java.lang.management.ThreadInfo;
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.
50 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
51 * @since 1.5
53 final class VMThreadMXBeanImpl
56 /**
57 * Cache of how many threads were found.
59 private static int filled;
61 /**
62 * Returns the ids of cycles of deadlocked threads, occurring
63 * due to monitor ownership.
65 * @return the ids of the deadlocked threads.
67 static native long[] findMonitorDeadlockedThreads();
69 /* This is the same as in Thread.getAllStackTraces() */
70 static Thread[] getAllThreads()
72 ThreadGroup group = Thread.currentThread().getThreadGroup();
73 while (group.getParent() != null)
74 group = group.getParent();
75 int arraySize = group.activeCount();
76 Thread[] threadList = new Thread[arraySize];
77 filled = group.enumerate(threadList);
78 while (filled == arraySize)
80 arraySize *= 2;
81 threadList = new Thread[arraySize];
82 filled = group.enumerate(threadList);
84 return threadList;
87 /**
88 * Returns the id of all live threads at the time of execution.
90 * @return the live thread ids.
92 static long[] getAllThreadIds()
94 Thread[] threadList = getAllThreads();
95 long[] ids = new long[filled];
96 for (int a = 0; a < filled; ++a)
97 ids[a] = threadList[a].getId();
98 return ids;
102 * Returns the number of nanoseconds of CPU time
103 * the current thread has used in total. This is
104 * only called if this feature is enabled and
105 * supported.
107 * @return the nanoseconds of CPU time used by
108 * the current thread.
110 static native long getCurrentThreadCpuTime();
113 * Returns the number of nanoseconds of user time
114 * the current thread has used in total. This is
115 * only called if this feature is enabled and
116 * supported.
118 * @return the nanoseconds of user time used by
119 * the current thread.
121 static native long getCurrentThreadUserTime();
124 * Returns the number of live daemon threads.
126 * @return the number of live daemon threads.
128 static int getDaemonThreadCount()
130 Thread[] threadList = getAllThreads();
131 int daemonCount = 0;
132 for (int a = 0; a < filled; ++a)
134 if (threadList[a].isDaemon())
135 ++daemonCount;
137 return daemonCount;
141 * Returns the current peak number of live threads.
143 * @return the peak number of live threads.
145 static native int getPeakThreadCount();
148 * Returns the number of live threads.
150 * @return the number of live threads.
152 static int getThreadCount()
154 getAllThreads();
155 return filled;
159 * Returns the number of nanoseconds of CPU time
160 * the specified thread has used in total. This is
161 * only called if this feature is enabled and
162 * supported.
164 * @param id the thread to obtain statistics on.
165 * @return the nanoseconds of CPU time used by
166 * the thread.
168 static native long getThreadCpuTime(long id);
171 * Returns the {@link java.lang.management.ThreadInfo}
172 * which corresponds to the specified id.
174 * @param id the id of the thread.
175 * @param maxDepth the depth of the stack trace.
176 * @return the corresponding <code>ThreadInfo</code>.
178 static native ThreadInfo getThreadInfoForId(long id, int maxDepth);
181 * Returns the number of nanoseconds of user time
182 * the specified thread has used in total. This is
183 * only called if this feature is enabled and
184 * supported.
186 * @param id the thread to obtain statistics on.
187 * @return the nanoseconds of user time used by
188 * the thread.
190 static native long getThreadUserTime(long id);
193 * Returns the total number of threads that have
194 * been started over the lifetime of the virtual
195 * machine.
197 * @return the total number of threads started.
199 static native long getTotalStartedThreadCount();
202 * Resets the peak thread count to the current
203 * number of live threads.
205 static native void resetPeakThreadCount();