From nobody Wed Apr 15 05:38:34 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 C6A6CC433EF for ; Mon, 25 Jul 2022 18:31:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235842AbiGYSbb (ORCPT ); Mon, 25 Jul 2022 14:31:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235512AbiGYSb3 (ORCPT ); Mon, 25 Jul 2022 14:31:29 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17FFA13D1F; Mon, 25 Jul 2022 11:31:29 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id i3so6736327pld.0; Mon, 25 Jul 2022 11:31:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WJYJFms8GJpoY0OsECigVFmxIO0a1eBqs63dgVV6oQI=; b=Savny8eH0HAoWNR41FqlGH3WJxiPWLvX9C4tSNIHowJEW1UOcqAQqQsAdONsh/XHyL AtSvsSMw08QdCJGQEgipQxgT0zhlZSQvTnU2X8xyPFcCQLMaXyzledr/qBlIvGA94XfP FYZo7a399KHpHPawyyfbGRbFgNoWB/rkesEY6FqXgwmEjBOdLCFm75umMMSMFISN5+i5 35VsLQagFpmpfLLCHslOmRJPY6FQtfss9Aayd1TlSzxGRFsnQe9YYB6EWDLrBxwuAdot 7ayfkzfjT8wDfczDXVM7QUGW08IIhkLB5RJbGyAyAR2y31vpMOa27/KA2THTsgGYRKvo FH8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=WJYJFms8GJpoY0OsECigVFmxIO0a1eBqs63dgVV6oQI=; b=bu4EwqM3qlg2U7zmyPnPE4OoxKm94uav7lJMks4fqlBH9WyZI10WsUnG6Z+W1XEOhA 0yJb89EpdwLFMCtg+AjMZwbKz181+B4e7WtOmF0JJxW0GikzBNpOz085lH4JtvccYkl8 iHH04vYQNUxhJ4geiDDTTqr8tpcF/SCa+A89VQ08fpakXaeZoWQ1IFrAzKlYAdA7wUtB RS/b6wDDJl3bIVu2242Gamyt/Ts+Uut0/Gk2p/1S/viHrQz7BxgzFDE2eHsLzC5sWU0v JEvKhU/MYZ47ye5YFyeDhlANw2+iOAxokPIngeEa6EROaiIZGbo9uM2agAF3iXjeTl5n ybjw== X-Gm-Message-State: AJIora+WWNb+ycunuKRTA3cua6ulbbJWubSDJorDKUUQUSNHi/Ee6UZu zdNFv9tcniZsLG0neCwPxAw= X-Google-Smtp-Source: AGRyM1uQ1MeT9G1WgldetqwfozEQ2/iwiKqZWawbw2zIlFP4oRgA8w1WSZOcGZLipgyg0jjQvaQtgg== X-Received: by 2002:a17:902:b70a:b0:16c:f62c:43aa with SMTP id d10-20020a170902b70a00b0016cf62c43aamr13464144pls.8.1658773888482; Mon, 25 Jul 2022 11:31:28 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:1010:2c4d:e9cd:d086:4d46]) by smtp.gmail.com with ESMTPSA id c17-20020a170903235100b0016be9d498d0sm9740556plh.211.2022.07.25.11.31.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 11:31:28 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Will Deacon , Waiman Long , Boqun Feng , Davidlohr Bueso Subject: [PATCH 1/5] perf lock: Add flags field in the lock_stat Date: Mon, 25 Jul 2022 11:31:20 -0700 Message-Id: <20220725183124.368304-2-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog In-Reply-To: <20220725183124.368304-1-namhyung@kernel.org> References: <20220725183124.368304-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" For lock contention tracepoint analysis, it needs to keep the flags. As nr_readlock and nr_trylock fields are not used for it, let's make it a union. Signed-off-by: Namhyung Kim --- tools/perf/builtin-lock.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 0aae88fdf93a..1de459198b99 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -59,7 +59,10 @@ struct lock_stat { unsigned int nr_contended; unsigned int nr_release; =20 - unsigned int nr_readlock; + union { + unsigned int nr_readlock; + unsigned int flags; + }; unsigned int nr_trylock; =20 /* these times are in nano sec. */ @@ -516,7 +519,7 @@ static struct lock_stat *lock_stat_find(u64 addr) return NULL; } =20 -static struct lock_stat *lock_stat_findnew(u64 addr, const char *name) +static struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int= flags) { struct hlist_head *entry =3D lockhashentry(addr); struct lock_stat *ret, *new; @@ -531,13 +534,13 @@ static struct lock_stat *lock_stat_findnew(u64 addr, = const char *name) goto alloc_failed; =20 new->addr =3D addr; - new->name =3D zalloc(sizeof(char) * strlen(name) + 1); + new->name =3D strdup(name); if (!new->name) { free(new); goto alloc_failed; } =20 - strcpy(new->name, name); + new->flags =3D flags; new->wait_time_min =3D ULLONG_MAX; =20 hlist_add_head(&new->hash_entry, entry); @@ -624,7 +627,7 @@ static int report_lock_acquire_event(struct evsel *evse= l, if (show_thread_stats) addr =3D sample->tid; =20 - ls =3D lock_stat_findnew(addr, name); + ls =3D lock_stat_findnew(addr, name, 0); if (!ls) return -ENOMEM; =20 @@ -696,7 +699,7 @@ static int report_lock_acquired_event(struct evsel *evs= el, if (show_thread_stats) addr =3D sample->tid; =20 - ls =3D lock_stat_findnew(addr, name); + ls =3D lock_stat_findnew(addr, name, 0); if (!ls) return -ENOMEM; =20 @@ -758,7 +761,7 @@ static int report_lock_contended_event(struct evsel *ev= sel, if (show_thread_stats) addr =3D sample->tid; =20 - ls =3D lock_stat_findnew(addr, name); + ls =3D lock_stat_findnew(addr, name, 0); if (!ls) return -ENOMEM; =20 @@ -813,7 +816,7 @@ static int report_lock_release_event(struct evsel *evse= l, if (show_thread_stats) addr =3D sample->tid; =20 - ls =3D lock_stat_findnew(addr, name); + ls =3D lock_stat_findnew(addr, name, 0); if (!ls) return -ENOMEM; =20 @@ -985,11 +988,12 @@ static int report_lock_contention_begin_event(struct = evsel *evsel, if (!ls) { char buf[128]; const char *caller =3D buf; + unsigned int flags =3D evsel__intval(evsel, sample, "flags"); =20 if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0) caller =3D "Unknown"; =20 - ls =3D lock_stat_findnew(addr, caller); + ls =3D lock_stat_findnew(addr, caller, flags); if (!ls) return -ENOMEM; } --=20 2.37.1.359.gd136c6c3e2-goog From nobody Wed Apr 15 05:38:34 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 30E01C433EF for ; Mon, 25 Jul 2022 18:31:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236027AbiGYSbi (ORCPT ); Mon, 25 Jul 2022 14:31:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235876AbiGYSbb (ORCPT ); Mon, 25 Jul 2022 14:31:31 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8157913D1F; Mon, 25 Jul 2022 11:31:30 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id b9so11191070pfp.10; Mon, 25 Jul 2022 11:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=D1DcUEHBVE6rdnLWJkIE3FghXMPvrALLztQYWoRsMVg=; b=N8TRWbLD6wRwFzdOqbuRTyG3kxj1iTJd38OG59x/uR/iTh9LrCT/40EVXDlrcqLJI5 j4Y5DbwDDXbLlGpXeDB8Ie9AYzIt1HgHPvgAu2XxUUqLmqjYmsmsQdR/mUNhHRYE3Rj4 DwRftEGjWxU4ryQxe1GBC+S7IZ2w4jagihKeQN0DRW3OE4IPUgRG6uooG7f6tx50ormM sE9ooRP6nzgLuVmvr6WvuWG14i67CzwO9PLC4YdeaOUVVBkIRIMvw2/9mDy/cwWNuhLT WfUvuk88woJOVNF6b8mF5rjMKUZsIC4YACDAcca1e1bfGR+3kSJwXGwPEhcPuedKjUBO AnjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=D1DcUEHBVE6rdnLWJkIE3FghXMPvrALLztQYWoRsMVg=; b=VmE9M82cWk3Key7cU5CoAuCSQ8I4lQuEmXYEEZpGlMGcSbpbpsD+DL3LJ0sO4ZcYVV 4eiE3ogp1ZlCC+RFXXS+pspv30mLvd2+XzWN0UjOMnYrDKoLT96cjPW4Qwa1oOUQ9Ljx OcoA3IYID2AGeXILSTadpdewkKm5ofu0agQa6oTJMJVqhpviZduOVLW+MhsoMITtSLCA xw98Bkkcco2LQhvgeJEcvqOv8QdIUTES6A+Ptdi7EQgH6j0D8q40p2gAKCfkkkbvDzU2 5v68O9NRnR7NKjh1zcT8Zd0dgy/FrgfaSkgREyDb9Mn4wv5KNQtUcxZJdg6WrEgIEzor HE0A== X-Gm-Message-State: AJIora+EtwaVDIBDHbDk5SR/X56ewHDI8PsK9QA1RodFQbnlFWzqQnp7 gYLA36m255bfiKgNPRWKYLE= X-Google-Smtp-Source: AGRyM1uE3AMaS6p9c/1QuEVy1Img7bPKdHFPd1jlHho4x4t63Ntl0QFf/VfZ6rOHd+9BjKfV0YSWEA== X-Received: by 2002:a05:6a00:cd5:b0:52b:1744:af86 with SMTP id b21-20020a056a000cd500b0052b1744af86mr13957094pfv.19.1658773889923; Mon, 25 Jul 2022 11:31:29 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:1010:2c4d:e9cd:d086:4d46]) by smtp.gmail.com with ESMTPSA id c17-20020a170903235100b0016be9d498d0sm9740556plh.211.2022.07.25.11.31.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 11:31:29 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Will Deacon , Waiman Long , Boqun Feng , Davidlohr Bueso Subject: [PATCH 2/5] perf lock: Add lock aggregation enum Date: Mon, 25 Jul 2022 11:31:21 -0700 Message-Id: <20220725183124.368304-3-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog In-Reply-To: <20220725183124.368304-1-namhyung@kernel.org> References: <20220725183124.368304-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" Introduce the aggr_mode variable to prepare the later code change. The default is LOCK_AGGR_ADDR which aggregate the result for the lock instances. When -t/--threads option is given, it'd be set to LOCK_AGGR_TASK. The LOCK_AGGR_CALLER is for the contention analysis and it'd aggregate the stat by comparing the callstacks. Signed-off-by: Namhyung Kim --- tools/perf/builtin-lock.c | 114 +++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 20 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 1de459198b99..b4e7a6e5c54e 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -126,6 +126,12 @@ static struct rb_root thread_stats; static bool combine_locks; static bool show_thread_stats; =20 +static enum { + LOCK_AGGR_ADDR, + LOCK_AGGR_TASK, + LOCK_AGGR_CALLER, +} aggr_mode =3D LOCK_AGGR_ADDR; + /* * CONTENTION_STACK_DEPTH * Number of stack trace entries to find callers @@ -622,12 +628,22 @@ static int report_lock_acquire_event(struct evsel *ev= sel, const char *name =3D evsel__strval(evsel, sample, "name"); u64 addr =3D evsel__intval(evsel, sample, "lockdep_addr"); int flag =3D evsel__intval(evsel, sample, "flags"); + u64 key; =20 - /* abuse ls->addr for tid */ - if (show_thread_stats) - addr =3D sample->tid; + switch (aggr_mode) { + case LOCK_AGGR_ADDR: + key =3D addr; + break; + case LOCK_AGGR_TASK: + key =3D sample->tid; + break; + case LOCK_AGGR_CALLER: + default: + pr_err("Invalid aggregation mode: %d\n", aggr_mode); + return -EINVAL; + } =20 - ls =3D lock_stat_findnew(addr, name, 0); + ls =3D lock_stat_findnew(key, name, 0); if (!ls) return -ENOMEM; =20 @@ -695,11 +711,22 @@ static int report_lock_acquired_event(struct evsel *e= vsel, u64 contended_term; const char *name =3D evsel__strval(evsel, sample, "name"); u64 addr =3D evsel__intval(evsel, sample, "lockdep_addr"); + u64 key; =20 - if (show_thread_stats) - addr =3D sample->tid; + switch (aggr_mode) { + case LOCK_AGGR_ADDR: + key =3D addr; + break; + case LOCK_AGGR_TASK: + key =3D sample->tid; + break; + case LOCK_AGGR_CALLER: + default: + pr_err("Invalid aggregation mode: %d\n", aggr_mode); + return -EINVAL; + } =20 - ls =3D lock_stat_findnew(addr, name, 0); + ls =3D lock_stat_findnew(key, name, 0); if (!ls) return -ENOMEM; =20 @@ -757,11 +784,22 @@ static int report_lock_contended_event(struct evsel *= evsel, struct lock_seq_stat *seq; const char *name =3D evsel__strval(evsel, sample, "name"); u64 addr =3D evsel__intval(evsel, sample, "lockdep_addr"); + u64 key; =20 - if (show_thread_stats) - addr =3D sample->tid; + switch (aggr_mode) { + case LOCK_AGGR_ADDR: + key =3D addr; + break; + case LOCK_AGGR_TASK: + key =3D sample->tid; + break; + case LOCK_AGGR_CALLER: + default: + pr_err("Invalid aggregation mode: %d\n", aggr_mode); + return -EINVAL; + } =20 - ls =3D lock_stat_findnew(addr, name, 0); + ls =3D lock_stat_findnew(key, name, 0); if (!ls) return -ENOMEM; =20 @@ -812,11 +850,22 @@ static int report_lock_release_event(struct evsel *ev= sel, struct lock_seq_stat *seq; const char *name =3D evsel__strval(evsel, sample, "name"); u64 addr =3D evsel__intval(evsel, sample, "lockdep_addr"); + u64 key; =20 - if (show_thread_stats) - addr =3D sample->tid; + switch (aggr_mode) { + case LOCK_AGGR_ADDR: + key =3D addr; + break; + case LOCK_AGGR_TASK: + key =3D sample->tid; + break; + case LOCK_AGGR_CALLER: + default: + pr_err("Invalid aggregation mode: %d\n", aggr_mode); + return -EINVAL; + } =20 - ls =3D lock_stat_findnew(addr, name, 0); + ls =3D lock_stat_findnew(key, name, 0); if (!ls) return -ENOMEM; =20 @@ -980,11 +1029,22 @@ static int report_lock_contention_begin_event(struct= evsel *evsel, struct thread_stat *ts; struct lock_seq_stat *seq; u64 addr =3D evsel__intval(evsel, sample, "lock_addr"); + u64 key; =20 - if (show_thread_stats) - addr =3D sample->tid; + switch (aggr_mode) { + case LOCK_AGGR_ADDR: + key =3D addr; + break; + case LOCK_AGGR_TASK: + key =3D sample->tid; + break; + case LOCK_AGGR_CALLER: + default: + pr_err("Invalid aggregation mode: %d\n", aggr_mode); + return -EINVAL; + } =20 - ls =3D lock_stat_find(addr); + ls =3D lock_stat_find(key); if (!ls) { char buf[128]; const char *caller =3D buf; @@ -993,7 +1053,7 @@ static int report_lock_contention_begin_event(struct e= vsel *evsel, if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0) caller =3D "Unknown"; =20 - ls =3D lock_stat_findnew(addr, caller, flags); + ls =3D lock_stat_findnew(key, caller, flags); if (!ls) return -ENOMEM; } @@ -1050,11 +1110,22 @@ static int report_lock_contention_end_event(struct = evsel *evsel, struct lock_seq_stat *seq; u64 contended_term; u64 addr =3D evsel__intval(evsel, sample, "lock_addr"); + u64 key; =20 - if (show_thread_stats) - addr =3D sample->tid; + switch (aggr_mode) { + case LOCK_AGGR_ADDR: + key =3D addr; + break; + case LOCK_AGGR_TASK: + key =3D sample->tid; + break; + case LOCK_AGGR_CALLER: + default: + pr_err("Invalid aggregation mode: %d\n", aggr_mode); + return -EINVAL; + } =20 - ls =3D lock_stat_find(addr); + ls =3D lock_stat_find(key); if (!ls) return 0; =20 @@ -1416,6 +1487,9 @@ static int __cmd_report(bool display_info) if (select_key()) goto out_delete; =20 + if (show_thread_stats) + aggr_mode =3D LOCK_AGGR_TASK; + err =3D perf_session__process_events(session); if (err) goto out_delete; --=20 2.37.1.359.gd136c6c3e2-goog From nobody Wed Apr 15 05:38:34 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 46B07CCA473 for ; Mon, 25 Jul 2022 18:31:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236329AbiGYSbm (ORCPT ); Mon, 25 Jul 2022 14:31:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235512AbiGYSbf (ORCPT ); Mon, 25 Jul 2022 14:31:35 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C8AD13F4B; Mon, 25 Jul 2022 11:31:32 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id g12so11214997pfb.3; Mon, 25 Jul 2022 11:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sswaviHsJHwIPu6wABFq54k6OlpMYZVGoIdbjO5HKfc=; b=AW+mTe6B4JzObgYQa1hOgJmgfqETRDnwBcbgmP81GuXtdyMGeuiar81edRVvTNfWre QXJt3dLqY7jkP3lRr4DMcxslL9pQ8HvSsKsK37P0saU5KDMCUgAhMXyDXlQcz4ZMWAFZ bFP+qVHoxAN7u5iqzJrKC/JWIXT8nagPnW2JHHkOrFOwyAaJVGMJNHvRhSPnlKDxe6zm y3qF31c6K7vWVpVDZE/s+6BpclP4k744icsAeaGWx9v6qW42D3Q31vlwUPnBdIHz/5Gh Zci6W73/aHUK+8ORaUXASOSYZJMVSjyaGr1euXYcV9NGz/UQPF/vTdP/HJkoLc98O7uo vDZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=sswaviHsJHwIPu6wABFq54k6OlpMYZVGoIdbjO5HKfc=; b=SIH/2TAMco56O1TwmQlQ8rcR51pCOWRqSJNTnznF1/KKgFuNotSsKHO7bT0ym6BvhN 9csqKB2zPIv0DUjs9KzFpG1kpXkmufPkaiNZ/n+XeFYiH/Iuc271+t+mFjVJTxWgWGtg XmIct/Zy7eMZUjiopnVYGpWuvIXrx0KHsgbYuKFUPAF5Eyxq2sQ+C9P4mxfwaSVh0OgO Z5lDTx3WHdcWlRdteTM8/Wfjm+54Sqh7CLeSXk9epk79yPt4uhdI7PQTR8HC0EJNEiFs enVVJrLC4AYaKMCA+F+33svEmrc3Qy2+0wuZSrQtZAjysEKpi3A3/hL3NRJozJdqUo9M lMOQ== X-Gm-Message-State: AJIora+w0dZ2Xgk60o8yHhFeiCsqAYyVBnCbkG1hd1FF3d2rkB+XFSqS ERlAqyh2YK5CmMeGwZctclI= X-Google-Smtp-Source: AGRyM1sJTV4ownLdzuVSTgPBQOZ4SS0xIEEqwwDyJGS+Kt3DFmffqg/BGcF+20F4jsQSt/X611QmOQ== X-Received: by 2002:a05:6a00:852:b0:528:c669:ad65 with SMTP id q18-20020a056a00085200b00528c669ad65mr13762491pfk.75.1658773891502; Mon, 25 Jul 2022 11:31:31 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:1010:2c4d:e9cd:d086:4d46]) by smtp.gmail.com with ESMTPSA id c17-20020a170903235100b0016be9d498d0sm9740556plh.211.2022.07.25.11.31.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 11:31:31 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Will Deacon , Waiman Long , Boqun Feng , Davidlohr Bueso Subject: [PATCH 3/5] perf lock: Add 'contention' subcommand Date: Mon, 25 Jul 2022 11:31:22 -0700 Message-Id: <20220725183124.368304-4-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog In-Reply-To: <20220725183124.368304-1-namhyung@kernel.org> References: <20220725183124.368304-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 'perf lock contention' processes the lock contention events and displays the result like perf lock report. Right now, there's not much difference between the two but the lock contention specific features will come soon. $ perf lock contention contended total wait max wait avg wait type caller 238 1.41 ms 29.20 us 5.94 us spinlock update_b= locked_averages+0x4c 1 902.08 us 902.08 us 902.08 us rwsem:R do_user_= addr_fault+0x1dd 81 330.30 us 17.24 us 4.08 us spinlock _nohz_id= le_balance+0x172 2 89.54 us 61.26 us 44.77 us spinlock do_anony= mous_page+0x16d 24 78.36 us 12.27 us 3.27 us mutex pipe_rea= d+0x56 2 71.58 us 59.56 us 35.79 us spinlock __handle= _mm_fault+0x6aa 6 25.68 us 6.89 us 4.28 us spinlock do_idle+= 0x28d 1 18.46 us 18.46 us 18.46 us rtmutex exec_fw_= cmd+0x21b 3 15.25 us 6.26 us 5.08 us spinlock tick_do_= update_jiffies64+0x2c Signed-off-by: Namhyung Kim --- tools/perf/Documentation/perf-lock.txt | 4 +- tools/perf/builtin-lock.c | 212 ++++++++++++++++++++++++- 2 files changed, 214 insertions(+), 2 deletions(-) diff --git a/tools/perf/Documentation/perf-lock.txt b/tools/perf/Documentat= ion/perf-lock.txt index 4b8568f0c53b..da0bcbc676a3 100644 --- a/tools/perf/Documentation/perf-lock.txt +++ b/tools/perf/Documentation/perf-lock.txt @@ -8,7 +8,7 @@ perf-lock - Analyze lock events SYNOPSIS -------- [verse] -'perf lock' {record|report|script|info} +'perf lock' {record|report|script|info|contention} =20 DESCRIPTION ----------- @@ -27,6 +27,8 @@ and statistics with this 'perf lock' command. 'perf lock info' shows metadata like threads or addresses of lock instances. =20 + 'perf lock contention' shows contention statistics. + COMMON OPTIONS -------------- =20 diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index b4e7a6e5c54e..64f635273660 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -145,6 +145,18 @@ static enum { */ #define CONTENTION_STACK_SKIP 3 =20 +/* + * flags for lock:contention_begin + * Imported from include/trace/events/lock.h. + */ +#define LCB_F_SPIN (1U << 0) +#define LCB_F_READ (1U << 1) +#define LCB_F_WRITE (1U << 2) +#define LCB_F_RT (1U << 3) +#define LCB_F_PERCPU (1U << 4) +#define LCB_F_MUTEX (1U << 5) + + static u64 sched_text_start; static u64 sched_text_end; static u64 lock_text_start; @@ -1022,6 +1034,51 @@ static int lock_contention_caller(struct evsel *evse= l, struct perf_sample *sampl return -1; } =20 +static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample) +{ + struct callchain_cursor *cursor =3D &callchain_cursor; + struct thread *thread; + u64 hash =3D 0; + int skip =3D 0; + int ret; + + thread =3D machine__findnew_thread(&session->machines.host, + -1, sample->pid); + if (thread =3D=3D NULL) + return -1; + + /* use caller function name from the callchain */ + ret =3D thread__resolve_callchain(thread, cursor, evsel, sample, + NULL, NULL, CONTENTION_STACK_DEPTH); + thread__put(thread); + + if (ret !=3D 0) + return -1; + + callchain_cursor_commit(cursor); + + while (true) { + struct callchain_cursor_node *node; + + node =3D callchain_cursor_current(cursor); + if (node =3D=3D NULL) + break; + + /* skip first few entries - for lock functions */ + if (++skip <=3D CONTENTION_STACK_SKIP) + goto next; + + if (node->ms.sym && is_lock_function(node->ip)) + goto next; + + hash ^=3D hash_long((unsigned long)node->ip, 64); + +next: + callchain_cursor_advance(cursor); + } + return hash; +} + static int report_lock_contention_begin_event(struct evsel *evsel, struct perf_sample *sample) { @@ -1039,6 +1096,8 @@ static int report_lock_contention_begin_event(struct = evsel *evsel, key =3D sample->tid; break; case LOCK_AGGR_CALLER: + key =3D callchain_id(evsel, sample); + break; default: pr_err("Invalid aggregation mode: %d\n", aggr_mode); return -EINVAL; @@ -1120,6 +1179,8 @@ static int report_lock_contention_end_event(struct ev= sel *evsel, key =3D sample->tid; break; case LOCK_AGGR_CALLER: + key =3D callchain_id(evsel, sample); + break; default: pr_err("Invalid aggregation mode: %d\n", aggr_mode); return -EINVAL; @@ -1183,6 +1244,12 @@ static struct trace_lock_handler report_lock_ops = =3D { .contention_end_event =3D report_lock_contention_end_event, }; =20 +static struct trace_lock_handler contention_lock_ops =3D { + .contention_begin_event =3D report_lock_contention_begin_event, + .contention_end_event =3D report_lock_contention_end_event, +}; + + static struct trace_lock_handler *trace_handler; =20 static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sa= mple *sample) @@ -1428,6 +1495,67 @@ 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" }, + }; + + for (unsigned int i =3D 0; i < ARRAY_SIZE(table); i++) { + if (table[i].flags =3D=3D st->flags) + return table[i].name; + } + return "unknown"; +} + +static void sort_contention_result(void) +{ + sort_result(); +} + +static void print_contention_result(void) +{ + struct lock_stat *st; + struct lock_key *key; + int bad, total; + + list_for_each_entry(key, &lock_keys, list) + pr_info("%*s ", key->len, key->header); + + pr_info(" %10s %s\n\n", "type", "caller"); + + bad =3D total =3D 0; + while ((st =3D pop_from_result())) { + total++; + if (st->broken) + bad++; + + list_for_each_entry(key, &lock_keys, list) { + key->print(key, st); + pr_info(" "); + } + + pr_info(" %10s %s\n", get_type_str(st), st->name); + } + + print_bad_events(bad, total); +} + static const struct evsel_str_handler lock_tracepoints[] =3D { { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKD= EP */ { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCK= DEP, CONFIG_LOCK_STAT */ @@ -1508,6 +1636,68 @@ static int __cmd_report(bool display_info) return err; } =20 +static int __cmd_contention(void) +{ + int err =3D -EINVAL; + struct perf_tool eops =3D { + .sample =3D process_sample_event, + .comm =3D perf_event__process_comm, + .mmap =3D perf_event__process_mmap, + .ordered_events =3D true, + }; + struct perf_data data =3D { + .path =3D input_name, + .mode =3D PERF_DATA_MODE_READ, + .force =3D force, + }; + + session =3D perf_session__new(&data, &eops); + if (IS_ERR(session)) { + pr_err("Initializing perf session failed\n"); + return PTR_ERR(session); + } + + /* for lock function check */ + symbol_conf.sort_by_name =3D true; + symbol__init(&session->header.env); + + if (!perf_session__has_traces(session, "lock record")) + goto out_delete; + + if (!evlist__find_evsel_by_str(session->evlist, "lock:contention_begin"))= { + pr_err("lock contention evsel not found\n"); + goto out_delete; + } + + if (perf_session__set_tracepoints_handlers(session, contention_tracepoint= s)) { + pr_err("Initializing perf session tracepoint handlers failed\n"); + goto out_delete; + } + + if (setup_output_field("contended,wait_total,wait_max,avg_wait")) + goto out_delete; + + sort_key =3D "wait_total"; + if (select_key()) + goto out_delete; + + aggr_mode =3D LOCK_AGGR_CALLER; + + err =3D perf_session__process_events(session); + if (err) + goto out_delete; + + setup_pager(); + + sort_contention_result(); + print_contention_result(); + +out_delete: + perf_session__delete(session); + return err; +} + + static int __cmd_record(int argc, const char **argv) { const char *record_args[] =3D { @@ -1626,12 +1816,17 @@ int cmd_lock(int argc, const char **argv) OPT_PARENT(lock_options) }; =20 + const struct option contention_options[] =3D { + OPT_PARENT(lock_options) + }; + const char * const info_usage[] =3D { "perf lock info []", NULL }; const char *const lock_subcommands[] =3D { "record", "report", "script", - "info", NULL }; + "info", "contention", + "contention", NULL }; const char *lock_usage[] =3D { NULL, NULL @@ -1640,6 +1835,10 @@ int cmd_lock(int argc, const char **argv) "perf lock report []", NULL }; + const char * const contention_usage[] =3D { + "perf lock contention []", + NULL + }; unsigned int i; int rc =3D 0; =20 @@ -1675,6 +1874,17 @@ int cmd_lock(int argc, const char **argv) /* recycling report_lock_ops */ trace_handler =3D &report_lock_ops; rc =3D __cmd_report(true); + } else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) { + trace_handler =3D &contention_lock_ops; + if (argc) { + argc =3D parse_options(argc, argv, contention_options, + contention_usage, 0); + if (argc) { + usage_with_options(contention_usage, + contention_options); + } + } + rc =3D __cmd_contention(); } else { usage_with_options(lock_usage, lock_options); } --=20 2.37.1.359.gd136c6c3e2-goog From nobody Wed Apr 15 05:38:34 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 C3374C43334 for ; Mon, 25 Jul 2022 18:31:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236411AbiGYSbp (ORCPT ); Mon, 25 Jul 2022 14:31:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235932AbiGYSbg (ORCPT ); Mon, 25 Jul 2022 14:31:36 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81CB31EC6E; Mon, 25 Jul 2022 11:31:33 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id c139so11212316pfc.2; Mon, 25 Jul 2022 11:31:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=E+lF/hOopeOfDOTh1RxazXCTh2KRGY7tiZ3ES3jDcAY=; b=Dn8f7wMfuuhmBXzlrBKREwx7Xi0vAm7iK5YjmHqiDZigMl8I4OZ4Hpsbjoz5fJ2EKA EsMl6iLlxyvumZhdS5M5/x3ipgZ1YXkVGB1CKaE+6KeVF2jJ7B1ukqekbgzQGijz2IBA ZH7KBCJkYT3ocxdc48rjDSFLvKnjFfxQBCu/JXpaKZ+KK5P06QXg/YJACAKkLDOUhePp MWthO8ZTIR1WULxUBEGf5yXa4LOlQghVH0CX4j5Q6ZIObBa/8paLaHoCCYRPZRTwT2FC F/JD4n2c31UagrJn4F26ruZofCbdHkYPQsSqVVhUyQUFGN7ZbNehkpePLn1BWDZtA2U7 XzPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=E+lF/hOopeOfDOTh1RxazXCTh2KRGY7tiZ3ES3jDcAY=; b=in21Y7Rg/8FAZWPw7mQGtnk+ayAa7wjAXOXBso9mHcZGNWOmpKNcPceMgb2JVjh/ev SQHj3bptfRCOt33B3N/9FwlfUNthEFx2HL02BKUo4sQOqZwYXglyliuy2o2kfrH8CSa1 HGR9ADsDzHzdNF1oI40E+IkCKwsQ9VPUvjdyB3NzXzSioXTOjoUKfsllRcf80KK9dXOJ E0MzKDGV9LKowscyLBjyaO+1TeLhY5MFdZkyEhO/o6d/+XT+VNAUzaFHVyWDVB0qva8Z eDQ2kjMggQAJ9rhjLEulc56VRr4/Nd0cwTSEWShwH4VzdXDabKnuXSu5CNgAEnu5jsYj JS4Q== X-Gm-Message-State: AJIora9ip+kAuQmnr4kMyVpLKy5td2Kh0rM50PmtS9r0sj1EIQMI4UtJ CkqK9MM4rKNHLvtK1O6BN0k= X-Google-Smtp-Source: AGRyM1taaBfKebI4jenowe1nDB10rEo9WBVYv9+nX8yN6Bd7NssSzOBgJELzFOAvBmFoDOwuhu4bmg== X-Received: by 2002:a05:6a00:aca:b0:52b:bd66:8c94 with SMTP id c10-20020a056a000aca00b0052bbd668c94mr14223601pfl.3.1658773892920; Mon, 25 Jul 2022 11:31:32 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:1010:2c4d:e9cd:d086:4d46]) by smtp.gmail.com with ESMTPSA id c17-20020a170903235100b0016be9d498d0sm9740556plh.211.2022.07.25.11.31.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 11:31:32 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Will Deacon , Waiman Long , Boqun Feng , Davidlohr Bueso Subject: [PATCH 4/5] perf lock: Add -k and -F options to 'contention' subcommand Date: Mon, 25 Jul 2022 11:31:23 -0700 Message-Id: <20220725183124.368304-5-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog In-Reply-To: <20220725183124.368304-1-namhyung@kernel.org> References: <20220725183124.368304-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" Like perf lock report, add -k/--key and -F/--field options to control output formatting and sorting. Note that it has slightly different default options as some fields are not available and to optimize the screen space. Signed-off-by: Namhyung Kim --- tools/perf/Documentation/perf-lock.txt | 15 ++++++++ tools/perf/builtin-lock.c | 51 ++++++++++++++++++++------ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/tools/perf/Documentation/perf-lock.txt b/tools/perf/Documentat= ion/perf-lock.txt index da0bcbc676a3..ae78e04346bd 100644 --- a/tools/perf/Documentation/perf-lock.txt +++ b/tools/perf/Documentation/perf-lock.txt @@ -105,6 +105,21 @@ INFO OPTIONS --map:: dump map of lock instances (address:name table) =20 +CONTENTION OPTIONS +-------------- + +-k:: +--key=3D:: + Sorting key. Possible values: contended, wait_total (default), + wait_max, wait_min, avg_wait. + +-F:: +--field=3D:: + Output fields. By default it shows all but the wait_min fields + and users can customize that using this. Possible values: + contended, wait_total, wait_max, wait_min, avg_wait. + + SEE ALSO -------- linkperf:perf[1] diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 64f635273660..ad3b191db2e7 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -362,7 +362,7 @@ static const char *output_fields; =20 #define DEF_KEY_LOCK(name, header, fn_suffix, len) \ { #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ #= # fn_suffix, {} } -struct lock_key keys[] =3D { +static struct lock_key report_keys[] =3D { DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10), DEF_KEY_LOCK(contended, "contended", nr_contended, 10), DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12), @@ -374,9 +374,24 @@ struct lock_key keys[] =3D { { } }; =20 -static int select_key(void) +static struct lock_key contention_keys[] =3D { + DEF_KEY_LOCK(contended, "contended", nr_contended, 10), + DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12), + DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12), + DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12), + DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12), + + /* extra comparisons much complicated should be here */ + { } +}; + +static int select_key(bool contention) { int i; + struct lock_key *keys =3D report_keys; + + if (contention) + keys =3D contention_keys; =20 for (i =3D 0; keys[i].name; i++) { if (!strcmp(keys[i].name, sort_key)) { @@ -394,9 +409,13 @@ static int select_key(void) return -1; } =20 -static int add_output_field(struct list_head *head, char *name) +static int add_output_field(bool contention, char *name) { int i; + struct lock_key *keys =3D report_keys; + + if (contention) + keys =3D contention_keys; =20 for (i =3D 0; keys[i].name; i++) { if (strcmp(keys[i].name, name)) @@ -404,7 +423,7 @@ static int add_output_field(struct list_head *head, cha= r *name) =20 /* prevent double link */ if (list_empty(&keys[i].list)) - list_add_tail(&keys[i].list, head); + list_add_tail(&keys[i].list, &lock_keys); =20 return 0; } @@ -413,10 +432,14 @@ static int add_output_field(struct list_head *head, c= har *name) return -1; } =20 -static int setup_output_field(const char *str) +static int setup_output_field(bool contention, const char *str) { char *tok, *tmp, *orig; int i, ret =3D 0; + struct lock_key *keys =3D report_keys; + + if (contention) + keys =3D contention_keys; =20 /* no output field given: use all of them */ if (str =3D=3D NULL) { @@ -433,7 +456,7 @@ static int setup_output_field(const char *str) return -ENOMEM; =20 while ((tok =3D strsep(&tmp, ",")) !=3D NULL){ - ret =3D add_output_field(&lock_keys, tok); + ret =3D add_output_field(contention, tok); if (ret < 0) break; } @@ -1609,10 +1632,10 @@ static int __cmd_report(bool display_info) goto out_delete; } =20 - if (setup_output_field(output_fields)) + if (setup_output_field(false, output_fields)) goto out_delete; =20 - if (select_key()) + if (select_key(false)) goto out_delete; =20 if (show_thread_stats) @@ -1674,11 +1697,10 @@ static int __cmd_contention(void) goto out_delete; } =20 - if (setup_output_field("contended,wait_total,wait_max,avg_wait")) + if (setup_output_field(true, output_fields)) goto out_delete; =20 - sort_key =3D "wait_total"; - if (select_key()) + if (select_key(true)) goto out_delete; =20 aggr_mode =3D LOCK_AGGR_CALLER; @@ -1817,6 +1839,10 @@ int cmd_lock(int argc, const char **argv) }; =20 const struct option contention_options[] =3D { + OPT_STRING('k', "key", &sort_key, "wait_total", + "key for sorting (contended / wait_total / wait_max / wait_min / avg= _wait)"), + OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,a= vg_wait", + "output fields (contended / wait_total / wait_max / wait_min / avg_w= ait)"), OPT_PARENT(lock_options) }; =20 @@ -1876,6 +1902,9 @@ int cmd_lock(int argc, const char **argv) rc =3D __cmd_report(true); } else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) { trace_handler =3D &contention_lock_ops; + sort_key =3D "wait_total"; + output_fields =3D "contended,wait_total,wait_max,avg_wait"; + if (argc) { argc =3D parse_options(argc, argv, contention_options, contention_usage, 0); --=20 2.37.1.359.gd136c6c3e2-goog From nobody Wed Apr 15 05:38:34 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 D8CBAC433EF for ; Mon, 25 Jul 2022 18:31:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236423AbiGYSbs (ORCPT ); Mon, 25 Jul 2022 14:31:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236042AbiGYSbi (ORCPT ); Mon, 25 Jul 2022 14:31:38 -0400 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 02BDC1F603; Mon, 25 Jul 2022 11:31:35 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id d3so3308151pls.4; Mon, 25 Jul 2022 11:31:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8JEJHCLbm9FaoQqQc3eYhH6ECpaTcGd/ZHTn5iTyFnE=; b=QxQ2mw6eAwh/HwShZr7aNJe8815zquepBSuCxSvmLwAY3A4t1M3gNVPMoUz+ftfGwy tD1U+Br9F29yQrOZVUp2BF6lqNVFDYWOKjD8YaoZv6cPIL9YhIURqy9yKV6yZK6HEDw+ 7T80zZgEVB2K/xW/RixITGAaS1zr1qbvdvvNWpskpDQivkE/BmrK08xFslaorhLoPh2B NBduWMIUyZTh1zz86FHIV943Lu4P3shUCVsq5hIfYCzzVb08eJfp7m4Blmk4DITzWmRI SC0CH30qGHKU88VhZ35BjuGgQ1y6l5pQgMn4+GMagyYazaq3wOUz1Zg7SqmghjQbmRUc IGGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=8JEJHCLbm9FaoQqQc3eYhH6ECpaTcGd/ZHTn5iTyFnE=; b=cSWQQlQmBV151taQ3ZdZjtRkujFrU4/pYgEhvLeDInkHluI1TG7LLgdyUIOgOIi5iO ND3Bc2VAn5qALNVvfGKnmDDjQq4oYeVpIF0ON6QAToYy35DSKqKILZrDqJByKcqnWsTJ R+9CHLq3QedQ64A3+6rBPBltN34pVVBNmavKihxP0LndU96PHD5ffxANjpRWq+60ITDM ixPVtkUFwHjoZkAUWoNc35KFwP6zym7QgCjHNo4rU1waNfWEAs6euyz1AQzM83TUgRyq LUqcGXSHlxZ6ACjzkLeqR/YYYwswLpugTDc3S3V5YTRYp9SSMRyzb540TYVUVNsMFzp9 cv9w== X-Gm-Message-State: AJIora9PiJd+6etAtgHGIBuZ9i2epMIIBV2KXG//rbXWzCYVIaDBIahQ a16X16gR7lpCMiqcqRpBNF0= X-Google-Smtp-Source: AGRyM1uRqKPKR1yNB/cQKqza6gj0fWAzLCl4qBI5fzjdU2/Mf5K0J2RoaYH29+HM/MqhzvOVxd6DAQ== X-Received: by 2002:a17:902:a586:b0:16b:e787:53f8 with SMTP id az6-20020a170902a58600b0016be78753f8mr13392534plb.163.1658773894352; Mon, 25 Jul 2022 11:31:34 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:1010:2c4d:e9cd:d086:4d46]) by smtp.gmail.com with ESMTPSA id c17-20020a170903235100b0016be9d498d0sm9740556plh.211.2022.07.25.11.31.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 11:31:33 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Will Deacon , Waiman Long , Boqun Feng , Davidlohr Bueso Subject: [PATCH 5/5] perf lock: Support -t option for 'contention' subcommand Date: Mon, 25 Jul 2022 11:31:24 -0700 Message-Id: <20220725183124.368304-6-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog In-Reply-To: <20220725183124.368304-1-namhyung@kernel.org> References: <20220725183124.368304-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" Like perf lock report, it can report lock contention stat of each task. $ perf lock contention -t contended total wait max wait avg wait pid comm 5 945.20 us 902.08 us 189.04 us 316167 EventMan= ager_De 33 98.17 us 6.78 us 2.97 us 766063 kworker/= 0:1-get 7 92.47 us 61.26 us 13.21 us 316170 EventMan= ager_De 14 76.31 us 12.87 us 5.45 us 12949 timedcall 24 76.15 us 12.27 us 3.17 us 767992 sched-pi= pe 15 75.62 us 11.93 us 5.04 us 15127 switchto= -defaul 24 71.84 us 5.59 us 2.99 us 629168 kworker/= u513:2- 17 67.41 us 7.94 us 3.96 us 13504 coroner- 1 59.56 us 59.56 us 59.56 us 316165 EventMan= ager_De 14 56.21 us 6.89 us 4.01 us 0 swapper Signed-off-by: Namhyung Kim --- tools/perf/Documentation/perf-lock.txt | 4 ++++ tools/perf/builtin-lock.c | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tools/perf/Documentation/perf-lock.txt b/tools/perf/Documentat= ion/perf-lock.txt index ae78e04346bd..8f4e34f924d5 100644 --- a/tools/perf/Documentation/perf-lock.txt +++ b/tools/perf/Documentation/perf-lock.txt @@ -119,6 +119,10 @@ CONTENTION OPTIONS and users can customize that using this. Possible values: contended, wait_total, wait_max, wait_min, avg_wait. =20 +-t:: +--threads:: + Show per-thread lock contention stat + =20 SEE ALSO -------- diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index ad3b191db2e7..041801d8b6ac 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -1560,7 +1560,10 @@ static void print_contention_result(void) list_for_each_entry(key, &lock_keys, list) pr_info("%*s ", key->len, key->header); =20 - pr_info(" %10s %s\n\n", "type", "caller"); + if (show_thread_stats) + pr_info(" %10s %s\n\n", "pid", "comm"); + else + pr_info(" %10s %s\n\n", "type", "caller"); =20 bad =3D total =3D 0; while ((st =3D pop_from_result())) { @@ -1573,6 +1576,16 @@ static void print_contention_result(void) pr_info(" "); } =20 + if (show_thread_stats) { + struct thread *t; + int pid =3D st->addr; + + /* st->addr contains tid of thread */ + t =3D perf_session__findnew(session, pid); + pr_info(" %10d %s\n", pid, thread__comm_str(t)); + continue; + } + pr_info(" %10s %s\n", get_type_str(st), st->name); } =20 @@ -1703,7 +1716,10 @@ static int __cmd_contention(void) if (select_key(true)) goto out_delete; =20 - aggr_mode =3D LOCK_AGGR_CALLER; + if (show_thread_stats) + aggr_mode =3D LOCK_AGGR_TASK; + else + aggr_mode =3D LOCK_AGGR_CALLER; =20 err =3D perf_session__process_events(session); if (err) @@ -1843,6 +1859,8 @@ int cmd_lock(int argc, const char **argv) "key for sorting (contended / wait_total / wait_max / wait_min / avg= _wait)"), OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,a= vg_wait", "output fields (contended / wait_total / wait_max / wait_min / avg_w= ait)"), + OPT_BOOLEAN('t', "threads", &show_thread_stats, + "show per-thread lock stats"), OPT_PARENT(lock_options) }; =20 --=20 2.37.1.359.gd136c6c3e2-goog