From nobody Thu Dec 18 08:35:44 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D2AB263F59 for ; Mon, 24 Feb 2025 18:49:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740422989; cv=none; b=mxf4De1bC8Ee2LUAxymM80l5GEi9qzFlJpspiFFaLNErKdumHN189TK10H6meojnaEx9huhagv4q7NQjcEuQa9VWYihShBfeNYj96O9eRIXp8VDoSLW7Bu2UEu61fm611BqH8s66WbkvXMLyF5r+cV5nEqHFtOfPnoGc50Hok50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740422989; c=relaxed/simple; bh=uMnqvAw20MjO8y1DkOGmvKZ76yZAmQWMHc3q5tVE6c4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=di027tMjOGv+jigghW+lYxv3c/FXbPUTmTEnfu3+3UsBvLKL+/0EJAnx7C6zjuUmTHlyLjKJTzGTH3W2NTTlIeYprfYAbhQlKB+huDQR6u/+iB0Q9wasjbg8zaPVnkQNKnulFarShGeCKWEdsK0wDnTBD8XcVaDOZgiq4qAuF0w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z1KpQT9j; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z1KpQT9j" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fc404aaed5so15725357a91.3 for ; Mon, 24 Feb 2025 10:49:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740422987; x=1741027787; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3LIHL/AK+Ozu93SKs05RSuuBRbA8GccmzuqZN2pzdx8=; b=Z1KpQT9jJX3mdhPHgOKCypSGCxakMaANE2uRlvMkinlGzTPv3ZLXQBlOV48WN3twTp uoPkMmF8B558NhoBpFTLxXvk6TZme2qczs8SKs+Bxlxl8qifVb1kd60cBLymQm6jAZ7O rk4f9mjNijsxy3U2jvb4NGKp6mlJ8DYbp5l1nZ2E4bbu0xLmJYKt91jJgJpbxIc1mayd EPIW7iW7ZluAF/TzEjOpvrjuHZylX+3Wvs8U+M+lL+fGX6tHl3YMrDu0ZU17VCksYFsF zVTaLfK44/GWC93WVPe1XEmxENa6d6cMlsy8H3jSso7+zovPMLGB6L3zqhtwc1P6/4Q5 5wgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740422987; x=1741027787; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3LIHL/AK+Ozu93SKs05RSuuBRbA8GccmzuqZN2pzdx8=; b=KtdB9m4CUevyGMZqjkkaysbtpaeuG7kCU5Fz1hY+744/8G0cxv8StZx0oSJEqlAXa8 zfNgftBzaxJds2r+OttD8jA38t7oNufkBEGXHYmpjOMCWdi06EETjhlZrsOUaE1gVck9 uwfPmKkqMcEF3rUS2KWhb+MMAVDGFW4QCAi40swMH6ZHT3lqEdXjPgdyGdmhT7HhP+uO 4lBQg1hMH/k1XbtFkrC+VBB70B0vaoanvS7XLt+3JNyFQbDvq3LElHsTVyyJjXwsuXaP DYg4s/j3sHVbD0r6usYQ7tpl6B+kfPR9xSWOwnT6fi9Dy2biIsUpfBfIzG/2ss6hxKg6 YXEA== X-Gm-Message-State: AOJu0Yw1ySi+Or00BRT1c9/o3XjHQPGkrqGYw31YznpNOJfDwxk8cJQO ydImYtCTwe8LGEI0LbE6VfcTe8va8oXz1TO9KTR2BR66kAupbmmNvS3Fr7E69+AqimK4IGkCmtE SPPM8WL6QWMmsjJhjq9xYhAn8cLWP2FhFKbLlBDfidr3wc4dX4WDLlhRJb3WHD50KODvyHyLV3f DjsgUlDjnnjZ9t+TPa2tjDEBNmvKr+Vfu81XAoOSQ7 X-Google-Smtp-Source: AGHT+IEh1EoJ/IsWmaDouBD9aRZTYrXlU/wFe3qPhbp9wKPPXJthGmxpT4u1854QjNGNn1vgPxuVUrbYIGg= X-Received: from pjbtb3.prod.google.com ([2002:a17:90b:53c3:b0:2f4:465d:5c61]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e51:b0:2ea:4c8d:c7a2 with SMTP id 98e67ed59e1d1-2fce7b23af6mr25948512a91.24.1740422986785; Mon, 24 Feb 2025 10:49:46 -0800 (PST) Date: Mon, 24 Feb 2025 10:42:48 -0800 In-Reply-To: <20250224184742.4144931-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250224184742.4144931-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250224184742.4144931-2-ctshao@google.com> Subject: [PATCH v7 1/4] perf lock: Add bpf maps for owner stack tracing From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, nick.forrington@arm.com, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a struct and few bpf maps in order to tracing owner stack. `struct owner_tracing_data`: Contains owner's pid, stack id, timestamp for when the owner acquires lock, and the count of lock waiters. `stack_buf`: Percpu buffer for retrieving owner stacktrace. `owner_stacks`: For tracing owner stacktrace to customized owner stack id. `owner_data`: For tracing lock_address to `struct owner_tracing_data` in bpf program. `owner_stat`: For reporting owner stacktrace in usermode. Signed-off-by: Chun-Tse Shao --- tools/perf/util/bpf_lock_contention.c | 14 ++++++-- .../perf/util/bpf_skel/lock_contention.bpf.c | 33 +++++++++++++++++++ tools/perf/util/bpf_skel/lock_data.h | 7 ++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index fc8666222399..76542b86e83f 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -131,10 +131,20 @@ int lock_contention_prepare(struct lock_contention *c= on) else bpf_map__set_max_entries(skel->maps.task_data, 1); =20 - if (con->save_callstack) + if (con->save_callstack) { bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries); - else + if (con->owner) { + bpf_map__set_value_size(skel->maps.stack_buf, con->max_stack * sizeof(u= 64)); + bpf_map__set_key_size(skel->maps.owner_stacks, + con->max_stack * sizeof(u64)); + bpf_map__set_max_entries(skel->maps.owner_stacks, con->map_nr_entries); + bpf_map__set_max_entries(skel->maps.owner_data, con->map_nr_entries); + bpf_map__set_max_entries(skel->maps.owner_stat, con->map_nr_entries); + skel->rodata->max_stack =3D con->max_stack; + } + } else { bpf_map__set_max_entries(skel->maps.stacks, 1); + } =20 if (target__has_cpu(target)) { skel->rodata->has_cpu =3D 1; diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/ut= il/bpf_skel/lock_contention.bpf.c index 6533ea9b044c..23fe9cc980ae 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -27,6 +27,38 @@ struct { __uint(max_entries, MAX_ENTRIES); } stacks SEC(".maps"); =20 +/* buffer for owner stacktrace */ +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u64)); + __uint(max_entries, 1); +} stack_buf SEC(".maps"); + +/* a map for tracing owner stacktrace to owner stack id */ +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__u64)); // owner stacktrace + __uint(value_size, sizeof(__s32)); // owner stack id + __uint(max_entries, 1); +} owner_stacks SEC(".maps"); + +/* a map for tracing lock address to owner data */ +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__u64)); // lock address + __uint(value_size, sizeof(struct owner_tracing_data)); + __uint(max_entries, 1); +} owner_data SEC(".maps"); + +/* a map for contention_key (stores owner stack id) to contention data */ +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(struct contention_key)); + __uint(value_size, sizeof(struct contention_data)); + __uint(max_entries, 1); +} owner_stat SEC(".maps"); + /* maintain timestamp at the beginning of contention */ struct { __uint(type, BPF_MAP_TYPE_HASH); @@ -143,6 +175,7 @@ const volatile int needs_callstack; const volatile int stack_skip; const volatile int lock_owner; const volatile int use_cgroup_v2; +const volatile int max_stack; =20 /* determine the key of lock stat */ const volatile int aggr_mode; diff --git a/tools/perf/util/bpf_skel/lock_data.h b/tools/perf/util/bpf_ske= l/lock_data.h index c15f734d7fc4..15f5743bd409 100644 --- a/tools/perf/util/bpf_skel/lock_data.h +++ b/tools/perf/util/bpf_skel/lock_data.h @@ -3,6 +3,13 @@ #ifndef UTIL_BPF_SKEL_LOCK_DATA_H #define UTIL_BPF_SKEL_LOCK_DATA_H =20 +struct owner_tracing_data { + u32 pid; // Who has the lock. + u32 count; // How many waiters for this lock. + u64 timestamp; // The time while the owner acquires lock and contention i= s going on. + s32 stack_id; // Identifier for `owner_stat`, which stores as value in `o= wner_stacks` +}; + struct tstamp_data { u64 timestamp; u64 lock; --=20 2.48.1.658.g4767266eb4-goog From nobody Thu Dec 18 08:35:44 2025 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4876E266184 for ; Mon, 24 Feb 2025 18:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740423030; cv=none; b=dH90RFzDC6LLb4smM/9m4OcIYxZD1w4xfg3VEX0STLZ0lYqaC6/kvl3ftIAzlp1m4XkvMJc38b/rZiR0y2RE5Vxkabrsf2mWFojPHFlyVzORQPxVKq6X5I2z8DorfeK85ar76yOvnCsKKxGeqUTi4fI+Cv6Yq2OywwDaX65epGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740423030; c=relaxed/simple; bh=sX6k+ExVgGNyDnumdmJIdayUEA+By8ga08Ti0Wfuoc8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dPuqxSNTylz+aMkZ9PRdNfzz2K7T1udf+rxrw3opCdgc4Nwf/DLq3XRLlTJXWzBWsvFzxYvDskSPyLgP/O27jtyL4p0jEsS4HyF2hW3jKcCxLBlHIfPbHcHhZEhPqAcZpao8YwszT94ii9WFP9cgqhksv0TboPsRpA6E9jYNRaI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HQd5SDJl; arc=none smtp.client-ip=209.85.210.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HQd5SDJl" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7272aebb457so1140192a34.0 for ; Mon, 24 Feb 2025 10:50:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740423027; x=1741027827; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uT5qN4teqIhN8OgGcN83osIEOThqIeAxronJTF18w94=; b=HQd5SDJlfoj9fEmnTN3O/DrPD5/CwTXNx7y/8MBBaA4+Ap+Y4LrrCpCtGzdrRA2K1q +RoqQ9tL/tGbO+/U7glBf8xDxk1PR6psZf7wU+eTPm0Fjr8rAFJ9ZqS8Li9o5uVsa/bQ MSKMSmgr75wb6ygHy3kzDTVOdlpp9psPqT0mVCkPO8ceH7qDp3KUVNzPISdDomCLvIJy ULVP2eVIDM4eFLDaziNiK5aX0OwT4TG2MKRfEd8aZWSljV3Hb8gbYd3xQ9FwOb16v35K J5Kqcg7rhrRXVH20UI44GHEPEskXA67F68LHGSPm/MjOaAPgVhNd75hCW9KlqDbQMFeY xVOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740423027; x=1741027827; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uT5qN4teqIhN8OgGcN83osIEOThqIeAxronJTF18w94=; b=caNyUkiD8+0J2pDFHU0o5H5i2Hrw8r7D+bslgLFADQB/+JcCtWtv+yiFfNRbyVaYdr pAxdLlYsFfPDciesCKojyf1UoXg/toLw292Qgt71S4hiFlHlK+fgBy0gH5bIptFeJ5Qo rdNnArFdqOjbIH9lwIvzVCC1MWJOUfZmi74ZiLFWz5m9JkGWGefUAd3MX8VtTwANQhSQ zyuYDdCkq9aAr+jYNn904i28WkLVSR7+k9pWlpkfVq72xuyrPPZpkWwPfPbf1wMfghI5 x6cb9BqLOIs9vVkbODLaZsblxNpwhJLG2erFEvUrC4BoSeQAEcLWRmGkeppUT4g0WOI1 hC6g== X-Gm-Message-State: AOJu0YxwlHwGUDhdIndSKwQUMLTkkox/oWyfxPFBwhzx5tv9lfYw5f46 ys4XalfOSe+V/hV2r27QRcG8Q7YxgH3NGdGt50dR9cuL7052UCHh88oXGxwXgKHNw2PVtv0NLP1 RwYA8MHe2uBnO/x9IAJ3/H6TFxglj42Ym5YcgtlvLkK/QvPzLZqpXsbTdx4r5hgXDUKZnLhEKjD zAthfcp+95r6+DEimH9K7r/boaEDG7aUwihN4AKjol X-Google-Smtp-Source: AGHT+IFmXbyoB2zLuG6R4z8qhunshoOwoK13liFzIrD6zfHfTgWx575xZoSL1MnlHCxkGXdM9TNGIb7dzpM= X-Received: from oablc15.prod.google.com ([2002:a05:6871:418f:b0:2bc:60dd:3800]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6830:6205:b0:727:3111:1412 with SMTP id 46e09a7af769-7289d17e470mr310076a34.24.1740423027409; Mon, 24 Feb 2025 10:50:27 -0800 (PST) Date: Mon, 24 Feb 2025 10:42:49 -0800 In-Reply-To: <20250224184742.4144931-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250224184742.4144931-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250224184742.4144931-3-ctshao@google.com> Subject: [PATCH v7 2/4] perf lock: Retrieve owner callstack in bpf program From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, nick.forrington@arm.com, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This implements per-callstack aggregation of lock owners in addition to per-thread. The owner callstack is captured using `bpf_get_task_stack()` at `contention_begin()` and it also adds a custom stackid function for the owner stacks to be compared easily. The owner info is kept in a hash map using lock addr as a key to handle multiple waiters for the same lock. At `contention_end()`, it updates the owner lock stat based on the info that was saved at `contention_begin()`. If there are more waiters, it'd update the owner pid to itself as `contention_end()` means it gets the lock now. But it also needs to check the return value of the lock function in case task was killed by a signal or something. Signed-off-by: Chun-Tse Shao --- .../perf/util/bpf_skel/lock_contention.bpf.c | 212 +++++++++++++++++- 1 file changed, 203 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/ut= il/bpf_skel/lock_contention.bpf.c index 23fe9cc980ae..69be7a4234e0 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -197,6 +197,9 @@ int data_fail; int task_map_full; int data_map_full; =20 +struct task_struct *bpf_task_from_pid(s32 pid) __ksym __weak; +void bpf_task_release(struct task_struct *p) __ksym __weak; + static inline __u64 get_current_cgroup_id(void) { struct task_struct *task; @@ -420,6 +423,61 @@ static inline struct tstamp_data *get_tstamp_elem(__u3= 2 flags) return pelem; } =20 +static inline s32 get_owner_stack_id(u64 *stacktrace) +{ + s32 *id, new_id; + static s64 id_gen =3D 1; + + id =3D bpf_map_lookup_elem(&owner_stacks, stacktrace); + if (id) + return *id; + + new_id =3D (s32)__sync_fetch_and_add(&id_gen, 1); + + bpf_map_update_elem(&owner_stacks, stacktrace, &new_id, BPF_NOEXIST); + + id =3D bpf_map_lookup_elem(&owner_stacks, stacktrace); + if (id) + return *id; + + return -1; +} + +static inline void update_contention_data(struct contention_data *data, u6= 4 duration, u32 count) +{ + __sync_fetch_and_add(&data->total_time, duration); + __sync_fetch_and_add(&data->count, count); + + /* FIXME: need atomic operations */ + if (data->max_time < duration) + data->max_time =3D duration; + if (data->min_time > duration) + data->min_time =3D duration; +} + +static inline void update_owner_stat(u32 id, u64 duration, u32 flags) +{ + struct contention_key key =3D { + .stack_id =3D id, + .pid =3D 0, + .lock_addr_or_cgroup =3D 0, + }; + struct contention_data *data =3D bpf_map_lookup_elem(&owner_stat, &key); + + if (!data) { + struct contention_data first =3D { + .total_time =3D duration, + .max_time =3D duration, + .min_time =3D duration, + .count =3D 1, + .flags =3D flags, + }; + bpf_map_update_elem(&owner_stat, &key, &first, BPF_NOEXIST); + } else { + update_contention_data(data, duration, 1); + } +} + SEC("tp_btf/contention_begin") int contention_begin(u64 *ctx) { @@ -437,6 +495,72 @@ int contention_begin(u64 *ctx) pelem->flags =3D (__u32)ctx[1]; =20 if (needs_callstack) { + u32 i =3D 0; + u32 id =3D 0; + int owner_pid; + u64 *buf; + struct task_struct *task; + struct owner_tracing_data *otdata; + + if (!lock_owner) + goto skip_owner; + + task =3D get_lock_owner(pelem->lock, pelem->flags); + if (!task) + goto skip_owner; + + owner_pid =3D BPF_CORE_READ(task, pid); + + buf =3D bpf_map_lookup_elem(&stack_buf, &i); + if (!buf) + goto skip_owner; + for (i =3D 0; i < max_stack; i++) + buf[i] =3D 0x0; + + if (!bpf_task_from_pid) + goto skip_owner; + + task =3D bpf_task_from_pid(owner_pid); + if (!task) + goto skip_owner; + + bpf_get_task_stack(task, buf, max_stack * sizeof(unsigned long), 0); + bpf_task_release(task); + + otdata =3D bpf_map_lookup_elem(&owner_data, &pelem->lock); + id =3D get_owner_stack_id(buf); + + /* + * Contention just happens, or corner case `lock` is owned by process not + * `owner_pid`. For the corner case we treat it as unexpected internal e= rror and + * just ignore the precvious tracing record. + */ + if (!otdata || otdata->pid !=3D owner_pid) { + struct owner_tracing_data first =3D { + .pid =3D owner_pid, + .timestamp =3D pelem->timestamp, + .count =3D 1, + .stack_id =3D id, + }; + bpf_map_update_elem(&owner_data, &pelem->lock, &first, BPF_ANY); + } + /* Contention is ongoing and new waiter joins */ + else { + __sync_fetch_and_add(&otdata->count, 1); + + /* + * The owner is the same, but stacktrace might be changed. In this case= we + * store/update `owner_stat` based on current owner stack id. + */ + if (id !=3D otdata->stack_id) { + update_owner_stat(id, pelem->timestamp - otdata->timestamp, + pelem->flags); + + otdata->timestamp =3D pelem->timestamp; + otdata->stack_id =3D id; + } + } +skip_owner: pelem->stack_id =3D bpf_get_stackid(ctx, &stacks, BPF_F_FAST_STACK_CMP | stack_skip); if (pelem->stack_id < 0) @@ -473,6 +597,7 @@ int contention_end(u64 *ctx) struct tstamp_data *pelem; struct contention_key key =3D {}; struct contention_data *data; + __u64 timestamp; __u64 duration; bool need_delete =3D false; =20 @@ -500,12 +625,88 @@ int contention_end(u64 *ctx) need_delete =3D true; } =20 - duration =3D bpf_ktime_get_ns() - pelem->timestamp; + timestamp =3D bpf_ktime_get_ns(); + duration =3D timestamp - pelem->timestamp; if ((__s64)duration < 0) { __sync_fetch_and_add(&time_fail, 1); goto out; } =20 + if (needs_callstack && lock_owner) { + struct owner_tracing_data *otdata =3D bpf_map_lookup_elem(&owner_data, &= pelem->lock); + + if (!otdata) + goto skip_owner; + + /* Update `owner_stat` */ + update_owner_stat(otdata->stack_id, timestamp - otdata->timestamp, pelem= ->flags); + + /* No contention is occurring, delete `lock` entry in `owner_data` */ + if (otdata->count <=3D 1) + bpf_map_delete_elem(&owner_data, &pelem->lock); + /* + * Contention is still ongoing, with a new owner (current task). `owner_= data` + * should be updated accordingly. + */ + else { + u32 i =3D 0; + s32 ret =3D (s32)ctx[1]; + u64 *buf; + + otdata->timestamp =3D timestamp; + __sync_fetch_and_add(&otdata->count, -1); + + buf =3D bpf_map_lookup_elem(&stack_buf, &i); + if (!buf) + goto skip_owner; + for (i =3D 0; i < (u32)max_stack; i++) + buf[i] =3D 0x0; + + /* + * `ret` has the return code of the lock function. + * If `ret` is negative, the current task terminates lock waiting witho= ut + * acquiring it. Owner is not changed, but we still need to update the = owner + * stack. + */ + if (ret < 0) { + s32 id =3D 0; + struct task_struct *task; + + if (!bpf_task_from_pid) + goto skip_owner; + + task =3D bpf_task_from_pid(otdata->pid); + if (!task) + goto skip_owner; + + bpf_get_task_stack(task, buf, + max_stack * sizeof(unsigned long), 0); + bpf_task_release(task); + + id =3D get_owner_stack_id(buf); + + /* + * If owner stack is changed, update owner stack id for this lock. + */ + if (id !=3D otdata->stack_id) + otdata->stack_id =3D id; + } + /* + * Otherwise, update tracing data with the current task, which is the n= ew + * owner. + */ + else { + otdata->pid =3D pid; + /* + * We don't want to retrieve callstack here, since it is where the + * current task acquires the lock and provides no additional + * information. We simply assign -1 to invalidate it. + */ + otdata->stack_id =3D -1; + } + } + } +skip_owner: switch (aggr_mode) { case LOCK_AGGR_CALLER: key.stack_id =3D pelem->stack_id; @@ -589,14 +790,7 @@ int contention_end(u64 *ctx) } =20 found: - __sync_fetch_and_add(&data->total_time, duration); - __sync_fetch_and_add(&data->count, 1); - - /* FIXME: need atomic operations */ - if (data->max_time < duration) - data->max_time =3D duration; - if (data->min_time > duration) - data->min_time =3D duration; + update_contention_data(data, duration, 1); =20 out: pelem->lock =3D 0; --=20 2.48.1.658.g4767266eb4-goog From nobody Thu Dec 18 08:35:44 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8236266184 for ; Mon, 24 Feb 2025 18:51:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740423069; cv=none; b=lDuhVjw5x9jE8fO/PNBf3jMftH9IJvZT11noAWDglCikv9+WiZ9XHQyBOw9PYAYoQLkNRsiU0esmZvHdo4PiUI1ojpSWdxwhOsYS8qRFyJwMzyISdaDBy5+IERnqLgsb8Wbr1CYEXYgiK9xyX0B9fVqWjLRNSQL4Tm7YKbULquo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740423069; c=relaxed/simple; bh=SLA+bDmdTB6ULu5zbTamkrhePwVhtRIJqJ5avVgZmLw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=enN/e2l1ov1I6axXryrR1WWXBQO645ls2bP/+TY2IbokKLNsSRjb8d4hCCczhO6kDQOxOoBUV86jB6CNiFiqBuiJib5gN/ORGR+Ybp0ZQYje0SXX87oSuwmy0v2IVVEFtcZb9iWXFEplA62DGw28BQMM5f2gptQgNh5JoeWmh4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WvcMtr3B; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WvcMtr3B" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fc404aaed5so15728782a91.3 for ; Mon, 24 Feb 2025 10:51:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740423067; x=1741027867; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6PFYTBhXyGigxmAagzwCn1stPWOoCBAs7wU0xTcNHU0=; b=WvcMtr3BKhpwlWndhLCmA9p/SXdw8LBk8rv920dZiUXptu+fxUPfgkuVEqMGsFtn62 lB3nvxKNrEbYYvP3yDNi4+AxkRi9hdz5FEeshlw5j1/ZPsYvgA9wyYxY/VHximaGl4jY 2NNFvg8CEeolnF8BKsH+czxzPtjg8Cke7hLSnWrqXL0kaEMi0a1Grs/GVmpZGo89jaEl 6MEKkDdQ7p8inbdzGU0SQkfKeoBCzvO1NSPPEauwMOJLQDly0jks3e7vQp0ua9vvLkxl IKYaer807SgneOy413O1zo06G954bbUvsl3dZmUwj2MNTxql5F1axXwN8oQvfvCKYxrw HO7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740423067; x=1741027867; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6PFYTBhXyGigxmAagzwCn1stPWOoCBAs7wU0xTcNHU0=; b=IDq+gGaCjMcPtlI/YrVPr879j0w7kP6Gc56JIJuoMPP3csIznu3gtNeq0boooxQu/F f8NnxXxfb5MTaNsIBeo/EV0vNvt04EUUmqtXE48aQVbFOsmO8dQT8CgAdOEJFR9m/nWc 4QGlhJH2Fapp+gikgSRhrkYGfRRC49xKl7pKLxAPsR2nB4Sj6TSmqoVSxD+3ahMG07hZ xxyVwMzhlZZn0gAuywStGSLIfbe4lHuIrwcaumy4t3UaFvt+l1JhxmyhJDZXH3xX1ERZ Jw6hf1ggAVrVVvbYX6SYrzPCA1/GAHM9FBkJ5JDLmo6r4SWlDZHUjFiY7MVu4PacVVs0 VF+Q== X-Gm-Message-State: AOJu0Yy2J69VBKPJOsn9tLKdl5LKQN/OlJ9XY/WHHrHXLrObEjqygaJN /7J9Xjm29plXSKJqBGRwnmoIhWPiUvVtrzzCLlXLPlgyBAbnTEs+8GIcBG0I0feWdInwYaArZjI CUVFPZRg1A3roWpVYNobq3BdjOzcjox8gMT7+hXvAaqsGSGIyz4RcUH7dM1sfyPR9KBCzWqsCV7 qrVDDVSjXxHf4swy+Kl0ai5BXaA2vVIfGBRmzN10aY X-Google-Smtp-Source: AGHT+IHBJ0S/vfoZwcwLL32RzZSgNFIQA2ntgsWLkZmiVrk9sGPlhL4/XXbPVBWMgrB6QkbBP2cKfAL4f2M= X-Received: from pjbph4.prod.google.com ([2002:a17:90b:3bc4:b0:2fc:3022:36b4]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:da87:b0:2fa:1e3e:9be0 with SMTP id 98e67ed59e1d1-2fce78c9247mr22071548a91.19.1740423067265; Mon, 24 Feb 2025 10:51:07 -0800 (PST) Date: Mon, 24 Feb 2025 10:42:50 -0800 In-Reply-To: <20250224184742.4144931-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250224184742.4144931-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250224184742.4144931-4-ctshao@google.com> Subject: [PATCH v7 3/4] perf lock: Make rb_tree helper functions generic From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, nick.forrington@arm.com, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The rb_tree helper functions can be reused for parsing `owner_lock_stat` into rb tree for sorting. Signed-off-by: Chun-Tse Shao --- tools/perf/builtin-lock.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 5d405cd8e696..9bebc186286f 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -418,16 +418,13 @@ static void combine_lock_stats(struct lock_stat *st) rb_insert_color(&st->rb, &sorted); } =20 -static void insert_to_result(struct lock_stat *st, - int (*bigger)(struct lock_stat *, struct lock_stat *)) +static void insert_to(struct rb_root *rr, struct lock_stat *st, + int (*bigger)(struct lock_stat *, struct lock_stat *)) { - struct rb_node **rb =3D &result.rb_node; + struct rb_node **rb =3D &rr->rb_node; struct rb_node *parent =3D NULL; struct lock_stat *p; =20 - if (combine_locks && st->combined) - return; - while (*rb) { p =3D container_of(*rb, struct lock_stat, rb); parent =3D *rb; @@ -439,13 +436,21 @@ static void insert_to_result(struct lock_stat *st, } =20 rb_link_node(&st->rb, parent, rb); - rb_insert_color(&st->rb, &result); + rb_insert_color(&st->rb, rr); } =20 -/* returns left most element of result, and erase it */ -static struct lock_stat *pop_from_result(void) +static inline void insert_to_result(struct lock_stat *st, + int (*bigger)(struct lock_stat *, + struct lock_stat *)) +{ + if (combine_locks && st->combined) + return; + insert_to(&result, st, bigger); +} + +static inline struct lock_stat *pop_from(struct rb_root *rr) { - struct rb_node *node =3D result.rb_node; + struct rb_node *node =3D rr->rb_node; =20 if (!node) return NULL; @@ -453,8 +458,15 @@ static struct lock_stat *pop_from_result(void) while (node->rb_left) node =3D node->rb_left; =20 - rb_erase(node, &result); + rb_erase(node, rr); return container_of(node, struct lock_stat, rb); + +} + +/* returns left most element of result, and erase it */ +static struct lock_stat *pop_from_result(void) +{ + return pop_from(&result); } =20 struct trace_lock_handler { --=20 2.48.1.658.g4767266eb4-goog From nobody Thu Dec 18 08:35:44 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E2CF26659D for ; Mon, 24 Feb 2025 18:51:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740423110; cv=none; b=sVWeUCZrV6s98ZKrywHZVV57SiOUO0NwwpvSg3r7cPKwLdCl4E2lH34yNQV043E19YFB7ZZ2+Xl0DeaLVaEyWxgylrbr1aJuFmu45n8SlcmgvNpXyG5H8wxRuZS+oEIqesKjAhrGUVrCqJcllyMNdrtEKBrpoccnjGff8ATXoWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740423110; c=relaxed/simple; bh=TEMGIR10WYHrpkHHdWlP0Obhht+RBjq4ihNlCJ0KvdE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i6t1PRdkB7lpMEvcaVlJguhfaoOEWjOPFlwOXAL2DWLUAIQ3y8dhS6oli2gMWglambm2AGraUoEW5CX1xJNrCXogDo5wTqYMtB+uNV0OKF1+KCvRtQ+9r37smaNhVct5NiYmvccCgTNY0Iry8T+q83lXmg20D1DuxPfV/+h+Mj0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QqF81R99; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QqF81R99" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-22119b07d52so60871275ad.1 for ; Mon, 24 Feb 2025 10:51:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740423108; x=1741027908; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qPrj4Gf1FvfcCgUu+hXAsKriyUrmM5yjH6bbIIdueC8=; b=QqF81R99yb5GYdf+dfzGgXhGd/87ifx22IqTPxoLvCmpKDz+JHfDqoW+a3kEhoKZYL 9/bhrbT2gVvNoL4+Y1LsIEDjbI7ZWhrMFSYpw+s4sRapri1L4Hb2CScYSzITe6GmiJB3 tTHlqgN/92hNXDo66AdRr3qymAjPDWzYDFSqAxLrmALuwD18yZ5P0xCVHen1SPn4imhJ kZpBUWWWWvDsrMA05xqM2o0+3WoT3zJT3Z6MDbjKfb94GJkjVLzK9+VOmKAc/UY3vezc PhB3tmbd8zdc06q8HPticyFYupLb58kMu3wsxAT1CKrp2DOLfeNQfyMZGBnIjlHvnxq/ dYQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740423108; x=1741027908; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qPrj4Gf1FvfcCgUu+hXAsKriyUrmM5yjH6bbIIdueC8=; b=CNqCwy4dIBs/P4N6QhOGcghrLWGJYva7qbpCWF829jlDu3Fgf4IupShB4WBAi1WVgV +C63x3KUPx7PaY2Rxw1JU8SgSXA9dJLc0kSWgmMlUkzY9tD1CGgkEPsluxiI0Ln4Y8l8 RhcZNfQ5wwQWYhKWLK8WosubSl0s9yueT6n84HKNFmRjiuExy3O7SaOpJc/wi/GLh8uK UP2FnO7aHttCYwBAGpstdUkMoJZem7dcmVolXM8JlHEpabqgqb1HcEoKzHPjLIrc4PkX l2JRjKYC9ka/9lk6qwM4U9Y3QB6Es8HVGBl8l8ZqDvZe5aQ2G5Vf7zwH2ThBNX0DzIMg c9cQ== X-Gm-Message-State: AOJu0YxCYuEXKUndO/DDzQ8s5YrHizZcgY1zDVpJ2L1cM0CwMAptFd7u RJDaJQrlonpCDa4KIT6L6c/uNuudCoqU2y0DUW2ZN70fj2ZoKINDzMMPAq2SmcM7+dnllLQouHS fZhOW9Z0Wsb5IdP9vj0zBULf2gjTLEgMQWMz3AXP6ERPWEArujsajLXKTW+i7JfKNHt5jeFwe7d KpBBwcpCLJdbAQY/UzcrX135OvsWQnLpKcBxGBB995 X-Google-Smtp-Source: AGHT+IEWIfVKVjzx5FeuvwVoXI6nrOx+HwUn9LBeZdP29jlOE6Qy5XtsULV48UA+xF/qJf1msm5mIFwAcYc= X-Received: from pfgu32.prod.google.com ([2002:a05:6a00:9a0:b0:730:50c0:136d]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a8f:b0:730:794e:7aca with SMTP id d2e1a72fcca58-7347918d073mr467287b3a.16.1740423108186; Mon, 24 Feb 2025 10:51:48 -0800 (PST) Date: Mon, 24 Feb 2025 10:42:51 -0800 In-Reply-To: <20250224184742.4144931-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250224184742.4144931-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250224184742.4144931-5-ctshao@google.com> Subject: [PATCH v7 4/4] perf lock: Report owner stack in usermode From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, nick.forrington@arm.com, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch parses `owner_lock_stat` into a RB tree, enabling ordered reporting of owner lock statistics with stack traces. It also updates the documentation for the `-o` option in contention mode, decouples `-o` from `-t`, and issues a warning to inform users about the new behavior of `-ov`. Example output: $ sudo ~/linux/tools/perf/perf lock con -abvo -Y mutex-spin -E3 perf benc= h sched pipe ... contended total wait max wait avg wait type caller 171 1.55 ms 20.26 us 9.06 us mutex pipe_rea= d+0x57 0xffffffffac6318e7 pipe_read+0x57 0xffffffffac623862 vfs_read+0x332 0xffffffffac62434b ksys_read+0xbb 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwfram= e+0x76 36 193.71 us 15.27 us 5.38 us mutex pipe_wri= te+0x50 0xffffffffac631ee0 pipe_write+0x50 0xffffffffac6241db vfs_write+0x3bb 0xffffffffac6244ab ksys_write+0xbb 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwfram= e+0x76 4 51.22 us 16.47 us 12.80 us mutex do_epoll= _wait+0x24d 0xffffffffac691f0d do_epoll_wait+0x24d 0xffffffffac69249b do_epoll_pwait.part.0+0xb 0xffffffffac693ba5 __x64_sys_epoll_pwait+0x95 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwfram= e+0x76 =3D=3D=3D owner stack trace =3D=3D=3D 3 31.24 us 15.27 us 10.41 us mutex pipe_rea= d+0x348 0xffffffffac631bd8 pipe_read+0x348 0xffffffffac623862 vfs_read+0x332 0xffffffffac62434b ksys_read+0xbb 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwfram= e+0x76 ... Signed-off-by: Chun-Tse Shao --- tools/perf/Documentation/perf-lock.txt | 5 ++- tools/perf/builtin-lock.c | 22 +++++++++- tools/perf/util/bpf_lock_contention.c | 57 ++++++++++++++++++++++++++ tools/perf/util/lock-contention.h | 7 ++++ 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/tools/perf/Documentation/perf-lock.txt b/tools/perf/Documentat= ion/perf-lock.txt index d3793054f7d3..859dc11a7372 100644 --- a/tools/perf/Documentation/perf-lock.txt +++ b/tools/perf/Documentation/perf-lock.txt @@ -179,8 +179,9 @@ CONTENTION OPTIONS =20 -o:: --lock-owner:: - Show lock contention stat by owners. Implies --threads and - requires --use-bpf. + Show lock contention stat by owners. This option can be combined with -t, + which shows owner's per thread lock stats, or -v, which shows owner's + stacktrace. Requires --use-bpf. =20 -Y:: --type-filter=3D:: diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 9bebc186286f..05e7bc30488a 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -1817,6 +1817,22 @@ static void print_contention_result(struct lock_cont= ention *con) break; } =20 + if (con->owner && con->save_callstack && verbose > 0) { + struct rb_root root =3D RB_ROOT; + + if (symbol_conf.field_sep) + fprintf(lock_output, "# owner stack trace:\n"); + else + fprintf(lock_output, "\n=3D=3D=3D owner stack trace =3D=3D=3D\n\n"); + while ((st =3D pop_owner_stack_trace(con))) + insert_to(&root, st, compare); + + while ((st =3D pop_from(&root))) { + print_lock_stat(con, st); + free(st); + } + } + if (print_nr_entries) { /* update the total/bad stats */ while ((st =3D pop_from_result())) { @@ -1962,8 +1978,10 @@ static int check_lock_contention_options(const struc= t option *options, } } =20 - if (show_lock_owner) - show_thread_stats =3D true; + if (show_lock_owner && !show_thread_stats) { + pr_warning("Now -o try to show owner's callstack instead of pid and comm= .\n"); + pr_warning("Please use -t option too to keep the old behavior.\n"); + } =20 return 0; } diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index 76542b86e83f..16f4deba69ec 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -549,6 +549,63 @@ static const char *lock_contention_get_name(struct loc= k_contention *con, return name_buf; } =20 +struct lock_stat *pop_owner_stack_trace(struct lock_contention *con) +{ + int stacks_fd, stat_fd; + u64 *stack_trace =3D NULL; + s32 stack_id; + struct contention_key ckey =3D {}; + struct contention_data cdata =3D {}; + size_t stack_size =3D con->max_stack * sizeof(*stack_trace); + struct lock_stat *st =3D NULL; + + stacks_fd =3D bpf_map__fd(skel->maps.owner_stacks); + stat_fd =3D bpf_map__fd(skel->maps.owner_stat); + if (!stacks_fd || !stat_fd) + goto out_err; + + stack_trace =3D zalloc(stack_size); + if (stack_trace =3D=3D NULL) + goto out_err; + + if (bpf_map_get_next_key(stacks_fd, NULL, stack_trace)) + goto out_err; + + bpf_map_lookup_elem(stacks_fd, stack_trace, &stack_id); + ckey.stack_id =3D stack_id; + bpf_map_lookup_elem(stat_fd, &ckey, &cdata); + + st =3D zalloc(sizeof(struct lock_stat)); + if (!st) + goto out_err; + + st->name =3D strdup(stack_trace[0] ? lock_contention_get_name(con, NULL, = stack_trace, 0) : + "unknown"); + if (!st->name) + goto out_err; + + st->flags =3D cdata.flags; + st->nr_contended =3D cdata.count; + st->wait_time_total =3D cdata.total_time; + st->wait_time_max =3D cdata.max_time; + st->wait_time_min =3D cdata.min_time; + st->callstack =3D stack_trace; + + if (cdata.count) + st->avg_wait_time =3D cdata.total_time / cdata.count; + + bpf_map_delete_elem(stacks_fd, stack_trace); + bpf_map_delete_elem(stat_fd, &ckey); + + return st; + +out_err: + free(stack_trace); + free(st); + + return NULL; +} + int lock_contention_read(struct lock_contention *con) { int fd, stack, err =3D 0; diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-conte= ntion.h index a09f7fe877df..97fd33c57f17 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -168,6 +168,8 @@ int lock_contention_stop(void); int lock_contention_read(struct lock_contention *con); int lock_contention_finish(struct lock_contention *con); =20 +struct lock_stat *pop_owner_stack_trace(struct lock_contention *con); + #else /* !HAVE_BPF_SKEL */ =20 static inline int lock_contention_prepare(struct lock_contention *con __ma= ybe_unused) @@ -187,6 +189,11 @@ static inline int lock_contention_read(struct lock_con= tention *con __maybe_unuse return 0; } =20 +struct lock_stat *pop_owner_stack_trace(struct lock_contention *con) +{ + return NULL; +} + #endif /* HAVE_BPF_SKEL */ =20 #endif /* PERF_LOCK_CONTENTION_H */ --=20 2.48.1.658.g4767266eb4-goog