Skip to content

Commit b263292

Browse files
committed
8353484: JFR: Simplify EventConfiguration
Reviewed-by: mgronlun
1 parent ffca4f2 commit b263292

File tree

4 files changed

+26
-70
lines changed

4 files changed

+26
-70
lines changed

src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataRepository.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import jdk.jfr.EventType;
4545
import jdk.jfr.Name;
4646
import jdk.jfr.Period;
47+
import jdk.jfr.SettingControl;
4748
import jdk.jfr.ValueDescriptor;
4849
import jdk.jfr.internal.consumer.RepositoryFiles;
4950
import jdk.jfr.internal.event.EventConfiguration;
@@ -59,7 +60,6 @@ public final class MetadataRepository {
5960
private final Map<String, EventControl> nativeControls = LinkedHashMap.newHashMap(150);
6061
private final SettingsManager settingsManager = new SettingsManager();
6162
private final HiddenWait threadSleeper = new HiddenWait();
62-
private Constructor<EventConfiguration> cachedEventConfigurationConstructor;
6363
private boolean staleMetadata = true;
6464
private boolean unregistered;
6565
private long lastUnloaded = -1;
@@ -103,7 +103,7 @@ public synchronized List<EventType> getRegisteredEventTypes() {
103103
List<EventType> eventTypes = new ArrayList<>(configurations.size() + nativeEventTypes.size());
104104
for (EventConfiguration ec : configurations) {
105105
if (ec.isRegistered()) {
106-
eventTypes.add(ec.getEventType());
106+
eventTypes.add(ec.eventType());
107107
}
108108
}
109109
for (EventType t : nativeEventTypes.values()) {
@@ -117,15 +117,15 @@ public synchronized List<EventType> getRegisteredEventTypes() {
117117
public synchronized EventType getEventType(Class<? extends jdk.internal.event.Event> eventClass) {
118118
EventConfiguration ec = getConfiguration(eventClass, false);
119119
if (ec != null && ec.isRegistered()) {
120-
return ec.getEventType();
120+
return ec.eventType();
121121
}
122122
throw new IllegalStateException("Event class " + eventClass.getName() + " is not registered");
123123
}
124124

125125
public synchronized void unregister(Class<? extends Event> eventClass) {
126126
EventConfiguration configuration = getConfiguration(eventClass, false);
127127
if (configuration != null) {
128-
configuration.getPlatformEventType().setRegistered(false);
128+
configuration.platformEventType().setRegistered(false);
129129
}
130130
// never registered, ignore call
131131
}
@@ -147,14 +147,14 @@ public synchronized EventType register(Class<? extends jdk.internal.event.Event>
147147
PlatformEventType pe = findMirrorType(eventClass);
148148
configuration = makeConfiguration(eventClass, pe, dynamicAnnotations, dynamicFields);
149149
}
150-
configuration.getPlatformEventType().setRegistered(true);
151-
TypeLibrary.addType(configuration.getPlatformEventType());
150+
configuration.platformEventType().setRegistered(true);
151+
TypeLibrary.addType(configuration.platformEventType());
152152
if (JVM.isRecording()) {
153-
settingsManager.setEventControl(configuration.getEventControl(), true, JVM.counterTime());
153+
settingsManager.setEventControl(configuration.eventControl(), true, JVM.counterTime());
154154
settingsManager.updateRetransform(Collections.singletonList((eventClass)));
155155
}
156156
setStaleMetadata();
157-
return configuration.getEventType();
157+
return configuration.eventType();
158158
}
159159

160160
private PlatformEventType findMirrorType(Class<? extends jdk.internal.event.Event> eventClass) throws InternalError {
@@ -179,20 +179,6 @@ private EventConfiguration getConfiguration(Class<? extends jdk.internal.event.E
179179
return JVMSupport.getConfiguration(eventClass);
180180
}
181181

182-
private EventConfiguration newEventConfiguration(EventType eventType, EventControl ec) {
183-
try {
184-
if (cachedEventConfigurationConstructor == null) {
185-
var argClasses = new Class<?>[] { EventType.class, EventControl.class};
186-
Constructor<EventConfiguration> c = EventConfiguration.class.getDeclaredConstructor(argClasses);
187-
c.setAccessible(true);
188-
cachedEventConfigurationConstructor = c;
189-
}
190-
return cachedEventConfigurationConstructor.newInstance(eventType, ec);
191-
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
192-
throw new InternalError(e);
193-
}
194-
}
195-
196182
private EventConfiguration makeConfiguration(Class<? extends jdk.internal.event.Event> eventClass, PlatformEventType pEventType, List<AnnotationElement> dynamicAnnotations, List<ValueDescriptor> dynamicFields) throws InternalError {
197183
SecuritySupport.addInternalEventExport(eventClass);
198184
if (pEventType == null) {
@@ -226,12 +212,12 @@ private EventConfiguration makeConfiguration(Class<? extends jdk.internal.event.
226212
}
227213
EventType eventType = PrivateAccess.getInstance().newEventType(pEventType);
228214
EventControl ec = new EventControl(pEventType, eventClass);
229-
EventConfiguration configuration = newEventConfiguration(eventType, ec);
230-
PlatformEventType pe = configuration.getPlatformEventType();
231-
pe.setRegistered(true);
215+
SettingControl[] settings = ec.getSettingControls().toArray(new SettingControl[0]);
216+
EventConfiguration configuration = new EventConfiguration(pEventType, eventType, ec, settings, eventType.getId());
217+
pEventType.setRegistered(true);
232218
// If class is instrumented or should not be instrumented, mark as instrumented.
233-
if (JVM.isInstrumented(eventClass) || !JVMSupport.shouldInstrument(pe.isJDK(), pe.getName())) {
234-
pe.setInstrumented();
219+
if (JVM.isInstrumented(eventClass) || !JVMSupport.shouldInstrument(pEventType.isJDK(), pEventType.getName())) {
220+
pEventType.setInstrumented();
235221
}
236222
JVMSupport.setConfiguration(eventClass, configuration);
237223
return configuration;
@@ -254,7 +240,7 @@ public synchronized List<EventControl> getEventControls() {
254240
for (Class<? extends jdk.internal.event.Event> clazz : eventClasses) {
255241
EventConfiguration eh = JVMSupport.getConfiguration(clazz);
256242
if (eh != null) {
257-
controls.add(eh.getEventControl());
243+
controls.add(eh.eventControl());
258244
}
259245
}
260246
return controls;

src/jdk.jfr/share/classes/jdk/jfr/internal/SettingsManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -158,7 +158,7 @@ public void updateRetransform(List<Class<? extends jdk.internal.event.Event>> ev
158158
for(Class<? extends jdk.internal.event.Event> eventClass: eventClasses) {
159159
EventConfiguration ec = JVMSupport.getConfiguration(eventClass);
160160
if (ec != null ) {
161-
PlatformEventType eventType = ec.getPlatformEventType();
161+
PlatformEventType eventType = ec.platformEventType();
162162
if (eventType.isMarkedForInstrumentation()) {
163163
classes.add(eventClass);
164164
eventType.markForInstrumentation(false);

src/jdk.jfr/share/classes/jdk/jfr/internal/event/EventConfiguration.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -26,38 +26,17 @@
2626
package jdk.jfr.internal.event;
2727

2828
import jdk.jfr.EventType;
29+
import jdk.jfr.SettingControl;
2930
import jdk.jfr.internal.EventControl;
3031
import jdk.jfr.internal.JVM;
3132
import jdk.jfr.internal.PlatformEventType;
32-
import jdk.jfr.internal.PrivateAccess;
33-
import jdk.jfr.SettingControl;
3433

35-
// Users should not be able to subclass or instantiate for security reasons.
36-
public final class EventConfiguration {
37-
private final PlatformEventType platformEventType;
38-
private final EventType eventType;
39-
private final EventControl eventControl;
40-
private final SettingControl[] settings;
41-
42-
// Private constructor so user code can't instantiate
43-
private EventConfiguration(EventType eventType, EventControl eventControl) {
44-
this.eventType = eventType;
45-
this.platformEventType = PrivateAccess.getInstance().getPlatformEventType(eventType);
46-
this.eventControl = eventControl;
47-
this.settings = eventControl.getSettingControls().toArray(new SettingControl[0]);
48-
}
49-
50-
// Class jdk.jfr.internal.PlatformEventType is not
51-
// accessible from event class by design
52-
public PlatformEventType getPlatformEventType() {
53-
return platformEventType;
54-
}
55-
56-
// Class jdk.jfr.internal.EventControl is not
57-
// accessible from event class by design
58-
public EventControl getEventControl() {
59-
return eventControl;
60-
}
34+
public record EventConfiguration(
35+
PlatformEventType platformEventType,
36+
EventType eventType,
37+
EventControl eventControl,
38+
SettingControl[] settings,
39+
long id) {
6140

6241
// Accessed by generated code in event class
6342
public boolean shouldCommit(long duration) {
@@ -74,11 +53,6 @@ public boolean isEnabled() {
7453
return platformEventType.isCommittable();
7554
}
7655

77-
// Accessed by generated code in event class
78-
public EventType getEventType() {
79-
return eventType;
80-
}
81-
8256
// Not really part of the configuration, but the method
8357
// needs to be somewhere the event class can access,
8458
// but not part of the public API.
@@ -100,8 +74,4 @@ public static long duration(long startTime) {
10074
public boolean isRegistered() {
10175
return platformEventType.isRegistered();
10276
}
103-
104-
public long getId() {
105-
return eventType.getId();
106-
}
10777
}

src/jdk.jfr/share/classes/jdk/jfr/internal/event/EventWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ private void flush(int usedSize, int requestedSize) {
234234

235235
public boolean beginEvent(EventConfiguration configuration, long typeId) {
236236
// This check makes sure the event type matches what was added by instrumentation.
237-
if (configuration.getId() != typeId) {
237+
if (configuration.id() != typeId) {
238238
throw new InternalError("Unexpected type id " + typeId);
239239
}
240240
if (excluded) {
241241
// thread is excluded from writing events
242242
return false;
243243
}
244-
this.eventType = configuration.getPlatformEventType();
244+
this.eventType = configuration.platformEventType();
245245
reserveEventSizeField();
246246
putLong(eventType.getId());
247247
return true;

0 commit comments

Comments
 (0)