From: Chun-Tse Shao <ctshao@google.com>
To: linux-kernel@vger.kernel.org
Cc: Chun-Tse Shao <ctshao@google.com>,
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, linux-perf-users@vger.kernel.org,
bpf@vger.kernel.org
Subject: [PATCH v2 1/4] perf lock: Add bpf maps for owner stack tracing
Date: Sun, 12 Jan 2025 21:20:14 -0800 [thread overview]
Message-ID: <20250113052220.2105645-2-ctshao@google.com> (raw)
In-Reply-To: <20250113052220.2105645-1-ctshao@google.com>
Add few bpf maps in order to tracing owner stack.
Signed-off-by: Chun-Tse Shao <ctshao@google.com>
---
tools/perf/util/bpf_lock_contention.c | 17 ++++++--
.../perf/util/bpf_skel/lock_contention.bpf.c | 40 +++++++++++++++++--
tools/perf/util/bpf_skel/lock_data.h | 6 +++
3 files changed, 56 insertions(+), 7 deletions(-)
diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c
index 41a1ad087895..c9c58f243ceb 100644
--- a/tools/perf/util/bpf_lock_contention.c
+++ b/tools/perf/util/bpf_lock_contention.c
@@ -41,9 +41,20 @@ int lock_contention_prepare(struct lock_contention *con)
else
bpf_map__set_max_entries(skel->maps.task_data, 1);
- if (con->save_callstack)
- bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
- else
+ if (con->save_callstack) {
+ bpf_map__set_max_entries(skel->maps.stacks,
+ con->map_nr_entries);
+ if (con->owner) {
+ bpf_map__set_value_size(skel->maps.owner_stacks_entries,
+ con->max_stack * sizeof(u64));
+ bpf_map__set_value_size(
+ skel->maps.contention_owner_stacks,
+ con->max_stack * sizeof(u64));
+ bpf_map__set_key_size(skel->maps.owner_lock_stat,
+ con->max_stack * sizeof(u64));
+ skel->rodata->max_stack = con->max_stack;
+ }
+ } else
bpf_map__set_max_entries(skel->maps.stacks, 1);
if (target__has_cpu(target)) {
diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c
index 1069bda5d733..05da19fdab23 100644
--- a/tools/perf/util/bpf_skel/lock_contention.bpf.c
+++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c
@@ -19,13 +19,37 @@
#define LCB_F_PERCPU (1U << 4)
#define LCB_F_MUTEX (1U << 5)
-/* callstack storage */
+ /* tmp buffer for owner callstack */
struct {
- __uint(type, BPF_MAP_TYPE_STACK_TRACE);
+ __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u64));
+ __uint(max_entries, 1);
+} owner_stacks_entries 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(cotd));
__uint(max_entries, MAX_ENTRIES);
-} stacks SEC(".maps");
+} contention_owner_tracing SEC(".maps");
+
+/* a map for tracing lock address to owner stacktrace */
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(key_size, sizeof(__u64)); // lock address
+ __uint(value_size, sizeof(__u64)); // straktrace
+ __uint(max_entries, MAX_ENTRIES);
+} contention_owner_stacks SEC(".maps");
+
+/* owner callstack to contention data storage */
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(key_size, sizeof(__u64));
+ __uint(value_size, sizeof(struct contention_data));
+ __uint(max_entries, MAX_ENTRIES);
+} owner_lock_stat SEC(".maps");
/* maintain timestamp at the beginning of contention */
struct {
@@ -43,6 +67,14 @@ struct {
__uint(max_entries, 1);
} tstamp_cpu SEC(".maps");
+/* callstack storage */
+struct {
+ __uint(type, BPF_MAP_TYPE_STACK_TRACE);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u64));
+ __uint(max_entries, MAX_ENTRIES);
+} stacks SEC(".maps");
+
/* actual lock contention statistics */
struct {
__uint(type, BPF_MAP_TYPE_HASH);
@@ -126,6 +158,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;
/* determine the key of lock stat */
const volatile int aggr_mode;
@@ -436,7 +469,6 @@ int contention_end(u64 *ctx)
return 0;
need_delete = true;
}
-
duration = bpf_ktime_get_ns() - pelem->timestamp;
if ((__s64)duration < 0) {
__sync_fetch_and_add(&time_fail, 1);
diff --git a/tools/perf/util/bpf_skel/lock_data.h b/tools/perf/util/bpf_skel/lock_data.h
index de12892f992f..1ef0bca9860e 100644
--- a/tools/perf/util/bpf_skel/lock_data.h
+++ b/tools/perf/util/bpf_skel/lock_data.h
@@ -3,6 +3,12 @@
#ifndef UTIL_BPF_SKEL_LOCK_DATA_H
#define UTIL_BPF_SKEL_LOCK_DATA_H
+typedef struct contention_owner_tracing_data {
+ u32 pid; // Who has the lock.
+ u64 timestamp; // The time while the owner acquires lock and contention is going on.
+ u32 count; // How many waiters for this lock.
+} cotd;
+
struct tstamp_data {
u64 timestamp;
u64 lock;
--
2.47.1.688.g23fc6f90ad-goog
next prev parent reply other threads:[~2025-01-13 5:22 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-13 5:20 [PATCH v2 0/4] Tracing contention lock owner call stack Chun-Tse Shao
2025-01-13 5:20 ` Chun-Tse Shao [this message]
2025-01-14 3:05 ` [PATCH v2 1/4] perf lock: Add bpf maps for owner stack tracing Namhyung Kim
2025-01-21 22:02 ` Chun-Tse Shao
2025-01-24 0:34 ` Namhyung Kim
2025-01-13 5:20 ` [PATCH v2 2/4] perf lock: Retrieve owner callstack in bpf program Chun-Tse Shao
2025-01-14 3:35 ` Namhyung Kim
2025-01-21 22:35 ` Chun-Tse Shao
2025-01-24 0:39 ` Namhyung Kim
2025-01-13 5:20 ` [PATCH v2 3/4] perf lock: Make rb_tree helper functions generic Chun-Tse Shao
2025-01-13 5:20 ` [PATCH v2 4/4] perf lock: Report owner stack in usermode Chun-Tse Shao
2025-01-14 3:56 ` Namhyung Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250113052220.2105645-2-ctshao@google.com \
--to=ctshao@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bpf@vger.kernel.org \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).