author | egahlin |
Wed, 05 Dec 2018 16:40:12 +0100 | |
changeset 52850 | f527b24990d7 |
parent 52334 | a181612f0715 |
child 52901 | 3ba9ff4d4aaf |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
2 |
* Copyright (c) 2016, 2018, 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. 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 static java.util.concurrent.TimeUnit.MICROSECONDS; |
|
29 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
30 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
|
31 |
import static java.util.concurrent.TimeUnit.SECONDS; |
|
32 |
||
33 |
import java.io.FileOutputStream; |
|
34 |
import java.io.FileWriter; |
|
35 |
import java.io.IOException; |
|
36 |
import java.io.PrintWriter; |
|
37 |
import java.io.RandomAccessFile; |
|
38 |
import java.lang.annotation.Annotation; |
|
39 |
import java.lang.annotation.Repeatable; |
|
40 |
import java.lang.reflect.Field; |
|
41 |
import java.lang.reflect.InvocationTargetException; |
|
42 |
import java.lang.reflect.Method; |
|
43 |
import java.lang.reflect.Modifier; |
|
44 |
import java.nio.file.Path; |
|
45 |
import java.time.Duration; |
|
50745 | 46 |
import java.time.LocalDateTime; |
50113 | 47 |
import java.util.ArrayList; |
48 |
import java.util.Arrays; |
|
49 |
import java.util.Collections; |
|
50 |
import java.util.HashMap; |
|
51 |
import java.util.List; |
|
52 |
import java.util.Map; |
|
53 |
import java.util.Objects; |
|
54 |
||
55 |
import jdk.internal.org.objectweb.asm.ClassReader; |
|
56 |
import jdk.internal.org.objectweb.asm.util.CheckClassAdapter; |
|
57 |
import jdk.jfr.Event; |
|
58 |
import jdk.jfr.FlightRecorderPermission; |
|
50745 | 59 |
import jdk.jfr.Recording; |
50113 | 60 |
import jdk.jfr.RecordingState; |
61 |
import jdk.jfr.internal.handlers.EventHandler; |
|
62 |
import jdk.jfr.internal.settings.PeriodSetting; |
|
63 |
import jdk.jfr.internal.settings.StackTraceSetting; |
|
64 |
import jdk.jfr.internal.settings.ThresholdSetting; |
|
65 |
||
66 |
public final class Utils { |
|
67 |
||
68 |
private static Boolean SAVE_GENERATED; |
|
69 |
||
70 |
public static final String EVENTS_PACKAGE_NAME = "jdk.jfr.events"; |
|
71 |
public static final String INSTRUMENT_PACKAGE_NAME = "jdk.jfr.internal.instrument"; |
|
72 |
public static final String HANDLERS_PACKAGE_NAME = "jdk.jfr.internal.handlers"; |
|
73 |
public static final String REGISTER_EVENT = "registerEvent"; |
|
74 |
public static final String ACCESS_FLIGHT_RECORDER = "accessFlightRecorder"; |
|
75 |
||
76 |
private final static String LEGACY_EVENT_NAME_PREFIX = "com.oracle.jdk."; |
|
77 |
||
78 |
public static void checkAccessFlightRecorder() throws SecurityException { |
|
79 |
SecurityManager sm = System.getSecurityManager(); |
|
80 |
if (sm != null) { |
|
81 |
sm.checkPermission(new FlightRecorderPermission(ACCESS_FLIGHT_RECORDER)); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
public static void checkRegisterPermission() throws SecurityException { |
|
86 |
SecurityManager sm = System.getSecurityManager(); |
|
87 |
if (sm != null) { |
|
88 |
sm.checkPermission(new FlightRecorderPermission(REGISTER_EVENT)); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
private static enum TimespanUnit { |
|
93 |
NANOSECONDS("ns", 1000), MICROSECONDS("us", 1000), MILLISECONDS("ms", 1000), SECONDS("s", 60), MINUTES("m", 60), HOURS("h", 24), DAYS("d", 7); |
|
94 |
||
95 |
final String text; |
|
96 |
final long amount; |
|
97 |
||
98 |
TimespanUnit(String unit, long amount) { |
|
99 |
this.text = unit; |
|
100 |
this.amount = amount; |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
public static String formatBytes(long bytes, String separation) { |
|
52850 | 105 |
if (bytes == 1) { |
106 |
return "1 byte"; |
|
107 |
} |
|
50113 | 108 |
if (bytes < 1024) { |
109 |
return bytes + " bytes"; |
|
110 |
} |
|
111 |
int exp = (int) (Math.log(bytes) / Math.log(1024)); |
|
112 |
char bytePrefix = "kMGTPE".charAt(exp - 1); |
|
113 |
return String.format("%.1f%s%cB", bytes / Math.pow(1024, exp), separation, bytePrefix); |
|
114 |
} |
|
115 |
||
116 |
public static String formatTimespan(Duration dValue, String separation) { |
|
117 |
if (dValue == null) { |
|
118 |
return "0"; |
|
119 |
} |
|
120 |
||
121 |
long value = dValue.toNanos(); |
|
122 |
TimespanUnit result = TimespanUnit.NANOSECONDS; |
|
123 |
for (TimespanUnit unit : TimespanUnit.values()) { |
|
124 |
result = unit; |
|
125 |
long amount = unit.amount; |
|
126 |
if (result == TimespanUnit.DAYS || value < amount || value % amount != 0) { |
|
127 |
break; |
|
128 |
} |
|
129 |
value /= amount; |
|
130 |
} |
|
131 |
return String.format("%d%s%s", value, separation, result.text); |
|
132 |
} |
|
133 |
||
134 |
public static long parseTimespan(String s) { |
|
135 |
if (s.endsWith("ns")) { |
|
136 |
return Long.parseLong(s.substring(0, s.length() - 2).trim()); |
|
137 |
} |
|
138 |
if (s.endsWith("us")) { |
|
139 |
return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MICROSECONDS); |
|
140 |
} |
|
141 |
if (s.endsWith("ms")) { |
|
142 |
return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MILLISECONDS); |
|
143 |
} |
|
144 |
if (s.endsWith("s")) { |
|
145 |
return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS); |
|
146 |
} |
|
147 |
if (s.endsWith("m")) { |
|
148 |
return 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS); |
|
149 |
} |
|
150 |
if (s.endsWith("h")) { |
|
151 |
return 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS); |
|
152 |
} |
|
153 |
if (s.endsWith("d")) { |
|
154 |
return 24 * 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS); |
|
155 |
} |
|
156 |
||
157 |
try { |
|
158 |
Long.parseLong(s); |
|
159 |
} catch (NumberFormatException nfe) { |
|
160 |
throw new NumberFormatException("'" + s + "' is not a valid timespan. Shoule be numeric value followed by a unit, i.e. 20 ms. Valid units are ns, us, s, m, h and d."); |
|
161 |
} |
|
162 |
// Only accept values with units |
|
163 |
throw new NumberFormatException("Timespan + '" + s + "' is missing unit. Valid units are ns, us, s, m, h and d."); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Return all annotations as they are visible in the source code |
|
168 |
* |
|
169 |
* @param clazz class to return annotations from |
|
170 |
* |
|
171 |
* @return list of annotation |
|
172 |
* |
|
173 |
*/ |
|
174 |
static List<Annotation> getAnnotations(Class<?> clazz) { |
|
175 |
List<Annotation> annos = new ArrayList<>(); |
|
176 |
for (Annotation a : clazz.getAnnotations()) { |
|
177 |
annos.addAll(getAnnotation(a)); |
|
178 |
} |
|
179 |
return annos; |
|
180 |
} |
|
181 |
||
182 |
private static List<? extends Annotation> getAnnotation(Annotation a) { |
|
183 |
Class<?> annotated = a.annotationType(); |
|
184 |
Method valueMethod = getValueMethod(annotated); |
|
185 |
if (valueMethod != null) { |
|
186 |
Class<?> returnType = valueMethod.getReturnType(); |
|
187 |
if (returnType.isArray()) { |
|
188 |
Class<?> candidate = returnType.getComponentType(); |
|
189 |
Repeatable r = candidate.getAnnotation(Repeatable.class); |
|
190 |
if (r != null) { |
|
191 |
Class<?> repeatClass = r.value(); |
|
192 |
if (annotated == repeatClass) { |
|
193 |
return getAnnotationValues(a, valueMethod); |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
List<Annotation> annos = new ArrayList<>(); |
|
199 |
annos.add(a); |
|
200 |
return annos; |
|
201 |
} |
|
202 |
||
203 |
static boolean isAfter(RecordingState stateToTest, RecordingState b) { |
|
204 |
return stateToTest.ordinal() > b.ordinal(); |
|
205 |
} |
|
206 |
||
207 |
static boolean isBefore(RecordingState stateToTest, RecordingState b) { |
|
208 |
return stateToTest.ordinal() < b.ordinal(); |
|
209 |
} |
|
210 |
||
211 |
static boolean isState(RecordingState stateToTest, RecordingState... states) { |
|
212 |
for (RecordingState s : states) { |
|
213 |
if (s == stateToTest) { |
|
214 |
return true; |
|
215 |
} |
|
216 |
} |
|
217 |
return false; |
|
218 |
} |
|
219 |
||
220 |
private static List<Annotation> getAnnotationValues(Annotation a, Method valueMethod) { |
|
221 |
try { |
|
222 |
return Arrays.asList((Annotation[]) valueMethod.invoke(a, new Object[0])); |
|
223 |
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
|
224 |
return new ArrayList<>(); |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
private static Method getValueMethod(Class<?> annotated) { |
|
229 |
try { |
|
230 |
return annotated.getMethod("value", new Class<?>[0]); |
|
231 |
} catch (NoSuchMethodException e) { |
|
232 |
return null; |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
public static void touch(Path dumpFile) throws IOException { |
|
237 |
RandomAccessFile raf = new RandomAccessFile(dumpFile.toFile(), "rw"); |
|
238 |
raf.close(); |
|
239 |
} |
|
240 |
||
241 |
public static Class<?> unboxType(Class<?> t) { |
|
242 |
if (t == Integer.class) { |
|
243 |
return int.class; |
|
244 |
} |
|
245 |
if (t == Long.class) { |
|
246 |
return long.class; |
|
247 |
} |
|
248 |
if (t == Float.class) { |
|
249 |
return float.class; |
|
250 |
} |
|
251 |
if (t == Double.class) { |
|
252 |
return double.class; |
|
253 |
} |
|
254 |
if (t == Byte.class) { |
|
255 |
return byte.class; |
|
256 |
} |
|
257 |
if (t == Short.class) { |
|
258 |
return short.class; |
|
259 |
} |
|
260 |
if (t == Boolean.class) { |
|
261 |
return boolean.class; |
|
262 |
} |
|
263 |
if (t == Character.class) { |
|
264 |
return char.class; |
|
265 |
} |
|
266 |
return t; |
|
267 |
} |
|
268 |
||
269 |
static long nanosToTicks(long nanos) { |
|
270 |
return (long) (nanos * JVM.getJVM().getTimeConversionFactor()); |
|
271 |
} |
|
272 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
273 |
static synchronized EventHandler getHandler(Class<? extends jdk.internal.event.Event> eventClass) { |
50113 | 274 |
Utils.ensureValidEventSubclass(eventClass); |
275 |
try { |
|
276 |
Field f = eventClass.getDeclaredField(EventInstrumentation.FIELD_EVENT_HANDLER); |
|
277 |
SecuritySupport.setAccessible(f); |
|
278 |
return (EventHandler) f.get(null); |
|
279 |
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { |
|
280 |
throw new InternalError("Could not access event handler"); |
|
281 |
} |
|
282 |
} |
|
283 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
284 |
static synchronized void setHandler(Class<? extends jdk.internal.event.Event> eventClass, EventHandler handler) { |
50113 | 285 |
Utils.ensureValidEventSubclass(eventClass); |
286 |
try { |
|
287 |
Field field = eventClass.getDeclaredField(EventInstrumentation.FIELD_EVENT_HANDLER); |
|
288 |
SecuritySupport.setAccessible(field); |
|
289 |
field.set(null, handler); |
|
290 |
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { |
|
291 |
throw new InternalError("Could not access event handler"); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
public static Map<String, String> sanitizeNullFreeStringMap(Map<String, String> settings) { |
|
296 |
HashMap<String, String> map = new HashMap<>(settings.size()); |
|
297 |
for (Map.Entry<String, String> e : settings.entrySet()) { |
|
298 |
String key = e.getKey(); |
|
299 |
if (key == null) { |
|
300 |
throw new NullPointerException("Null key is not allowed in map"); |
|
301 |
} |
|
302 |
String value = e.getValue(); |
|
303 |
if (value == null) { |
|
304 |
throw new NullPointerException("Null value is not allowed in map"); |
|
305 |
} |
|
306 |
map.put(key, value); |
|
307 |
} |
|
308 |
return map; |
|
309 |
} |
|
310 |
||
311 |
public static <T> List<T> sanitizeNullFreeList(List<T> elements, Class<T> clazz) { |
|
312 |
List<T> sanitized = new ArrayList<>(elements.size()); |
|
313 |
for (T element : elements) { |
|
314 |
if (element == null) { |
|
315 |
throw new NullPointerException("Null is not an allowed element in list"); |
|
316 |
} |
|
317 |
if (element.getClass() != clazz) { |
|
318 |
throw new ClassCastException(); |
|
319 |
} |
|
320 |
sanitized.add(element); |
|
321 |
} |
|
322 |
return sanitized; |
|
323 |
} |
|
324 |
||
325 |
static List<Field> getVisibleEventFields(Class<?> clazz) { |
|
326 |
Utils.ensureValidEventSubclass(clazz); |
|
327 |
List<Field> fields = new ArrayList<>(); |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
328 |
for (Class<?> c = clazz; c != jdk.internal.event.Event.class; c = c.getSuperclass()) { |
50113 | 329 |
for (Field field : c.getDeclaredFields()) { |
330 |
// skip private field in base classes |
|
331 |
if (c == clazz || !Modifier.isPrivate(field.getModifiers())) { |
|
332 |
fields.add(field); |
|
333 |
} |
|
334 |
} |
|
335 |
} |
|
336 |
return fields; |
|
337 |
} |
|
338 |
||
339 |
public static void ensureValidEventSubclass(Class<?> eventClass) { |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
340 |
if (jdk.internal.event.Event.class.isAssignableFrom(eventClass) && Modifier.isAbstract(eventClass.getModifiers())) { |
50113 | 341 |
throw new IllegalArgumentException("Abstract event classes are not allowed"); |
342 |
} |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
343 |
if (eventClass == Event.class || eventClass == jdk.internal.event.Event.class || !jdk.internal.event.Event.class.isAssignableFrom(eventClass)) { |
50113 | 344 |
throw new IllegalArgumentException("Must be a subclass to " + Event.class.getName()); |
345 |
} |
|
346 |
} |
|
347 |
||
348 |
public static void writeGeneratedASM(String className, byte[] bytes) { |
|
349 |
if (SAVE_GENERATED == null) { |
|
350 |
// We can't calculate value statically because it will force |
|
351 |
// initialization of SecuritySupport, which cause |
|
352 |
// UnsatisfiedLinkedError on JDK 8 or non-Oracle JDKs |
|
353 |
SAVE_GENERATED = SecuritySupport.getBooleanProperty("jfr.save.generated.asm"); |
|
354 |
} |
|
355 |
if (SAVE_GENERATED) { |
|
356 |
try { |
|
357 |
try (FileOutputStream fos = new FileOutputStream(className + ".class")) { |
|
358 |
fos.write(bytes); |
|
359 |
} |
|
360 |
||
361 |
try (FileWriter fw = new FileWriter(className + ".asm"); PrintWriter pw = new PrintWriter(fw)) { |
|
362 |
ClassReader cr = new ClassReader(bytes); |
|
363 |
CheckClassAdapter.verify(cr, true, pw); |
|
364 |
} |
|
365 |
Logger.log(LogTag.JFR_SYSTEM_BYTECODE, LogLevel.INFO, "Instrumented code saved to " + className + ".class and .asm"); |
|
366 |
} catch (IOException e) { |
|
367 |
Logger.log(LogTag.JFR_SYSTEM_BYTECODE, LogLevel.INFO, "Could not save instrumented code, for " + className + ".class and .asm"); |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
372 |
public static void ensureInitialized(Class<? extends jdk.internal.event.Event> eventClass) { |
50113 | 373 |
SecuritySupport.ensureClassIsInitialized(eventClass); |
374 |
} |
|
375 |
||
376 |
public static Object makePrimitiveArray(String typeName, List<Object> values) { |
|
377 |
int length = values.size(); |
|
378 |
switch (typeName) { |
|
379 |
case "int": |
|
380 |
int[] ints = new int[length]; |
|
381 |
for (int i = 0; i < length; i++) { |
|
382 |
ints[i] = (int) values.get(i); |
|
383 |
} |
|
384 |
return ints; |
|
385 |
case "long": |
|
386 |
long[] longs = new long[length]; |
|
387 |
for (int i = 0; i < length; i++) { |
|
388 |
longs[i] = (long) values.get(i); |
|
389 |
} |
|
390 |
return longs; |
|
391 |
||
392 |
case "float": |
|
393 |
float[] floats = new float[length]; |
|
394 |
for (int i = 0; i < length; i++) { |
|
395 |
floats[i] = (float) values.get(i); |
|
396 |
} |
|
397 |
return floats; |
|
398 |
||
399 |
case "double": |
|
400 |
double[] doubles = new double[length]; |
|
401 |
for (int i = 0; i < length; i++) { |
|
402 |
doubles[i] = (double) values.get(i); |
|
403 |
} |
|
404 |
return doubles; |
|
405 |
||
406 |
case "short": |
|
407 |
short[] shorts = new short[length]; |
|
408 |
for (int i = 0; i < length; i++) { |
|
409 |
shorts[i] = (short) values.get(i); |
|
410 |
} |
|
411 |
return shorts; |
|
412 |
case "char": |
|
413 |
char[] chars = new char[length]; |
|
414 |
for (int i = 0; i < length; i++) { |
|
415 |
chars[i] = (char) values.get(i); |
|
416 |
} |
|
417 |
return chars; |
|
418 |
case "byte": |
|
419 |
byte[] bytes = new byte[length]; |
|
420 |
for (int i = 0; i < length; i++) { |
|
421 |
bytes[i] = (byte) values.get(i); |
|
422 |
} |
|
423 |
return bytes; |
|
424 |
case "boolean": |
|
425 |
boolean[] booleans = new boolean[length]; |
|
426 |
for (int i = 0; i < length; i++) { |
|
427 |
booleans[i] = (boolean) values.get(i); |
|
428 |
} |
|
429 |
return booleans; |
|
430 |
case "java.lang.String": |
|
431 |
String[] strings = new String[length]; |
|
432 |
for (int i = 0; i < length; i++) { |
|
433 |
strings[i] = (String) values.get(i); |
|
434 |
} |
|
435 |
return strings; |
|
436 |
} |
|
437 |
return null; |
|
438 |
} |
|
439 |
||
440 |
public static boolean isSettingVisible(Control c, boolean hasEventHook) { |
|
441 |
if (c instanceof ThresholdSetting) { |
|
442 |
return !hasEventHook; |
|
443 |
} |
|
444 |
if (c instanceof PeriodSetting) { |
|
445 |
return hasEventHook; |
|
446 |
} |
|
447 |
if (c instanceof StackTraceSetting) { |
|
448 |
return !hasEventHook; |
|
449 |
} |
|
450 |
return true; |
|
451 |
} |
|
452 |
||
453 |
public static boolean isSettingVisible(long typeId, boolean hasEventHook) { |
|
454 |
if (ThresholdSetting.isType(typeId)) { |
|
455 |
return !hasEventHook; |
|
456 |
} |
|
457 |
if (PeriodSetting.isType(typeId)) { |
|
458 |
return hasEventHook; |
|
459 |
} |
|
460 |
if (StackTraceSetting.isType(typeId)) { |
|
461 |
return !hasEventHook; |
|
462 |
} |
|
463 |
return true; |
|
464 |
} |
|
465 |
||
466 |
public static Type getValidType(Class<?> type, String name) { |
|
467 |
Objects.requireNonNull(type, "Null is not a valid type for value descriptor " + name); |
|
468 |
if (type.isArray()) { |
|
469 |
type = type.getComponentType(); |
|
470 |
if (type != String.class && !type.isPrimitive()) { |
|
471 |
throw new IllegalArgumentException("Only arrays of primitives and Strings are allowed"); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
Type knownType = Type.getKnownType(type); |
|
476 |
if (knownType == null || knownType == Type.STACK_TRACE) { |
|
477 |
throw new IllegalArgumentException("Only primitive types, java.lang.Thread, java.lang.String and java.lang.Class are allowed for value descriptors. " + type.getName()); |
|
478 |
} |
|
479 |
return knownType; |
|
480 |
} |
|
481 |
||
482 |
public static <T> List<T> smallUnmodifiable(List<T> list) { |
|
483 |
if (list.isEmpty()) { |
|
484 |
return Collections.emptyList(); |
|
485 |
} |
|
486 |
if (list.size() == 1) { |
|
487 |
return Collections.singletonList(list.get(0)); |
|
488 |
} |
|
489 |
return Collections.unmodifiableList(list); |
|
490 |
} |
|
491 |
||
492 |
public static String upgradeLegacyJDKEvent(String eventName) { |
|
493 |
if (eventName.length() <= LEGACY_EVENT_NAME_PREFIX.length()) { |
|
494 |
return eventName; |
|
495 |
} |
|
496 |
if (eventName.startsWith(LEGACY_EVENT_NAME_PREFIX)) { |
|
497 |
int index = eventName.lastIndexOf("."); |
|
498 |
if (index == LEGACY_EVENT_NAME_PREFIX.length() - 1) { |
|
499 |
return Type.EVENT_NAME_PREFIX + eventName.substring(index + 1); |
|
500 |
} |
|
501 |
} |
|
502 |
return eventName; |
|
503 |
} |
|
50745 | 504 |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
505 |
public static void verifyMirror(Class<?> mirror, Class<?> real) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
506 |
Class<?> cMirror = Objects.requireNonNull(mirror); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
507 |
Class<?> cReal = Objects.requireNonNull(real); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
508 |
|
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
509 |
while (cReal != null) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
510 |
Map<String, Field> mirrorFields = new HashMap<>(); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
511 |
if (cMirror != null) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
512 |
for (Field f : cMirror.getDeclaredFields()) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
513 |
if (isSupportedType(f.getType())) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
514 |
mirrorFields.put(f.getName(), f); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
515 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
516 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
517 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
518 |
for (Field realField : cReal.getDeclaredFields()) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
519 |
if (isSupportedType(realField.getType())) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
520 |
String fieldName = realField.getName(); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
521 |
Field mirrorField = mirrorFields.get(fieldName); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
522 |
if (mirrorField == null) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
523 |
throw new InternalError("Missing mirror field for " + cReal.getName() + "#" + fieldName); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
524 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
525 |
if (realField.getModifiers() != mirrorField.getModifiers()) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
526 |
throw new InternalError("Incorrect modifier for mirror field "+ cMirror.getName() + "#" + fieldName); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
527 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
528 |
mirrorFields.remove(fieldName); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
529 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
530 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
531 |
if (!mirrorFields.isEmpty()) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
532 |
throw new InternalError( |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
533 |
"Found additional fields in mirror class " + cMirror.getName() + " " + mirrorFields.keySet()); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
534 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
535 |
if (cMirror != null) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
536 |
cMirror = cMirror.getSuperclass(); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
537 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
538 |
cReal = cReal.getSuperclass(); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
539 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
540 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
541 |
|
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
542 |
private static boolean isSupportedType(Class<?> type) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
543 |
if (Modifier.isTransient(type.getModifiers()) || Modifier.isStatic(type.getModifiers())) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
544 |
return false; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
545 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
546 |
return Type.isValidJavaFieldType(type.getName()); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
547 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
548 |
|
50745 | 549 |
public static String makeFilename(Recording recording) { |
550 |
String pid = JVM.getJVM().getPid(); |
|
551 |
String date = Repository.REPO_DATE_FORMAT.format(LocalDateTime.now()); |
|
552 |
String idText = recording == null ? "" : "-id-" + Long.toString(recording.getId()); |
|
553 |
return "hotspot-" + "pid-" + pid + idText + "-" + date + ".jfr"; |
|
554 |
} |
|
50113 | 555 |
} |