author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 52334 | a181612f0715 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.internal; |
|
27 |
||
28 |
import java.lang.annotation.Annotation; |
|
29 |
import java.lang.reflect.Constructor; |
|
30 |
import java.lang.reflect.InvocationTargetException; |
|
31 |
import java.lang.reflect.Method; |
|
32 |
import java.lang.reflect.Modifier; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.List; |
|
36 |
||
37 |
import jdk.internal.module.Modules; |
|
38 |
import jdk.jfr.AnnotationElement; |
|
39 |
import jdk.jfr.Enabled; |
|
40 |
import jdk.jfr.Name; |
|
41 |
import jdk.jfr.Period; |
|
42 |
import jdk.jfr.SettingControl; |
|
43 |
import jdk.jfr.SettingDefinition; |
|
44 |
import jdk.jfr.StackTrace; |
|
45 |
import jdk.jfr.Threshold; |
|
46 |
import jdk.jfr.events.ActiveSettingEvent; |
|
47 |
import jdk.jfr.internal.EventInstrumentation.SettingInfo; |
|
48 |
import jdk.jfr.internal.settings.CutoffSetting; |
|
49 |
import jdk.jfr.internal.settings.EnabledSetting; |
|
50 |
import jdk.jfr.internal.settings.PeriodSetting; |
|
51 |
import jdk.jfr.internal.settings.StackTraceSetting; |
|
52 |
import jdk.jfr.internal.settings.ThresholdSetting; |
|
53 |
||
54 |
// This class can't have a hard reference from PlatformEventType, since it |
|
55 |
// holds SettingControl instances that need to be released |
|
56 |
// when a class is unloaded (to avoid memory leaks). |
|
57 |
public final class EventControl { |
|
58863 | 58 |
final static class NamedControl { |
59 |
public final String name; |
|
60 |
public final Control control; |
|
61 |
NamedControl(String name, Control control) { |
|
62 |
this.name = name; |
|
63 |
this.control = control; |
|
64 |
} |
|
65 |
} |
|
50113 | 66 |
static final String FIELD_SETTING_PREFIX = "setting"; |
67 |
private static final Type TYPE_ENABLED = TypeLibrary.createType(EnabledSetting.class); |
|
68 |
private static final Type TYPE_THRESHOLD = TypeLibrary.createType(ThresholdSetting.class); |
|
69 |
private static final Type TYPE_STACK_TRACE = TypeLibrary.createType(StackTraceSetting.class); |
|
70 |
private static final Type TYPE_PERIOD = TypeLibrary.createType(PeriodSetting.class); |
|
71 |
private static final Type TYPE_CUTOFF = TypeLibrary.createType(CutoffSetting.class); |
|
72 |
||
58863 | 73 |
private final ArrayList<SettingInfo> settingInfos = new ArrayList<>(); |
74 |
private final ArrayList<NamedControl> namedControls = new ArrayList<>(5); |
|
50113 | 75 |
private final PlatformEventType type; |
76 |
private final String idName; |
|
77 |
||
78 |
EventControl(PlatformEventType eventType) { |
|
58863 | 79 |
addControl(Enabled.NAME, defineEnabled(eventType)); |
50113 | 80 |
if (eventType.hasDuration()) { |
58863 | 81 |
addControl(Threshold.NAME, defineThreshold(eventType)); |
50113 | 82 |
} |
83 |
if (eventType.hasStackTrace()) { |
|
58863 | 84 |
addControl(StackTrace.NAME, defineStackTrace(eventType)); |
50113 | 85 |
} |
86 |
if (eventType.hasPeriod()) { |
|
58863 | 87 |
addControl(Period.NAME, definePeriod(eventType)); |
50113 | 88 |
} |
89 |
if (eventType.hasCutoff()) { |
|
58863 | 90 |
addControl(Cutoff.NAME, defineCutoff(eventType)); |
50113 | 91 |
} |
92 |
||
93 |
ArrayList<AnnotationElement> aes = new ArrayList<>(eventType.getAnnotationElements()); |
|
94 |
remove(eventType, aes, Threshold.class); |
|
95 |
remove(eventType, aes, Period.class); |
|
96 |
remove(eventType, aes, Enabled.class); |
|
97 |
remove(eventType, aes, StackTrace.class); |
|
98 |
remove(eventType, aes, Cutoff.class); |
|
99 |
aes.trimToSize(); |
|
100 |
eventType.setAnnotations(aes); |
|
101 |
this.type = eventType; |
|
102 |
this.idName = String.valueOf(eventType.getId()); |
|
103 |
} |
|
104 |
||
58863 | 105 |
private boolean hasControl(String name) { |
106 |
for (NamedControl nc : namedControls) { |
|
107 |
if (name.equals(nc.name)) { |
|
108 |
return true; |
|
109 |
} |
|
110 |
} |
|
111 |
return false; |
|
112 |
} |
|
113 |
||
114 |
private void addControl(String name, Control control) { |
|
115 |
namedControls.add(new NamedControl(name, control)); |
|
116 |
} |
|
117 |
||
50113 | 118 |
static void remove(PlatformEventType type, List<AnnotationElement> aes, Class<? extends java.lang.annotation.Annotation> clazz) { |
119 |
long id = Type.getTypeId(clazz); |
|
120 |
for (AnnotationElement a : type.getAnnotationElements()) { |
|
121 |
if (a.getTypeId() == id && a.getTypeName().equals(clazz.getName())) { |
|
122 |
aes.remove(a); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
127 |
EventControl(PlatformEventType es, Class<? extends jdk.internal.event.Event> eventClass) { |
50113 | 128 |
this(es); |
129 |
defineSettings(eventClass); |
|
130 |
} |
|
131 |
||
132 |
@SuppressWarnings("unchecked") |
|
133 |
private void defineSettings(Class<?> eventClass) { |
|
134 |
// Iterate up the class hierarchy and let |
|
135 |
// subclasses shadow base classes. |
|
136 |
boolean allowPrivateMethod = true; |
|
137 |
while (eventClass != null) { |
|
138 |
for (Method m : eventClass.getDeclaredMethods()) { |
|
139 |
boolean isPrivate = Modifier.isPrivate(m.getModifiers()); |
|
140 |
if (m.getReturnType() == Boolean.TYPE && m.getParameterCount() == 1 && (!isPrivate || allowPrivateMethod)) { |
|
141 |
SettingDefinition se = m.getDeclaredAnnotation(SettingDefinition.class); |
|
142 |
if (se != null) { |
|
143 |
Class<?> settingClass = m.getParameters()[0].getType(); |
|
144 |
if (!Modifier.isAbstract(settingClass.getModifiers()) && SettingControl.class.isAssignableFrom(settingClass)) { |
|
145 |
String name = m.getName(); |
|
146 |
Name n = m.getAnnotation(Name.class); |
|
147 |
if (n != null) { |
|
148 |
name = n.value(); |
|
149 |
} |
|
58863 | 150 |
|
151 |
if (!hasControl(name)) { |
|
50113 | 152 |
defineSetting((Class<? extends SettingControl>) settingClass, m, type, name); |
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
eventClass = eventClass.getSuperclass(); |
|
159 |
allowPrivateMethod = false; |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
private void defineSetting(Class<? extends SettingControl> settingsClass, Method method, PlatformEventType eventType, String settingName) { |
|
164 |
try { |
|
165 |
Module settingModule = settingsClass.getModule(); |
|
166 |
Modules.addReads(settingModule, EventControl.class.getModule()); |
|
167 |
int index = settingInfos.size(); |
|
168 |
SettingInfo si = new SettingInfo(FIELD_SETTING_PREFIX + index, index); |
|
169 |
si.settingControl = instantiateSettingControl(settingsClass); |
|
170 |
Control c = si.settingControl; |
|
171 |
c.setDefault(); |
|
172 |
String defaultValue = c.getValueSafe(); |
|
173 |
if (defaultValue != null) { |
|
174 |
Type settingType = TypeLibrary.createType(settingsClass); |
|
175 |
ArrayList<AnnotationElement> aes = new ArrayList<>(); |
|
176 |
for (Annotation a : method.getDeclaredAnnotations()) { |
|
177 |
AnnotationElement ae = TypeLibrary.createAnnotation(a); |
|
178 |
if (ae != null) { |
|
179 |
aes.add(ae); |
|
180 |
} |
|
181 |
} |
|
182 |
aes.trimToSize(); |
|
58863 | 183 |
addControl(settingName, si.settingControl); |
50113 | 184 |
eventType.add(PrivateAccess.getInstance().newSettingDescriptor(settingType, settingName, defaultValue, aes)); |
185 |
settingInfos.add(si); |
|
186 |
} |
|
187 |
} catch (InstantiationException e) { |
|
188 |
// Programming error by user, fail fast |
|
189 |
throw new InstantiationError("Could not instantiate setting " + settingsClass.getName() + " for event " + eventType.getLogName() + ". " + e.getMessage()); |
|
190 |
} catch (IllegalAccessException e) { |
|
191 |
// Programming error by user, fail fast |
|
192 |
throw new IllegalAccessError("Could not access setting " + settingsClass.getName() + " for event " + eventType.getLogName() + ". " + e.getMessage()); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
private SettingControl instantiateSettingControl(Class<? extends SettingControl> settingControlClass) throws IllegalAccessException, InstantiationException { |
|
197 |
SecuritySupport.makeVisibleToJFR(settingControlClass); |
|
198 |
final Constructor<?> cc; |
|
199 |
try { |
|
200 |
cc = settingControlClass.getDeclaredConstructors()[0]; |
|
201 |
} catch (Exception e) { |
|
202 |
throw (Error) new InternalError("Could not get constructor for " + settingControlClass.getName()).initCause(e); |
|
203 |
} |
|
204 |
SecuritySupport.setAccessible(cc); |
|
205 |
try { |
|
206 |
return (SettingControl) cc.newInstance(); |
|
207 |
} catch (IllegalArgumentException | InvocationTargetException e) { |
|
208 |
throw (Error) new InternalError("Could not instantiate setting for class " + settingControlClass.getName()); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
private static Control defineEnabled(PlatformEventType type) { |
|
213 |
Enabled enabled = type.getAnnotation(Enabled.class); |
|
214 |
// Java events are enabled by default, |
|
215 |
// JVM events are not, maybe they should be? Would lower learning curve |
|
216 |
// there too. |
|
217 |
String def = type.isJVM() ? "false" : "true"; |
|
218 |
if (enabled != null) { |
|
219 |
def = Boolean.toString(enabled.value()); |
|
220 |
} |
|
221 |
type.add(PrivateAccess.getInstance().newSettingDescriptor(TYPE_ENABLED, Enabled.NAME, def, Collections.emptyList())); |
|
222 |
return new EnabledSetting(type, def); |
|
223 |
} |
|
224 |
||
225 |
private static Control defineThreshold(PlatformEventType type) { |
|
226 |
Threshold threshold = type.getAnnotation(Threshold.class); |
|
227 |
String def = "0 ns"; |
|
228 |
if (threshold != null) { |
|
229 |
def = threshold.value(); |
|
230 |
} |
|
231 |
type.add(PrivateAccess.getInstance().newSettingDescriptor(TYPE_THRESHOLD, Threshold.NAME, def, Collections.emptyList())); |
|
232 |
return new ThresholdSetting(type, def); |
|
233 |
} |
|
234 |
||
235 |
private static Control defineStackTrace(PlatformEventType type) { |
|
236 |
StackTrace stackTrace = type.getAnnotation(StackTrace.class); |
|
237 |
String def = "true"; |
|
238 |
if (stackTrace != null) { |
|
239 |
def = Boolean.toString(stackTrace.value()); |
|
240 |
} |
|
241 |
type.add(PrivateAccess.getInstance().newSettingDescriptor(TYPE_STACK_TRACE, StackTrace.NAME, def, Collections.emptyList())); |
|
242 |
return new StackTraceSetting(type, def); |
|
243 |
} |
|
244 |
||
245 |
private static Control defineCutoff(PlatformEventType type) { |
|
246 |
Cutoff cutoff = type.getAnnotation(Cutoff.class); |
|
247 |
String def = Cutoff.INIFITY; |
|
248 |
if (cutoff != null) { |
|
249 |
def = cutoff.value(); |
|
250 |
} |
|
251 |
type.add(PrivateAccess.getInstance().newSettingDescriptor(TYPE_CUTOFF, Cutoff.NAME, def, Collections.emptyList())); |
|
252 |
return new CutoffSetting(type, def); |
|
253 |
} |
|
254 |
||
255 |
||
256 |
private static Control definePeriod(PlatformEventType type) { |
|
257 |
Period period = type.getAnnotation(Period.class); |
|
258 |
String def = "everyChunk"; |
|
259 |
if (period != null) { |
|
260 |
def = period.value(); |
|
261 |
} |
|
262 |
type.add(PrivateAccess.getInstance().newSettingDescriptor(TYPE_PERIOD, PeriodSetting.NAME, def, Collections.emptyList())); |
|
263 |
return new PeriodSetting(type, def); |
|
264 |
} |
|
265 |
||
266 |
void disable() { |
|
58863 | 267 |
for (NamedControl nc : namedControls) { |
268 |
if (nc.control instanceof EnabledSetting) { |
|
269 |
nc.control.setValueSafe("false"); |
|
50113 | 270 |
return; |
271 |
} |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
void writeActiveSettingEvent() { |
|
276 |
if (!type.isRegistered()) { |
|
277 |
return; |
|
278 |
} |
|
58863 | 279 |
ActiveSettingEvent event = ActiveSettingEvent.EVENT.get(); |
280 |
for (NamedControl nc : namedControls) { |
|
281 |
if (Utils.isSettingVisible(nc.control, type.hasEventHook())) { |
|
282 |
String value = nc.control.getLastValue(); |
|
50113 | 283 |
if (value == null) { |
58863 | 284 |
value = nc.control.getDefaultValue(); |
50113 | 285 |
} |
58863 | 286 |
event.id = type.getId(); |
287 |
event.name = nc.name; |
|
288 |
event.value = value; |
|
289 |
event.commit(); |
|
50113 | 290 |
} |
291 |
} |
|
292 |
} |
|
293 |
||
58863 | 294 |
public ArrayList<NamedControl> getNamedControls() { |
295 |
return namedControls; |
|
50113 | 296 |
} |
297 |
||
298 |
public PlatformEventType getEventType() { |
|
299 |
return type; |
|
300 |
} |
|
301 |
||
302 |
public String getSettingsId() { |
|
303 |
return idName; |
|
304 |
} |
|
305 |
||
306 |
public List<SettingInfo> getSettingInfos() { |
|
307 |
return settingInfos; |
|
308 |
} |
|
309 |
} |