|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 SAP SE. 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 | +package jdk.jfr.api.recording.event; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | + |
| 28 | +import jdk.jfr.Event; |
| 29 | +import jdk.jfr.FlightRecorder; |
| 30 | +import jdk.jfr.consumer.RecordingStream; |
| 31 | +import jdk.test.lib.jfr.EventNames; |
| 32 | + |
| 33 | +/** |
| 34 | + * @test Tests that periodic events are not disabled when using a very short |
| 35 | + * period |
| 36 | + * @key jfr |
| 37 | + * @requires vm.hasJFR |
| 38 | + * @library /test/lib /test/jdk |
| 39 | + * @run main/othervm jdk.jfr.api.recording.event.TestShortPeriod |
| 40 | + */ |
| 41 | +public class TestShortPeriod { |
| 42 | + |
| 43 | + static class PeriodicEvent extends Event { |
| 44 | + } |
| 45 | + |
| 46 | + private static Thread startLoopingThread() { |
| 47 | + var t = new Thread(() -> { |
| 48 | + while (!Thread.interrupted()) {} |
| 49 | + }); |
| 50 | + t.start(); |
| 51 | + return t; |
| 52 | + } |
| 53 | + |
| 54 | + public static void main(String... args) { |
| 55 | + testNativeEventPeriod(); |
| 56 | + testJavaEventPeriod(); |
| 57 | + testExecutionSamplePeriod(); |
| 58 | + } |
| 59 | + |
| 60 | + private static void testNativeEventPeriod() { |
| 61 | + try (var r = new RecordingStream()) { |
| 62 | + r.enable(EventNames.JVMInformation).withPeriod(Duration.ofNanos(1)); |
| 63 | + r.onEvent(e -> r.close()); |
| 64 | + r.start(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static void testJavaEventPeriod() { |
| 69 | + Runnable hook = () -> { |
| 70 | + PeriodicEvent e = new PeriodicEvent(); |
| 71 | + e.commit(); |
| 72 | + }; |
| 73 | + FlightRecorder.addPeriodicEvent(PeriodicEvent.class, hook); |
| 74 | + try (var r = new RecordingStream()) { |
| 75 | + r.enable(PeriodicEvent.class).withPeriod(Duration.ofNanos(1)); |
| 76 | + r.onEvent(e -> r.close()); |
| 77 | + r.start(); |
| 78 | + } |
| 79 | + FlightRecorder.removePeriodicEvent(hook); |
| 80 | + } |
| 81 | + |
| 82 | + // The execution sample event doesn't use the standard mechanism |
| 83 | + // for periodic events |
| 84 | + private static void testExecutionSamplePeriod() { |
| 85 | + try (var r = new RecordingStream()) { |
| 86 | + // start a new thread as the current thread is excluded from |
| 87 | + // sample capturing to avoid recursion |
| 88 | + var t = startLoopingThread(); |
| 89 | + r.enable(EventNames.ExecutionSample).withPeriod(Duration.ofNanos(1)); |
| 90 | + r.onEvent("jdk.ExecutionSample", e -> { |
| 91 | + t.interrupt(); |
| 92 | + r.close(); |
| 93 | + }); |
| 94 | + r.start(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments