Re: [PATCH v3 06/18] perf script: Change metric format to use json metrics

Namhyung Kim
2025-11-11T06:59:24Z

On Mon, Nov 10, 2025 at 08:04:05PM -0800, Ian Rogers wrote:
> The metric format option isn't properly supported. This change
> improves that by making the sample events update the counts of an
> evsel, where the shadow metric code expects to read the values. To
> support printing metrics, metrics need to be found. This is done on
> the first attempt to print a metric. Every metric is parsed and then
> the evsels in the metric's evlist compared to those in perf script
> using the perf_event_attr type and config. If the metric matches then
> it is added for printing. As an event in the perf script's evlist may
> have >1 metric id, or different leader for aggregation, the first
> metric matched will be displayed in those cases.
>
> An example use is:
> ```
> $ perf record -a -e '{instructions,cpu-cycles}:S' -a -- sleep 1
> $ perf script -F period,metric
> ...
> 867817
> metric: 0.30 insn per cycle
> 125394
> metric: 0.04 insn per cycle
> 313516
> metric: 0.11 insn per cycle
> metric: 1.00 insn per cycle
> ```
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
[SNIP]
> @@ -2150,23 +2296,72 @@ static void perf_sample__fprint_metric(struct perf_script *script,
> },
> .force_header = false,
> };
> - struct evsel *ev2;
> - u64 val;
> + struct perf_counts_values *count, *old_count;
> + int cpu_map_idx, thread_map_idx, aggr_idx;
> + struct evsel *pos;
> +
> + if (!init_metrics) {
> + /* One time initialization of stat_config and metric data. */
> + struct script_find_metrics_args args = {
> + .evlist = evsel->evlist,
> + /* TODO: Determine system-wide based on evlist.. */
> + .system_wide = true,

Probably you can check if the thread_map has an entry for -1.

> + };
> + if (!stat_config.output)
> + stat_config.output = stdout;
> +
> + if (!stat_config.aggr_map) {
> + /* TODO: currently only global aggregation is supported. */
> + assert(stat_config.aggr_mode == AGGR_GLOBAL);

IIUC there's no option or config to set different aggregation mode for
perf script.

Thanks,
Namhyung

> + stat_config.aggr_get_id = script_aggr_cpu_id_get;
> + stat_config.aggr_map =
> + cpu_aggr_map__new(evsel->evlist->core.user_requested_cpus,
> + aggr_cpu_id__global, /*data=*/NULL,
> + /*needs_sort=*/false);
> + }
>
> - if (!evsel->stats)
> - evlist__alloc_stats(&stat_config, script->session->evlist, /*alloc_raw=*/false);
> - if (evsel_script(leader)->gnum++ == 0)
> - perf_stat__reset_shadow_stats();
> - val = sample->period * evsel->scale;
> - evsel_script(evsel)->val = val;
> - if (evsel_script(leader)->gnum == leader->core.nr_members) {
> - for_each_group_member (ev2, leader) {
> - perf_stat__print_shadow_stats(&stat_config, ev2,
> - evsel_script(ev2)->val,
> - sample->cpu,
> - &ctx);
> + metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_metrics, &args);
> + init_metrics = true;
> + }
> +
> + if (!evsel->stats) {
> + if (evlist__alloc_stats(&stat_config, evsel->evlist, /*alloc_raw=*/true) < 0)
> + return;
> + }
> + if (!evsel->stats->aggr) {
> + if (evlist__alloc_aggr_stats(evsel->evlist, stat_config.aggr_map->nr) < 0)
> + return;
> + }
> +
> + /* Update the evsel's count using the sample's data. */
> + cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){sample->cpu});
> + thread_map_idx = perf_thread_map__idx(evsel->core.threads, sample->tid);
> + if (thread_map_idx < 0) {
> + /* Missing thread, check for any thread. */
> + if (perf_thread_map__pid(evsel->core.threads, /*idx=*/0) == -1) {
> + thread_map_idx = 0;
> + } else {
> + pr_info("Missing thread map entry for thread %d\n", sample->tid);
> + return;
> + }
> + }
> + count = perf_counts(evsel->counts, cpu_map_idx, thread_map_idx);
> + old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread_map_idx);
> + count->val = old_count->val + sample->period;
> + count->run = old_count->run + 1;
> + count->ena = old_count->ena + 1;
> +
> + /* Update the aggregated stats. */
> + perf_stat_process_counter(&stat_config, evsel);
> +
> + /* Display all metrics. */
> + evlist__for_each_entry(evsel->evlist, pos) {
> + cpu_aggr_map__for_each_idx(aggr_idx, stat_config.aggr_map) {
> + perf_stat__print_shadow_stats(&stat_config, pos,
> + count->val,
> + aggr_idx,
> + &ctx);
> }
> - evsel_script(leader)->gnum = 0;
> }
> }
>
> @@ -2348,7 +2543,7 @@ static void process_event(struct perf_script *script,
> }
>
> if (PRINT_FIELD(METRIC))
> - perf_sample__fprint_metric(script, thread, evsel, sample, fp);
> + perf_sample__fprint_metric(thread, evsel, sample, fp);
>
> if (verbose > 0)
> fflush(fp);
> --
> 2.51.2.1041.gc1ab5b90ca-goog
>