|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Alibaba Group Holding Limited. 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. Alibaba designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @test TestGCLockerEvent |
| 24 | + * @key jfr |
| 25 | + * @requires vm.hasJFR |
| 26 | + * @library /test/lib |
| 27 | + * @build sun.hotspot.WhiteBox |
| 28 | + * @run driver ClassFileInstaller sun.hotspot.WhiteBox |
| 29 | + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xmx32m -Xms32m -Xmn12m -XX:+UseG1GC jdk.jfr.event.gc.detailed.TestGCLockerEvent |
| 30 | + */ |
| 31 | + |
| 32 | +package jdk.jfr.event.gc.detailed; |
| 33 | + |
| 34 | +import static jdk.test.lib.Asserts.assertTrue; |
| 35 | + |
| 36 | +import java.util.concurrent.CountDownLatch; |
| 37 | + |
| 38 | +import jdk.jfr.Recording; |
| 39 | +import jdk.jfr.consumer.RecordedEvent; |
| 40 | +import jdk.test.lib.jfr.EventNames; |
| 41 | +import jdk.test.lib.jfr.Events; |
| 42 | + |
| 43 | +import sun.hotspot.WhiteBox; |
| 44 | + |
| 45 | +public class TestGCLockerEvent { |
| 46 | + |
| 47 | + private static final String EVENT_NAME = EventNames.GCLocker; |
| 48 | + |
| 49 | + private static final int CRITICAL_THREAD_COUNT = 4; |
| 50 | + |
| 51 | + private static final CountDownLatch LOCK_COUNT_SIGNAL = new CountDownLatch(CRITICAL_THREAD_COUNT); |
| 52 | + |
| 53 | + private static final CountDownLatch UNLOCK_SIGNAL = new CountDownLatch(1); |
| 54 | + |
| 55 | + private static final CountDownLatch UNLOCK_COUNT_SIGNAL = new CountDownLatch(CRITICAL_THREAD_COUNT); |
| 56 | + |
| 57 | + private static final String CRITICAL_THREAD_NAME_PREFIX = "Critical Thread "; |
| 58 | + |
| 59 | + private static final int STALL_THREAD_COUNT = 8; |
| 60 | + |
| 61 | + private static final CountDownLatch STALL_COUNT_SIGNAL = new CountDownLatch(STALL_THREAD_COUNT); |
| 62 | + |
| 63 | + private static final int LOOP = 32; |
| 64 | + |
| 65 | + private static final int M = 1024 * 1024; |
| 66 | + |
| 67 | + public static void main(String[] args) throws Exception { |
| 68 | + var recording = new Recording(); |
| 69 | + recording.enable(EVENT_NAME); |
| 70 | + recording.start(); |
| 71 | + |
| 72 | + startCriticalThreads(); |
| 73 | + LOCK_COUNT_SIGNAL.await(); |
| 74 | + startStallThreads(); |
| 75 | + STALL_COUNT_SIGNAL.await(); |
| 76 | + |
| 77 | + // Wait threads to be stalled |
| 78 | + Thread.sleep(1500); |
| 79 | + |
| 80 | + UNLOCK_SIGNAL.countDown(); |
| 81 | + UNLOCK_COUNT_SIGNAL.await(); |
| 82 | + recording.stop(); |
| 83 | + |
| 84 | + // Verify recording |
| 85 | + var all = Events.fromRecording(recording); |
| 86 | + Events.hasEvents(all); |
| 87 | + var event = all.get(0); |
| 88 | + |
| 89 | + assertTrue(Events.isEventType(event, EVENT_NAME)); |
| 90 | + Events.assertField(event, "lockCount").equal(CRITICAL_THREAD_COUNT); |
| 91 | + Events.assertField(event, "stallCount").atLeast(STALL_THREAD_COUNT); |
| 92 | + assertTrue(event.getThread().getJavaName().startsWith(CRITICAL_THREAD_NAME_PREFIX)); |
| 93 | + |
| 94 | + recording.close(); |
| 95 | + } |
| 96 | + |
| 97 | + private static void startCriticalThreads() { |
| 98 | + for (var i = 0; i < CRITICAL_THREAD_COUNT; i++) { |
| 99 | + new Thread(() -> { |
| 100 | + try { |
| 101 | + WhiteBox.getWhiteBox().lockCritical(); |
| 102 | + LOCK_COUNT_SIGNAL.countDown(); |
| 103 | + |
| 104 | + UNLOCK_SIGNAL.await(); |
| 105 | + WhiteBox.getWhiteBox().unlockCritical(); |
| 106 | + UNLOCK_COUNT_SIGNAL.countDown(); |
| 107 | + } catch (InterruptedException ex) { |
| 108 | + } |
| 109 | + }, CRITICAL_THREAD_NAME_PREFIX + i).start(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static void startStallThreads() { |
| 114 | + var ts = new Thread[STALL_THREAD_COUNT]; |
| 115 | + for (var i = 0; i < STALL_THREAD_COUNT; i++) { |
| 116 | + ts[i] = new Thread(() -> { |
| 117 | + STALL_COUNT_SIGNAL.countDown(); |
| 118 | + for (int j = 0; j < LOOP; j++) { |
| 119 | + byte[] bytes = new byte[M]; |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + for (Thread t : ts) { |
| 124 | + t.start(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
0 commit comments