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         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