APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.jfr

public final Class Recording

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.Closeable, .IOException, .InputStream, java.nio.file.Path, java.time.Duration, .Instant, java.util.HashMap, .Map, .Objects, jdk.jfr.internal.PlatformRecorder, .PlatformRecording, .Type, .WriteablePath, jdk.jfr.internal.util.Utils

Provides means to configure, start, stop and dump recording data to disk.

The following example shows how to configure, start, stop and dump recording data to disk.

Configuration c = Configuration.getConfiguration("default"); try (Recording r = new Recording(c)) { r.start(); System.gc(); Thread.sleep(5000); r.stop(); r.dump(Files.createTempFile("my-recording", ".jfr")); }
Configuration c = Configuration.getConfiguration("default");
try (Recording r = new Recording(c)) {
    r.start();
    System.gc();
    Thread.sleep(5000);
    r.stop();
    r.dump(Files.createTempFile("my-recording", ".jfr"));
}
Since
9

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final PlatformRecording

Constructor Summary

AccessConstructor and Description
public
Recording(Map<String, String>
settings as a map of name-value pairs, not null
settings
)

Creates a recording with settings from a map of name-value pairs.

pack-priv
Recording(Boolean register, Map<String, String> settings)

public
Recording()

Creates a recording without any settings.

public
Recording(Configuration
configuration that contains the settings to be used, not null
configuration
)

Creates a recording with settings from a configuration.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.io.Closeable.close.

Releases all data that is associated with this recording.

public Recording

Returns:

the recording copy, not null
copy
(boolean
true if the newly created copy should be stopped immediately, false otherwise
stop
)

Returns a clone of this recording, with a new recording ID and name.

public EventSettings

Returns:

an event setting for further configuration, not null
disable
(String
the settings for the event, not null
name
)

Disables event with the specified name.

public EventSettings

Returns:

an event setting for further configuration, not null
disable
(Class<? extends Event>
the event to disable, not null
eventClass
)

Disables event.

public void
dump(Path
the location where recording data is written, not null
destination
)

Writes recording data to a file.

public EventSettings

Returns:

an event setting for further configuration, not null
enable
(String
the settings for the event, not null
name
)

Enables the event with the specified name.

public EventSettings

Returns:

an event setting for further configuration, not null
enable
(Class<? extends Event>
the event to enable, not null
eventClass
)

Enables event.

public Path

Returns:

the destination file, or null if not set.
getDestination
()

Returns the destination file, where recording data is written when the recording stops, or null if no destination is set.

public boolean

Returns:

true if the recording is dumped on exit, false otherwise.
getDumpOnExit
()

Returns whether this recording is dumped to disk when the JVM exits.

public Duration

Returns:

the desired duration of the recording, or null if no duration has been set.
getDuration
()

Returns the specified duration for this recording, or null if no duration is set.

pack-priv Duration

Returns:

the flush interval, or null if no interval has been set
getFlushInterval
()

Returns how often events are made available for streaming purposes.

public long

Returns:

the recording ID
getId
()

Returns a unique ID for this recording.

pack-priv PlatformRecording
public Duration

Returns:

maximum length of time, or null if no maximum length of time has been set
getMaxAge
()

Returns the length of time that the data is kept in the disk repository before it is removed.

public long

Returns:

maximum size in bytes, or 0 if no maximum size is set
getMaxSize
()

Returns the maximum size, measured in bytes, at which data is no longer kept in the disk repository.

public String

Returns:

the recording name, not null
getName
()

Returns the name of this recording.

public Map<String, String>

Returns:

recording settings, not null
getSettings
()

Returns settings used by this recording.

public long

Returns:

amount of recorded data, measured in bytes, or 0 if the recording is not written to the disk repository
getSize
()

Returns the current size of this recording in the disk repository, measured in bytes.

public Instant

Returns:

the time, or null if this recording is not started
getStartTime
()

Returns the time when this recording was started.

public RecordingState

Returns:

the recording state, not null
getState
()

Returns the recording state that this recording is currently in.

public Instant

Returns:

the time, or null if this recording is not stopped
getStopTime
()

Returns the time when this recording was stopped.

public InputStream

Returns:

an input stream, or null if no data is available in the interval, or the recording was not recorded to disk
getStream
(Instant
the start time for the stream, or null to get data from start time of the recording
start
,
Instant
the end time for the stream, or null to get data until the present time.
end
)

Creates a data stream for a specified interval.

public boolean

Returns:

true if the recording uses the disk repository, false otherwise
isToDisk
()

Returns true if this recording uses the disk repository, false otherwise.

public void
scheduleStart(Duration
the time to wait before starting this recording, not null
delay
)

Starts this recording after a delay.

public void
setDestination(Path
the destination path, or null if recording should not be dumped at stop
destination
)

Sets a location where data is written on recording stop, or null if data is not to be dumped.

public void
setDumpOnExit(boolean
if this recording should be dumped when the JVM exits
dumpOnExit
)

Sets whether this recording is dumped to disk when the JVM exits.

public void
setDuration(Duration
the duration, or null if no duration is set
duration
)

Sets a duration for how long a recording runs before it stops.

public void
setMaxAge(Duration
the length of time that data is kept, or null if infinite
maxAge
)

Determines how far back data is kept in the disk repository.

public void
setMaxSize(long
the amount of data to retain, 0 if infinite
maxSize
)

Determines how much data is kept in the disk repository.

public void
setName(String
the recording name, not null
name
)

Sets a human-readable name (for example, "My Recording").

private void
public void
setSettings(Map<String, String>
the settings to set, not null
settings
)

Replaces all settings for this recording.

public void
setToDisk(boolean
true if this recording is written to disk, false if in-memory
disk
)

Determines whether this recording is continuously flushed to the disk repository or data is constrained to what is available in memory buffers.

public void
start()

Starts this recording.

public boolean

Returns:

true if recording is stopped, false otherwise
stop
()

Stops this recording.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

internalback to summary
private final PlatformRecording internal

Constructor Detail

Recordingback to summary
public Recording(Map<String, String> settings)

Creates a recording with settings from a map of name-value pairs.

A newly created recording is in the RecordingState#NEW state. To start the recording, invoke the Recording#start() method.

Parameters
settings:Map<String, String>

settings as a map of name-value pairs, not null

Exceptions
IllegalStateException:
if Flight Recorder can't be created (for example, if the Java Virtual Machine (JVM) lacks Flight Recorder support, or if the file repository can't be created or accessed)
Since
11
See Also
jdk.jfr
Recordingback to summary
pack-priv Recording(Boolean register, Map<String, String> settings)
Recordingback to summary
public Recording()

Creates a recording without any settings.

A newly created recording is in the RecordingState#NEW state. To start the recording, invoke the Recording#start() method.

Exceptions
IllegalStateException:
if Flight Recorder can't be created (for example, if the Java Virtual Machine (JVM) lacks Flight Recorder support, or if the file repository can't be created or accessed)
Recordingback to summary
public Recording(Configuration configuration)

Creates a recording with settings from a configuration.

The following example shows how create a recording that uses a predefined configuration.

Recording r = new Recording(Configuration.getConfiguration("default"));
Recording r = new Recording(Configuration.getConfiguration("default"));
The newly created recording is in the RecordingState#NEW state. To start the recording, invoke the Recording#start() method.
Parameters
configuration:Configuration

configuration that contains the settings to be used, not null

Exceptions
IllegalStateException:
if Flight Recorder can't be created (for example, if the Java Virtual Machine (JVM) lacks Flight Recorder support, or if the file repository can't be created or accessed)
See Also
Configuration

Method Detail

closeback to summary
public void close()

Implements java.io.Closeable.close.

Releases all data that is associated with this recording.

After a successful invocation of this method, this recording is in the CLOSED state.

Annotations
@Override
copyback to summary
public Recording copy(boolean stop)

Returns a clone of this recording, with a new recording ID and name.

Clones are useful for dumping data without stopping the recording. After a clone is created, the amount of data to copy is constrained with the setMaxAge(Duration) method and the setMaxSize(long) method.

Parameters
stop:boolean

true if the newly created copy should be stopped immediately, false otherwise

Returns:Recording

the recording copy, not null

disableback to summary
public EventSettings disable(String name)

Disables event with the specified name.

If multiple events with the 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(Class) method or a String representation of the event type ID.

Parameters
name:String

the settings for the event, not null

Returns:EventSettings

an event setting for further configuration, not null

disableback to summary
public EventSettings disable(Class<? extends Event> eventClass)

Disables event.

Parameters
eventClass:Class<? extends Event>

the event to disable, not null

Returns:EventSettings

an event setting for further configuration, not null

Exceptions
IllegalArgumentException:
if eventClass is an abstract class or not a subclass of Event
dumpback to summary
public void dump(Path destination) throws IOException

Writes recording data to a file.

For a dump to succeed, the recording must either be 1) running, or 2) stopped and to disk. If the recording is in any other state, an IOException is thrown.

Parameters
destination:Path

the location where recording data is written, not null

Exceptions
IOException:
if recording data can't be copied to the specified location, for example, if the recording is closed or the destination path is not writable
See Also
getState(), isToDisk()
enableback 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(Class) method or a String representation of the event type ID.

Parameters
name:String

the settings for the event, not null

Returns:EventSettings

an event setting for further configuration, not null

See Also
EventType
enableback to summary
public EventSettings enable(Class<? extends Event> eventClass)

Enables event.

Parameters
eventClass:Class<? extends Event>

the event to enable, not null

Returns:EventSettings

an event setting for further configuration, not null

Exceptions
IllegalArgumentException:
if eventClass is an abstract class or not a subclass of Event
getDestinationback to summary
public Path getDestination()

Returns the destination file, where recording data is written when the recording stops, or null if no destination is set.

Returns:Path

the destination file, or null if not set.

getDumpOnExitback to summary
public boolean getDumpOnExit()

Returns whether this recording is dumped to disk when the JVM exits.

If dump on exit is not set, false is returned.

Returns:boolean

true if the recording is dumped on exit, false otherwise.

getDurationback to summary
public Duration getDuration()

Returns the specified duration for this recording, or null if no duration is set.

The duration can be set only when the recording is in the RecordingState#NEW state.

Returns:Duration

the desired duration of the recording, or null if no duration has been set.

getFlushIntervalback to summary
pack-priv Duration getFlushInterval()

Returns how often events are made available for streaming purposes.

Returns:Duration

the flush interval, or null if no interval has been set

Since
14
getIdback to summary
public long getId()

Returns a unique ID for this recording.

Returns:long

the recording ID

getInternalback to summary
pack-priv PlatformRecording getInternal()
getMaxAgeback to summary
public Duration getMaxAge()

Returns the length of time that the data is kept in the disk repository before it is removed.

Returns:Duration

maximum length of time, or null if no maximum length of time has been set

getMaxSizeback to summary
public long getMaxSize()

Returns the maximum size, measured in bytes, at which data is no longer kept in the disk repository.

Returns:long

maximum size in bytes, or 0 if no maximum size is set

getNameback to summary
public String getName()

Returns the name of this recording.

By default, the name is the same as the recording ID.

Returns:String

the recording name, not null

getSettingsback to summary
public Map<String, String> getSettings()

Returns settings used by this recording.

Modifying the returned Map will not change the settings for this recording.

If no settings are set for this recording, an empty Map is returned.

Returns:Map<String, String>

recording settings, not null

getSizeback to summary
public long getSize()

Returns the current size of this recording in the disk repository, measured in bytes.

The size is updated when recording buffers are flushed. If the recording is not written to the disk repository the returned size is always 0.

Returns:long

amount of recorded data, measured in bytes, or 0 if the recording is not written to the disk repository

getStartTimeback to summary
public Instant getStartTime()

Returns the time when this recording was started.

Returns:Instant

the time, or null if this recording is not started

getStateback to summary
public RecordingState getState()

Returns the recording state that this recording is currently in.

Returns:RecordingState

the recording state, not null

See Also
RecordingState
getStopTimeback to summary
public Instant getStopTime()

Returns the time when this recording was stopped.

Returns:Instant

the time, or null if this recording is not stopped

getStreamback to summary
public InputStream getStream(Instant start, Instant end) throws IOException

Creates a data stream for a specified interval.

The stream may contain some data outside the specified range.

If the recording is not to disk, a stream can't be created and null is returned.

Parameters
start:Instant

the start time for the stream, or null to get data from start time of the recording

end:Instant

the end time for the stream, or null to get data until the present time.

Returns:InputStream

an input stream, or null if no data is available in the interval, or the recording was not recorded to disk

Exceptions
IOException:
if a stream can't be opened
IllegalArgumentException:
if end happens before start
See Also
setToDisk(boolean)
isToDiskback to summary
public boolean isToDisk()

Returns true if this recording uses the disk repository, false otherwise.

If no value is set, true is returned.

Returns:boolean

true if the recording uses the disk repository, false otherwise

scheduleStartback to summary
public void scheduleStart(Duration delay)

Starts this recording after a delay.

After a successful invocation of this method, this recording is in the DELAYED state.

Parameters
delay:Duration

the time to wait before starting this recording, not null

Exceptions
IllegalStateException:
if the recording is not it the NEW state
setDestinationback to summary
public void setDestination(Path destination) throws IOException

Sets a location where data is written on recording stop, or null if data is not to be dumped.

If a destination is set, this recording is automatically closed after data is successfully copied to the destination path.

If a destination is not set, Flight Recorder retains the recording data until this recording is closed. Use the dump(Path) method to manually write data to a file.

Parameters
destination:Path

the destination path, or null if recording should not be dumped at stop

Exceptions
IOException:
if the path is not writable
IllegalStateException:
if recording is in the STOPPED or CLOSED state.
setDumpOnExitback to summary
public void setDumpOnExit(boolean dumpOnExit)

Sets whether this recording is dumped to disk when the JVM exits.

Parameters
dumpOnExit:boolean

if this recording should be dumped when the JVM exits

setDurationback to summary
public void setDuration(Duration duration)

Sets a duration for how long a recording runs before it stops.

By default, a recording has no duration (null).

Parameters
duration:Duration

the duration, or null if no duration is set

Exceptions
IllegalStateException:
if recording is in the STOPPED or CLOSED state
setMaxAgeback to summary
public void setMaxAge(Duration maxAge)

Determines how far back data is kept in the disk repository.

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 maximum limit or the maximum age is set, the size of the recording may grow indefinitely.

Parameters
maxAge:Duration

the length of time that data is kept, or null if infinite

Exceptions
IllegalArgumentException:
if maxAge is negative
IllegalStateException:
if the recording is in the CLOSED state
setMaxSizeback to summary
public void setMaxSize(long maxSize)

Determines how much data is kept in the disk repository.

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.

Parameters
maxSize:long

the amount of data to retain, 0 if infinite

Exceptions
IllegalArgumentException:
if maxSize is negative
IllegalStateException:
if the recording is in CLOSED state
setNameback to summary
public void setName(String name)

Sets a human-readable name (for example, "My Recording").

Parameters
name:String

the recording name, not null

Exceptions
IllegalStateException:
if the recording is in CLOSED state
setSettingback to summary
private void setSetting(String id, String value)
setSettingsback to summary
public void setSettings(Map<String, String> settings)

Replaces all settings for this recording.

The following example shows how to set event settings for a recording.

Map<String, String> settings = new HashMap<>(); settings.putAll(EventSettings.enabled("jdk.CPUSample").withPeriod(Duration.ofSeconds(2)).toMap()); settings.putAll(EventSettings.enabled(MyEvent.class).withThreshold(Duration.ofSeconds(2)).withoutStackTrace().toMap()); settings.put("jdk.ExecutionSample#period", "10 ms"); recording.setSettings(settings);
Map<String, String> settings = new HashMap<>();
settings.putAll(EventSettings.enabled("jdk.CPUSample").withPeriod(Duration.ofSeconds(2)).toMap());
settings.putAll(EventSettings.enabled(MyEvent.class).withThreshold(Duration.ofSeconds(2)).withoutStackTrace().toMap());
settings.put("jdk.ExecutionSample#period", "10 ms");
recording.setSettings(settings);
The following example shows how to merge settings.
Map<String, String> settings = recording.getSettings(); settings.putAll(additionalSettings); recording.setSettings(settings);
Map<String, String> settings = recording.getSettings();
settings.putAll(additionalSettings);
recording.setSettings(settings);
Parameters
settings:Map<String, String>

the settings to set, not null

setToDiskback to summary
public void setToDisk(boolean disk)

Determines whether this recording is continuously flushed to the disk repository or data is constrained to what is available in memory buffers.

Parameters
disk:boolean

true if this recording is written to disk, false if in-memory

startback to summary
public void start()

Starts this recording.

It's recommended that the recording options and event settings are configured before calling this method. The benefits of doing so are a more consistent state when analyzing the recorded data, and improved performance because the configuration can be applied atomically.

After a successful invocation of this method, this recording is in the RUNNING state.

Exceptions
IllegalStateException:
if recording is already started or is in the CLOSED state
stopback to summary
public boolean stop()

Stops this recording.

When a recording is stopped it can't be restarted. If this recording has a destination, data is written to that destination and the recording is closed. After a recording is closed, the data is no longer available.

After a successful invocation of this method, this recording will be in the STOPPED state.

Returns:boolean

true if recording is stopped, false otherwise

Exceptions
IllegalStateException:
if the recording is not started or is already stopped
See Also
setDestination(Path)
jdk.jfr back to summary

private Class Recording.RecordingSettings

extends EventSettings
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final String
private final Recording

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
RecordingSettings(Recording r, Class<? extends Event> eventClass)

Method Summary

Modifier and TypeMethod and Description
public Map<String, String>
toMap()

Implements abstract jdk.jfr.EventSettings.toMap.

Creates a settings Map for the event that is associated with this event setting.

public EventSettings
with(String name, String value)

Implements abstract jdk.jfr.EventSettings.with.

Sets a setting value for the event that is associated with this event setting.

Inherited from jdk.jfr.EventSettings:
withoutStackTracewithoutThresholdwithPeriodwithStackTracewithThreshold

Field Detail

identifierback to summary
private final String identifier
recordingback to summary
private final Recording recording

Constructor Detail

RecordingSettingsback to summary
pack-priv RecordingSettings(Recording r, String identifier)
RecordingSettingsback to summary
pack-priv RecordingSettings(Recording r, Class<? extends Event> eventClass)

Method Detail

toMapback to summary
public Map<String, String> toMap()

Implements abstract jdk.jfr.EventSettings.toMap.

Doc from jdk.jfr.EventSettings.toMap.

Creates a settings Map for the event that is associated with this event setting.

Annotations
@Override
withback to summary
public EventSettings with(String name, String value)

Implements abstract jdk.jfr.EventSettings.with.

Doc from jdk.jfr.EventSettings.with.

Sets a setting value for the event that is associated with this event setting.

Annotations
@Override