Package jdk.jfr
Defining events
Flight Recorder data is stored in events. An event has a timestamp, duration and usually an application specific payload, useful for diagnosing the application up to the failure/crash.
To define a Flight Recorder event, extend Event
and add
fields matching the data types of the payload that should be recorded.
Metadata about fields, such as labels, descriptions and units, can be added
using annotations available in the jdk.jfr.annotation
package,
or a user-defined annotation can be used (if it has the
MetadataDefinition
annotation).
Once an event class has been defined, individual instances can be created
(event objects). Data is stored to the event by assigning to fields. Event
timing can be explicitly controlled by using the begin
and
end
methods available in the Event
class.
To actually store data into the Flight Recorder system, use the
Event.commit()
method. If gathering data in order to store to
an event would be relative expensive, an application can use the
Event.shouldCommit()
to branch if the event would be
committed on an invocation to commit, thus avoiding expensive operations
unnecessarily.
Sometimes the field layout of an event is not known at compile time. If that is the case, an event can be dynamically defined. However, dynamic events have a couple of drawbacks, they might not have the same performance as statically defined ones and it's usually hard for tools to identify and visualize the data without knowing the layout.
To define an event dynamically, use EventFactory
and define
fields using the ValueDescriptor
class and annotations using
the AnnotationElement
class. Use the factory to allocate an
event and the Event.set(int, Object)
method to populate it.
Controlling Flight Recorder
Flight Recorder can be controlled using the jcmd
command line
tool or via JMX using the FlightRecorderMXBean
, registered in
the platform MBeanServer. This is usually sufficient for most use cases, but
if direct programmatic access is needed, an instance can be obtained by
invoking FlightRecorder.getFlightRecorder()
.
With a Flight Recorder instance, a Recording
can be created,
from which the amount of data to record can be configured.
Settings and configuration
A setting consists of a name value pair, where the name specifies which event and setting to configure, and the value determines what it should be set to.
The name can be formed in two ways,
<event-name> + "#" + <setting-name>
or
<event-id> + "#" + <setting-name>
For example, to set the sample interval of the CPU Load event to once every
second, the name should be "com.oracle.jdk.CPULoad#period"
and the
value "1 s"
. If multiple events use the same name, for instance if an
event class is loaded in multiple class loaders, and differentiation is
needed between them, the second form can be used, for example
"56#period"
. The id for an event can be obtained by invoking
EventType.getId()
and is only valid for the JVM instance the
event was registered in.
A list of available event names can be retrieved by calling
FlightRecorder.getEventTypes()
and
EventType.getName()
. A list of available settings for an
event type can be obtained by invoking EventType.getSettingDescriptors()
and ValueDescriptor.getName()
.
The JDK comes with following predefined settings.
Name | Description | Default value | Format | Example values |
---|---|---|---|---|
enabled |
If event should be recorded at all | "true" |
String representation of a Boolean ("true" or
"false" ) |
"true" ,"false" |
threshold |
Cut-off below which event should not be recorded | "0" (no limit) |
"0" if no threshold should be used, otherwise a string
representation of a positive Long followed by an empty space and one
of the following units:
| "0" ,"10 ms" ,"1 s" |
period |
Interval at which the event should be emitted, if it is periodic. | "everyChunk" |
"everyChunk" , if a periodic event should be emitted with every
file rotation, otherwise a string representation of a positive Long
value followed by an empty space and one of the following units:
|
"20 ms" ,"1 s" ,"everyChunk" |
stackTrace |
If the stack trace from the Event#commit() method should be
recorded |
"true" |
String representation of a Boolean ("true" or
"false" ) |
"true" ,"false" |
Null-handling
All methods define whether they accept or return null
in the Javadoc.
Typically this is expressed as "not null"
. If a null
parameter is used where it is not allowed, a
java.lang.NullPointerException
is thrown. If a null
parameters is passed to a method that throws other exceptions, such as
java.io.IOException
, the java.lang.NullPointerException
takes
precedence, unless the Javadoc for the method explicitly states how
null
is handled, i.e. by throwing java.lang.IllegalArgumentException
.
- Since:
- 9
- Commercial feature
- This is a commercial feature that must be unlocked before being used. To learn more about commercial features and how to unlock them visit http://www.oracle.com/technetwork/java/javaseproducts/.
-
Interface Summary Interface Description FlightRecorderListener Callback interface to monitor Flight Recorder's life cycle. -
Class Summary Class Description AnnotationElement Describes event metadata, such as labels, descriptions and units.Configuration A collection of settings and metadata describing the configuration.Event Base class for events, to be subclassed in order to define events and their fields.EventFactory Class for defining events at runtime.EventSettings Convenience class for applying event settings to a recording.EventType Describes an event, its fields, settings and annotations.FlightRecorder Class for accessing, controlling and managing Flight Recorder.FlightRecorderPermission Permission for controlling access to Flight Recorder.Recording Provides means to configure, start, stop and dump recording data to disk.SettingControl Base class to extend to create setting controls.SettingDescriptor Describes an event settingValueDescriptor Describes event fields and annotation elements. -
Enum Summary Enum Description RecordingState Indicates a state in the life cycle of a recording. -
Annotation Types Summary Annotation Type Description BooleanFlag Event field annotation, signifies that value is a boolean flag, atrue
orfalse
valueCategory Event annotation, to associate the event type with a category, in the format of a human readable path.ContentType Meta annotation, signifies that an annotation represents a content type, such as a timespan or a frequency.DataAmount Event field annotation, signifies that a value represents an amount of data, for example bytes.Description Annotation that describes an element with a sentence or two.Enabled Event annotation, determines if an event should be enabled by default.Experimental Annotation that signifies that an element is experimental and may change without further notice.Frequency Event field annotation, signifies that value is a frequency, measured in Hz.Label Annotation that sets a human-readable name for an element, for example"Maximum Throughput"
.MemoryAddress Event field annotation, signifies that value is a memory address.MetadataDefinition Meta annotation for defining new types of event metadata.Name Annotation that overrides the default name for an element, for instance when the default package for an event is not appropriate.Percentage Event field annotation to be used on fractions, typically between0.0
and1.0
, to signify that the value is a percentage.Period Event annotation, determines the default period for a periodic event.Registered Event annotation, for programmatic event registration.Relational Meta annotation for relational annotations, to be used on annotation.SettingDefinition Annotation that signifies that a method in an event class should be used to filter out events.StackTrace Event annotation, determines if an event should by default have a stack trace or not.Threshold Event annotation, determines the default cut-off below which an event should not be recorded, for example"20 ms"
.Timespan Event field annotation, signifies that value is a duration.Timestamp Event field annotation, signifies that value is a point in time.TransitionFrom Event field annotation, signifies that the event transitioned from a thread.TransitionTo Event field annotation, signifies that the event is about to transitions to a thread.Unsigned Event field annotation, signifies that value is as unsigned data type.