The following example shows how to record events using the default configuration and print the Garbage Collection, CPU Load and JVM Information event to standard out.
Configuration c = Configuration.getConfiguration("default"); try (var rs = new RecordingStream(c)) { rs.onEvent("jdk.GarbageCollection", System.out::println); rs.onEvent("jdk.CPULoad", System.out::println); rs.onEvent("jdk.JVMInformation", System.out::println); rs.start(); }
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private final Instant | |
| private final EventDirectoryStream | |
| private Duration | |
| private long | |
| private final Recording |
| Access | Constructor and Description |
|---|---|
| public | |
| private | |
| public | RecordingStream(Configuration
configuration that contains the settings to use,
not configuration)nullCreates a recording stream using settings from a configuration. |
| Modifier and Type | Method and Description |
|---|---|
| public void | awaitTermination(Duration timeout)
Implements jdk. Blocks until all actions are completed, or the stream is closed, or the timeout occurs, or the current thread is interrupted, whichever happens first. |
| public void | awaitTermination()
Implements jdk. Blocks until all actions are completed, or the stream is closed, or the current thread is interrupted, whichever happens first. |
| public void | close()
Implements java. Closes this resource, relinquishing any underlying resources. |
| private List | |
| public EventSettings | |
| public EventSettings | |
| public void | |
| public EventSettings | |
| public EventSettings | |
| public void | onClose(Runnable action)
Implements jdk. Registers an action to perform when the stream is closed. |
| public void | onError(Consumer<Throwable> action)
Implements jdk. Registers an action to perform if an exception occurs. |
| public void | onEvent(String eventName, Consumer<RecordedEvent> action)
Implements jdk. Registers an action to perform on all events matching a name. |
| public void | onEvent(Consumer<RecordedEvent> action)
Implements jdk. Registers an action to perform on all events in the stream. |
| public void | onFlush(Runnable action)
Implements jdk. Registers an action to perform after the stream has been flushed. |
| public void | onMetadata(Consumer<MetadataEvent>
to perform, not action)nullOverrides default jdk. Registers an action to perform when new metadata arrives in the stream. |
| public boolean | |
| public void | setEndTime(Instant endTime)
Implements jdk. Specifies the end time of the stream. |
| public void | |
| public void | setMaxSize(long
the amount of data to retain, maxSize)0 if infiniteDetermines how much data is kept for the stream. |
| public void | setOrdered(boolean ordered)
Implements jdk. Specifies that events arrive in chronological order, sorted by the time they were committed to the stream. |
| public void | setReuse(boolean reuse)
Implements jdk. Specifies that the event object in an |
| public void | setSettings(Map<String, String>
the settings to set, not settings)nullReplaces all settings for this recording stream. |
| public void | setStartTime(Instant startTime)
Implements jdk. Specifies the start time of the stream. |
| public void | |
| public void | startAsync()
Implements jdk. Starts asynchronous processing of actions. |
| public boolean | |
| private void |
| creationTime | back to summary |
|---|---|
| private final Instant creationTime | |
| directoryStream | back to summary |
|---|---|
| private final EventDirectoryStream directoryStream | |
| maxAge | back to summary |
|---|---|
| private Duration maxAge | |
| maxSize | back to summary |
|---|---|
| private long maxSize | |
| recording | back to summary |
|---|---|
| private final Recording recording | |
| RecordingStream | back to summary |
|---|---|
| public RecordingStream() Creates an event stream for the current JVM (Java Virtual Machine).
| |
| RecordingStream | back to summary |
|---|---|
| private RecordingStream(Map<String, String> settings) | |
| RecordingStream | back to summary |
|---|---|
| public RecordingStream(Configuration configuration) Creates a recording stream using settings from a configuration. The following example shows how to create a recording stream that uses a predefined configuration. var c = Configuration.getConfiguration("default"); try (var rs = new RecordingStream(c)) { rs.onEvent(System.out::println); rs.start(); }
| |
| awaitTermination | back to summary |
|---|---|
| public void awaitTermination(Duration timeout) throws InterruptedException Implements jdk. Doc from jdk. Blocks until all actions are completed, or the stream is closed, or the timeout occurs, or the current thread is interrupted, whichever happens first.
| |
| awaitTermination | back to summary |
|---|---|
| public void awaitTermination() throws InterruptedException Implements jdk. Doc from jdk. Blocks until all actions are completed, or the stream is closed, or the current thread is interrupted, whichever happens first.
| |
| close | back to summary |
|---|---|
| public void close() Implements java. Doc from java. Closes this resource, relinquishing any underlying resources.
This method is invoked automatically on objects managed by the
| |
| configurations | back to summary |
|---|---|
| private List | |
| disable | back to summary |
|---|---|
| public EventSettings disable(String name) Disables event with the specified name.
If multiple events with same name (for example, the same class is loaded
in different class loaders), then all events that match the name are
disabled. To disable a specific class, use the
| |
| disable | back to summary |
|---|---|
| public EventSettings disable(Class<? extends Event> eventClass) Disables event.
| |
| dump | back to summary |
|---|---|
| public void dump(Path destination) throws IOException Writes recording data to a file. The recording stream must be started, but not closed. It's highly recommended that a max age or max size is set before starting the stream. Otherwise, the dump may not contain any events.
| |
| enable | back to summary |
|---|---|
| public EventSettings enable(String name) Enables the event with the specified name.
If multiple events have the same name (for example, the same class is
loaded in different class loaders), then all events that match the name
are enabled. To enable a specific class, use the
| |
| enable | back to summary |
|---|---|
| public EventSettings enable(Class<? extends Event> eventClass) Enables event.
| |
| onClose | back to summary |
|---|---|
| public void onClose(Runnable action) Implements jdk. Doc from jdk. Registers an action to perform when the stream is closed. If the stream is already closed, the action will be performed immediately in the current thread.
| |
| onError | back to summary |
|---|---|
| public void onError(Consumer<Throwable> action) Implements jdk. Doc from jdk. Registers an action to perform if an exception occurs. If an action is not registered, an exception stack trace is printed to standard error. Registering an action overrides the default behavior. If multiple actions have been registered, they are performed in the order of registration. If this method itself throws an exception, resulting behavior is undefined.
| |
| onEvent | back to summary |
|---|---|
| public void onEvent(String eventName, Consumer<RecordedEvent> action) Implements jdk. Doc from jdk. Registers an action to perform on all events matching a name.
| |
| onEvent | back to summary |
|---|---|
| public void onEvent(Consumer<RecordedEvent> action) Implements jdk. Doc from jdk. Registers an action to perform on all events in the stream.
To perform an action on a subset of event types, consider using
| |
| onFlush | back to summary |
|---|---|
| public void onFlush(Runnable action) Implements jdk. Doc from jdk. Registers an action to perform after the stream has been flushed.
| |
| onMetadata | back to summary |
|---|---|
| public void onMetadata(Consumer<MetadataEvent> action) Overrides default jdk. Registers an action to perform when new metadata arrives in the stream. The event type of an event always arrives sometime before the actual event. The action must be registered before the stream is started.
The following example shows how to listen to new event types, register
an action if the event type name matches a regular expression and increase a
counter if a matching event is found. A benefit of using an action per
event type, instead of the generic static long count = 0; public static void main(String... args) throws Exception { String regExp = args[0]; var pr = Pattern.compile(regExp).asMatchPredicate(); Configuration c = Configuration.getConfiguration("default"); try (var s = new RecordingStream(c)) { s.setOrdered(false); s.onMetadata(metadata -> metadata.getAddedEventTypes() .stream().map(EventType::getName).filter(pr) .forEach(eventName -> s.onEvent(eventName, event -> count++))); s.startAsync(); System.out.println("Running recording for 5 s. Please wait."); s.awaitTermination(Duration.ofSeconds(5)); System.out.println(count + " events matches " + regExp); } }
| |
| remove | back to summary |
|---|---|
| public boolean remove(Object action) Implements jdk. Doc from jdk. Unregisters an action. If the action has been registered multiple times, all instances are unregistered.
| |
| setEndTime | back to summary |
|---|---|
| public void setEndTime(Instant endTime) Implements jdk. Doc from jdk. Specifies the end time of the stream. The end time must be set before starting the stream. At end time, the stream is closed.
| |
| setMaxAge | back to summary |
|---|---|
| public void setMaxAge(Duration maxAge) Determines how far back data is kept for the stream. To control the amount of recording data stored on disk, the maximum length of time to retain the data can be specified. Data stored on disk that is older than the specified length of time is removed by the Java Virtual Machine (JVM). If neither the maximum limit nor the maximum age is set, the size of the recording may grow indefinitely if events are not consumed.
| |
| setMaxSize | back to summary |
|---|---|
| public void setMaxSize(long maxSize) Determines how much data is kept for the stream. To control the amount of recording data that is stored on disk, the maximum amount of data to retain can be specified. When the maximum limit is exceeded, the Java Virtual Machine (JVM) removes the oldest chunk to make room for a more recent chunk. If neither maximum limit or the maximum age is set, the size of the recording may grow indefinitely. The size is measured in bytes.
| |
| setOrdered | back to summary |
|---|---|
| public void setOrdered(boolean ordered) Implements jdk. Doc from jdk. Specifies that events arrive in chronological order, sorted by the time they were committed to the stream.
| |
| setReuse | back to summary |
|---|---|
| public void setReuse(boolean reuse) Implements jdk. Doc from jdk. Specifies that the event object in an
If reuse is set to
| |
| setSettings | back to summary |
|---|---|
| public void setSettings(Map<String, String> settings) Replaces all settings for this recording stream. The following example records 20 seconds using the "default" configuration and then changes settings to the "profile" configuration. Configuration defaultConfiguration = Configuration.getConfiguration("default"); Configuration profileConfiguration = Configuration.getConfiguration("profile"); try (var rs = new RecordingStream(defaultConfiguration)) { rs.onEvent(System.out::println); rs.startAsync(); Thread.sleep(20_000); rs.setSettings(profileConfiguration.getSettings()); Thread.sleep(20_000); }
| |
| setStartTime | back to summary |
|---|---|
| public void setStartTime(Instant startTime) Implements jdk. Doc from jdk. Specifies the start time of the stream. The start time must be set before starting the stream
| |
| start | back to summary |
|---|---|
| public void start() Implements jdk. Doc from jdk. Starts processing of actions. Actions are performed in the current thread.
To stop the stream, use the
| |
| startAsync | back to summary |
|---|---|
| public void startAsync() Implements jdk. Starts asynchronous processing of actions. Actions are performed in a single separate thread.
To stop the stream, use the The following example prints the CPU usage for ten seconds. When the current thread leaves the try-with-resources block the stream is stopped/closed. try (var stream = new RecordingStream()) { stream.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1)); stream.onEvent("jdk.CPULoad", event -> { System.out.println(event); }); stream.startAsync(); Thread.sleep(10_000); }
| |
| stop | back to summary |
|---|---|
| public boolean stop() Stops the recording stream. Stops a started stream and waits until all events in the recording have been consumed.
Invoking this method in an action, for example in the
The following code snippet illustrates how this method can be used in
conjunction with the AtomicBoolean socketUse = new AtomicBoolean(); try (var r = new RecordingStream()) { r.setMaxSize(Long.MAX_VALUE); r.enable("jdk.SocketWrite").withoutThreshold(); r.enable("jdk.SocketRead").withoutThreshold(); r.onEvent(event -> socketUse.set(true)); r.startAsync(); testFoo(); r.stop(); if (socketUse.get()) { r.dump(Path.of("socket-events.jfr")); throw new AssertionError("testFoo() should not use network"); } }
| |
| updateOnCompleteHandler | back to summary |
|---|---|
| private void updateOnCompleteHandler() | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | accept(Long endNanos)
Implements java. Performs this operation on the given argument. |
| recording | back to summary |
|---|---|
| private final Recording recording | |
| ChunkConsumer | back to summary |
|---|---|
| pack-priv ChunkConsumer(Recording recording) | |
| accept | back to summary |
|---|---|
| public void accept(Long endNanos) Implements java. Doc from java. Performs this operation on the given argument.
| |