Class BinaryThreadRecorder
- All Implemented Interfaces:
Recorder,ThreadRecorder,Closeable,AutoCloseable
Overview
The generated .flow file is a linear sequence of events without any global header. Two
event types exist:
- ENTER: a method entry event
- EXIT: one or more consecutive method exits
Method identifiers are numeric values assigned by a MethodIdMapping and stored in a
separate file. The mapping associates a method identifier with a fully qualified method
signature.
Event Structure
Each event starts with a single byte that encodes both:
- the event type (ENTER or EXIT), and
- the beginning of an unsigned variable-length integer.
The most significant bit (bit 7) determines the event type:
Packed First Byte Layout
The first byte of every event has the following structure:
bit 7 : flag (1 = ENTER, 0 = EXIT)
bit 6 : continuation bit (1 = more bytes follow)
bits 5-0 : lowest 6 bits of the encoded value
The encoded value depends on the event type:
- For ENTER events: the value is the method ID.
- For EXIT events: the value is the number of additional consecutive exits.
Variable-Length Integer Encoding
All integer values are encoded as unsigned variable-length integers in a format equivalent to LEB128.
After the first byte, if the continuation bit (bit 6) is set, the remaining higher-order bits of the value are encoded using standard 7-bit groups:
bit 7 : continuation (1 = more bytes follow)
bits 6-0 : next 7 bits of the value
Each subsequent byte contributes 7 additional bits to the value. Decoding proceeds by accumulating payload bits while continuation bits are set.
ENTER Event
An ENTER event encodes a single method entry as [flag=1][methodId]. The
methodId refers to the numeric identifier defined in the ID mapping file.
Example with methodId = 1: 1000 0001
Example with methodId = 8192: 1100 0000 0000 0001
EXIT Event
EXIT events are run-length encoded.
Instead of writing one byte per exit, consecutive exits are accumulated internally and flushed as a single event.
- If exactly one exit occurred:
[flag=0](no continuation, no payload) - If
n > 1consecutive exits occurred:[flag=0][n - 1]
This means:
value == 0represents a single exit.value == krepresentsk + 1consecutive exits.
This run-length encoding significantly reduces file size when methods return in bursts.
Decoding Algorithm (High-Level)
- Read first byte.
- Extract event type from bit 7.
- Extract continuation bit from bit 6.
- Extract lower 6 bits as initial value.
- If continuation is set, read additional LEB128 bytes and accumulate 7-bit groups.
- If ENTER: emit method entry with decoded
methodId. - If EXIT: emit
value + 1exits.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteHighest bit is 1 (0x80).static final byteHighest bit is 0 (0x00).protected final StringThe file containing the call tree data of the recorder's thread.static final byteContinuation bit in a varint, the highest bit (0x80).static final byteFlag bit, the highest bit (0x80).static final byteContinuation bit in a packed varint, second highest bit (0x40).static final bytePayload in a packed varint, the lowest 6 bits (0x3F).static final bytePayload in a varint, the lowest 7 bits (0x7F).static final longRest of payload to encode in following varint bytes, all but 7 lowest bits (~0x7FL).protected final OutputStreamOutput stream for the binary call tree data of the recorder's thread.protected longPending consecutive exits. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidenter(long methodId) Emit a method enter event with the given method ID.voidexit()Emit a method exit event.protected voidFlush a pending exit event if there is one.protected voidwriteFlagAndVarInt(int flag, long value) Write unsigned variable-length integer with a 1-bit flag packed into the first byte.protected voidwriteVarInt(long value) Write an unsigned variable-length integer (LEB128-style).
-
Field Details
-
F_ENTER
public static final byte F_ENTERHighest bit is 1 (0x80).- See Also:
-
F_EXIT
public static final byte F_EXITHighest bit is 0 (0x00).- See Also:
-
M_FLAG
public static final byte M_FLAGFlag bit, the highest bit (0x80).- See Also:
-
M_CONT
public static final byte M_CONTContinuation bit in a varint, the highest bit (0x80).- See Also:
-
M_PAYLOAD
public static final byte M_PAYLOADPayload in a varint, the lowest 7 bits (0x7F).- See Also:
-
M_PAYLOAD_REST
public static final long M_PAYLOAD_RESTRest of payload to encode in following varint bytes, all but 7 lowest bits (~0x7FL).- See Also:
-
M_PACK_CONT
public static final byte M_PACK_CONTContinuation bit in a packed varint, second highest bit (0x40).- See Also:
-
M_PACK_PAYLOAD
public static final byte M_PACK_PAYLOADPayload in a packed varint, the lowest 6 bits (0x3F).- See Also:
-
out
Output stream for the binary call tree data of the recorder's thread. -
fileName
The file containing the call tree data of the recorder's thread. -
pendingExits
protected long pendingExitsPending consecutive exits.
-
-
Constructor Details
-
BinaryThreadRecorder
- Parameters:
outputDir- Path to output directory- Throws:
IOException- If an I/O error occurs when opening an output stream.
-
-
Method Details
-
getFileName
- Returns:
- The file name this recorder is writing to
-
enter
Description copied from interface:RecorderEmit a method enter event with the given method ID.- Specified by:
enterin interfaceRecorder- Parameters:
methodId- The ID of the method to trace- Throws:
IOException- If an I/O error occurs when emitting the event.
-
exit
Description copied from interface:RecorderEmit a method exit event.- Specified by:
exitin interfaceRecorder- Throws:
IOException- If an I/O error occurs when emitting the event.
-
flushPendingExits
Flush a pending exit event if there is one.- Throws:
IOException- If an I/O error occurs when writing to file.
-
writeVarInt
Write an unsigned variable-length integer (LEB128-style).bit 7 : continuation (1 if more bytes follow) bits 6-0 : next 7 bits of value- Parameters:
value- The positive integer to encode- Throws:
IOException- If an I/O error occurs when writing to file.
-
writeFlagAndVarInt
Write unsigned variable-length integer with a 1-bit flag packed into the first byte.First byte layout:
Following bytes (if any) are encoded usingbit 7 : flag bit 6 : continuation (1 if more bytes follow) bits 5-0 : lowest 6 bits of valuewriteVarInt.- Parameters:
flag- The event flagvalue- The positive integer to encode- Throws:
IOException- If an I/O error occurs when writing to file.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-