< prev index next >

test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java

Print this page

 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 

 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 = 0; 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 ((currentTime - lastTimestamp) > 500) {
 91                 System.out.format("%s %d remaining ...%n", Instant.now(), (iterations - i));
 92                 lastTimestamp = currentTime;
 93             }
 94         }
 95     }
 96 
< prev index next >