1 /*
  2  * Copyright (c) 2023, 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  * @bug 8322818
 27  * @summary Stress test Thread.getStackTrace on a virtual thread that is pinned
 28  * @requires vm.debug != true
 29  * @modules java.base/java.lang:+open
 30  * @library /test/lib
 31  * @run main/othervm --enable-native-access=ALL-UNNAMED GetStackTraceALotWhenPinned 500000
 32  */
 33 
 34 /*
 35  * @test
 36  * @requires vm.debug == true
 37  * @modules java.base/java.lang:+open
 38  * @library /test/lib
 39  * @run main/othervm/timeout=300 --enable-native-access=ALL-UNNAMED GetStackTraceALotWhenPinned 200000
 40  */
 41 
 42 import java.time.Instant;
 43 import java.util.concurrent.atomic.AtomicInteger;
 44 import java.util.concurrent.locks.LockSupport;
 45 import jdk.test.lib.thread.VThreadRunner;
 46 import jdk.test.lib.thread.VThreadPinner;
 47 
 48 public class GetStackTraceALotWhenPinned {
 49 
 50     public static void main(String[] args) throws Exception {
 51         // need at least two carrier threads when main thread is a virtual thread
 52         if (Thread.currentThread().isVirtual()) {
 53             VThreadRunner.ensureParallelism(2);
 54         }
 55 
 56         int iterations = Integer.parseInt(args[0]);
 57         var barrier = new Barrier(2);
 58 
 59         // Start a virtual thread that loops doing Thread.yield and parking while pinned.
 60         // This loop creates the conditions for the main thread to sample the stack trace
 61         // as it transitions from being unmounted to parking while pinned.
 62         var thread = Thread.startVirtualThread(() -> {
 63             boolean timed = false;
 64             for (int i = 0; i < iterations; i++) {
 65                 // wait for main thread to arrive
 66                 barrier.await();
 67 
 68                 Thread.yield();
 69                 boolean b = timed;
 70                 VThreadPinner.runPinned(() -> {
 71                     if (b) {
 72                         LockSupport.parkNanos(Long.MAX_VALUE);
 73                     } else {
 74                         LockSupport.park();
 75                     }
 76                 });
 77                 timed = !timed;
 78             }
 79         });
 80 
 81         long lastTimestamp = System.currentTimeMillis();
 82         for (int i = 1; i <= iterations; i++) {
 83             // wait for virtual thread to arrive
 84             barrier.await();
 85 
 86             thread.getStackTrace();
 87             LockSupport.unpark(thread);
 88 
 89             long currentTime = System.currentTimeMillis();
 90             if (i == iterations || ((currentTime - lastTimestamp) > 500)) {
 91                 System.out.format("%s => %d of %d%n", Instant.now(), i, iterations);
 92                 lastTimestamp = currentTime;
 93             }
 94         }
 95     }
 96 
 97     /**
 98      * Alow threads wait for each other to reach a common barrier point. This class does
 99      * not park threads that are waiting for the barrier to trip, instead it spins. This
100      * makes it suitable for tests that use LockSupport.park or Thread.yield.
101      */
102     private static class Barrier {
103         private final int parties;
104         private final AtomicInteger count;
105         private volatile int generation;
106 
107         Barrier(int parties) {
108             this.parties = parties;
109             this.count = new AtomicInteger(parties);
110         }
111 
112         void await() {
113             int g = generation;
114             if (count.decrementAndGet() == 0) {
115                 count.set(parties);
116                 generation = g + 1;
117             } else {
118                 while (generation == g) {
119                     Thread.onSpinWait();
120                 }
121             }
122         }
123 
124     }
125 }