1 /*
  2  * Copyright (c) 2023, 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 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         if (Thread.currentThread().isVirtual()) {
 63             System.out.println("Skipping test as current thread is a virtual thread");
 64             return;
 65         }
 66         VThreadRunner.ensureParallelism(SPINNING_THREAD_COUNT+1);
 67 
 68         // start threads that park (unmount)
 69         var threads1 = new ArrayList<Thread>();
 70         for (int i = 0; i < PARKED_THREAD_COUNT; i++) {
 71             var started = new AtomicBoolean();
 72             var thread = Thread.startVirtualThread(() -> {
 73                 started.set(true);
 74                 LockSupport.park();
 75             });
 76 
 77             // wait for thread to start execution + park
 78             while (!started.get()) {
 79                 Thread.sleep(10);
 80             }
 81             await(thread, Thread.State.WAITING);
 82             threads1.add(thread);
 83         }
 84 
 85         // start threads that spin (stay mounted)
 86         var threads2 = new ArrayList<Thread>();
 87         for (int i = 0; i < SPINNING_THREAD_COUNT; i++) {
 88             var started = new AtomicBoolean();
 89             var thread = Thread.startVirtualThread(() -> {
 90                 started.set(true);
 91                 while (!attached) {
 92                     Thread.onSpinWait();
 93                 }
 94             });
 95 
 96             // wait for thread to start execution
 97             while (!started.get()) {
 98                 Thread.sleep(10);
 99             }
100             threads2.add(thread);
101         }
102 
103         // attach to the current VM
104         VirtualMachine vm = VirtualMachine.attach(String.valueOf(ProcessHandle.current().pid()));
105         vm.loadAgentLibrary("VThreadEventTest");
106 
107         // wait for agent to start
108         while (!attached) {
109             Thread.sleep(10);
110         }
111 
112         // unpark the threads that were parked
113         for (Thread thread : threads1) {
114             LockSupport.unpark(thread);
115         }
116 
117         // wait for all threads to terminate
118         for (Thread thread : threads1) {
119             thread.join();
120         }
121         for (Thread thread : threads2) {
122             thread.join();
123         }
124 
125         int threadEndCnt = threadEndCount();
126         int threadMountCnt = threadMountCount();
127         int threadUnmountCnt = threadUnmountCount();
128 
129         int threadCount = PARKED_THREAD_COUNT + SPINNING_THREAD_COUNT;
130         log("VirtualThreadEnd events: " + threadEndCnt + ", expected: " + threadCount);
131         log("VirtualThreadMount events: " + threadMountCnt + ", expected: " + PARKED_THREAD_COUNT);
132         log("VirtualThreadUnmount events: " + threadUnmountCnt + ", expected: " + threadCount);
133 
134         boolean failed = false;
135         if (threadEndCnt != threadCount) {
136             log("FAILED: unexpected count of VirtualThreadEnd events");
137             failed = true;
138         }
139         if (threadMountCnt != PARKED_THREAD_COUNT) {
140             log("FAILED: unexpected count of VirtualThreadMount events");
141             failed = true;
142         }
143         if (threadUnmountCnt != threadCount) {
144             log("FAILED: unexpected count of VirtualThreadUnmount events");
145             failed = true;
146         }
147         if (failed) {
148             throw new RuntimeException("FAILED: event count is wrong");
149         }
150     }
151 
152     private static void await(Thread thread, Thread.State expectedState) throws InterruptedException {
153         Thread.State state = thread.getState();
154         while (state != expectedState) {
155             assert state != Thread.State.TERMINATED : "Thread has terminated";
156             Thread.sleep(10);
157             state = thread.getState();
158         }
159     }
160 
161 }
162