如何将perf.data转换为文本/CSV?提取1ms采样的硬件计数器数据
Got it, let's tackle how to pull that 1ms interval hardware counter data out of your perf.data file. Since perf stat doesn't support intervals smaller than 10ms, working directly with the perf.data from your 1ms-sampled perf record run is the right approach. Here are the most straightforward methods:
1. Use perf script (Perf's Built-in Tool)
perf script is designed to parse and output raw data from perf.data in human-readable or machine-friendly formats. It’s perfect for extracting timestamped counter values.
Basic Raw Data Export
To get each sample’s timestamp, event name, and counter value (period), run:
perf script -F time,event,period -i perf.data > raw_counter_data.csv
-F time,event,period: Specifies which fields to output (timestamp in seconds, the hardware event you sampled, and the counter’s raw value for that sample).-i perf.data: Explicitly tellsperf scriptto use your existing data file (optional if it’s the defaultperf.data).
If you need additional context (like which CPU/process the sample came from), add more fields to the -F flag. Run perf script -F list to see all available fields. For example:
perf script -F time,cpu,pid,comm,event,period -i perf.data > raw_data_with_context.csv
Aggregate to 1ms Intervals
If you want to group multiple samples that fall into the same 1ms window (to account for minor timing jitter), use a simple awk script to sum values per millisecond.
Create a file named aggregate_1ms.awk with this content:
BEGIN { prev_ms = 0 total = 0 print "timestamp_ms,counter_total" } { curr_ms = int($1 * 1000) # Convert timestamp from seconds to milliseconds if (curr_ms != prev_ms) { if (prev_ms != 0) { print prev_ms "," total } prev_ms = curr_ms total = $3 } else { total += $3 } } END { print prev_ms "," total }
Then run:
perf script -F time,period -i perf.data | awk -f aggregate_1ms.awk > aggregated_1ms_counter.csv
This outputs a CSV where each line represents the total counter value for a single millisecond interval.
2. Custom Scripting for Advanced Needs
If you need more control (like filtering specific events or complex data transformations), use perf script with a custom Python/Perl script. Here’s a minimal Python snippet to parse and structure the data:
import sys import csv writer = csv.writer(sys.stdout) writer.writerow(["timestamp_ms", "event", "period"]) for line in sys.stdin: parts = line.strip().split() if len(parts) < 3: continue timestamp_sec = float(parts[0]) timestamp_ms = int(timestamp_sec * 1000) event = parts[1] period = int(parts[2]) writer.writerow([timestamp_ms, event, period])
Run it like this:
perf script -F time,event,period -i perf.data | python parse_perf.py > parsed_counter_data.csv
Quick Check on Your Initial perf record Command
Just to confirm, you should have run something like this to generate your perf.data with 1ms sampling:
perf record -e cycles -i 1ms -o perf.data -- ./your_target_program
Replace cycles with whatever hardware event you’re tracking (e.g., instructions, cache-misses).
内容的提问来源于stack exchange,提问作者onkar.omr

