jdk/src/java.management/share/classes/sun/management/ThreadImpl.java
changeset 30355 e37c7eba132f
parent 30348 9a9e6c13ab1d
equal deleted inserted replaced
30354:ca83b4cae363 30355:e37c7eba132f
    24  */
    24  */
    25 
    25 
    26 package sun.management;
    26 package sun.management;
    27 
    27 
    28 import java.lang.management.ManagementFactory;
    28 import java.lang.management.ManagementFactory;
    29 
       
    30 import java.lang.management.ThreadInfo;
    29 import java.lang.management.ThreadInfo;
    31 
    30 import java.lang.management.ThreadMXBean;
    32 import javax.management.ObjectName;
    31 import javax.management.ObjectName;
    33 
    32 
    34 /**
    33 /**
    35  * Implementation class for the thread subsystem.
    34  * Implementation for java.lang.management.ThreadMXBean as well as providing the
    36  * Standard and committed hotspot-specific metrics if any.
    35  * supporting method for com.sun.management.ThreadMXBean.
    37  *
    36  * The supporting method for com.sun.management.ThreadMXBean can be moved to
    38  * ManagementFactory.getThreadMXBean() returns an instance
    37  * jdk.management in the future.
    39  * of this class.
       
    40  */
    38  */
    41 class ThreadImpl implements com.sun.management.ThreadMXBean {
    39 
    42 
    40 public class ThreadImpl implements ThreadMXBean {
    43     private final VMManagement jvm;
    41     private final VMManagement jvm;
    44 
    42 
    45     // default for thread contention monitoring is disabled.
    43     // default for thread contention monitoring is disabled.
    46     private boolean contentionMonitoringEnabled = false;
    44     private boolean contentionMonitoringEnabled = false;
    47     private boolean cpuTimeEnabled;
    45     private boolean cpuTimeEnabled;
    48     private boolean allocatedMemoryEnabled;
    46     private boolean allocatedMemoryEnabled;
    49 
    47 
    50     /**
    48     /**
    51      * Constructor of ThreadImpl class.
    49      * Constructor of ThreadImpl class.
    52      */
    50      */
    53     ThreadImpl(VMManagement vm) {
    51     protected ThreadImpl(VMManagement vm) {
    54         this.jvm = vm;
    52         this.jvm = vm;
    55         this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
    53         this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
    56         this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
    54         this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
    57     }
    55     }
    58 
    56 
       
    57     @Override
    59     public int getThreadCount() {
    58     public int getThreadCount() {
    60         return jvm.getLiveThreadCount();
    59         return jvm.getLiveThreadCount();
    61     }
    60     }
    62 
    61 
       
    62     @Override
    63     public int getPeakThreadCount() {
    63     public int getPeakThreadCount() {
    64         return jvm.getPeakThreadCount();
    64         return jvm.getPeakThreadCount();
    65     }
    65     }
    66 
    66 
       
    67     @Override
    67     public long getTotalStartedThreadCount() {
    68     public long getTotalStartedThreadCount() {
    68         return jvm.getTotalThreadCount();
    69         return jvm.getTotalThreadCount();
    69     }
    70     }
    70 
    71 
       
    72     @Override
    71     public int getDaemonThreadCount() {
    73     public int getDaemonThreadCount() {
    72         return jvm.getDaemonThreadCount();
    74         return jvm.getDaemonThreadCount();
    73     }
    75     }
    74 
    76 
       
    77     @Override
    75     public boolean isThreadContentionMonitoringSupported() {
    78     public boolean isThreadContentionMonitoringSupported() {
    76         return jvm.isThreadContentionMonitoringSupported();
    79         return jvm.isThreadContentionMonitoringSupported();
    77     }
    80     }
    78 
    81 
       
    82     @Override
    79     public synchronized boolean isThreadContentionMonitoringEnabled() {
    83     public synchronized boolean isThreadContentionMonitoringEnabled() {
    80        if (!isThreadContentionMonitoringSupported()) {
    84        if (!isThreadContentionMonitoringSupported()) {
    81             throw new UnsupportedOperationException(
    85             throw new UnsupportedOperationException(
    82                 "Thread contention monitoring is not supported.");
    86                 "Thread contention monitoring is not supported.");
    83         }
    87         }
    84         return contentionMonitoringEnabled;
    88         return contentionMonitoringEnabled;
    85     }
    89     }
    86 
    90 
       
    91     @Override
    87     public boolean isThreadCpuTimeSupported() {
    92     public boolean isThreadCpuTimeSupported() {
    88         return jvm.isOtherThreadCpuTimeSupported();
    93         return jvm.isOtherThreadCpuTimeSupported();
    89     }
    94     }
    90 
    95 
       
    96     @Override
    91     public boolean isCurrentThreadCpuTimeSupported() {
    97     public boolean isCurrentThreadCpuTimeSupported() {
    92         return jvm.isCurrentThreadCpuTimeSupported();
    98         return jvm.isCurrentThreadCpuTimeSupported();
    93     }
    99     }
    94 
   100 
    95     public boolean isThreadAllocatedMemorySupported() {
   101     protected boolean isThreadAllocatedMemorySupported() {
    96         return jvm.isThreadAllocatedMemorySupported();
   102         return jvm.isThreadAllocatedMemorySupported();
    97     }
   103     }
    98 
   104 
       
   105     @Override
    99     public boolean isThreadCpuTimeEnabled() {
   106     public boolean isThreadCpuTimeEnabled() {
   100         if (!isThreadCpuTimeSupported() &&
   107         if (!isThreadCpuTimeSupported() &&
   101             !isCurrentThreadCpuTimeSupported()) {
   108             !isCurrentThreadCpuTimeSupported()) {
   102             throw new UnsupportedOperationException(
   109             throw new UnsupportedOperationException(
   103                 "Thread CPU time measurement is not supported");
   110                 "Thread CPU time measurement is not supported");
   104         }
   111         }
   105         return cpuTimeEnabled;
   112         return cpuTimeEnabled;
   106     }
   113     }
   107 
   114 
   108     public boolean isThreadAllocatedMemoryEnabled() {
   115     protected boolean isThreadAllocatedMemoryEnabled() {
   109         if (!isThreadAllocatedMemorySupported()) {
   116         if (!isThreadAllocatedMemorySupported()) {
   110             throw new UnsupportedOperationException(
   117             throw new UnsupportedOperationException(
   111                 "Thread allocated memory measurement is not supported");
   118                 "Thread allocated memory measurement is not supported");
   112         }
   119         }
   113         return allocatedMemoryEnabled;
   120         return allocatedMemoryEnabled;
   114     }
   121     }
   115 
   122 
       
   123     @Override
   116     public long[] getAllThreadIds() {
   124     public long[] getAllThreadIds() {
   117         Util.checkMonitorAccess();
   125         Util.checkMonitorAccess();
   118 
   126 
   119         Thread[] threads = getThreads();
   127         Thread[] threads = getThreads();
   120         int length = threads.length;
   128         int length = threads.length;
   124             ids[i] = t.getId();
   132             ids[i] = t.getId();
   125         }
   133         }
   126         return ids;
   134         return ids;
   127     }
   135     }
   128 
   136 
       
   137     @Override
   129     public ThreadInfo getThreadInfo(long id) {
   138     public ThreadInfo getThreadInfo(long id) {
   130         long[] ids = new long[1];
   139         long[] ids = new long[1];
   131         ids[0] = id;
   140         ids[0] = id;
   132         final ThreadInfo[] infos = getThreadInfo(ids, 0);
   141         final ThreadInfo[] infos = getThreadInfo(ids, 0);
   133         return infos[0];
   142         return infos[0];
   134     }
   143     }
   135 
   144 
       
   145     @Override
   136     public ThreadInfo getThreadInfo(long id, int maxDepth) {
   146     public ThreadInfo getThreadInfo(long id, int maxDepth) {
   137         long[] ids = new long[1];
   147         long[] ids = new long[1];
   138         ids[0] = id;
   148         ids[0] = id;
   139         final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
   149         final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
   140         return infos[0];
   150         return infos[0];
   141     }
   151     }
   142 
   152 
       
   153     @Override
   143     public ThreadInfo[] getThreadInfo(long[] ids) {
   154     public ThreadInfo[] getThreadInfo(long[] ids) {
   144         return getThreadInfo(ids, 0);
   155         return getThreadInfo(ids, 0);
   145     }
   156     }
   146 
   157 
   147     private void verifyThreadIds(long[] ids) {
   158     private void verifyThreadIds(long[] ids) {
   155                     "Invalid thread ID parameter: " + ids[i]);
   166                     "Invalid thread ID parameter: " + ids[i]);
   156             }
   167             }
   157         }
   168         }
   158     }
   169     }
   159 
   170 
       
   171     @Override
   160     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
   172     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
   161         verifyThreadIds(ids);
   173         verifyThreadIds(ids);
   162 
   174 
   163         if (maxDepth < 0) {
   175         if (maxDepth < 0) {
   164             throw new IllegalArgumentException(
   176             throw new IllegalArgumentException(
   178             getThreadInfo1(ids, maxDepth, infos);
   190             getThreadInfo1(ids, maxDepth, infos);
   179         }
   191         }
   180         return infos;
   192         return infos;
   181     }
   193     }
   182 
   194 
       
   195     @Override
   183     public void setThreadContentionMonitoringEnabled(boolean enable) {
   196     public void setThreadContentionMonitoringEnabled(boolean enable) {
   184         if (!isThreadContentionMonitoringSupported()) {
   197         if (!isThreadContentionMonitoringSupported()) {
   185             throw new UnsupportedOperationException(
   198             throw new UnsupportedOperationException(
   186                 "Thread contention monitoring is not supported");
   199                 "Thread contention monitoring is not supported");
   187         }
   200         }
   211                 "Current thread CPU time measurement is not supported.");
   224                 "Current thread CPU time measurement is not supported.");
   212         }
   225         }
   213         return isThreadCpuTimeEnabled();
   226         return isThreadCpuTimeEnabled();
   214     }
   227     }
   215 
   228 
       
   229     @Override
   216     public long getCurrentThreadCpuTime() {
   230     public long getCurrentThreadCpuTime() {
   217         if (verifyCurrentThreadCpuTime()) {
   231         if (verifyCurrentThreadCpuTime()) {
   218             return getThreadTotalCpuTime0(0);
   232             return getThreadTotalCpuTime0(0);
   219         }
   233         }
   220         return -1;
   234         return -1;
   221     }
   235     }
   222 
   236 
       
   237     @Override
   223     public long getThreadCpuTime(long id) {
   238     public long getThreadCpuTime(long id) {
   224         long[] ids = new long[1];
   239         long[] ids = new long[1];
   225         ids[0] = id;
   240         ids[0] = id;
   226         final long[] times = getThreadCpuTime(ids);
   241         final long[] times = getThreadCpuTime(ids);
   227         return times[0];
   242         return times[0];
   249         }
   264         }
   250 
   265 
   251         return isThreadCpuTimeEnabled();
   266         return isThreadCpuTimeEnabled();
   252     }
   267     }
   253 
   268 
   254     public long[] getThreadCpuTime(long[] ids) {
   269     protected long[] getThreadCpuTime(long[] ids) {
   255         boolean verified = verifyThreadCpuTime(ids);
   270         boolean verified = verifyThreadCpuTime(ids);
   256 
   271 
   257         int length = ids.length;
   272         int length = ids.length;
   258         long[] times = new long[length];
   273         long[] times = new long[length];
   259         java.util.Arrays.fill(times, -1);
   274         java.util.Arrays.fill(times, -1);
   270             }
   285             }
   271         }
   286         }
   272         return times;
   287         return times;
   273     }
   288     }
   274 
   289 
       
   290     @Override
   275     public long getCurrentThreadUserTime() {
   291     public long getCurrentThreadUserTime() {
   276         if (verifyCurrentThreadCpuTime()) {
   292         if (verifyCurrentThreadCpuTime()) {
   277             return getThreadUserCpuTime0(0);
   293             return getThreadUserCpuTime0(0);
   278         }
   294         }
   279         return -1;
   295         return -1;
   280     }
   296     }
   281 
   297 
       
   298     @Override
   282     public long getThreadUserTime(long id) {
   299     public long getThreadUserTime(long id) {
   283         long[] ids = new long[1];
   300         long[] ids = new long[1];
   284         ids[0] = id;
   301         ids[0] = id;
   285         final long[] times = getThreadUserTime(ids);
   302         final long[] times = getThreadUserTime(ids);
   286         return times[0];
   303         return times[0];
   287     }
   304     }
   288 
   305 
   289     public long[] getThreadUserTime(long[] ids) {
   306     protected long[] getThreadUserTime(long[] ids) {
   290         boolean verified = verifyThreadCpuTime(ids);
   307         boolean verified = verifyThreadCpuTime(ids);
   291 
   308 
   292         int length = ids.length;
   309         int length = ids.length;
   293         long[] times = new long[length];
   310         long[] times = new long[length];
   294         java.util.Arrays.fill(times, -1);
   311         java.util.Arrays.fill(times, -1);
   305             }
   322             }
   306         }
   323         }
   307         return times;
   324         return times;
   308     }
   325     }
   309 
   326 
       
   327     @Override
   310     public void setThreadCpuTimeEnabled(boolean enable) {
   328     public void setThreadCpuTimeEnabled(boolean enable) {
   311         if (!isThreadCpuTimeSupported() &&
   329         if (!isThreadCpuTimeSupported() &&
   312             !isCurrentThreadCpuTimeSupported()) {
   330             !isCurrentThreadCpuTimeSupported()) {
   313             throw new UnsupportedOperationException(
   331             throw new UnsupportedOperationException(
   314                 "Thread CPU time measurement is not supported");
   332                 "Thread CPU time measurement is not supported");
   322                 cpuTimeEnabled = enable;
   340                 cpuTimeEnabled = enable;
   323             }
   341             }
   324         }
   342         }
   325     }
   343     }
   326 
   344 
   327     public long getThreadAllocatedBytes(long id) {
   345     protected long getThreadAllocatedBytes(long id) {
   328         long[] ids = new long[1];
   346         long[] ids = new long[1];
   329         ids[0] = id;
   347         ids[0] = id;
   330         final long[] sizes = getThreadAllocatedBytes(ids);
   348         final long[] sizes = getThreadAllocatedBytes(ids);
   331         return sizes[0];
   349         return sizes[0];
   332     }
   350     }
   341         }
   359         }
   342 
   360 
   343         return isThreadAllocatedMemoryEnabled();
   361         return isThreadAllocatedMemoryEnabled();
   344     }
   362     }
   345 
   363 
   346     public long[] getThreadAllocatedBytes(long[] ids) {
   364     protected long[] getThreadAllocatedBytes(long[] ids) {
   347         boolean verified = verifyThreadAllocatedMemory(ids);
   365         boolean verified = verifyThreadAllocatedMemory(ids);
   348 
   366 
   349         long[] sizes = new long[ids.length];
   367         long[] sizes = new long[ids.length];
   350         java.util.Arrays.fill(sizes, -1);
   368         java.util.Arrays.fill(sizes, -1);
   351 
   369 
   353             getThreadAllocatedMemory1(ids, sizes);
   371             getThreadAllocatedMemory1(ids, sizes);
   354         }
   372         }
   355         return sizes;
   373         return sizes;
   356     }
   374     }
   357 
   375 
   358     public void setThreadAllocatedMemoryEnabled(boolean enable) {
   376     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
   359         if (!isThreadAllocatedMemorySupported()) {
   377         if (!isThreadAllocatedMemorySupported()) {
   360             throw new UnsupportedOperationException(
   378             throw new UnsupportedOperationException(
   361                 "Thread allocated memory measurement is not supported.");
   379                 "Thread allocated memory measurement is not supported.");
   362         }
   380         }
   363 
   381 
   369                 allocatedMemoryEnabled = enable;
   387                 allocatedMemoryEnabled = enable;
   370             }
   388             }
   371         }
   389         }
   372     }
   390     }
   373 
   391 
       
   392     @Override
   374     public long[] findMonitorDeadlockedThreads() {
   393     public long[] findMonitorDeadlockedThreads() {
   375         Util.checkMonitorAccess();
   394         Util.checkMonitorAccess();
   376 
   395 
   377         Thread[] threads = findMonitorDeadlockedThreads0();
   396         Thread[] threads = findMonitorDeadlockedThreads0();
   378         if (threads == null) {
   397         if (threads == null) {
   385             ids[i] = t.getId();
   404             ids[i] = t.getId();
   386         }
   405         }
   387         return ids;
   406         return ids;
   388     }
   407     }
   389 
   408 
       
   409     @Override
   390     public long[] findDeadlockedThreads() {
   410     public long[] findDeadlockedThreads() {
   391         if (!isSynchronizerUsageSupported()) {
   411         if (!isSynchronizerUsageSupported()) {
   392             throw new UnsupportedOperationException(
   412             throw new UnsupportedOperationException(
   393                 "Monitoring of Synchronizer Usage is not supported.");
   413                 "Monitoring of Synchronizer Usage is not supported.");
   394         }
   414         }
   406             ids[i] = t.getId();
   426             ids[i] = t.getId();
   407         }
   427         }
   408         return ids;
   428         return ids;
   409     }
   429     }
   410 
   430 
       
   431     @Override
   411     public void resetPeakThreadCount() {
   432     public void resetPeakThreadCount() {
   412         Util.checkControlAccess();
   433         Util.checkControlAccess();
   413         resetPeakThreadCount0();
   434         resetPeakThreadCount0();
   414     }
   435     }
   415 
   436 
       
   437     @Override
   416     public boolean isObjectMonitorUsageSupported() {
   438     public boolean isObjectMonitorUsageSupported() {
   417         return jvm.isObjectMonitorUsageSupported();
   439         return jvm.isObjectMonitorUsageSupported();
   418     }
   440     }
   419 
   441 
       
   442     @Override
   420     public boolean isSynchronizerUsageSupported() {
   443     public boolean isSynchronizerUsageSupported() {
   421         return jvm.isSynchronizerUsageSupported();
   444         return jvm.isSynchronizerUsageSupported();
   422     }
   445     }
   423 
   446 
   424     private void verifyDumpThreads(boolean lockedMonitors,
   447     private void verifyDumpThreads(boolean lockedMonitors,
   434         }
   457         }
   435 
   458 
   436         Util.checkMonitorAccess();
   459         Util.checkMonitorAccess();
   437     }
   460     }
   438 
   461 
       
   462     @Override
   439     public ThreadInfo[] getThreadInfo(long[] ids,
   463     public ThreadInfo[] getThreadInfo(long[] ids,
   440                                       boolean lockedMonitors,
   464                                       boolean lockedMonitors,
   441                                       boolean lockedSynchronizers) {
   465                                       boolean lockedSynchronizers) {
   442         verifyThreadIds(ids);
   466         verifyThreadIds(ids);
   443         // ids has been verified to be non-null
   467         // ids has been verified to be non-null
   446 
   470 
   447         verifyDumpThreads(lockedMonitors, lockedSynchronizers);
   471         verifyDumpThreads(lockedMonitors, lockedSynchronizers);
   448         return dumpThreads0(ids, lockedMonitors, lockedSynchronizers);
   472         return dumpThreads0(ids, lockedMonitors, lockedSynchronizers);
   449     }
   473     }
   450 
   474 
       
   475     @Override
   451     public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
   476     public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
   452                                        boolean lockedSynchronizers) {
   477                                        boolean lockedSynchronizers) {
   453         verifyDumpThreads(lockedMonitors, lockedSynchronizers);
   478         verifyDumpThreads(lockedMonitors, lockedSynchronizers);
   454         return dumpThreads0(null, lockedMonitors, lockedSynchronizers);
   479         return dumpThreads0(null, lockedMonitors, lockedSynchronizers);
   455     }
   480     }
   475                                                     boolean lockedSynchronizers);
   500                                                     boolean lockedSynchronizers);
   476 
   501 
   477     // tid == 0 to reset contention times for all threads
   502     // tid == 0 to reset contention times for all threads
   478     private static native void resetContentionTimes0(long tid);
   503     private static native void resetContentionTimes0(long tid);
   479 
   504 
       
   505     @Override
   480     public ObjectName getObjectName() {
   506     public ObjectName getObjectName() {
   481         return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
   507         return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
   482     }
   508     }
   483 
   509 
   484 }
   510 }