author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 52413 | 6372f5af9612 |
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.util.ArrayList; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.Collections; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.HashSet; |
|
33 |
import java.util.LinkedHashMap; |
|
34 |
import java.util.List; |
|
35 |
import java.util.Map; |
|
36 |
import java.util.Set; |
|
37 |
import java.util.StringJoiner; |
|
38 |
||
58863 | 39 |
import jdk.jfr.internal.EventControl.NamedControl; |
50113 | 40 |
import jdk.jfr.internal.handlers.EventHandler; |
41 |
||
42 |
final class SettingsManager { |
|
43 |
||
44 |
private static class InternalSetting { |
|
45 |
||
46 |
private final String identifier; |
|
47 |
private Map<String, Set<String>> enabledMap = new LinkedHashMap<>(5); |
|
48 |
private Map<String, Set<String>> allMap = new LinkedHashMap<>(5); |
|
49 |
private boolean enabled; |
|
50 |
||
51 |
/** |
|
52 |
* Settings identifier, for example "com.example.HelloWorld" or "56" |
|
53 |
* (id of event) |
|
54 |
* |
|
55 |
* @param settingsId |
|
56 |
*/ |
|
57 |
public InternalSetting(String settingsId) { |
|
58 |
this.identifier = settingsId; |
|
59 |
} |
|
60 |
||
61 |
public Set<String> getValues(String key) { |
|
62 |
if (enabled) { |
|
63 |
return enabledMap.get(key); |
|
64 |
} else { |
|
65 |
return allMap.get(key); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
public void add(String attribute, String value) { |
|
70 |
if ("enabled".equals(attribute) && "true".equals(value)) { |
|
71 |
enabled = true; |
|
72 |
allMap = null; // no need to keep these around |
|
73 |
} |
|
74 |
addToMap(enabledMap, attribute, value); |
|
75 |
if (allMap != null) { |
|
76 |
addToMap(allMap, attribute, value); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
private void addToMap(Map<String, Set<String>> map, String attribute, String value) { |
|
81 |
Set<String> values = map.get(attribute); |
|
82 |
if (values == null) { |
|
83 |
values = new HashSet<String>(5); |
|
84 |
map.put(attribute, values); |
|
85 |
} |
|
86 |
values.add(value); |
|
87 |
||
88 |
} |
|
89 |
||
90 |
public String getSettingsId() { |
|
91 |
return identifier; |
|
92 |
} |
|
93 |
||
94 |
public void add(InternalSetting enabled) { |
|
95 |
for (Map.Entry<String, Set<String>> entry : enabled.enabledMap.entrySet()) { |
|
96 |
for (String value : entry.getValue()) { |
|
97 |
add(entry.getKey(), value); |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
public boolean isEnabled() { |
|
103 |
return enabled; |
|
104 |
} |
|
105 |
||
106 |
@Override |
|
107 |
public String toString() { |
|
108 |
StringBuilder sb = new StringBuilder(); |
|
109 |
sb.append(identifier); |
|
110 |
sb.append(": "); |
|
111 |
sb.append(enabledMap.toString()); |
|
112 |
return sb.toString(); |
|
113 |
} |
|
114 |
||
115 |
public void finish() { |
|
116 |
if (!enabled) { |
|
117 |
// settings from disabled |
|
118 |
// events should not impact results, but |
|
119 |
// we can't clear enabledMap since enabled=false |
|
120 |
// needs be there, so events that are enabled |
|
121 |
// by default are turned off |
|
122 |
Map<String, Set<String>> disabledMap = new HashMap<>(2); |
|
123 |
Set<String> values = new HashSet<>(2); |
|
124 |
values.add("false"); |
|
125 |
disabledMap.put("enabled", values); |
|
126 |
enabledMap = disabledMap; |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
private Map<String, InternalSetting> availableSettings = new LinkedHashMap<>(); |
|
132 |
||
133 |
void setSettings(List<Map<String, String>> activeSettings) { |
|
134 |
// store settings so they are available if a new event class is loaded |
|
135 |
availableSettings = createSettingsMap(activeSettings); |
|
136 |
List<EventControl> eventControls = MetadataRepository.getInstance().getEventControls(); |
|
137 |
if (!JVM.getJVM().isRecording()) { |
|
138 |
for (EventControl ec : eventControls) { |
|
139 |
ec.disable(); |
|
140 |
} |
|
141 |
} else { |
|
52413 | 142 |
if (Logger.shouldLog(LogTag.JFR_SETTING, LogLevel.INFO)) { |
50113 | 143 |
Collections.sort(eventControls, (x,y) -> x.getEventType().getName().compareTo(y.getEventType().getName())); |
144 |
} |
|
145 |
for (EventControl ec : eventControls) { |
|
146 |
setEventControl(ec); |
|
147 |
} |
|
148 |
} |
|
149 |
if (JVM.getJVM().getAllowedToDoEventRetransforms()) { |
|
150 |
updateRetransform(JVM.getJVM().getAllEventClasses()); |
|
151 |
} |
|
152 |
} |
|
153 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
154 |
public void updateRetransform(List<Class<? extends jdk.internal.event.Event>> eventClasses) { |
50113 | 155 |
List<Class<?>> classes = new ArrayList<>(); |
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
156 |
for(Class<? extends jdk.internal.event.Event> eventClass: eventClasses) { |
50113 | 157 |
EventHandler eh = Utils.getHandler(eventClass); |
158 |
if (eh != null ) { |
|
159 |
PlatformEventType eventType = eh.getPlatformEventType(); |
|
160 |
if (eventType.isMarkedForInstrumentation()) { |
|
161 |
classes.add(eventClass); |
|
162 |
eventType.markForInstrumentation(false); |
|
163 |
// A bit premature to set it here, but hard to check |
|
164 |
// after call to retransformClasses. |
|
165 |
eventType.setInstrumented(); |
|
166 |
} |
|
167 |
} |
|
168 |
} |
|
169 |
if (!classes.isEmpty()) { |
|
170 |
JVM.getJVM().retransformClasses(classes.toArray(new Class<?>[0])); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
private Map<String, InternalSetting> createSettingsMap(List<Map<String,String>> activeSettings) { |
|
175 |
Map<String, InternalSetting> map = new LinkedHashMap<>(activeSettings.size()); |
|
176 |
for (Map<String, String> rec : activeSettings) { |
|
177 |
for (InternalSetting internal : makeInternalSettings(rec)) { |
|
178 |
InternalSetting is = map.get(internal.getSettingsId()); |
|
179 |
if (is == null) { |
|
180 |
map.put(internal.getSettingsId(), internal); |
|
181 |
} else { |
|
182 |
is.add(internal); |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
return map; |
|
187 |
} |
|
188 |
||
189 |
private Collection<InternalSetting> makeInternalSettings(Map<String, String> rec) { |
|
190 |
Map<String, InternalSetting> internals = new LinkedHashMap<>(); |
|
191 |
for (Map.Entry<String, String> entry : rec.entrySet()) { |
|
192 |
String key = entry.getKey(); |
|
193 |
String value = entry.getValue(); |
|
194 |
int index = key.indexOf("#"); |
|
195 |
if (index > 1 && index < key.length() - 2) { |
|
196 |
String eventName = key.substring(0, index); |
|
197 |
eventName = Utils.upgradeLegacyJDKEvent(eventName); |
|
198 |
InternalSetting s = internals.get(eventName); |
|
199 |
String settingName = key.substring(index + 1).trim(); |
|
200 |
if (s == null) { |
|
201 |
s = new InternalSetting(eventName); |
|
202 |
internals.put(eventName, s); |
|
203 |
} |
|
204 |
s.add(settingName, value); |
|
205 |
} |
|
206 |
} |
|
207 |
for (InternalSetting s : internals.values()) { |
|
208 |
s.finish(); |
|
209 |
} |
|
210 |
||
211 |
return internals.values(); |
|
212 |
} |
|
213 |
||
214 |
void setEventControl(EventControl ec) { |
|
215 |
InternalSetting is = getInternalSetting(ec); |
|
58863 | 216 |
boolean shouldLog = Logger.shouldLog(LogTag.JFR_SETTING, LogLevel.INFO); |
217 |
if (shouldLog) { |
|
218 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, "Applied settings for " + ec.getEventType().getLogName() + " {"); |
|
219 |
} |
|
220 |
for (NamedControl nc: ec.getNamedControls()) { |
|
50113 | 221 |
Set<String> values = null; |
58863 | 222 |
String settingName = nc.name; |
50113 | 223 |
if (is != null) { |
224 |
values = is.getValues(settingName); |
|
225 |
} |
|
58863 | 226 |
Control control = nc.control; |
50113 | 227 |
if (values != null) { |
228 |
control.apply(values); |
|
229 |
String after = control.getLastValue(); |
|
58863 | 230 |
if (shouldLog) { |
50113 | 231 |
if (Utils.isSettingVisible(control, ec.getEventType().hasEventHook())) { |
232 |
if (values.size() > 1) { |
|
233 |
StringJoiner sj = new StringJoiner(", ", "{", "}"); |
|
234 |
for (String s : values) { |
|
235 |
sj.add("\"" + s + "\""); |
|
236 |
} |
|
237 |
String message = " " + settingName + "= " + sj.toString() + " => \"" + after + "\""; |
|
238 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, message); |
|
239 |
} else { |
|
240 |
String message = " " + settingName + "=\"" + control.getLastValue() + "\""; |
|
241 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, message); |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
} else { |
|
246 |
control.setDefault(); |
|
58863 | 247 |
if (shouldLog) { |
50113 | 248 |
String message = " " + settingName + "=\"" + control.getLastValue() + "\""; |
249 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, message); |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
ec.writeActiveSettingEvent(); |
|
58863 | 254 |
if (shouldLog) { |
255 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, "}"); |
|
256 |
} |
|
50113 | 257 |
} |
258 |
||
259 |
private InternalSetting getInternalSetting(EventControl ec) { |
|
260 |
String name = ec.getEventType().getName(); |
|
261 |
InternalSetting nameBased = availableSettings.get(name); |
|
262 |
InternalSetting idBased = availableSettings.get(ec.getSettingsId()); |
|
263 |
||
264 |
if (nameBased == null && idBased == null) { |
|
265 |
return null; |
|
266 |
} |
|
267 |
if (idBased == null) { |
|
268 |
return nameBased; |
|
269 |
} |
|
270 |
if (nameBased == null) { |
|
271 |
return idBased; |
|
272 |
} |
|
273 |
InternalSetting mixed = new InternalSetting(nameBased.getSettingsId()); |
|
274 |
mixed.add(nameBased); |
|
275 |
mixed.add(idBased); |
|
276 |
return mixed; |
|
277 |
} |
|
278 |
||
279 |
@Override |
|
280 |
public String toString() { |
|
281 |
StringBuilder sb = new StringBuilder(); |
|
282 |
for (InternalSetting enabled : availableSettings.values()) { |
|
283 |
sb.append(enabled.toString()); |
|
284 |
sb.append("\n"); |
|
285 |
} |
|
286 |
return sb.toString(); |
|
287 |
} |
|
288 |
||
289 |
boolean isEnabled(String eventName) { |
|
290 |
InternalSetting is = availableSettings.get(eventName); |
|
291 |
if (is == null) { |
|
292 |
return false; |
|
293 |
} |
|
294 |
return is.isEnabled(); |
|
295 |
} |
|
296 |
} |