Skip to contentSkip to conversation

perf stat -D: CPUs_utilized metric includes delay period (regression in 6.19)

BUG Francesco Nigro 1 participant 1 message May 17

lore
Share & tools
lore
#0 lore raw

Hi,

I found a regression in perf stat where the CPUs_utilized metric is incorrect when using -D (delay).

The metric divides task-clock by total wall time including the delay period, instead of only the measurement window. This was introduced in 6.19 by commit b71f46a6a708 ("perf stat: Remove hard coded shadow metrics").

Reproducer:

  # Start a CPU-bound process pinned to 2 cores
  taskset -c 0,1 stress-ng --cpu 2 --timeout 60 &
  PID=$!
  perf stat -D 10000 --timeout 10000 -p $PID
  kill $PID

Expected:

20,000.xx msec task-clock # 2.0 CPUs CPUs_utilized
...
10.0xx seconds time elapsed

Actual:

20,000.xx msec task-clock # 1.0 CPUs CPUs_utilized
...
10.0xx seconds time elapsed

The "seconds time elapsed" line correctly shows ~10s (measurement window only), but CPUs_utilized computes 20000/20000 = 1.0 instead of 20000/10000 = 2.0.

Root cause:

The JSON-defined metric in pmu-events/arch/common/common/metrics.json uses duration_time as the denominator:

"MetricExpr": "(software@cpu\\-clock@ ...) / (duration_time * 1e9)"

The duration_time tool PMU event captures start_time in evsel__tool_pmu_open (tool_pmu.c) which runs BEFORE the -D delay sleep in __run_perf_stat. So duration_time = delay + measurement window.

The old hardcoded code in stat-shadow.c used walltime_nsecs_stats (set AFTER the delay completes) as the denominator, correctly excluding the delay.

Suggested fix:

Reset evsel->start_time when counters are enabled after the initial delay, not when they are opened:

// In the enable path, after delay completes:
if (ev == TOOL_PMU__EVENT_DURATION_TIME)

      evsel->start_time = rdclock();

Alternatively, introduce a measurement_time tool PMU event that reflects only the measurement window, and use it in the metric expression.

Affected: Linux 6.19+ (confirmed on 6.19.14-200.fc43.x86_64) Not affected: 6.14 and earlier

Thanks,
Francesco