Skip to content

Commit 38fd794

Browse files
committed
8364190: JFR: RemoteRecordingStream withers don't work
Reviewed-by: mgronlun Backport-of: da0d9598d049b17c04da95b61214b093c97fb60e
1 parent 702e30e commit 38fd794

File tree

3 files changed

+151
-8
lines changed

3 files changed

+151
-8
lines changed

src/jdk.management.jfr/share/classes/jdk/management/jfr/RemoteRecordingStream.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ static final class RemoteSettings implements EventSettingsModifier {
9898

9999
private final FlightRecorderMXBean mbean;
100100
private final long recordingId;
101+
private final String identifier;
101102

102-
RemoteSettings(FlightRecorderMXBean mbean, long recordingId) {
103+
RemoteSettings(FlightRecorderMXBean mbean, long recordingId, String identifier) {
103104
this.mbean = mbean;
104105
this.recordingId = recordingId;
106+
this.identifier = identifier;
105107
}
106108

107109
@Override
@@ -111,7 +113,7 @@ public void with(String name, String value) {
111113
// FlightRecorderMXBean implementation always returns
112114
// new instance of Map so no need to create new here.
113115
Map<String, String> newSettings = getEventSettings();
114-
newSettings.put(name, value);
116+
newSettings.put(identifier + "#" + name, value);
115117
mbean.setRecordingSettings(recordingId, newSettings);
116118
}
117119

@@ -340,9 +342,9 @@ public void setSettings(Map<String, String> settings) {
340342
*/
341343
public EventSettings disable(String name) {
342344
Objects.requireNonNull(name, "name");
343-
EventSettings s = ManagementSupport.newEventSettings(new RemoteSettings(mbean, recordingId));
345+
EventSettings s = ManagementSupport.newEventSettings(new RemoteSettings(mbean, recordingId, name));
344346
try {
345-
return s.with(name + "#" + ENABLED, "false");
347+
return s.with(ENABLED, "false");
346348
} catch (Exception e) {
347349
ManagementSupport.logDebug(e.getMessage());
348350
close();
@@ -364,9 +366,9 @@ public EventSettings disable(String name) {
364366
*/
365367
public EventSettings enable(String name) {
366368
Objects.requireNonNull(name, "name");
367-
EventSettings s = ManagementSupport.newEventSettings(new RemoteSettings(mbean, recordingId));
369+
EventSettings s = ManagementSupport.newEventSettings(new RemoteSettings(mbean, recordingId, name));
368370
try {
369-
return s.with(name + "#" + ENABLED, "true");
371+
return s.with(ENABLED, "true");
370372
} catch (Exception e) {
371373
ManagementSupport.logDebug(e.getMessage());
372374
close();

test/jdk/jdk/jfr/jmx/streaming/TestEnableDisable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,7 +36,7 @@
3636
/**
3737
* @test
3838
* @requires vm.flagless
39-
* @summary Tests that event settings for a RemoteRecordingStream can be changed
39+
* @summary Tests that the enabled setting can be configured for a RemoteRecordingStream
4040
* @requires vm.hasJFR
4141
* @library /test/lib /test/jdk
4242
* @run main/othervm jdk.jfr.jmx.streaming.TestEnableDisable
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. 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+
package jdk.jfr.jmx.streaming;
24+
25+
import java.lang.management.ManagementFactory;
26+
import java.time.Duration;
27+
import java.util.Collections;
28+
import java.util.HashSet;
29+
import java.util.Set;
30+
import java.util.function.Consumer;
31+
import java.util.function.Predicate;
32+
33+
import javax.management.MBeanServerConnection;
34+
35+
import jdk.jfr.Event;
36+
import jdk.jfr.EventSettings;
37+
import jdk.jfr.FlightRecorder;
38+
import jdk.jfr.Name;
39+
import jdk.jfr.Period;
40+
import jdk.jfr.StackTrace;
41+
import jdk.jfr.Threshold;
42+
import jdk.jfr.consumer.RecordedEvent;
43+
import jdk.jfr.consumer.RecordedStackTrace;
44+
import jdk.management.jfr.RemoteRecordingStream;
45+
46+
/**
47+
* @test
48+
* @requires vm.flagless
49+
* @summary Tests that event settings for a RemoteRecordingStream can be changed
50+
* @requires vm.hasJFR
51+
* @library /test/lib /test/jdk
52+
* @run main/othervm jdk.jfr.jmx.streaming.TestWithers
53+
*/
54+
public class TestWithers {
55+
private static final Set<String> RESULT = Collections.synchronizedSet(new HashSet<>());
56+
57+
@Name("AA")
58+
@StackTrace(false)
59+
static class A extends Event {
60+
}
61+
62+
@Name("BB")
63+
@StackTrace(true)
64+
static class B extends Event {
65+
}
66+
67+
@Name("CC")
68+
@Threshold("10 h")
69+
static class C extends Event {
70+
}
71+
72+
@Name("DD")
73+
@Threshold("10 h")
74+
static class D extends Event {
75+
}
76+
77+
@Name("EE")
78+
@StackTrace(false)
79+
static class E extends Event {
80+
}
81+
82+
@Name("FF")
83+
@Period("10 h")
84+
static class F extends Event {
85+
}
86+
87+
public static void main(String... args) throws Exception {
88+
MBeanServerConnection conn = ManagementFactory.getPlatformMBeanServer();
89+
try (RemoteRecordingStream stream = new RemoteRecordingStream(conn)) {
90+
addCheck(stream, es -> es.withStackTrace(), "AA", TestWithers::hasStackTrace);
91+
addCheck(stream, es -> es.withoutStackTrace(), "BB", e -> !hasStackTrace(e));
92+
addCheck(stream, es -> es.withThreshold(Duration.ofMillis(0)), "CC", e -> true);
93+
addCheck(stream, es -> es.withoutThreshold(), "DD", e -> true);
94+
addCheck(stream, es -> es.with("stackTrace", "true"), "EE", TestWithers::hasStackTrace);
95+
addCheck(stream, es -> es.withPeriod(Duration.ofMillis(700)), "FF", e -> true);
96+
FlightRecorder.addPeriodicEvent(F.class, () -> {
97+
F f = new F();
98+
f.commit();
99+
});
100+
stream.onFlush(() -> {
101+
System.out.println(RESULT);
102+
if (RESULT.size() == 6) {
103+
stream.close();
104+
}
105+
});
106+
107+
stream.startAsync();
108+
A a = new A();
109+
a.commit();
110+
111+
B b = new B();
112+
b.commit();
113+
114+
C c = new C();
115+
c.commit();
116+
117+
D d = new D();
118+
d.commit();
119+
120+
E e = new E();
121+
e.commit();
122+
123+
stream.awaitTermination();
124+
}
125+
}
126+
127+
private static void addCheck(RemoteRecordingStream stream, Consumer<EventSettings> es, String eventName, Predicate<RecordedEvent> validator) {
128+
es.accept(stream.enable(eventName));
129+
stream.onEvent(eventName, e -> {
130+
System.out.println(e);
131+
if (validator.test(e)) {
132+
RESULT.add(eventName);
133+
}
134+
});
135+
}
136+
137+
private static boolean hasStackTrace(RecordedEvent e) {
138+
RecordedStackTrace rs = e.getStackTrace();
139+
return rs != null && !rs.getFrames().isEmpty();
140+
}
141+
}

0 commit comments

Comments
 (0)