Package com.helix.profiler.flamegraph
Class FlameGraphAggregator
java.lang.Object
com.helix.profiler.flamegraph.FlameGraphAggregator
- All Implemented Interfaces:
ProfileEventListener,AutoCloseable
In-memory folded stack trace aggregation engine that transforms execution and allocation samples
from internal profilers, thread dumps, and JDK Flight Recorder (JFR) into hierarchical flame graph call trees.
Supports dynamic switching between CPU execution samples (MetricType.CPU_TIME) and
Heap memory allocation bytes (MetricType.ALLOCATION_BYTES).
Outputs folded stack data strictly matching Brendan Gregg's FlameGraph specification.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFoldedLine(MetricType metric, String line) Parses and aggregates a single Brendan Gregg folded stack trace line into the specified metric.voidaddFoldedLine(String line) Parses and aggregates a single Brendan Gregg folded stack trace line (e.g.voidaddSample(MetricType metric, StackTraceElement[] stackTrace, long value) Ingests a JVMStackTraceElementarray into the specified metric.voidaddSample(MetricType metric, List<String> frames, long value) Adds a stack trace sample with the specified value to a target metric.voidaddSample(StackTraceElement[] stackTrace, long value) Ingests a JVMStackTraceElementarray into the active metric.voidAdds a single count stack trace sample to the active metric.voidAdds a stack trace sample with the specified value to the active metric.voidclear()Clears aggregated data for all metrics.voidclear(MetricType metric) Clears aggregated data for the specified metric.voidclose()Exports the aggregated stack traces for the active metric in Brendan Gregg folded format.exportFolded(MetricType metric) Exports the aggregated stack traces for the specified metric in Brendan Gregg folded format.voidexportFolded(MetricType metric, Writer writer) Writes the folded stack traces to the provided Writer.voidexportFolded(MetricType metric, Path destination) Writes the folded stack traces for the specified metric to the destination path.voidexportFolded(Path destination) Writes the folded stack traces for the active metric to the specified destination path.intReturns the count of distinct call stack paths terminating with samples.intgetDistinctStacksCount(MetricType metric) Returns the count of distinct call stack paths terminating with samples for the specified metric.intReturns the maximum depth of the flame graph call tree for the active metric.intgetMaxDepth(MetricType metric) Returns the maximum depth of the flame graph call tree for the specified metric.Gets the currently active metric dimension.Retrieves the root prefix trie node for the active metric.getRootNode(MetricType metricType) Retrieves the root prefix trie node for the specified metric.longReturns total samples/value aggregated for the active metric.longgetTotalSamples(MetricType metric) Returns total samples/value aggregated for the specified metric.voidingestFoldedFile(MetricType metric, Path path) Ingests folded stack data from a file for a specific metric.voidingestFoldedFile(Path path) Ingests folded stack data from a file.voidingestFoldedText(MetricType metric, String foldedText) Ingests a multiline block of Brendan Gregg folded stack data for a specific metric.voidingestFoldedText(String foldedText) Ingests a multiline block of Brendan Gregg folded stack data.voidIngests events from a JDK Flight Recorder (.jfr) recording file.voidingestJfrEvent(RecordedEvent event) Ingests a single JFR RecordedEvent, automatically detecting CPU vs allocation metrics.voidingestThreadDump(MetricType metric, Map<Thread, StackTraceElement[]> traces) Ingests a thread dump snapshot into the specified metric.voidingestThreadDump(Map<Thread, StackTraceElement[]> traces) Ingests a thread dump snapshot (e.g.booleanvoidonEvent(ProfileEvent event) Called when a profiler event is emitted.voidPerforms a one-shot sample of all live JVM threads and aggregates them into the active metric.voidsetActiveMetric(MetricType metricType) voidsetMetric(MetricType metricType) Dynamically sets the active metric dimension.voidstartThreadSampling(long interval, TimeUnit unit) Starts an asynchronous background periodic thread sampling loop.voidStops the background thread sampling loop.
-
Constructor Details
-
FlameGraphAggregator
public FlameGraphAggregator()
-
-
Method Details
-
setMetric
Dynamically sets the active metric dimension. -
setActiveMetric
-
getMetric
Gets the currently active metric dimension. -
getRootNode
Retrieves the root prefix trie node for the active metric. -
getRootNode
Retrieves the root prefix trie node for the specified metric. -
addSample
Adds a stack trace sample with the specified value to the active metric.- Parameters:
frames- ordered call stack frames (root caller at index 0, leaf callee at last index)value- weight/count or allocation bytes
-
addSample
Adds a single count stack trace sample to the active metric. -
addSample
Adds a stack trace sample with the specified value to a target metric.- Parameters:
metric- target metric dimensionframes- ordered call stack frames (root caller at index 0, leaf callee at last index)value- weight/count or allocation bytes
-
addSample
Ingests a JVMStackTraceElementarray into the active metric. -
addSample
Ingests a JVMStackTraceElementarray into the specified metric. -
ingestThreadDump
Ingests a thread dump snapshot (e.g. from Thread.getAllStackTraces()). -
ingestThreadDump
Ingests a thread dump snapshot into the specified metric. -
sampleAllThreads
public void sampleAllThreads()Performs a one-shot sample of all live JVM threads and aggregates them into the active metric. -
ingestJfr
Ingests events from a JDK Flight Recorder (.jfr) recording file. Automatically extracts CPU samples (jdk.ExecutionSample) intoMetricType.CPU_TIMEand heap allocations (jdk.ObjectAllocation*) intoMetricType.ALLOCATION_BYTES.- Parameters:
jfrFile- path to .jfr file- Throws:
IOException- if reading the file fails
-
ingestJfrEvent
Ingests a single JFR RecordedEvent, automatically detecting CPU vs allocation metrics. -
addFoldedLine
Parses and aggregates a single Brendan Gregg folded stack trace line (e.g. "func1;func2;func3 42"). -
addFoldedLine
Parses and aggregates a single Brendan Gregg folded stack trace line into the specified metric. -
ingestFoldedText
Ingests a multiline block of Brendan Gregg folded stack data. -
ingestFoldedText
Ingests a multiline block of Brendan Gregg folded stack data for a specific metric. -
ingestFoldedFile
Ingests folded stack data from a file.- Throws:
IOException
-
ingestFoldedFile
Ingests folded stack data from a file for a specific metric.- Throws:
IOException
-
startThreadSampling
Starts an asynchronous background periodic thread sampling loop.- Parameters:
interval- interval between thread dump samplesunit- time unit
-
stopThreadSampling
public void stopThreadSampling()Stops the background thread sampling loop. -
isThreadSamplingActive
public boolean isThreadSamplingActive() -
onEvent
Description copied from interface:ProfileEventListenerCalled when a profiler event is emitted.- Specified by:
onEventin interfaceProfileEventListener- Parameters:
event- the profiler event
-
exportFolded
Exports the aggregated stack traces for the active metric in Brendan Gregg folded format.- Returns:
- newline-delimited folded stack lines ("frame1;frame2;frame3 42")
-
exportFolded
Exports the aggregated stack traces for the specified metric in Brendan Gregg folded format. -
exportFolded
Writes the folded stack traces for the active metric to the specified destination path.- Throws:
IOException
-
exportFolded
Writes the folded stack traces for the specified metric to the destination path.- Throws:
IOException
-
exportFolded
Writes the folded stack traces to the provided Writer.- Throws:
IOException
-
getTotalSamples
public long getTotalSamples()Returns total samples/value aggregated for the active metric. -
getTotalSamples
Returns total samples/value aggregated for the specified metric. -
getDistinctStacksCount
public int getDistinctStacksCount()Returns the count of distinct call stack paths terminating with samples. -
getDistinctStacksCount
Returns the count of distinct call stack paths terminating with samples for the specified metric. -
getMaxDepth
public int getMaxDepth()Returns the maximum depth of the flame graph call tree for the active metric. -
getMaxDepth
Returns the maximum depth of the flame graph call tree for the specified metric. -
clear
public void clear()Clears aggregated data for all metrics. -
clear
Clears aggregated data for the specified metric. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-