From patchwork Wed Feb 12 22:24:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Tse Shao X-Patchwork-Id: 13972541 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 5986C202F67 for ; Wed, 12 Feb 2025 22:29:39 +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=1739399380; cv=none; b=d4Oi+KKUH7Kim73rIIF46No675l3SXZbuSsFpiHsmOnX537HVqQ/THXIOTxNfxN71S01qVRrj5YXuXW+WmmfyRELF33VG3S0Haxvs5LNe0pjqV+t9MH2bTbBijenWWyOi37+/cOgJ8enJRgJkDeKXCFPks9Ua/qxy9fNGot9XYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399380; c=relaxed/simple; bh=OWCpQfV15fjfs91X5TWsuZAUowIODtiQVMeIkdX4org=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sovv6ssLrkxXqtJmfj/uaylDptaOdkyEb0lYMUjis0XeZcOUkcg1vs6AZ1MK6g+31tAFjLD0Gk/mXUATJIEC4XBfync34aKt1xNo2zNre85t00ONSwKTx6VE+f+9TAXcx20f348Klsf43x90NVSS1hJulyrEz2iE2nn9OsbzHig= 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=IF0brA+Z; 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="IF0brA+Z" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-220c86b3ef3so13955435ad.1 for ; Wed, 12 Feb 2025 14:29:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739399378; x=1740004178; 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=3O9bMfrQLSuEn1cesD7grG9bYwMcsNdAGFS2Zv9IEeA=; b=IF0brA+Zd65etVDoaHi5ADqKjNjD8E31MTNFg1mVhO3CJeWL4u53PgPlSI4hQhz02E CgAeq5ZsJts3DjjorUdMYL/xn3CC45FV5qZGrEsWClZr8jChCFfwHw9ppGASstQxO5CG kzt2j8XG+DfmgzjbbHTBLR15of+EKtFbdKzxfaYBHNI0s2pkNGafS319VFaYoxSdk/3R QTGIgl+ytQPvOEHLnCbv4/4+fFiywqixua5Z1cOUnNG1RLtdr0lVGtwBH3eZkc8/fa2I zuget0tsLbX/6+c8mAJfW8/JdCiwlKMLyXQaE3EWkhZB0mSje+Hifku50B922LIsWtp+ cewQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739399378; x=1740004178; 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=3O9bMfrQLSuEn1cesD7grG9bYwMcsNdAGFS2Zv9IEeA=; b=BGtkIc1wzO9djOAZtvumYKBTB1uzGFvBJnU2Ep4Cies0ehfte3C3X0QdlQDRbS23tU jzAkJJPxN0rT+lWtwdc8b0SENIB0+S1H92XLAwC5aMibEtVi+gST5PTCgddXeGKz+BOH nkRzetT61/msvwqdvuRep+4M2ZaEh01KrOpUDiLjJhls0vFz65wNWm154ea8rRIuavfm wAp7uq9NuW9cm6BXVEJ5oblYlRMnFlGQgCOFwc/8bV6Z+wuNDn66AkCWeCGKwqt3QLpl 90X8+DXvYXu5vW9JkLprW41OZ9SGHa+D6qs/dIsLoWkr2K/b54XSd3Ws8FEPBDnQiuSH oslw== X-Forwarded-Encrypted: i=1; AJvYcCVLA7s7cZtzi1vJYG1qPEbYNqqKjmUwALi+JUQJg6IEN8VVdO4xm8MbNtxeCzP38V8y8eg=@vger.kernel.org X-Gm-Message-State: AOJu0Yzhiiud43SkcXmaGr6Eb2LcR5A9HhS31j7UTvvE1yKVbojuwjSs nKKwuaBePWuls3WulKItwcIx1P3lrRNM28xyQ2ssgwdQMGLqH08PzC/nWyVWvXwlnDaHrksU4hF t/Q== X-Google-Smtp-Source: AGHT+IGH91oQbQqQOltRzwNzcwjx/9c8q5oX8zM6hIaW2Mk5foDZ/RclC94i5ELe3GWDZVuHmetFFqhI8es= X-Received: from pgbcr7.prod.google.com ([2002:a05:6a02:4107:b0:ad5:55c3:4034]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:12cb:b0:1ee:64c0:aa85 with SMTP id adf61e73a8af0-1ee6c619d45mr958280637.6.1739399378557; Wed, 12 Feb 2025 14:29:38 -0800 (PST) Date: Wed, 12 Feb 2025 14:24:52 -0800 In-Reply-To: <20250212222859.2086080-1-ctshao@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250212222859.2086080-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212222859.2086080-2-ctshao@google.com> Subject: [PATCH v5 1/5] 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, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org 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_lock_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 *con) else bpf_map__set_max_entries(skel->maps.task_data, 1); - 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(u64)); + 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 = con->max_stack; + } + } else { bpf_map__set_max_entries(skel->maps.stacks, 1); + } if (target__has_cpu(target)) { skel->rodata->has_cpu = 1; diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/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"); +/* 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; /* 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_skel/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 +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 is going on. + s32 stack_id; // Identifier for `owner_stat`, which stores as value in `owner_stacks` +}; + struct tstamp_data { u64 timestamp; u64 lock; From patchwork Wed Feb 12 22:24:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Tse Shao X-Patchwork-Id: 13972542 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 E00DC2036F4 for ; Wed, 12 Feb 2025 22:30:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399415; cv=none; b=APn6lvgDZaQdVAqvBd9CiYi7t78iNoWg2RC1yWpxfi1FT2kvDx1t1Bv0ytP6ar6WrJ1xYCEduzvvhUSI2DzTpTCei0RXpIYCU69Vh171+ObackJUTvgT16hiH8RO+ADykL2gLOQYuHiN/90z+f7tes9bVYgNL85T1Wic5QHbFbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399415; c=relaxed/simple; bh=VMSWxcNmO8no8BHLvLxOhA30OkINBXw7JvnooWa9Ev0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=heil3n0aVLsN2S+2iDoA4idJCJ04H1o7Yz80INv03XgOCuJ5FgccR9blfYHdSDEAUGnVAD4ISvxSBAOZCLej6Swekg5RuxW26kuLEuBbUHk6TN7myQXszbNrRXbNuY7OpBGKYx04MGAkQMVuL/eh7nVDJObmX8zX35vkqzlYf5o= 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=ZwVcVAkh; arc=none smtp.client-ip=209.85.216.73 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="ZwVcVAkh" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa44233a04so535617a91.1 for ; Wed, 12 Feb 2025 14:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739399413; x=1740004213; 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=XSbEcYPaOo1Mf2YKv2LhLfOPLMl0BBUQrr31+aE8g7M=; b=ZwVcVAkhqLlD4gZvCjKDKd20hHDT6BBOMO+LEVO6RqsGH7EZIdVYWg4C4I/YADE9Ff /XEekpHIjf4KOM8EOvh+0SgB1IPZdGyWVNBf1wiAWBmG/l1QQnrW4R5v1SSGsCzU/u4F 43z9FdkKk3sLaHdo+brKT0ths0y9xoYbc3rf8U7+oOIQ5vml77HQ518SYNWnm4wD9x4/ Zy2/QC/WHL3WazjC/DaCUtClMTTO5xVYq810wOQKfkqosPITdsPj+fSiBTZu+X6rUYkK VyneeZyOTsfAP+I/skZLnJLMPwwBRqBA7OuMKe4ETh8lHWTPKdOQpjBZnSL73v5GRV6X h9/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739399413; x=1740004213; 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=XSbEcYPaOo1Mf2YKv2LhLfOPLMl0BBUQrr31+aE8g7M=; b=oZZtwkesSMrrm4YEy/zDXy139XejlcLPcY3g/i1HfirF5lHTdmL5l+Q7mVRSdBRtn0 SWhZRkk3G+/srIblHv4nJt66pEm7j2K+IzXHz4CPps+nWEFxfQlhjL8m4ouPCGr/+naK I+vnFNYZ5W3uuAvcwQAfe05abgRmiLH4vQkez4HsRgKgOrMnE6bOBLOd9SaYq17GN2mo 4FdZYaL+jvCUgVLNQ0y5QajEOcN/pT9WE7QjWBwwaui1QVgKJq1b8CW45r7YR1DP8YlM QduuDP61mskP/xYC61VzJ3hcEet0KN9EskFm0PgucazEe+xIYT4dnU4OINrF/BXrfEpn SF5A== X-Forwarded-Encrypted: i=1; AJvYcCV+e1MxEduMDpT9n0Ht+Ma/PTfVp/97thCX+xlY3EGZ5cSPkp+U3RGY4i5+BwlaK24tAE0=@vger.kernel.org X-Gm-Message-State: AOJu0YxLwmK2srXMYockuRmbu2zEKOSSsOrcY+w7CL9Gw0VViNhjDi5p a7GE6VKsStU05hu9uwYXCV/qQGoMaVJCyLq2sMYlVSm9C/34ZglcJZVW78mvbfjbnyb7jg6pPx+ CFQ== X-Google-Smtp-Source: AGHT+IGFKaRYCnm24tciFSPM+jbVn6VUGWLylTtEWjxKMVSlIzJwJ3A/SpYnvLm+ugPwNJmzcDUVpDjNFW4= X-Received: from pjur15.prod.google.com ([2002:a17:90a:d40f:b0:2f6:e47c:1747]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:ec84:b0:2f4:f7f8:fc8b with SMTP id 98e67ed59e1d1-2fc0f0b05a8mr1269502a91.27.1739399413221; Wed, 12 Feb 2025 14:30:13 -0800 (PST) Date: Wed, 12 Feb 2025 14:24:53 -0800 In-Reply-To: <20250212222859.2086080-1-ctshao@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250212222859.2086080-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212222859.2086080-3-ctshao@google.com> Subject: [PATCH v5 2/5] 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, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org 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 | 218 +++++++++++++++++- 1 file changed, 209 insertions(+), 9 deletions(-) -- 2.48.1.502.g6dc24dfdaf-goog diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index 23fe9cc980ae..e8b113d5802a 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; +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(__u32 flags) return pelem; } +static inline s32 get_owner_stack_id(u64 *stacktrace) +{ + s32 *id, new_id; + static s64 id_gen = 1; + + id = bpf_map_lookup_elem(&owner_stacks, stacktrace); + if (id) + return *id; + + new_id = (s32)__sync_fetch_and_add(&id_gen, 1); + + bpf_map_update_elem(&owner_stacks, stacktrace, &new_id, BPF_NOEXIST); + + id = bpf_map_lookup_elem(&owner_stacks, stacktrace); + if (id) + return *id; + + return -1; +} + +static inline void update_contention_data(struct contention_data *data, u64 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 = duration; + if (data->min_time > duration) + data->min_time = duration; +} + +static inline void update_owner_stat(u32 id, u64 duration, u32 flags) +{ + struct contention_key key = { + .stack_id = id, + .pid = 0, + .lock_addr_or_cgroup = 0, + }; + struct contention_data *data = bpf_map_lookup_elem(&owner_stat, &key); + + if (!data) { + struct contention_data first = { + .total_time = duration, + .max_time = duration, + .min_time = duration, + .count = 1, + .flags = 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 = (__u32)ctx[1]; if (needs_callstack) { + u32 i = 0; + u32 id = 0; + int owner_pid; + u64 *buf; + struct task_struct *task; + struct owner_tracing_data *otdata; + + if (!lock_owner) + goto skip_owner; + + task = get_lock_owner(pelem->lock, pelem->flags); + if (!task) + goto skip_owner; + + owner_pid = BPF_CORE_READ(task, pid); + + buf = bpf_map_lookup_elem(&stack_buf, &i); + if (!buf) + goto skip_owner; + for (i = 0; i < max_stack; i++) + buf[i] = 0x0; + + if (!bpf_task_from_pid) + goto skip_owner; + + task = 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 = bpf_map_lookup_elem(&owner_data, &pelem->lock); + id = 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 error and + * just ignore the precvious tracing record. + */ + if (!otdata || otdata->pid != owner_pid) { + struct owner_tracing_data first = { + .pid = owner_pid, + .timestamp = pelem->timestamp, + .count = 1, + .stack_id = 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 != otdata->stack_id) { + update_owner_stat(id, pelem->timestamp - otdata->timestamp, + pelem->flags); + + otdata->timestamp = pelem->timestamp; + otdata->stack_id = id; + } + } +skip_owner: pelem->stack_id = 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 = {}; struct contention_data *data; + __u64 timestamp; __u64 duration; bool need_delete = false; @@ -500,12 +625,94 @@ int contention_end(u64 *ctx) need_delete = true; } - duration = bpf_ktime_get_ns() - pelem->timestamp; + timestamp = bpf_ktime_get_ns(); + duration = timestamp - pelem->timestamp; if ((__s64)duration < 0) { __sync_fetch_and_add(&time_fail, 1); goto out; } + if (needs_callstack && lock_owner) { + struct owner_tracing_data *otdata = 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 <= 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 = 0; + s32 ret = (s32)ctx[1]; + u64 *buf; + + __sync_fetch_and_add(&otdata->count, -1); + + buf = bpf_map_lookup_elem(&stack_buf, &i); + if (!buf) + goto skip_owner; + for (i = 0; i < (u32)max_stack; i++) + buf[i] = 0x0; + + /* + * `ret` has the return code of the lock function. + * If `ret` is negative, the current task terminates lock waiting without + * acquiring it. Owner is not changed, but we still need to update the owner + * stack. + */ + if (ret < 0) { + s32 id = 0; + struct task_struct *task; + + if (!bpf_task_from_pid) + goto skip_owner; + + task = 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 = get_owner_stack_id(buf); + + /* + * If owner stack is changed, update `owner_data` and `owner_stat` + * accordingly. + */ + if (id != otdata->stack_id) { + update_owner_stat(id, pelem->timestamp - otdata->timestamp, + pelem->flags); + + otdata->timestamp = pelem->timestamp; + otdata->stack_id = id; + } + } + /* + * Otherwise, update tracing data with the current task, which is the new + * owner. + */ + else { + otdata->pid = pid; + otdata->timestamp = timestamp; + /* + * 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 = -1; + } + } + } +skip_owner: switch (aggr_mode) { case LOCK_AGGR_CALLER: key.stack_id = pelem->stack_id; @@ -589,14 +796,7 @@ int contention_end(u64 *ctx) } 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 = duration; - if (data->min_time > duration) - data->min_time = duration; + update_contention_data(data, duration, 1); out: pelem->lock = 0; From patchwork Wed Feb 12 22:24:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Tse Shao X-Patchwork-Id: 13972544 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 7EA43204086 for ; Wed, 12 Feb 2025 22:30:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399450; cv=none; b=a6oTAClPTni31ABwNqPlWzYOFGF9YeHv8JkJo5nvk5AVZyuF+T49s2gBiEdrD3DlwipQb3RtkM1VkkUJio4UWy6p0qjKfPF29KcxuKnIVeXT6uYrlxdvOa4UFFmds8UBsGV6w1CLud5mJPIO4K4YSwBHP2cimS3J4/PE0O7uNMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399450; c=relaxed/simple; bh=4JuFZc5w2LCt7uJHwRk3CddKeQ1pM4baFKxQ+C/RUgI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XT9XgrxnpHFqIWpl9V7gDGHeZGqQRh89PQHQ0ol58DpcSR1yUVZVCpT9s7l0t5saKqIZGeXFqiXgmeND9iPQxskPh5EdMDsqmAWNGwNKZ4hFVd+pIraRrYdIGMoJgjKLG3W83A7mVSTzvfPqcVcJ5/rGjW3PMDG0EA2pxd4/Oq0= 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=tX065EKa; arc=none smtp.client-ip=209.85.214.201 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="tX065EKa" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-220d1c24b25so4331025ad.0 for ; Wed, 12 Feb 2025 14:30:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739399448; x=1740004248; 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=Xp6JXzWQDgJraueffuAM7pmy5niWB0JPsWhbLqxLJuY=; b=tX065EKaTv2mhh/296KU6+We68bngvpU4VoQcvg4TJpK+hcGk11cPQgmJYQyZXLEIB QP1l5MM75EiEaaGjOEHkdBKNu5ME92ZaHMR27CM2Us0bQGmTaRxpV8N/fsRzrKOYLHkj ww3e1f9ckxTHWtOgVJXn8bYz6qBuBDSGifRrR4otJ2rUR7zvsWzQn4bKTU8sKAq0bVGd 1suxjupeIMoibl2EWX2BfFYDQdFlMAVoGdWbtzJI1rMUIgrv92xny91j7OzNyDhFeQLk MyVyvhroQRus3NlQBnuCVT2o2xEALOXWDrkrDIUI6Lxgi0IH8e8VYyTlMsT0j7HSbgkI DwJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739399448; x=1740004248; 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=Xp6JXzWQDgJraueffuAM7pmy5niWB0JPsWhbLqxLJuY=; b=fGddM1J5nc1v7kT/ctNa+JDphQSfYqpihiC7Wjhx2rAoBo/uUjJC5+5Foi3sFxwxsN 9AzJTgV/mOZT8osprdD23Rpfr965qj848FZZUu0xL58dltqTlGegBSC0YauAA2XaKqP3 0fbiBMfsYPYmebFEUixUZhS0wA7LV+iqk+M8sOPiq08v/5MEgcdHWg6Xc2SOtp4FqLvn 9Hq3G/U8PMsSAhIyw/E+rLMwhyXJujRnLt3tvBWkI4HjIK7Ikz7doBwIBFaiQE9cZygI 7bsehIEn9f3MtFLaIo74B8Ouom+U5vIalkCnYLC4hoEi6qroVLw37+LS7Bsjdby2wGHk 9TfA== X-Forwarded-Encrypted: i=1; AJvYcCX/fb9t2IQyyFlUt6afSjuhaiAK0iR0zLDclo0rI3jMXcwnyYjt9w6K/qd6nvXae6qnbdY=@vger.kernel.org X-Gm-Message-State: AOJu0YzB4GpVkCtX4MgahazRmgdklpMzs+oNsCf7u4MkC3zw2/DtNu9c +h8FOhIOsP4nvzkwARVpQFLEzDRpubg598qJHu+I8SNmgR1VNPatYkEflGPXJE5eaQoed3fHbn+ LNA== X-Google-Smtp-Source: AGHT+IEBBiJFCF3wEaVNJRVbpU0yixawbj3CSdHQuUgkpm5T1RSKHo6nSIBj+hC+a0gSgUgMG6dxyJIN0Pc= X-Received: from pjyp8.prod.google.com ([2002:a17:90a:e708:b0:2f8:4024:b59a]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d549:b0:216:6901:d588 with SMTP id d9443c01a7336-220d1ed1f92mr17422035ad.15.1739399447824; Wed, 12 Feb 2025 14:30:47 -0800 (PST) Date: Wed, 12 Feb 2025 14:24:54 -0800 In-Reply-To: <20250212222859.2086080-1-ctshao@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250212222859.2086080-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212222859.2086080-4-ctshao@google.com> Subject: [PATCH v5 3/5] 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, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org 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); } -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 = &result.rb_node; + struct rb_node **rb = &rr->rb_node; struct rb_node *parent = NULL; struct lock_stat *p; - if (combine_locks && st->combined) - return; - while (*rb) { p = container_of(*rb, struct lock_stat, rb); parent = *rb; @@ -439,13 +436,21 @@ static void insert_to_result(struct lock_stat *st, } rb_link_node(&st->rb, parent, rb); - rb_insert_color(&st->rb, &result); + rb_insert_color(&st->rb, rr); } -/* 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 = result.rb_node; + struct rb_node *node = rr->rb_node; if (!node) return NULL; @@ -453,8 +458,15 @@ static struct lock_stat *pop_from_result(void) while (node->rb_left) node = node->rb_left; - 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); } struct trace_lock_handler { From patchwork Wed Feb 12 22:24:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Tse Shao X-Patchwork-Id: 13972545 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 038AA20408D for ; Wed, 12 Feb 2025 22:31:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399484; cv=none; b=mc808l1g22AnTTIGW3WohJ9baimTIbWbxTinlmlh7rlYgPv9/tiiRk7A/gRVNl6jsV4PMd/eF1JJJeYmnbjutKB2jtyT5g5GnlIjHk8gIfnYLj3x0uaqjjRdq5l6AOR57Xun4YwxnHRCst1P7GcMOjc7QVx8t4mOuZ/WTcR8NZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399484; c=relaxed/simple; bh=+oY4H/9/iKeBTJJnO2W7fp5SJV9ZRax204HZtg1Ok1k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YxV4ZzwhDZmNZfzoiM1rfHwSpqFti7F4uYXgQMiZttJGFEA8nyMfCh51RUzaEUQzJrqgfTH4dwYZ9Hq2b+sZ9Q9TFeXYoqCKkJfrauT23KvlqJvpeCPdkFCH/RJS2QZ9JOl9Pziyh3YWq0oOfde/UfduoC7I8/J9PDyP70+UYDk= 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=L/fjPlef; arc=none smtp.client-ip=209.85.216.73 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="L/fjPlef" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f9da17946fso924325a91.3 for ; Wed, 12 Feb 2025 14:31:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739399482; x=1740004282; 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=PNqZSc1JLn0dwuRoJKceisL6qvWdg1Q9d+T90xi+KO0=; b=L/fjPlef3CulS5bESkypXCe1VAnl8MAYAYhJRkZghnARlIhDQgvyXliErIA8yh83CK hbA2BHomxq18undsEtWP6HMBB7q2m7OU9UKHe6nxXEkIju0ewfmZ+CMvp+NkTF+gIOye HAZRuj5+oycFZhsQTKMH8flpN3oeawg3NI69IXIXxmd9WLs84gtUNtOjilHAfEaStUMW 43oKzBk4akeF5R1/WqCARM9kT4pFd5LKr4tbcfJ/8dyMfKCWEjDbwrbXVSCJ/t79vpDy u/XAi1APxjbDeITtioD830vNMJbygZSaXI6LSWOtfNdipT/tCoQXIoxsGycVFhzy033G Uxzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739399482; x=1740004282; 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=PNqZSc1JLn0dwuRoJKceisL6qvWdg1Q9d+T90xi+KO0=; b=nEyHgrTvIrHoVYhYHqt+TtER6F1E7jhk+euk7XXvD6l9syHT8IPDH+7Nsk67g7+w6z UTgWSgE6mEgB9idHLpt2AXhYwlqKg7005qk7bN9ub4sdJqgtf307cBK5CO24jxLZpPyS 3JLrGRykvdt1X+xxJYWZtjooIDnN53e26Q5vMfJ1/do03sTTwjLHWqo8vnqQ9lgRRBK6 FPgJE3kauNzLAOP7dKOH8J5ul2KnpNdUvglZB0TCH0il7/SPqgwvGMJfg4d8DAyG5G15 BYS2L+nJ8y8xnnJCWj8FsB6rWW7w9BFOlOHWylH1KF9FPuDDj36V3+NWV0sBpjUUMf2m z6Bw== X-Forwarded-Encrypted: i=1; AJvYcCUgqkRHqI3D2atttdYhG8xw90k3t6cBW6QEM+Nr+e1aD/TvkUeY1DEfBPbTBqO+UU8WDZc=@vger.kernel.org X-Gm-Message-State: AOJu0YwH6peDRLtVleiaPkF21b2ROrqUqNc1i1mPrBDzoSVAGEd/vYPc Q443/V1tUiXaL1vWQ7m1emiNgY4Tv5M+W4Y1JcVGDsR1b3+FOzKrsASHuesL3f/jXZqSAAXqhLt 1fA== X-Google-Smtp-Source: AGHT+IHpscGhRRSM86wF3Vg9mBVITGPBSWOzu8ifxEDnBRXopIq1MT6mUs+2OPyt7cpCD2ZCuLBegQyMQXs= X-Received: from pjyf13.prod.google.com ([2002:a17:90a:ec8d:b0:2ef:78ff:bc3b]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2f06:b0:2ee:53b3:3f1c with SMTP id 98e67ed59e1d1-2fbf5bc1a9dmr7256534a91.5.1739399482247; Wed, 12 Feb 2025 14:31:22 -0800 (PST) Date: Wed, 12 Feb 2025 14:24:55 -0800 In-Reply-To: <20250212222859.2086080-1-ctshao@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250212222859.2086080-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212222859.2086080-5-ctshao@google.com> Subject: [PATCH v5 4/5] 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, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Parse `owner_lock_stat` into a rb tree, and report owner lock stats with stack trace in order. Example output: $ sudo ~/linux/tools/perf/perf lock con -abvo -Y mutex-spin -E3 perf bench sched pipe ... contended total wait max wait avg wait type caller 171 1.55 ms 20.26 us 9.06 us mutex pipe_read+0x57 0xffffffffac6318e7 pipe_read+0x57 0xffffffffac623862 vfs_read+0x332 0xffffffffac62434b ksys_read+0xbb 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwframe+0x76 36 193.71 us 15.27 us 5.38 us mutex pipe_write+0x50 0xffffffffac631ee0 pipe_write+0x50 0xffffffffac6241db vfs_write+0x3bb 0xffffffffac6244ab ksys_write+0xbb 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwframe+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_hwframe+0x76 === owner stack trace === 3 31.24 us 15.27 us 10.41 us mutex pipe_read+0x348 0xffffffffac631bd8 pipe_read+0x348 0xffffffffac623862 vfs_read+0x332 0xffffffffac62434b ksys_read+0xbb 0xfffffffface604b2 do_syscall_64+0x82 0xffffffffad00012f entry_SYSCALL_64_after_hwframe+0x76 ... Signed-off-by: Chun-Tse Shao --- tools/perf/builtin-lock.c | 19 ++++++++-- tools/perf/util/bpf_lock_contention.c | 54 +++++++++++++++++++++++++++ tools/perf/util/lock-contention.h | 7 ++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 9bebc186286f..3dc100cf30ef 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -1817,6 +1817,22 @@ static void print_contention_result(struct lock_contention *con) break; } + if (con->owner && con->save_callstack) { + struct rb_root root = RB_ROOT; + + if (symbol_conf.field_sep) + fprintf(lock_output, "# owner stack trace:\n"); + else + fprintf(lock_output, "\n=== owner stack trace ===\n\n"); + while ((st = pop_owner_stack_trace(con))) + insert_to(&root, st, compare); + + while ((st = pop_from(&root))) { + print_lock_stat(con, st); + zfree(st); + } + } + if (print_nr_entries) { /* update the total/bad stats */ while ((st = pop_from_result())) { @@ -1962,9 +1978,6 @@ static int check_lock_contention_options(const struct option *options, } } - if (show_lock_owner) - show_thread_stats = true; - return 0; } diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 76542b86e83f..dc83b02c9724 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -549,6 +549,60 @@ static const char *lock_contention_get_name(struct lock_contention *con, return name_buf; } +struct lock_stat *pop_owner_stack_trace(struct lock_contention *con) +{ + int stacks_fd, stat_fd; + u64 *stack_trace; + s32 stack_id; + struct contention_key ckey = {}; + struct contention_data cdata = {}; + size_t stack_size = con->max_stack * sizeof(*stack_trace); + struct lock_stat *st; + char name[KSYM_NAME_LEN]; + + stacks_fd = bpf_map__fd(skel->maps.owner_stacks); + stat_fd = bpf_map__fd(skel->maps.owner_stat); + if (!stacks_fd || !stat_fd) + return NULL; + + stack_trace = zalloc(stack_size); + if (stack_trace == NULL) + return NULL; + + if (bpf_map_get_next_key(stacks_fd, NULL, stack_trace)) + return NULL; + + bpf_map_lookup_elem(stacks_fd, stack_trace, &stack_id); + ckey.stack_id = stack_id; + bpf_map_lookup_elem(stat_fd, &ckey, &cdata); + + st = zalloc(sizeof(struct lock_stat)); + if (!st) + return NULL; + + strcpy(name, + stack_trace[0] ? lock_contention_get_name(con, NULL, stack_trace, 0) : "unknown"); + + st->name = strdup(name); + if (!st->name) + return NULL; + + st->flags = cdata.flags; + st->nr_contended = cdata.count; + st->wait_time_total = cdata.total_time; + st->wait_time_max = cdata.max_time; + st->wait_time_min = cdata.min_time; + st->callstack = stack_trace; + + if (cdata.count) + st->avg_wait_time = cdata.total_time / cdata.count; + + bpf_map_delete_elem(stacks_fd, stack_trace); + bpf_map_delete_elem(stat_fd, &ckey); + + return st; +} + int lock_contention_read(struct lock_contention *con) { int fd, stack, err = 0; diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-contention.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); +struct lock_stat *pop_owner_stack_trace(struct lock_contention *con); + #else /* !HAVE_BPF_SKEL */ static inline int lock_contention_prepare(struct lock_contention *con __maybe_unused) @@ -187,6 +189,11 @@ static inline int lock_contention_read(struct lock_contention *con __maybe_unuse return 0; } +struct lock_stat *pop_owner_stack_trace(struct lock_contention *con) +{ + return NULL; +} + #endif /* HAVE_BPF_SKEL */ #endif /* PERF_LOCK_CONTENTION_H */ From patchwork Wed Feb 12 22:24:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Tse Shao X-Patchwork-Id: 13972546 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 737001FFC59 for ; Wed, 12 Feb 2025 22:31:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399518; cv=none; b=B7IIDCVJj0KcHbaDP4/TAjN5eqdpB5eP440Jh9TPQed6kQ8k5l5Ejv0BvttE6hb/Y6w1W/rMx9w3+gwUI/GxkMn7vJ8EbNllGWKn5YV+3llj5YIU/7CkRH7kAwCl1LtGxEP2MGmA9HZvq5V0mK6zi/tYEWPxoFL1WzgHlNR0tnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739399518; c=relaxed/simple; bh=9xYAmC7Odj+wcG5scUKb2hxhqBVZwbTv98XQABN1UOE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=S65D5pvnGbmCHHUTW6kWNJoARz4rD+QXqZouaowBF5Ah9TYgDhSugZKC/WBlNfgq/jWb6Jpj/ITpoDEGV3P6S3tj+fIopOLbHTWChE73a0K/Il46+RnHpCwymF39islL3xkBQ+RiSAJ80Jqzgf6U1D9Bu9ZFfKd2saMoHdaQLwE= 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=sLT1C2rD; arc=none smtp.client-ip=209.85.216.73 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="sLT1C2rD" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa228b4151so536769a91.1 for ; Wed, 12 Feb 2025 14:31:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739399517; x=1740004317; 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=P4u0lqhDVCs3kWFzXSRSy88eSlq3hW173Ud7Yk2USkk=; b=sLT1C2rDOEyzzLjEjuaRckKwaQ00FSPM7NcwXHLowCOWOBl7jqp258G7LJ6nhbMS3N 3KCYLouS3RjIFHy+L6x32LYG/wvm4gdvQ92X3Lmf7eeRZvMcIMZUiPFq1iHrpdJM7sj+ /1Vq/Ul3gknf1Q9T1DviNgGoT67r/Dxz8Ce4oOBWyvjPqeLT2+ihOMXr6AXpngEJjxPL om5whT5SD6ile3O21yTz2NxFZLQTTgjVruCtLeSUCT7bXdF2X9OD+6lf8AEH+0pi6oo0 n7IvgLC7MsPON58ug2Ah5HAjyUAdwUGb0kwtbZGAo2PibTzeWSmYid8jA/Yqd8zmavmg 2hIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739399517; x=1740004317; 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=P4u0lqhDVCs3kWFzXSRSy88eSlq3hW173Ud7Yk2USkk=; b=qDxPfJziMZdb3m6LzQsI0aA0xHjMaCDd47wmSbZ6CwYzsmRE1wYAe9zejMOPLkQwAY Lf4bvWZv7WSu3JLilKoeCSmAGDPJucJ6VfgbRxRAhdeuAf+qF14VrAjVpjHskN0xs/DK qW/h7WD17nxRSqcmvwyf4dp58Exs8TfUMiA4ubqU1rhDg+esUVuRwQMukWWcYc+iCJSh R+4dj6VU+1qGIt2lanBmfY1zDtPUXqJ3Ufkb9Ql6+8+k6L2/NLiefSxZtnyoG0q3jHsw i0M6P3wDb2BTGiSZntS/ZDiehLYp1bnUyHzzBYCZB3r3WmYj/FDLs1+UytbdxDUXbujh 7n5Q== X-Forwarded-Encrypted: i=1; AJvYcCVObRsZ1n8nHy+cnwQkm4H7fx/YLPazukB/FhTuofEHkDf2osVnXdrwwwo5stlgCZrwjQ8=@vger.kernel.org X-Gm-Message-State: AOJu0YxUJ87D8xY6n6FPY6NL2W9Cp2NP/iCim1p3GawFvdO21Y44jxMw 6/oy5gY39wrHtoGD3BibB7DjywZ20Rzl4Cy1j0IOKl8Zdnf1ozLehVCwLmmCjgT35QsMEztmoP0 f8g== X-Google-Smtp-Source: AGHT+IHCkcBnyBvA5rBqb0nlmu6y/i7b8JGoyzx+WFgpOL9irq8xmcP/AfVCc/JAi46e7AZbhgN0hE+xX1s= X-Received: from pjbpt6.prod.google.com ([2002:a17:90b:3d06:b0:2ef:7af4:5e8e]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c04:b0:2fa:f8d:65e7 with SMTP id 98e67ed59e1d1-2fbf5bb574fmr8098207a91.2.1739399516973; Wed, 12 Feb 2025 14:31:56 -0800 (PST) Date: Wed, 12 Feb 2025 14:24:56 -0800 In-Reply-To: <20250212222859.2086080-1-ctshao@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250212222859.2086080-1-ctshao@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212222859.2086080-6-ctshao@google.com> Subject: [PATCH v5 5/5] perf lock: Update documentation for -o option in contention mode 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, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org This patch also decouple -o with -t, and shows warning to notify the new behavior for -ov. Signed-off-by: Chun-Tse Shao --- tools/perf/builtin-lock.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 3dc100cf30ef..e16bda6ce525 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -1817,7 +1817,7 @@ static void print_contention_result(struct lock_contention *con) break; } - if (con->owner && con->save_callstack) { + if (con->owner && con->save_callstack && verbose > 0) { struct rb_root root = RB_ROOT; if (symbol_conf.field_sep) @@ -1978,6 +1978,11 @@ static int check_lock_contention_options(const struct option *options, } } + 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"); + } + return 0; } @@ -2569,7 +2574,8 @@ int cmd_lock(int argc, const char **argv) "Filter specific address/symbol of locks", parse_lock_addr), OPT_CALLBACK('S', "callstack-filter", NULL, "NAMES", "Filter specific function in the callstack", parse_call_stack), - OPT_BOOLEAN('o', "lock-owner", &show_lock_owner, "show lock owners instead of waiters"), + OPT_BOOLEAN('o', "lock-owner", &show_lock_owner, "show lock owners instead of waiters.\n" + "\t\t\tThis option can be combined with -t, which shows owner's per thread lock stats, or -v, which shows owner's stacktrace"), OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, "separator", "print result in CSV format with custom separator"), OPT_BOOLEAN(0, "lock-cgroup", &show_lock_cgroups, "show lock stats by cgroup"),