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             if (Thread.currentThread().isVirtual()) {
120                 assertEquals(-1L, allocated[0]);
121             } else {
122                 assertTrue(allocated[0] >= 0L);
123             }
124             assertEquals(-1L, allocated[1]);
125         } finally {
126             LockSupport.unpark(vthread);
127         }
128     }
129 
130     /**
131      * Test that ThreadMXBean.getThreadCpuTime(long[]) returns -1 for
132      * elements that correspond to a virtual thread.
133      */
134     @Test
135     void testGetThreadCpuTime() {
136         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
137         assumeTrue(bean.isThreadCpuTimeSupported(), "Thread CPU time measurement not supported");
138 
139         Thread vthread = Thread.startVirtualThread(LockSupport::park);
140         try {
141             long tid0 = Thread.currentThread().threadId();
142             long tid1 = vthread.threadId();
143             long[] tids = new long[] { tid0, tid1 };
144             long[] cpuTimes = bean.getThreadCpuTime(tids);
145             if (Thread.currentThread().isVirtual()) {
146                 assertEquals(-1L, cpuTimes[0]);
147             } else {
148                 assertTrue(cpuTimes[0] >= 0L);
149             }
150             assertEquals(-1L, cpuTimes[1]);
151         } finally {
152             LockSupport.unpark(vthread);
153         }
154     }
155 
156     /**
157      * Test that ThreadMXBean.getThreadUserTime(long[])returns -1 for
158      * elements that correspond to a virtual thread.
159      */
160     @Test
161     void testGetThreadUserTime() {
162         ThreadMXBean bean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
163         assumeTrue(bean.isThreadCpuTimeSupported(), "Thread CPU time measurement not supported");
164 
165         Thread vthread = Thread.startVirtualThread(LockSupport::park);
166         try {
167             long tid0 = Thread.currentThread().threadId();
168             long tid1 = vthread.threadId();
169             long[] tids = new long[] { tid0, tid1 };
170             long[] userTimes = bean.getThreadUserTime(tids);
171             if (Thread.currentThread().isVirtual()) {
172                 assertEquals(-1L, userTimes[0]);
173             } else {
174                 assertTrue(userTimes[0] >= 0L);
175             }
176             assertEquals(-1L, userTimes[1]);
177         } finally {
178             LockSupport.unpark(vthread);
179         }
180     }
181 }