Skip to content

Commit

Permalink
8284686: Interval of < 1 ms disables ExecutionSample events
Browse files Browse the repository at this point in the history
Backport-of: 902b1dd4550136c520e5ec6cb8c4fe9a81218938
  • Loading branch information
parttimenerd authored and GoeLin committed Jun 14, 2022
1 parent f8d3d42 commit cb415a1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
Expand Up @@ -114,10 +114,10 @@ public void setValue(String value) {
break;
default:
long nanos = Utils.parseTimespanWithInfinity(value);
if (nanos != Long.MAX_VALUE) {
eventType.setPeriod(nanos / 1_000_000, false, false);
if (nanos == 0 || nanos == Long.MAX_VALUE) {
eventType.setPeriod(nanos, false, false);
} else {
eventType.setPeriod(Long.MAX_VALUE, false, false);
eventType.setPeriod(Math.max(1, nanos / 1_000_000), false, false);
}
}
this.value = value;
Expand Down
97 changes: 97 additions & 0 deletions test/jdk/jdk/jfr/api/recording/event/TestShortPeriod.java
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2022 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.jfr.api.recording.event;

import java.time.Duration;

import jdk.jfr.Event;
import jdk.jfr.FlightRecorder;
import jdk.jfr.consumer.RecordingStream;
import jdk.test.lib.jfr.EventNames;

/**
* @test Tests that periodic events are not disabled when using a very short
* period
* @key jfr
* @requires vm.hasJFR
* @library /test/lib /test/jdk
* @run main/othervm jdk.jfr.api.recording.event.TestShortPeriod
*/
public class TestShortPeriod {

static class PeriodicEvent extends Event {
}

private static Thread startLoopingThread() {
var t = new Thread(() -> {
while (!Thread.interrupted()) {}
});
t.start();
return t;
}

public static void main(String... args) {
testNativeEventPeriod();
testJavaEventPeriod();
testExecutionSamplePeriod();
}

private static void testNativeEventPeriod() {
try (var r = new RecordingStream()) {
r.enable(EventNames.JVMInformation).withPeriod(Duration.ofNanos(1));
r.onEvent(e -> r.close());
r.start();
}
}

private static void testJavaEventPeriod() {
Runnable hook = () -> {
PeriodicEvent e = new PeriodicEvent();
e.commit();
};
FlightRecorder.addPeriodicEvent(PeriodicEvent.class, hook);
try (var r = new RecordingStream()) {
r.enable(PeriodicEvent.class).withPeriod(Duration.ofNanos(1));
r.onEvent(e -> r.close());
r.start();
}
FlightRecorder.removePeriodicEvent(hook);
}

// The execution sample event doesn't use the standard mechanism
// for periodic events
private static void testExecutionSamplePeriod() {
try (var r = new RecordingStream()) {
// start a new thread as the current thread is excluded from
// sample capturing to avoid recursion
var t = startLoopingThread();
r.enable(EventNames.ExecutionSample).withPeriod(Duration.ofNanos(1));
r.onEvent("jdk.ExecutionSample", e -> {
t.interrupt();
r.close();
});
r.start();
}
}
}

1 comment on commit cb415a1

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.