1 /* 2 * Copyright (c) 2023, 2025, 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 26 * @bug 8337199 27 * @summary Basic test for jcmd Thread.vthread_scheduler and Thread.vthread_pollers 28 * @requires vm.continuations 29 * @modules jdk.jcmd 30 * @library /test/lib 31 * @run junit/othervm VThreadCommandsTest 32 */ 33 34 import java.net.InetAddress; 35 import java.net.InetSocketAddress; 36 import java.net.ServerSocket; 37 import java.net.Socket; 38 import java.net.SocketTimeoutException; 39 import java.util.List; 40 import java.util.Objects; 41 import java.util.concurrent.Executors; 42 import java.util.concurrent.ExecutorService; 43 import java.util.concurrent.ForkJoinPool; 44 import java.util.concurrent.ForkJoinWorkerThread; 45 import java.util.concurrent.ScheduledThreadPoolExecutor; 46 import java.util.concurrent.atomic.AtomicBoolean; 47 import java.lang.management.ManagementFactory; 48 import jdk.management.VirtualThreadSchedulerMXBean; 49 50 import jdk.test.lib.dcmd.PidJcmdExecutor; 51 import jdk.test.lib.process.OutputAnalyzer; 52 import org.junit.jupiter.api.Test; 53 import static org.junit.jupiter.api.Assertions.*; 54 55 class VThreadCommandsTest { 56 57 /** 58 * Thread.vthread_scheduler 59 */ 60 @Test 61 void testVThreadScheduler() { 62 // ensure default scheduler is initialized 63 Thread.startVirtualThread(() -> { }); 64 65 jcmd("Thread.vthread_scheduler") 66 .shouldContain(Objects.toIdentityString(defaultScheduler())); 67 } 68 69 /** 70 * Thread.vthread_pollers 71 */ 72 @Test 73 void testVThreadPollers() throws Exception { 74 // do blocking I/O op on a virtual thread to ensure poller mechanism is initialized 75 try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { 76 executor.submit(() -> { 77 try (var listener = new ServerSocket()) { 78 InetAddress lb = InetAddress.getLoopbackAddress(); 79 listener.bind(new InetSocketAddress(lb, 0)); 80 listener.setSoTimeout(200); 81 try (Socket s = listener.accept()) { 82 System.err.format("Connection from %s ??%n", s.getRemoteSocketAddress()); 83 } catch (SocketTimeoutException e) { 84 // expected 85 } 86 } 87 return null; 88 }).get(); 89 } 90 91 jcmd("Thread.vthread_pollers") 92 .shouldContain("Read I/O pollers:") 93 .shouldContain("Write I/O pollers:") 94 .shouldMatch("^\\[0\\] sun\\.nio\\.ch\\..+ \\[registered = [\\d]+, owner = .+\\]$"); 95 } 96 97 private OutputAnalyzer jcmd(String cmd) { 98 return new PidJcmdExecutor().execute(cmd); 99 } 100 101 /** 102 * Returns the virtual thread default scheduler. This implementation works by finding 103 * all FJ worker threads and mapping them to their pool. VirtualThreadSchedulerMXBean 104 * is used to temporarily changing target parallelism to an "unique" value, make it 105 * possbile to find the right pool. 106 */ 107 private ForkJoinPool defaultScheduler() { 108 var done = new AtomicBoolean(); 109 Thread vthread = Thread.startVirtualThread(() -> { 110 while (!done.get()) { 111 Thread.onSpinWait(); 112 } 113 }); 114 var bean = ManagementFactory.getPlatformMXBean(VirtualThreadSchedulerMXBean.class); 115 int parallelism = bean.getParallelism(); 116 try { 117 bean.setParallelism(133); 118 return Thread.getAllStackTraces() 119 .keySet() 120 .stream() 121 .filter(ForkJoinWorkerThread.class::isInstance) 122 .map(t -> ((ForkJoinWorkerThread) t).getPool()) 123 .filter(p -> p.getParallelism() == 133) 124 .findAny() 125 .orElseThrow(); 126 } finally { 127 bean.setParallelism(parallelism); 128 done.set(true); 129 } 130 } 131 }