From nobody Sun Apr 12 21:46:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0B3CC4332F for ; Mon, 19 Dec 2022 20:17:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232268AbiLSURy (ORCPT ); Mon, 19 Dec 2022 15:17:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232455AbiLSURl (ORCPT ); Mon, 19 Dec 2022 15:17:41 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05CD9D4B; Mon, 19 Dec 2022 12:17:41 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id gt4so10265742pjb.1; Mon, 19 Dec 2022 12:17:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=cSBoXkTO9My+aXQVa73V9O3XAdMbkV24JOkMxSiJcY8=; b=ldw1Ubfb3TjOoV8lyocAD3ad70POYsiMVbiHAwjBkuOH+en9Ff+QJWE9C0z0rHdUtS rxj8pXSIgt810LRp8agyqRdfOrRmISRLFKkn8Gdwss1F+wxjhx8MCFOqEhJnO/SMriPE faKs7reTjqSH1TDRPmP45MfiCdvX8IeDDjWVKlsDu5B+g9Andgeu2gK04A0zTufBniTa HKXG26BYX0FbhxbWC82Y8UqFj8E+jyuFVktFK9CuMNC4T31fKCBtaH3F2ODEJYAYj7s9 M6wgVUHUOPG9T4gwgtFEPC3bW7JsMAiR5j9e5wJqOGb8gQX/LCGbMRUmTWB+E793StJn Z/UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cSBoXkTO9My+aXQVa73V9O3XAdMbkV24JOkMxSiJcY8=; b=48w/RqQNrJKwu5L/QdIJvt5afRxLZkWfY8MQmc1UTtrJ/9IjVxjJyGwch++giKGoHL w+V1QV4DA/ZEW+QTyOht648bjQvcK+bKB1IH7ULlwC9lGt9xxAD/v8QEZGFpHFbanddd flereDlVa9I9rabr6EgMy41XXI7JbejUpmERNbSsoM3ii2CAABvOk1SiNA/B0oi6iekI nLhXnhVmHLza22fk0HLp/l2iD1iCap/9M/YctxzSV4SKNMUejgoPZarBT1mVRuM+MnqN ONWV+foqainSOgXNABVx839ixG9TW8YroGUdURWmcI1cp69gT6+sut9Twe+Qg1CRITAk VXMQ== X-Gm-Message-State: AFqh2kphrbNHq26qQnVmIcFXmRg9hmDyXS5CE0e/8ZCq2yfq89NzCOkX lUrWLqZ7oRcSl21TJLMgandOsFPkkpM= X-Google-Smtp-Source: AMrXdXssdnN3A8dp+4at4lNkqCo6ZBKkRRyB4eNIrVaWlnhtXU8nQXWQOpDTF+3bg0oHij03Ht8v4w== X-Received: by 2002:a17:90a:c7d3:b0:219:ba3d:7ee2 with SMTP id gf19-20020a17090ac7d300b00219ba3d7ee2mr11600054pjb.30.1671481060401; Mon, 19 Dec 2022 12:17:40 -0800 (PST) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:ff0:735a:2805:e706:5164]) by smtp.gmail.com with ESMTPSA id f10-20020a17090ace0a00b00219220edf0dsm6324260pju.48.2022.12.19.12.17.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 12:17:39 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, Song Liu , Blake Jones , bpf@vger.kernel.org Subject: [PATCH 3/6] perf lock contention: Support lock type filtering for BPF Date: Mon, 19 Dec 2022 12:17:29 -0800 Message-Id: <20221219201732.460111-4-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20221219201732.460111-1-namhyung@kernel.org> References: <20221219201732.460111-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Likewise, add type_filter BPF hash map and check it when user gave a lock type filter. $ sudo ./perf lock con -ab -Y rwlock -- ./perf bench sched messaging # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups =3D=3D 400 processes run Total time: 0.203 [sec] contended total wait max wait avg wait type caller 15 156.19 us 19.45 us 10.41 us rwlock:W do_exit+= 0x36d 1 11.12 us 11.12 us 11.12 us rwlock:R do_wait+= 0x8b 1 5.09 us 5.09 us 5.09 us rwlock:W release_= task+0x6e Signed-off-by: Namhyung Kim --- tools/perf/builtin-lock.c | 1 + tools/perf/util/bpf_lock_contention.c | 15 ++++++++++++- .../perf/util/bpf_skel/lock_contention.bpf.c | 21 +++++++++++++++++-- tools/perf/util/lock-contention.h | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 49b4add53204..e4e785d3b4ec 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -1689,6 +1689,7 @@ static int __cmd_contention(int argc, const char **ar= gv) .map_nr_entries =3D bpf_map_entries, .max_stack =3D max_stack_depth, .stack_skip =3D stack_skip, + .filters =3D &filters, }; =20 session =3D perf_session__new(use_bpf ? NULL : &data, &eops); diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index 8e1b791dc58f..b8590b82ad3d 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -20,7 +20,7 @@ static struct lock_contention_bpf *skel; int lock_contention_prepare(struct lock_contention *con) { int i, fd; - int ncpus =3D 1, ntasks =3D 1; + int ncpus =3D 1, ntasks =3D 1, ntypes =3D 1; struct evlist *evlist =3D con->evlist; struct target *target =3D con->target; =20 @@ -46,9 +46,12 @@ int lock_contention_prepare(struct lock_contention *con) ncpus =3D perf_cpu_map__nr(evlist->core.user_requested_cpus); if (target__has_task(target)) ntasks =3D perf_thread_map__nr(evlist->core.threads); + if (con->filters->nr_types) + ntypes =3D con->filters->nr_types; =20 bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + bpf_map__set_max_entries(skel->maps.type_filter, ntypes); =20 if (lock_contention_bpf__load(skel) < 0) { pr_err("Failed to load lock-contention BPF skeleton\n"); @@ -90,6 +93,16 @@ int lock_contention_prepare(struct lock_contention *con) bpf_map_update_elem(fd, &pid, &val, BPF_ANY); } =20 + if (con->filters->nr_types) { + u8 val =3D 1; + + skel->bss->has_type =3D 1; + fd =3D bpf_map__fd(skel->maps.type_filter); + + for (i =3D 0; i < con->filters->nr_types; i++) + bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY); + } + /* these don't work well if in the rodata section */ skel->bss->stack_skip =3D con->stack_skip; skel->bss->aggr_mode =3D con->aggr_mode; diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/ut= il/bpf_skel/lock_contention.bpf.c index 11b0fc7ee53b..fb0128de7c00 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -62,10 +62,18 @@ struct { __uint(max_entries, 1); } task_filter SEC(".maps"); =20 +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u8)); + __uint(max_entries, 1); +} type_filter SEC(".maps"); + /* control flags */ int enabled; int has_cpu; int has_task; +int has_type; int stack_skip; =20 /* determine the key of lock stat */ @@ -74,7 +82,7 @@ int aggr_mode; /* error stat */ int lost; =20 -static inline int can_record(void) +static inline int can_record(u64 *ctx) { if (has_cpu) { __u32 cpu =3D bpf_get_smp_processor_id(); @@ -94,6 +102,15 @@ static inline int can_record(void) return 0; } =20 + if (has_type) { + __u8 *ok; + __u32 flags =3D (__u32)ctx[1]; + + ok =3D bpf_map_lookup_elem(&type_filter, &flags); + if (!ok) + return 0; + } + return 1; } =20 @@ -116,7 +133,7 @@ int contention_begin(u64 *ctx) __u32 pid; struct tstamp_data *pelem; =20 - if (!enabled || !can_record()) + if (!enabled || !can_record(ctx)) return 0; =20 pid =3D bpf_get_current_pid_tgid(); diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-conte= ntion.h index d5b75b222d8e..dc621386a16b 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -118,6 +118,7 @@ struct lock_contention { struct target *target; struct machine *machine; struct hlist_head *result; + struct lock_filter *filters; unsigned long map_nr_entries; int lost; int max_stack; --=20 2.39.0.314.g84b9a713c41-goog