jdk/src/share/classes/sun/management/ThreadImpl.java
changeset 8001 192adf3627b7
parent 5506 202f599c92aa
child 8002 5d022b92fbef
--- a/jdk/src/share/classes/sun/management/ThreadImpl.java	Thu Jan 20 15:23:57 2011 +0000
+++ b/jdk/src/share/classes/sun/management/ThreadImpl.java	Thu Jan 20 19:34:40 2011 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@
 
 package sun.management;
 
-import java.lang.management.ThreadMXBean;
 import java.lang.management.ManagementFactory;
 
 import java.lang.management.ThreadInfo;
@@ -39,13 +38,14 @@
  * ManagementFactory.getThreadMXBean() returns an instance
  * of this class.
  */
-class ThreadImpl implements ThreadMXBean {
+class ThreadImpl implements com.sun.management.ThreadMXBean {
 
     private final VMManagement jvm;
 
     // default for thread contention monitoring is disabled.
     private boolean contentionMonitoringEnabled = false;
     private boolean cpuTimeEnabled;
+    private boolean allocatedMemoryEnabled;
 
     /**
      * Constructor of ThreadImpl class.
@@ -53,6 +53,7 @@
     ThreadImpl(VMManagement vm) {
         this.jvm = vm;
         this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled();
+        this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled();
     }
 
     public int getThreadCount() {
@@ -91,6 +92,10 @@
         return jvm.isCurrentThreadCpuTimeSupported();
     }
 
+    public boolean isThreadAllocatedMemorySupported() {
+        return jvm.isThreadAllocatedMemorySupported();
+    }
+
     public boolean isThreadCpuTimeEnabled() {
         if (!isThreadCpuTimeSupported() &&
             !isCurrentThreadCpuTimeSupported()) {
@@ -100,6 +105,14 @@
         return cpuTimeEnabled;
     }
 
+    public boolean isThreadAllocatedMemoryEnabled() {
+        if (!isThreadAllocatedMemorySupported()) {
+            throw new UnsupportedOperationException(
+                "Thread allocated memory measurement is not supported");
+        }
+        return allocatedMemoryEnabled;
+    }
+
     public long[] getAllThreadIds() {
         Util.checkMonitorAccess();
 
@@ -114,11 +127,6 @@
     }
 
     public ThreadInfo getThreadInfo(long id) {
-        if (id <= 0) {
-            throw new IllegalArgumentException(
-                "Invalid thread ID parameter: " + id);
-        }
-
         long[] ids = new long[1];
         ids[0] = id;
         final ThreadInfo[] infos = getThreadInfo(ids, 0);
@@ -126,15 +134,6 @@
     }
 
     public ThreadInfo getThreadInfo(long id, int maxDepth) {
-        if (id <= 0) {
-            throw new IllegalArgumentException(
-                "Invalid thread ID parameter: " + id);
-        }
-        if (maxDepth < 0) {
-            throw new IllegalArgumentException(
-                "Invalid maxDepth parameter: " + maxDepth);
-        }
-
         long[] ids = new long[1];
         ids[0] = id;
         final ThreadInfo[] infos = getThreadInfo(ids, maxDepth);
@@ -145,11 +144,22 @@
         return getThreadInfo(ids, 0);
     }
 
-    public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
+    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]);
+            }
+        }
+    }
+
+    public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
+        verifyThreadIds(ids);
+
         if (maxDepth < 0) {
             throw new IllegalArgumentException(
                 "Invalid maxDepth parameter: " + maxDepth);
@@ -157,17 +167,15 @@
 
         Util.checkMonitorAccess();
 
-        ThreadInfo[] infos = new ThreadInfo[ids.length];
+        ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
         if (maxDepth == Integer.MAX_VALUE) {
-            getThreadInfo0(ids, -1, infos);
+            getThreadInfo1(ids, -1, infos);
         } else {
-            getThreadInfo0(ids, maxDepth, infos);
+            getThreadInfo1(ids, maxDepth, infos);
         }
         return infos;
     }
 
-
-
     public void setThreadContentionMonitoringEnabled(boolean enable) {
         if (!isThreadContentionMonitoringSupported()) {
             throw new UnsupportedOperationException(
@@ -192,69 +200,32 @@
         }
     }
 
-    public long getCurrentThreadCpuTime() {
+    private boolean verifyCurrentThreadCpuTime() {
         // check if Thread CPU time measurement is supported.
         if (!isCurrentThreadCpuTimeSupported()) {
             throw new UnsupportedOperationException(
                 "Current thread CPU time measurement is not supported.");
         }
+        return isThreadCpuTimeEnabled();
+    }
 
-        if (!isThreadCpuTimeEnabled()) {
-            return -1;
+    public long getCurrentThreadCpuTime() {
+        if (verifyCurrentThreadCpuTime()) {
+            return getThreadTotalCpuTime0(0);
         }
-
-        return getThreadTotalCpuTime0(0);
+        return -1;
     }
 
     public long getThreadCpuTime(long id) {
-        // check if Thread CPU time measurement is supported.
-        if (!isThreadCpuTimeSupported() &&
-            !isCurrentThreadCpuTimeSupported()) {
-            throw new UnsupportedOperationException(
-                "Thread CPU Time Measurement is not supported.");
-        }
-
-        if (!isThreadCpuTimeSupported()) {
-            // support current thread only
-            if (id != Thread.currentThread().getId()) {
-                throw new UnsupportedOperationException(
-                    "Thread CPU Time Measurement is only supported" +
-                    " for the current thread.");
-            }
-        }
-
-        if (id <= 0) {
-            throw new IllegalArgumentException(
-                "Invalid thread ID parameter: " + id);
-        }
-
-        if (!isThreadCpuTimeEnabled()) {
-            return -1;
-        }
-
-        if (id == Thread.currentThread().getId()) {
-            // current thread
-            return getThreadTotalCpuTime0(0);
-        } else {
-            return getThreadTotalCpuTime0(id);
-        }
+        long[] ids = new long[1];
+        ids[0] = id;
+        final long[] times = getThreadCpuTime(ids);
+        return times[0];
     }
 
-    public long getCurrentThreadUserTime() {
-        // check if Thread CPU time measurement is supported.
-        if (!isCurrentThreadCpuTimeSupported()) {
-            throw new UnsupportedOperationException(
-                "Current thread CPU time measurement is not supported.");
-        }
+    private boolean verifyThreadCpuTime(long[] ids) {
+        verifyThreadIds(ids);
 
-        if (!isThreadCpuTimeEnabled()) {
-            return -1;
-        }
-
-        return getThreadUserCpuTime0(0);
-    }
-
-    public long getThreadUserTime(long id) {
         // check if Thread CPU time measurement is supported.
         if (!isThreadCpuTimeSupported() &&
             !isCurrentThreadCpuTimeSupported()) {
@@ -264,30 +235,71 @@
 
         if (!isThreadCpuTimeSupported()) {
             // support current thread only
-            if (id != Thread.currentThread().getId()) {
-                throw new UnsupportedOperationException(
-                    "Thread CPU time measurement is only supported" +
-                    " for the current thread.");
+            for (int i = 0; i < ids.length; i++) {
+                if (ids[i] != Thread.currentThread().getId()) {
+                    throw new UnsupportedOperationException(
+                        "Thread CPU time measurement is only supported" +
+                        " for the current thread.");
+                }
             }
         }
 
-        if (id <= 0) {
-            throw new IllegalArgumentException(
-                "Invalid thread ID parameter: " + id);
-        }
+        return isThreadCpuTimeEnabled();
+    }
+
+    public long[] getThreadCpuTime(long[] ids) {
+        boolean verified = verifyThreadCpuTime(ids);
+
+        int length = ids.length;
+        long[] times = java.util.Arrays.fill(new long[length], -1);
 
-        if (!isThreadCpuTimeEnabled()) {
-            return -1;
+        if (verified) {
+            if (length == 1) {
+                long id = ids[0];
+                if (id == Thread.currentThread().getId()) {
+                    id = 0;
+                }
+                getThreadTotalCpuTime0(id);
+            } else {
+                getThreadTotalCpuTime1(ids, times);
+            }
         }
-
-        if (id == Thread.currentThread().getId()) {
-            // current thread
-           return getThreadUserCpuTime0(0);
-        } else {
-           return getThreadUserCpuTime0(id);
-        }
+        return times;
     }
 
+    public long getCurrentThreadUserTime() {
+        if (verifyCurrentThreadCpuTime()) {
+            return getThreadUserCpuTime0(0);
+        }
+        return -1;
+    }
+
+    public long getThreadUserTime(long id) {
+        long[] ids = new long[1];
+        ids[0] = id;
+        final long[] times = getThreadUserTime(ids);
+        return times[0];
+    }
+
+    public long[] getThreadUserTime(long[] ids) {
+        boolean verified = verifyThreadCpuTime(ids);
+
+        int length = ids.length;
+        long[] times = java.util.Arrays.fill(new long[length], -1);
+
+        if (verified) {
+            if (length == 1) {
+                long id = ids[0];
+                if (id == Thread.currentThread().getId()) {
+                    id = 0;
+                }
+                times[0] = getThreadUserCpuTime0(id);
+            } else {
+                getThreadUserCpuTime1(ids, times);
+            }
+        }
+        return times;
+    }
 
     public void setThreadCpuTimeEnabled(boolean enable) {
         if (!isThreadCpuTimeSupported() &&
@@ -299,13 +311,59 @@
         Util.checkControlAccess();
         synchronized (this) {
             if (cpuTimeEnabled != enable) {
-                // update VM of the state change
+                // notify VM of the state change
                 setThreadCpuTimeEnabled0(enable);
                 cpuTimeEnabled = enable;
             }
         }
     }
 
+    public long getThreadAllocatedBytes(long id) {
+        long[] ids = new long[1];
+        ids[0] = id;
+        final long[] sizes = getThreadAllocatedBytes(ids);
+        return sizes[0];
+    }
+
+    private boolean verifyThreadAllocatedMemory(long[] ids) {
+        verifyThreadIds(ids);
+
+        // check if Thread allocated memory measurement is supported.
+        if (!isThreadAllocatedMemorySupported()) {
+            throw new UnsupportedOperationException(
+                "Thread allocated memory measurement is not supported.");
+        }
+
+        return isThreadAllocatedMemoryEnabled();
+    }
+
+    public long[] getThreadAllocatedBytes(long[] ids) {
+        boolean verified = verifyThreadAllocatedMemory(ids);
+
+        long[] times = java.util.Arrays.fill(new long[length], -1);
+
+        if (verified) {
+            getThreadAllocatedMemory1(ids, sizes);
+        }
+        return sizes;
+    }
+
+    public void setThreadAllocatedMemoryEnabled(boolean enable) {
+        if (!isThreadAllocatedMemorySupported()) {
+            throw new UnsupportedOperationException(
+                "Thread allocated memory measurement is not supported.");
+        }
+
+        Util.checkControlAccess();
+        synchronized (this) {
+            if (allocatedMemoryEnabled != enable) {
+                // notify VM of the state change
+                setThreadAllocatedMemoryEnabled0(enable);
+                allocatedMemoryEnabled = enable;
+            }
+        }
+    }
+
     public long[] findMonitorDeadlockedThreads() {
         Util.checkMonitorAccess();
 
@@ -356,49 +414,47 @@
         return jvm.isSynchronizerUsageSupported();
     }
 
-    public ThreadInfo[] getThreadInfo(long[] ids,
-                                      boolean lockedMonitors,
-                                      boolean lockedSynchronizers) {
-        if (ids == null) {
-            throw new NullPointerException("Null ids parameter.");
-        }
-
+    private void verifyDumpThreads(boolean lockedMonitors,
+                                   boolean lockedSynchronizers) {
         if (lockedMonitors && !isObjectMonitorUsageSupported()) {
             throw new UnsupportedOperationException(
                 "Monitoring of Object Monitor Usage is not supported.");
         }
+
         if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
             throw new UnsupportedOperationException(
                 "Monitoring of Synchronizer Usage is not supported.");
         }
 
         Util.checkMonitorAccess();
+    }
+
+    public ThreadInfo[] getThreadInfo(long[] ids,
+                                      boolean lockedMonitors,
+                                      boolean lockedSynchronizers) {
+        verifyThreadIds(ids);
+        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
         return dumpThreads0(ids, lockedMonitors, lockedSynchronizers);
     }
 
-
-    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers) {
-        if (lockedMonitors && !isObjectMonitorUsageSupported()) {
-            throw new UnsupportedOperationException(
-                "Monitoring of Object Monitor Usage is not supported.");
-        }
-        if (lockedSynchronizers && !isSynchronizerUsageSupported()) {
-            throw new UnsupportedOperationException(
-                "Monitoring of Synchronizer Usage is not supported.");
-        }
-
-        Util.checkMonitorAccess();
+    public ThreadInfo[] dumpAllThreads(boolean lockedMonitors,
+                                       boolean lockedSynchronizers) {
+        verifyDumpThreads(lockedMonitors, lockedSynchronizers);
         return dumpThreads0(null, lockedMonitors, lockedSynchronizers);
     }
 
     // VM support where maxDepth == -1 to request entire stack dump
     private static native Thread[] getThreads();
-    private static native void getThreadInfo0(long[] ids,
+    private static native void getThreadInfo1(long[] ids,
                                               int maxDepth,
                                               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 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();
     private static native Thread[] findDeadlockedThreads0();