< prev index next >

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

Print this page
*** 26,10 ***
--- 26,15 ---
  package sun.management;
  
  import java.lang.management.ManagementFactory;
  import java.lang.management.ThreadInfo;
  import java.lang.management.ThreadMXBean;
+ import java.lang.reflect.InvocationTargetException;
+ import java.lang.reflect.Method;
+ import java.security.AccessController;
+ import java.security.PrivilegedActionException;
+ import java.security.PrivilegedExceptionAction;
  import javax.management.ObjectName;
  import java.util.Objects;
  
  /**
   * Implementation for java.lang.management.ThreadMXBean as well as providing the

*** 224,10 ***
--- 229,13 ---
          }
      }
  
      private boolean verifyCurrentThreadCpuTime() {
          // check if Thread CPU time measurement is supported.
+         if (isVirtual(Thread.currentThread())) {
+             throw new UnsupportedOperationException("Not supported by virtual threads");
+         }
          if (!isCurrentThreadCpuTimeSupported()) {
              throw new UnsupportedOperationException(
                  "Current thread CPU time measurement is not supported.");
          }
          return isThreadCpuTimeEnabled();

*** 281,14 ***
          java.util.Arrays.fill(times, -1);
  
          if (verified) {
              if (length == 1) {
                  long id = ids[0];
!                 if (id == Thread.currentThread().getId()) {
!                     id = 0;
                  }
-                 times[0] = getThreadTotalCpuTime0(id);
              } else {
                  getThreadTotalCpuTime1(ids, times);
              }
          }
          return times;
--- 289,20 ---
          java.util.Arrays.fill(times, -1);
  
          if (verified) {
              if (length == 1) {
                  long id = ids[0];
!                 Thread thread = Thread.currentThread();
!                 if (id == thread.getId()) {
+                     if (isVirtual(thread)) {
+                         times[0] = -1;
+                     } else {
+                         times[0] = getThreadTotalCpuTime0(0);
+                     }
+                 } else {
+                     times[0] = getThreadTotalCpuTime0(id);
                  }
              } else {
                  getThreadTotalCpuTime1(ids, times);
              }
          }
          return times;

*** 318,14 ***
          java.util.Arrays.fill(times, -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;
--- 332,20 ---
          java.util.Arrays.fill(times, -1);
  
          if (verified) {
              if (length == 1) {
                  long id = ids[0];
!                 Thread thread = Thread.currentThread();
!                 if (id == thread.getId()) {
+                     if (isVirtual(thread)) {
+                         times[0] = -1;
+                     } else {
+                         times[0] = getThreadUserCpuTime0(0);
+                     }
+                 } else {
+                     times[0] = getThreadUserCpuTime0(id);
                  }
              } else {
                  getThreadUserCpuTime1(ids, times);
              }
          }
          return times;

*** 348,11 ***
              }
          }
      }
  
      protected long getCurrentThreadAllocatedBytes() {
!         if (isThreadAllocatedMemoryEnabled()) {
              return getThreadAllocatedMemory0(0);
          }
          return -1;
      }
  
--- 368,11 ---
              }
          }
      }
  
      protected long getCurrentThreadAllocatedBytes() {
!         if (isThreadAllocatedMemoryEnabled() && !isVirtual(Thread.currentThread())) {
              return getThreadAllocatedMemory0(0);
          }
          return -1;
      }
  

*** 361,14 ***
          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) {
--- 381,21 ---
          return isThreadAllocatedMemoryEnabled();
      }
  
      protected long getThreadAllocatedBytes(long id) {
          boolean verified = verifyThreadAllocatedMemory(id);
          if (verified) {
!             Thread thread = Thread.currentThread();
!             if (id == thread.getId()) {
+                 if (isVirtual(thread)) {
+                     return -1L;
+                 } else {
+                     return getThreadAllocatedMemory0(0);
+                 }
+             } else {
+                 return getThreadAllocatedMemory0(id);
+             }
          }
          return -1;
      }
  
      private boolean verifyThreadAllocatedMemory(long[] ids) {

*** 549,6 ***
--- 576,29 ---
      @Override
      public ObjectName getObjectName() {
          return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
      }
  
+     /**
+      * Returns true if the given Thread is a virutal thread.
+      */
+     private static boolean isVirtual(Thread thread) {
+         try {
+             return (boolean) IS_VIRTUAL.invoke(thread);
+         } catch (IllegalAccessException | InvocationTargetException e) {
+             throw new InternalError(e);
+         }
+     }
+ 
+     static final Method IS_VIRTUAL;
+     static {
+         try {
+             PrivilegedExceptionAction<Method> pa = () -> Thread.class.getMethod("isVirtual");
+             @SuppressWarnings("removal")
+             Method m = AccessController.doPrivileged(pa);
+             IS_VIRTUAL = m;
+         } catch (PrivilegedActionException e) {
+             throw new InternalError(e);
+         }
+ 
+     }
  }
< prev index next >