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