src/java.management/share/classes/sun/management/ThreadImpl.java
changeset 58223 778fc2dcbdaa
parent 58208 0f3c23c374a4
child 58348 c29e49148be7
equal deleted inserted replaced
58222:ee37c9b2eb61 58223:778fc2dcbdaa
    27 
    27 
    28 import java.lang.management.ManagementFactory;
    28 import java.lang.management.ManagementFactory;
    29 import java.lang.management.ThreadInfo;
    29 import java.lang.management.ThreadInfo;
    30 import java.lang.management.ThreadMXBean;
    30 import java.lang.management.ThreadMXBean;
    31 import javax.management.ObjectName;
    31 import javax.management.ObjectName;
    32 import java.util.Objects;
       
    33 
    32 
    34 /**
    33 /**
    35  * Implementation for java.lang.management.ThreadMXBean as well as providing the
    34  * Implementation for java.lang.management.ThreadMXBean as well as providing the
    36  * supporting method for com.sun.management.ThreadMXBean.
    35  * supporting method for com.sun.management.ThreadMXBean.
    37  * The supporting method for com.sun.management.ThreadMXBean can be moved to
    36  * The supporting method for com.sun.management.ThreadMXBean can be moved to
   111                 "Thread CPU time measurement is not supported");
   110                 "Thread CPU time measurement is not supported");
   112         }
   111         }
   113         return cpuTimeEnabled;
   112         return cpuTimeEnabled;
   114     }
   113     }
   115 
   114 
   116     private void ensureThreadAllocatedMemorySupported() {
   115     protected boolean isThreadAllocatedMemoryEnabled() {
   117         if (!isThreadAllocatedMemorySupported()) {
   116         if (!isThreadAllocatedMemorySupported()) {
   118             throw new UnsupportedOperationException(
   117             throw new UnsupportedOperationException(
   119                 "Thread allocated memory measurement is not supported.");
   118                 "Thread allocated memory measurement is not supported");
   120         }
   119         }
   121     }
       
   122 
       
   123     protected boolean isThreadAllocatedMemoryEnabled() {
       
   124         ensureThreadAllocatedMemorySupported();
       
   125         return allocatedMemoryEnabled;
   120         return allocatedMemoryEnabled;
   126     }
   121     }
   127 
   122 
   128     @Override
   123     @Override
   129     public long[] getAllThreadIds() {
   124     public long[] getAllThreadIds() {
   158     @Override
   153     @Override
   159     public ThreadInfo[] getThreadInfo(long[] ids) {
   154     public ThreadInfo[] getThreadInfo(long[] ids) {
   160         return getThreadInfo(ids, 0);
   155         return getThreadInfo(ids, 0);
   161     }
   156     }
   162 
   157 
   163     private void verifyThreadId(long id) {
       
   164         if (id <= 0) {
       
   165             throw new IllegalArgumentException(
       
   166                 "Invalid thread ID parameter: " + id);
       
   167         }
       
   168     }
       
   169 
       
   170     private void verifyThreadIds(long[] ids) {
   158     private void verifyThreadIds(long[] ids) {
   171         Objects.requireNonNull(ids);
   159         if (ids == null) {
       
   160             throw new NullPointerException("Null ids parameter.");
       
   161         }
   172 
   162 
   173         for (int i = 0; i < ids.length; i++) {
   163         for (int i = 0; i < ids.length; i++) {
   174             verifyThreadId(ids[i]);
   164             if (ids[i] <= 0) {
       
   165                 throw new IllegalArgumentException(
       
   166                     "Invalid thread ID parameter: " + ids[i]);
       
   167             }
   175         }
   168         }
   176     }
   169     }
   177 
   170 
   178     @Override
   171     @Override
   179     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
   172     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
   347                 cpuTimeEnabled = enable;
   340                 cpuTimeEnabled = enable;
   348             }
   341             }
   349         }
   342         }
   350     }
   343     }
   351 
   344 
   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) {
   345     protected long getThreadAllocatedBytes(long id) {
   365         boolean verified = verifyThreadAllocatedMemory(id);
   346         long[] ids = new long[1];
   366 
   347         ids[0] = id;
   367         if (verified) {
   348         final long[] sizes = getThreadAllocatedBytes(ids);
   368             return getThreadAllocatedMemory0(
   349         return sizes[0];
   369                 Thread.currentThread().getId() == id ? 0 : id);
       
   370         }
       
   371         return -1;
       
   372     }
   350     }
   373 
   351 
   374     private boolean verifyThreadAllocatedMemory(long[] ids) {
   352     private boolean verifyThreadAllocatedMemory(long[] ids) {
   375         verifyThreadIds(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 
   376         return isThreadAllocatedMemoryEnabled();
   361         return isThreadAllocatedMemoryEnabled();
   377     }
   362     }
   378 
   363 
   379     protected long[] getThreadAllocatedBytes(long[] ids) {
   364     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);
   365         boolean verified = verifyThreadAllocatedMemory(ids);
   388 
   366 
   389         long[] sizes = new long[ids.length];
   367         long[] sizes = new long[ids.length];
   390         java.util.Arrays.fill(sizes, -1);
   368         java.util.Arrays.fill(sizes, -1);
   391 
   369 
   394         }
   372         }
   395         return sizes;
   373         return sizes;
   396     }
   374     }
   397 
   375 
   398     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
   376     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
   399         ensureThreadAllocatedMemorySupported();
   377         if (!isThreadAllocatedMemorySupported()) {
       
   378             throw new UnsupportedOperationException(
       
   379                 "Thread allocated memory measurement is not supported.");
       
   380         }
   400 
   381 
   401         Util.checkControlAccess();
   382         Util.checkControlAccess();
   402         synchronized (this) {
   383         synchronized (this) {
   403             if (allocatedMemoryEnabled != enable) {
   384             if (allocatedMemoryEnabled != enable) {
   404                 // notify VM of the state change
   385                 // notify VM of the state change
   528                                               ThreadInfo[] result);
   509                                               ThreadInfo[] result);
   529     private static native long getThreadTotalCpuTime0(long id);
   510     private static native long getThreadTotalCpuTime0(long id);
   530     private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
   511     private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
   531     private static native long getThreadUserCpuTime0(long id);
   512     private static native long getThreadUserCpuTime0(long id);
   532     private static native void getThreadUserCpuTime1(long[] ids, long[] result);
   513     private static native void getThreadUserCpuTime1(long[] ids, long[] result);
   533     private static native long getThreadAllocatedMemory0(long id);
       
   534     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
   514     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
   535     private static native void setThreadCpuTimeEnabled0(boolean enable);
   515     private static native void setThreadCpuTimeEnabled0(boolean enable);
   536     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
   516     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
   537     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
   517     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
   538     private static native Thread[] findMonitorDeadlockedThreads0();
   518     private static native Thread[] findMonitorDeadlockedThreads0();