< prev index next >

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

Print this page

        

@@ -38,10 +38,13 @@
  */
 
 public class ThreadImpl implements ThreadMXBean {
     private final VMManagement jvm;
 
+    private static final String THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED =
+        "Thread allocated memory measurement is not supported.";
+
     // default for thread contention monitoring is disabled.
     private boolean contentionMonitoringEnabled = false;
     private boolean cpuTimeEnabled;
     private boolean allocatedMemoryEnabled;
 

@@ -113,11 +116,11 @@
     }
 
     protected boolean isThreadAllocatedMemoryEnabled() {
         if (!isThreadAllocatedMemorySupported()) {
             throw new UnsupportedOperationException(
-                "Thread allocated memory measurement is not supported");
+                THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED);
         }
         return allocatedMemoryEnabled;
     }
 
     @Override

@@ -153,20 +156,24 @@
     @Override
     public ThreadInfo[] getThreadInfo(long[] ids) {
         return getThreadInfo(ids, 0);
     }
 
+    private void verifyThreadId(long id) {
+        if (id <= 0) {
+            throw new IllegalArgumentException(
+                "Invalid thread ID parameter: " + id);
+        }
+    }
+
     private void verifyThreadIds(long[] ids) {
         if (ids == null) {
             throw new NullPointerException("Null ids parameter.");
         }
 
         for (int i = 0; i < ids.length; i++) {
-            if (ids[i] <= 0) {
-                throw new IllegalArgumentException(
-                    "Invalid thread ID parameter: " + ids[i]);
-            }
+            verifyThreadId(ids[i]);
         }
     }
 
     @Override
     public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {

@@ -340,45 +347,71 @@
                 cpuTimeEnabled = enable;
             }
         }
     }
 
-    protected long getThreadAllocatedBytes(long id) {
-        long[] ids = new long[1];
-        ids[0] = id;
-        final long[] sizes = getThreadAllocatedBytes(ids);
-        return sizes[0];
+    private boolean verifyThreadAllocatedMemory() {
+        if (!isThreadAllocatedMemorySupported()) {
+            throw new UnsupportedOperationException(
+                THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED);
+        }
+        return isThreadAllocatedMemoryEnabled();
     }
 
-    private boolean verifyThreadAllocatedMemory(long[] ids) {
-        verifyThreadIds(ids);
+    protected long getCurrentThreadAllocatedBytes() {
+        if (verifyThreadAllocatedMemory()) {
+            return getThreadAllocatedMemory0(0);
+        }
+        return -1;
+    }
 
-        // check if Thread allocated memory measurement is supported.
-        if (!isThreadAllocatedMemorySupported()) {
-            throw new UnsupportedOperationException(
-                "Thread allocated memory measurement is not supported.");
+    private boolean verifyThreadAllocatedMemory(long id) {
+        verifyThreadId(id);
+        return verifyThreadAllocatedMemory();
         }
 
-        return isThreadAllocatedMemoryEnabled();
+    protected long getThreadAllocatedBytes(long id) {
+        boolean verified = verifyThreadAllocatedMemory(id);
+
+        if (verified) {
+            return getThreadAllocatedMemory0(
+                Thread.currentThread().getId() == id ? 0 : id);
+        }
+        return -1;
+    }
+
+    private boolean verifyThreadAllocatedMemory(long[] ids) {
+        verifyThreadIds(ids);
+        return verifyThreadAllocatedMemory();
     }
 
     protected long[] getThreadAllocatedBytes(long[] ids) {
         boolean verified = verifyThreadAllocatedMemory(ids);
 
         long[] sizes = new long[ids.length];
+        if (ids.length == 1) {
+            sizes[0] = -1;
+        } else {
         java.util.Arrays.fill(sizes, -1);
+        }
 
         if (verified) {
+            if (ids.length == 1) {
+                long id = ids[0];
+                sizes[0] = getThreadAllocatedMemory0(
+                    Thread.currentThread().getId() == id ? 0 : id);
+            } else {
             getThreadAllocatedMemory1(ids, sizes);
         }
+        }
         return sizes;
     }
 
     protected void setThreadAllocatedMemoryEnabled(boolean enable) {
         if (!isThreadAllocatedMemorySupported()) {
             throw new UnsupportedOperationException(
-                "Thread allocated memory measurement is not supported.");
+                THREAD_ALLOCATED_MEMORY_NOT_SUPPORTED);
         }
 
         Util.checkControlAccess();
         synchronized (this) {
             if (allocatedMemoryEnabled != enable) {

@@ -509,10 +542,11 @@
                                               ThreadInfo[] result);
     private static native long getThreadTotalCpuTime0(long id);
     private static native void getThreadTotalCpuTime1(long[] ids, long[] result);
     private static native long getThreadUserCpuTime0(long id);
     private static native void getThreadUserCpuTime1(long[] ids, long[] result);
+    private static native long getThreadAllocatedMemory0(long id);
     private static native void getThreadAllocatedMemory1(long[] ids, long[] result);
     private static native void setThreadCpuTimeEnabled0(boolean enable);
     private static native void setThreadAllocatedMemoryEnabled0(boolean enable);
     private static native void setThreadContentionMonitoringEnabled0(boolean enable);
     private static native Thread[] findMonitorDeadlockedThreads0();
< prev index next >