From patchwork Thu Feb 2 05:04:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 51689 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp45052wrn; Wed, 1 Feb 2023 21:07:21 -0800 (PST) X-Google-Smtp-Source: AK7set8H0k4bCpOx1cL6Xh83awWI1DyCHKxFXWG59yC57MjSpyNNbpRTGddq/2V493hxb9G3ZRhu X-Received: by 2002:a17:907:8d06:b0:889:f989:d8ee with SMTP id tc6-20020a1709078d0600b00889f989d8eemr4778991ejc.22.1675314440963; Wed, 01 Feb 2023 21:07:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675314440; cv=none; d=google.com; s=arc-20160816; b=MC0gy47sjuzB51Uygngy/X2PXYtttDrwDMxT86PDbnHW9hdvml/dTMv89ngjFDWXL+ fDX8CaESvfHC+Mkk1iaVevPFTd4g0XlgsS47KskDW3FB/zDFMJfcyepvczRQKAC2cyGD giLY9z1U3X1f7XdvBIVWAJp08JNFggz4G2sSwaHnn7qGOzBWZcnZVIX/JvteVWsIZS+T Db42wr60QBbjWcKk+qfQW2KvP2UwGMbYCHrupjeJRX6CwQ7dtUSDVqBUQtF0bJwmBfnC YcutzprLmC0vnC33H+vYJRCUtn8coXofbqaaWPLAj3JS1j5pLpkQLGKjB117He6fpO1x w4Rg== 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=fEIr+OoIpQkzb209AcBpca6C2Jr4FCiGLC2+Zeyv8Bw=; b=neBIbj8X0HOlGIZGHohUqqSqhE7xujc0RcmlfvQ22lD9HavGVp28AAs0VMTHJheWpV xN6Qu45K+LMQVAYj4X01iKavfzIU2uU7ySU+sohbkWA27tjj1TABGZhZdmaKLbfxRbQf Vd/UkAm9WpMglXzok+x2jKynEQe4fJr9FP0KW5fTg+n2oxUeJ7xvcDd0sAX4WTcjy+t+ y1c4R1nAc47RZ4ggOLMogPhvlbsV8XVC3cgNnUWd54CI4kdvc8gBS2cP5M3mBIvWzP2g Ihm5qoidRZBd8vhp2fvPQv8gav7O+kuIbaN2VWM94+XzbEdoVHkC5peS1N90inNsYN6I Gn5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="gAc/tKg3"; 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 eu3-20020a170907298300b0086ea1c7ae21si29500194ejc.54.2023.02.01.21.06.55; Wed, 01 Feb 2023 21:07:20 -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="gAc/tKg3"; 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 S231737AbjBBFFJ (ORCPT + 99 others); Thu, 2 Feb 2023 00:05:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231617AbjBBFFE (ORCPT ); Thu, 2 Feb 2023 00:05:04 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90D5B1C58E; Wed, 1 Feb 2023 21:05:02 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id cq16-20020a17090af99000b0022c9791ac39so4455608pjb.4; Wed, 01 Feb 2023 21:05:02 -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=fEIr+OoIpQkzb209AcBpca6C2Jr4FCiGLC2+Zeyv8Bw=; b=gAc/tKg3eLTGHm2VPNaDwZzhE7xqzIKrnrxrhgU7cQdglEXBy3rEkSLvc2RfgKdWjl sCNcCDPekxC7g7gu+3QJYOMTwEf/PA2oCJAUNbsM/HoUw2teHZxSi6UdDTn+fmJgb1zJ 9MamybUMyys/O2W6GdoG8A2Uo3xb6hjfcL0X4db4B6VRTXS7C48v7rtsR0+heFeu9R9O BU2qHTvQkcrzJUkkPLpr4obHkpA1CNlAymqgDMntPwjx6Wvx+7zZclmwSolaZSFAlh5L sg/sVqcsWEafjGOh6tdYRykpLEULvNoHasdme15YOW+bhx5gURGoSt/Yf8v6xzDbDDMl pclw== 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=fEIr+OoIpQkzb209AcBpca6C2Jr4FCiGLC2+Zeyv8Bw=; b=DqgSLHvCKkbekdSzI6gn2uCDT3KOd3svZfYx9YYrjSWINp0QpD1nG9zYvPDdSjLX+I 0E/2PMwaftzyvHuw/c04g+G+tr3y8jnf0ORdhqJeAZVRZiaBW/8p9Zyo096Hey4M6HMf Rxadg/GJvOtnghRUAEfNlUpxoaVdawAfJWjDnz4XJcXo0+nRqDxVsX2MnmYbXbFEvqNC 6KyrBhHQFOKkD3RRcYREoqIvB9IFU43aUcyN1zccALeO0vRmg8RMDL1vK8JdhFuw/Tmo QfOxGX9AY48faY9/c3HTLAuMlMx/R10Fu2OB2sxWdn6ekEdH6KOSbahhm69xm+OXgLUi ouLw== X-Gm-Message-State: AO0yUKVptoZZAR9lTX8MJFvt++EWhGK50BnZTlZzKjQtaN+Fg3GRM1is ioLHYp4PdpqBr3BascUCXFM= X-Received: by 2002:a17:90b:1bd0:b0:22c:2a36:db3b with SMTP id oa16-20020a17090b1bd000b0022c2a36db3bmr5003499pjb.31.1675314301930; Wed, 01 Feb 2023 21:05:01 -0800 (PST) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:ff0:8899:7288:e54d:8b87]) by smtp.gmail.com with ESMTPSA id t3-20020a17090aba8300b0022bb3ee9b68sm2238374pjr.13.2023.02.01.21.05.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 21:05:01 -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 , Hao Luo , bpf@vger.kernel.org Subject: [PATCH 2/4] perf lock contention: Use lock_stat_find{,new} Date: Wed, 1 Feb 2023 21:04:53 -0800 Message-Id: <20230202050455.2187592-3-namhyung@kernel.org> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog In-Reply-To: <20230202050455.2187592-1-namhyung@kernel.org> References: <20230202050455.2187592-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?1756694515454149145?= X-GMAIL-MSGID: =?utf-8?q?1756694515454149145?= 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/bpf_lock_contention.c | 41 ++++++++++++++++----------- tools/perf/util/lock-contention.h | 3 ++ 3 files changed, 30 insertions(+), 18 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/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 967ce168f163..c6f2db603d5a 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -254,12 +254,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; @@ -272,14 +294,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); @@ -287,19 +301,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