From patchwork Mon Dec 19 20:17:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13077117 X-Patchwork-Delegate: bpf@iogearbox.net 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 59220C4167B for ; Mon, 19 Dec 2022 20:17:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232437AbiLSURl (ORCPT ); Mon, 19 Dec 2022 15:17:41 -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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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) } } -static const char *get_type_str(struct lock_stat *st) -{ - static const struct { - unsigned int flags; - const char *name; - } table[] = { - { 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[] = { + { 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" }, +}; - for (unsigned int i = 0; i < ARRAY_SIZE(table); i++) { - if (table[i].flags == st->flags) - return table[i].name; +static const char *get_type_str(unsigned int flags) +{ + for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) { + if (lock_type_table[i].flags == flags) + return lock_type_table[i].name; } return "unknown"; } @@ -1514,7 +1514,7 @@ static void print_contention_result(struct lock_contention *con) 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 = st->addr; From patchwork Mon Dec 19 20:17:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13077119 X-Patchwork-Delegate: bpf@iogearbox.net 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 A120CC4332F for ; Mon, 19 Dec 2022 20:17:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231391AbiLSURp (ORCPT ); Mon, 19 Dec 2022 15:17:45 -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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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_up_common_lock+0x62 13 787.16 us 105.44 us 60.55 us spinlock remove_wait_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_wake_up+0x1f5 83 226.38 us 9.15 us 2.73 us spinlock folio_lruvec_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/Documentation/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. -C:: ---cpu:: +--cpu=:: 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. -p:: ---pid=:: +--pid=:: Record events on existing process ID (comma separated list). ---tid=:: +--tid=:: Record events on existing thread ID (comma separated list). ---map-nr-entries:: +--map-nr-entries=:: Maximum number of BPF map entries (default: 10240). ---max-stack:: +--max-stack=:: Maximum stack depth when collecting lock contention (default: 8). ---stack-skip +--stack-skip=:: Number of stack depth to skip when finding a lock caller (default: 3). -E:: @@ -172,6 +172,17 @@ CONTENTION OPTIONS --lock-addr:: Show lock contention stat by address +-Y:: +--type-filter=:: + 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, pcpu-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 = rwsem:R + rwsem:W. + 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 = CONTENTION_STACK_DEPTH; static int stack_skip = CONTENTION_STACK_SKIP; static int print_nr_entries = INT_MAX / 2; +static struct lock_filter filters; + static enum lock_aggr_mode aggr_mode = LOCK_AGGR_ADDR; static struct thread_stat *thread_stat_find(u32 tid) @@ -990,8 +992,9 @@ static int report_lock_contention_begin_event(struct evsel *evsel, struct thread_stat *ts; struct lock_seq_stat *seq; u64 addr = evsel__intval(evsel, sample, "lock_addr"); + unsigned int flags = evsel__intval(evsel, sample, "flags"); u64 key; - int ret; + int i, ret; ret = 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 = ""; - unsigned int flags = evsel__intval(evsel, sample, "flags"); struct machine *machine = &session->machines.host; struct map *kmap; struct symbol *sym; @@ -1036,6 +1038,20 @@ static int report_lock_contention_begin_event(struct evsel *evsel, } } + if (filters.nr_types) { + bool found = false; + + for (i = 0; i < filters.nr_types; i++) { + if (flags == filters.types[i]) { + found = true; + break; + } + } + + if (!found) + return 0; + } + ts = 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" }, }; static const char *get_type_str(unsigned int flags) @@ -1465,6 +1483,21 @@ static const char *get_type_str(unsigned int flags) return "unknown"; } +static unsigned int get_type_flag(const char *str) +{ + for (unsigned int i = 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 = 0; +} + static void sort_contention_result(void) { sort_result(); @@ -1507,6 +1540,9 @@ static void print_contention_result(struct lock_contention *con) if (st->broken) bad++; + 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 **argv) print_contention_result(&con); 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; } +static bool add_lock_type(unsigned int flags) +{ + unsigned int *tmp; + + tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types)); + if (tmp == NULL) + return false; + + tmp[filters.nr_types++] = flags; + filters.types = 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 = 0; + + s = strdup(str); + if (s == NULL) + return -1; + + for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) { + unsigned int flags = get_type_flag(tok); + + if (flags == -1U) { + char buf[32]; + + if (strchr(tok, ':')) + continue; + + /* try :R and :W suffixes for rwlock, rwsem, ... */ + scnprintf(buf, sizeof(buf), "%s:R", tok); + flags = get_type_flag(buf); + if (flags != -1UL) { + if (!add_lock_type(flags)) { + ret = -1; + break; + } + } + + scnprintf(buf, sizeof(buf), "%s:W", tok); + flags = get_type_flag(buf); + if (flags != -1UL) { + if (!add_lock_type(flags)) { + ret = -1; + break; + } + } + continue; + } + + if (!add_lock_type(flags)) { + ret = -1; + break; + } + + if (!strcmp(tok, "mutex")) { + flags = get_type_flag("mutex-spin"); + if (flags != -1UL) { + if (!add_lock_type(flags)) { + ret = -1; + break; + } + } + } + } + + free(s); + return ret; +} + int cmd_lock(int argc, const char **argv) { const struct option lock_options[] = { @@ -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 functions"), OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by address"), + OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS", + "Filter specific type of locks", parse_lock_type), OPT_PARENT(lock_options) }; diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-contention.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 +struct lock_filter { + int nr_types; + unsigned int *types; +}; + struct lock_stat { struct hlist_node hash_entry; struct rb_node rb; /* used for sorting */ From patchwork Mon Dec 19 20:17:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13077118 X-Patchwork-Delegate: bpf@iogearbox.net 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 4BBE2C4708D for ; Mon, 19 Dec 2022 20:17:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232543AbiLSURo (ORCPT ); Mon, 19 Dec 2022 15:17:44 -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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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 == 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 **argv) .map_nr_entries = bpf_map_entries, .max_stack = max_stack_depth, .stack_skip = stack_skip, + .filters = &filters, }; session = perf_session__new(use_bpf ? NULL : &data, &eops); diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_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 = 1, ntasks = 1; + int ncpus = 1, ntasks = 1, ntypes = 1; struct evlist *evlist = con->evlist; struct target *target = con->target; @@ -46,9 +46,12 @@ int lock_contention_prepare(struct lock_contention *con) ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus); if (target__has_task(target)) ntasks = perf_thread_map__nr(evlist->core.threads); + if (con->filters->nr_types) + ntypes = con->filters->nr_types; 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); 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); } + if (con->filters->nr_types) { + u8 val = 1; + + skel->bss->has_type = 1; + fd = bpf_map__fd(skel->maps.type_filter); + + for (i = 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 = con->stack_skip; skel->bss->aggr_mode = con->aggr_mode; diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/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"); +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; /* determine the key of lock stat */ @@ -74,7 +82,7 @@ int aggr_mode; /* error stat */ int lost; -static inline int can_record(void) +static inline int can_record(u64 *ctx) { if (has_cpu) { __u32 cpu = bpf_get_smp_processor_id(); @@ -94,6 +102,15 @@ static inline int can_record(void) return 0; } + if (has_type) { + __u8 *ok; + __u32 flags = (__u32)ctx[1]; + + ok = bpf_map_lookup_elem(&type_filter, &flags); + if (!ok) + return 0; + } + return 1; } @@ -116,7 +133,7 @@ int contention_begin(u64 *ctx) __u32 pid; struct tstamp_data *pelem; - if (!enabled || !can_record()) + if (!enabled || !can_record(ctx)) return 0; pid = bpf_get_current_pid_tgid(); diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-contention.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; From patchwork Mon Dec 19 20:17:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13077120 X-Patchwork-Delegate: bpf@iogearbox.net 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 EA478C4167B for ; Mon, 19 Dec 2022 20:17:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232277AbiLSURv (ORCPT ); Mon, 19 Dec 2022 15:17:51 -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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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 symbol 57 1.11 ms 42.83 us 19.54 us ffff9f4140059000 15 280.88 us 23.51 us 18.73 us ffffffff9d007a40 jiffies_lock 1 20.49 us 20.49 us 20.49 us ffffffff9d0d50c0 rcu_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_sched_do_timer+0x93 1 20.49 us 20.49 us 20.49 us spinlock __softirqentry_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_thread+0x50 11 216.30 us 39.87 us 19.66 us spinlock queue_work_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/Documentation/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 = rwsem:R + rwsem:W. +-L:: +--lock-filter=:: + Show lock contention only for given lock addresses or names (comma separated list). + 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 #include #include @@ -995,24 +996,52 @@ static int report_lock_contention_begin_event(struct evsel *evsel, unsigned int flags = evsel__intval(evsel, sample, "flags"); u64 key; int i, ret; + static bool kmap_loaded; + struct machine *machine = &session->machines.host; + struct map *kmap; + struct symbol *sym; ret = get_key_by_aggr_mode(&key, addr, evsel, sample); if (ret < 0) return ret; + 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 = true; + + /* convert (kernel) symbols to addresses */ + for (i = 0; i < filters.nr_syms; i++) { + sym = machine__find_kernel_symbol_by_name(machine, + filters.syms[i], + &kmap); + if (sym == NULL) { + pr_warning("ignore unknown symbol: %s\n", + filters.syms[i]); + continue; + } + + addrs = realloc(filters.addrs, + (filters.nr_addrs + 1) * sizeof(*addrs)); + if (addrs == NULL) { + pr_warning("memory allocation failure\n"); + return -ENOMEM; + } + + addrs[filters.nr_addrs++] = kmap->unmap_ip(kmap, sym->start); + filters.addrs = addrs; + } + } + ls = lock_stat_find(key); if (!ls) { char buf[128]; const char *name = ""; - struct machine *machine = &session->machines.host; - struct map *kmap; - struct symbol *sym; 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 = machine__find_kernel_symbol(machine, key, &kmap); if (sym) name = sym->name; @@ -1052,6 +1081,20 @@ static int report_lock_contention_begin_event(struct evsel *evsel, return 0; } + if (filters.nr_addrs) { + bool found = false; + + for (i = 0; i < filters.nr_addrs; i++) { + if (addr == filters.addrs[i]) { + found = true; + break; + } + } + + if (!found) + return 0; + } + ts = 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 = 0; + + zfree(&filters.addrs); + filters.nr_addrs = 0; + + for (int i = 0; i < filters.nr_syms; i++) + free(filters.syms[i]); + + zfree(&filters.syms); + filters.nr_syms = 0; } 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; } +static bool add_lock_addr(unsigned long addr) +{ + unsigned long *tmp; + + tmp = realloc(filters.addrs, (filters.nr_addrs + 1) * sizeof(*filters.addrs)); + if (tmp == NULL) { + pr_err("Memory allocation failure\n"); + return false; + } + + tmp[filters.nr_addrs++] = addr; + filters.addrs = tmp; + return true; +} + +static bool add_lock_sym(char *name) +{ + char **tmp; + char *sym = strdup(name); + + if (sym == NULL) { + pr_err("Memory allocation failure\n"); + return false; + } + + tmp = realloc(filters.syms, (filters.nr_syms + 1) * sizeof(*filters.syms)); + if (tmp == NULL) { + pr_err("Memory allocation failure\n"); + free(sym); + return false; + } + + tmp[filters.nr_syms++] = sym; + filters.syms = 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 = 0; + u64 addr; + + s = strdup(str); + if (s == NULL) + return -1; + + for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) { + char *end; + + addr = strtoul(tok, &end, 16); + if (*end == '\0') { + if (!add_lock_addr(addr)) { + ret = -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 = -1; + break; + } + } + + free(s); + return ret; +} + int cmd_lock(int argc, const char **argv) { const struct option lock_options[] = { @@ -2060,6 +2186,8 @@ int cmd_lock(int argc, const char **argv) OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by address"), 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) }; diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-contention.h index dc621386a16b..b99e83fccf5c 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -7,7 +7,11 @@ struct lock_filter { int nr_types; + int nr_addrs; + int nr_syms; unsigned int *types; + unsigned long *addrs; + char **syms; }; struct lock_stat { From patchwork Mon Dec 19 20:17:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13077121 X-Patchwork-Delegate: bpf@iogearbox.net 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 4EE77C46467 for ; Mon, 19 Dec 2022 20:18:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232615AbiLSUR7 (ORCPT ); Mon, 19 Dec 2022 15:17:59 -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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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 messaging # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups == 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_lock_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 = 1, ntasks = 1, ntypes = 1; + int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1; struct evlist *evlist = con->evlist; struct target *target = con->target; @@ -49,9 +49,39 @@ int lock_contention_prepare(struct lock_contention *con) if (con->filters->nr_types) ntypes = con->filters->nr_types; + /* resolve lock name filters to addr */ + if (con->filters->nr_syms) { + struct symbol *sym; + struct map *kmap; + unsigned long *addrs; + + for (i = 0; i < con->filters->nr_syms; i++) { + sym = machine__find_kernel_symbol_by_name(con->machine, + con->filters->syms[i], + &kmap); + if (sym == NULL) { + pr_warning("ignore unknown symbol: %s\n", + con->filters->syms[i]); + continue; + } + + addrs = realloc(con->filters->addrs, + (con->filters->nr_addrs + 1) * sizeof(*addrs)); + if (addrs == NULL) { + pr_warning("memory allocation failure\n"); + continue; + } + + addrs[con->filters->nr_addrs++] = kmap->unmap_ip(kmap, sym->start); + con->filters->addrs = addrs; + } + naddrs = 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); 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 *con) bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY); } + if (con->filters->nr_addrs) { + u8 val = 1; + + skel->bss->has_addr = 1; + fd = bpf_map__fd(skel->maps.addr_filter); + + for (i = 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 = con->stack_skip; skel->bss->aggr_mode = con->aggr_mode; diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/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"); +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; /* determine the key of lock stat */ @@ -111,6 +119,15 @@ static inline int can_record(u64 *ctx) return 0; } + if (has_addr) { + __u8 *ok; + __u64 addr = ctx[0]; + + ok = bpf_map_lookup_elem(&addr_filter, &addr); + if (!ok) + return 0; + } + return 1; } From patchwork Mon Dec 19 20:17:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13077122 X-Patchwork-Delegate: bpf@iogearbox.net 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 791EDC4332F for ; Mon, 19 Dec 2022 20:18:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232552AbiLSUSA (ORCPT ); Mon, 19 Dec 2022 15:18:00 -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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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/shell/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 # 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) != "1" ]; then echo "[Fail] BPF result count is not 1:" $(cat "${result}" | wc -l) err=1 @@ -123,6 +123,60 @@ test_aggr_addr() fi } +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}") != "0" ]; then + echo "[Fail] Recorded should not have non-spinlocks:" $(cat "${result}") + err=1 + 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/null 2> ${result} + if [ $(grep -c -v spinlock "${result}") != "0" ]; then + echo "[Fail] Recorded should not have non-spinlocks:" $(cat "${result}") + err=1 + 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}") != "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=$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//') + + if [ $(grep -c -v "${type}" "${result}") != "0" ]; then + echo "[Fail] Recorded should not have non-${type} locks:" $(cat "${result}") + err=1 + 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}") != "0" ]; then + echo "[Fail] Recorded should not have non-${type} locks:" $(cat "${result}") + err=1 + exit + fi +} + check test_record @@ -130,5 +184,7 @@ test_bpf test_record_concurrent test_aggr_task test_aggr_addr +test_type_filter +test_lock_filter exit ${err}