Re: [PATCH v6 29/59] perf futex-contention: Port futex-contention to use python module

sashiko-bot
2026-04-25T18:09:45Z

Sashiko AI review found 1 potential issue(s):
- [Medium] Futex contention events are silently dropped and performance degrades due to mishandled thread lookup exceptions.
--

commit fa57ea79589727dc084c228173fba34a0b5fa3d6
Author: Ian Rogers <irogers@google.com>

perf futex-contention: Port futex-contention to use python module

This commit rewrites the futex-contention python script to use the perf
python module. This avoids the overhead of executing through perf script
and improves performance significantly.

> diff --git a/tools/perf/python/futex-contention.py b/tools/perf/python/futex-contention.py
> new file mode 100755
> index 000000000000..1fc87ec0e6e5
> --- /dev/null
> +++ b/tools/perf/python/futex-contention.py
[ ... ]
> +def process_event(sample: perf.sample_event) -> None:
> + """Process a single sample event."""
> + def handle_start(tid: int, uaddr: int, op: int, start_time: int) -> None:
> + if (op & FUTEX_CMD_MASK) != FUTEX_WAIT:
> + return
> + if tid not in process_names:
> + try:
> + if session:
> + process = session.find_thread(tid)
> + if process:
> + process_names[tid] = process.comm()
> + except (TypeError, AttributeError):
> + return

Does this code silently drop futex wait events and degrade performance when a
thread cannot be resolved?

If session.find_thread(tid) raises a TypeError, the exception is caught but
the early return skips recording the start_time:

> + start_times[tid] = (uaddr, start_time)

Additionally, because the unresolved thread isn't added to process_names
(e.g., as 'unknown'), the slow lookup and exception handling are redundantly
executed on every event for that thread.

Since the output logic explicitly handles missing thread names:

[ ... ]
> + for ((t, u), stats) in sorted(durations.items()):
> + avg_ns = stats.avg()
> + print(f"{process_names.get(t, 'unknown')}[{t}] lock {u:x} contended {stats.count} times, "
> + f"{avg_ns:.0f} avg ns [max: {stats.max_time} ns, min {stats.min_time} ns]")

Should the exception handler skip the early return, and should unresolved
threads be negatively cached to avoid repeated lookups?

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260425174858.3922152-1-irogers@google.com?part=29