|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2022, 2025, Red Hat Inc. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | + |
| 27 | +package com.oracle.svm.test.jfr; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertFalse; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | +import static org.junit.Assert.fail; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.locks.LockSupport; |
| 35 | + |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +import com.oracle.svm.core.jfr.JfrEvent; |
| 39 | +import com.oracle.svm.core.util.TimeUtils; |
| 40 | + |
| 41 | +import jdk.jfr.Recording; |
| 42 | +import jdk.jfr.consumer.RecordedClass; |
| 43 | +import jdk.jfr.consumer.RecordedEvent; |
| 44 | + |
| 45 | +public class TestOmitInternalParkEvents extends JfrRecordingTest { |
| 46 | + private static final int MILLIS = 100; |
| 47 | + private final MonitorHelper monitorHelper = new MonitorHelper(); |
| 48 | + private Thread secondThread; |
| 49 | + private volatile boolean passedCheckpoint; |
| 50 | + |
| 51 | + @Test |
| 52 | + public void test() throws Throwable { |
| 53 | + String[] events = new String[]{JfrEvent.JavaMonitorEnter.getName(), JfrEvent.JavaMonitorWait.getName(), JfrEvent.ThreadPark.getName()}; |
| 54 | + Recording recording = startRecording(events); |
| 55 | + |
| 56 | + /* Generate monitor enter events. */ |
| 57 | + Thread firstThread = new Thread(() -> { |
| 58 | + try { |
| 59 | + monitorHelper.doWork(); |
| 60 | + } catch (InterruptedException e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + secondThread = new Thread(() -> { |
| 66 | + try { |
| 67 | + passedCheckpoint = true; |
| 68 | + monitorHelper.doWork(); |
| 69 | + } catch (InterruptedException e) { |
| 70 | + throw new RuntimeException(e); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + /* Start the first thread so that it can then start the second thread. */ |
| 75 | + firstThread.start(); |
| 76 | + |
| 77 | + firstThread.join(); |
| 78 | + secondThread.join(); |
| 79 | + |
| 80 | + /* Generate monitor wait events. */ |
| 81 | + try { |
| 82 | + monitorHelper.waitUntilTimeout(); |
| 83 | + } catch (InterruptedException e) { |
| 84 | + fail(e.getMessage()); |
| 85 | + } |
| 86 | + |
| 87 | + /* Generate thread park events. */ |
| 88 | + LockSupport.parkNanos(this, TimeUtils.millisToNanos(MILLIS)); |
| 89 | + |
| 90 | + stopRecording(recording, this::validateEvents); |
| 91 | + } |
| 92 | + |
| 93 | + private void validateEvents(List<RecordedEvent> events) { |
| 94 | + boolean found = false; |
| 95 | + for (RecordedEvent event : events) { |
| 96 | + if (event.getEventType().getName().equals(JfrEvent.ThreadPark.getName())) { |
| 97 | + RecordedClass parkedClass = event.getValue("parkedClass"); |
| 98 | + if (parkedClass != null) { |
| 99 | + String parkedClassName = parkedClass.getName(); |
| 100 | + assertFalse(parkedClassName.contains("JavaMonitor")); |
| 101 | + found = true; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + assertTrue("Expected jdk.ThreadPark event not found", found); |
| 106 | + } |
| 107 | + |
| 108 | + private final class MonitorHelper { |
| 109 | + private synchronized void doWork() throws InterruptedException { |
| 110 | + if (Thread.currentThread().equals(secondThread)) { |
| 111 | + /* The second thread doesn't need to do any work. */ |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + /* Start the second thread while this thread holds the lock. */ |
| 116 | + secondThread.start(); |
| 117 | + |
| 118 | + /* Spin until the second thread blocks. */ |
| 119 | + while (!secondThread.getState().equals(Thread.State.BLOCKED) || !passedCheckpoint) { |
| 120 | + Thread.sleep(100); |
| 121 | + } |
| 122 | + Thread.sleep(MILLIS); |
| 123 | + } |
| 124 | + |
| 125 | + private synchronized void waitUntilTimeout() throws InterruptedException { |
| 126 | + wait(MILLIS); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments