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