author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 53014 | 339d2fbe8675 |
child 59226 | a0f39cc47387 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2017, 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.io.IOException; |
|
29 |
import java.util.List; |
|
30 |
||
31 |
import jdk.internal.HotSpotIntrinsicCandidate; |
|
32 |
import jdk.jfr.Event; |
|
33 |
||
34 |
/** |
|
35 |
* Interface against the JVM. |
|
36 |
* |
|
37 |
*/ |
|
38 |
public final class JVM { |
|
39 |
private static final JVM jvm = new JVM(); |
|
40 |
||
41 |
// JVM signals file changes by doing Object#notifu on this object |
|
42 |
static final Object FILE_DELTA_CHANGE = new Object(); |
|
43 |
||
44 |
static final long RESERVED_CLASS_ID_LIMIT = 400; |
|
45 |
||
46 |
private volatile boolean recording; |
|
47 |
private volatile boolean nativeOK; |
|
48 |
||
49 |
private static native void registerNatives(); |
|
50 |
||
51 |
static { |
|
52 |
registerNatives(); |
|
53 |
for (LogTag tag : LogTag.values()) { |
|
54 |
subscribeLogLevel(tag, tag.id); |
|
55 |
} |
|
56 |
Options.ensureInitialized(); |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Get the one and only JVM. |
|
61 |
* |
|
62 |
* @return the JVM |
|
63 |
*/ |
|
64 |
public static JVM getJVM() { |
|
65 |
return jvm; |
|
66 |
} |
|
67 |
||
68 |
private JVM() { |
|
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* Begin recording events |
|
73 |
* |
|
74 |
* Requires that JFR has been started with {@link #createNativeJFR()} |
|
75 |
*/ |
|
76 |
public native void beginRecording(); |
|
77 |
||
78 |
/** |
|
79 |
* Return ticks |
|
80 |
* |
|
81 |
* @return the time, in ticks |
|
82 |
* |
|
83 |
*/ |
|
84 |
@HotSpotIntrinsicCandidate |
|
85 |
public static native long counterTime(); |
|
86 |
||
87 |
||
88 |
/** |
|
89 |
* Emits native periodic event. |
|
90 |
* |
|
91 |
* @param eventTypeId type id |
|
92 |
* |
|
93 |
* @param timestamp commit time for event |
|
94 |
* @param when when it is being done {@link Periodic.When} |
|
95 |
* |
|
96 |
* @return true if the event was committed |
|
97 |
*/ |
|
98 |
public native boolean emitEvent(long eventTypeId, long timestamp, long when); |
|
99 |
||
100 |
/** |
|
101 |
* End recording events, which includes flushing data in thread buffers |
|
102 |
* |
|
103 |
* Requires that JFR has been started with {@link #createNativeJFR()} |
|
104 |
* |
|
105 |
*/ |
|
106 |
public native void endRecording(); |
|
107 |
||
108 |
/** |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
109 |
* Return a list of all classes deriving from {@link jdk.internal.event.Event} |
50113 | 110 |
* |
111 |
* @return list of event classes. |
|
112 |
*/ |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
113 |
public native List<Class<? extends jdk.internal.event.Event>> getAllEventClasses(); |
50113 | 114 |
|
115 |
/** |
|
116 |
* Return a count of the number of unloaded classes deriving from {@link Event} |
|
117 |
* |
|
118 |
* @return number of unloaded event classes. |
|
119 |
*/ |
|
120 |
public native long getUnloadedEventClassCount(); |
|
121 |
||
122 |
/** |
|
123 |
* Return a unique identifier for a class. The class is marked as being |
|
124 |
* "in use" in JFR. |
|
125 |
* |
|
126 |
* @param clazz clazz |
|
127 |
* |
|
128 |
* @return a unique class identifier |
|
129 |
*/ |
|
130 |
@HotSpotIntrinsicCandidate |
|
131 |
public static native long getClassId(Class<?> clazz); |
|
132 |
||
133 |
// temporary workaround until we solve intrinsics supporting epoch shift tagging |
|
134 |
public static native long getClassIdNonIntrinsic(Class<?> clazz); |
|
135 |
||
136 |
/** |
|
137 |
* Return process identifier. |
|
138 |
* |
|
139 |
* @return process identifier |
|
140 |
*/ |
|
141 |
public native String getPid(); |
|
142 |
||
143 |
/** |
|
144 |
* Return unique identifier for stack trace. |
|
145 |
* |
|
146 |
* Requires that JFR has been started with {@link #createNativeJFR()} |
|
147 |
* |
|
148 |
* @param skipCount number of frames to skip |
|
149 |
* @return a unique stack trace identifier |
|
150 |
*/ |
|
151 |
public native long getStackTraceId(int skipCount); |
|
152 |
||
153 |
/** |
|
154 |
* Return identifier for thread |
|
155 |
* |
|
156 |
* @param t thread |
|
157 |
* @return a unique thread identifier |
|
158 |
*/ |
|
159 |
public native long getThreadId(Thread t); |
|
160 |
||
161 |
/** |
|
162 |
* Frequency, ticks per second |
|
163 |
* |
|
164 |
* @return frequency |
|
165 |
*/ |
|
166 |
public native long getTicksFrequency(); |
|
167 |
||
168 |
/** |
|
169 |
* Write message to log. Should swallow null or empty message, and be able |
|
170 |
* to handle any Java character and not crash with very large message |
|
171 |
* |
|
172 |
* @param tagSetId the tagset id |
|
173 |
* @param level on level |
|
174 |
* @param message log message |
|
175 |
* |
|
176 |
*/ |
|
177 |
public static native void log(int tagSetId, int level, String message); |
|
178 |
||
179 |
/** |
|
180 |
* Subscribe to LogLevel updates for LogTag |
|
181 |
* |
|
182 |
* @param lt the log tag to subscribe |
|
183 |
* @param tagSetId the tagset id |
|
184 |
*/ |
|
185 |
public static native void subscribeLogLevel(LogTag lt, int tagSetId); |
|
186 |
||
187 |
/** |
|
188 |
* Call to invoke event tagging and retransformation of the passed classes |
|
189 |
* |
|
190 |
* @param classes |
|
191 |
*/ |
|
192 |
public native synchronized void retransformClasses(Class<?>[] classes); |
|
193 |
||
194 |
/** |
|
195 |
* Enable event |
|
196 |
* |
|
197 |
* @param eventTypeId event type id |
|
198 |
* |
|
199 |
* @param enabled enable event |
|
200 |
*/ |
|
201 |
public native void setEnabled(long eventTypeId, boolean enabled); |
|
202 |
||
203 |
/** |
|
204 |
* Interval at which the JVM should notify on {@link #FILE_DELTA_CHANGE} |
|
205 |
* |
|
206 |
* @param delta number of bytes, reset after file rotation |
|
207 |
*/ |
|
208 |
public native void setFileNotification(long delta); |
|
209 |
||
210 |
/** |
|
211 |
* Set the number of global buffers to use |
|
212 |
* |
|
213 |
* @param count |
|
214 |
* |
|
215 |
* @throws IllegalArgumentException if count is not within a valid range |
|
216 |
* @throws IllegalStateException if value can't be changed |
|
217 |
*/ |
|
218 |
public native void setGlobalBufferCount(long count) throws IllegalArgumentException, IllegalStateException; |
|
219 |
||
220 |
/** |
|
221 |
* Set size of a global buffer |
|
222 |
* |
|
223 |
* @param size |
|
224 |
* |
|
225 |
* @throws IllegalArgumentException if buffer size is not within a valid |
|
226 |
* range |
|
227 |
*/ |
|
228 |
public native void setGlobalBufferSize(long size) throws IllegalArgumentException; |
|
229 |
||
230 |
/** |
|
231 |
* Set overall memory size |
|
232 |
* |
|
233 |
* @param size |
|
234 |
* |
|
235 |
* @throws IllegalArgumentException if memory size is not within a valid |
|
236 |
* range |
|
237 |
*/ |
|
238 |
public native void setMemorySize(long size) throws IllegalArgumentException; |
|
239 |
||
240 |
/** |
|
241 |
||
242 |
/** |
|
243 |
* Set interval for method samples, in milliseconds. |
|
244 |
* |
|
245 |
* Setting interval to 0 turns off the method sampler. |
|
246 |
* |
|
247 |
* @param intervalMillis the sampling interval |
|
248 |
*/ |
|
249 |
public native void setMethodSamplingInterval(long type, long intervalMillis); |
|
250 |
||
251 |
/** |
|
252 |
* Sets the file where data should be written. |
|
253 |
* |
|
254 |
* Requires that JFR has been started with {@link #createNativeJFR()} |
|
255 |
* |
|
256 |
* <pre> |
|
257 |
* Recording Previous Current Action |
|
258 |
* ============================================== |
|
259 |
* true null null Ignore, keep recording in-memory |
|
260 |
* true null file1 Start disk recording |
|
261 |
* true file null Copy out metadata to disk and continue in-memory recording |
|
262 |
* true file1 file2 Copy out metadata and start with new File (file2) |
|
263 |
* false * null Ignore, but start recording to memory with {@link #beginRecording()} |
|
264 |
* false * file Ignore, but start recording to disk with {@link #beginRecording()} |
|
265 |
* |
|
266 |
* </pre> |
|
267 |
* |
|
268 |
* recording can be set to true/false with {@link #beginRecording()} |
|
269 |
* {@link #endRecording()} |
|
270 |
* |
|
271 |
* @param file the file where data should be written, or null if it should |
|
272 |
* not be copied out (in memory). |
|
273 |
* @throws IOException |
|
274 |
*/ |
|
275 |
public native void setOutput(String file); |
|
276 |
||
277 |
/** |
|
278 |
* Controls if a class deriving from jdk.jfr.Event should |
|
279 |
* always be instrumented on class load. |
|
280 |
* |
|
281 |
* @param force, true to force initialization, false otherwise |
|
282 |
*/ |
|
283 |
public native void setForceInstrumentation(boolean force); |
|
284 |
||
285 |
/** |
|
286 |
* Turn on/off thread sampling. |
|
287 |
* |
|
288 |
* @param sampleThreads true if threads should be sampled, false otherwise. |
|
289 |
* |
|
290 |
* @throws IllegalStateException if state can't be changed. |
|
291 |
*/ |
|
292 |
public native void setSampleThreads(boolean sampleThreads) throws IllegalStateException; |
|
293 |
||
294 |
/** |
|
295 |
* Turn on/off compressed integers. |
|
296 |
* |
|
297 |
* @param compressed true if compressed integers should be used, false |
|
298 |
* otherwise. |
|
299 |
* |
|
300 |
* @throws IllegalStateException if state can't be changed. |
|
301 |
*/ |
|
302 |
public native void setCompressedIntegers(boolean compressed) throws IllegalStateException; |
|
303 |
||
304 |
/** |
|
305 |
* Set stack depth. |
|
306 |
* |
|
307 |
* @param depth |
|
308 |
* |
|
309 |
* @throws IllegalArgumentException if not within a valid range |
|
310 |
* @throws IllegalStateException if depth can't be changed |
|
311 |
*/ |
|
312 |
public native void setStackDepth(int depth) throws IllegalArgumentException, IllegalStateException; |
|
313 |
||
314 |
/** |
|
315 |
* Turn on stack trace for an event |
|
316 |
* |
|
317 |
* @param eventTypeId the event id |
|
318 |
* |
|
319 |
* @param enabled if stack traces should be enabled |
|
320 |
*/ |
|
321 |
public native void setStackTraceEnabled(long eventTypeId, boolean enabled); |
|
322 |
||
323 |
/** |
|
324 |
* Set thread buffer size. |
|
325 |
* |
|
326 |
* @param size |
|
327 |
* |
|
328 |
* @throws IllegalArgumentException if size is not within a valid range |
|
329 |
* @throws IllegalStateException if size can't be changed |
|
330 |
*/ |
|
331 |
public native void setThreadBufferSize(long size) throws IllegalArgumentException, IllegalStateException; |
|
332 |
||
333 |
/** |
|
334 |
* Set threshold for event, |
|
335 |
* |
|
336 |
* Long.MAXIMUM_VALUE = no limit |
|
337 |
* |
|
338 |
* @param eventTypeId the id of the event type |
|
339 |
* @param ticks threshold in ticks, |
|
340 |
* @return true, if it could be set |
|
341 |
*/ |
|
342 |
public native boolean setThreshold(long eventTypeId, long ticks); |
|
343 |
||
344 |
/** |
|
345 |
* Store the metadata descriptor that is to be written at the end of a |
|
346 |
* chunk, data should be written after GMT offset and size of metadata event |
|
347 |
* should be adjusted |
|
348 |
* |
|
349 |
* Requires that JFR has been started with {@link #createNativeJFR()} |
|
350 |
* |
|
351 |
* @param bytes binary representation of metadata descriptor |
|
352 |
* |
|
353 |
* @param binary representation of descriptor |
|
354 |
*/ |
|
355 |
public native void storeMetadataDescriptor(byte[] bytes); |
|
356 |
||
357 |
public void endRecording_() { |
|
358 |
endRecording(); |
|
359 |
recording = false; |
|
360 |
} |
|
361 |
||
362 |
public void beginRecording_() { |
|
363 |
beginRecording(); |
|
364 |
recording = true; |
|
365 |
} |
|
366 |
||
367 |
public boolean isRecording() { |
|
368 |
return recording; |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* If the JVM supports JVM TI and retransformation has not been disabled this |
|
373 |
* method will return true. This flag can not change during the lifetime of |
|
374 |
* the JVM. |
|
375 |
* |
|
376 |
* @return if transform is allowed |
|
377 |
*/ |
|
378 |
public native boolean getAllowedToDoEventRetransforms(); |
|
379 |
||
380 |
/** |
|
381 |
* Set up native resources, data structures, threads etc. for JFR |
|
382 |
* |
|
383 |
* @param simulateFailure simulate a initialization failure and rollback in |
|
384 |
* native, used for testing purposes |
|
385 |
* |
|
386 |
* @throws IllegalStateException if native part of JFR could not be created. |
|
387 |
* |
|
388 |
*/ |
|
389 |
private native boolean createJFR(boolean simulateFailure) throws IllegalStateException; |
|
390 |
||
391 |
/** |
|
392 |
* Destroys native part of JFR. If already destroy, call is ignored. |
|
393 |
* |
|
394 |
* Requires that JFR has been started with {@link #createNativeJFR()} |
|
395 |
* |
|
396 |
* @return if an instance was actually destroyed. |
|
397 |
* |
|
398 |
*/ |
|
399 |
private native boolean destroyJFR(); |
|
400 |
||
401 |
public boolean createFailedNativeJFR() throws IllegalStateException { |
|
402 |
return createJFR(true); |
|
403 |
} |
|
404 |
||
405 |
public void createNativeJFR() { |
|
406 |
nativeOK = createJFR(false); |
|
407 |
} |
|
408 |
||
409 |
public boolean destroyNativeJFR() { |
|
410 |
boolean result = destroyJFR(); |
|
411 |
nativeOK = !result; |
|
412 |
return result; |
|
413 |
} |
|
414 |
||
415 |
public boolean hasNativeJFR() { |
|
416 |
return nativeOK; |
|
417 |
} |
|
418 |
||
419 |
/** |
|
420 |
* Cheap test to check if JFR functionality is available. |
|
421 |
* |
|
422 |
* @return |
|
423 |
*/ |
|
424 |
public native boolean isAvailable(); |
|
425 |
||
426 |
/** |
|
427 |
* To convert ticks to wall clock time. |
|
428 |
*/ |
|
429 |
public native double getTimeConversionFactor(); |
|
430 |
||
431 |
/** |
|
432 |
* Return a unique identifier for a class. Compared to {@link #getClassId()} |
|
433 |
* , this method does not tag the class as being "in-use". |
|
434 |
* |
|
435 |
* @param clazz class |
|
436 |
* |
|
437 |
* @return a unique class identifier |
|
438 |
*/ |
|
439 |
public native long getTypeId(Class<?> clazz); |
|
440 |
||
441 |
/** |
|
442 |
* Fast path fetching the EventWriter using VM intrinsics |
|
443 |
* |
|
444 |
* @return thread local EventWriter |
|
445 |
*/ |
|
446 |
@HotSpotIntrinsicCandidate |
|
447 |
public static native Object getEventWriter(); |
|
448 |
||
449 |
/** |
|
450 |
* Create a new EventWriter |
|
451 |
* |
|
452 |
* @return thread local EventWriter |
|
453 |
*/ |
|
454 |
public static native EventWriter newEventWriter(); |
|
455 |
||
456 |
/** |
|
457 |
* Flushes the EventWriter for this thread. |
|
458 |
*/ |
|
459 |
public static native boolean flush(EventWriter writer, int uncommittedSize, int requestedSize); |
|
460 |
||
461 |
/** |
|
58863 | 462 |
* Flushes all thread buffers to disk and the constant pool data needed to read |
463 |
* them. |
|
464 |
* <p> |
|
465 |
* When the method returns, the chunk header should be updated with valid |
|
466 |
* pointers to the metadata event, last check point event, correct file size and |
|
467 |
* the generation id. |
|
468 |
* |
|
469 |
*/ |
|
470 |
public native void flush(); |
|
471 |
/** |
|
50113 | 472 |
* Sets the location of the disk repository, to be used at an emergency |
473 |
* dump. |
|
474 |
* |
|
475 |
* @param dirText |
|
476 |
*/ |
|
477 |
public native void setRepositoryLocation(String dirText); |
|
478 |
||
479 |
/** |
|
480 |
* Access to VM termination support. |
|
481 |
* |
|
482 |
*@param errorMsg descriptive message to be include in VM termination sequence |
|
483 |
*/ |
|
484 |
public native void abort(String errorMsg); |
|
485 |
||
486 |
/** |
|
487 |
* Adds a string to the string constant pool. |
|
488 |
* |
|
489 |
* If the same string is added twice, two entries will be created. |
|
490 |
* |
|
491 |
* @param id identifier associated with the string, not negative |
|
492 |
* |
|
493 |
* @param s string constant to be added, not null |
|
494 |
* |
|
495 |
* @return the current epoch of this insertion attempt |
|
496 |
*/ |
|
497 |
public static native boolean addStringConstant(boolean epoch, long id, String s); |
|
498 |
/** |
|
499 |
* Gets the address of the jboolean epoch. |
|
500 |
* |
|
501 |
* The epoch alternates every checkpoint. |
|
502 |
* |
|
503 |
* @return The address of the jboolean. |
|
504 |
*/ |
|
505 |
public native long getEpochAddress(); |
|
506 |
||
507 |
public native void uncaughtException(Thread thread, Throwable t); |
|
508 |
/** |
|
509 |
* Sets cutoff for event. |
|
510 |
* |
|
511 |
* Determines how long the event should be allowed to run. |
|
512 |
* |
|
513 |
* Long.MAXIMUM_VALUE = no limit |
|
514 |
* |
|
515 |
* @param eventTypeId the id of the event type |
|
516 |
* @param cutoffTicks cutoff in ticks, |
|
517 |
* @return true, if it could be set |
|
518 |
*/ |
|
519 |
public native boolean setCutoff(long eventTypeId, long cutoffTicks); |
|
520 |
||
521 |
/** |
|
522 |
* Emit old object sample events. |
|
523 |
* |
|
524 |
* @param cutoff the cutoff in ticks |
|
525 |
* @param emitAll emit all samples in old object queue |
|
526 |
*/ |
|
527 |
public native void emitOldObjectSamples(long cutoff, boolean emitAll); |
|
53014
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
528 |
|
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
529 |
/** |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
530 |
* Test if a chunk rotation is warranted. |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
531 |
* |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
532 |
* @return if it is time to perform a chunk rotation |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
533 |
*/ |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
534 |
public native boolean shouldRotateDisk(); |
58863 | 535 |
|
536 |
/** |
|
537 |
* Exclude a thread from the jfr system |
|
538 |
* |
|
539 |
*/ |
|
540 |
public native void exclude(Thread thread); |
|
541 |
||
542 |
/** |
|
543 |
* Include a thread back into the jfr system |
|
544 |
* |
|
545 |
*/ |
|
546 |
public native void include(Thread thread); |
|
547 |
||
548 |
/** |
|
549 |
* Test if a thread ius currently excluded from the jfr system. |
|
550 |
* |
|
551 |
* @return is thread currently excluded |
|
552 |
*/ |
|
553 |
public native boolean isExcluded(Thread thread); |
|
554 |
||
555 |
/** |
|
556 |
* Get the start time in nanos from the header of the current chunk |
|
557 |
* |
|
558 |
*@return start time of the recording in nanos, -1 in case of in-memory |
|
559 |
*/ |
|
560 |
public native long getChunkStartNanos(); |
|
50113 | 561 |
} |