关于Perf工具计数器格式说明符与stat子命令interval-print选项counts unit列格式说明符的技术咨询
Great question! Let's break this down clearly to answer both parts of your inquiry:
In the perf tool, format specifiers for counters are mostly aligned with standard C printf specifiers, but with extensions to handle 64-bit values and automatic unit scaling. Common ones include:
%lldor%'lld: For 64-bit signed integer values (the'adds thousands separators for readability)%llu: For 64-bit unsigned integer values- Perf also uses helper functions like
perf_event__scnprintfthat wrap these base specifiers to automatically adjust units (e.g., K, M, G) based on the counter's magnitude, making large values more readable.
perf stat -I Output Looking at the tools/perf/util/stat-display.c file you referenced, the "counts unit" column is generated by combining the counter's numerical value and its associated unit string.
The core format logic lives in functions like print_interval or format_interval (depending on your perf version). The key format string used is:
"%'lld %s"
Here's what each part does:
%'lld: Formats the 64-bit counter value as a signed long long integer, with thousands separators added for large numbers%s: Inserts the event's unit string (like "xyz" from your example output)
Behind the scenes, perf uses perf_event__scnprintf to handle automatic unit scaling (e.g., converting raw counts to K/M/G when appropriate) before applying this format, ensuring the output stays readable no matter how large the counter value is.
For your example line 1.001177595 110 xyz, the 110 xyz is exactly the result of this format: the small count 110 uses %'lld (no separators needed here) followed by the unit string xyz via %s.
内容的提问来源于stack exchange,提问作者Milan

