Terminal Flame Graphs for Performance Analysis
Collecting flame graph data is already straightforward, but viewing the results could be more convenient. HuaTuo profiler provides a terminal user interface (TUI) for viewing samples directly in the terminal, without installing additional tools such as Grafana, Pyroscope, or brendangregg/FlameGraph. The relevant options are:
--output-path value Output path: file|directory|socket|stdout (WARNING: stdout may expose data)
--output-format value Output format: collapsed (folded stacks), flamegraph (SVG, alias: svg), tui, json, speedscope, chrometrace, remote-store (default: "collapsed")
--tui-open FILE Open a saved profiling report from FILE in the terminal
View Flame Graphs in the Terminal
Set --output-format=tui to open a flame graph directly in the terminal when collection finishes.
./_output/bin/profiler --type cpu --language go \
--pid "$BASHPID" \
--thread-group \
--duration 5 \
--aggr-interval 1 \
--output-format=tui
Figure 1. The default view.
Group Flame Graphs by Different Dimensions
Start by checking which CPUs the samples came from, then examine the call stacks of individual processes. Press g in the flame graph, enter comma-separated fields, and press Enter to apply. The field order determines the grouping hierarchy.
Enter cpu,comm to group by CPU first, then by task name (comm), showing which tasks were sampled on each CPU.
Figure 2. Samples from profiler are distributed across CPUs 1, 2, and 3.
To examine profiler’s overall hotspots, change the grouping to comm. Samples from tasks with the same name across CPUs are combined into a single call tree, making their call stacks easier to examine together.
Figure 3. Aggregated call stacks under comm="profiler".
Tasks with the same name may belong to different processes. Change the grouping to comm,pid to separate them by PID under each task name.
Figure 4. This capture includes only one profiler process, with PID 3981984.
To restore the default view, press g, clear the fields with Ctrl+U, and press Enter to apply.
Inspect Call Hierarchies and Hotspots
Grouping shows which CPU or process each sample belongs to. To inspect the call hierarchy and each frame’s percentage, press v once from the flame graph to open the all-frames list.
Figure 5. The all-frames list retains the current task-name and PID grouping hierarchy.
To find functions with high costs directly, press v again to open the Top Table. It ranks functions across the entire capture using two metrics:
- Self: Sample weight attributed to a function when it is the leaf of the stack.
- Total: Sample weight accumulated for every occurrence of a function in the stack, including recursive calls.
The default order is descending Self, showing each function’s own cost. Press s to switch to descending Total and examine cumulative cost, including callees. The distinction matters: a function may have little cost of its own while the functions it calls consume substantial time.
Figure 6. parseKallsymsLine has approximately 20.2 ms of Self time and 40.4 ms of Total time.
Save Profiling Data
Specify --output-path=self.gob during collection. After you press q to exit the viewer, profiler writes the call stacks, weights, and grouping labels to that file. To resume the analysis later, open the report:
./_output/bin/profiler --tui-open self.gob
You can still change the grouping after reopening the report. Because each view uses data from the same capture, you can compare hotspots across different groupings. Omit --output-path if you only need to view the results immediately.
Time to Open 100,000 Samples
On an Ubuntu 20.04 virtual machine with 4 vCPUs, opening a gob file containing 100,000 synthetic sample records took 0.72 seconds to fully display the first terminal screen, measured as the median of 5 runs.
Figure 7. Processing times for synthetic samples. TUI timing ends when the first screen is displayed; FlameGraph timing ends when the SVG has been written. The input formats and timing endpoints differ, so these numbers cannot establish a speedup for equivalent rendering work.
Terminal flame graphs have been merged into HuaTuo’s main branch. For options and keyboard shortcuts, see the documentation on terminal flame graphs and report viewing.