From nobody Sun Apr 12 20:03:07 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 0E3FBC4332F for ; Mon, 19 Dec 2022 20:17:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232568AbiLSURt (ORCPT ); Mon, 19 Dec 2022 15:17:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232277AbiLSURi (ORCPT ); Mon, 19 Dec 2022 15:17:38 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4741D4B; Mon, 19 Dec 2022 12:17:37 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id o12so10234576pjo.4; Mon, 19 Dec 2022 12:17:37 -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=laFGg4zCVR1KZhPUCJ4Z8IBvPLNSm6yToKYeukylBe4=; b=JSaw3AL5Qs/aLLEIhZkGlWQjtbiLd6reqEJgtPQtVZWqk0tC2naigv64Nj8txbOkSY Ax7iDwwirwjgjoQmbAT3Igf0talaUi9NfsHRvHsVAu4ZNe/1jo1+GFuDCSxuQQjwDh2n Xqz7Gm0IcbnJEv6jFEDYDpxWgJq4m9sCItnJgKTRzBN4CROcbvX83Uvokjg/Y7DRymCC Un1Av0URxnapT46oZmGXG9OgyYbfmUaYCrs6crzX6mA8EQ+qpw8Rzzif1JA8SA4SFRAQ yshDpe7EWXwsL/hV+KWZq0NM4mRCP7IXrfQQ6QOnbdaZmVg/6HJhORL9RF1FgHrFZaR5 Fj2g== 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=laFGg4zCVR1KZhPUCJ4Z8IBvPLNSm6yToKYeukylBe4=; b=2qoOZGi55D28aaDryMlCe6JxvXND4Rdw9ao+HfnHyBFshdKcxlICS3aDDUcmTcAtsr qlgfLiSDI4z15eV2vvCHqDgr9gp3oWLhYQhrfGzkd1nhkwlAm01090nE2QjbQJws5c8P Ask2JItfU4mOI4/5nui1J9qjWfsWloFjl3twac397u+okLOaai2uKYS9RAzn612SqApt R+ov0PzEzZ7tLJ7QYMi2fIGvDtCiQVg2rMaJvhWMsXWQdeknbcBFNOIqLygYZbb6uEcm xWFqxhEMq62wYeWhrRgj41EXe1JhfnZNIQMacf2xvONADwDjFXmRL+gFEt2YF+Adscsa biiQ== X-Gm-Message-State: ANoB5pl9oPAc0rhfH+jngTPG4WdsblRS29Uj5FkTurJnMBiFXKyVrsXZ 51q+8TGCAbQkb5CmgtZzUGE= X-Google-Smtp-Source: AA0mqf7TTcW05+NqmqIunPoYnOIUVQuwXu7tpi2V6Az2fSsxUN4be9vsep2gtzl9dVmsNipX4iHYow== X-Received: by 2002:a17:90b:1d05:b0:219:f993:7f0f with SMTP id on5-20020a17090b1d0500b00219f9937f0fmr45744797pjb.11.1671481057295; Mon, 19 Dec 2022 12:17:37 -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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 12:17:36 -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 1/6] perf lock contention: Factor out lock_type_table Date: Mon, 19 Dec 2022 12:17:27 -0800 Message-Id: <20221219201732.460111-2-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" Move it out of get_type_str() so that we can reuse the table for others later. Signed-off-by: Namhyung Kim --- tools/perf/builtin-lock.c | 48 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 25c0a5e5051f..548d81eb0b18 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -1437,30 +1437,30 @@ static void sort_result(void) } } =20 -static const char *get_type_str(struct lock_stat *st) -{ - static const struct { - unsigned int flags; - const char *name; - } table[] =3D { - { 0, "semaphore" }, - { LCB_F_SPIN, "spinlock" }, - { LCB_F_SPIN | LCB_F_READ, "rwlock:R" }, - { LCB_F_SPIN | LCB_F_WRITE, "rwlock:W"}, - { LCB_F_READ, "rwsem:R" }, - { LCB_F_WRITE, "rwsem:W" }, - { LCB_F_RT, "rtmutex" }, - { LCB_F_RT | LCB_F_READ, "rwlock-rt:R" }, - { LCB_F_RT | LCB_F_WRITE, "rwlock-rt:W"}, - { LCB_F_PERCPU | LCB_F_READ, "pcpu-sem:R" }, - { LCB_F_PERCPU | LCB_F_WRITE, "pcpu-sem:W" }, - { LCB_F_MUTEX, "mutex" }, - { LCB_F_MUTEX | LCB_F_SPIN, "mutex" }, - }; +static const struct { + unsigned int flags; + const char *name; +} lock_type_table[] =3D { + { 0, "semaphore" }, + { LCB_F_SPIN, "spinlock" }, + { LCB_F_SPIN | LCB_F_READ, "rwlock:R" }, + { LCB_F_SPIN | LCB_F_WRITE, "rwlock:W"}, + { LCB_F_READ, "rwsem:R" }, + { LCB_F_WRITE, "rwsem:W" }, + { LCB_F_RT, "rtmutex" }, + { LCB_F_RT | LCB_F_READ, "rwlock-rt:R" }, + { LCB_F_RT | LCB_F_WRITE, "rwlock-rt:W"}, + { LCB_F_PERCPU | LCB_F_READ, "pcpu-sem:R" }, + { LCB_F_PERCPU | LCB_F_WRITE, "pcpu-sem:W" }, + { LCB_F_MUTEX, "mutex" }, + { LCB_F_MUTEX | LCB_F_SPIN, "mutex" }, +}; =20 - for (unsigned int i =3D 0; i < ARRAY_SIZE(table); i++) { - if (table[i].flags =3D=3D st->flags) - return table[i].name; +static const char *get_type_str(unsigned int flags) +{ + for (unsigned int i =3D 0; i < ARRAY_SIZE(lock_type_table); i++) { + if (lock_type_table[i].flags =3D=3D flags) + return lock_type_table[i].name; } return "unknown"; } @@ -1514,7 +1514,7 @@ static void print_contention_result(struct lock_conte= ntion *con) =20 switch (aggr_mode) { case LOCK_AGGR_CALLER: - pr_info(" %10s %s\n", get_type_str(st), st->name); + pr_info(" %10s %s\n", get_type_str(st->flags), st->name); break; case LOCK_AGGR_TASK: pid =3D st->addr; --=20 2.39.0.314.g84b9a713c41-goog From nobody Sun Apr 12 20:03:07 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 E4839C4332F for ; Mon, 19 Dec 2022 20:17:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232539AbiLSUR6 (ORCPT ); Mon, 19 Dec 2022 15:17:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232394AbiLSURk (ORCPT ); Mon, 19 Dec 2022 15:17:40 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9528FB20; Mon, 19 Dec 2022 12:17:39 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id o1-20020a17090a678100b00219cf69e5f0so14300484pjj.2; Mon, 19 Dec 2022 12:17:39 -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=EZkB4QuSwHTQCLJv4c82Z6FZXzye3hmuSZyyGw5CfKo=; b=ffiQULjKKRzXhpUY0AzbdmAOwIDrA+JSyz+7u/IQszYHdVb+znQ4lQ5LQmyT9vz1vB Y9sYFLbWMtzGR7pZBnPcc4W9+XRpJjCAU8uzN2N+u1LpmAgNd9S6rmHdkL2da/xRoF29 i8qeCJhC6s4WGDR0jRTnsn4fQd9mY6dcjuMZEzlS7B+SFSZrIOiBbNRv9gPRKbTwHFK6 VH4y+cD6UW1UIdUhmswcNWkt/GVvzxeiJyBM6wx7MWxcIB/tcSLIBEK3kN+BF3D3rRTB rNkonqIE+x/oO0qO7XtQAOP/tOOdPTbkHRj7zzmW956ht/AoaUU98gmBni4/1+009SLs dQuQ== 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=EZkB4QuSwHTQCLJv4c82Z6FZXzye3hmuSZyyGw5CfKo=; b=j0wQXCjzLgE7nBz6cyJ2tvAF+y2KTnv0UCVOKfwFyCt7MrXKdTYAP7WdzNv3/oimM+ UE/qGgZ+8+0bH6JFlfgTkIdTggGJJfmo25HhjBZV6dCvwOhxmbMKwYP3lrR8Q4fvVBRW uBbHg3HCOozcpyKslBQlfxF1iPGEhSCekdMSKlbu947bW/EZRwaoonADzjUNxM3KiofW CsdHomA2VZPAJR/TO/9au7UACHxewQSEuspa7YvmkkEO8Jtmx+gZR8UUMLeTaFZOEaEn LN0nCYsfyE1P/xAnhV5jwRpImGMcaluCJCnKmgomrjH5ulMkswIvr+9sgXzkRZNmwpD1 hRqg== X-Gm-Message-State: ANoB5pkp2l0WGNPJozEmfCyMS8ZE5FuvWR1CygocjOu6PKabEMyYxPWf I/q3tqktlEdu7GxnuCPxRZM= X-Google-Smtp-Source: AA0mqf7UmdNe91hR1c61hwA1v4sjJlxe+G174ZISXTbzc90YhmHSBZ21f+TiztAu0CpzHI7th64j8g== X-Received: by 2002:a17:90b:1bcc:b0:223:2654:9dd0 with SMTP id oa12-20020a17090b1bcc00b0022326549dd0mr26243137pjb.28.1671481058913; Mon, 19 Dec 2022 12:17:38 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 12:17:38 -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 2/6] perf lock contention: Add -Y/--type-filter option Date: Mon, 19 Dec 2022 12:17:28 -0800 Message-Id: <20221219201732.460111-3-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" The -Y/--type-filter option is to filter the result for specific lock types only. It can accept comma-separated values. Note that it would accept type names like one in the output. spinlock, mutex, rwsem:R and so on. For RW-variant lock types, it converts the name to the both variants. In other words, "rwsem" is same as "rwsem:R,rwsem:W". Also note that "mutex" has two different encoding - one for sleeping wait, another for optimistic spinning. Add "mutex-spin" entry for the lock_type_table so that we can add it for "mutex" under the table. $ sudo ./perf lock record -a -- ./perf bench sched messaging $ sudo ./perf lock con -E 5 -Y spinlock contended total wait max wait avg wait type caller 802 1.26 ms 11.73 us 1.58 us spinlock __wake_u= p_common_lock+0x62 13 787.16 us 105.44 us 60.55 us spinlock remove_w= ait_queue+0x14 12 612.96 us 78.70 us 51.08 us spinlock prepare_= to_wait+0x27 114 340.68 us 12.61 us 2.99 us spinlock try_to_w= ake_up+0x1f5 83 226.38 us 9.15 us 2.73 us spinlock folio_lr= uvec_lock_irqsave+0x5e Signed-off-by: Namhyung Kim Acked-by: Namhyung Kim --- tools/perf/Documentation/perf-lock.txt | 23 +++-- tools/perf/builtin-lock.c | 116 ++++++++++++++++++++++++- tools/perf/util/lock-contention.h | 5 ++ 3 files changed, 136 insertions(+), 8 deletions(-) diff --git a/tools/perf/Documentation/perf-lock.txt b/tools/perf/Documentat= ion/perf-lock.txt index 38e79d45e426..dea04ad5c28e 100644 --- a/tools/perf/Documentation/perf-lock.txt +++ b/tools/perf/Documentation/perf-lock.txt @@ -143,25 +143,25 @@ CONTENTION OPTIONS System-wide collection from all CPUs. =20 -C:: ---cpu:: +--cpu=3D:: Collect samples only on the list of CPUs provided. Multiple CPUs can be provided as a comma-separated list with no space: 0,1. Ranges of CPUs are specified with -: 0-2. Default is to monitor all CPUs. =20 -p:: ---pid=3D:: +--pid=3D:: Record events on existing process ID (comma separated list). =20 ---tid=3D:: +--tid=3D:: Record events on existing thread ID (comma separated list). =20 ---map-nr-entries:: +--map-nr-entries=3D:: Maximum number of BPF map entries (default: 10240). =20 ---max-stack:: +--max-stack=3D:: Maximum stack depth when collecting lock contention (default: 8). =20 ---stack-skip +--stack-skip=3D:: Number of stack depth to skip when finding a lock caller (default: 3). =20 -E:: @@ -172,6 +172,17 @@ CONTENTION OPTIONS --lock-addr:: Show lock contention stat by address =20 +-Y:: +--type-filter=3D:: + Show lock contention only for given lock types (comma separated list). + Available values are: + semaphore, spinlock, rwlock, rwlock:R, rwlock:W, rwsem, rwsem:R, rwsem:= W, + rtmutex, rwlock-rt, rwlock-rt:R, rwlock-rt:W, pcpu-sem, pcpu-sem:R, pcp= u-sem:W, + mutex + + Note that RW-variant of locks have :R and :W suffix. Names without the + suffix are shortcuts for the both variants. Ex) rwsem =3D rwsem:R + rwse= m:W. + =20 SEE ALSO -------- diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 548d81eb0b18..49b4add53204 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -63,6 +63,8 @@ static int max_stack_depth =3D CONTENTION_STACK_DEPTH; static int stack_skip =3D CONTENTION_STACK_SKIP; static int print_nr_entries =3D INT_MAX / 2; =20 +static struct lock_filter filters; + static enum lock_aggr_mode aggr_mode =3D LOCK_AGGR_ADDR; =20 static struct thread_stat *thread_stat_find(u32 tid) @@ -990,8 +992,9 @@ static int report_lock_contention_begin_event(struct ev= sel *evsel, struct thread_stat *ts; struct lock_seq_stat *seq; u64 addr =3D evsel__intval(evsel, sample, "lock_addr"); + unsigned int flags =3D evsel__intval(evsel, sample, "flags"); u64 key; - int ret; + int i, ret; =20 ret =3D get_key_by_aggr_mode(&key, addr, evsel, sample); if (ret < 0) @@ -1001,7 +1004,6 @@ static int report_lock_contention_begin_event(struct = evsel *evsel, if (!ls) { char buf[128]; const char *name =3D ""; - unsigned int flags =3D evsel__intval(evsel, sample, "flags"); struct machine *machine =3D &session->machines.host; struct map *kmap; struct symbol *sym; @@ -1036,6 +1038,20 @@ static int report_lock_contention_begin_event(struct= evsel *evsel, } } =20 + if (filters.nr_types) { + bool found =3D false; + + for (i =3D 0; i < filters.nr_types; i++) { + if (flags =3D=3D filters.types[i]) { + found =3D true; + break; + } + } + + if (!found) + return 0; + } + ts =3D thread_stat_findnew(sample->tid); if (!ts) return -ENOMEM; @@ -1454,6 +1470,8 @@ static const struct { { LCB_F_PERCPU | LCB_F_WRITE, "pcpu-sem:W" }, { LCB_F_MUTEX, "mutex" }, { LCB_F_MUTEX | LCB_F_SPIN, "mutex" }, + /* alias for get_type_flag() */ + { LCB_F_MUTEX | LCB_F_SPIN, "mutex-spin" }, }; =20 static const char *get_type_str(unsigned int flags) @@ -1465,6 +1483,21 @@ static const char *get_type_str(unsigned int flags) return "unknown"; } =20 +static unsigned int get_type_flag(const char *str) +{ + for (unsigned int i =3D 0; i < ARRAY_SIZE(lock_type_table); i++) { + if (!strcmp(lock_type_table[i].name, str)) + return lock_type_table[i].flags; + } + return -1U; +} + +static void lock_filter_finish(void) +{ + zfree(&filters.types); + filters.nr_types =3D 0; +} + static void sort_contention_result(void) { sort_result(); @@ -1507,6 +1540,9 @@ static void print_contention_result(struct lock_conte= ntion *con) if (st->broken) bad++; =20 + if (!st->wait_time_total) + continue; + list_for_each_entry(key, &lock_keys, list) { key->print(key, st); pr_info(" "); @@ -1753,6 +1789,7 @@ static int __cmd_contention(int argc, const char **ar= gv) print_contention_result(&con); =20 out_delete: + lock_filter_finish(); evlist__delete(con.evlist); lock_contention_finish(); perf_session__delete(session); @@ -1884,6 +1921,79 @@ static int parse_max_stack(const struct option *opt,= const char *str, return 0; } =20 +static bool add_lock_type(unsigned int flags) +{ + unsigned int *tmp; + + tmp =3D realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.t= ypes)); + if (tmp =3D=3D NULL) + return false; + + tmp[filters.nr_types++] =3D flags; + filters.types =3D tmp; + return true; +} + +static int parse_lock_type(const struct option *opt __maybe_unused, const = char *str, + int unset __maybe_unused) +{ + char *s, *tmp, *tok; + int ret =3D 0; + + s =3D strdup(str); + if (s =3D=3D NULL) + return -1; + + for (tok =3D strtok_r(s, ", ", &tmp); tok; tok =3D strtok_r(NULL, ", ", &= tmp)) { + unsigned int flags =3D get_type_flag(tok); + + if (flags =3D=3D -1U) { + char buf[32]; + + if (strchr(tok, ':')) + continue; + + /* try :R and :W suffixes for rwlock, rwsem, ... */ + scnprintf(buf, sizeof(buf), "%s:R", tok); + flags =3D get_type_flag(buf); + if (flags !=3D -1UL) { + if (!add_lock_type(flags)) { + ret =3D -1; + break; + } + } + + scnprintf(buf, sizeof(buf), "%s:W", tok); + flags =3D get_type_flag(buf); + if (flags !=3D -1UL) { + if (!add_lock_type(flags)) { + ret =3D -1; + break; + } + } + continue; + } + + if (!add_lock_type(flags)) { + ret =3D -1; + break; + } + + if (!strcmp(tok, "mutex")) { + flags =3D get_type_flag("mutex-spin"); + if (flags !=3D -1UL) { + if (!add_lock_type(flags)) { + ret =3D -1; + break; + } + } + } + } + + free(s); + return ret; +} + int cmd_lock(int argc, const char **argv) { const struct option lock_options[] =3D { @@ -1947,6 +2057,8 @@ int cmd_lock(int argc, const char **argv) "Default: " __stringify(CONTENTION_STACK_SKIP)), OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functio= ns"), OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by addre= ss"), + OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS", + "Filter specific type of locks", parse_lock_type), OPT_PARENT(lock_options) }; =20 diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-conte= ntion.h index 47fd47fb56c1..d5b75b222d8e 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -5,6 +5,11 @@ #include #include =20 +struct lock_filter { + int nr_types; + unsigned int *types; +}; + struct lock_stat { struct hlist_node hash_entry; struct rb_node rb; /* used for sorting */ --=20 2.39.0.314.g84b9a713c41-goog From nobody Sun Apr 12 20:03:07 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 From nobody Sun Apr 12 20:03:07 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 8ADF5C4167B for ; Mon, 19 Dec 2022 20:18:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231556AbiLSUSC (ORCPT ); Mon, 19 Dec 2022 15:18:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232524AbiLSURn (ORCPT ); Mon, 19 Dec 2022 15:17:43 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD32464FD; Mon, 19 Dec 2022 12:17:42 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id 17so10201264pll.0; Mon, 19 Dec 2022 12:17:42 -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=RjRD6WezQur7RnAVSzg16H/TNhNuOpZXrFzdEgyZjWY=; b=K/iKHmimDZqirm0C7DqsQepDan/duix8fBLJ8QxlpMoI9Q/F+HJT8NS6kSDLd5FvI8 qHPfSE5jSlN0T/qSRbfhrt3vHNREdAWYIT8/6MRy/r4lKwV6ModsbZfTRjyVYv5egBnt rUUOw0Fm9+pt5kUgixtixgUCOUK7f1/Bk/Y4omdYwDKHAVaa1/ezTfDEYNutI+DEWJhm QcSAlVXIcUecVuO03SWTH2YonmxNyKIixBH0XCypanctfeQA0sNPLPwasaZcMfCF9Zyc OB/Mac6lGYTBl/WvGzy+O4F7FSkUxmDI1YSBn+o2XOBomWfUl8RVh2cS/2XOhBrtZt1g 0xNA== 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=RjRD6WezQur7RnAVSzg16H/TNhNuOpZXrFzdEgyZjWY=; b=zhk+/W+dC5XFG9wNKG+tSP8bl0viQRKBoacLZUaUNaxeoNXsMaqH8y6D7JZBPQWTKr HSx3mxG9DcJ9HAi8xJhRSj0d1n3uQ50UHcBVwBVZLKilgIE5Sy67y3AWB1tFFOleqGTG C5FFT51SB1dhPgOTjIGNhrgCiEBjvPmZjZnVU2KSD6UFabZMQydQKBfNpAHxx6Xu4rUs 5rqFvJeNxZODH/oMTFAuFnL4jYYQMQKP4Gv1GcAv5fVEBkNNf0bWu9PKz5XhAda4oD20 OV4B2zsOeIzNRRM0PcT10iRUqgrEXCSlD1Uh/0AKhsDgmWYFvpDhVWBY5tzElHMF1kWm PaoQ== X-Gm-Message-State: ANoB5plcZS4kubLNPMo5lBLG30W0ivkjRl2mv9l1w1Cqo0AdYn2zl/Tp lRl3LsnxtRv/25dRsMXCGtQ= X-Google-Smtp-Source: AA0mqf6od1Sl2FQ2RQ0VOzeajp7ymIBLRUWtAdXUHXhWOr03W5wAoNhJktICdLgTO3MR/2F5ecy3sA== X-Received: by 2002:a17:90a:7525:b0:221:77b4:46e1 with SMTP id q34-20020a17090a752500b0022177b446e1mr27427217pjk.48.1671481062108; Mon, 19 Dec 2022 12:17:42 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 12:17:41 -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 4/6] perf lock contention: Add -L/--lock-filter option Date: Mon, 19 Dec 2022 12:17:30 -0800 Message-Id: <20221219201732.460111-5-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" The -L/--lock-filter option is to filter only given locks. The locks can be specified by address or name (if exists). $ sudo ./perf lock record -a sleep 1 $ sudo ./perf lock con -l contended total wait max wait avg wait address sy= mbol 57 1.11 ms 42.83 us 19.54 us ffff9f4140059000 15 280.88 us 23.51 us 18.73 us ffffffff9d007a40 ji= ffies_lock 1 20.49 us 20.49 us 20.49 us ffffffff9d0d50c0 rc= u_state 1 9.02 us 9.02 us 9.02 us ffff9f41759e9ba0 $ sudo ./perf lock con -L jiffies_lock,rcu_state contended total wait max wait avg wait type caller 15 280.88 us 23.51 us 18.73 us spinlock tick_sch= ed_do_timer+0x93 1 20.49 us 20.49 us 20.49 us spinlock __softir= qentry_text_start+0xeb $ sudo ./perf lock con -L ffff9f4140059000 contended total wait max wait avg wait type caller 38 779.40 us 42.83 us 20.51 us spinlock worker_t= hread+0x50 11 216.30 us 39.87 us 19.66 us spinlock queue_wo= rk_on+0x39 8 118.13 us 20.51 us 14.77 us spinlock kthread+= 0xe5 Signed-off-by: Namhyung Kim --- tools/perf/Documentation/perf-lock.txt | 4 + tools/perf/builtin-lock.c | 140 +++++++++++++++++++++++-- tools/perf/util/lock-contention.h | 4 + 3 files changed, 142 insertions(+), 6 deletions(-) diff --git a/tools/perf/Documentation/perf-lock.txt b/tools/perf/Documentat= ion/perf-lock.txt index dea04ad5c28e..0f9f720e599d 100644 --- a/tools/perf/Documentation/perf-lock.txt +++ b/tools/perf/Documentation/perf-lock.txt @@ -183,6 +183,10 @@ CONTENTION OPTIONS Note that RW-variant of locks have :R and :W suffix. Names without the suffix are shortcuts for the both variants. Ex) rwsem =3D rwsem:R + rwse= m:W. =20 +-L:: +--lock-filter=3D:: + Show lock contention only for given lock addresses or names (comma separa= ted list). + =20 SEE ALSO -------- diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index e4e785d3b4ec..6b8ea2f0b90a 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -32,6 +32,7 @@ #include #include #include +#include =20 #include #include @@ -995,24 +996,52 @@ static int report_lock_contention_begin_event(struct = evsel *evsel, unsigned int flags =3D evsel__intval(evsel, sample, "flags"); u64 key; int i, ret; + static bool kmap_loaded; + struct machine *machine =3D &session->machines.host; + struct map *kmap; + struct symbol *sym; =20 ret =3D get_key_by_aggr_mode(&key, addr, evsel, sample); if (ret < 0) return ret; =20 + if (!kmap_loaded) { + unsigned long *addrs; + + /* make sure it loads the kernel map to find lock symbols */ + map__load(machine__kernel_map(machine)); + kmap_loaded =3D true; + + /* convert (kernel) symbols to addresses */ + for (i =3D 0; i < filters.nr_syms; i++) { + sym =3D machine__find_kernel_symbol_by_name(machine, + filters.syms[i], + &kmap); + if (sym =3D=3D NULL) { + pr_warning("ignore unknown symbol: %s\n", + filters.syms[i]); + continue; + } + + addrs =3D realloc(filters.addrs, + (filters.nr_addrs + 1) * sizeof(*addrs)); + if (addrs =3D=3D NULL) { + pr_warning("memory allocation failure\n"); + return -ENOMEM; + } + + addrs[filters.nr_addrs++] =3D kmap->unmap_ip(kmap, sym->start); + filters.addrs =3D addrs; + } + } + ls =3D lock_stat_find(key); if (!ls) { char buf[128]; const char *name =3D ""; - struct machine *machine =3D &session->machines.host; - struct map *kmap; - struct symbol *sym; =20 switch (aggr_mode) { case LOCK_AGGR_ADDR: - /* make sure it loads the kernel map to find lock symbols */ - map__load(machine__kernel_map(machine)); - sym =3D machine__find_kernel_symbol(machine, key, &kmap); if (sym) name =3D sym->name; @@ -1052,6 +1081,20 @@ static int report_lock_contention_begin_event(struct= evsel *evsel, return 0; } =20 + if (filters.nr_addrs) { + bool found =3D false; + + for (i =3D 0; i < filters.nr_addrs; i++) { + if (addr =3D=3D filters.addrs[i]) { + found =3D true; + break; + } + } + + if (!found) + return 0; + } + ts =3D thread_stat_findnew(sample->tid); if (!ts) return -ENOMEM; @@ -1496,6 +1539,15 @@ static void lock_filter_finish(void) { zfree(&filters.types); filters.nr_types =3D 0; + + zfree(&filters.addrs); + filters.nr_addrs =3D 0; + + for (int i =3D 0; i < filters.nr_syms; i++) + free(filters.syms[i]); + + zfree(&filters.syms); + filters.nr_syms =3D 0; } =20 static void sort_contention_result(void) @@ -1995,6 +2047,80 @@ static int parse_lock_type(const struct option *opt = __maybe_unused, const char * return ret; } =20 +static bool add_lock_addr(unsigned long addr) +{ + unsigned long *tmp; + + tmp =3D realloc(filters.addrs, (filters.nr_addrs + 1) * sizeof(*filters.a= ddrs)); + if (tmp =3D=3D NULL) { + pr_err("Memory allocation failure\n"); + return false; + } + + tmp[filters.nr_addrs++] =3D addr; + filters.addrs =3D tmp; + return true; +} + +static bool add_lock_sym(char *name) +{ + char **tmp; + char *sym =3D strdup(name); + + if (sym =3D=3D NULL) { + pr_err("Memory allocation failure\n"); + return false; + } + + tmp =3D realloc(filters.syms, (filters.nr_syms + 1) * sizeof(*filters.sym= s)); + if (tmp =3D=3D NULL) { + pr_err("Memory allocation failure\n"); + free(sym); + return false; + } + + tmp[filters.nr_syms++] =3D sym; + filters.syms =3D tmp; + return true; +} + +static int parse_lock_addr(const struct option *opt __maybe_unused, const = char *str, + int unset __maybe_unused) +{ + char *s, *tmp, *tok; + int ret =3D 0; + u64 addr; + + s =3D strdup(str); + if (s =3D=3D NULL) + return -1; + + for (tok =3D strtok_r(s, ", ", &tmp); tok; tok =3D strtok_r(NULL, ", ", &= tmp)) { + char *end; + + addr =3D strtoul(tok, &end, 16); + if (*end =3D=3D '\0') { + if (!add_lock_addr(addr)) { + ret =3D -1; + break; + } + continue; + } + + /* + * At this moment, we don't have kernel symbols. Save the symbols + * in a separate list and resolve them to addresses later. + */ + if (!add_lock_sym(tok)) { + ret =3D -1; + break; + } + } + + free(s); + return ret; +} + int cmd_lock(int argc, const char **argv) { const struct option lock_options[] =3D { @@ -2060,6 +2186,8 @@ int cmd_lock(int argc, const char **argv) OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by addre= ss"), OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS", "Filter specific type of locks", parse_lock_type), + OPT_CALLBACK('L', "lock-filter", NULL, "ADDRS/NAMES", + "Filter specific address/symbol of locks", parse_lock_addr), OPT_PARENT(lock_options) }; =20 diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-conte= ntion.h index dc621386a16b..b99e83fccf5c 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -7,7 +7,11 @@ =20 struct lock_filter { int nr_types; + int nr_addrs; + int nr_syms; unsigned int *types; + unsigned long *addrs; + char **syms; }; =20 struct lock_stat { --=20 2.39.0.314.g84b9a713c41-goog From nobody Sun Apr 12 20:03:07 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 5B4C6C4332F for ; Mon, 19 Dec 2022 20:18:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231539AbiLSUSJ (ORCPT ); Mon, 19 Dec 2022 15:18:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232473AbiLSURp (ORCPT ); Mon, 19 Dec 2022 15:17:45 -0500 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 593FB2BEB; Mon, 19 Dec 2022 12:17:44 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id u7so1953502plq.11; Mon, 19 Dec 2022 12:17:44 -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=rPDKYa8rD0VGPJQCF4t4qysKE20di3XOvK38s4BKO3o=; b=eu74UH9wGz7WdMFh5I8pM9WdmBiR0Gv/5jH4BeNEPO5Oh7kKrO5Zw9QorVrRLl3E7l oFgfj9hH2ZqKdQENWy9Bjknh7HEN4CvLiXJ+CQBl8JxTiah8rgaUafJJTsy6PWInyYmH QeMM6U7TxlcqfoJ0ZTbZ8fS2H1bHWyCNVkWt0J8myV2L/g5GzZyuidb8eKNmTaop6wN7 IprPWJ9430EJgQoBc5Vk1Dhqv//azBjgF1hF/yd1krmYuVyHGhKIiyUcEJBT6DcXRAv9 U1s/G1kalt1Xr0/8BUi8JBlYTXVJ+6rjpmFLnH1N25LvKC7MeD9rlswVqWYKVuW7eosn WKVQ== 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=rPDKYa8rD0VGPJQCF4t4qysKE20di3XOvK38s4BKO3o=; b=JLW8YZsPbHBYxKbI37XrGvpDDwsTOAuIFiKMPwP/n9UjFKNVuHKWF0zNymHlZcdIrW hBjF+KCtK0FL8GdDoJKwTRfLZ1tkUKNwed31/dK4tih+NIvFOiuhN659fp6JpzFZvSnv JFCdE0G8wI2mg5yXSKZ1n8geRbQYYa9DbdzF3s1P+TGMfQGyVbVuNJkLlweV7mme7YmM M+wcQoRrrg86Jg2JTAU8MWpfHHdC7YVIpgT/9NFc83U3NsBEPdtWq2MtzXej20bHR49F XC5/Ay3EwRwXmjsiYFQEa/n0JiwBaBcq1bqOaS03YiQLBmRJ1bBsJxPq6BuVGOBtDOiF BCBA== X-Gm-Message-State: ANoB5pkocWtEtaA3/WnUY70PtuyUxykZIrPy8hHGsLnov/upoMYSDdpE li4+pZHlfVYTwAKmaqvGk2MOtJut6oA= X-Google-Smtp-Source: AA0mqf5diJCT17BnJvoYPnuCPXbLNOWfhI+btTr1+tmXTumar6BFC9seq0Cix3EVbaz6BKBHk04sxA== X-Received: by 2002:a17:90a:6be6:b0:219:34cb:476c with SMTP id w93-20020a17090a6be600b0021934cb476cmr46122823pjj.9.1671481063752; Mon, 19 Dec 2022 12:17:43 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 12:17:43 -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 5/6] perf lock contention: Support lock addr/name filtering for BPF Date: Mon, 19 Dec 2022 12:17:31 -0800 Message-Id: <20221219201732.460111-6-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 addr_filter BPF hash map and check it with the lock address. $ sudo ./perf lock con -ab -L tasklist_lock -- ./perf bench sched messagi= ng # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups =3D=3D 400 processes run Total time: 0.169 [sec] contended total wait max wait avg wait type caller 18 174.09 us 25.31 us 9.67 us rwlock:W do_exit+= 0x36d 5 32.34 us 10.87 us 6.47 us rwlock:R do_wait+= 0x8b 4 15.41 us 4.73 us 3.85 us rwlock:W release_= task+0x6e Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_lock_contention.c | 42 ++++++++++++++++++- .../perf/util/bpf_skel/lock_contention.bpf.c | 17 ++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index b8590b82ad3d..4a95c0ebcb3c 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, ntypes =3D 1; + int ncpus =3D 1, ntasks =3D 1, ntypes =3D 1, naddrs =3D 1; struct evlist *evlist =3D con->evlist; struct target *target =3D con->target; =20 @@ -49,9 +49,39 @@ int lock_contention_prepare(struct lock_contention *con) if (con->filters->nr_types) ntypes =3D con->filters->nr_types; =20 + /* resolve lock name filters to addr */ + if (con->filters->nr_syms) { + struct symbol *sym; + struct map *kmap; + unsigned long *addrs; + + for (i =3D 0; i < con->filters->nr_syms; i++) { + sym =3D machine__find_kernel_symbol_by_name(con->machine, + con->filters->syms[i], + &kmap); + if (sym =3D=3D NULL) { + pr_warning("ignore unknown symbol: %s\n", + con->filters->syms[i]); + continue; + } + + addrs =3D realloc(con->filters->addrs, + (con->filters->nr_addrs + 1) * sizeof(*addrs)); + if (addrs =3D=3D NULL) { + pr_warning("memory allocation failure\n"); + continue; + } + + addrs[con->filters->nr_addrs++] =3D kmap->unmap_ip(kmap, sym->start); + con->filters->addrs =3D addrs; + } + naddrs =3D con->filters->nr_addrs; + } + 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); + bpf_map__set_max_entries(skel->maps.addr_filter, naddrs); =20 if (lock_contention_bpf__load(skel) < 0) { pr_err("Failed to load lock-contention BPF skeleton\n"); @@ -103,6 +133,16 @@ int lock_contention_prepare(struct lock_contention *co= n) bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY); } =20 + if (con->filters->nr_addrs) { + u8 val =3D 1; + + skel->bss->has_addr =3D 1; + fd =3D bpf_map__fd(skel->maps.addr_filter); + + for (i =3D 0; i < con->filters->nr_addrs; i++) + bpf_map_update_elem(fd, &con->filters->addrs[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 fb0128de7c00..ad0ca5d50557 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -69,11 +69,19 @@ struct { __uint(max_entries, 1); } type_filter SEC(".maps"); =20 +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__u64)); + __uint(value_size, sizeof(__u8)); + __uint(max_entries, 1); +} addr_filter SEC(".maps"); + /* control flags */ int enabled; int has_cpu; int has_task; int has_type; +int has_addr; int stack_skip; =20 /* determine the key of lock stat */ @@ -111,6 +119,15 @@ static inline int can_record(u64 *ctx) return 0; } =20 + if (has_addr) { + __u8 *ok; + __u64 addr =3D ctx[0]; + + ok =3D bpf_map_lookup_elem(&addr_filter, &addr); + if (!ok) + return 0; + } + return 1; } =20 --=20 2.39.0.314.g84b9a713c41-goog From nobody Sun Apr 12 20:03:07 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 D1D07C4332F for ; Mon, 19 Dec 2022 20:18:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231783AbiLSUSP (ORCPT ); Mon, 19 Dec 2022 15:18:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232502AbiLSURr (ORCPT ); Mon, 19 Dec 2022 15:17:47 -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 D3F0D64ED; Mon, 19 Dec 2022 12:17:45 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id v23so5007547pju.3; Mon, 19 Dec 2022 12:17:45 -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=E/YKT1JJIPuRoA7Hx7R8arjPSGFufHSp1KclETKmED4=; b=HVbC+Iu6DOauKYKAjXJ43ZOUiX8W5s0QydrESBi85TvL2VmyDCJR/2ymPdVYq/iDf/ IO6JdGU4dwlAukwN6rby4URhzT7owcP4jlDE7KILweEbgmvaWh+tGJlJzYD1tXTVYGSc LWNHMvXZrUg5P/xm50ZkS7+tQkGNaKPvYW9Zn2vDbBdFSpLQ4WgcFx3BMgkEqaEOeGis jWD0VFpNiPZQg5fT8EfCEAdwmkBJFsP4/GCJSNA9Dq1G7InW2qzihT9tyIqEZHighyhM DI/RyyArd/3wL6L81SseIQT4GhJscNZJGGQ+Ozj6E5HBWubdlQs2B2YNCaYU0m9UzYI/ /Epg== 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=E/YKT1JJIPuRoA7Hx7R8arjPSGFufHSp1KclETKmED4=; b=X7XX/EOCfQbmZeg0Dnr/Oq/qtZc2UMRxaFSQwqe3QkjSewv4gcNcx9/zhtqf4O6NCC 5TXdof5GJh+qAMzcnXAdXnbdMCXFzI63M86OmbMKiaOU5LzGmckqPWsj3/1E2wfoGTZ6 0EUWuA/YoVD4q5pEXVLsCSdpCzRCWb9i98jVpg4J/+0RSElAyc8A3C0RofvfH0+nh1Wr 1qymH484eP7hEVVs7qXQqCoJR2G/aWrC5F7VHQKE/Z2i0ULKMDgtsQh5EhNpmkLh12cW ABBCp6/CB9TQp85x5Wxxpqma6Ssp7XXc/x8lBJ9yO1/UY5D2+f53ETjF8IBXgvtz+8PC t+4Q== X-Gm-Message-State: ANoB5pmZd8DZ19gwS+zUqY1UYv8o9XrY/sIfcBefOtYazmpidil+Rptn Ma29RzfaflyQQaGyNm7EAZs= X-Google-Smtp-Source: AA0mqf61SP7/nZq9TfSNMMcb4bbYVKQVTkKEFue+8KZXn3Lu9cPuwaVOSuM5ZwGEwLEZzSwFMg1C/A== X-Received: by 2002:a17:90b:2d83:b0:219:e002:1ba3 with SMTP id sj3-20020a17090b2d8300b00219e0021ba3mr46689926pjb.9.1671481065326; Mon, 19 Dec 2022 12:17:45 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 12:17:44 -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 6/6] perf test: Update perf lock contention test Date: Mon, 19 Dec 2022 12:17:32 -0800 Message-Id: <20221219201732.460111-7-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" Add more tests for the new filters. $ sudo perf test contention -v 87: kernel lock contention analysis test : --- start --- test child forked, pid 412379 Testing perf lock record and perf lock contention Testing perf lock contention --use-bpf Testing perf lock record and perf lock contention at the same time Testing perf lock contention --threads Testing perf lock contention --lock-addr Testing perf lock contention --type-filter Testing perf lock contention --lock-filter test child finished with 0 ---- end ---- kernel lock contention analysis test: Ok Signed-off-by: Namhyung Kim --- tools/perf/tests/shell/lock_contention.sh | 58 ++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tools/perf/tests/shell/lock_contention.sh b/tools/perf/tests/s= hell/lock_contention.sh index cc9ceb9e19ca..b05f1b1ca6c8 100755 --- a/tools/perf/tests/shell/lock_contention.sh +++ b/tools/perf/tests/shell/lock_contention.sh @@ -115,7 +115,7 @@ test_aggr_addr() fi =20 # the perf lock contention output goes to the stderr - perf lock con -a -b -t -E 1 -q -- perf bench sched messaging > /dev/null = 2> ${result} + perf lock con -a -b -l -E 1 -q -- perf bench sched messaging > /dev/null = 2> ${result} if [ $(cat "${result}" | wc -l) !=3D "1" ]; then echo "[Fail] BPF result count is not 1:" $(cat "${result}" | wc -l) err=3D1 @@ -123,6 +123,60 @@ test_aggr_addr() fi } =20 +test_type_filter() +{ + echo "Testing perf lock contention --type-filter (w/ spinlock)" + perf lock contention -i ${perfdata} -Y spinlock -q 2> ${result} + if [ $(grep -c -v spinlock "${result}") !=3D "0" ]; then + echo "[Fail] Recorded should not have non-spinlocks:" $(cat "${result}") + err=3D1 + exit + fi + + if ! perf lock con -b true > /dev/null 2>&1 ; then + return + fi + + perf lock con -a -b -Y spinlock -q -- perf bench sched messaging > /dev/n= ull 2> ${result} + if [ $(grep -c -v spinlock "${result}") !=3D "0" ]; then + echo "[Fail] Recorded should not have non-spinlocks:" $(cat "${result}") + err=3D1 + exit + fi +} + +test_lock_filter() +{ + echo "Testing perf lock contention --lock-filter (w/ tasklist_lock)" + perf lock contention -i ${perfdata} -l -q 2> ${result} + if [ $(grep -c tasklist_lock "${result}") !=3D "1" ]; then + echo "[Skip] Could not find 'tasklist_lock'" + return + fi + + perf lock contention -i ${perfdata} -L tasklist_lock -q 2> ${result} + + # find out the type of tasklist_lock + local type=3D$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//= ') + + if [ $(grep -c -v "${type}" "${result}") !=3D "0" ]; then + echo "[Fail] Recorded should not have non-${type} locks:" $(cat "${resul= t}") + err=3D1 + exit + fi + + if ! perf lock con -b true > /dev/null 2>&1 ; then + return + fi + + perf lock con -a -b -L tasklist_lock -q -- perf bench sched messaging > /= dev/null 2> ${result} + if [ $(grep -c -v "${type}" "${result}") !=3D "0" ]; then + echo "[Fail] Recorded should not have non-${type} locks:" $(cat "${resul= t}") + err=3D1 + exit + fi +} + check =20 test_record @@ -130,5 +184,7 @@ test_bpf test_record_concurrent test_aggr_task test_aggr_addr +test_type_filter +test_lock_filter =20 exit ${err} --=20 2.39.0.314.g84b9a713c41-goog