From patchwork Fri Feb 3 02:13:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 52263 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp596151wrn; Thu, 2 Feb 2023 18:17:50 -0800 (PST) X-Google-Smtp-Source: AK7set8q5yHLcIiwUW4+ZYm2yw1F9hpbH8FuMJmRyyt7JLlfv1y9PA4K1Iz68nRQuNmwP5M7dCLq X-Received: by 2002:a17:902:c40f:b0:196:37db:b4cb with SMTP id k15-20020a170902c40f00b0019637dbb4cbmr10763737plk.55.1675390670321; Thu, 02 Feb 2023 18:17:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675390670; cv=none; d=google.com; s=arc-20160816; b=vRVDQRvdBS7S+tWnIZmcZ6X/E0ynAD7pAmnP8b90iOHzJ4ZZYmmNZnOlvXF1wmpvGC frkHORZwcDjRa08Dy7wallEV7MA99ZYeUxc5ZlrO65dqhnOvDpAGXttqL8nmWl7KdFCc m15XaJ/o0k5gHSYDWYLNUGoiKVc/orU8HCmtGDoBiK82+gUbnqKPeeY/+H0f6YMHvZuT ShalT1Db3r55s6kNiO/Qg/+RRh1YygL8/SE+KlMo8BePRgenwtwIDXRmmIp1YC7EZQLE es10TgslMZ2nIQxUwVrSG1x++6K06OPZNtpMA9Nk0Pt9sr6HhiTPjaeQs8ll/bZXZxL3 rjlg== 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=vVb6/wY5BO86ABIxc8pLAp0Fww2Vgk9VT30AEyLAIxo=; b=G2+7+vpRu3KlsasxLhasRenJFjOc3oGuFFip2R5VkgYS3QKloBYgI+92Cgi7pPL+sg xOqNRJtHzdzNZx/dUi4gBVMbV06h588sEnXN8rHSLE7Ukj1PbtGqQWloh5L1Gew5lx7a jlYhIcNzlX0LhXSr9ceyNykl9pvazBkksVqxKCWWE5wpGBxxGLfDmZTzjvsKKUAd3uMH KVPyQWOdo4WWhtdCmFpooMAOWN6Sk2A85en68IGvjtXRNY3FzC6AWP5gWMZ0Lj60KWZU i4Gp2hTQVrGQyrIndA09jHmT8/hdc9rJvxb1Z8z6SqYM0zAITZUrhErsJMOxpekbVEZY dg4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KwzAhqGo; 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 e12-20020a170902e0cc00b00198a6eb26bdsi846981pla.389.2023.02.02.18.17.38; Thu, 02 Feb 2023 18:17:50 -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=KwzAhqGo; 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 S231766AbjBCCNe (ORCPT + 99 others); Thu, 2 Feb 2023 21:13:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231465AbjBCCNb (ORCPT ); Thu, 2 Feb 2023 21:13:31 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C70C21290; Thu, 2 Feb 2023 18:13:30 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id t12-20020a17090aae0c00b00229f4cff534so6918907pjq.1; Thu, 02 Feb 2023 18:13:30 -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=vVb6/wY5BO86ABIxc8pLAp0Fww2Vgk9VT30AEyLAIxo=; b=KwzAhqGoIxk/QgLhdRBq68ZGuM3GsY8mmRu3zZkE72wvSJgiW7HVIR5DLr5bsVNXgS l3/v0/PUF7kihPB+DkLf0oKJRwoBOxI4Fqmfkjre1f59fjOjkn6QnDbCxWXnulFYa53t IOHkq0NU4tX7T0pZFPih49XcawI0iwRtRsmSugKjBE6eH3kHINggzOOevDM41o9p5pbX FCSHWoFW9AcwrFJbZBHonkbmIhRZexqklRx5BJpdc99pHimcQBpcUUD7Z9P3j+nv9c/V bc7ToLj1fzUA0kHMOcVaVxiK1oW+WV1yINawg3USoBG/2/Ud7geReHVJEtzRO4o9sIa0 xsTA== 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=vVb6/wY5BO86ABIxc8pLAp0Fww2Vgk9VT30AEyLAIxo=; b=oNrYOPLqZJ2ZeMHkGae1XtXR57AK8rQ6TAT+MWFufxaWkwT18MOvsV5Mveervdx6pi 4rk2H4MmaOwC+nYDiIrZUH1dIekdXt2DzWPR5CWTMDZ0HD6f2uKsJ8Q41O6eG5oqQtcU JNwyv8Ael6X1Mk0jI385OaV2S0RD8TO8GrW88uUsqIBEImD/uJVkxrTqP73gNenzIng5 IxQObkhYUEF+33q8yJ1I09Y+R9iMyJXY2rd2HqXUhrAsnDptOcskc5PrTqJr+PqGL/qa tmakfJtjUX2zRlEPDQCNQFmGxaruXajxoDNN8PCZdkIAOEj7r9YM+Zc72vYbBEkFtx1o S6Yg== X-Gm-Message-State: AO0yUKW1D2jHHfDeUsvs/o4PUXTzChr4brMLRATZnMIl0vOgv4W7Wk4L C4Lq1cBtb+gvGR0U0vlGtZI= X-Received: by 2002:a17:90b:38cc:b0:22c:61c:2d5b with SMTP id nn12-20020a17090b38cc00b0022c061c2d5bmr8857231pjb.20.1675390409931; Thu, 02 Feb 2023 18:13:29 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 18:13:29 -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 1/3] perf lock contention: Factor out lock_contention_get_name() Date: Thu, 2 Feb 2023 18:13:22 -0800 Message-Id: <20230203021324.143540-2-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?1756774447309007845?= X-GMAIL-MSGID: =?utf-8?q?1756774447309007845?= 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 | 115 +++++++++++++++----------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 4902ac331f41..286e52ba9f91 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -163,9 +163,70 @@ 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; + 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) { + struct thread *t = __machine__findnew_thread(machine, /*pid=*/-1, pid); + + if (t == NULL) + return name; + if (!bpf_map_lookup_elem(task_fd, &pid, &task) && + 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 +236,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 +255,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 +276,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 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 From patchwork Fri Feb 3 02:13:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 52265 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp599481wrn; Thu, 2 Feb 2023 18:29:14 -0800 (PST) X-Google-Smtp-Source: AK7set+4V4Svma47MAsHDxxQMoJr2O6vPc5J4cEdJqVagjIJ16EtJ/dhBzUCTyK+xfAJmjJNEl3z X-Received: by 2002:a05:6402:345a:b0:499:c343:30e5 with SMTP id l26-20020a056402345a00b00499c34330e5mr9251178edc.3.1675391354040; Thu, 02 Feb 2023 18:29:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675391354; cv=none; d=google.com; s=arc-20160816; b=xAad1gm4asPF9YxBnQNHMslZ9NGqakRu+RVWvfqzNWQpPt9UnWlorjYU5nFA6pCusm G2cJ0aUvhYJvuunHfgeIOz4Ouhv8/Zw4w6ykISCao0Qsyf3ezZJRCOfPAXaUFHd+S63n 1w+u3WYAgDuenbOjXUJ928KYf0KqgQ/tMaeY0zbQwoPOndS9/u/O3WaoVFSgSyxeepET lAzPEjOlWlGe6QPqbEbuQlGfDL/g+0X/P1C3IbM1nqaaE5PRsqlQKzYdn0kjhYcj/qJr qTGFzl1hJaY9ymHgsRHQSwLywoZCPrt7yUOznwgBCQgX/PM3FcmOKpQtR6yaxybf2oD3 IXbw== 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=qsl4zDTK13ujlyXZ/hVCCD+IHyADuxL7e1KSOZYRAoo=; b=Ebq9V2Vzo0GcPRl0xJer7MD6uMIjIDQ2oKhud43ynUytdmWqLxMhu61ckhz4hD/yDS GI4vSu7nrns9E/+6/J57ack3ZYPA0Rp84j9SxutY+XC6nP3mhjtwy3qNIjWHrygxLQVy rkqgmiir7SVgO4ILlIudkv+ugl3dwHgWWVMPnCzW/v9PNtYK56St1S6vUJ3TUligS1xD HyrzYQmgA7g3eiGP+AHix2XVTrSPsvI7+Zux4ffeM84zGN6t5ZoqPc3N7R8JBA7X6uMm lmHKNvaYKoErjGg/FfSE692W4fijWD/B86Ap7dkTMd0zMZPNVWrY43KFtAR1xO+eXlfB gTrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=VpkePtr6; 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 q10-20020a056402518a00b004a2336212d1si1441508edd.309.2023.02.02.18.28.47; Thu, 02 Feb 2023 18:29:14 -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=VpkePtr6; 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 S232124AbjBCCNq (ORCPT + 99 others); Thu, 2 Feb 2023 21:13:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230454AbjBCCNk (ORCPT ); Thu, 2 Feb 2023 21:13:40 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C572269B3A; Thu, 2 Feb 2023 18:13:33 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id m11so3732925pji.0; Thu, 02 Feb 2023 18:13:33 -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=qsl4zDTK13ujlyXZ/hVCCD+IHyADuxL7e1KSOZYRAoo=; b=VpkePtr6jXYhrKCDhIdHwErNGl00IdgqWRUnhHknzgrtMmr9S4oMyYAKJPXppD2s7B jKl9A5Re5YZYkOQlI+YgMDFG66s6BciBUMSZPwXo98LiP1D4TqFl/qVMyD6aCN9pOAtm Uiv/T70K6+w3pAhNSTvBs71ABTj4d4H2L4EqaDBnrwnqrSnW3yYAGMjwQ1Hr1tOv/On8 /rPKoXEwAfKJTNs0DF3/w63TXNgifgRt4nUd57ZQYTSzcDt0lZ/7i1Zb/6m9/VPglu/p tMZ4lfTUeHBQn02BxuzKKoYD5R1OY3Mvyan1xkiVoWEng8o9BY9qmnKfnptAmcVflJAg JHGw== 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=qsl4zDTK13ujlyXZ/hVCCD+IHyADuxL7e1KSOZYRAoo=; b=nT4oxfV656m2iNI7dbd8jrAf16g5sbqQUzs99ylHADcWYSjg/hlyKPy2ofKPJxCg1z 56OpZS5zKyTCaSG6ScVmzQpAXMQ3UVedXIDGBDQcBYdt4ECwPYtrShRvOsS/YaBYz3L9 pkJVE9rB8Qltr7nnsXScYYtxtCg2u6ra/EDpKSdHqyHpHIPqLFYtpbHnOO0aYF/ux2Zu pCKnhXGlTLfYoS3wx/R/hH90wTXwKWrFYNDyqFfU38S83uO39MN4+/FRhmLX3lP9e7l3 2kS6EhgNJ7aDeUEfmojnCz9kUX6yz0cVSwVJEkFN95JseVLaz+iW31nT7LIqXW96Lan/ jhsQ== X-Gm-Message-State: AO0yUKXCg1FaF8T3Q21cFmWes2zUEeW2C8ktgrQjokF7miek9MAqZGln VOT8RBhbqx7HyfOTFukpatg= X-Received: by 2002:a17:90b:1b52:b0:22b:f93b:b5ae with SMTP id nv18-20020a17090b1b5200b0022bf93bb5aemr8329918pjb.46.1675390413082; Thu, 02 Feb 2023 18:13:33 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 18:13:32 -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 3/3] perf lock contention: Support filters for different aggregation Date: Thu, 2 Feb 2023 18:13:24 -0800 Message-Id: <20230203021324.143540-4-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?1756775164404035259?= X-GMAIL-MSGID: =?utf-8?q?1756775164404035259?= 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 ead2898ba377..72cf81114982 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; @@ -177,7 +180,7 @@ static const char *lock_contention_get_name(struct lock_contention *con, if (con->aggr_mode == LOCK_AGGR_TASK) { struct contention_task_data task; - 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 */ @@ -194,7 +197,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; @@ -255,20 +258,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) @@ -283,7 +301,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; @@ -295,8 +313,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