Skip to contentSkip to conversation

perf/lock: enable end-timestamp accounting for cgroup aggregation

PATCH Superseded Suchit Karunakaran 2 participants 2 messages Apr 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf/lock: enable end-timestamp accounting for cgroup aggregation

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.

---
 tools/perf/util/bpf_lock_contention.c          | 4 ++--
 tools/perf/util/bpf_skel/lock_contention.bpf.c | 4 +++-
 tools/perf/util/bpf_skel/lock_data.h           | 1 +
 3 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/util/bpf_lock_contention.c +2 -2
index cbd7435579fe..1a5bd2ff8ee4 100644
@@ -463,8 +463,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;
 	}
tools/perf/util/bpf_skel/lock_contention.bpf.c +3 -1
index 96e7d853b9ed..139199811020 100644
@@ -536,6 +536,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;
@@ -771,7 +773,7 @@ int contention_end(u64 *ctx)
 			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 */
tools/perf/util/bpf_skel/lock_data.h +1 -0
index 28c5e5aced7f..652e114e6b87 100644
@@ -13,6 +13,7 @@ struct owner_tracing_data {
 struct tstamp_data {
 	u64 timestamp;
 	u64 lock;
+	u64 cgroup_id;
 	u32 flags;
 	s32 stack_id;
 };
Signature · 1 line
2.53.0
#1 lore raw
Sashiko AI review found 1 potential issue(s): - [Medium] Incomplete spinlock and rwlock contentions are silently dropped under cgroup aggregation because of a leftover early return. --

Sashiko AI review found 1 potential issue(s):
- [Medium] Incomplete spinlock and rwlock contentions are silently dropped under cgroup aggregation because of a leftover early return. --

commit 5bccbbefb61cd7effb0fec5a7f0bee5add3ba114
Author: Suchit Karunakaran <[email protected]>

perf/lock: enable end-timestamp accounting for cgroup aggregation

This commit adds a cgroup_id field to struct tstamp_data, recording it at contention_begin for cgroup aggregation mode. It also updates update_lock_stat() to use this recorded cgroup_id, enabling accounting for incomplete contentions that start but never reach a contention_end event.

Quoted 5 lines
tools/perf/util/bpf_lock_contention.c +0 -0
index cbd7435579feaf..1a5bd2ff8ee4c2 100644

tools/perf/util/bpf_lock_contention.c static void update_lock_stat(int map_fd, int pid, u64 end_ts, · line 470

Quoted 10 lines
tools/perf/util/bpf_lock_contention.c +2 -2
@@ -463,8 +463,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;
 	}

Now that update_lock_stat() handles cgroup aggregation, does account_end_timestamp() also need an update to stop returning early?

Looking at account_end_timestamp() in tools/perf/util/bpf_lock_contention.c:

	/* Now it'll check per-cpu tstamp map which doesn't have TID. */
	if (aggr_mode == LOCK_AGGR_TASK || aggr_mode == LOCK_AGGR_CGROUP)
		return;

	total_cpus = cpu__max_cpu().cpu;
	ts_fd = bpf_map__fd(skel->maps.tstamp_cpu);

Because of this early return, will incomplete spinlock and rwlock contentions (which use the tstamp_cpu map) still be dropped when using cgroup aggregation?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1