From patchwork Fri Feb 3 02:13:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 52264 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp598978wrn; Thu, 2 Feb 2023 18:27:33 -0800 (PST) X-Google-Smtp-Source: AK7set971xVTWFUoUkOjE0SPRIxLVnsztfz7UtIFQMpS2OJlIIDqNXPkxtHbLcCgo5Wrs1Ba+Msf X-Received: by 2002:a17:906:3714:b0:86f:b99c:ac8d with SMTP id d20-20020a170906371400b0086fb99cac8dmr8580782ejc.44.1675391253159; Thu, 02 Feb 2023 18:27:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675391253; cv=none; d=google.com; s=arc-20160816; b=KR1v7dBknd9VaaZaOHrqIMHIc7vnoNWWPWGcOKd9vqGBSwg+xWV89Y7ojSqPGc9bCX 9aIFiv5xH+AS8DJ+Xqb1ctVkf1kwIdwr39ToDR0+2EJp17vz0tNUGjPiflXglZ9Fuzln 5OmvNLBNy8OeltO+nvERphBqWIdJkQvcXApwNIebRvQENJfSz0NFTsel+HwOkFyLCzmz +zop0lRYrbJrhj9jZoKSxZRUA6fPDWfbOC8L2zzLe79excP3a54wOdY4POHychgfQDiq fVbicCWOmvKEFZhrYlfbMKjnxz83cNuUwAWDLweLcic7h/S641po8zuUYo7+fE0Jiw8D oVDg== 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=fyJQOGiYU/hwXdfGS7h3Hw/9gX5jq5IXXOZzfWeyHFw=; b=wSTaWCGbd1HVHu/XDbEpzi5FOxdClzgYCmM9d2goPxGbo7nkTpjpI/Jgoy5c1gur3t n+HCh0/nbVoaI88Vp0PmdAmwdWJsHLTAB3TPZzzCJjr//SWh9uN3wKsQPA5camefXKVH e/dhq/69mqz2pDZMk3+dArrZwTcKVV/ejWe38vYxYD0Kb4VxuDLttDohPAhdFbPEwn3i 8jYbJtNdf0zlXWyN2o/pWH9qJsGfdtcvspwKTweYWfx8aWUDL0Lgo15560UilDx3eV40 Gud/Woe2Fj2jNuJVJzVJK04TR+jM9l8jn9xRzbiDzkKxeFH7hr3v6A/inupVWjTR3GzJ jS5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=T9Ux7T65; 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 fq7-20020a1709069d8700b0088be22484aasi1253262ejc.66.2023.02.02.18.27.06; Thu, 02 Feb 2023 18:27:33 -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=T9Ux7T65; 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 S232088AbjBCCNh (ORCPT + 99 others); Thu, 2 Feb 2023 21:13:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231748AbjBCCNd (ORCPT ); Thu, 2 Feb 2023 21:13:33 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D07F21290; Thu, 2 Feb 2023 18:13:32 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id k13so3926812plg.0; Thu, 02 Feb 2023 18:13:32 -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=fyJQOGiYU/hwXdfGS7h3Hw/9gX5jq5IXXOZzfWeyHFw=; b=T9Ux7T65//D97ke/jh5gsXIp89LPngqP8fNgMXuXaKcDf0gs+aeu59hnL+5fAGPGMh JFRHzNmtfdcJHgUvyozlDVX/wAaSP2TGk2/nA7XPR22LP7jywJ4WOtFW++8L8TiOqRT/ HAxKyvr3YWjw1KKMvj1HgOGILH07uBbzkuKmeVySxEG2dIGLZ3KqSRPKaOIgB0TllkJF i1uFK1d9rQEDHhUD6dKMupWc07xkWWDExS9Z07P+ziCNa58cVupj8DVpZa9eh7GBsSz9 kRGdTVcbdzLlIb6Mvp+kKhqXflB56LvbdxyTq/c7x0oToDuddzvKslVM0H+B7xxLhOhG 6wIw== 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=fyJQOGiYU/hwXdfGS7h3Hw/9gX5jq5IXXOZzfWeyHFw=; b=Y/XDuz9P01UeXFjTwc85tb1cakVLro7I78hxEWe/a+AQHzWlnKM1CWuAz8f6OjaUhU x4K9MU93uOklWoyQUjGDE9qEl21/ODwRFzfKLLmttD2fduWH4U0Of9mMw7wsVnb6QpJ3 xdYnqLmfou+c16uqQHAqsgW3AYMVv5zOkfqEwDAuua5cfT/5iPUyDRpwCGjkRpyDc67H +GyRrLyiiY3yFBgitkwJ9TgTlsOq+ULANsV8XtTgC/iSR3BMPmI8lIyQRLLuSz2iYXuc AQOBwTzpUZFrDSZJjLikQiT4xV+3dXdrqD/JEqG7eqm6AznRnK3tairUO8gDcltdIEsy 29mQ== X-Gm-Message-State: AO0yUKVUsT3ghlsojzuJzrEZBq4Qa+0r9iWHDBXAHy7dvU4BZditX9Np p+kcLvHI1ei/hiwYMnZ7J5E= X-Received: by 2002:a17:90b:3b87:b0:22b:afe2:2bb4 with SMTP id pc7-20020a17090b3b8700b0022bafe22bb4mr8865901pjb.8.1675390411555; Thu, 02 Feb 2023 18:13:31 -0800 (PST) Received: from moohyul.svl.corp.google.com ([2620:15c:2d4:203:6755:96ed:3f18:dac4]) by smtp.gmail.com with ESMTPSA id fs23-20020a17090af29700b0022c942b8683sm497249pjb.56.2023.02.02.18.13.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 18:13:30 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: acme@kernel.org, jolsa@kernel.org Cc: peterz@infradead.org, mingo@redhat.com, irogers@google.com, linux-kernel@vger.kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, song@kernel.org, haoluo@google.com, bpf@vger.kernel.org, juri.lelli@redhat.com Subject: [PATCH v2 2/3] perf lock contention: Use lock_stat_find{,new} Date: Thu, 2 Feb 2023 18:13:23 -0800 Message-Id: <20230203021324.143540-3-namhyung@kernel.org> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog In-Reply-To: <20230203021324.143540-1-namhyung@kernel.org> References: <20230203021324.143540-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 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?1756775059018151239?= X-GMAIL-MSGID: =?utf-8?q?1756775059018151239?= This is a preparation work to support complex keys of BPF maps. Now it has single value key according to the aggregation mode like stack_id or pid. But we want to use a combination of those keys. Then lock_contention_read() should still aggregate the result based on the key that was requested by user. The other key info will be used for filtering. So instead of creating a lock_stat entry always, Check if it's already there using lock_stat_find() first. Signed-off-by: Namhyung Kim --- tools/perf/builtin-lock.c | 4 +-- tools/perf/util/Build | 5 +++- tools/perf/util/bpf_lock_contention.c | 41 ++++++++++++++++----------- tools/perf/util/lock-contention.h | 3 ++ 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 216a9a252bf4..0593c6e636c6 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -465,7 +465,7 @@ static struct lock_stat *pop_from_result(void) return container_of(node, struct lock_stat, rb); } -static struct lock_stat *lock_stat_find(u64 addr) +struct lock_stat *lock_stat_find(u64 addr) { struct hlist_head *entry = lockhashentry(addr); struct lock_stat *ret; @@ -477,7 +477,7 @@ static struct lock_stat *lock_stat_find(u64 addr) return NULL; } -static struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags) +struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags) { struct hlist_head *entry = lockhashentry(addr); struct lock_stat *ret, *new; diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 79b9498886a2..918b501f9bd8 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -154,7 +154,10 @@ perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter_cgroup.o perf-$(CONFIG_PERF_BPF_SKEL) += bpf_ftrace.o perf-$(CONFIG_PERF_BPF_SKEL) += bpf_off_cpu.o -perf-$(CONFIG_PERF_BPF_SKEL) += bpf_lock_contention.o + +ifeq ($(CONFIG_LIBTRACEEVENT),y) + perf-$(CONFIG_PERF_BPF_SKEL) += bpf_lock_contention.o +endif ifeq ($(CONFIG_LIBTRACEEVENT),y) perf-$(CONFIG_PERF_BPF_SKEL) += bpf_kwork.o diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 286e52ba9f91..ead2898ba377 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -256,12 +256,34 @@ int lock_contention_read(struct lock_contention *con) prev_key = NULL; while (!bpf_map_get_next_key(fd, prev_key, &key)) { s32 stack_id; + const char *name; /* to handle errors in the loop body */ err = -1; bpf_map_lookup_elem(fd, &key, &data); - st = zalloc(sizeof(*st)); + + if (con->save_callstack) { + stack_id = key.aggr_key; + bpf_map_lookup_elem(stack, &stack_id, stack_trace); + } + + st = lock_stat_find(key.aggr_key); + if (st != NULL) { + st->wait_time_total += data.total_time; + if (st->wait_time_max < data.max_time) + st->wait_time_max = data.max_time; + if (st->wait_time_min > data.min_time) + st->wait_time_min = data.min_time; + + st->nr_contended += data.count; + if (st->nr_contended) + st->avg_wait_time = st->wait_time_total / st->nr_contended; + goto next; + } + + name = lock_contention_get_name(con, &key, stack_trace); + st = lock_stat_findnew(key.aggr_key, name, data.flags); if (st == NULL) break; @@ -274,14 +296,6 @@ int lock_contention_read(struct lock_contention *con) st->avg_wait_time = data.total_time / data.count; st->flags = data.flags; - st->addr = key.aggr_key; - - stack_id = key.aggr_key; - bpf_map_lookup_elem(stack, &stack_id, stack_trace); - - st->name = strdup(lock_contention_get_name(con, &key, stack_trace)); - if (st->name == NULL) - break; if (con->save_callstack) { st->callstack = memdup(stack_trace, stack_size); @@ -289,19 +303,14 @@ int lock_contention_read(struct lock_contention *con) break; } - hlist_add_head(&st->hash_entry, con->result); +next: prev_key = &key; - /* we're fine now, reset the values */ - st = NULL; + /* we're fine now, reset the error */ err = 0; } free(stack_trace); - if (st) { - free(st->name); - free(st); - } return err; } diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-contention.h index 17e594d57a61..39d5bfc77f4e 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -65,6 +65,9 @@ struct lock_stat { */ #define MAX_LOCK_DEPTH 48 +struct lock_stat *lock_stat_find(u64 addr); +struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags); + /* * struct lock_seq_stat: * Place to put on state of one lock sequence