< prev index next >

test/hotspot/jtreg/serviceability/jvmti/vthread/VThreadEventTest/VThreadEventTest.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 8312174
 27  * @summary missing JVMTI events from vthreads parked during JVMTI attach
 28  * @requires vm.continuations
 29  * @requires vm.jvmti
 30  * @requires vm.compMode != "Xcomp"


 31  * @run main/othervm/native
 32  *     -Djdk.virtualThreadScheduler.parallelism=9
 33  *     -Djdk.attach.allowAttachSelf=true -XX:+EnableDynamicAgentLoading VThreadEventTest attach
 34  */
 35 
 36 import com.sun.tools.attach.VirtualMachine;
 37 import java.util.concurrent.ExecutorService;
 38 import java.util.concurrent.Executors;
 39 import java.util.concurrent.locks.LockSupport;
 40 import java.util.List;
 41 import java.util.ArrayList;
 42 
 43 /*
 44  * The test uses custom implementation of the CountDownLatch class.
 45  * The reason is we want the state of tested thread to be predictable.
 46  * With java.util.concurrent.CountDownLatch it is not clear what thread state is expected.
 47  */
 48 class CountDownLatch {
 49     private int count = 0;
 50 
 51     CountDownLatch(int count) {
 52         this.count = count;
 53     }
 54 
 55     public synchronized void countDown() {
 56         count--;
 57         notify();
 58     }
 59 
 60     public synchronized void await() throws InterruptedException {
 61         while (count > 0) {
 62             wait(1);
 63         }
 64     }
 65 }
 66 
 67 public class VThreadEventTest {
 68     static final int TCNT1 = 10;
 69     static final int TCNT2 = 4;
 70     static final int TCNT3 = 4;
 71     static final int THREAD_CNT = TCNT1 + TCNT2 + TCNT3;
 72 
 73     private static void log(String msg) { System.out.println(msg); }
 74 
 75     private static native int threadEndCount();
 76     private static native int threadMountCount();
 77     private static native int threadUnmountCount();
 78 
 79     private static volatile boolean attached;
 80     private static boolean failed;
 81     private static List<Thread> test1Threads = new ArrayList(TCNT1);
 82 
 83     private static CountDownLatch ready0 = new CountDownLatch(THREAD_CNT);
 84     private static CountDownLatch ready1 = new CountDownLatch(TCNT1);
 85     private static CountDownLatch ready2 = new CountDownLatch(THREAD_CNT);
 86     private static CountDownLatch mready = new CountDownLatch(1);
 87 
 88     private static void await(CountDownLatch dumpedLatch) {
 89         try {
 90             dumpedLatch.await();
 91         } catch (InterruptedException e) {
 92             throw new RuntimeException(e);
 93         }
 94     }
 95 
 96     // The test1 vthreads are kept unmounted until interrupted after agent attach.
 97     static final Runnable test1 = () -> {
 98         synchronized (test1Threads) {
 99             test1Threads.add(Thread.currentThread());
100         }
101         log("test1 vthread started");
102         ready0.countDown();
103         await(mready);
104         ready1.countDown(); // to guaranty state is not State.WAITING after await(mready)
105         try {
106             Thread.sleep(20000); // big timeout to keep unmounted until interrupted
107         } catch (InterruptedException ex) {
108             // it is expected, ignore





109         }
110         ready2.countDown();
111     };
112 
113     // The test2 vthreads are kept mounted until agent attach.
114     static final Runnable test2 = () -> {
115         log("test2 vthread started");
116         ready0.countDown();
117         await(mready);
118         while (!attached) {
119             // keep mounted







120         }
121         ready2.countDown();
122     };
123 
124     // The test3 vthreads are kept mounted until agent attach.
125     static final Runnable test3 = () -> {
126         log("test3 vthread started");
127         ready0.countDown();
128         await(mready);
129         while (!attached) {
130             // keep mounted
131         }
132         LockSupport.parkNanos(10_000_000L); // will cause extra mount and unmount
133         ready2.countDown();
134     };
135 
136     public static void main(String[] args) throws Exception {
137         if (Runtime.getRuntime().availableProcessors() < 8) {
138             log("WARNING: test expects at least 8 processors.");
139         }
140         try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
141             for (int i = 0; i < TCNT1; i++) {
142                 executorService.execute(test1);
143             }
144             for (int i = 0; i < TCNT2; i++) {
145                 executorService.execute(test2);
146             }
147             for (int i = 0; i < TCNT3; i++) {
148                 executorService.execute(test3);
149             }
150             await(ready0);
151             mready.countDown();
152             await(ready1); // to guarantee state is not State.TIMED_WAITING after await(mready) in test1()
153             // wait for test1 threads to reach TIMED_WAITING state in sleep()
154             for (Thread t : test1Threads) {
155                 Thread.State state = t.getState();
156                 log("DBG: state: " + state);
157                 while (state != Thread.State.TIMED_WAITING) {
158                     Thread.sleep(10);
159                     state = t.getState();
160                     log("DBG: state: " + state);
161                 }
162             }
163 
164             VirtualMachine vm = VirtualMachine.attach(String.valueOf(ProcessHandle.current().pid()));
165             vm.loadAgentLibrary("VThreadEventTest");
166             Thread.sleep(200); // to allow the agent to get ready
167 
168             attached = true;
169             for (Thread t : test1Threads) {
170                  t.interrupt();
171             }
172             ready2.await();
173         }
174         // wait until all VirtualThreadEnd events have been sent
175         for (int sleepNo = 1; threadEndCount() < THREAD_CNT; sleepNo++) {
176             Thread.sleep(100);
177             if (sleepNo % 100 == 0) { // 10 sec period of waiting
178                 log("main: waited seconds: " + sleepNo/10);
179             }
180         }

181         int threadEndCnt = threadEndCount();
182         int threadMountCnt = threadMountCount();
183         int threadUnmountCnt = threadUnmountCount();
184         int threadEndExp = THREAD_CNT;
185         int threadMountExp = THREAD_CNT - TCNT2;
186         int threadUnmountExp = THREAD_CNT + TCNT3;
187 
188         log("ThreadEnd cnt: "     + threadEndCnt     + " (expected: " + threadEndExp + ")");
189         log("ThreadMount cnt: "   + threadMountCnt   + " (expected: " + threadMountExp + ")");
190         log("ThreadUnmount cnt: " + threadUnmountCnt + " (expected: " + threadUnmountExp + ")");

191 
192         if (threadEndCnt != threadEndExp) {
193             log("FAILED: unexpected count of ThreadEnd events");


194             failed = true;
195         }
196         if (threadMountCnt != threadMountExp) {
197             log("FAILED: unexpected count of ThreadMount events");
198             failed = true;
199         }
200         if (threadUnmountCnt != threadUnmountExp) {
201             log("FAILED: unexpected count of ThreadUnmount events");
202             failed = true;
203         }
204         if (failed) {
205             throw new RuntimeException("FAILED: event count is wrong");
206         }
207     }
208 









209 }
210 

 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 8312174
 27  * @summary missing JVMTI events from vthreads parked during JVMTI attach
 28  * @requires vm.continuations
 29  * @requires vm.jvmti
 30  * @requires vm.compMode != "Xcomp"
 31  * @modules java.base/java.lang:+open
 32  * @library /test/lib
 33  * @run main/othervm/native

 34  *     -Djdk.attach.allowAttachSelf=true -XX:+EnableDynamicAgentLoading VThreadEventTest attach
 35  */
 36 
 37 import com.sun.tools.attach.VirtualMachine;
 38 import java.util.concurrent.atomic.AtomicBoolean;

 39 import java.util.concurrent.locks.LockSupport;
 40 import java.util.List;
 41 import java.util.ArrayList;
 42 import jdk.test.lib.thread.VThreadRunner;























 43 
 44 public class VThreadEventTest {
 45     static final int PARKED_THREAD_COUNT = 4;
 46     static final int SPINNING_THREAD_COUNT = 4;


 47 
 48     private static void log(String msg) { System.out.println(msg); }
 49 
 50     private static native int threadEndCount();
 51     private static native int threadMountCount();
 52     private static native int threadUnmountCount();
 53 
 54     private static volatile boolean attached;
 55 
 56     // called by agent when it is initialized and has enabled events
 57     static void agentStarted() {
 58         attached = true;










 59     }
 60 
 61     public static void main(String[] args) throws Exception {
 62         VThreadRunner.ensureParallelism(SPINNING_THREAD_COUNT+1);
 63 
 64         // start threads that park (unmount)
 65         var threads1 = new ArrayList<Thread>();
 66         for (int i = 0; i < PARKED_THREAD_COUNT; i++) {
 67             var started = new AtomicBoolean();
 68             var thread = Thread.startVirtualThread(() -> {
 69                 started.set(true);
 70                 LockSupport.park();
 71             });
 72 
 73             // wait for thread to start execution + park
 74             while (!started.get()) {
 75                 Thread.sleep(10);
 76             }
 77             await(thread, Thread.State.WAITING);
 78             threads1.add(thread);
 79         }
 80 
 81         // start threads that spin (stay mounted)
 82         var threads2 = new ArrayList<Thread>();
 83         for (int i = 0; i < SPINNING_THREAD_COUNT; i++) {
 84             var started = new AtomicBoolean();
 85             var thread = Thread.startVirtualThread(() -> {
 86                 started.set(true);
 87                 while (!attached) {
 88                     Thread.onSpinWait();
 89                 }
 90             });
 91 
 92             // wait for thread to start execution
 93             while (!started.get()) {
 94                 Thread.sleep(10);
 95             }
 96             threads2.add(thread);
 97         }
 98 
 99         // attach to the current VM
100         VirtualMachine vm = VirtualMachine.attach(String.valueOf(ProcessHandle.current().pid()));
101         vm.loadAgentLibrary("VThreadEventTest");
102 
103         // wait for agent to start


104         while (!attached) {
105             Thread.sleep(10);
106         }



107 
108         // unpark the threads that were parked
109         for (Thread thread : threads1) {
110             LockSupport.unpark(thread);
111         }



























112 
113         // wait for all threads to terminate
114         for (Thread thread : threads1) {
115             thread.join();


116         }
117         for (Thread thread : threads2) {
118             thread.join();




119         }
120 
121         int threadEndCnt = threadEndCount();
122         int threadMountCnt = threadMountCount();
123         int threadUnmountCnt = threadUnmountCount();



124 
125         int threadCount = PARKED_THREAD_COUNT + SPINNING_THREAD_COUNT;
126         log("VirtualThreadEnd events: " + threadEndCnt + ", expected: " + threadCount);
127         log("VirtualThreadMount events: " + threadMountCnt + ", expected: " + PARKED_THREAD_COUNT);
128         log("VirtualThreadUnmount events: " + threadUnmountCnt + ", expected: " + threadCount);
129 
130 
131         boolean failed = false;
132         if (threadEndCnt != threadCount) {
133             log("FAILED: unexpected count of VirtualThreadEnd events");
134             failed = true;
135         }
136         if (threadMountCnt != PARKED_THREAD_COUNT) {
137             log("FAILED: unexpected count of VirtualThreadMount events");
138             failed = true;
139         }
140         if (threadUnmountCnt != threadCount) {
141             log("FAILED: unexpected count of VirtualThreadUnmount events");
142             failed = true;
143         }
144         if (failed) {
145             throw new RuntimeException("FAILED: event count is wrong");
146         }
147     }
148 
149     private static void await(Thread thread, Thread.State expectedState) throws InterruptedException {
150         Thread.State state = thread.getState();
151         while (state != expectedState) {
152             assert state != Thread.State.TERMINATED : "Thread has terminated";
153             Thread.sleep(10);
154             state = thread.getState();
155         }
156     }
157 
158 }
159 
< prev index next >