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