1 /* 2 * Copyright (c) 2020, 2024, 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 * @summary Stress test asynchronous Thread.getStackTrace when parking 27 * @requires vm.debug != true & vm.continuations 28 * @modules java.base/java.lang:+open 29 * @library /test/lib 30 * @run main GetStackTraceALotWhenParking 31 */ 32 33 /** 34 * @test 35 * @requires vm.debug == true & vm.continuations 36 * @modules java.base/java.lang:+open 37 * @library /test/lib 38 * @run main/timeout=300 GetStackTraceALotWhenParking 1000 39 */ 40 41 import java.time.Duration; 42 import java.time.Instant; 43 import java.util.concurrent.Executor; 44 import java.util.concurrent.Executors; 45 import java.util.concurrent.ExecutorService; 46 import java.util.concurrent.ThreadFactory; 47 import java.util.concurrent.atomic.AtomicInteger; 48 import java.util.concurrent.locks.LockSupport; 49 import jdk.test.lib.thread.VThreadScheduler; 50 51 public class GetStackTraceALotWhenParking { 52 static class RoundRobinExecutor implements Executor, AutoCloseable { 53 private final ExecutorService[] executors; 54 private int next; 55 56 RoundRobinExecutor() { 57 var factory = Thread.ofPlatform().name("worker-", 1).daemon(true).factory(); 58 var executors = new ExecutorService[2]; 59 for (int i = 0; i < executors.length; i++) { 60 executors[i] = Executors.newSingleThreadExecutor(factory); 61 } 62 this.executors = executors; 63 } 64 65 @Override 66 public void execute(Runnable task) { 67 executors[next].execute(task); 68 next = (next + 1) % executors.length; 69 } 70 71 @Override 72 public void close() { 73 for (int i = 0; i < executors.length; i++) { 74 executors[i].shutdown(); 75 } 76 } 77 } 78 79 public static void main(String[] args) throws Exception { 80 int iterations = args.length > 0 ? Integer.parseInt(args[0]) : 10_000; 81 82 final int ITERATIONS = iterations; 83 final int SPIN_NANOS = 5000; 84 85 AtomicInteger count = new AtomicInteger(); 86 87 try (RoundRobinExecutor executor = new RoundRobinExecutor()) { 88 ThreadFactory factory = VThreadScheduler.virtualThreadFactory(executor); 89 90 Thread thread = factory.newThread(() -> { 91 while (count.incrementAndGet() < ITERATIONS) { 92 long start = System.nanoTime(); 93 while ((System.nanoTime() - start) < SPIN_NANOS) { 94 Thread.onSpinWait(); 95 } 96 LockSupport.parkNanos(500_000); 97 } 98 }); 99 thread.start(); 100 101 long start = System.nanoTime(); 102 while (thread.isAlive()) { 103 StackTraceElement[] stackTrace = thread.getStackTrace(); 104 // printStackTrace(stackTrace); 105 Thread.sleep(5); 106 if ((System.nanoTime() - start) > 500_000_000) { 107 System.out.format("%s => %d of %d%n", Instant.now(), count.get(), ITERATIONS); 108 start = System.nanoTime(); 109 } 110 } 111 112 int countValue = count.get(); 113 if (countValue != ITERATIONS) { 114 throw new RuntimeException("count = " + countValue); 115 } 116 } 117 } 118 119 static void printStackTrace(StackTraceElement[] stackTrace) { 120 if (stackTrace == null) { 121 System.out.println("NULL"); 122 } else { 123 for (var e : stackTrace) { 124 System.out.println("\t" + e); 125 } 126 } 127 } 128 }