1 /*
  2  * Copyright (c) 2018, 2020, 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 Verifies JVMTI GetStackTrace functions called without suspend.
 27  * @library /test/lib
 28  * @compile --enable-preview -source ${jdk.version} GetStackTraceNotSuspendedStressTest.java
 29  * @run main/othervm/native --enable-preview -agentlib:GetStackTraceNotSuspendedStress GetStackTraceNotSuspendedStressTest
 30  */
 31 
 32 import jdk.test.lib.jvmti.DebugeeClass;
 33 
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import java.util.concurrent.*;
 37 
 38 public class GetStackTraceNotSuspendedStressTest extends DebugeeClass {
 39     private static final String agentLib = "GetStackTraceNotSuspendedStress";
 40 
 41     static final int MSG_COUNT = 1000;
 42     static final SynchronousQueue<String> QUEUE = new SynchronousQueue<>();
 43 
 44     static void producer(String msg) throws InterruptedException {
 45         int ii = 1;
 46         long ll = 2*(long)ii;
 47         float ff = ll + 1.2f;
 48         double dd = ff + 1.3D;
 49         msg += dd;
 50         QUEUE.put(msg);
 51     }
 52 
 53     static final Runnable PRODUCER = () -> {
 54         try {
 55             for (int i = 0; i < MSG_COUNT; i++) {
 56                 producer("msg: ");
 57             }
 58         } catch (InterruptedException e) { }
 59     };
 60 
 61     static final Runnable CONSUMER = () -> {
 62         try {
 63             for (int i = 0; i < MSG_COUNT; i++) {
 64                 String s = QUEUE.take();
 65             }
 66         } catch (InterruptedException e) { }
 67     };
 68 
 69     public static void test1() throws Exception {
 70         List<Thread> threads = new ArrayList<>();
 71         for (int i = 0; i < 100; i++) {
 72             threads.add(Thread.ofVirtual().name("VThread-Producer-" + i).unstarted(PRODUCER));
 73             threads.add(Thread.ofVirtual().name("VThread-Consumer-" + i).unstarted(CONSUMER));
 74         }
 75 
 76         for (Thread t: threads) {
 77             t.start();
 78         }
 79 
 80         for (Thread t: threads) {
 81             t.join();
 82         }
 83     }
 84 
 85     void runTest() throws Exception {
 86         // sync point to start agent thread
 87         checkStatus(0);
 88         test1();
 89     }
 90 
 91     public static void main(String[] args) throws Exception {
 92         try {
 93             System.loadLibrary(agentLib);
 94         } catch (UnsatisfiedLinkError ex) {
 95             System.err.println("Failed to load " + agentLib + " lib");
 96             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
 97             throw ex;
 98         }
 99 
100         GetStackTraceNotSuspendedStressTest obj = new GetStackTraceNotSuspendedStressTest();
101         obj.runTest();
102     }
103 }