1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test id=default
 26  * @bug 8284161 8303242
 27  * @summary Test com.sun.management.ThreadMXBean with virtual threads
 28  * @enablePreview
 29  * @library /test/lib
 30  * @run junit/othervm VirtualThreads
 31  */
 32 
 33 /**
 34  * @test id=no-vmcontinuations
 35  * @requires vm.continuations
 36  * @enablePreview
 37  * @library /test/lib
 38  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreads
 39  */
 40 
 41 import java.lang.management.ManagementFactory;
 42 import java.util.concurrent.locks.LockSupport;
 43 import com.sun.management.ThreadMXBean;
 44 
 45 import jdk.test.lib.thread.VThreadRunner;
 46 import org.junit.jupiter.api.Test;
 47 import static org.junit.jupiter.api.Assertions.*;
 48 import static org.junit.jupiter.api.Assumptions.*;
 49 
 50 public class VirtualThreads {
 51 
 52     /**
 53      * Test that ThreadMXBean::getCurrentThreadAllocatedBytes() returns -1 when
 54      * invoked from a virtual thread.
 55      */
 56     @Test
 57     void testGetCurrentThreadAllocatedBytes() throws Exception {
 58         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
 59         assumeTrue(bean.isThreadAllocatedMemorySupported(),
 60                 "Thread memory allocation measurement not supported");
 61 
 62         VThreadRunner.run(() -> {
 63             assertEquals(-1L, bean.getCurrentThreadAllocatedBytes());
 64         });
 65     }
 66 
 67     /**
 68      * Test that ThreadMXBean.getThreadAllocatedBytes(long) returns -1 when invoked
 69      * with the thread ID of a virtual thread.
 70      */
 71     @Test
 72     void testGetThreadAllocatedBytes1() {
 73         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
 74         assumeTrue(bean.isThreadAllocatedMemorySupported(),
 75                 "Thread memory allocation measurement not supported");
 76 
 77         Thread vthread = Thread.startVirtualThread(LockSupport::park);
 78         try {
 79             long allocated = bean.getThreadAllocatedBytes(vthread.threadId());
 80             assertEquals(-1L, allocated);
 81         } finally {
 82             LockSupport.unpark(vthread);
 83         }
 84     }
 85 
 86     /**
 87      * Test that ThreadMXBean.getThreadAllocatedBytes(long) returns -1 when invoked
 88      * by a virtual thread with its own thread id.
 89      */
 90     @Test
 91     void testGetThreadAllocatedBytes2() throws Exception {
 92         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
 93         assumeTrue(bean.isThreadAllocatedMemorySupported(),
 94                 "Thread memory allocation measurement not supported");
 95 
 96         VThreadRunner.run(() -> {
 97             long tid = Thread.currentThread().threadId();
 98             long allocated = bean.getThreadAllocatedBytes(tid);
 99             assertEquals(-1L, allocated);
100         });
101     }
102 
103     /**
104      * Test that ThreadMXBean.getThreadAllocatedBytes(long[]) returns -1 for
105      * elements that correspond to a virtual thread.
106      */
107     @Test
108     void testGetThreadAllocatedBytes3() {
109         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
110         assumeTrue(bean.isThreadAllocatedMemorySupported(),
111                 "Thread memory allocation measurement not supported");
112 
113         Thread vthread = Thread.startVirtualThread(LockSupport::park);
114         try {
115             long tid0 = Thread.currentThread().threadId();
116             long tid1 = vthread.threadId();
117             long[] tids = new long[] { tid0, tid1 };
118             long[] allocated = bean.getThreadAllocatedBytes(tids);
119             assertTrue(allocated[0] >= 0L);
120             assertEquals(-1L, allocated[1]);
121         } finally {
122             LockSupport.unpark(vthread);
123         }
124     }
125 
126     /**
127      * Test that ThreadMXBean.getThreadCpuTime(long[]) returns -1 for
128      * elements that correspond to a virtual thread.
129      */
130     @Test
131     void testGetThreadCpuTime() {
132         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
133         assumeTrue(bean.isThreadCpuTimeSupported(), "Thread CPU time measurement not supported");
134 
135         Thread vthread = Thread.startVirtualThread(LockSupport::park);
136         try {
137             long tid0 = Thread.currentThread().threadId();
138             long tid1 = vthread.threadId();
139             long[] tids = new long[] { tid0, tid1 };
140             long[] cpuTimes = bean.getThreadCpuTime(tids);
141             assertTrue(cpuTimes[0] >= 0L);
142             assertEquals(-1L, cpuTimes[1]);
143         } finally {
144             LockSupport.unpark(vthread);
145         }
146     }
147 
148     /**
149      * Test that ThreadMXBean.getThreadUserTime(long[])returns -1 for
150      * elements that correspond to a virtual thread.
151      */
152     @Test
153     void testGetThreadUserTime() {
154         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
155         assumeTrue(bean.isThreadCpuTimeSupported(), "Thread CPU time measurement not supported");
156 
157         Thread vthread = Thread.startVirtualThread(LockSupport::park);
158         try {
159             long tid0 = Thread.currentThread().threadId();
160             long tid1 = vthread.threadId();
161             long[] tids = new long[] { tid0, tid1 };
162             long[] userTimes = bean.getThreadUserTime(tids);
163             assertTrue(userTimes[0] >= 0L);
164             assertEquals(-1L, userTimes[1]);
165         } finally {
166             LockSupport.unpark(vthread);
167         }
168     }
169 }