]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf lock contention: Enable end-timestamp accounting for cgroup aggregation
authorSuchit Karunakaran <suchitkarunakaran@gmail.com>
Sat, 30 May 2026 19:59:40 +0000 (01:29 +0530)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 4 Jun 2026 20:34:52 +0000 (17:34 -0300)
update_lock_stat() handles lock contentions that start but never reach a
contention_end event (e.g., locks still held when profiling stops), but
previously treated LOCK_AGGR_CGROUP as a no-op due to missing cgroup
context in userspace.

Fix this by adding a cgroup_id field to struct tstamp_data, recording it
at contention_begin using get_current_cgroup_id() when aggr_mode is
LOCK_AGGR_CGROUP. Capturing it at contention_begin is semantically
correct, the contention cost is incurred by the task that had to wait,
not by whatever task happens to be running at contention_end. It is also
preferable from a performance standpoint, as contention_end runs just
before the task enters the critical section.

Update contention_end to use pelem->cgroup_id instead of calling
get_current_cgroup_id() dynamically, ensuring both complete and
incomplete contention events attribute the wait time to the cgroup at
wait-start time consistently.

Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Tycho Andersen (AMD) <tycho@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/bpf_lock_contention.c
tools/perf/util/bpf_skel/lock_contention.bpf.c
tools/perf/util/bpf_skel/lock_data.h

index eb8e29b8064b7348ce33f079f884d1d7cc1178f0..b1cfa63a488fa6430ff43cdaafc5826f11e72622 100644 (file)
@@ -470,8 +470,8 @@ static void update_lock_stat(int map_fd, int pid, u64 end_ts,
                stat_key.lock_addr_or_cgroup = ts_data->lock;
                break;
        case LOCK_AGGR_CGROUP:
-               /* TODO */
-               return;
+               stat_key.lock_addr_or_cgroup = ts_data->cgroup_id;
+               break;
        default:
                return;
        }
index d4186ae9f85c7f0b33aed0309d1f2dfc8899ba55..0d9c6f55050eff9aaa629509ef39eae8f41c6dcb 100644 (file)
@@ -597,6 +597,8 @@ int contention_begin(u64 *ctx)
        pelem->timestamp = bpf_ktime_get_ns();
        pelem->lock = (__u64)ctx[0];
        pelem->flags = (__u32)ctx[1];
+       if (aggr_mode == LOCK_AGGR_CGROUP)
+               pelem->cgroup_id = get_current_cgroup_id();
 
        if (needs_callstack) {
                u32 i = 0;
@@ -832,7 +834,7 @@ skip_owner:
                        key.stack_id = pelem->stack_id;
                break;
        case LOCK_AGGR_CGROUP:
-               key.lock_addr_or_cgroup = get_current_cgroup_id();
+               key.lock_addr_or_cgroup = pelem->cgroup_id;
                break;
        default:
                /* should not happen */
index 28c5e5aced7fcc9104550f2b846763277580a32e..652e114e6b877214a8a9adb78b24a51367847cd1 100644 (file)
@@ -13,6 +13,7 @@ struct owner_tracing_data {
 struct tstamp_data {
        u64 timestamp;
        u64 lock;
+       u64 cgroup_id;
        u32 flags;
        s32 stack_id;
 };