< prev index next >

test/jdk/java/lang/Thread/virtual/JfrEvents.java

Print this page

  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 Basic test for JFR jdk.VirtualThreadXXX events
 27  * @requires vm.continuations
 28  * @modules jdk.jfr java.base/java.lang:+open
 29  * @run junit/othervm JfrEvents

 30  */
 31 
 32 import java.io.IOException;
 33 import java.nio.file.Path;
 34 import java.time.Duration;
 35 import java.util.List;
 36 import java.util.Map;
 37 import java.util.concurrent.Executor;
 38 import java.util.concurrent.ExecutorService;
 39 import java.util.concurrent.Executors;
 40 import java.util.concurrent.RejectedExecutionException;
 41 import java.util.concurrent.ThreadFactory;

 42 import java.util.concurrent.atomic.AtomicReference;
 43 import java.util.concurrent.locks.LockSupport;

 44 import java.util.stream.Collectors;

 45 
 46 import jdk.jfr.EventType;
 47 import jdk.jfr.Recording;
 48 import jdk.jfr.consumer.RecordedEvent;
 49 import jdk.jfr.consumer.RecordingFile;
 50 


 51 import org.junit.jupiter.api.Test;



 52 import static org.junit.jupiter.api.Assertions.*;
 53 
 54 class JfrEvents {
 55     private static final Object lock = new Object();
 56 
 57     /**
 58      * Test jdk.VirtualThreadStart and jdk.VirtualThreadEnd events.
 59      */
 60     @Test
 61     void testVirtualThreadStartAndEnd() throws Exception {
 62         try (Recording recording = new Recording()) {
 63             recording.enable("jdk.VirtualThreadStart");
 64             recording.enable("jdk.VirtualThreadEnd");
 65 
 66             // execute 100 tasks, each in their own virtual thread
 67             recording.start();
 68             ThreadFactory factory = Thread.ofVirtual().factory();
 69             try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
 70                 for (int i = 0; i < 100; i++) {
 71                     executor.submit(() -> { });
 72                 }
 73                 Thread.sleep(1000); // give time for thread end events to be recorded
 74             } finally {
 75                 recording.stop();
 76             }
 77 
 78             Map<String, Integer> events = sumEvents(recording);
 79             System.err.println(events);
 80 
 81             int startCount = events.getOrDefault("jdk.VirtualThreadStart", 0);
 82             int endCount = events.getOrDefault("jdk.VirtualThreadEnd", 0);
 83             assertEquals(100, startCount);
 84             assertEquals(100, endCount);
 85         }
 86     }
 87 

















































































 88     /**
 89      * Test jdk.VirtualThreadPinned event.
 90      */
 91     @Test
 92     void testVirtualThreadPinned() throws Exception {
 93         Runnable[] parkers = new Runnable[] {
 94             () -> LockSupport.park(),
 95             () -> LockSupport.parkNanos(Duration.ofDays(1).toNanos())
 96         };
 97 
 98         try (Recording recording = new Recording()) {
 99             recording.enable("jdk.VirtualThreadPinned");
100 
101             recording.start();
102             try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
103                 for (Runnable parker : parkers) {
104                     // execute parking task in virtual thread
105                     var threadRef = new AtomicReference<Thread>();
106                     executor.submit(() -> {
107                         threadRef.set(Thread.currentThread());
108                         synchronized (lock) {
109                             parker.run();   // should pin carrier
110                         }
111                     });
112 
113                     // wait for the task to start and the virtual thread to park
114                     Thread thread;
115                     while ((thread = threadRef.get()) == null) {
116                         Thread.sleep(10);
117                     }
118                     try {
119                         Thread.State state = thread.getState();
120                         while (state != Thread.State.WAITING && state != Thread.State.TIMED_WAITING) {
121                             Thread.sleep(10);
122                             state = thread.getState();
123                         }
124                     } finally {
125                         LockSupport.unpark(thread);





126                     }




127                 }
128             } finally {
129                 recording.stop();
130             }
131 
132             Map<String, Integer> events = sumEvents(recording);
133             System.err.println(events);
134 
135             // should have a pinned event for each park
136             int pinnedCount = events.getOrDefault("jdk.VirtualThreadPinned", 0);
137             assertEquals(parkers.length, pinnedCount);
138         }
139     }
140 
141     /**
142      * Test jdk.VirtualThreadSubmitFailed event.
143      */
144     @Test
145     void testVirtualThreadSubmitFailed() throws Exception {
146         try (Recording recording = new Recording()) {
147             recording.enable("jdk.VirtualThreadSubmitFailed");
148 
149             recording.start();
150             try (ExecutorService pool = Executors.newCachedThreadPool()) {
151                 Executor scheduler = task -> pool.execute(task);
152 
153                 // create virtual thread that uses custom scheduler
154                 ThreadFactory factory = ThreadBuilders.virtualThreadBuilder(scheduler).factory();
155 
156                 // start a thread
157                 Thread thread = factory.newThread(LockSupport::park);

  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 Basic test for JFR jdk.VirtualThreadXXX events
 27  * @requires vm.continuations
 28  * @modules jdk.jfr java.base/java.lang:+open
 29  * @library /test/lib
 30  * @run junit/othervm --enable-native-access=ALL-UNNAMED JfrEvents
 31  */
 32 
 33 import java.io.IOException;
 34 import java.nio.file.Path;

 35 import java.util.List;
 36 import java.util.Map;
 37 import java.util.concurrent.Executor;
 38 import java.util.concurrent.ExecutorService;
 39 import java.util.concurrent.Executors;
 40 import java.util.concurrent.RejectedExecutionException;
 41 import java.util.concurrent.ThreadFactory;
 42 import java.util.concurrent.atomic.AtomicBoolean;
 43 import java.util.concurrent.atomic.AtomicReference;
 44 import java.util.concurrent.locks.LockSupport;
 45 import java.util.function.Consumer;
 46 import java.util.stream.Collectors;
 47 import java.util.stream.Stream;
 48 
 49 import jdk.jfr.EventType;
 50 import jdk.jfr.Recording;
 51 import jdk.jfr.consumer.RecordedEvent;
 52 import jdk.jfr.consumer.RecordingFile;
 53 
 54 import jdk.test.lib.thread.VThreadPinner;
 55 import jdk.test.lib.thread.VThreadPinner.ThrowingAction;
 56 import org.junit.jupiter.api.Test;
 57 import org.junit.jupiter.params.ParameterizedTest;
 58 import org.junit.jupiter.params.provider.Arguments;
 59 import org.junit.jupiter.params.provider.MethodSource;
 60 import static org.junit.jupiter.api.Assertions.*;
 61 
 62 class JfrEvents {

 63 
 64     /**
 65      * Test jdk.VirtualThreadStart and jdk.VirtualThreadEnd events.
 66      */
 67     @Test
 68     void testVirtualThreadStartAndEnd() throws Exception {
 69         try (Recording recording = new Recording()) {
 70             recording.enable("jdk.VirtualThreadStart");
 71             recording.enable("jdk.VirtualThreadEnd");
 72 
 73             // execute 100 tasks, each in their own virtual thread
 74             recording.start();
 75             ThreadFactory factory = Thread.ofVirtual().factory();
 76             try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
 77                 for (int i = 0; i < 100; i++) {
 78                     executor.submit(() -> { });
 79                 }
 80                 Thread.sleep(1000); // give time for thread end events to be recorded
 81             } finally {
 82                 recording.stop();
 83             }
 84 
 85             Map<String, Integer> events = sumEvents(recording);
 86             System.err.println(events);
 87 
 88             int startCount = events.getOrDefault("jdk.VirtualThreadStart", 0);
 89             int endCount = events.getOrDefault("jdk.VirtualThreadEnd", 0);
 90             assertEquals(100, startCount);
 91             assertEquals(100, endCount);
 92         }
 93     }
 94 
 95     /**
 96      * Arguments for testVirtualThreadPinned to test jdk.VirtualThreadPinned event.
 97      *   [0] label/description
 98      *   [1] the operation to park/wait
 99      *   [2] the Thread.State when parked/waiting
100      *   [3] the action to unpark/notify the thread
101      */
102     static Stream<Arguments> pinnedCases() {
103         Object lock = new Object();
104 
105         // park with native frame on stack
106         var finish1 = new AtomicBoolean();
107         var parkWhenPinned = Arguments.of(
108             "LockSupport.park when pinned",
109             (ThrowingAction) () -> {
110                 VThreadPinner.runPinned(() -> {
111                     while (!finish1.get()) {
112                         LockSupport.park();
113                     }
114                 });
115             },
116             Thread.State.WAITING,
117                 (Consumer<Thread>) t -> {
118                     finish1.set(true);
119                     LockSupport.unpark(t);
120                 }
121         );
122 
123         // timed park with native frame on stack
124         var finish2 = new AtomicBoolean();
125         var timedParkWhenPinned = Arguments.of(
126             "LockSupport.parkNanos when pinned",
127             (ThrowingAction) () -> {
128                 VThreadPinner.runPinned(() -> {
129                     while (!finish2.get()) {
130                         LockSupport.parkNanos(Long.MAX_VALUE);
131                     }
132                 });
133             },
134             Thread.State.TIMED_WAITING,
135             (Consumer<Thread>) t -> {
136                 finish2.set(true);
137                 LockSupport.unpark(t);
138             }
139         );
140 
141         // untimed Object.wait
142         var waitWhenPinned = Arguments.of(
143             "Object.wait",
144             (ThrowingAction) () -> {
145                 synchronized (lock) {
146                     lock.wait();
147                 }
148             },
149             Thread.State.WAITING,
150             (Consumer<Thread>) t -> {
151                 synchronized (lock) {
152                     lock.notifyAll();
153                 }
154             }
155         );
156 
157         // timed Object.wait
158         var timedWaitWhenPinned = Arguments.of(
159             "Object.wait(millis)",
160             (ThrowingAction) () -> {
161                 synchronized (lock) {
162                     lock.wait(Long.MAX_VALUE);
163                 }
164             },
165             Thread.State.TIMED_WAITING,
166             (Consumer<Thread>) t -> {
167                 synchronized (lock) {
168                     lock.notifyAll();
169                 }
170             }
171         );
172 
173         return Stream.of(parkWhenPinned, timedParkWhenPinned, waitWhenPinned, timedWaitWhenPinned);
174     }
175 
176     /**
177      * Test jdk.VirtualThreadPinned event.
178      */
179     @ParameterizedTest
180     @MethodSource("pinnedCases")
181     void testVirtualThreadPinned(String label,
182                                  ThrowingAction<Exception> parker,
183                                  Thread.State expectedState,
184                                  Consumer<Thread> unparker) throws Exception {
185 
186         try (Recording recording = new Recording()) {
187             recording.enable("jdk.VirtualThreadPinned");
188 
189             recording.start();
190             try {
191                 var exception = new AtomicReference<Throwable>();
192                 var thread = Thread.ofVirtual().start(() -> {













193                     try {
194                         parker.run();
195                     } catch (Throwable e) {
196                         exception.set(e);
197                     }
198                 });
199                 try {
200                     // wait for thread to park/wait
201                     Thread.State state = thread.getState();
202                     while (state != expectedState) {
203                         assertTrue(state != Thread.State.TERMINATED, thread.toString());
204                         Thread.sleep(10);
205                         state = thread.getState();
206                     }
207                 } finally {
208                     unparker.accept(thread);
209                     thread.join();
210                     assertNull(exception.get());
211                 }
212             } finally {
213                 recording.stop();
214             }
215 
216             Map<String, Integer> events = sumEvents(recording);
217             System.err.println(events);
218 
219             // should have at least one pinned event
220             int pinnedCount = events.getOrDefault("jdk.VirtualThreadPinned", 0);
221             assertTrue(pinnedCount >= 1, "Expected one or more events");
222         }
223     }
224 
225     /**
226      * Test jdk.VirtualThreadSubmitFailed event.
227      */
228     @Test
229     void testVirtualThreadSubmitFailed() throws Exception {
230         try (Recording recording = new Recording()) {
231             recording.enable("jdk.VirtualThreadSubmitFailed");
232 
233             recording.start();
234             try (ExecutorService pool = Executors.newCachedThreadPool()) {
235                 Executor scheduler = task -> pool.execute(task);
236 
237                 // create virtual thread that uses custom scheduler
238                 ThreadFactory factory = ThreadBuilders.virtualThreadBuilder(scheduler).factory();
239 
240                 // start a thread
241                 Thread thread = factory.newThread(LockSupport::park);
< prev index next >