From patchwork Thu Feb 2 05:04:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 51688 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp44830wrn; Wed, 1 Feb 2023 21:06:43 -0800 (PST) X-Google-Smtp-Source: AK7set9BvXeNHFHPsoT6RZH1mKJt/jujEQRuWFMCDAiV8HaGDVFm67hKMoPfwtaQxSLg+jyeQbdQ X-Received: by 2002:a17:906:d184:b0:885:fee4:69ee with SMTP id c4-20020a170906d18400b00885fee469eemr4798777ejz.59.1675314403256; Wed, 01 Feb 2023 21:06:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675314403; cv=none; d=google.com; s=arc-20160816; b=huKTZNjHc4dVEgjEy27u2Us3sXggs+DahRMhJRKXmECICaouR9yX3gOTeApi0nqFPF 2p5tCK5rpmWOhpm2ZnQ4Yf7ETKQJsYTCXvi4fnitBmlPYl9+XzfzL7Am1oIlkdOuI+hG mmnOwh0qSWCsbl6rj3xGVdP8zroSvICORc93EB8+ue+6j9fvwr0KzSxuzuiQsI1ILLhM 2wA6hUmjg9jSUa4GyaE+J0W6czNH+oqAtPJPkm0Kt2FHDKH3ZyYMliiMHit/czP9D9q6 X4mht/klxkEug1jIprS1ggxW2Hc3wX/2Qvim0lbgsDNljtEOcXiemQdR8jyHQVBqfanq 8V1Q== 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=fOU/DIS3FJule2LiTwVBX9EbwBghUDusHfGnb+FT/ZA=; b=lCp2/lmT9in6bmMFynA9jPxtummCYg+dSB4/rUK+kf99T7WgmLE37LTRht1Jfom/D8 nu/ODaiDKMJZap+C1KKxbACq1+nQaTDapekCXBrInCQMyLwXJ+loY1xIDj/X5oLYc1Gs BBJbZm2GxSL+IqRGEXqCdHSAVw3sG2DzJ6bGeSHXHkuhsCupR/UgK1XQjMYtgLeSdQFQ 74tmWqEinrSeYxtC4pcSewW5VX5GS9OJQJLxalBqPkoOwfc/YDwwd4Og5o0t7PHaeWIG 6JKpoN2da9Q9DMQWz24j8+OYq56eydMqEs6k53/CLGKRJK9Rk00ZLrfmMQaR1qUCYBxY tzyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="lCF2pcP/"; 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 j2-20020a170906534200b0087849c91571si25175770ejo.352.2023.02.01.21.06.19; Wed, 01 Feb 2023 21:06:43 -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="lCF2pcP/"; 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 S231649AbjBBFFE (ORCPT + 99 others); Thu, 2 Feb 2023 00:05:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231160AbjBBFFC (ORCPT ); Thu, 2 Feb 2023 00:05:02 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE22413D6F; Wed, 1 Feb 2023 21:05:00 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id mi9so828312pjb.4; Wed, 01 Feb 2023 21:05:00 -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=fOU/DIS3FJule2LiTwVBX9EbwBghUDusHfGnb+FT/ZA=; b=lCF2pcP/QHMEJGiIPMBZVeaiXjF3+zNSoFftzLQdl+mbYFwFkeNee9PfGMQ6ouvTJw G4Mnlg9vSoQ6ljihWWKl2x0ghROZz+9fqYZkBKkpz8UFnspiVr2fFygI5XQwUM0zrwgV 0GKb+b4Y4Y4vJ32xqROGYeiDUwpI5ygQcWBb0lpYgIZv5nM7yNqA1/1YSG03NSPdGLgs CWlcKajH8NQi74hLdtJhl2zZYhPjCse2ZRChebemZRFvfRxSQoUKxIVFJfTdBbh3E+7A Qq7i9hx1j7e5lfNA9A9vtrGcVTVtQkem/7L8IFWaYSC9jgnh1A3/7ZHPuNAXzrWmofxM +JUQ== 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=fOU/DIS3FJule2LiTwVBX9EbwBghUDusHfGnb+FT/ZA=; b=YCGBORA5tUwe1uMl7DC1Ijq+vW7nB1MBbPNf3q9BQHZ6F7PQ8SWYjEmGfYTheuVyxh SxFcQ6Wre6j4yADJMcVTEH8FfqF7Djdm9iEEl61AjScmlTg98ublJUgbKMj70nWiab28 IeN0aApzC814Rh+YURhxwPriBZFSh48NdJHDFRtH2HXMQ7rvyQ2lmPJqDmHOT95eDbFg T7KFuCWm4AUuJlXv6OuGKV+E6YVYqq0MNe011jC3c4wkeazKxciBX3BHiyxgj+zLHokQ 7dj7Sf5Z/MZ1HAO9l/0DLEJCVoyktwG2RorWd1AnT++5F9KxImXZOCX+lOKCHaUffMxI sSDA== X-Gm-Message-State: AO0yUKWYke8qE2vfWbYpe0jQJ8JFTkNxkpIWkQES5m89n/SYZd4kgjIk 2dD0eKyYvnOmkwXLGUFxt1k= X-Received: by 2002:a17:90a:db8e:b0:22c:147d:137f with SMTP id h14-20020a17090adb8e00b0022c147d137fmr5356825pjv.17.1675314300163; Wed, 01 Feb 2023 21:05:00 -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.04.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 21:04:59 -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 1/4] perf lock contention: Factor out lock_contention_get_name() Date: Wed, 1 Feb 2023 21:04:52 -0800 Message-Id: <20230202050455.2187592-2-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?1756694475772142539?= X-GMAIL-MSGID: =?utf-8?q?1756694475772142539?= The lock_contention_get_name() returns a name for the lock stat entry based on the current aggregation mode. As it's called sequentially in a single thread, it can return the address of a static buffer for symbol and offset of the caller. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_lock_contention.c | 113 ++++++++++++++------------ 1 file changed, 63 insertions(+), 50 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 4902ac331f41..967ce168f163 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -163,9 +163,68 @@ int lock_contention_stop(void) return 0; } +static const char *lock_contention_get_name(struct lock_contention *con, + struct contention_key *key, + u64 *stack_trace) +{ + int idx = 0; + u64 addr; + const char *name = ""; + static char name_buf[KSYM_NAME_LEN]; + struct symbol *sym; + struct map *kmap; + struct machine *machine = con->machine; + + if (con->aggr_mode == LOCK_AGGR_TASK) { + struct contention_task_data task; + struct thread *t; + int pid = key->aggr_key; + int task_fd = bpf_map__fd(skel->maps.task_data); + + /* do not update idle comm which contains CPU number */ + if (pid) { + bpf_map_lookup_elem(task_fd, &pid, &task); + t = __machine__findnew_thread(machine, /*pid=*/-1, pid); + thread__set_comm(t, task.comm, /*timestamp=*/0); + name = task.comm; + } + return name; + } + + if (con->aggr_mode == LOCK_AGGR_ADDR) { + sym = machine__find_kernel_symbol(machine, key->aggr_key, &kmap); + if (sym) + name = sym->name; + return name; + } + + /* LOCK_AGGR_CALLER: skip lock internal functions */ + while (machine__is_lock_function(machine, stack_trace[idx]) && + idx < con->max_stack - 1) + idx++; + + addr = stack_trace[idx]; + sym = machine__find_kernel_symbol(machine, addr, &kmap); + + if (sym) { + unsigned long offset; + + offset = kmap->map_ip(kmap, addr) - sym->start; + + if (offset == 0) + return sym->name; + + snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset); + } else { + snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr); + } + + return name_buf; +} + int lock_contention_read(struct lock_contention *con) { - int fd, stack, task_fd, err = 0; + int fd, stack, err = 0; struct contention_key *prev_key, key; struct contention_data data = {}; struct lock_stat *st = NULL; @@ -175,7 +234,6 @@ int lock_contention_read(struct lock_contention *con) fd = bpf_map__fd(skel->maps.lock_stat); stack = bpf_map__fd(skel->maps.stacks); - task_fd = bpf_map__fd(skel->maps.task_data); con->lost = skel->bss->lost; @@ -195,9 +253,6 @@ int lock_contention_read(struct lock_contention *con) prev_key = NULL; while (!bpf_map_get_next_key(fd, prev_key, &key)) { - struct map *kmap; - struct symbol *sym; - int idx = 0; s32 stack_id; /* to handle errors in the loop body */ @@ -219,61 +274,19 @@ int lock_contention_read(struct lock_contention *con) st->flags = data.flags; st->addr = key.aggr_key; - if (con->aggr_mode == LOCK_AGGR_TASK) { - struct contention_task_data task; - struct thread *t; - int pid = key.aggr_key; - - /* do not update idle comm which contains CPU number */ - if (st->addr) { - bpf_map_lookup_elem(task_fd, &pid, &task); - t = __machine__findnew_thread(machine, /*pid=*/-1, pid); - thread__set_comm(t, task.comm, /*timestamp=*/0); - } - goto next; - } - - if (con->aggr_mode == LOCK_AGGR_ADDR) { - sym = machine__find_kernel_symbol(machine, st->addr, &kmap); - if (sym) - st->name = strdup(sym->name); - goto next; - } - stack_id = key.aggr_key; bpf_map_lookup_elem(stack, &stack_id, stack_trace); - /* skip lock internal functions */ - while (machine__is_lock_function(machine, stack_trace[idx]) && - idx < con->max_stack - 1) - idx++; - - st->addr = stack_trace[idx]; - sym = machine__find_kernel_symbol(machine, st->addr, &kmap); - - if (sym) { - unsigned long offset; - int ret = 0; - - offset = kmap->map_ip(kmap, st->addr) - sym->start; - - if (offset) - ret = asprintf(&st->name, "%s+%#lx", sym->name, offset); - else - st->name = strdup(sym->name); - - if (ret < 0 || st->name == NULL) - break; - } else if (asprintf(&st->name, "%#lx", (unsigned long)st->addr) < 0) { + 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); if (st->callstack == NULL) break; } -next: + hlist_add_head(&st->hash_entry, con->result); prev_key = &key; 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 From patchwork Thu Feb 2 05:04:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 51690 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp46135wrn; Wed, 1 Feb 2023 21:10:32 -0800 (PST) X-Google-Smtp-Source: AK7set+PUpiVE3GLJu1/TXfEdc+s14b+yX1NsmohR9bOPDGZxqnFjAjlc0idOxphA6wnooSJIHdM X-Received: by 2002:a50:d79b:0:b0:49e:5ffd:5be2 with SMTP id w27-20020a50d79b000000b0049e5ffd5be2mr4722872edi.40.1675314631886; Wed, 01 Feb 2023 21:10:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675314631; cv=none; d=google.com; s=arc-20160816; b=ZODs/xr95DphnjajGriZvDaFkYC9HWaKLgEhSTykncDN6jKXRVdEhcIymn2GB1Utu4 QcqlmldGwdsSgcwQLMkuQDGsyrBhal2WZjtuxBFK7CLAFLr+nhyjlfu8pOZzJZgHg8Ls 9y9ZbGKZMBNr7ldfBn0q3PB5yTkS3EZFJMDJhcf2qhMKQyTaXF4GQ6iDngtSpt5kgk9O 98HzyvR4aW4xNzsOfvJWRrFURIWJNIq7TKQAcjjmpGQPKgaqQmKidIznPX9OZ2PTDkYO Tw2H08ZCW+incjdVYYhv+qmHQ090Kdz80O2ArkYqGG/mwKV0pAjPy/tUJp8H1hhRBHTO 69jQ== 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=55jw19q8kUEYnAPhTjk+7NZAYN/Z3zsEUE3RkruDNgg=; b=nllW/VZj3hdtyzUhSQgPVSQpXgmPOKaHw+f877E7gtYl6I7ppr+6do9FeJPuMMRmrY XIUNTKJNWocK2MBv06FiTOVhKnvAOQ/KlVqFCNfpSj9UGMzPgvy+bA6jlchbrwTPa1kH ysphIb21qcEWy1nlgvUvQb4GJ22bWfwguugivy2RY1cU5+6rM8UnQ3dxuKahC3aEzDR3 Lxu58z4Vm4uxgN/H7fW1gB/5OCBQs/Q9fi5Een+wbjcCguDckWBOgvaGht3hN51A1ukH H0mwkqeEd4MgsA0JJSIA0DBbr4WxrBdjXOW2sqr0iiBIth3iOOtTyZ0dyt5h0FN7XTF5 KjTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=iLt0fwhr; 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 be23-20020a0564021a3700b004a22938c8desi16891953edb.258.2023.02.01.21.10.06; Wed, 01 Feb 2023 21:10:31 -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=iLt0fwhr; 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 S231755AbjBBFFU (ORCPT + 99 others); Thu, 2 Feb 2023 00:05:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231720AbjBBFFG (ORCPT ); Thu, 2 Feb 2023 00:05:06 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 300C670D7F; Wed, 1 Feb 2023 21:05:04 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id mi9so828413pjb.4; Wed, 01 Feb 2023 21:05:04 -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=55jw19q8kUEYnAPhTjk+7NZAYN/Z3zsEUE3RkruDNgg=; b=iLt0fwhrTdeWab77xDTdgnBj4sgdotnGd9W+EmJKaKMW8IbTIi9guj00XPhpUOroz8 7h6Q3GMS0WxrLyaKVBmgC6Hw5UAoSL9liDX/aU7YbQiG+OtescXa/datB6VXozMU7qZb sO6sMBGVLG2bnCw5vFLxU41D2yt1bwcbdyaD7V8nOJpmHJ38MxIJ+alHzE+7wiV4ooQH E0eVPdqXTK25a3/EvrgKrzE5VVJ5v8AZG/G7ZsmSoJfMvjuzicA78hTz6G8gyrBCJiuJ So7pdcQ12QDyHJ7OeTN+jTGDlyBYIv2lcUMoad3/UnP4jorP6oSpyxBHFerftZNVGE+S /foA== 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=55jw19q8kUEYnAPhTjk+7NZAYN/Z3zsEUE3RkruDNgg=; b=oODJgyg+PcAV5DcxEdP8lJt+KD7zaC4plC84k6GfeDYwa7+7e8v8/AnUpUN+MDeVxx yUFAv92rA0g0fcxX/7Tdv+Ai+txHOFvRJoGfSBeTKQvAlL5feCLS+83O/RRqCLU4EVfz ux9BHqDSeLS81VlDa2H0r5zZWPAN2wxrPXDyweuiDu2F5KtMP39jxImit/Nhi6RS/7E6 GLAfPwFWjvdieM7HrhiA5mcqfSY7Q4XJUm+Uk2wbNcTYge9wOXPYRt4DlyNv3XlARH4a fqOSZcLeOLd7Xjkvk+VXnJXR+ePiJrn8r0/tQxy3tn1DiBV0IbYcxCrCOGDTlLw9iCJr y4mw== X-Gm-Message-State: AO0yUKVmKg1NHzOjZb99qFvDGV3Fc3UJtDL/2FVXeRYm2RdHqoCF6imT slEW94IGzl106Ps7WeVEU/DHb/kI73s= X-Received: by 2002:a17:90a:30d:b0:22c:932:2870 with SMTP id 13-20020a17090a030d00b0022c09322870mr5144511pje.33.1675314303647; Wed, 01 Feb 2023 21:05:03 -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.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 21:05:03 -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 3/4] perf lock contention: Support filters for different aggregation Date: Wed, 1 Feb 2023 21:04:54 -0800 Message-Id: <20230202050455.2187592-4-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?1756694715592037399?= X-GMAIL-MSGID: =?utf-8?q?1756694715592037399?= It'd be useful to filter other than the current aggregation mode. For example, users may want to see callstacks for specific locks only. Or they may want tasks from a certain callstack. The tracepoints already collected the information but it needs to check the condition again when processing the event. And it needs to change BPF to allow the key combinations. The lock contentions on 'rcu_state' spinlock can be monitored: $ sudo perf lock con -abv -L rcu_state sleep 1 ... contended total wait max wait avg wait type caller 4 151.39 us 62.57 us 37.85 us spinlock rcu_core+0xcb 0xffffffff81fd1666 _raw_spin_lock_irqsave+0x46 0xffffffff8172d76b rcu_core+0xcb 0xffffffff822000eb __softirqentry_text_start+0xeb 0xffffffff816a0ba9 __irq_exit_rcu+0xc9 0xffffffff81fc0112 sysvec_apic_timer_interrupt+0xa2 0xffffffff82000e46 asm_sysvec_apic_timer_interrupt+0x16 0xffffffff81d49f78 cpuidle_enter_state+0xd8 0xffffffff81d4a259 cpuidle_enter+0x29 1 30.21 us 30.21 us 30.21 us spinlock rcu_core+0xcb 0xffffffff81fd1666 _raw_spin_lock_irqsave+0x46 0xffffffff8172d76b rcu_core+0xcb 0xffffffff822000eb __softirqentry_text_start+0xeb 0xffffffff816a0ba9 __irq_exit_rcu+0xc9 0xffffffff81fc00c4 sysvec_apic_timer_interrupt+0x54 0xffffffff82000e46 asm_sysvec_apic_timer_interrupt+0x16 1 28.84 us 28.84 us 28.84 us spinlock rcu_accelerate_cbs_unlocked+0x40 0xffffffff81fd1c60 _raw_spin_lock+0x30 0xffffffff81728cf0 rcu_accelerate_cbs_unlocked+0x40 0xffffffff8172da82 rcu_core+0x3e2 0xffffffff822000eb __softirqentry_text_start+0xeb 0xffffffff816a0ba9 __irq_exit_rcu+0xc9 0xffffffff81fc0112 sysvec_apic_timer_interrupt+0xa2 0xffffffff82000e46 asm_sysvec_apic_timer_interrupt+0x16 0xffffffff81d49f78 cpuidle_enter_state+0xd8 ... To see tasks calling 'rcu_core' function: $ sudo perf lock con -abt -S rcu_core sleep 1 contended total wait max wait avg wait pid comm 19 23.46 us 2.21 us 1.23 us 0 swapper 2 18.37 us 17.01 us 9.19 us 2061859 ThreadPoolForeg 3 5.76 us 1.97 us 1.92 us 3909 pipewire-pulse 1 2.26 us 2.26 us 2.26 us 1809271 MediaSu~isor #2 1 1.97 us 1.97 us 1.97 us 1514882 Chrome_ChildIOT 1 987 ns 987 ns 987 ns 3740 pipewire-pulse Signed-off-by: Namhyung Kim --- tools/perf/builtin-lock.c | 75 +++++++++++-------- tools/perf/util/bpf_lock_contention.c | 44 +++++++---- .../perf/util/bpf_skel/lock_contention.bpf.c | 15 ++-- tools/perf/util/bpf_skel/lock_data.h | 4 +- tools/perf/util/lock-contention.h | 2 + 5 files changed, 89 insertions(+), 51 deletions(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 0593c6e636c6..0d11f301fd72 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -509,6 +509,34 @@ struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags) return NULL; } +bool match_callstack_filter(struct machine *machine, u64 *callstack) +{ + struct map *kmap; + struct symbol *sym; + u64 ip; + + if (list_empty(&callstack_filters)) + return true; + + for (int i = 0; i < max_stack_depth; i++) { + struct callstack_filter *filter; + + if (!callstack || !callstack[i]) + break; + + ip = callstack[i]; + sym = machine__find_kernel_symbol(machine, ip, &kmap); + if (sym == NULL) + continue; + + list_for_each_entry(filter, &callstack_filters, list) { + if (strstr(sym->name, filter->name)) + return true; + } + } + return false; +} + struct trace_lock_handler { /* it's used on CONFIG_LOCKDEP */ int (*acquire_event)(struct evsel *evsel, @@ -1070,12 +1098,6 @@ static int report_lock_contention_begin_event(struct evsel *evsel, ls = lock_stat_findnew(key, name, flags); if (!ls) return -ENOMEM; - - if (aggr_mode == LOCK_AGGR_CALLER && needs_callstack()) { - ls->callstack = get_callstack(sample, max_stack_depth); - if (ls->callstack == NULL) - return -ENOMEM; - } } if (filters.nr_types) { @@ -1106,6 +1128,22 @@ static int report_lock_contention_begin_event(struct evsel *evsel, return 0; } + if (needs_callstack()) { + u64 *callstack = get_callstack(sample, max_stack_depth); + if (callstack == NULL) + return -ENOMEM; + + if (!match_callstack_filter(machine, callstack)) { + free(callstack); + return 0; + } + + if (ls->callstack == NULL) + ls->callstack = callstack; + else + free(callstack); + } + ts = thread_stat_findnew(sample->tid); if (!ts) return -ENOMEM; @@ -1606,31 +1644,6 @@ static void print_contention_result(struct lock_contention *con) if (!st->wait_time_total) continue; - if (aggr_mode == LOCK_AGGR_CALLER && !list_empty(&callstack_filters)) { - struct map *kmap; - struct symbol *sym; - u64 ip; - - for (int i = 0; i < max_stack_depth; i++) { - struct callstack_filter *filter; - - if (!st->callstack || !st->callstack[i]) - break; - - ip = st->callstack[i]; - sym = machine__find_kernel_symbol(con->machine, ip, &kmap); - if (sym == NULL) - continue; - - list_for_each_entry(filter, &callstack_filters, list) { - if (strstr(sym->name, filter->name)) - goto found; - } - } - continue; - } - -found: list_for_each_entry(key, &lock_keys, list) { key->print(key, st); pr_info(" "); diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index c6f2db603d5a..3b4308b7b6bf 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -34,13 +34,15 @@ int lock_contention_prepare(struct lock_contention *con) bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries); bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries); - if (con->aggr_mode == LOCK_AGGR_TASK) { + if (con->aggr_mode == LOCK_AGGR_TASK) bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries); - bpf_map__set_max_entries(skel->maps.stacks, 1); - } else { + else bpf_map__set_max_entries(skel->maps.task_data, 1); + + if (con->save_callstack) bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries); - } + else + bpf_map__set_max_entries(skel->maps.stacks, 1); if (target__has_cpu(target)) ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus); @@ -146,6 +148,7 @@ int lock_contention_prepare(struct lock_contention *con) /* these don't work well if in the rodata section */ skel->bss->stack_skip = con->stack_skip; skel->bss->aggr_mode = con->aggr_mode; + skel->bss->needs_callstack = con->save_callstack; lock_contention_bpf__attach(skel); return 0; @@ -178,7 +181,7 @@ static const char *lock_contention_get_name(struct lock_contention *con, if (con->aggr_mode == LOCK_AGGR_TASK) { struct contention_task_data task; struct thread *t; - int pid = key->aggr_key; + int pid = key->pid; int task_fd = bpf_map__fd(skel->maps.task_data); /* do not update idle comm which contains CPU number */ @@ -192,7 +195,7 @@ static const char *lock_contention_get_name(struct lock_contention *con, } if (con->aggr_mode == LOCK_AGGR_ADDR) { - sym = machine__find_kernel_symbol(machine, key->aggr_key, &kmap); + sym = machine__find_kernel_symbol(machine, key->lock_addr, &kmap); if (sym) name = sym->name; return name; @@ -253,20 +256,35 @@ int lock_contention_read(struct lock_contention *con) prev_key = NULL; while (!bpf_map_get_next_key(fd, prev_key, &key)) { - s32 stack_id; + s64 ls_key; const char *name; /* to handle errors in the loop body */ err = -1; bpf_map_lookup_elem(fd, &key, &data); - if (con->save_callstack) { - stack_id = key.aggr_key; - bpf_map_lookup_elem(stack, &stack_id, stack_trace); + bpf_map_lookup_elem(stack, &key.stack_id, stack_trace); + + if (!match_callstack_filter(machine, stack_trace)) + goto next; } - st = lock_stat_find(key.aggr_key); + switch (con->aggr_mode) { + case LOCK_AGGR_CALLER: + ls_key = key.stack_id; + break; + case LOCK_AGGR_TASK: + ls_key = key.pid; + break; + case LOCK_AGGR_ADDR: + ls_key = key.lock_addr; + break; + default: + goto next; + } + + st = lock_stat_find(ls_key); if (st != NULL) { st->wait_time_total += data.total_time; if (st->wait_time_max < data.max_time) @@ -281,7 +299,7 @@ int lock_contention_read(struct lock_contention *con) } name = lock_contention_get_name(con, &key, stack_trace); - st = lock_stat_findnew(key.aggr_key, name, data.flags); + st = lock_stat_findnew(ls_key, name, data.flags); if (st == NULL) break; @@ -293,8 +311,6 @@ int lock_contention_read(struct lock_contention *con) if (data.count) st->avg_wait_time = data.total_time / data.count; - st->flags = data.flags; - if (con->save_callstack) { st->callstack = memdup(stack_trace, stack_size); if (st->callstack == NULL) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index ad0ca5d50557..7ce276ed987e 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -82,6 +82,7 @@ int has_cpu; int has_task; int has_type; int has_addr; +int needs_callstack; int stack_skip; /* determine the key of lock stat */ @@ -173,7 +174,7 @@ int contention_begin(u64 *ctx) pelem->lock = (__u64)ctx[0]; pelem->flags = (__u32)ctx[1]; - if (aggr_mode == LOCK_AGGR_CALLER) { + if (needs_callstack) { pelem->stack_id = bpf_get_stackid(ctx, &stacks, BPF_F_FAST_STACK_CMP | stack_skip); if (pelem->stack_id < 0) @@ -188,7 +189,7 @@ int contention_end(u64 *ctx) { __u32 pid; struct tstamp_data *pelem; - struct contention_key key; + struct contention_key key = {}; struct contention_data *data; __u64 duration; @@ -204,14 +205,18 @@ int contention_end(u64 *ctx) switch (aggr_mode) { case LOCK_AGGR_CALLER: - key.aggr_key = pelem->stack_id; + key.stack_id = pelem->stack_id; break; case LOCK_AGGR_TASK: - key.aggr_key = pid; + key.pid = pid; update_task_data(pid); + if (needs_callstack) + key.stack_id = pelem->stack_id; break; case LOCK_AGGR_ADDR: - key.aggr_key = pelem->lock; + key.lock_addr = pelem->lock; + if (needs_callstack) + key.stack_id = pelem->stack_id; break; default: /* should not happen */ diff --git a/tools/perf/util/bpf_skel/lock_data.h b/tools/perf/util/bpf_skel/lock_data.h index ce71cf1a7e1e..3d35fd4407ac 100644 --- a/tools/perf/util/bpf_skel/lock_data.h +++ b/tools/perf/util/bpf_skel/lock_data.h @@ -4,7 +4,9 @@ #define UTIL_BPF_SKEL_LOCK_DATA_H struct contention_key { - u64 aggr_key; /* can be stack_id, pid or lock addr */ + u32 stack_id; + u32 pid; + u64 lock_addr; }; #define TASK_COMM_LEN 16 diff --git a/tools/perf/util/lock-contention.h b/tools/perf/util/lock-contention.h index 39d5bfc77f4e..e5fc036108ec 100644 --- a/tools/perf/util/lock-contention.h +++ b/tools/perf/util/lock-contention.h @@ -68,6 +68,8 @@ struct lock_stat { struct lock_stat *lock_stat_find(u64 addr); struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags); +bool match_callstack_filter(struct machine *machine, u64 *callstack); + /* * struct lock_seq_stat: * Place to put on state of one lock sequence From patchwork Thu Feb 2 05:04:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 51691 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp51252wrn; Wed, 1 Feb 2023 21:25:47 -0800 (PST) X-Google-Smtp-Source: AK7set/sUAJMxbYSZBKv4Xmf1ySNBguN8E0GhcO908fwFe3qnlABUwOJ9M/BCE7DBDk1CdkvI+Fq X-Received: by 2002:a62:3003:0:b0:593:e59c:3d2e with SMTP id w3-20020a623003000000b00593e59c3d2emr3924518pfw.16.1675315546707; Wed, 01 Feb 2023 21:25:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675315546; cv=none; d=google.com; s=arc-20160816; b=rcWJL+HbZV+aay7qfMnGliJSG1KRSi7WSGSn+5F6DEBWfow+OC9BfA0XeaEJyUj6S0 V4uxkUj9WqjslGiAWi0oGO5MrFN9nAmKPpAaWYI3uPgpTjvqJPoD8lFaZNgPFArZcWce +uP/ABV2lafn+bDYPzLEMMoVZXtD5fzRIJ2b1L5hJS78Dq/puXnJdoeU7Q6asYsh92bL /Gb6iSBwqx1B+iCWyWuvIfHmVnMQckpOq+FrwjV9zXk3iH6RuWmwEb3tBXYm4LS/nRhN tm5zY7Xi7iHkYMA8Icxtl9sskz2KNmSYXsIoVYAR/dZV14xnObNupA1oSlg5XSC9yArE ni5Q== 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=N/28sWI3Oy4sxzP6YrpEXjm5TO0C88CC0iz6gGXgbCM=; b=UyJpF7+U7MrrNvCutBvMz1A3eKX7WxHCTmLGVb1GR+/DFO3cVMq1JnS+gJ5/fBPiGf SzVyO7FX01kictw5/VKuxRDNudP3BceaHnsVvfbSHJVQzc4Z1k4HpiYRHW8ZA+et4WFG 0dHC6z2DjrojH4fqokkXYrHqeHHmxfm4KY4riL015Join7s6PIFWROoTlHyiMV2GclNn b26C3cgzsLkcv6fb+UmNj6AwDUzBdnxPzKQqHvHbMDiJaAuHAJy1sBHZGHo+l1bKZ7x5 JS8Pg2u4dMDKQEruWc2DbtF/KDepYq6LafOp09dMAO35TdEziHSpuYSe/Ssfct9hL8Lf CRTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JZvW81C0; 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 138-20020a621790000000b0058de3cfaa71si20912599pfx.11.2023.02.01.21.25.33; Wed, 01 Feb 2023 21:25: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=JZvW81C0; 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 S231888AbjBBFFX (ORCPT + 99 others); Thu, 2 Feb 2023 00:05:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231761AbjBBFFS (ORCPT ); Thu, 2 Feb 2023 00:05:18 -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 0889A70D54; Wed, 1 Feb 2023 21:05:06 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id e6so707105plg.12; Wed, 01 Feb 2023 21:05:06 -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=N/28sWI3Oy4sxzP6YrpEXjm5TO0C88CC0iz6gGXgbCM=; b=JZvW81C0nvW21mZLuJzI2vUccKQysLsAAekhbxbvRpum5MyZYoWbc8gKbKWXUyEnKZ 12VPfbZVGiC3m5/igWxUCwXjyG66Ecw2UclNd5vL5jl1yij0FEypkZvRXe1LxTeZmhH6 cnPySkHk/dbvf99zoQfgn/qb8w2Q8rml2IeD4eGLBsg+IGYqGIIyaWZI2ojUsQcvOXWM PI3wP5/UCUL+oQHzb/YpQo+MraTLs//ZdTCwaquW/fDwznY/xPRUExA0q2wQitnrwq3b k3wnHJblDzhA8WVdilwdgkfZwQ6tBbc20Nq3A4cFAq3rD9MGRQ0YiHImH/luWuc+g9ap 4KPQ== 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=N/28sWI3Oy4sxzP6YrpEXjm5TO0C88CC0iz6gGXgbCM=; b=jdlHqgn2S6DgPUsNEg8+rIt88GklU35J3qXAe6W7VXU8tWKzBkPdwOAaBpB/iFId8b +2zgcSKPcNCkyn7diwcjcFskivGNg/mhZ0b0sb+Xqth04S/LqEIbPk9t23EoS9m5dPZR FuO0vvE95iA7rweG8Vb7fWaWKUM4/zgrgAXCd5REOL3XflntbNKlcy3etp8QsVbxzuzp NZhPvMzXo2/NnQySnfYCXnWfqjGvFS0iIwbmLVQi43/HkvRDrCsJVcfqZXGSEkCX4gTD tfEy92rEbsrdWEmehErNUDeG5/P42a0ZLdmAUU8QFiXuOra449RbQKc4qWGv3ZlZz/Mm 3waw== X-Gm-Message-State: AO0yUKUisPOcE14kQh5UNTWjWJv74iFPyRe1aLuiaHzfJ0E7o5ugan/r MUasVNWrsHhCvH3xq5xLbzs= X-Received: by 2002:a17:90b:4c81:b0:22c:69b6:2be8 with SMTP id my1-20020a17090b4c8100b0022c69b62be8mr5360687pjb.8.1675314305316; Wed, 01 Feb 2023 21:05:05 -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.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 21:05:04 -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 4/4] perf test: Add more test cases for perf lock contention Date: Wed, 1 Feb 2023 21:04:55 -0800 Message-Id: <20230202050455.2187592-5-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?1756695675009087964?= X-GMAIL-MSGID: =?utf-8?q?1756695675009087964?= Check callstack filter with two different aggregation mode. $ sudo ./perf test -v contention 88: kernel lock contention analysis test : --- start --- test child forked, pid 83416 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 (w/ spinlock) Testing perf lock contention --lock-filter (w/ tasklist_lock) Testing perf lock contention --callstack-filter (w/ unix_stream) Testing perf lock contention --callstack-filter with task aggregation test child finished with 0 ---- end ---- kernel lock contention analysis test: Ok Signed-off-by: Namhyung Kim --- tools/perf/tests/shell/lock_contention.sh | 66 +++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/tools/perf/tests/shell/lock_contention.sh b/tools/perf/tests/shell/lock_contention.sh index b05f1b1ca6c8..be5fcafb26aa 100755 --- a/tools/perf/tests/shell/lock_contention.sh +++ b/tools/perf/tests/shell/lock_contention.sh @@ -128,7 +128,7 @@ 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}") + echo "[Fail] Recorded result should not have non-spinlocks:" $(cat "${result}") err=1 exit fi @@ -139,7 +139,7 @@ test_type_filter() 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}") + echo "[Fail] BPF result should not have non-spinlocks:" $(cat "${result}") err=1 exit fi @@ -160,7 +160,7 @@ test_lock_filter() 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}") + echo "[Fail] Recorded result should not have non-${type} locks:" $(cat "${result}") err=1 exit fi @@ -171,7 +171,63 @@ test_lock_filter() 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}") + echo "[Fail] BPF result should not have non-${type} locks:" $(cat "${result}") + err=1 + exit + fi +} + +test_stack_filter() +{ + echo "Testing perf lock contention --callstack-filter (w/ unix_stream)" + perf lock contention -i ${perfdata} -v -q 2> ${result} + if [ $(grep -c unix_stream "${result}") == "0" ]; then + echo "[Skip] Could not find 'unix_stream'" + return + fi + + perf lock contention -i ${perfdata} -E 1 -S unix_stream -q 2> ${result} + if [ $(cat "${result}" | wc -l) != "1" ]; then + echo "[Fail] Recorded result should have a lock from unix_stream:" $(cat "${result}") + err=1 + exit + fi + + if ! perf lock con -b true > /dev/null 2>&1 ; then + return + fi + + perf lock con -a -b -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result} + if [ $(cat "${result}" | wc -l) != "1" ]; then + echo "[Fail] BPF result should have a lock from unix_stream:" $(cat "${result}") + err=1 + exit + fi +} + +test_aggr_task_stack_filter() +{ + echo "Testing perf lock contention --callstack-filter with task aggregation" + perf lock contention -i ${perfdata} -v -q 2> ${result} + if [ $(grep -c unix_stream "${result}") == "0" ]; then + echo "[Skip] Could not find 'unix_stream'" + return + fi + + perf lock contention -i ${perfdata} -t -E 1 -S unix_stream -q 2> ${result} + if [ $(cat "${result}" | wc -l) != "1" ]; then + echo "[Fail] Recorded result should have a task from unix_stream:" $(cat "${result}") + err=1 + exit + fi + + if ! perf lock con -b true > /dev/null 2>&1 ; then + return + fi + + perf lock con -a -b -t -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result} + if [ $(cat "${result}" | wc -l) != "1" ]; then + echo "[Fail] BPF result should have a task from unix_stream:" $(cat "${result}") err=1 exit fi @@ -186,5 +242,7 @@ test_aggr_task test_aggr_addr test_type_filter test_lock_filter +test_stack_filter +test_aggr_task_stack_filter exit ${err}