Skip to content

Commit cb415a1

Browse files
parttimenerdGoeLin
authored andcommitted
8284686: Interval of < 1 ms disables ExecutionSample events
Backport-of: 902b1dd4550136c520e5ec6cb8c4fe9a81218938
1 parent f8d3d42 commit cb415a1

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/jdk.jfr/share/classes/jdk/jfr/internal/settings/PeriodSetting.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ public void setValue(String value) {
114114
break;
115115
default:
116116
long nanos = Utils.parseTimespanWithInfinity(value);
117-
if (nanos != Long.MAX_VALUE) {
118-
eventType.setPeriod(nanos / 1_000_000, false, false);
117+
if (nanos == 0 || nanos == Long.MAX_VALUE) {
118+
eventType.setPeriod(nanos, false, false);
119119
} else {
120-
eventType.setPeriod(Long.MAX_VALUE, false, false);
120+
eventType.setPeriod(Math.max(1, nanos / 1_000_000), false, false);
121121
}
122122
}
123123
this.value = value;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)