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