< prev index next >

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

Print this page




  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 
  33 /**
  34  * Implementation for java.lang.management.ThreadMXBean as well as providing the
  35  * supporting method for com.sun.management.ThreadMXBean.
  36  * The supporting method for com.sun.management.ThreadMXBean can be moved to
  37  * jdk.management in the future.
  38  */
  39 
  40 public class ThreadImpl implements ThreadMXBean {
  41     private final VMManagement jvm;
  42 



  43     // default for thread contention monitoring is disabled.
  44     private boolean contentionMonitoringEnabled = false;
  45     private boolean cpuTimeEnabled;
  46     private boolean allocatedMemoryEnabled;
  47 
  48     /**
  49      * Constructor of ThreadImpl class.
  50      */
  51     protected ThreadImpl(VMManagement vm) {
  52         this.jvm = vm;
  53         this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
  54         this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
  55     }
  56 
  57     @Override
  58     public int getThreadCount() {
  59         return jvm.getLiveThreadCount();
  60     }
  61 
  62     @Override


  98         return jvm.isCurrentThreadCpuTimeSupported();
  99     }
 100 
 101     protected boolean isThreadAllocatedMemorySupported() {
 102         return jvm.isThreadAllocatedMemorySupported();
 103     }
 104 
 105     @Override
 106     public boolean isThreadCpuTimeEnabled() {
 107         if (!isThreadCpuTimeSupported() &&
 108             !isCurrentThreadCpuTimeSupported()) {
 109             throw new UnsupportedOperationException(
 110                 "Thread CPU time measurement is not supported");
 111         }
 112         return cpuTimeEnabled;
 113     }
 114 
 115     protected boolean isThreadAllocatedMemoryEnabled() {
 116         if (!isThreadAllocatedMemorySupported()) {
 117             throw new UnsupportedOperationException(
 118                 "Thread allocated memory measurement is not supported");
 119         }
 120         return allocatedMemoryEnabled;
 121     }
 122 
 123     @Override
 124     public long[] getAllThreadIds() {
 125         Util.checkMonitorAccess();
 126 
 127         Thread[] threads = getThreads();
 128         int length = threads.length;
 129         long[] ids = new long[length];
 130         for (int i = 0; i < length; i++) {
 131             Thread t = threads[i];
 132             ids[i] = t.getId();
 133         }
 134         return ids;
 135     }
 136 
 137     @Override
 138     public ThreadInfo getThreadInfo(long id) {
 139         long[] ids = new long[1];
 140         ids[0] = id;
 141         final ThreadInfo[] infos = getThreadInfo(ids, 0);
 142         return infos[0];
 143     }
 144 
 145     @Override
 146     public ThreadInfo getThreadInfo(long id, int maxDepth) {
 147         long[] ids = new long[1];
 148         ids[0] = id;
 149         final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
 150         return infos[0];
 151     }
 152 
 153     @Override
 154     public ThreadInfo[] getThreadInfo(long[] ids) {
 155         return getThreadInfo(ids, 0);
 156     }
 157 







 158     private void verifyThreadIds(long[] ids) {
 159         if (ids == null) {
 160             throw new NullPointerException("Null ids parameter.");
 161         }
 162 
 163         for (int i = 0; i < ids.length; i++) {
 164             if (ids[i] <= 0) {
 165                 throw new IllegalArgumentException(
 166                     "Invalid thread ID parameter: " + ids[i]);
 167             }
 168         }
 169     }
 170 
 171     @Override
 172     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
 173         verifyThreadIds(ids);
 174 
 175         if (maxDepth < 0) {
 176             throw new IllegalArgumentException(
 177                 "Invalid maxDepth parameter: " + maxDepth);
 178         }
 179 
 180         // ids has been verified to be non-null
 181         // an empty array of ids should return an empty array of ThreadInfos
 182         if (ids.length == 0) return new ThreadInfo[0];
 183 
 184         Util.checkMonitorAccess();
 185 
 186         ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
 187         if (maxDepth == Integer.MAX_VALUE) {


 325     }
 326 
 327     @Override
 328     public void setThreadCpuTimeEnabled(boolean enable) {
 329         if (!isThreadCpuTimeSupported() &&
 330             !isCurrentThreadCpuTimeSupported()) {
 331             throw new UnsupportedOperationException(
 332                 "Thread CPU time measurement is not supported");
 333         }
 334 
 335         Util.checkControlAccess();
 336         synchronized (this) {
 337             if (cpuTimeEnabled != enable) {
 338                 // notify VM of the state change
 339                 setThreadCpuTimeEnabled0(enable);
 340                 cpuTimeEnabled = enable;
 341             }
 342         }
 343     }
 344 
 345     protected long getThreadAllocatedBytes(long id) {
 346         long[] ids = new long[1];
 347         ids[0] = id;
 348         final long[] sizes = getThreadAllocatedBytes(ids);
 349         return sizes[0];

 350     }
 351 
 352     private boolean verifyThreadAllocatedMemory(long[] ids) {
 353         verifyThreadIds(ids);




 354 
 355         // check if Thread allocated memory measurement is supported.
 356         if (!isThreadAllocatedMemorySupported()) {
 357             throw new UnsupportedOperationException(
 358                 "Thread allocated memory measurement is not supported.");
 359         }
 360 
 361         return isThreadAllocatedMemoryEnabled();












 362     }
 363 
 364     protected long[] getThreadAllocatedBytes(long[] ids) {
 365         boolean verified = verifyThreadAllocatedMemory(ids);
 366 
 367         long[] sizes = new long[ids.length];



 368         java.util.Arrays.fill(sizes, -1);

 369 
 370         if (verified) {





 371             getThreadAllocatedMemory1(ids, sizes);
 372         }

 373         return sizes;
 374     }
 375 
 376     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
 377         if (!isThreadAllocatedMemorySupported()) {
 378             throw new UnsupportedOperationException(
 379                 "Thread allocated memory measurement is not supported.");
 380         }
 381 
 382         Util.checkControlAccess();
 383         synchronized (this) {
 384             if (allocatedMemoryEnabled != enable) {
 385                 // notify VM of the state change
 386                 setThreadAllocatedMemoryEnabled0(enable);
 387                 allocatedMemoryEnabled = enable;
 388             }
 389         }
 390     }
 391 
 392     @Override
 393     public long[] findMonitorDeadlockedThreads() {
 394         Util.checkMonitorAccess();
 395 
 396         Thread[] threads = findMonitorDeadlockedThreads0();
 397         if (threads == null) {
 398             return null;
 399         }


 494     public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
 495                                        boolean lockedSynchronizers,
 496                                        int maxDepth) {
 497         if (maxDepth < 0) {
 498             throw new IllegalArgumentException(
 499                     "Invalid maxDepth parameter: " + maxDepth);
 500         }
 501         verifyDumpThreads(lockedMonitors, lockedSynchronizers);
 502         return dumpThreads0(null, lockedMonitors, lockedSynchronizers, maxDepth);
 503     }
 504 
 505     // VM support where maxDepth == -1 to request entire stack dump
 506     private static native Thread[] getThreads();
 507     private static native void getThreadInfo1(long[] ids,
 508                                               int maxDepth,
 509                                               ThreadInfo[] result);
 510     private static native long getThreadTotalCpuTime0(long id);
 511     private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
 512     private static native long getThreadUserCpuTime0(long id);
 513     private static native void getThreadUserCpuTime1(long[] ids, long[] result);

 514     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
 515     private static native void setThreadCpuTimeEnabled0(boolean enable);
 516     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
 517     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
 518     private static native Thread[] findMonitorDeadlockedThreads0();
 519     private static native Thread[] findDeadlockedThreads0();
 520     private static native void resetPeakThreadCount0();
 521     private static native ThreadInfo[] dumpThreads0(long[] ids,
 522                                                     boolean lockedMonitors,
 523                                                     boolean lockedSynchronizers,
 524                                                     int maxDepth);
 525 
 526     // tid == 0 to reset contention times for all threads
 527     private static native void resetContentionTimes0(long tid);
 528 
 529     @Override
 530     public ObjectName getObjectName() {
 531         return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
 532     }
 533 


  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 
  33 /**
  34  * Implementation for java.lang.management.ThreadMXBean as well as providing the
  35  * supporting method for com.sun.management.ThreadMXBean.
  36  * The supporting method for com.sun.management.ThreadMXBean can be moved to
  37  * jdk.management in the future.
  38  */
  39 
  40 public class ThreadImpl implements ThreadMXBean {
  41     private final VMManagement jvm;
  42 
  43     private static final String THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED =
  44         "Thread allocated memory measurement is not supported.";
  45 
  46     // default for thread contention monitoring is disabled.
  47     private boolean contentionMonitoringEnabled = false;
  48     private boolean cpuTimeEnabled;
  49     private boolean allocatedMemoryEnabled;
  50 
  51     /**
  52      * Constructor of ThreadImpl class.
  53      */
  54     protected ThreadImpl(VMManagement vm) {
  55         this.jvm = vm;
  56         this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
  57         this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
  58     }
  59 
  60     @Override
  61     public int getThreadCount() {
  62         return jvm.getLiveThreadCount();
  63     }
  64 
  65     @Override


 101         return jvm.isCurrentThreadCpuTimeSupported();
 102     }
 103 
 104     protected boolean isThreadAllocatedMemorySupported() {
 105         return jvm.isThreadAllocatedMemorySupported();
 106     }
 107 
 108     @Override
 109     public boolean isThreadCpuTimeEnabled() {
 110         if (!isThreadCpuTimeSupported() &&
 111             !isCurrentThreadCpuTimeSupported()) {
 112             throw new UnsupportedOperationException(
 113                 "Thread CPU time measurement is not supported");
 114         }
 115         return cpuTimeEnabled;
 116     }
 117 
 118     protected boolean isThreadAllocatedMemoryEnabled() {
 119         if (!isThreadAllocatedMemorySupported()) {
 120             throw new UnsupportedOperationException(
 121                 THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED);
 122         }
 123         return allocatedMemoryEnabled;
 124     }
 125 
 126     @Override
 127     public long[] getAllThreadIds() {
 128         Util.checkMonitorAccess();
 129 
 130         Thread[] threads = getThreads();
 131         int length = threads.length;
 132         long[] ids = new long[length];
 133         for (int i = 0; i < length; i++) {
 134             Thread t = threads[i];
 135             ids[i] = t.getId();
 136         }
 137         return ids;
 138     }
 139 
 140     @Override
 141     public ThreadInfo getThreadInfo(long id) {
 142         long[] ids = new long[1];
 143         ids[0] = id;
 144         final ThreadInfo[] infos = getThreadInfo(ids, 0);
 145         return infos[0];
 146     }
 147 
 148     @Override
 149     public ThreadInfo getThreadInfo(long id, int maxDepth) {
 150         long[] ids = new long[1];
 151         ids[0] = id;
 152         final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
 153         return infos[0];
 154     }
 155 
 156     @Override
 157     public ThreadInfo[] getThreadInfo(long[] ids) {
 158         return getThreadInfo(ids, 0);
 159     }
 160 
 161     private void verifyThreadId(long id) {
 162         if (id <= 0) {
 163             throw new IllegalArgumentException(
 164                 "Invalid thread ID parameter: " + id);
 165         }
 166     }
 167 
 168     private void verifyThreadIds(long[] ids) {
 169         if (ids == null) {
 170             throw new NullPointerException("Null ids parameter.");
 171         }
 172 
 173         for (int i = 0; i < ids.length; i++) {
 174             verifyThreadId(ids[i]);



 175         }
 176     }
 177 
 178     @Override
 179     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
 180         verifyThreadIds(ids);
 181 
 182         if (maxDepth < 0) {
 183             throw new IllegalArgumentException(
 184                 "Invalid maxDepth parameter: " + maxDepth);
 185         }
 186 
 187         // ids has been verified to be non-null
 188         // an empty array of ids should return an empty array of ThreadInfos
 189         if (ids.length == 0) return new ThreadInfo[0];
 190 
 191         Util.checkMonitorAccess();
 192 
 193         ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
 194         if (maxDepth == Integer.MAX_VALUE) {


 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     private boolean verifyThreadAllocatedMemory() {
 353         if (!isThreadAllocatedMemorySupported()) {
 354             throw new UnsupportedOperationException(
 355                 THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED);
 356         }
 357         return isThreadAllocatedMemoryEnabled();
 358     }
 359 
 360     protected long getCurrentThreadAllocatedBytes() {
 361         if (verifyThreadAllocatedMemory()) {
 362             return getThreadAllocatedMemory0(0);
 363         }
 364         return -1;
 365     }
 366 
 367     private boolean verifyThreadAllocatedMemory(long id) {
 368         verifyThreadId(id);
 369         return verifyThreadAllocatedMemory();

 370     }
 371 
 372     protected long getThreadAllocatedBytes(long id) {
 373         boolean verified = verifyThreadAllocatedMemory(id);
 374 
 375         if (verified) {
 376             return getThreadAllocatedMemory0(
 377                 Thread.currentThread().getId() == id ? 0 : id);
 378         }
 379         return -1;
 380     }
 381 
 382     private boolean verifyThreadAllocatedMemory(long[] ids) {
 383         verifyThreadIds(ids);
 384         return verifyThreadAllocatedMemory();
 385     }
 386 
 387     protected long[] getThreadAllocatedBytes(long[] ids) {
 388         boolean verified = verifyThreadAllocatedMemory(ids);
 389 
 390         long[] sizes = new long[ids.length];
 391         if (ids.length == 1) {
 392             sizes[0] = -1;
 393         } else {
 394             java.util.Arrays.fill(sizes, -1);
 395         }
 396 
 397         if (verified) {
 398             if (ids.length == 1) {
 399                 long id = ids[0];
 400                 sizes[0] = getThreadAllocatedMemory0(
 401                     Thread.currentThread().getId() == id ? 0 : id);
 402             } else {
 403                 getThreadAllocatedMemory1(ids, sizes);
 404             }
 405         }
 406         return sizes;
 407     }
 408 
 409     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
 410         if (!isThreadAllocatedMemorySupported()) {
 411             throw new UnsupportedOperationException(
 412                 THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED);
 413         }
 414 
 415         Util.checkControlAccess();
 416         synchronized (this) {
 417             if (allocatedMemoryEnabled != enable) {
 418                 // notify VM of the state change
 419                 setThreadAllocatedMemoryEnabled0(enable);
 420                 allocatedMemoryEnabled = enable;
 421             }
 422         }
 423     }
 424 
 425     @Override
 426     public long[] findMonitorDeadlockedThreads() {
 427         Util.checkMonitorAccess();
 428 
 429         Thread[] threads = findMonitorDeadlockedThreads0();
 430         if (threads == null) {
 431             return null;
 432         }


 527     public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
 528                                        boolean lockedSynchronizers,
 529                                        int maxDepth) {
 530         if (maxDepth < 0) {
 531             throw new IllegalArgumentException(
 532                     "Invalid maxDepth parameter: " + maxDepth);
 533         }
 534         verifyDumpThreads(lockedMonitors, lockedSynchronizers);
 535         return dumpThreads0(null, lockedMonitors, lockedSynchronizers, maxDepth);
 536     }
 537 
 538     // VM support where maxDepth == -1 to request entire stack dump
 539     private static native Thread[] getThreads();
 540     private static native void getThreadInfo1(long[] ids,
 541                                               int maxDepth,
 542                                               ThreadInfo[] result);
 543     private static native long getThreadTotalCpuTime0(long id);
 544     private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
 545     private static native long getThreadUserCpuTime0(long id);
 546     private static native void getThreadUserCpuTime1(long[] ids, long[] result);
 547     private static native long getThreadAllocatedMemory0(long id);
 548     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
 549     private static native void setThreadCpuTimeEnabled0(boolean enable);
 550     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
 551     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
 552     private static native Thread[] findMonitorDeadlockedThreads0();
 553     private static native Thread[] findDeadlockedThreads0();
 554     private static native void resetPeakThreadCount0();
 555     private static native ThreadInfo[] dumpThreads0(long[] ids,
 556                                                     boolean lockedMonitors,
 557                                                     boolean lockedSynchronizers,
 558                                                     int maxDepth);
 559 
 560     // tid == 0 to reset contention times for all threads
 561     private static native void resetContentionTimes0(long tid);
 562 
 563     @Override
 564     public ObjectName getObjectName() {
 565         return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
 566     }
 567 
< prev index next >