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: 34742 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2596509wrn; Mon, 19 Dec 2022 12:19:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf5n6tKUaOO2NfflxCQsJZCBupAbILzKglslszBHJIx1J6PGVsiY4Ec8GTMsoLARFoHrqJSL X-Received: by 2002:a17:902:9683:b0:189:c93c:e5e6 with SMTP id n3-20020a170902968300b00189c93ce5e6mr46177619plp.33.1671481165683; Mon, 19 Dec 2022 12:19:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671481165; cv=none; d=google.com; s=arc-20160816; b=PgK3Jc1dgTESUjubvvyqgRRA82JhHe+46jJ9t/a4s1SZl5nw007NlZrkDj7hzMHsPf JgyvVXwqjxNtKZipx4t6RUttdJLt31jyJYj9m5ps9P/d6P8rRlfTW/q4uVikArgo8Oxh 0QKsQydz/W9ri62C19meS/4AGCKwGBGXtoMX5kdOneebB5vJetebQail+rXGJdT2DCob sIgCZqdXzmIvjOLmBzj5y00iTSdPrXSrHdZAIOj4B+QY6xFo2HflW2x9B1/2PtCgctFi JB7EkRUJWFq+5aHN/aY0H1ZeMa3OLt3PIY6mAVDAIc+jenzQ0WYwhINmyKCdoGnqBzDM Hf0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=laFGg4zCVR1KZhPUCJ4Z8IBvPLNSm6yToKYeukylBe4=; b=qXaEb394ni478UByrJMX7oFbY9Bo8GZXqNwPn81RMkl4gPPDjyzCe1BqPIHjyE94f1 hn3VS61FAElZXQkTI5VCVF/mYM/ZVtwCXeNhqlU40C+/m54uC4aTksDHRXSC9BUDWqAe HmFMhuTRCQNCRdlcHe3uI8qFcsR5Ghe6yw9SMR+bceLr5DKylRDAXGNi+QrWNkabENVx Zc8BublfqpuJ1NutSDlDJXvhybe6GcLVmvQeIalmmDAXjSlmxgaLYXFhrK1q6Wj/Fq3j razt4HWGQmbNEX0EsuGQxEVkkR+vhbAGmlQckO4SD8UnKQGanvTkcAdKChzohAPTJ+bd pBvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JSaw3AL5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e65-20020a636944000000b00476e640ddfesi12141761pgc.80.2022.12.19.12.19.12; Mon, 19 Dec 2022 12:19:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JSaw3AL5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232532AbiLSURs (ORCPT + 99 others); Mon, 19 Dec 2022 15:17:48 -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-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 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752675034906232539?= X-GMAIL-MSGID: =?utf-8?q?1752675034906232539?= 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: 34744 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2596658wrn; Mon, 19 Dec 2022 12:19:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf7HgMFD4+I15ld1f/bxMLpOxjV/xfWytjrnbnED7VvzhsCpRV7kn8R4dlmGa/o/smj8rFke X-Received: by 2002:a17:90a:ec0e:b0:219:661f:9d8c with SMTP id l14-20020a17090aec0e00b00219661f9d8cmr45958587pjy.3.1671481186805; Mon, 19 Dec 2022 12:19:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671481186; cv=none; d=google.com; s=arc-20160816; b=h2esixrR3NBDjOqa49w8pmNnISO4HhsXaFO3He5C2ROo3n+GpZk/1PhliHt0HhU9vk zNKxh+CB989MPZFVI9HaukE5rDO3GOj4aXQgC4s3eFZ4zswEJLttulMA+IwLTSMZ3g7I bGMcfUM3YmofR1gBHjMQvKrkh+9XHWOmeMeTs7tKIX7Htj6KXz3MKlzUWtZNbsl3JJWG HiCR2rja/MOYqqCMrA5pvIUGwAq0PGun5BS6yRP5q1uAo78GOUhUVzmVVZkXI/9knZIr uTEtqJ8eEcX9auCXhw9caV/WLiMnFdfVytPS6p/mBGEkmtY79GNYptfk7nfe5JKDmrxR Oc+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=EZkB4QuSwHTQCLJv4c82Z6FZXzye3hmuSZyyGw5CfKo=; b=E0SbHsIBBAuVT4bb4TIcBXQ6CwaWxVN2D8eWcBIWl0a8UEWLwtEFX4fk96kZch4U1f FNdbwJ5wNLyXUGjLU4Amkjye38812Fmk6vzrY8z1SlXGW9vSX/8T861NJXh5fQTzvYzJ 83liCmK16S8NcgIeAsbj8AAU/KXMYmfdQOuucOm2EZFo1sbna+Bo9VQ/WeASZXvkTvF3 yiqJc6zOrLd5qCWeVptmhTUSA7NCgYwen5ZCSYQdfS9uWbnXESY+t8TnOFshDbIfBOgc Uwg6yzl9Vid4cFUIpJqGFvxguWgu6bBIz6n/JpLvxCq9Ycmeg/dP2KVZUE+ZXQemp5LZ S6rQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ffiQULjK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b74-20020a63344d000000b0046eed3142cesi12662717pga.350.2022.12.19.12.19.34; Mon, 19 Dec 2022 12:19:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ffiQULjK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232584AbiLSUR4 (ORCPT + 99 others); Mon, 19 Dec 2022 15:17:56 -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-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 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752675056780442522?= X-GMAIL-MSGID: =?utf-8?q?1752675056780442522?= 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: 34743 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2596529wrn; Mon, 19 Dec 2022 12:19:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXvsLUO6VcZirwM34ruYzUlGhHIRwoHJI3NuqxTXOO+EFmmvXLN1i+smiKYRs/jgXayGlfOk X-Received: by 2002:a17:902:d905:b0:189:ac49:fe9d with SMTP id c5-20020a170902d90500b00189ac49fe9dmr12025043plz.19.1671481167846; Mon, 19 Dec 2022 12:19:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671481167; cv=none; d=google.com; s=arc-20160816; b=LATvnVrW/mNPo3zhA3Kue6vmEJ5bEJo8poxFQwXY1VzjC9sOTWqUBscyx54aKdV4D8 8LArt59MIYNoQ7Sdd+WxCmYF2Zj+d0bK773zmpt7D1a1qOvoAiS3K7Dscdb2jPsKMptA E7KNfKlcoaDyzwPIWWeDVALV3vCo2Hw9kWwgZTgRAjNO2xUIcxn13byw2i8KHLEqeFrr YCDoaAQzksSV/XiHl9sGqp2GDfAZvJpQtwb1jz/HdQJpFKeQyWYkaSkkb55sM/DXIIrO SOZRuOTDS7nEdYRDeZ8xMj7whqmn/0EglO8q/REkJPTIkVnregIYwOEGuM28Nv2+6txp Umyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=cSBoXkTO9My+aXQVa73V9O3XAdMbkV24JOkMxSiJcY8=; b=JwKxARtBttn86dhGlVrk9Ecv4bn5tMZ7HlsvKTqD3L/fCF0GryD+TrlY04oErmH1hm iBJtdToj9eA4XfZRNmTo3UzL6C6EnqXNSE9hMPOr+nxkd9+exzsddmPEqguHYeBNdCwq 1GpLygkzv+c+J6MYqLXaExcsnG7DBFqK+z2PT5kOpSBvj0kV4wRzYMlpK4rY0VQQZNr6 utqTdlmYsEMPV8Zx9PbdDo/OkHgbz9oByA+GL+NgafFPLZ+QVAKSZvjlXMBTpJUdJ70d dw2L9vsaVGmDUYXu0jluOteyilfl8Ahso08PlZsHHDd9ZB2JzXdzrvigh1AlV0EGFMHR 3XVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ldw1Ubfb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 28-20020a63145c000000b004791c6d13f2si11073409pgu.162.2022.12.19.12.19.14; Mon, 19 Dec 2022 12:19:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ldw1Ubfb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232577AbiLSURx (ORCPT + 99 others); Mon, 19 Dec 2022 15:17:53 -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-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 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752675036793968732?= X-GMAIL-MSGID: =?utf-8?q?1752675036793968732?= 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: 34745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2596966wrn; Mon, 19 Dec 2022 12:20:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf4+W8FgyrTbFoekCBETB5Bf4W5H6JYsFisRfVe7PAgn4HE2xbzW2gR7a9mcSNHIPgPSPTSj X-Received: by 2002:a17:90a:f681:b0:219:6bf0:9861 with SMTP id cl1-20020a17090af68100b002196bf09861mr45016722pjb.10.1671481240946; Mon, 19 Dec 2022 12:20:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671481240; cv=none; d=google.com; s=arc-20160816; b=P3FoPKjXZCbMjgvmn9RAtlZ+D/AKD0XP29AbHAERiWQ1zdnwGVPfC96Dym/RFDgdk9 tlRMtGTnM6CAd0iPgoro/MnhKYU/nysbcQ1t7+BHwq2L/wE5moyRpwlyv42RnDvYVDmc PrcMg53fNNtdsXrtuWUvJw5h2jklSYt098CGM1FuaQiFtFk4IaF06Cc6BmnuTGiwR+5n L9HdjKEAbM6Hehs3lk3kM2VAd9z2hCYFg2Ie2Yp1YJgw90fV4+QE/uIVTy/b7N7gtSvP gZzd0b0FO3VeSANzgM02db4e1KhjMujupnZtS6vw9uYUShP6CxvcvYpi4xQHTR+qfn2L qQ2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=RjRD6WezQur7RnAVSzg16H/TNhNuOpZXrFzdEgyZjWY=; b=DjGthhPmm8rsIXSRxOmzL1EadKnO5l7XZuBoz/HRQTomkOZizJ7syx27Kdr5457lR3 9s34+KDPMOcYX/W7zdOgygaA6IArkOaQn3+K1UWj1Bwg9qmBNZSQlPsjOICD/4mjIBd7 3bfbqPdcab1R0I4GTpHT8myRhX/JW7Nedl3Kp/G9O9lxH5StJDfx2rg3voS/3ottiyzs Qom6paxeh15JAxWSNCEnR/bKGNHeXky87kVYIam631/G3B6aeMEI6Rgv1L3A5LK/tpIy lZcKtM/ayeDJ9l1EKpGRFjMH0E0jkbXBxjr58IFLA87YnZ/XOINJNBI/cYE2bWkZXHpW LNaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="K/iKHmim"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b2-20020a6567c2000000b0046b2ec0de65si11438400pgs.789.2022.12.19.12.20.28; Mon, 19 Dec 2022 12:20:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="K/iKHmim"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232558AbiLSUSA (ORCPT + 99 others); Mon, 19 Dec 2022 15:18:00 -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-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 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752675113356870764?= X-GMAIL-MSGID: =?utf-8?q?1752675113356870764?= 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: 34746 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2597041wrn; Mon, 19 Dec 2022 12:20:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf6ogNdUyp2hYdWFMiwLQsGxl5xkOCy9nNdcIynw1UniRE3Dp2LlLKz5EBy0Q3kmj3QkfnQQ X-Received: by 2002:a05:6a20:7f96:b0:a8:a203:ec97 with SMTP id d22-20020a056a207f9600b000a8a203ec97mr69245962pzj.60.1671481252530; Mon, 19 Dec 2022 12:20:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671481252; cv=none; d=google.com; s=arc-20160816; b=Fx8vVAwrUuwF64EJtN3asMqK3+hsGXi9fCs19g6f1njOgcUyNog1IUMolqt4C8Rtjb bvpcwHZnN4sekMO4BJmHy6K+crUU9QbkoGzYHtZGdlw7O988PfwpfusBx+JCbimEQnM5 F/TyO41FtngGoDsE8vcjggMGOOzuVZjrbYmkf0LKK2PRzsYeZA5ru2/h7yUmEnAL+nRi d8f78IYPRnH/8ln/RwEXhCv8sNtqEnZdYe/4kuczX2MOIuIs2+e8cLE+O5fdsa6UF9rW ivyXsblWnp9tXJ0PkRDsU5psBqenDXROzUk2PIXyK89d5+5NonMIOsI1XxDMTuJccnQ7 jhSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=rPDKYa8rD0VGPJQCF4t4qysKE20di3XOvK38s4BKO3o=; b=JUU4cR412nHylU2sAKap5jB013RzNVMb2p5vqZxz0FmCJjPCGfJ8lx6baPGxpywWJj AmdXc2F1yDjrhyHBzX/+qNR/0bjk2IMb8anLOkgNWuvK0/U9jeSHCQXsMX8nzWo3ailO sd0Efzszkimt/5P8IzDp1VmnH2l1IW7u+qvHUpNn5+ei0LOTssqxHjPkerFIFPOqZzt5 NjbL39JLB21OjWxS2oDmv97n/XEsn/CeBKT1rKvnOzcDMGKYlG1gdbs9o165uxbrysMT 1LMA0VNABvK9EAJDf3RaJmL5TjASgs8aTE1Lf3Lw08Bg2QR/TYVBoxP/AAVJZCdj10/L oRnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eu74UH9w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q197-20020a632ace000000b00478b09b9e9csi11913189pgq.561.2022.12.19.12.20.39; Mon, 19 Dec 2022 12:20:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eu74UH9w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232531AbiLSUSF (ORCPT + 99 others); Mon, 19 Dec 2022 15:18:05 -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-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 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752675125690739692?= X-GMAIL-MSGID: =?utf-8?q?1752675125690739692?= 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: 34747 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2597193wrn; Mon, 19 Dec 2022 12:21:19 -0800 (PST) X-Google-Smtp-Source: AA0mqf4CWrAZbeKLwku0WOWC2QyQokTQxQ8qRsormBbBQDf40YNgBy1fUbToA+kVP6LGyRTrk26X X-Received: by 2002:a05:6a20:a012:b0:a6:713d:4343 with SMTP id p18-20020a056a20a01200b000a6713d4343mr50939393pzj.5.1671481278833; Mon, 19 Dec 2022 12:21:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671481278; cv=none; d=google.com; s=arc-20160816; b=UQKBmnd2uz9HsoF9ssxGDb3vBOXOS3QF8Ew2aPNW9XtAI0zPuVAikLrpkd+YvLvhW1 rVlQQEBk/5rUHqgbvLj85wjDxElRyNt5ijj04LQFDcI9uoEPkePrkTYz9dotsKdVXlP2 IcQxhh2zNGRNbHb88WQd/NCohq4/2efXv0J8kpowuHqjalzvEHtOuPKQeujOqL3RbYsp Io6or+Dzc+1cFl1CIKq5aSLyc+IeEbxHNU3GqNhqeG6zcSJG4CL8yNmcfzp9x5Dml2Nh iDRpWbyRyPlkrc25wVoJO/Tibdzg/VnonaPmK+CA/x9cW8pAjhN7yj/U+aBMgiVBxgDO 0cyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=E/YKT1JJIPuRoA7Hx7R8arjPSGFufHSp1KclETKmED4=; b=wxpDrSrua4LUDhvTgEl8/2Hr94BXutWMjC0zu01Za6nxME4v59lhpbFTpxf+70+215 S6S2RG2URczZYqs+wU3IEZx3YYfrLtX3obrcnszlFaWhzz3SnSPXB3qj5qwaCnsLUGBu Jhf6CLMJ3DgZY4ZL02MBF0E6H0xFhTGZ7bYT2MKguiU5WJopFjxXjWB05FQdw2HAFA3y 4+UXOh/ZEkYwsJ/yHl6ubQ49bORUkjfUkzHcE15qx2nCoIrftK5z7ySac8Se28esXps+ dfP6X9Z5wb15T95xnxMTdBcxTziNxigmn+bcNyU5GDIPSjs+Maslt8yKsTeSLjEoCnI7 06dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=HVbC+Iu6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cp6-20020a056a00348600b005726ac39d2esi11014750pfb.30.2022.12.19.12.21.06; Mon, 19 Dec 2022 12:21:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=HVbC+Iu6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232578AbiLSUSM (ORCPT + 99 others); Mon, 19 Dec 2022 15:18:12 -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-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 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752675153343488604?= X-GMAIL-MSGID: =?utf-8?q?1752675153343488604?= 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}