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
26  * @summary Stress test Thread.getStackTrace on virtual threads that are blocking or
27  *     blocked on monitorenter
28  * @requires vm.debug != true
29  * @modules java.base/java.lang:+open
30  * @library /test/lib
31  * @run main/othervm GetStackTraceALotWhenBlocking 500000
32  */
33 
34 /*
35  * @test
36  * @requires vm.debug == true & vm.continuations
37  * @modules java.base/java.lang:+open
38  * @library /test/lib
39  * @run main/othervm/timeout=300 GetStackTraceALotWhenBlocking 50000
40  */
41 
42 import java.time.Instant;
43 import java.util.concurrent.ThreadLocalRandom;
44 import java.util.concurrent.atomic.AtomicBoolean;
45 import jdk.test.lib.thread.VThreadRunner;
46 
47 public class GetStackTraceALotWhenBlocking {
48 
49     public static void main(String[] args) throws Exception {
50         // need at least two carriers
51         VThreadRunner.ensureParallelism(2);
52 
53         int iterations = args.length > 0 ? Integer.parseInt(args[0]) : 100_000;
54 
55         var done = new AtomicBoolean();
56         var lock = new Object();
57 
58         Runnable task = () -> {
59             long count = 0L;
60             while (!done.get()) {
61                 synchronized (lock) {
62                     pause();
63                 }
64                 count++;
65             }
66             System.out.format("%s %s => %d loops%n", Instant.now(), Thread.currentThread(), count);
67         };
68 
69         var thread1 = Thread.ofVirtual().start(task);
70         var thread2 = Thread.ofVirtual().start(task);
71         try {
72             for (int i = 1; i <= iterations; i++) {
73                 if ((i % 10_000) == 0) {
74                     System.out.format("%s => %d of %d%n", Instant.now(), i, iterations);
75                 }
76 
77                 thread1.getStackTrace();
78                 pause();
79                 thread2.getStackTrace();
80                 pause();
81             }
82         } finally {
83             done.set(true);
84             thread1.join();
85             thread2.join();
86         }
87     }
88 
89     private static void pause() {
90         if (ThreadLocalRandom.current().nextBoolean()) {
91             Thread.onSpinWait();
92         } else {
93             Thread.yield();
94         }
95     }
96 }