< prev index next >

src/java.management/share/classes/sun/management/ThreadImpl.java

Print this page

 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.management;
 27 
 28 import java.lang.management.ManagementFactory;
 29 import java.lang.management.ThreadInfo;
 30 import java.lang.management.ThreadMXBean;





 31 import javax.management.ObjectName;
 32 import java.util.Objects;
 33 
 34 /**
 35  * Implementation for java.lang.management.ThreadMXBean as well as providing the
 36  * supporting method for com.sun.management.ThreadMXBean.
 37  * The supporting method for com.sun.management.ThreadMXBean can be moved to
 38  * jdk.management in the future.
 39  */
 40 
 41 public class ThreadImpl implements ThreadMXBean {
 42     private final VMManagement jvm;
 43 
 44     // default for thread contention monitoring is disabled.
 45     private boolean contentionMonitoringEnabled = false;
 46     private boolean cpuTimeEnabled;
 47     private boolean allocatedMemoryEnabled;
 48 
 49     /**
 50      * Constructor of ThreadImpl class.

209         Util.checkControlAccess();
210 
211         synchronized (this) {
212             if (contentionMonitoringEnabled != enable) {
213                 if (enable) {
214                     // if reeabled, reset contention time statistics
215                     // for all threads
216                     resetContentionTimes0(0);
217                 }
218 
219                 // update the VM of the state change
220                 setThreadContentionMonitoringEnabled0(enable);
221 
222                 contentionMonitoringEnabled = enable;
223             }
224         }
225     }
226 
227     private boolean verifyCurrentThreadCpuTime() {
228         // check if Thread CPU time measurement is supported.



229         if (!isCurrentThreadCpuTimeSupported()) {
230             throw new UnsupportedOperationException(
231                 "Current thread CPU time measurement is not supported.");
232         }
233         return isThreadCpuTimeEnabled();
234     }
235 
236     @Override
237     public long getCurrentThreadCpuTime() {
238         if (verifyCurrentThreadCpuTime()) {
239             return getThreadTotalCpuTime0(0);
240         }
241         return -1;
242     }
243 
244     @Override
245     public long getThreadCpuTime(long id) {
246         long[] ids = new long[1];
247         ids[0] = id;
248         final long[] times = getThreadCpuTime(ids);

266                     throw new UnsupportedOperationException(
267                         "Thread CPU time measurement is only supported" +
268                         " for the current thread.");
269                 }
270             }
271         }
272 
273         return isThreadCpuTimeEnabled();
274     }
275 
276     protected long[] getThreadCpuTime(long[] ids) {
277         boolean verified = verifyThreadCpuTime(ids);
278 
279         int length = ids.length;
280         long[] times = new long[length];
281         java.util.Arrays.fill(times, -1);
282 
283         if (verified) {
284             if (length == 1) {
285                 long id = ids[0];
286                 if (id == Thread.currentThread().getId()) {
287                     id = 0;







288                 }
289                 times[0] = getThreadTotalCpuTime0(id);
290             } else {
291                 getThreadTotalCpuTime1(ids, times);
292             }
293         }
294         return times;
295     }
296 
297     @Override
298     public long getCurrentThreadUserTime() {
299         if (verifyCurrentThreadCpuTime()) {
300             return getThreadUserCpuTime0(0);
301         }
302         return -1;
303     }
304 
305     @Override
306     public long getThreadUserTime(long id) {
307         long[] ids = new long[1];
308         ids[0] = id;
309         final long[] times = getThreadUserTime(ids);
310         return times[0];
311     }
312 
313     protected long[] getThreadUserTime(long[] ids) {
314         boolean verified = verifyThreadCpuTime(ids);
315 
316         int length = ids.length;
317         long[] times = new long[length];
318         java.util.Arrays.fill(times, -1);
319 
320         if (verified) {
321             if (length == 1) {
322                 long id = ids[0];
323                 if (id == Thread.currentThread().getId()) {
324                     id = 0;







325                 }
326                 times[0] = getThreadUserCpuTime0(id);
327             } else {
328                 getThreadUserCpuTime1(ids, times);
329             }
330         }
331         return times;
332     }
333 
334     @Override
335     public void setThreadCpuTimeEnabled(boolean enable) {
336         if (!isThreadCpuTimeSupported() &&
337             !isCurrentThreadCpuTimeSupported()) {
338             throw new UnsupportedOperationException(
339                 "Thread CPU time measurement is not supported");
340         }
341 
342         Util.checkControlAccess();
343         synchronized (this) {
344             if (cpuTimeEnabled != enable) {
345                 // notify VM of the state change
346                 setThreadCpuTimeEnabled0(enable);
347                 cpuTimeEnabled = enable;
348             }
349         }
350     }
351 
352     protected long getCurrentThreadAllocatedBytes() {
353         if (isThreadAllocatedMemoryEnabled()) {
354             return getThreadAllocatedMemory0(0);
355         }
356         return -1;
357     }
358 
359     private boolean verifyThreadAllocatedMemory(long id) {
360         verifyThreadId(id);
361         return isThreadAllocatedMemoryEnabled();
362     }
363 
364     protected long getThreadAllocatedBytes(long id) {
365         boolean verified = verifyThreadAllocatedMemory(id);
366 
367         if (verified) {
368             return getThreadAllocatedMemory0(
369                 Thread.currentThread().getId() == id ? 0 : id);








370         }
371         return -1;
372     }
373 
374     private boolean verifyThreadAllocatedMemory(long[] ids) {
375         verifyThreadIds(ids);
376         return isThreadAllocatedMemoryEnabled();
377     }
378 
379     protected long[] getThreadAllocatedBytes(long[] ids) {
380         Objects.requireNonNull(ids);
381 
382         if (ids.length == 1) {
383             long size = getThreadAllocatedBytes(ids[0]);
384             return new long[] { size };
385         }
386 
387         boolean verified = verifyThreadAllocatedMemory(ids);
388 
389         long[] sizes = new long[ids.length];

534     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
535     private static native void setThreadCpuTimeEnabled0(boolean enable);
536     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
537     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
538     private static native Thread[] findMonitorDeadlockedThreads0();
539     private static native Thread[] findDeadlockedThreads0();
540     private static native void resetPeakThreadCount0();
541     private static native ThreadInfo[] dumpThreads0(long[] ids,
542                                                     boolean lockedMonitors,
543                                                     boolean lockedSynchronizers,
544                                                     int maxDepth);
545 
546     // tid == 0 to reset contention times for all threads
547     private static native void resetContentionTimes0(long tid);
548 
549     @Override
550     public ObjectName getObjectName() {
551         return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
552     }
553 























554 }

 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.management;
 27 
 28 import java.lang.management.ManagementFactory;
 29 import java.lang.management.ThreadInfo;
 30 import java.lang.management.ThreadMXBean;
 31 import java.lang.reflect.InvocationTargetException;
 32 import java.lang.reflect.Method;
 33 import java.security.AccessController;
 34 import java.security.PrivilegedActionException;
 35 import java.security.PrivilegedExceptionAction;
 36 import javax.management.ObjectName;
 37 import java.util.Objects;
 38 
 39 /**
 40  * Implementation for java.lang.management.ThreadMXBean as well as providing the
 41  * supporting method for com.sun.management.ThreadMXBean.
 42  * The supporting method for com.sun.management.ThreadMXBean can be moved to
 43  * jdk.management in the future.
 44  */
 45 
 46 public class ThreadImpl implements ThreadMXBean {
 47     private final VMManagement jvm;
 48 
 49     // default for thread contention monitoring is disabled.
 50     private boolean contentionMonitoringEnabled = false;
 51     private boolean cpuTimeEnabled;
 52     private boolean allocatedMemoryEnabled;
 53 
 54     /**
 55      * Constructor of ThreadImpl class.

214         Util.checkControlAccess();
215 
216         synchronized (this) {
217             if (contentionMonitoringEnabled != enable) {
218                 if (enable) {
219                     // if reeabled, reset contention time statistics
220                     // for all threads
221                     resetContentionTimes0(0);
222                 }
223 
224                 // update the VM of the state change
225                 setThreadContentionMonitoringEnabled0(enable);
226 
227                 contentionMonitoringEnabled = enable;
228             }
229         }
230     }
231 
232     private boolean verifyCurrentThreadCpuTime() {
233         // check if Thread CPU time measurement is supported.
234         if (isVirtual(Thread.currentThread())) {
235             throw new UnsupportedOperationException("Not supported by virtual threads");
236         }
237         if (!isCurrentThreadCpuTimeSupported()) {
238             throw new UnsupportedOperationException(
239                 "Current thread CPU time measurement is not supported.");
240         }
241         return isThreadCpuTimeEnabled();
242     }
243 
244     @Override
245     public long getCurrentThreadCpuTime() {
246         if (verifyCurrentThreadCpuTime()) {
247             return getThreadTotalCpuTime0(0);
248         }
249         return -1;
250     }
251 
252     @Override
253     public long getThreadCpuTime(long id) {
254         long[] ids = new long[1];
255         ids[0] = id;
256         final long[] times = getThreadCpuTime(ids);

274                     throw new UnsupportedOperationException(
275                         "Thread CPU time measurement is only supported" +
276                         " for the current thread.");
277                 }
278             }
279         }
280 
281         return isThreadCpuTimeEnabled();
282     }
283 
284     protected long[] getThreadCpuTime(long[] ids) {
285         boolean verified = verifyThreadCpuTime(ids);
286 
287         int length = ids.length;
288         long[] times = new long[length];
289         java.util.Arrays.fill(times, -1);
290 
291         if (verified) {
292             if (length == 1) {
293                 long id = ids[0];
294                 Thread thread = Thread.currentThread();
295                 if (id == thread.getId()) {
296                     if (isVirtual(thread)) {
297                         times[0] = -1;
298                     } else {
299                         times[0] = getThreadTotalCpuTime0(0);
300                     }
301                 } else {
302                     times[0] = getThreadTotalCpuTime0(id);
303                 }

304             } else {
305                 getThreadTotalCpuTime1(ids, times);
306             }
307         }
308         return times;
309     }
310 
311     @Override
312     public long getCurrentThreadUserTime() {
313         if (verifyCurrentThreadCpuTime()) {
314             return getThreadUserCpuTime0(0);
315         }
316         return -1;
317     }
318 
319     @Override
320     public long getThreadUserTime(long id) {
321         long[] ids = new long[1];
322         ids[0] = id;
323         final long[] times = getThreadUserTime(ids);
324         return times[0];
325     }
326 
327     protected long[] getThreadUserTime(long[] ids) {
328         boolean verified = verifyThreadCpuTime(ids);
329 
330         int length = ids.length;
331         long[] times = new long[length];
332         java.util.Arrays.fill(times, -1);
333 
334         if (verified) {
335             if (length == 1) {
336                 long id = ids[0];
337                 Thread thread = Thread.currentThread();
338                 if (id == thread.getId()) {
339                     if (isVirtual(thread)) {
340                         times[0] = -1;
341                     } else {
342                         times[0] = getThreadUserCpuTime0(0);
343                     }
344                 } else {
345                     times[0] = getThreadUserCpuTime0(id);
346                 }

347             } else {
348                 getThreadUserCpuTime1(ids, times);
349             }
350         }
351         return times;
352     }
353 
354     @Override
355     public void setThreadCpuTimeEnabled(boolean enable) {
356         if (!isThreadCpuTimeSupported() &&
357             !isCurrentThreadCpuTimeSupported()) {
358             throw new UnsupportedOperationException(
359                 "Thread CPU time measurement is not supported");
360         }
361 
362         Util.checkControlAccess();
363         synchronized (this) {
364             if (cpuTimeEnabled != enable) {
365                 // notify VM of the state change
366                 setThreadCpuTimeEnabled0(enable);
367                 cpuTimeEnabled = enable;
368             }
369         }
370     }
371 
372     protected long getCurrentThreadAllocatedBytes() {
373         if (isThreadAllocatedMemoryEnabled() && !isVirtual(Thread.currentThread())) {
374             return getThreadAllocatedMemory0(0);
375         }
376         return -1;
377     }
378 
379     private boolean verifyThreadAllocatedMemory(long id) {
380         verifyThreadId(id);
381         return isThreadAllocatedMemoryEnabled();
382     }
383 
384     protected long getThreadAllocatedBytes(long id) {
385         boolean verified = verifyThreadAllocatedMemory(id);

386         if (verified) {
387             Thread thread = Thread.currentThread();
388             if (id == thread.getId()) {
389                 if (isVirtual(thread)) {
390                     return -1L;
391                 } else {
392                     return getThreadAllocatedMemory0(0);
393                 }
394             } else {
395                 return getThreadAllocatedMemory0(id);
396             }
397         }
398         return -1;
399     }
400 
401     private boolean verifyThreadAllocatedMemory(long[] ids) {
402         verifyThreadIds(ids);
403         return isThreadAllocatedMemoryEnabled();
404     }
405 
406     protected long[] getThreadAllocatedBytes(long[] ids) {
407         Objects.requireNonNull(ids);
408 
409         if (ids.length == 1) {
410             long size = getThreadAllocatedBytes(ids[0]);
411             return new long[] { size };
412         }
413 
414         boolean verified = verifyThreadAllocatedMemory(ids);
415 
416         long[] sizes = new long[ids.length];

561     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
562     private static native void setThreadCpuTimeEnabled0(boolean enable);
563     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
564     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
565     private static native Thread[] findMonitorDeadlockedThreads0();
566     private static native Thread[] findDeadlockedThreads0();
567     private static native void resetPeakThreadCount0();
568     private static native ThreadInfo[] dumpThreads0(long[] ids,
569                                                     boolean lockedMonitors,
570                                                     boolean lockedSynchronizers,
571                                                     int maxDepth);
572 
573     // tid == 0 to reset contention times for all threads
574     private static native void resetContentionTimes0(long tid);
575 
576     @Override
577     public ObjectName getObjectName() {
578         return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
579     }
580 
581     /**
582      * Returns true if the given Thread is a virutal thread.
583      */
584     private static boolean isVirtual(Thread thread) {
585         try {
586             return (boolean) IS_VIRTUAL.invoke(thread);
587         } catch (IllegalAccessException | InvocationTargetException e) {
588             throw new InternalError(e);
589         }
590     }
591 
592     static final Method IS_VIRTUAL;
593     static {
594         try {
595             PrivilegedExceptionAction<Method> pa = () -> Thread.class.getMethod("isVirtual");
596             @SuppressWarnings("removal")
597             Method m = AccessController.doPrivileged(pa);
598             IS_VIRTUAL = m;
599         } catch (PrivilegedActionException e) {
600             throw new InternalError(e);
601         }
602 
603     }
604 }
< prev index next >