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 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 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 
 47 public class GetStackTraceALotWhenPinned {
 48 
 49     public static void main(String[] args) throws Exception {
 50         // need at least two carrier threads when main thread is a virtual thread
 51         if (Thread.currentThread().isVirtual()) {
 52             VThreadRunner.ensureParallelism(2);
 53         }
 54 
 55         int iterations = Integer.parseInt(args[0]);
 56         var barrier = new Barrier(2);
 57 
 58         // Start a virtual thread that loops doing Thread.yield and parking while pinned.
 59         // This loop creates the conditions for the main thread to sample the stack trace
 60         // as it transitions from being unmounted to parking while pinned.
 61         var thread = Thread.startVirtualThread(() -> {
 62             boolean timed = false;
 63             for (int i = 0; i < iterations; i++) {
 64                 // wait for main thread to arrive
 65                 barrier.await();
 66 
 67                 Thread.yield();
 68                 synchronized (GetStackTraceALotWhenPinned.class) {
 69                     if (timed) {
 70                         LockSupport.parkNanos(Long.MAX_VALUE);
 71                     } else {
 72                         LockSupport.park();
 73                     }
 74                 }
 75                 timed = !timed;
 76             }
 77         });
 78 
 79         long lastTimestamp = System.currentTimeMillis();
 80         for (int i = 0; i < iterations; i++) {
 81             // wait for virtual thread to arrive
 82             barrier.await();
 83 
 84             thread.getStackTrace();
 85             LockSupport.unpark(thread);
 86 
 87             long currentTime = System.currentTimeMillis();
 88             if ((currentTime - lastTimestamp) > 500) {
 89                 System.out.format("%s %d remaining ...%n", Instant.now(), (iterations - i));
 90                 lastTimestamp = currentTime;
 91             }
 92         }
 93     }
 94 
 95     /**
 96      * Alow threads wait for each other to reach a common barrier point. This class does
 97      * not park threads that are waiting for the barrier to trip, instead it spins. This
 98      * makes it suitable for tests that use LockSupport.park or Thread.yield.
 99      */
100     private static class Barrier {
101         private final int parties;
102         private final AtomicInteger count;
103         private volatile int generation;
104 
105         Barrier(int parties) {
106             this.parties = parties;
107             this.count = new AtomicInteger(parties);
108         }
109 
110         void await() {
111             int g = generation;
112             if (count.decrementAndGet() == 0) {
113                 count.set(parties);
114                 generation = g + 1;
115             } else {
116                 while (generation == g) {
117                     Thread.onSpinWait();
118                 }
119             }
120         }
121 
122     }
123 }