From patchwork Tue Oct 24 22:23:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157714 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2236960vqx; Tue, 24 Oct 2023 15:24:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3cJNUd0w+FAqeuy5TSxKgtJrn0YrQMcVqonX+dSXIP6lj4H4vxTEOOtQwPiZHvsA3wYr7 X-Received: by 2002:a05:6a21:9993:b0:171:a8bc:74b2 with SMTP id ve19-20020a056a21999300b00171a8bc74b2mr4303645pzb.7.1698186285667; Tue, 24 Oct 2023 15:24:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186285; cv=none; d=google.com; s=arc-20160816; b=ZKrAfRYanctyDo2dGiEUyWWxv6vSppjCb1zKMm6B1nOOdvPPNCCbSSQmGzrFI3YqVr pvY2Odzs3ag0G9Dwja4DCGEz0IRe3qcXu9KCJdIbjKhFJiV8vTAJyJIr+seqo+2MdhjK uQQQQzRC0pTaTpQEmTR1oRWS1nCrXSawAbvY98F7Axl0ACkBTEZZa9gQgc3EeEZOFYnv S5ssGZC2uTVrzNGJJxcxtSmdQgdnU+AJN7KlPiq5bCQPpDNx/PWEcNbJhZxYvJ/VuTk6 Vos5xIaycd3/6L2+chJew0xm6XmMUkMEYQOD9Q0R0k/YhP8OyPWhWo/p3Hdrl5hJmywu 0gqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=NM9dcQFM3UJZ90iyVMmsCAaNS1wz3u9iN52BCuG9Qew=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=vyRNRv11+OwjVDl06oVY73SaRI8fzZaUvGWis/v0f+Hlo7TwfVGDkUtWazPDlHllMK UZreL0geWIe4evNt97kyWjrt1MePZGm8WYEBBqyY+LUyQE+ufSOsnc9l3fyLDUWpn5ru 7kOGHppGuT+2KKE4QCsZsxV1AVVG0l0UALQkA6f+lu0r8bZp+NRrCj29DQ6UrTgQGueH lzl1yA0EhjdpxMthy8V6DZBawa/B1sNv4B/Uqup7CWmQXcXyqOl+tAvjAMtJDSLTaBeZ cbFGKPTa074fVzNJdUZOBzXuWey/oQ3rbHeK860+tnpWPQbMO81Zof6OkOQJ3FYRrkUb kvRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=meDc3XHp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id h4-20020a17090aa88400b002774985e8b7si11806176pjq.168.2023.10.24.15.24.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:24:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=meDc3XHp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 997F9802B01D; Tue, 24 Oct 2023 15:24:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344436AbjJXWYV (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234958AbjJXWYS (ORCPT ); Tue, 24 Oct 2023 18:24:18 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FD5B10D1 for ; Tue, 24 Oct 2023 15:24:16 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9cafa90160so5745255276.2 for ; Tue, 24 Oct 2023 15:24:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186255; x=1698791055; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=NM9dcQFM3UJZ90iyVMmsCAaNS1wz3u9iN52BCuG9Qew=; b=meDc3XHpY9ExaB280nqOJ6K12OSUwce6tf+IECrl3phpC11mlm2lK0RtXg1hlcetqQ IARFzopmnj2l0M1LYYdylBWVYy8/qq2za77E8GuduEiFUQJyM9cjX9GO45Fiil3YMs6Y GY3R2xmNPk5JnsdAhz+XU2mGcSN++OBrYzAvjbRZU5i9w7OSu5mV/7UmwQRYvderROeH qFtLHtmi3fAE89TaP/eI9TrrMZosgnEr2XPChozMnTtw2Q2rXVkY0eIs5kE5k/VjROEs M4j+fFEFoz23hcJILF8zpJj/P9hNWAXbMOqtnsHCx7dDkufNWU8GmNTqVk1gMtn7S+Q1 LfIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186255; x=1698791055; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NM9dcQFM3UJZ90iyVMmsCAaNS1wz3u9iN52BCuG9Qew=; b=nqnVu+0weadMj83l6jmG+VzU0F3NLIF+un41zNGbA4a78a/6pnh1c5DBA0D7lIJpnY fVJswvGlfXaQUPMwwmc8wvXC1ObSffGttzXXhRfoltekHb0k3HsXAEjbQjyS88T9+/Zr GcLNUVuvIb9MlSiT3oAsA+6sgBCyyoyWfZiubjmwA+E4PazPXYMhYm6S7SBf8Wel+PjG rZZWYhqxkGUIO0Q+m0JmoFR3DOCsRW4pApBUFHnJR8smhcVSf7a6+0gqjGvw9Egb2JMP QVu4oqRqT0uVHvEtnKoWDXejNdvUplPEQg/4NS9boJbKSs9CfFjlIvPoa+yLcdpbPh74 A6YA== X-Gm-Message-State: AOJu0Yw+m51ZQmHMjVI5+zkXw93cKuzH1j1ydOYjiQh7Kef27QpD5p/1 DteBeSVoEfds8KM8iuYvfS5rewQZ9qtw X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:d107:0:b0:d9a:50d2:a8ba with SMTP id i7-20020a25d107000000b00d9a50d2a8bamr250692ybg.2.1698186255187; Tue, 24 Oct 2023 15:24:15 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:04 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-2-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 01/50] perf rwsem: Add debug mode that uses a mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:24:40 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677382999253715 X-GMAIL-MSGID: 1780677382999253715 Mutex error check will capture trying to take the lock recursively and other problems that rwlock won't. At the expense of concurrency, adda debug mode that uses a mutex in place of a rwsem. Signed-off-by: Ian Rogers --- tools/perf/util/rwsem.c | 34 ++++++++++++++++++++++++++++++++++ tools/perf/util/rwsem.h | 11 +++++++++++ 2 files changed, 45 insertions(+) diff --git a/tools/perf/util/rwsem.c b/tools/perf/util/rwsem.c index f3d29d8ddc99..5109167f27f7 100644 --- a/tools/perf/util/rwsem.c +++ b/tools/perf/util/rwsem.c @@ -2,32 +2,66 @@ #include "util.h" #include "rwsem.h" +#if RWS_ERRORCHECK +#include "mutex.h" +#endif + int init_rwsem(struct rw_semaphore *sem) { +#if RWS_ERRORCHECK + mutex_init(&sem->mtx); + return 0; +#else return pthread_rwlock_init(&sem->lock, NULL); +#endif } int exit_rwsem(struct rw_semaphore *sem) { +#if RWS_ERRORCHECK + mutex_destroy(&sem->mtx); + return 0; +#else return pthread_rwlock_destroy(&sem->lock); +#endif } int down_read(struct rw_semaphore *sem) { +#if RWS_ERRORCHECK + mutex_lock(&sem->mtx); + return 0; +#else return perf_singlethreaded ? 0 : pthread_rwlock_rdlock(&sem->lock); +#endif } int up_read(struct rw_semaphore *sem) { +#if RWS_ERRORCHECK + mutex_unlock(&sem->mtx); + return 0; +#else return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock); +#endif } int down_write(struct rw_semaphore *sem) { +#if RWS_ERRORCHECK + mutex_lock(&sem->mtx); + return 0; +#else return perf_singlethreaded ? 0 : pthread_rwlock_wrlock(&sem->lock); +#endif } int up_write(struct rw_semaphore *sem) { +#if RWS_ERRORCHECK + mutex_unlock(&sem->mtx); + return 0; +#else return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock); +#endif } diff --git a/tools/perf/util/rwsem.h b/tools/perf/util/rwsem.h index 94565ad4d494..ef5cbc31d967 100644 --- a/tools/perf/util/rwsem.h +++ b/tools/perf/util/rwsem.h @@ -2,9 +2,20 @@ #define _PERF_RWSEM_H #include +#include "mutex.h" + +/* + * Mutexes have additional error checking. Enable to use a mutex rather than a + * rwlock for debugging. + */ +#define RWS_ERRORCHECK 0 struct rw_semaphore { +#if RWS_ERRORCHECK + struct mutex mtx; +#else pthread_rwlock_t lock; +#endif }; int init_rwsem(struct rw_semaphore *sem); From patchwork Tue Oct 24 22:23:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157718 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237117vqx; Tue, 24 Oct 2023 15:25:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGPQC/Qsh/tn405OYIdsZ0iFSK5c5VjuiRIEH3bECE+AaX7nk/o9GYcV1xz7aB4uEm/lAye X-Received: by 2002:a05:6a21:71c7:b0:13d:1d14:6693 with SMTP id ay7-20020a056a2171c700b0013d1d146693mr3481910pzc.45.1698186310585; Tue, 24 Oct 2023 15:25:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186310; cv=none; d=google.com; s=arc-20160816; b=gn8IwsTZNccvhFjjLj4LGcS1aiO2dBURbR+IzDpxAzl41T3MXtnZbxckQGjmHv5kKI QEG+OthIQPVdm3Ba4/FzopytxPa6eRygW+2kiP1JzgmFYJOAnrWXbqmY8V/fXCQCfcmz X46G2D0t5B2FMx9hBprGf42JL+e+efTEoy6bQCtDrHkG2J4beaIt93ti2f5GZEIkKzp2 M43eupxbgX1PKafjVczQRuiAlN17stLp6B838Fl+PxRTvdmIBQyTIXXlU4FhR/U5OxI6 6t3b8lWaDoTmO1j8qgVTzitn634FQT6Me4lJ2LiG7DJvZMOfPZhFJGk6fR1qkBNp4Jcq 1LZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=soqORP6tbK7HSPMdiExWiSFnufIqC4NXz+eAMSjew4k=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=uP1Gj1jo6OfWasXlL19cijcHECfRWs93ftfWWCvTQBVHOwg+94IevQ9kNwyb1F1Rx6 t6dwfTxxvXlaoSyWJCSGHTOxDL8zCYcl21LbQXkKsEMLUvKfX1V63JTeun7oBxurMfS7 oHoiBy8faYyMOxgsi0WbN58JhhW06xBeJ95SbyO2nG/T4zeIQr9GgKyJ4KhsbgPAiH/0 lRJD7Gu4orlGdN8wB5eaLV9Q0V6029c49UxBkJ2GZnqvRRzYi4QrIdOHE2BsWhBvr2UV S+V4Nlux+kV948bj3dCIPn6QAx7F/+TxdEaJKBkSBlAQ5tpOFjj37SpVlEh5Jtg9lQyR 8Y5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VAYKbecQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id by17-20020a056a02059100b005702257f32esi9691338pgb.185.2023.10.24.15.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VAYKbecQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id C9A9880BEF22; Tue, 24 Oct 2023 15:25:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344434AbjJXWYY (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344431AbjJXWYU (ORCPT ); Tue, 24 Oct 2023 18:24:20 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 490E010CB for ; Tue, 24 Oct 2023 15:24:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da040c021aeso1140818276.3 for ; Tue, 24 Oct 2023 15:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186257; x=1698791057; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=soqORP6tbK7HSPMdiExWiSFnufIqC4NXz+eAMSjew4k=; b=VAYKbecQNlVTg/B52zPDUaaiURDyjiKy7S+6RIJOxJpJ0eHhouwdTSRRBJ27EJULRu dxqgDSMBuegaaEh2V9wyGFRlyBqDnas82lSaTbvD97QErmN4VbCrwd0kjJRS7TO3x9FG tVNhfEvHEB2Z/CQ6oMCpKUWDBVFqU090UkqLf+jv2vfjskvOUE8FlHbCKTT2mZsnQ+CN kegQ5T4b6AhmuMBRT431gSOe7l5kezCOzc/yHc/ZJmidQMGqyLqsQAIlmn4DT6WXgMhg osWbRexaBrpnCVGgOf22q7eEulXwowZ8Lmy7vwtcI9C+cz0kdMMFoWER/jYCV8iUL5cC 4KLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186257; x=1698791057; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=soqORP6tbK7HSPMdiExWiSFnufIqC4NXz+eAMSjew4k=; b=sleI/+GYK6CKPllGJMxQ6AGn6XD0RtnpwbU9MtV8TxctYRR+gYO/0sCv64cjDnbHJG q/DdNpSJxKKrN/Qb9WzLtbnELFZrzPRixKa7VYrgKzn0kTCnYlteQ80SVlkvvd9UT1qu IXUCMmP/sJwUTVN2Tqgrz7GRGWRs3Bhy2kmTWlNRthv4NdVZjSmYym1/ivzbyUvmw+Il rpQYIoL8VgBZ0ZQ3zYUd4loBgSt9D/tVBQ38/Z/tN27E+4JeCy7PzGwplp++6gKHdfBC s68wspFjR7R+RVL5TLlVNCK0m0bUlgVcm22AY3w4U1P+sYLaqBFNILhcIvA0Z7DTFzUP 9Law== X-Gm-Message-State: AOJu0YyFE3VJTigjuMj6UgYGtr41NybZjrb6Xqw/nh5aoT7oNMCafwpS GuKtHNJTv1e4hf7a8Tq5VOgA7dJ8rPZS X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:324b:0:b0:d9b:f88c:ca2b with SMTP id y72-20020a25324b000000b00d9bf88cca2bmr258271yby.11.1698186257401; Tue, 24 Oct 2023 15:24:17 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:05 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-3-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 02/50] perf machine: Avoid out of bounds LBR memory read From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:03 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677408473636311 X-GMAIL-MSGID: 1780677408473636311 Running perf top with address sanitizer and "--call-graph=lbr" fails due to reading sample 0 when no samples exist. Add a guard to prevent this. Fixes: e2b23483eb1d ("perf machine: Factor out lbr_callchain_add_lbr_ip()") Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index addfae2f63ef..e0e2c4a943e4 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2622,16 +2622,18 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread, save_lbr_cursor_node(thread, cursor, i); } - /* Add LBR ip from first entries.to */ - ip = entries[0].to; - flags = &entries[0].flags; - *branch_from = entries[0].from; - err = add_callchain_ip(thread, cursor, parent, - root_al, &cpumode, ip, - true, flags, NULL, - *branch_from); - if (err) - return err; + if (lbr_nr > 0) { + /* Add LBR ip from first entries.to */ + ip = entries[0].to; + flags = &entries[0].flags; + *branch_from = entries[0].from; + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + true, flags, NULL, + *branch_from); + if (err) + return err; + } return 0; } From patchwork Tue Oct 24 22:23:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157721 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237222vqx; Tue, 24 Oct 2023 15:25:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEzSZUIvKS7mVoZ3bsWK2T1+Ww4mwoACQHkWdnwFkmrNxR7v/6RFHYhlSoOq7XK5iCN54Cn X-Received: by 2002:a05:6358:c9e:b0:168:e7aa:596c with SMTP id o30-20020a0563580c9e00b00168e7aa596cmr5566487rwj.19.1698186324064; Tue, 24 Oct 2023 15:25:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186324; cv=none; d=google.com; s=arc-20160816; b=xHlTAP7D18NAqUljeBNhoptTUsSfAqSSApvZLR4dp+CUudVIvHUpfCHLQ5hT1vx+3k Ci/RugxBPl7hb1HOVMd0AKm7Kmw+iTM7Ab3hEnvIBqHa1/edptfI486fubtLTWhvwusT +HA3dfws3NjDDCFTZLRvZdXgQ1yYFj47f5rTWo6zh5Nhkb9LeN3AD5f1OsqRiWRJ6P4X xWDMNuQ3+6llNKIJdKLTtL1WPzZHylor2SkQ1/6ELgQjm8o1mmjRsMlvT+RceIUP+O1q wJLRvEOCZ4UjK1/BgOwseFg6oHBC6FHWzpKL6lyrWpUrgF3ikGQoKo44YVjbSSPIzQGU dnog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=IMICwswuJ/vFGwUP8a8fOw6frHIgUKsDaScdLo+z8js=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=CRM7zuEERgoFrdH69tj7UT86G4drFOWdjDRrO2dpTQRKglvj1QM7mj/AjoTo8XcMg2 8OHf7asX68NGej2Cn0EjHfrFpetdCIDWOtGsHjvoEORAijgJheX3pYODRu4z6SZCbYA1 HnQrBrAdLID+obCzVx/M+vxfbThysaP6lfkimOySkLq8KgjWeQVVd03T5Rla3W3JGNct nDvCKqHtBd+sB0lm09bg2bGyekKAS8dAQM5MQdwqZuCAO3QLZdnnb1U+2nBuLSvStRSO rmuoK2Sb7Mtxs9UW2B3QJsbi7j+PyZv5ykgXjO7vkXc9voFLFStL50uFK1aFYAtL3QTN fR/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yzf29tqS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id m65-20020a632644000000b00578afb3123bsi9077849pgm.700.2023.10.24.15.25.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yzf29tqS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 745CA80BEF1A; Tue, 24 Oct 2023 15:25:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344474AbjJXWY2 (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344438AbjJXWYW (ORCPT ); Tue, 24 Oct 2023 18:24:22 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45A2710CB for ; Tue, 24 Oct 2023 15:24:20 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9cfec5e73dso4928212276.2 for ; Tue, 24 Oct 2023 15:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186259; x=1698791059; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=IMICwswuJ/vFGwUP8a8fOw6frHIgUKsDaScdLo+z8js=; b=yzf29tqSgdydmD27uXNLY91T1l+1xEhTu+Qf8zjxwFlVdrHiJASm//O1glCrXisOlQ 4/Fft7aAdPvmDbUF2T1Z8H7eOVrKsKJuBlkATpb8TKk5f6WpRn7T7wQLo8TIUx7c1hKI lD5ItFDFLWi1XtcDaIIlyzFSwXGHmWo+aB8v8W8hS67EBvh/pf+kLWe7ENxIbGcrKW67 O+VAkhdigKl6XKUvH2gAw1HyUfdA+HWgHkqIfFqtYo8M+lblSlMD8SMKsjSbgidOvU1Y o0LJyyOdNnCPNiDCH1U2ZV+dgTqmTF2Y8Or5rJtJpP8boqX7MYnOC+Qfdj1n9tmY607J vVsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186259; x=1698791059; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IMICwswuJ/vFGwUP8a8fOw6frHIgUKsDaScdLo+z8js=; b=drIuWMt24e/ZBG0lTjHAmH8pvp1bw9iJCNXnrOLGewEnFztmTFu8stdX57cRJ7plp3 7BG2OadFatT07HHxhX/GjDRO8B66DsVYD/q2BuvKqNDuA05ggXThyxjDoF/EOdmlWqXR e3ZiZw1G1/QbN3RhWxVI0tF5eE/gnRpu6k8UpqvizK0Qa65cGoxdfCTrnYL6mL3qALfn AOvH8vIurfUJnE6fgTHaLwL4YAyTTuAtJW5/DDtccq6I4nlZK36fTsm4g+xbGZEK+iuL 5G0X6Wr7G8rdeOXl2LFO57QC0lFFZ/Bq5i5nZgGbf7HT91QReH1uCZf8nqhuNI/gU8r1 mQGA== X-Gm-Message-State: AOJu0Yxy1DaZH4SPcWK4HCLqZVqlhZj+k6pwrhu3xuL+7bAdub6IHWbN qitTw5BCVNGw07z3hQ9SIALG26KKpJ7v X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:168c:b0:d9a:e6ae:ddb7 with SMTP id bx12-20020a056902168c00b00d9ae6aeddb7mr243432ybb.7.1698186259418; Tue, 24 Oct 2023 15:24:19 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:06 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-4-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 03/50] libperf rc_check: Make implicit enabling work for GCC From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:17 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677422938778629 X-GMAIL-MSGID: 1780677422938778629 Make the implicit REFCOUNT_CHECKING robust to when building with GCC. Fixes: 9be6ab181b7b ("libperf rc_check: Enable implicitly with sanitizers") Signed-off-by: Ian Rogers --- tools/lib/perf/include/internal/rc_check.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/lib/perf/include/internal/rc_check.h b/tools/lib/perf/include/internal/rc_check.h index d5d771ccdc7b..e88a6d8a0b0f 100644 --- a/tools/lib/perf/include/internal/rc_check.h +++ b/tools/lib/perf/include/internal/rc_check.h @@ -9,8 +9,12 @@ * Enable reference count checking implicitly with leak checking, which is * integrated into address sanitizer. */ -#if defined(LEAK_SANITIZER) || defined(ADDRESS_SANITIZER) +#if defined(__SANITIZE_ADDRESS__) || defined(LEAK_SANITIZER) || defined(ADDRESS_SANITIZER) #define REFCNT_CHECKING 1 +#elif defined(__has_feature) +#if __has_feature(address_sanitizer) || __has_feature(leak_sanitizer) +#define REFCNT_CHECKING 1 +#endif #endif /* From patchwork Tue Oct 24 22:23:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157715 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2236981vqx; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnsnodGhUwCWfbNOqGhd0EgwKATmMIR7inrKjQjnBX0ex9i1VUFa+gFzLtoJFXge2N2F6I X-Received: by 2002:a17:902:ee53:b0:1c0:cbaf:6930 with SMTP id 19-20020a170902ee5300b001c0cbaf6930mr11740877plo.54.1698186292042; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186292; cv=none; d=google.com; s=arc-20160816; b=pqPqUGEH5pAh26abf5JUfZcqcx6qNJR4T28fnV2sJ7Ew7NjKRyh+ZsCIEboxNOskYb A+ua+BH/3X8hgy+le/JFioKIt4RtzMetXM+AGBssV2Y05+aMbhe5Lp7ye29aI6WUiyNx vKA2qLpH6Sx/C+O+wACoetr6bGfsWIqeScHgaAhhSkPZaE4SzTeKNiww+u/b7zCVrUE6 oBxFrC8kiSoYMeHILoNhp2TlkT2qFiQUOSUniyNz0H7VwSP33GBRa25rG6uJe+rdhXXQ bGfdjnp6dSurQh5BRlflSI5pzkAyGFFBebe8rYVWXGCdUB1rCMkODmZFHkBamlnxLKxe FgkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=JudBbpLKu9DuXeuHBT9WbAyZXtZgx6Ki97wXYqwU0f4=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=GDWFWUa1OqYUDxytmuAmdHtGKz0AKWdsM+UvULNiHKYPpASNFGeNefupsL0aj666+L o9ChGCa+uxM71tseCdxu1TuEWBKGSLSBNlq2KnAs4h4eGuXQj+dAS8Mf47bpL7te94q7 siG6H0iX99gph2N5lpMjWG5Fv40s+J+jca4gMUO48yKfsA6zrdOj/LNwV8rHKPZMeQQp FJDPKTL7wMSTZvq86UfE4Abv1G3iDeFh5LuLyRGp4mLPPgFvUDYYIT7m0YqIkGuE+i83 otWvCS0kvnVTZAp6WN/w+0vTqjOcBxnMEQeCMagK4HPlyz5bz75W0aEkBy4ftGwgadb+ lhEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Wn8jJcXA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id x15-20020a170902a38f00b001c9c3a16a91si8971998pla.70.2023.10.24.15.24.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:24:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Wn8jJcXA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 676708053F96; Tue, 24 Oct 2023 15:24:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344484AbjJXWYb (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344464AbjJXWYZ (ORCPT ); Tue, 24 Oct 2023 18:24:25 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91C0010D5 for ; Tue, 24 Oct 2023 15:24:22 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d99ec34829aso5776699276.1 for ; Tue, 24 Oct 2023 15:24:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186261; x=1698791061; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=JudBbpLKu9DuXeuHBT9WbAyZXtZgx6Ki97wXYqwU0f4=; b=Wn8jJcXA6Fcc8ZZdIe/342psqZ6dlwfGzMng4izWmLtVxwmxiHpO/a5Hh32LXlyUC+ 3ejotriifsZcJcDIe5eDtq50jIJxR9e1NdM9hwrB4QwHc61sU3bqoUlstdz1ZW8WFlXv LeiC77txrHDheLX/8g/R/Fsd6mtXc9k928SpPhlyTlxfTxZCstuGtrcgi5j9PZjlUmDG vVFcmAlsetjQxgRsKw3IrJ/lp9T/W8k932LqxfzBeBLuDDumiw2jXf6Ctozjwdq2yUQn bFADo9IwERtlRzKAHb3GJvs2fh3kLGeSVdEP/ecvvtqAyrzQFJhYLcGudx0MrXyOzRBN sNCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186261; x=1698791061; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JudBbpLKu9DuXeuHBT9WbAyZXtZgx6Ki97wXYqwU0f4=; b=n/cEcJQphNigOCbJ/8Zoc3TDlFQQ6Ws5lGtJjRVUA7N14okWUFLSfk0LAxyp5Wf7Em R66ouQaSP5cqth87hj1aSOklcvXLGd737BQJwpkX5K7glqlrP0SyMSl5EuZTnYPkgnzJ tqNfHIV/bQij3hwQfH8kefqrucZ67hfDp7r4IlLUTj3XEN70JI6hac7uw0PsDrHg1s5/ NJYn2yvWF6PdYXBykf90TyUzfFB9yx2iPZck+WQoL3+G+THe2NAJLgN0tnUy7qholsld zTzHLu2IjBHbriSpAIDst8QXrPCl5g6l4ZXRuIzmbnMVipfa8JP+XwCkcpQKpFXqFG+r Gxsg== X-Gm-Message-State: AOJu0YwUJ7flqvm7JzUyT9W86Gz7HvAtdWasTuRYNplf4e77L6KHUSTa QwH2y0V6nMgd1GqvskXO2bFV0EXysU61 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:d386:0:b0:d9a:6b49:433d with SMTP id e128-20020a25d386000000b00d9a6b49433dmr242608ybf.6.1698186261582; Tue, 24 Oct 2023 15:24:21 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:07 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-5-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 04/50] libperf rc_check: Add RC_CHK_EQUAL From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:24:48 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677389316682477 X-GMAIL-MSGID: 1780677389316682477 Comparing pointers with reference count checking is tricky to avoid a SEGV. Add a convenience macro to simplify and use. Signed-off-by: Ian Rogers --- tools/lib/perf/include/internal/rc_check.h | 7 +++++++ tools/perf/builtin-sched.c | 2 +- tools/perf/tests/hists_link.c | 4 ++-- tools/perf/tests/thread-maps-share.c | 9 ++++----- tools/perf/util/callchain.c | 2 +- tools/perf/util/hist.c | 2 +- tools/perf/util/machine.c | 4 ++-- tools/perf/util/sort.c | 2 +- tools/perf/util/symbol.c | 4 ++-- 9 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tools/lib/perf/include/internal/rc_check.h b/tools/lib/perf/include/internal/rc_check.h index e88a6d8a0b0f..f80ddfc80129 100644 --- a/tools/lib/perf/include/internal/rc_check.h +++ b/tools/lib/perf/include/internal/rc_check.h @@ -54,6 +54,9 @@ /* A put operation removing the indirection layer. */ #define RC_CHK_PUT(object) {} +/* Pointer equality when the indirection may or may not be there. */ +#define RC_CHK_EQUAL(object1, object2) (object1 == object2) + #else /* Replaces "struct foo" so that the pointer may be interposed. */ @@ -101,6 +104,10 @@ } \ } while(0) +/* Pointer equality when the indirection may or may not be there. */ +#define RC_CHK_EQUAL(object1, object2) (object1 == object2 || \ + (object1 && object2 && object1->orig == object2->orig)) + #endif #endif /* __LIBPERF_INTERNAL_RC_CHECK_H */ diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 9ab300b6f131..dd6065afbbaf 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -1385,7 +1385,7 @@ static int pid_cmp(struct work_atoms *l, struct work_atoms *r) { pid_t l_tid, r_tid; - if (RC_CHK_ACCESS(l->thread) == RC_CHK_ACCESS(r->thread)) + if (RC_CHK_EQUAL(l->thread, r->thread)) return 0; l_tid = thread__tid(l->thread); r_tid = thread__tid(r->thread); diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c index 2d19657ab5e0..5b6f1e883466 100644 --- a/tools/perf/tests/hists_link.c +++ b/tools/perf/tests/hists_link.c @@ -148,8 +148,8 @@ static int find_sample(struct sample *samples, size_t nr_samples, struct thread *t, struct map *m, struct symbol *s) { while (nr_samples--) { - if (RC_CHK_ACCESS(samples->thread) == RC_CHK_ACCESS(t) && - RC_CHK_ACCESS(samples->map) == RC_CHK_ACCESS(m) && + if (RC_CHK_EQUAL(samples->thread, t) && + RC_CHK_EQUAL(samples->map, m) && samples->sym == s) return 1; samples++; diff --git a/tools/perf/tests/thread-maps-share.c b/tools/perf/tests/thread-maps-share.c index faf980b26252..7fa6f7c568e2 100644 --- a/tools/perf/tests/thread-maps-share.c +++ b/tools/perf/tests/thread-maps-share.c @@ -46,9 +46,9 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 4); /* test the maps pointer is shared */ - TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) == RC_CHK_ACCESS(thread__maps(t1))); - TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) == RC_CHK_ACCESS(thread__maps(t2))); - TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) == RC_CHK_ACCESS(thread__maps(t3))); + TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t1))); + TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t2))); + TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t3))); /* * Verify the other leader was created by previous call. @@ -73,8 +73,7 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s other_maps = thread__maps(other); TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(other_maps)), 2); - TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(other_maps) == - RC_CHK_ACCESS(thread__maps(other_leader))); + TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(other_maps, thread__maps(other_leader))); /* release thread group */ thread__put(t3); diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index aee937d14fbb..18d545c0629e 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -1142,7 +1142,7 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node * if (al->map == NULL) goto out; } - if (RC_CHK_ACCESS(al->maps) == RC_CHK_ACCESS(machine__kernel_maps(machine))) { + if (RC_CHK_EQUAL(al->maps, machine__kernel_maps(machine))) { if (machine__is_host(machine)) { al->cpumode = PERF_RECORD_MISC_KERNEL; al->level = 'k'; diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 3dc8a4968beb..cde0078e6c90 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -2142,7 +2142,7 @@ static bool hists__filter_entry_by_thread(struct hists *hists, struct hist_entry *he) { if (hists->thread_filter != NULL && - RC_CHK_ACCESS(he->thread) != RC_CHK_ACCESS(hists->thread_filter)) { + !RC_CHK_EQUAL(he->thread, hists->thread_filter)) { he->filtered |= (1 << HIST_FILTER__THREAD); return true; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index e0e2c4a943e4..098600d983c5 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -969,7 +969,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine, if (!map) return 0; - if (RC_CHK_ACCESS(map) != RC_CHK_ACCESS(machine->vmlinux_map)) + if (!RC_CHK_EQUAL(map, machine->vmlinux_map)) maps__remove(machine__kernel_maps(machine), map); else { struct dso *dso = map__dso(map); @@ -2058,7 +2058,7 @@ static void __machine__remove_thread(struct machine *machine, struct thread_rb_n if (!nd) nd = thread_rb_node__find(th, &threads->entries.rb_root); - if (threads->last_match && RC_CHK_ACCESS(threads->last_match) == RC_CHK_ACCESS(th)) + if (threads->last_match && RC_CHK_EQUAL(threads->last_match, th)) threads__set_last_match(threads, NULL); if (lock) diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 6aa1c7f2b444..80e4f6132740 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -128,7 +128,7 @@ static int hist_entry__thread_filter(struct hist_entry *he, int type, const void if (type != HIST_FILTER__THREAD) return -1; - return th && RC_CHK_ACCESS(he->thread) != RC_CHK_ACCESS(th); + return th && !RC_CHK_EQUAL(he->thread, th); } struct sort_entry sort_thread = { diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 96587fd7a5a2..822f4dcebfe6 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -877,7 +877,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, *module++ = '\0'; curr_map_dso = map__dso(curr_map); if (strcmp(curr_map_dso->short_name, module)) { - if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map) && + if (!RC_CHK_EQUAL(curr_map, initial_map) && dso->kernel == DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(machine)) { /* @@ -1469,7 +1469,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map, list_del_init(&new_node->node); - if (RC_CHK_ACCESS(new_map) == RC_CHK_ACCESS(replacement_map)) { + if (RC_CHK_EQUAL(new_map, replacement_map)) { struct map *map_ref; map__set_start(map, map__start(new_map)); From patchwork Tue Oct 24 22:23:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157717 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237091vqx; Tue, 24 Oct 2023 15:25:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHlKrt7xeVBsd4UJV109hLoZtNqPeq7VM5wDm9UE3AdakI+exM8MLw/8QSWjb8As7i8uoCW X-Received: by 2002:a05:6a00:1a15:b0:693:4a43:1c7e with SMTP id g21-20020a056a001a1500b006934a431c7emr12735675pfv.29.1698186306062; Tue, 24 Oct 2023 15:25:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186306; cv=none; d=google.com; s=arc-20160816; b=fFZj0fbnXgj69EygJrJJS92R4RUK/lahhFfZ6fcMDilm4fjrCr/IeCo6wz+2gYHKno XPxhbOHPGHB24TPsa6N76vUZ7pYsskfwKJy/HsywyczB+ylLqx7fHZVozP0fdJuefVc7 gIKWj0WNaU3j4i4mzlsrEE62u9uPHPKjm35u1c2ST0Apb8PPIwpP2o73eNlbNkrNvmx5 K1wRiPsbA3fWmM+i32xgiT6jVJW2C9L13/PTb1H6tZZvEjgC56JR3NvYWZ8NPmID1ZzG lAsIWSUqyrx2X9y0hDioE2pzjSF0bdBS7mdKMl3ymwaEeVIqCPo/ps2cVscObUhNtPEv GCyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=cG5+OY088Om6+iLiqKt06WFCMXUosiNbLSusAjLjbIM=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=ZOISWu0xCP+D0JACUYcgGak0K5ijMaAPppkGNOtNWhYdRT34XVotmvi0toXnNpCMmE jdWqpGd4WYgr2YB6j6zgoIzUssDOROMdE4yZtgpXcG19Aye2QjzWAwSd1cmBr0NRuKWR PRUnGkqtsHtw9e4VPuZp4ZvteRgk+gR7AK4cnXqSTEw/n3VGuTP0n9ccD+KYWczNe/Pf iEGv7DA6qNzYbHwJseWtjw29PHN+CSkULwgcr42NHttZ/i9oMkt+fdjam4v0xi4ObKog kH0Be/cEb49/EtCGUhaJzNlYXnOx65l+lZyYk/y7n/0iKPzYYlgmjFjKSZx220xfpzDe 0U/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZZqrKT+U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id 199-20020a6300d0000000b00563e283786esi8639646pga.104.2023.10.24.15.25.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZZqrKT+U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 6C14F80A9B2B; Tue, 24 Oct 2023 15:25:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344502AbjJXWYg (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344440AbjJXWY3 (ORCPT ); Tue, 24 Oct 2023 18:24:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C19B210DA for ; Tue, 24 Oct 2023 15:24:24 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9c4ae201e0so251735276.1 for ; Tue, 24 Oct 2023 15:24:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186264; x=1698791064; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=cG5+OY088Om6+iLiqKt06WFCMXUosiNbLSusAjLjbIM=; b=ZZqrKT+UjHHvy/H00VQVIZt3PiS/jfnqDnz7ouPps9VLW80Fv2MkQkEZp+0e3wLOvi aIC9Le/kVULcDKBJ0GuhTEYHFDpIpHkFYeU2Vivs0+agAus9cmVUypi2M6ZIE5lsnBwd F0x4UKY7hlNf8NIh4Hiky32niofP45ujmy8OV6r4DCzjX2xk21gGUPKeve6IZmY/i0/w MpxWzRL0gKARR1KGhg9O6ItbnHJWZ/Fd1AVL3ooyhebg2Fnjq1GzBJGN8uO/fVV+FRBT ef0MFik8vbgkT404AwvA9/VZ8dcmVWEn/uZN0ECmOGyWEFjzxotYS3SucyA82jueLp4D b7Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186264; x=1698791064; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cG5+OY088Om6+iLiqKt06WFCMXUosiNbLSusAjLjbIM=; b=d4WGfQrILMSEjNpPKavyT9Z51Sc1/isgzOdyugUqkEKw9C4Zt6LNROGEGoRohRiY6c pY+LzyjDtdjmQZABMPpjAHXcyUtQBQX1YvB/NZS7rWNsmT+WYwaeuaUIMeqERPwIWQJk euF4qgDjCHu/Jb5z46Q7q7rcDLKKnJaH+eeuVe/Si2UjbXdxOdX4QtjjfTnH7+5UPrUE GKbQGa66dSGd2qSo2Pb+tAy76nEmqN7KIOUeaeEN3VCJ11te8NuSurLzeb/ZceN1H1h1 kMXputmf3bSKasKozuzrMdcu2ry6VzsVEupSxFmS9YHQtnOS/zW3XYBuqJeU1pnefLu3 icpQ== X-Gm-Message-State: AOJu0YxMN4ZeeLL1MDXvlvoIMHayYy9THt8Xd6zTc1xrIlgUMHXkz736 QwBvP+aP90kzOzeb9KxIR8sp5vCasfnl X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:34d1:0:b0:d9a:6360:485b with SMTP id b200-20020a2534d1000000b00d9a6360485bmr385686yba.2.1698186263960; Tue, 24 Oct 2023 15:24:23 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:08 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-6-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 05/50] perf hist: Add missing puts to hist__account_cycles From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:03 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677403735304955 X-GMAIL-MSGID: 1780677403735304955 Caught using reference count checking on perf top with "--call-graph=lbr". After this no memory leaks were detected. Fixes: 57849998e2cd ("perf report: Add processing for cycle histograms") Signed-off-by: Ian Rogers --- tools/perf/util/hist.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index cde0078e6c90..0aa7e231172a 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -2676,8 +2676,6 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, /* If we have branch cycles always annotate them. */ if (bs && bs->nr && entries[0].flags.cycles) { - int i; - bi = sample__resolve_bstack(sample, al); if (bi) { struct addr_map_symbol *prev = NULL; @@ -2692,7 +2690,7 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, * Note that perf stores branches reversed from * program order! */ - for (i = bs->nr - 1; i >= 0; i--) { + for (int i = bs->nr - 1; i >= 0; i--) { addr_map_symbol__account_cycles(&bi[i].from, nonany_branch_mode ? NULL : prev, bi[i].flags.cycles); @@ -2701,6 +2699,12 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, if (total_cycles) *total_cycles += bi[i].flags.cycles; } + for (unsigned int i = 0; i < bs->nr; i++) { + map__put(bi[i].to.ms.map); + maps__put(bi[i].to.ms.maps); + map__put(bi[i].from.ms.map); + maps__put(bi[i].from.ms.maps); + } free(bi); } } From patchwork Tue Oct 24 22:23:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157716 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237074vqx; Tue, 24 Oct 2023 15:25:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG0arEZowk0Nh3raSseZ1Cr+wTJ+2O7m21ZPXFnLsfu9jCumbHPEAZgicDcaN4xn572/wys X-Received: by 2002:a17:903:28c4:b0:1b8:66f6:87a3 with SMTP id kv4-20020a17090328c400b001b866f687a3mr11111407plb.52.1698186304014; Tue, 24 Oct 2023 15:25:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186304; cv=none; d=google.com; s=arc-20160816; b=HtrAH5jyYrXXwsedX/6mGc/AQFAnGrRlzj4xNkMv1eK9cUe9yJJpR3Z0rJZrCoYa4n 45KwlhB/IK9MHre6hCuDyhMPIP5U7vAbwKg14C4DmFDCZhZazI2f8jz0DI0Yxtb8JF+s 0EFdTsJh5NGwfJUBmGjapTurmnwLZ/Q2qNzBLKEyHrdi7oPjffzkYB2sFkDoqITzLj3n XMjaYyu/6UhmUZxbHvgJ/BdhCcEGK9dL93KOv0GsFk3R8kSYjAVS+aYtwYuL1puTz6DJ 26cmKBb0xTYd4sep1M0CHmkpCzTdgxJtf1wfGnGsAWMyKmVERPclaKWdIkVjuQ9bw3/l xp2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=TVGR9Xyq2raGKRiGJds6iUcO5Lwf67o/Z2QHhpJQbfc=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=VRub4E8eQMVn0xXlinD5lIYhDSKyi9DW/fqL2OZ3bbxlNg1UOQyFqG4EbMoXwsIU6Y BSLLBfWN/NqKB6saJVpFOic17EAYChvYQRBBqmEDiH5Q7vbXSR/mZQnP5D0gW7SKpk9B gngXVbtNxrxlxUU2bh5AbN6pCQYt2NUuPr8XPJs9kpDak4inIjVR6gJ/ZU5Gle9EG0Jq mccuT4csJeZknWjXr/qA3RoCeeTybZIxCJGuePnzK0YUgZoLFY8Gvpfc5Ejro/aDG6KR ZGdZfOZHL3pdznqtdDMldg5nMEKPO7/k96amS+hLUG5rBQ2YdE+evZ89tcmlVeKA/fth 5l5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ydbfqHeK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id 19-20020a170902c15300b001c9cc3a07c3si8481739plj.280.2023.10.24.15.25.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ydbfqHeK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 3FEEF802ACB9; Tue, 24 Oct 2023 15:25:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344520AbjJXWYo (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344481AbjJXWYb (ORCPT ); Tue, 24 Oct 2023 18:24:31 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE6231705 for ; Tue, 24 Oct 2023 15:24:27 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9cb4de3bf0so5993692276.0 for ; Tue, 24 Oct 2023 15:24:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186266; x=1698791066; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=TVGR9Xyq2raGKRiGJds6iUcO5Lwf67o/Z2QHhpJQbfc=; b=ydbfqHeKgSmvFccd0/lADv3jDFkKDDTtxmixOmTXahACyrE+ZbWlkxxgbjNr/CQEeM d+cfSwlTgZSyT6SM8NTk2sjvATpeYifQt66+2zi2i+FpB2gH0A5VVjJrkoAPKe4A8s0G 09dIdKRnwy2R1ipHQJPVWTt8+jS7TRPuFFkH6t0heiUigPwY35F0Bvmsp55cYhUVKROf CiXKIeWui1hEXCBec5lnKU4Kqke7cIWHJg2bd4xtHQc5wmK4RZ9HBH7t7t/ZvTDu0PiF JWwc5+dfjSDD2voG0Agh4C4fQVN4AehnvGsbJnZYty009NEwb+6UYRvsZbdoGEQYGUbx ho5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186266; x=1698791066; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TVGR9Xyq2raGKRiGJds6iUcO5Lwf67o/Z2QHhpJQbfc=; b=CI41txB337NQS2u/XufX66uC0q3q6OEmB2zHTod/bMLWQOw92RQa3dzVJOwDvSTssj HQ6rzRlp/crQUjAGuBz77xLzmQZN1iS+brgU8eccr1auSsZ/i53FJ98HwFNF3W+2rH1g 7rfm0i16QaNmx9bDCHvzyrA4gljfVxezxEv4F/Gtc4K+rxyEwfzTz7DammUELxmlqa4F Z0pCVRlOX/yengzMMyhqBUX7TRSXhtWcx/rtB1qQIFoSGRJFrwIOEogk6IzQhSKnFNFh az1RQ3yFpYvu5Dh3pXw9oc6ika71UzLqEcarEm78LyV8so316ZaF5sFrVnLSvKrAdfmk vjYA== X-Gm-Message-State: AOJu0YzXle8Kzwc2aZMeLGhle9j9A99IBTdJCVDQfXTOivyigd1+j2dT warLvKKyG1+y6+6Bj3mmc7hwJlQB1bVW X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:8541:0:b0:d89:b072:d06f with SMTP id f1-20020a258541000000b00d89b072d06fmr256399ybn.7.1698186266012; Tue, 24 Oct 2023 15:24:26 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:09 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-7-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 06/50] perf threads: Remove unused dead thread list From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:00 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677402384641733 X-GMAIL-MSGID: 1780677402384641733 Commit 40826c45eb0b ("perf thread: Remove notion of dead threads") removed dead threads but the list head wasn't removed. Remove it here. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 1 - tools/perf/util/machine.h | 1 - 2 files changed, 2 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 098600d983c5..31fdbb0f27af 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -67,7 +67,6 @@ static void machine__threads_init(struct machine *machine) threads->entries = RB_ROOT_CACHED; init_rwsem(&threads->lock); threads->nr = 0; - INIT_LIST_HEAD(&threads->dead); threads->last_match = NULL; } } diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index d034ecaf89c1..1279acda6a8a 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -35,7 +35,6 @@ struct threads { struct rb_root_cached entries; struct rw_semaphore lock; unsigned int nr; - struct list_head dead; struct thread *last_match; }; From patchwork Tue Oct 24 22:23:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157730 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237703vqx; Tue, 24 Oct 2023 15:26:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFB4AXpLQtjjILoyVUJKVPtdCXfqOe2du7MxIyCo6IzjeurZhV0hjGk4CVtA0ocPvkhOmm+ X-Received: by 2002:a05:6a20:8f04:b0:14d:7130:7bb3 with SMTP id b4-20020a056a208f0400b0014d71307bb3mr4923671pzk.13.1698186397038; Tue, 24 Oct 2023 15:26:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186397; cv=none; d=google.com; s=arc-20160816; b=TTZm+pqFBUQ+Q5WXhDguFZnLWLryWGmSqhTbD1wf37wE8Jpq6DZDZ03Zh91HgCwty9 /HcVOkPOXsbJewfx8AIP3Gg3My3nLGGvlT0uf+VN9Qma6THarzbwxyJy51T1csPZGfa/ UycZ01KZIAtfRxk4Vi7jPDFCmVlQ0joO+65D+6SWSuQJtGp6fwk5Vuk4rBwdAELvvNZv Al1eW8glQm+mw2p9abvYo1TCJXWZGnjuTRFrQTgXbf22gPMu2Pxu4Diu4aKEKn9E82lC TCztgZ7S6CIJ9pYvXE6amowduRXjEqFFfATFen7noFyvRrte0gN5nozY4e4E3cRW4hss vN7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=b/WYylQjwixRJXAqoqT9x2vsT/egkNyyGQzseJqJY9U=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=pAS+Pz0r9miixGjH23H6iHT9sDu/qK2LCAfEnEfiHwdlZ7Eb4c0RQ+UG3atOKLpCGc dGBu1UBzWREkoz+39prwo8FnKNuOz03WZkIa69QBUF2FwrcZIlsn5KLWL7U0FXN0RWZ7 kOifb6phKNhOWHq59lul5tteLYaUqdIale04pJz5YYbceVjrs6z5+LlRnZIAhB6UmfWI AbOaZoKnQ4Ky9c4GxE9ajQuyqFMONaCBMFFHxxQSobkonhDfQdz7Nt0dKsLmnrQjhzm/ l+BT+uRSBNWtyPNG33bifdhoxXpW7JOkohDKKObCMGdG30Mt0mIapQFb3/k1LRrzFo1H xFqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uT7w4lEc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id i184-20020a6387c1000000b0057d08dac75csi8761111pge.517.2023.10.24.15.26.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uT7w4lEc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 6A0778094756; Tue, 24 Oct 2023 15:26:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344519AbjJXWYw (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344440AbjJXWYl (ORCPT ); Tue, 24 Oct 2023 18:24:41 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D98110DB for ; Tue, 24 Oct 2023 15:24:29 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a92864859bso45292427b3.0 for ; Tue, 24 Oct 2023 15:24:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186268; x=1698791068; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=b/WYylQjwixRJXAqoqT9x2vsT/egkNyyGQzseJqJY9U=; b=uT7w4lEcUTQXJpCxjyDA/TSOS6e54jIhBLpvXasOjLYIoosAK7q2RldpGD5bsFz2Hl 4WibsMTU5PYsS12Lb6iMR5AKHHZEhAFUbW7FyAf/fdqHr0KuuLmcRmHLi98Srsqc1j7w URJ+83mJ5RWs4be6uAGHMoYyGoQpX+jFmSYNDHwPPD42UqnlXo1MKVHASfxJLu+ZxPzw a5ZcpmOovxvmtoo4hoJLDgDdjNS2Pm4CRsQQMARVAzqlat11bLb1eNkRtmcIM3XDh5wP KK+DRATMsBx1DWH7gPycIrobSjpCz/+4X9eVnb6shk7EnuS2Ps8fvOvlcjGsuDfSIED/ qpCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186268; x=1698791068; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=b/WYylQjwixRJXAqoqT9x2vsT/egkNyyGQzseJqJY9U=; b=f3sfyS/rWDCFCR1Rg7Ib9/hSRZ37YDFPag1baJJrEQaY5JadGD94O6M62VIjCYAnH4 /eDQTr/jMkhoIudobwbtzznt2mgg06FABgQbvQSnlHd8kv2DsKRFT9RvxMf2HVYFqIvo S2TyI6x9FKNuVntDILAQ2YKW7rQRBckqMXpf8g02Tb58lTZGll8tQhm95uQ7k5AzJk3/ bkc3oyrFYLHHC3BoGVxpQLONx+mr3EFCEH5PEJHRzPnXRiGE6QwnEGjpkxgDXG4Oap7z plzAMn9W5Pw4IG6amu4HUaKd+lDAzMMXnJMSwwzAWA2B/kpKCfzM7xdqKX+Gg7aQU1Kj 8Rag== X-Gm-Message-State: AOJu0YynMx+IE+4+EWJygcgu1D6NrkdHW2bnDcr/6yh/tFnZiTzpaX6R rwsyoOuVb711N46eEMnnPCTT6PPjVsLx X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:4c8f:0:b0:59f:3cde:b33a with SMTP id z137-20020a814c8f000000b0059f3cdeb33amr57926ywa.6.1698186268457; Tue, 24 Oct 2023 15:24:28 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:10 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-8-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 07/50] perf offcpu: Add missed btf_free From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:26 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677499266729076 X-GMAIL-MSGID: 1780677499266729076 Caught by address/leak sanitizer. Signed-off-by: Ian Rogers --- tools/perf/util/bpf_off_cpu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 21f4d9ba023d..6af36142dc5a 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -98,22 +98,22 @@ static void off_cpu_finish(void *arg __maybe_unused) /* v5.18 kernel added prev_state arg, so it needs to check the signature */ static void check_sched_switch_args(void) { - const struct btf *btf = btf__load_vmlinux_btf(); + struct btf *btf = btf__load_vmlinux_btf(); const struct btf_type *t1, *t2, *t3; u32 type_id; type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch", BTF_KIND_TYPEDEF); if ((s32)type_id < 0) - return; + goto cleanup; t1 = btf__type_by_id(btf, type_id); if (t1 == NULL) - return; + goto cleanup; t2 = btf__type_by_id(btf, t1->type); if (t2 == NULL || !btf_is_ptr(t2)) - return; + goto cleanup; t3 = btf__type_by_id(btf, t2->type); /* btf_trace func proto has one more argument for the context */ @@ -121,6 +121,8 @@ static void check_sched_switch_args(void) /* new format: pass prev_state as 4th arg */ skel->rodata->has_prev_state = true; } +cleanup: + btf__free(btf); } int off_cpu_prepare(struct evlist *evlist, struct target *target, From patchwork Tue Oct 24 22:23:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157719 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237143vqx; Tue, 24 Oct 2023 15:25:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGoJq5XaE+SnWE8VIh1U83KkyJihRiRP0PBwopmu/aS94YkhMPsfgDLYJ9GJc/0yvzWW5DP X-Received: by 2002:a17:902:d2c2:b0:1c7:49dd:2ff with SMTP id n2-20020a170902d2c200b001c749dd02ffmr16229655plc.27.1698186314839; Tue, 24 Oct 2023 15:25:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186314; cv=none; d=google.com; s=arc-20160816; b=zyE9xR7E6S+Y7DEiLFPul2mjlusp/EM7bEOgDG/fu01eCR/PaXfYcLqNkBHN6V3XG4 E89cNtFeF3vmCjkPXffGowc5EuGosX3Q56hIcH8wq2ioJvyYAA63ll0sQcSmHoIOofon 8E5q5/P9xEQvhLopaYOoX++3IbAjtH5uUXC5JafeZnlLlYxequhAyQMVDsJq7JkKgN6Y ukCM1QoeXBs1DGSEicKk1/4izl9UEeXSZ3kBHX2wxCW1K5dBzaR7fp5UZYxUHQNVxVT6 Yo89s1jJE7aR5H9gO2zwwwKMH0/QnD6vFjbVYnuQ7itY8bnfOliZ5FT003LC1JKcFiEe w8+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=E9Urizhv5WS8Bux3imXxMiLlOJqV8I9Pz4v3kegUcno=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=qjlp1G8/skjMj9tI4fz+hsPEK+fBbovFKWAtAmjn7HVWNmt71iz0X6zrz8GjC7NuvQ 96LJiBdmdXUUjO482pUJbeBOP24TAENSfu0DbV1OeR4QPpwVAcnzMT4JHPopsyn5BxTS KqCIGJmiUegpTetAUEL6Rb2INZrFcL/inyIt4b7SMT8/GN9bOIvSnuSe2LLwIGFM3dJP x/sQ/FTJde9KKbxcidHbLQ399dZKdtDOmCOyfTS34Jg32WJjN1lD+i+/pZHexSlUQz2T rmTPazw8mwzyPFEB/yS0GzyLxfqHSYfMbIi619n5mZW1CcAcXKOId+R5tc2RIgT7GpK3 N8dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cj997Knq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id f13-20020a170902ce8d00b001c7388413besi9063979plg.529.2023.10.24.15.25.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cj997Knq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 00622802A9CD; Tue, 24 Oct 2023 15:25:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344530AbjJXWY5 (ORCPT + 27 others); Tue, 24 Oct 2023 18:24:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344433AbjJXWYn (ORCPT ); Tue, 24 Oct 2023 18:24:43 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F0AF1713 for ; Tue, 24 Oct 2023 15:24:31 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da05b786f1dso667629276.2 for ; Tue, 24 Oct 2023 15:24:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186271; x=1698791071; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=E9Urizhv5WS8Bux3imXxMiLlOJqV8I9Pz4v3kegUcno=; b=cj997KnqBuk1bJmOrE1IaIjCfFJfdq7f3X0TDebG3W1tvzl1CfqDsoRbsWSkZbq3EV VILP3F1wqjbRACu+v39fc+dbw/umoBJed3H+6Z3KbqpiOSeTXGXKJaOdgrvzEg3CMPwO FCknZrWklnt84Q14XXGuetp0yDNwIWUVTdVjyYNqz4FTPhU+8kI0nFochpdvBzosdypS y63K/24yBnFMwIP4AmRbjjFMHDZjfPAU1k+jVBjJ25N6bh8kZaocYa2I5BH03L9AiuGR fhbWg0PujwL3cGy4Ad9NGkHZeRYuD4mkPZvDjXGs1Ad8Lrh4IBf++4iHjGZKu36Qk9aX skMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186271; x=1698791071; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=E9Urizhv5WS8Bux3imXxMiLlOJqV8I9Pz4v3kegUcno=; b=oekuStW2mC2dZsC8QIIMwKkLGKworgUE0P8VNG+LSqI5taJEn71NKusXTnEfWQuZXj A1kgl4htejTQ10NRWqa6eYmbY5d079f54a9CNK3r1o20EXbcbfz9LmQM+Uo71Ibd2HlE DLmLA+6zquZ+VNizv7Mw8EOVCcoI/eKRfqRxVsnRY6QODU8l4K7Pd6vvbpdOrzGBU5PM QxSa7s19qpJnfZEkz0Z7uGM+F3f1pG+WBSWzzeswziPF40ZPpwcOBUgm8GB9pQ/uYASz acThix9vwXX3jrhnFBAhPupLP2+H7DJYEFpq7QeqGK0rOPwgAz3T2RCuRRb0JnHelC24 ikBg== X-Gm-Message-State: AOJu0YwTK/heHg9f1AjVZxHKaRsr4QApEAVnWulAoy2+Cgf6C5Djgtec vCCXBjQcCE5/+rTRNZyUvc3OSQxKPqcl X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:4f4c:961b with SMTP id v3-20020a056902108300b00d9a4f4c961bmr353592ybu.1.1698186270729; Tue, 24 Oct 2023 15:24:30 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:11 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-9-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 08/50] perf callchain: Make display use of branch_type_stat const From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:10 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677413193922979 X-GMAIL-MSGID: 1780677413193922979 Display code doesn't modify the branch_type_stat so switch uses to const. This is done to aid refactoring struct callchain_list where current the branch_type_stat is embedded even if not used. Signed-off-by: Ian Rogers --- tools/perf/util/branch.c | 4 ++-- tools/perf/util/branch.h | 4 ++-- tools/perf/util/callchain.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/branch.c b/tools/perf/util/branch.c index 378f16a24751..ab760e267d41 100644 --- a/tools/perf/util/branch.c +++ b/tools/perf/util/branch.c @@ -109,7 +109,7 @@ const char *get_branch_type(struct branch_entry *e) return branch_type_name(e->flags.type); } -void branch_type_stat_display(FILE *fp, struct branch_type_stat *st) +void branch_type_stat_display(FILE *fp, const struct branch_type_stat *st) { u64 total = 0; int i; @@ -171,7 +171,7 @@ static int count_str_scnprintf(int idx, const char *str, char *bf, int size) return scnprintf(bf, size, "%s%s", (idx) ? " " : " (", str); } -int branch_type_str(struct branch_type_stat *st, char *bf, int size) +int branch_type_str(const struct branch_type_stat *st, char *bf, int size) { int i, j = 0, printed = 0; u64 total = 0; diff --git a/tools/perf/util/branch.h b/tools/perf/util/branch.h index e41bfffe2217..87704d713ff6 100644 --- a/tools/perf/util/branch.h +++ b/tools/perf/util/branch.h @@ -86,8 +86,8 @@ void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags, const char *branch_type_name(int type); const char *branch_new_type_name(int new_type); const char *get_branch_type(struct branch_entry *e); -void branch_type_stat_display(FILE *fp, struct branch_type_stat *st); -int branch_type_str(struct branch_type_stat *st, char *bf, int bfsize); +void branch_type_stat_display(FILE *fp, const struct branch_type_stat *st); +int branch_type_str(const struct branch_type_stat *st, char *bf, int bfsize); const char *branch_spec_desc(int spec); diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 18d545c0629e..cde4860e6f28 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -1339,7 +1339,7 @@ static int count_float_printf(int idx, const char *str, float value, static int branch_to_str(char *bf, int bfsize, u64 branch_count, u64 predicted_count, u64 abort_count, - struct branch_type_stat *brtype_stat) + const struct branch_type_stat *brtype_stat) { int printed, i = 0; @@ -1403,7 +1403,7 @@ static int counts_str_build(char *bf, int bfsize, u64 abort_count, u64 cycles_count, u64 iter_count, u64 iter_cycles, u64 from_count, - struct branch_type_stat *brtype_stat) + const struct branch_type_stat *brtype_stat) { int printed; @@ -1430,7 +1430,7 @@ static int callchain_counts_printf(FILE *fp, char *bf, int bfsize, u64 abort_count, u64 cycles_count, u64 iter_count, u64 iter_cycles, u64 from_count, - struct branch_type_stat *brtype_stat) + const struct branch_type_stat *brtype_stat) { char str[256]; From patchwork Tue Oct 24 22:23:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157720 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237182vqx; Tue, 24 Oct 2023 15:25:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEodkW3Uyn45GfYzsBf8Q2vAdqUBfqeWjN1dn1utXx86a0mx9nXo9SKTU/gD/nC7vDThgEM X-Received: by 2002:a17:90a:f184:b0:27d:306d:71cb with SMTP id bv4-20020a17090af18400b0027d306d71cbmr22711665pjb.10.1698186318992; Tue, 24 Oct 2023 15:25:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186318; cv=none; d=google.com; s=arc-20160816; b=b5d23+aALagkQq3E4fIAZPQK8wqF7BWWMxrnJZATpXvNOC9kS2tSzu2m8MxEMG0a2x l/GZ+RnAEBATguh7bc/Fls2yMLimuqfhmO4bwnQdn0DHeJr+GpyIe91X88p1JPzO9JC9 pMzZi1rA1zj+RKRK6pgIwHp6D+Uaon9Tj1fxoJKCA7WgEuemVf5TVPRQA6PBofBVcmen gCn+dO8pIX6wM99idPbBGxhHd+XhiJOVE3w1wx4Vvu3Qj0v0+lYWj0qrhrO7UqW7DEMO j8nNknotlafqeUw+eXoFtY6qHN/7aOAU6WsaJzDCbgMVkwTnrgHCCwFK/yaGcepHLz5A Jspg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=suhDjyVWL4E1hV+3n6FJxzBer/uTQy3hSIxvwlmIybE=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=VNP1ARbGW6ztPtVGgnjNoltbb6XDObb6og+P/Eb4lzuZIMDUPPu7bJvawABypSLpCU i4ab+m668SiQdrXfuBHOQZVwW2LEZy7X0gILkmBGKcjAglWaoF3M1WJyQSWv3Q2nB1mS Zk3ewHf6VOAihG2rVM5hKn3GkX2AM2twyqmRxzAAngshCRxIegJJWoNcwdYCq6uncrmk AN+v8B4y20nXAW68F+5nEtilcORlysEBk13J1XUVgV+196Of3zF7Hsx0a4odaII2qoTy jFlzPbiWqpAAKUoBdTq9HF1cqbw/nSq02Uf5PgvQRwtr7+HgHXVxHpfoAZjK35+8VKsD 9/AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zCU8CFVJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id 189-20020a6300c6000000b005b8f6936eacsi1050343pga.628.2023.10.24.15.25.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zCU8CFVJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C7E10801F949; Tue, 24 Oct 2023 15:25:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344454AbjJXWZC (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344489AbjJXWYq (ORCPT ); Tue, 24 Oct 2023 18:24:46 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4060E1738 for ; Tue, 24 Oct 2023 15:24:35 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7be940fe1so65244917b3.2 for ; Tue, 24 Oct 2023 15:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186273; x=1698791073; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=suhDjyVWL4E1hV+3n6FJxzBer/uTQy3hSIxvwlmIybE=; b=zCU8CFVJ1mxe9S1kRflGZLVulexeKS1d12mFQacSp2B/pkolQVNZfgAneQSp2wc6zw vpEy58qi1Iivguek2u7qnKXofPcWLfXQggQXGr3LzqDLIYOp7TgS6rK4VPwDBzwwuEIG 8cRQH35fsSO68wxoTdAABhK16E0c6b1TpBz9SK9IxbRk0rV1waNH1mLDeSdT5VEtaUBf /vjYg9GvB/EmmJ/sLvCEgdCdoXPDssHCIw9yzUWWFoF4NfZpLAWFCFQYV+EyG7a3mLEd aZ0mqsStDXEHowH1sVOSp4VYubJM2QBpXmPMFDFlgBPDCupkSqeecT6bCGDuy/lyFWT+ cguw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186273; x=1698791073; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=suhDjyVWL4E1hV+3n6FJxzBer/uTQy3hSIxvwlmIybE=; b=OIzLNfbUpEd5jE9vqmQSX7E3dgzDgCKEMGPAoFG6RkfSKqhDRsgOoZgdBEF8I0Adji /K46sN5m1/6Fj8rl4OHUtwQqj9yPGvBiMz15z3ro11KKkGJiDnaJvFAyFWzWg31t7qQJ ZboVIy20wdbPCyv7SYgx5wd7akfdimnrY+kQXEb7LMeDUmgNbWHNT0mJv3ELJ8CPGY9v iE/nYHc0TQpU4WNMBHFVlfB7tlGqmJThEy6AhOGfQgiiYh+jLv1Tmj5LLiawshZuSzSH UmK6M4J2X9On6c6mLZcO77/dHThg87jwN1wQq789TFj+UeHsgqGRhHwnIz8Sm4tdxkxN qe6A== X-Gm-Message-State: AOJu0YxtSirmH2Z3o0+Xw5MoBAISSFWF2tf+vZcNkIExkPIpOJIRtkx0 Q0aFBSMJVLgMGdL0HaFilyx/Zz0ugSKN X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:c7c7:0:b0:da0:433e:871d with SMTP id w190-20020a25c7c7000000b00da0433e871dmr54206ybe.7.1698186273164; Tue, 24 Oct 2023 15:24:33 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:12 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-10-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 09/50] perf callchain: Make brtype_stat in callchain_list optional From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:13 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677417682578549 X-GMAIL-MSGID: 1780677417682578549 struct callchain_list is 352bytes in size, 232 of which are brtype_stat. brtype_stat is only used for certain callchain_list items so make it optional, allocating when necessary. So that printing doesn't need to deal with an optional brtype_stat, pass an empty/zero version. Before: ``` struct callchain_list { u64 ip; /* 0 8 */ struct map_symbol ms; /* 8 24 */ struct { _Bool unfolded; /* 32 1 */ _Bool has_children; /* 33 1 */ }; /* 32 2 */ /* XXX 6 bytes hole, try to pack */ u64 branch_count; /* 40 8 */ u64 from_count; /* 48 8 */ u64 predicted_count; /* 56 8 */ /* --- cacheline 1 boundary (64 bytes) --- */ u64 abort_count; /* 64 8 */ u64 cycles_count; /* 72 8 */ u64 iter_count; /* 80 8 */ u64 iter_cycles; /* 88 8 */ struct branch_type_stat brtype_stat; /* 96 232 */ /* --- cacheline 5 boundary (320 bytes) was 8 bytes ago --- */ const char * srcline; /* 328 8 */ struct list_head list; /* 336 16 */ /* size: 352, cachelines: 6, members: 13 */ /* sum members: 346, holes: 1, sum holes: 6 */ /* last cacheline: 32 bytes */ }; ``` After: ``` struct callchain_list { u64 ip; /* 0 8 */ struct map_symbol ms; /* 8 24 */ struct { _Bool unfolded; /* 32 1 */ _Bool has_children; /* 33 1 */ }; /* 32 2 */ /* XXX 6 bytes hole, try to pack */ u64 branch_count; /* 40 8 */ u64 from_count; /* 48 8 */ u64 predicted_count; /* 56 8 */ /* --- cacheline 1 boundary (64 bytes) --- */ u64 abort_count; /* 64 8 */ u64 cycles_count; /* 72 8 */ u64 iter_count; /* 80 8 */ u64 iter_cycles; /* 88 8 */ struct branch_type_stat * brtype_stat; /* 96 8 */ const char * srcline; /* 104 8 */ struct list_head list; /* 112 16 */ /* size: 128, cachelines: 2, members: 13 */ /* sum members: 122, holes: 1, sum holes: 6 */ }; ``` Signed-off-by: Ian Rogers --- tools/perf/util/callchain.c | 41 +++++++++++++++++++++++++++++-------- tools/perf/util/callchain.h | 2 +- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index cde4860e6f28..5349c6a21849 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -586,7 +586,7 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor) call = zalloc(sizeof(*call)); if (!call) { perror("not enough memory for the code path tree"); - return -1; + return -ENOMEM; } call->ip = cursor_node->ip; call->ms = cursor_node->ms; @@ -602,7 +602,15 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor) * branch_from is set with value somewhere else * to imply it's "to" of a branch. */ - call->brtype_stat.branch_to = true; + if (!call->brtype_stat) { + call->brtype_stat = zalloc(sizeof(*call->brtype_stat)); + if (!call->brtype_stat) { + perror("not enough memory for the code path branch statisitcs"); + free(call->brtype_stat); + return -ENOMEM; + } + } + call->brtype_stat->branch_to = true; if (cursor_node->branch_flags.predicted) call->predicted_count = 1; @@ -610,7 +618,7 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor) if (cursor_node->branch_flags.abort) call->abort_count = 1; - branch_type_count(&call->brtype_stat, + branch_type_count(call->brtype_stat, &cursor_node->branch_flags, cursor_node->branch_from, cursor_node->ip); @@ -618,7 +626,8 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor) /* * It's "from" of a branch */ - call->brtype_stat.branch_to = false; + if (call->brtype_stat && call->brtype_stat->branch_to) + call->brtype_stat->branch_to = false; call->cycles_count = cursor_node->branch_flags.cycles; call->iter_count = cursor_node->nr_loop_iter; @@ -652,6 +661,7 @@ add_child(struct callchain_node *parent, list_del_init(&call->list); map__zput(call->ms.map); maps__zput(call->ms.maps); + zfree(&call->brtype_stat); free(call); } free(new); @@ -762,7 +772,14 @@ static enum match_result match_chain(struct callchain_cursor_node *node, /* * It's "to" of a branch */ - cnode->brtype_stat.branch_to = true; + if (!cnode->brtype_stat) { + cnode->brtype_stat = zalloc(sizeof(*cnode->brtype_stat)); + if (!cnode->brtype_stat) { + perror("not enough memory for the code path branch statisitcs"); + return MATCH_ERROR; + } + } + cnode->brtype_stat->branch_to = true; if (node->branch_flags.predicted) cnode->predicted_count++; @@ -770,7 +787,7 @@ static enum match_result match_chain(struct callchain_cursor_node *node, if (node->branch_flags.abort) cnode->abort_count++; - branch_type_count(&cnode->brtype_stat, + branch_type_count(cnode->brtype_stat, &node->branch_flags, node->branch_from, node->ip); @@ -778,7 +795,8 @@ static enum match_result match_chain(struct callchain_cursor_node *node, /* * It's "from" of a branch */ - cnode->brtype_stat.branch_to = false; + if (cnode->brtype_stat && cnode->brtype_stat->branch_to) + cnode->brtype_stat->branch_to = false; cnode->cycles_count += node->branch_flags.cycles; cnode->iter_count += node->nr_loop_iter; cnode->iter_cycles += node->iter_cycles; @@ -1026,6 +1044,7 @@ merge_chain_branch(struct callchain_cursor *cursor, maps__zput(ms.maps); map__zput(list->ms.map); maps__zput(list->ms.maps); + zfree(&list->brtype_stat); free(list); } @@ -1447,11 +1466,14 @@ static int callchain_counts_printf(FILE *fp, char *bf, int bfsize, int callchain_list_counts__printf_value(struct callchain_list *clist, FILE *fp, char *bf, int bfsize) { + static const struct branch_type_stat empty_brtype_stat = {}; + const struct branch_type_stat *brtype_stat; u64 branch_count, predicted_count; u64 abort_count, cycles_count; u64 iter_count, iter_cycles; u64 from_count; + brtype_stat = clist->brtype_stat ?: &empty_brtype_stat; branch_count = clist->branch_count; predicted_count = clist->predicted_count; abort_count = clist->abort_count; @@ -1463,7 +1485,7 @@ int callchain_list_counts__printf_value(struct callchain_list *clist, return callchain_counts_printf(fp, bf, bfsize, branch_count, predicted_count, abort_count, cycles_count, iter_count, iter_cycles, - from_count, &clist->brtype_stat); + from_count, brtype_stat); } static void free_callchain_node(struct callchain_node *node) @@ -1476,6 +1498,7 @@ static void free_callchain_node(struct callchain_node *node) list_del_init(&list->list); map__zput(list->ms.map); maps__zput(list->ms.maps); + zfree(&list->brtype_stat); free(list); } @@ -1483,6 +1506,7 @@ static void free_callchain_node(struct callchain_node *node) list_del_init(&list->list); map__zput(list->ms.map); maps__zput(list->ms.maps); + zfree(&list->brtype_stat); free(list); } @@ -1569,6 +1593,7 @@ int callchain_node__make_parent_list(struct callchain_node *node) list_del_init(&chain->list); map__zput(chain->ms.map); maps__zput(chain->ms.maps); + zfree(&chain->brtype_stat); free(chain); } return -ENOMEM; diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h index d2618a47deca..86e8a9e81456 100644 --- a/tools/perf/util/callchain.h +++ b/tools/perf/util/callchain.h @@ -129,7 +129,7 @@ struct callchain_list { u64 cycles_count; u64 iter_count; u64 iter_cycles; - struct branch_type_stat brtype_stat; + struct branch_type_stat *brtype_stat; const char *srcline; struct list_head list; }; From patchwork Tue Oct 24 22:23:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157723 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237276vqx; Tue, 24 Oct 2023 15:25:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF84B2IwKncjmQO1vA7KhaHFVHHr0p6GpRda14kxC88O+wCvDdP5ioxgOGG0VHyzcZMCFq/ X-Received: by 2002:a17:902:ecc6:b0:1c9:cc41:76e4 with SMTP id a6-20020a170902ecc600b001c9cc4176e4mr11246722plh.10.1698186334914; Tue, 24 Oct 2023 15:25:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186334; cv=none; d=google.com; s=arc-20160816; b=sENIgRRENdi5HYoPp3MZQ/5orD2aFdnIyYz2nA7MthWaaE1xN9Dck5d466KFfEZIzs Z9swdNVJFQcGbUGsxP/vuhSYD9W5rE5kldQ6CvCOud28FnD/5qpB5p4qxiLs8Uffuz3u UsQMjlsXF385/4ChjJhm7ELa7X9Z7d5CkziQzL/hxgHnRfpubaV2rkjEzQvwTLhXVb7O ARwbC2EtiSUScWRYT8vZvOL+qq/ziohIHZg1Y8yw2owxVEtWmgCX1D+5F5NTgfg6mgY7 /bW1rNFtksOcyudY1OOuB0ZnCuO/tffrLDo7/TBI+Xi+S9iJmsB8ohuP/gf5ecvhNSQl HprQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=fymRqgLhl5n1SIBrPc0QfOKYmvDTL24JHKj2ULWmDh8=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=ANm0wbkxr9QrIzbvAKtnlKh+8+b1eHcp6S1hBycGL7nekAKFG+Ju5td3WAk3+rjMIe gpOSPeQEJQB37LWIX0KVuZL5EYnu3yw+kMEYpJgjImheiqA1XG5ChJF+pw/AhrxURAIr hie1jaijfNjlvyHsaRNs8xX6/oL+5jZfFgH4SLAONn5FAjX+AsdPEqrMkb6OE4bdlJnA HqF/Ku7p1YnkKCrUPCv4jm13P1OKNmIkAQNWKp4pok/UCcnqfWQ2mxNj4ZHLLnxrTy3m p1YLFR2OiSyxeVxTrdTdSJoR8yfq1+oPWuip2XqOme9ETXWuu7axX2BGZC1PqZesZqZu E8Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MB6fSisQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id ij26-20020a170902ab5a00b001c9cc838d72si8640639plb.125.2023.10.24.15.25.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MB6fSisQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 20A6A8020C10; Tue, 24 Oct 2023 15:25:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344497AbjJXWZM (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344541AbjJXWYy (ORCPT ); Tue, 24 Oct 2023 18:24:54 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 016A51998 for ; Tue, 24 Oct 2023 15:24:37 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a81a80097fso65155077b3.3 for ; Tue, 24 Oct 2023 15:24:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186275; x=1698791075; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=fymRqgLhl5n1SIBrPc0QfOKYmvDTL24JHKj2ULWmDh8=; b=MB6fSisQ9Mzg+nwYpdT9l1RYvP7E37uzK/PFXvFTgNtN/Aem2ahuiBBObur4VXkFVo p9RcgL9HdBEekHsLtX/Yf2xzTLGU4StIUU5bA6UiUaSVq4z06w4TAYEKckw6gx03sW1N EQGwHUpZ32bLHGA1AxHd5PLMVS0yges3zPcF2Pl3yhdlKnbLUUE55ALZ6L6oMScLVG/z Y+MLkEs/Ydz7hqs+oHCYKfIkuM+6NoNAOdhu3XBtPNyseQZZFzFCNwrHZXxCsNqPdEiE 0KdcYH/RQ5aum9KaqMn4egqsERjkcdTCmCdrqGHXFeLB29FgCwasQmxV+aiPfUFU4VRm wCDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186275; x=1698791075; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fymRqgLhl5n1SIBrPc0QfOKYmvDTL24JHKj2ULWmDh8=; b=HTC0sUYgrOcwOlGHSeT5pgBfYPUpcxapbRXgk68DIi/cq5Lq3TMxXHierFbnmkCO/7 ptCc3XYWEGEN0+vZL/nAL1DSQ22TrvK1Z3ILurqdfIU/ri/TzTyHPC8Kjug4HeNIbzBy ResmVpWhXbFe9DHxJW0p3V4ha5nO9or3o1JI8CDMUjUBoZqEWwZhmG8UoSPLl39rpQw+ KgRr0WjsB4EPW+FRQhcPEPyF1ClhCsFyszPq4ce9XlA49PQSG/JKWkIwOZAMyp957RnE pglG2njn0T44LEzeMalau6ITsIa5OMU07DSjyAMIQBryvO6SWK+mPQ8Wens47sGfdS8X 2flg== X-Gm-Message-State: AOJu0YwIYfVyZFQRk6VXWIHlZQtF1P90Rt2tHpZL0GqYC6YKvB3j4mTW ljZG95tkiNoMMw3l6KtXWZuz1EBz+6VZ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:76cc:0:b0:d9a:68de:16a1 with SMTP id r195-20020a2576cc000000b00d9a68de16a1mr273675ybc.0.1698186275302; Tue, 24 Oct 2023 15:24:35 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:13 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-11-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 10/50] perf callchain: Minor layout changes to callchain_list From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677434628352476 X-GMAIL-MSGID: 1780677434628352476 Avoid 6 byte hole for padding. Place more frequently used fields first in an attempt to use just 1 cacheline in the common case. Before: ``` struct callchain_list { u64 ip; /* 0 8 */ struct map_symbol ms; /* 8 24 */ struct { _Bool unfolded; /* 32 1 */ _Bool has_children; /* 33 1 */ }; /* 32 2 */ /* XXX 6 bytes hole, try to pack */ u64 branch_count; /* 40 8 */ u64 from_count; /* 48 8 */ u64 predicted_count; /* 56 8 */ /* --- cacheline 1 boundary (64 bytes) --- */ u64 abort_count; /* 64 8 */ u64 cycles_count; /* 72 8 */ u64 iter_count; /* 80 8 */ u64 iter_cycles; /* 88 8 */ struct branch_type_stat * brtype_stat; /* 96 8 */ const char * srcline; /* 104 8 */ struct list_head list; /* 112 16 */ /* size: 128, cachelines: 2, members: 13 */ /* sum members: 122, holes: 1, sum holes: 6 */ }; ``` After: ``` struct callchain_list { struct list_head list; /* 0 16 */ u64 ip; /* 16 8 */ struct map_symbol ms; /* 24 24 */ const char * srcline; /* 48 8 */ u64 branch_count; /* 56 8 */ /* --- cacheline 1 boundary (64 bytes) --- */ u64 from_count; /* 64 8 */ u64 cycles_count; /* 72 8 */ u64 iter_count; /* 80 8 */ u64 iter_cycles; /* 88 8 */ struct branch_type_stat * brtype_stat; /* 96 8 */ u64 predicted_count; /* 104 8 */ u64 abort_count; /* 112 8 */ struct { _Bool unfolded; /* 120 1 */ _Bool has_children; /* 121 1 */ }; /* 120 2 */ /* size: 128, cachelines: 2, members: 13 */ /* padding: 6 */ }; ``` Signed-off-by: Ian Rogers --- tools/perf/util/callchain.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h index 86e8a9e81456..d5c66345ae31 100644 --- a/tools/perf/util/callchain.h +++ b/tools/perf/util/callchain.h @@ -116,22 +116,22 @@ extern struct callchain_param callchain_param; extern struct callchain_param callchain_param_default; struct callchain_list { + struct list_head list; u64 ip; struct map_symbol ms; - struct /* for TUI */ { - bool unfolded; - bool has_children; - }; + const char *srcline; u64 branch_count; u64 from_count; - u64 predicted_count; - u64 abort_count; u64 cycles_count; u64 iter_count; u64 iter_cycles; struct branch_type_stat *brtype_stat; - const char *srcline; - struct list_head list; + u64 predicted_count; + u64 abort_count; + struct /* for TUI */ { + bool unfolded; + bool has_children; + }; }; /* From patchwork Tue Oct 24 22:23:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157729 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237688vqx; Tue, 24 Oct 2023 15:26:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLVOfn5Z6M0RNZpUq5gOSjYaYOlOQKSMm3dhafanQX1O3TgNca+SaHd5FsZmwjAWCkUc9+ X-Received: by 2002:a05:6870:8585:b0:1e9:c2b7:2c99 with SMTP id f5-20020a056870858500b001e9c2b72c99mr15340849oal.33.1698186394809; Tue, 24 Oct 2023 15:26:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186394; cv=none; d=google.com; s=arc-20160816; b=fetwr5LC9LuiE2Two1fq+rvQMnV+JsmKqrYVPoKAnOKz8rAF0bq8IDdWCI0hudLxlW y8HzsollwL/t0Yhy7F73RcGEwidPOddM9tfiWqaE4nivBOH9wQbfSmWc7/jNqESyvlEf CTSm4mi4ZfSFBDUlJUjjmwwqmLD9aL9kYxIvQvWGiWIlrf3ZCfO6bXfidZMVlfXhfigx yhDRKpdUxi0f2bKiw9a6gOMweD+m4+cCDpRTLSdq4zeEzD6/uE5Fc4YyYLbxgoJHAh9n C5AQpj3wP1wnr2u4bWM5kEKp4ggsStj1IfoMeKkbiSlOn79iMr2/NpKYUh357BKbNEw0 N8Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=qKe0Mkvuri05dA2HE591fwZ1gmZep5T1+9DDRS5n7Yw=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=v1PJTerqHKN+No5xUqWUVkuJ5806Js02waG7mts3KbTWWGsOzeKwtKO/BlncC/BHLl WlMULq4TvOKyXgCYNwLrmKlbCzCLMV7AbxDeSYXBwl4QOBM9BGeThcc1umwCqXFl3etp BrTWWK4tokTUXVt48JLjpfnOjR54qSvuQDEF+Yw51Cq0lPPC03pJN8rGZf/aUlF6Vkuy 0yhlEkozQYNsiz2jj1MrwG6UCKVtZWRH9Fq2lLotimxfVHLNqvv6GYFPYz0gJlWdTVFz 8+B9DBgvaz4o0/5ycJdh2KtS4ufoXXrYWxUFHnC6chRqJZfXp20fzMnJ7hNzu2o/oOKn N0LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="T0/PNkQU"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id bz39-20020a056a02062700b0056da0ae25a2si9275902pgb.32.2023.10.24.15.26.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="T0/PNkQU"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id AEF9180B6C2C; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344489AbjJXWZQ (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344491AbjJXWY6 (ORCPT ); Tue, 24 Oct 2023 18:24:58 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C305119B0 for ; Tue, 24 Oct 2023 15:24:39 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a90d6ab944so56797557b3.2 for ; Tue, 24 Oct 2023 15:24:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186277; x=1698791077; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=qKe0Mkvuri05dA2HE591fwZ1gmZep5T1+9DDRS5n7Yw=; b=T0/PNkQUjqZPlKDd/uv6T4jQUdGwQZNjsSd6Dsje8Dga2krp6apPzMYxe/cCDKsT8y f0wi8Ygi5VsRo0ysX2wBcMEbX/WxA4Qz0nkYk/sIdtwhyufhaDG/UDjYsCD4Ob5hiyAa M9kWbSbZLgETXo4J05hJ51VSnQUtp4ftrMFPnUurlfMXOPuTPQyryl+kBQJ/IS/ag+NH /4SpIOCwElSIuGc0QPu2YaEMzPGT04kSXWLQ+P0yLekYwSUWceG0mLJKXrZKiJNI0HKp i3LiWtj0fUw1haf1b3Q/Yhlgji9i1OTRaA1RomLTJpVNmh7YOER2n3je5LVacsvbjcvL gxhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186277; x=1698791077; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qKe0Mkvuri05dA2HE591fwZ1gmZep5T1+9DDRS5n7Yw=; b=pZuE6+0NPRAAwIDYB5doIP0XJm+OGwdG0V3T/KWIT0lDoQDefJ9l6VW4kWUe3rSsID KoFrP6p31/XB/fg5FuSO4AGMzk9Wqc2VGaQb4ljbddyfBzq9A7+XgDaQaw6lRsvHo8Q4 HSj7YHxEPJMGHqy3ZW+X6TJPz6XJbY0sA6N4bjNS4ykOe6kqMYeb9Lcdlrf2XTqtjsWe QenyoUT7W0Cpdw4GGGCL2er4Rn/CoPel0VQoApg04gyszrdGEvPMvpKx4N0i4Orw6Zxj /KOwfwbA3Tll53a7MlAvLXIR/bKakXcnYAXI89vuoPZJSYcxgxq9VsuK/FfTXi/jxvey fDBg== X-Gm-Message-State: AOJu0Yx6oVjygPDwVvqUlsSIzMUuvTNVReEH3eIKTeEB2Qym+201u6Bo R4CjihjvOX9oD5tz8Xp2mDQmcG0p9sZL X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:d84a:0:b0:5a8:5653:3323 with SMTP id a71-20020a0dd84a000000b005a856533323mr271855ywe.2.1698186277302; Tue, 24 Oct 2023 15:24:37 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:14 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-12-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 11/50] perf mem_info: Add and use map_symbol__exit and addr_map_symbol__exit From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677497089981030 X-GMAIL-MSGID: 1780677497089981030 Fix leak where mem_info__put wouldn't release the maps/map as used by perf mem. Add exit functions and use elsewhere that the maps and map are released. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/callchain.c | 27 +++++++++------------------ tools/perf/util/hist.c | 28 ++++++++++++---------------- tools/perf/util/machine.c | 6 ++---- tools/perf/util/map_symbol.c | 15 +++++++++++++++ tools/perf/util/map_symbol.h | 4 ++++ tools/perf/util/symbol.c | 5 ++++- 7 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 tools/perf/util/map_symbol.c diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 0ea5a9d368d4..96058f949ec9 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -49,6 +49,7 @@ perf-y += dso.o perf-y += dsos.o perf-y += symbol.o perf-y += symbol_fprintf.o +perf-y += map_symbol.o perf-y += color.o perf-y += color_config.o perf-y += metricgroup.o diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 5349c6a21849..99cce43ba152 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -659,8 +659,7 @@ add_child(struct callchain_node *parent, list_for_each_entry_safe(call, tmp, &new->val, list) { list_del_init(&call->list); - map__zput(call->ms.map); - maps__zput(call->ms.maps); + map_symbol__exit(&call->ms); zfree(&call->brtype_stat); free(call); } @@ -1040,10 +1039,8 @@ merge_chain_branch(struct callchain_cursor *cursor, }; callchain_cursor_append(cursor, list->ip, &ms, false, NULL, 0, 0, 0, list->srcline); list_del_init(&list->list); - map__zput(ms.map); - maps__zput(ms.maps); - map__zput(list->ms.map); - maps__zput(list->ms.maps); + map_symbol__exit(&ms); + map_symbol__exit(&list->ms); zfree(&list->brtype_stat); free(list); } @@ -1096,8 +1093,7 @@ int callchain_cursor_append(struct callchain_cursor *cursor, } node->ip = ip; - maps__zput(node->ms.maps); - map__zput(node->ms.map); + map_symbol__exit(&node->ms); node->ms = *ms; node->ms.maps = maps__get(ms->maps); node->ms.map = map__get(ms->map); @@ -1496,16 +1492,14 @@ static void free_callchain_node(struct callchain_node *node) list_for_each_entry_safe(list, tmp, &node->parent_val, list) { list_del_init(&list->list); - map__zput(list->ms.map); - maps__zput(list->ms.maps); + map_symbol__exit(&list->ms); zfree(&list->brtype_stat); free(list); } list_for_each_entry_safe(list, tmp, &node->val, list) { list_del_init(&list->list); - map__zput(list->ms.map); - maps__zput(list->ms.maps); + map_symbol__exit(&list->ms); zfree(&list->brtype_stat); free(list); } @@ -1591,8 +1585,7 @@ int callchain_node__make_parent_list(struct callchain_node *node) out: list_for_each_entry_safe(chain, new, &head, list) { list_del_init(&chain->list); - map__zput(chain->ms.map); - maps__zput(chain->ms.maps); + map_symbol__exit(&chain->ms); zfree(&chain->brtype_stat); free(chain); } @@ -1676,10 +1669,8 @@ void callchain_cursor_reset(struct callchain_cursor *cursor) cursor->nr = 0; cursor->last = &cursor->first; - for (node = cursor->first; node != NULL; node = node->next) { - map__zput(node->ms.map); - maps__zput(node->ms.maps); - } + for (node = cursor->first; node != NULL; node = node->next) + map_symbol__exit(&node->ms); } void callchain_param_setup(u64 sample_type, const char *arch) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 0aa7e231172a..0888b7163b7c 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -515,17 +515,16 @@ static int hist_entry__init(struct hist_entry *he, err_infos: if (he->branch_info) { - map__put(he->branch_info->from.ms.map); - map__put(he->branch_info->to.ms.map); + map_symbol__exit(&he->branch_info->from.ms); + map_symbol__exit(&he->branch_info->to.ms); zfree(&he->branch_info); } if (he->mem_info) { - map__put(he->mem_info->iaddr.ms.map); - map__put(he->mem_info->daddr.ms.map); + map_symbol__exit(&he->mem_info->iaddr.ms); + map_symbol__exit(&he->mem_info->daddr.ms); } err: - maps__zput(he->ms.maps); - map__zput(he->ms.map); + map_symbol__exit(&he->ms); zfree(&he->stat_acc); return -ENOMEM; } @@ -1317,20 +1316,19 @@ void hist_entry__delete(struct hist_entry *he) struct hist_entry_ops *ops = he->ops; thread__zput(he->thread); - maps__zput(he->ms.maps); - map__zput(he->ms.map); + map_symbol__exit(&he->ms); if (he->branch_info) { - map__zput(he->branch_info->from.ms.map); - map__zput(he->branch_info->to.ms.map); + map_symbol__exit(&he->branch_info->from.ms); + map_symbol__exit(&he->branch_info->to.ms); zfree_srcline(&he->branch_info->srcline_from); zfree_srcline(&he->branch_info->srcline_to); zfree(&he->branch_info); } if (he->mem_info) { - map__zput(he->mem_info->iaddr.ms.map); - map__zput(he->mem_info->daddr.ms.map); + map_symbol__exit(&he->mem_info->iaddr.ms); + map_symbol__exit(&he->mem_info->daddr.ms); mem_info__zput(he->mem_info); } @@ -2700,10 +2698,8 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, *total_cycles += bi[i].flags.cycles; } for (unsigned int i = 0; i < bs->nr; i++) { - map__put(bi[i].to.ms.map); - maps__put(bi[i].to.ms.maps); - map__put(bi[i].from.ms.map); - maps__put(bi[i].from.ms.maps); + map_symbol__exit(&bi[i].to.ms); + map_symbol__exit(&bi[i].from.ms); } free(bi); } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 31fdbb0f27af..90c750150b19 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2389,8 +2389,7 @@ static int add_callchain_ip(struct thread *thread, iter_cycles, branch_from, srcline); out: addr_location__exit(&al); - maps__put(ms.maps); - map__put(ms.map); + map_symbol__exit(&ms); return err; } @@ -3116,8 +3115,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms if (ret != 0) return ret; } - map__put(ilist_ms.map); - maps__put(ilist_ms.maps); + map_symbol__exit(&ilist_ms); return ret; } diff --git a/tools/perf/util/map_symbol.c b/tools/perf/util/map_symbol.c new file mode 100644 index 000000000000..bef5079f2403 --- /dev/null +++ b/tools/perf/util/map_symbol.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "map_symbol.h" +#include "maps.h" +#include "map.h" + +void map_symbol__exit(struct map_symbol *ms) +{ + maps__zput(ms->maps); + map__zput(ms->map); +} + +void addr_map_symbol__exit(struct addr_map_symbol *ams) +{ + map_symbol__exit(&ams->ms); +} diff --git a/tools/perf/util/map_symbol.h b/tools/perf/util/map_symbol.h index e08817b0c30f..72d5ed938ed6 100644 --- a/tools/perf/util/map_symbol.h +++ b/tools/perf/util/map_symbol.h @@ -22,4 +22,8 @@ struct addr_map_symbol { u64 phys_addr; u64 data_page_size; }; + +void map_symbol__exit(struct map_symbol *ms); +void addr_map_symbol__exit(struct addr_map_symbol *ams); + #endif // __PERF_MAP_SYMBOL diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 822f4dcebfe6..82cc74b9358e 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -2791,8 +2791,11 @@ struct mem_info *mem_info__get(struct mem_info *mi) void mem_info__put(struct mem_info *mi) { - if (mi && refcount_dec_and_test(&mi->refcnt)) + if (mi && refcount_dec_and_test(&mi->refcnt)) { + addr_map_symbol__exit(&mi->iaddr); + addr_map_symbol__exit(&mi->daddr); free(mi); + } } struct mem_info *mem_info__new(void) From patchwork Tue Oct 24 22:23:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157722 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237262vqx; Tue, 24 Oct 2023 15:25:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEmU5GhrZ+av9smOBsvG1+fvjxTipU9OgJeVibDpJpWdoTqMvHoTj0TptYJRv4KQ6okrl91 X-Received: by 2002:a05:6359:2181:b0:168:e438:15e5 with SMTP id lr1-20020a056359218100b00168e43815e5mr5679303rwb.29.1698186331164; Tue, 24 Oct 2023 15:25:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186331; cv=none; d=google.com; s=arc-20160816; b=Cu8a+RMGXPR47NZwMgLDc7UR08E/cP8dzMA+Z6W6JiOmG6IzPowKSKTrLDGifPPwBO hQ/Vsx6tV8Y90XXJmzvtuaOxzfIZAwZUSDvnG54GsRvLK7mLTB4uHijp8k3oQGokorpg C66XrVRQAIeTjlDCpowAvuuHEGPnjeMWz05c09igjvouX2oYa4yI+rsK3nyqOmqbdiVP 2oeDNpE1g09iQhy8Rz7m3FvpaZLVMY1gdqGK3F8HOU9HLjgV1A0iOIFa5b0sElokyxek W3dgr8JkVXGE+3vZNx/cVdvmRIH/PrzAgrZRry6irRWxIZ0PEavt1pSF22+DcYWuisl+ F+HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=m3RrFaAylSJfbHKcTDzvj4sdZE51LV5H16MsLijA4K8=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=CMpi8xqVa3LCLTu51ImQ8EM8lhxNVuajFkcJrgsMJ4q+uoBuvhsJ4XN5basCUJuWE8 X1g4KApEVHR77k5Ol5msqtn5GN8ECBWIQJw0YRXWLQo5FsKyCYWQmqUF9PGPKs3B6Q+R 9k7TCqqIKO4WoDOayaGduBFxuKuw31/KbXZrga5DcH6mmAjgM7xkRIHM+uPDsexImkez U/DHkxDTx2PZChySnTBkh/Ro1qc5/nmCjGfwxnRakyYeM1PCu0bBrZYj6FOIeQMiwFHP Lp99+M1Mkw/GqXtQ4gcj0izl/ZUqFLiJW86/Ztj6hjnUgneBHE9C1iq+z0jdVhv4LrdC D1gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="UEQxx/EE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id v202-20020a6361d3000000b005b74de09ee0si9054255pgb.108.2023.10.24.15.25.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:25:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="UEQxx/EE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 530578028FDF; Tue, 24 Oct 2023 15:25:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344569AbjJXWZX (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344558AbjJXWZG (ORCPT ); Tue, 24 Oct 2023 18:25:06 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46CB510D1 for ; Tue, 24 Oct 2023 15:24:41 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a839b31a0dso94314447b3.0 for ; Tue, 24 Oct 2023 15:24:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186280; x=1698791080; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=m3RrFaAylSJfbHKcTDzvj4sdZE51LV5H16MsLijA4K8=; b=UEQxx/EE0WtiG9dMIp4bJpg1YKY88bG+AufaRZWd9HFwxpHH5JY2Fl1U16waQAeGEO uRa+PYvkIDGXPxN6K6ecLrp2PIQem4YVBdIEinc0kRyfAHOMkclMGXkJsCKfq+8wz9no of0a/G3PxbIgUTFZ1Zgy2ERPtV83GcjlyIHcDmnccPogIiVL//y7hufQC85+tfkZyX++ 8yt+S72qyrnHrWRwu23IlpgX2Rtjo4bq4H4vi5gjVvJWTJItoe4xo9T3DGF+vkJ1Qyei lw020vG7K2N3so94j/txWSbAWkF/3fHzhCxH9grblZsqpZiw/bjb0KIsai0xuxHOUs4E MG6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186280; x=1698791080; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=m3RrFaAylSJfbHKcTDzvj4sdZE51LV5H16MsLijA4K8=; b=pwmEFzHCY1hvr9iWWDGOeU4MrgWt2ucqv6jwBE/1xHmgyZTwYrTFuW+hznB0k/TYuh Fq9RxEwky2/pGbTUNP5BiHYc7BtOzJ9qAai2aGf6vudP4Tn3gNuryhnTr4RmTFVH7+2q r4SDT9pjE+OtRv5tFkZkxJOEJigrgnPgnmFIYwhuivVIZU2yRjRJQnmBQp1zJu/RKk9T VwN66A3pUQvDbmJBhByJ9H82RBNBH/0QewTwLLhtoQpkW5dhUWUAWmXwyIkSQBaHDW9+ MtyQNOaDh0qr3aomodF9oTSYAMsX2igV5Cnqfx6g2zuBYXL7Mc8elyeZSviYgyRQBDwx vLNw== X-Gm-Message-State: AOJu0YzwIJIU5DmWdu3m6SgrE1gUIOlyw/2KnT1CtouB5jO3HiV/uE1j xBco5SuELiURBF/7tO2c+vVb6hDhuDml X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:5206:0:b0:5a7:b672:4d88 with SMTP id g6-20020a815206000000b005a7b6724d88mr288070ywb.0.1698186279736; Tue, 24 Oct 2023 15:24:39 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:15 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-13-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 12/50] perf record: Lazy load kernel symbols From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677429969333615 X-GMAIL-MSGID: 1780677429969333615 Commit 5b7ba82a7591 ("perf symbols: Load kernel maps before using") changed it so that loading a kernel dso would cause the symbols for the dso to be eagerly loaded. For perf record this is overhead as the symbols won't be used. Add a symbol_conf to control the behavior and disable it for perf record and perf inject. Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 6 ++++++ tools/perf/builtin-record.c | 2 ++ tools/perf/util/event.c | 4 ++-- tools/perf/util/symbol_conf.h | 3 ++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index c8cf2fdd9cff..eb3ef5c24b66 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -2265,6 +2265,12 @@ int cmd_inject(int argc, const char **argv) "perf inject []", NULL }; + + if (!inject.itrace_synth_opts.set) { + /* Disable eager loading of kernel symbols that adds overhead to perf inject. */ + symbol_conf.lazy_load_kernel_maps = true; + } + #ifndef HAVE_JITDUMP set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true); #endif diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index dcf288a4fb9a..8ec818568662 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3989,6 +3989,8 @@ int cmd_record(int argc, const char **argv) # undef set_nobuild #endif + /* Disable eager loading of kernel symbols that adds overhead to perf record. */ + symbol_conf.lazy_load_kernel_maps = true; rec->opts.affinity = PERF_AFFINITY_SYS; rec->evlist = evlist__new(); diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 923c0fb15122..68f45e9e63b6 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -617,13 +617,13 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { al->level = 'k'; maps = machine__kernel_maps(machine); - load_map = true; + load_map = !symbol_conf.lazy_load_kernel_maps; } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { al->level = '.'; } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { al->level = 'g'; maps = machine__kernel_maps(machine); - load_map = true; + load_map = !symbol_conf.lazy_load_kernel_maps; } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { al->level = 'u'; } else { diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 0b589570d1d0..2b2fb9e224b0 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -42,7 +42,8 @@ struct symbol_conf { inline_name, disable_add2line_warn, buildid_mmap2, - guest_code; + guest_code, + lazy_load_kernel_maps; const char *vmlinux_name, *kallsyms_name, *source_prefix, From patchwork Tue Oct 24 22:23:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157724 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237424vqx; Tue, 24 Oct 2023 15:26:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE62VQdVo47xj1TjgaH65rwDjdcdjBajSXbicqvyUJ3jhFE55u9WHlbsOunr6re2PCkSdvU X-Received: by 2002:a17:902:e88e:b0:1c6:2b9d:56ce with SMTP id w14-20020a170902e88e00b001c62b9d56cemr11553321plg.21.1698186362544; Tue, 24 Oct 2023 15:26:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186362; cv=none; d=google.com; s=arc-20160816; b=CdGP4DD1+R3zVLMpxGqIIq1x5lPs5A/ONdi5fU+RcTUPzAMu7n2WY1eiamiJwqQo4F XhVHFHQs5hPY+PmVRgJ12d+kQ2nOqArtsbq/6MtCpSvbkmQNp7WxuzpvIM7lu2WXqT/X PI6s7ws2kVuceXRWFxpQjX9wz2zkamGmNEEAXaW4oH7UXauXb1x2LHLnePHmjTrbtQiz E9yf9KDIZfrMy3OuPbqRsBge1Ajo5ra+tjE8zHlld3kbizX3tB1PG8ahhBqFjhxel/uI M3EDWh4rNY9D6e6ChlWitG2O6QVfl5NqAb7z64a6xivJ0fGs4AZhBuB4WSB9/OIcvspP MR3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=a57oV/e0Ni3jLZTf3mm6h3wI1Kphc9bMaGQTthj78GI=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=YlDeLFwZ4F+CFW8S11gUynjx5nY6HgnN1mKGoeRRsu5O2oIMCyT+OBtWME6OeMPg99 CcUC2XDTfDu0enMVSsK87qYyE+Q3OMdFq09yVgAWAbtL3MePorXhhsVbGaonSb3Q3XON T268RW2nDnzuZH0LjrIjrCVxiggdBcJ7i3mXmXl8owIop0gA1xjCadtn9zAMSeYNey3M xdt/3xnMi79quzWVCjpiOu/RMxeWr/NycPrdXh98lyUDU8Boq3R8WzPvBe99cZT1uzgL VfA6mrBZdWu3gSzvgp9AfjGtIXi/bPqO5Nyg1ydiu33055uvVbeRTX0HSt8hDuw53EtA HCZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lAtV+NBS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id n8-20020a170902e54800b001c60636e426si4082494plf.432.2023.10.24.15.26.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lAtV+NBS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 9EBD0801F957; Tue, 24 Oct 2023 15:25:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344529AbjJXWZc (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344525AbjJXWZN (ORCPT ); Tue, 24 Oct 2023 18:25:13 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7163410E2 for ; Tue, 24 Oct 2023 15:24:45 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a3a38b96cso5573853276.0 for ; Tue, 24 Oct 2023 15:24:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186282; x=1698791082; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=a57oV/e0Ni3jLZTf3mm6h3wI1Kphc9bMaGQTthj78GI=; b=lAtV+NBSe+UpA5Rr+Qd3pgNNsrTd3pDBkC2VnSTgUeHV+/WWSNMzgqe+hFljO5X3Xe OW+vvhGQnJNm4LaVzIjI7Mlg1SDO/g6gUfX4HKCffk2z6HJ+26ZvARpkL3E+8ZHfEiRQ p4rx+J3d+OOBZnyPb4c6C1dmCCQInCcRR5niZhlW3zmENUqwYtFwfHrtPghdVmE5ViZD /OYXinPQu9CTdOApQBa9RBJx97eEE4TJiizBdQRj7eIwZO2UHpilM+OauJBRMUvxVxYt oeej7fuVOb+dKVoIcE/v1jPDbO8BUU2XBfNY5t1J5vouR/+WumYlOvgYAyAYc4ztMILH 0YUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186282; x=1698791082; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=a57oV/e0Ni3jLZTf3mm6h3wI1Kphc9bMaGQTthj78GI=; b=Sm/ze2VOKNreCitf6wm1C8M8YpRpE7m3vhjxNN9qUbMASVbVCFo8wAj76kfUlNbHQE RGH0oWqA08IykJXnqv5Ji2teZeyTYdcLEVpy74rABjxJCd+4K+W9ZeLJwJDJodznh3fh SmVDNbKP86ZIUwOTWuYaWI5lqnkLHtSr7UF8LDFiWX4DqfNuoKjiUkz+zusA2T/h4dO8 SKk4bWhCw88SM6jfyQ4D5JKG5jSzqaVO/iTDDnj/pR8u1t+cKM7+1RUN5xgBSS91ZsNw IKsN+MgBzvX2c2Rt+Ik6h2JVA1s3Z0tEsJ1krKlgpjlsPVzMgLGHhXb3wD8jSbowoiaB mYZQ== X-Gm-Message-State: AOJu0YyHCm3FHO+ELtflow5uCYGShwaP9IHntBJO2EEOLm5w1hX++GwP qEEJxPFJ/YkuIWVgLLkxH+iyhMzZRrKU X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:4f4c:961b with SMTP id v3-20020a056902108300b00d9a4f4c961bmr353600ybu.1.1698186282072; Tue, 24 Oct 2023 15:24:42 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:16 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-14-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 13/50] libperf: Lazily allocate mmap event copy From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:25:57 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677463340936230 X-GMAIL-MSGID: 1780677463340936230 The event copy in the mmap is used to have storage to a read event. Not all users of mmaps read the events, such as perf record, so switch the allocation to being on first read rather than being embedded within the perf_mmap. Signed-off-by: Ian Rogers --- tools/lib/perf/include/internal/mmap.h | 2 +- tools/lib/perf/mmap.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h index 5a062af8e9d8..b11aaf5ed645 100644 --- a/tools/lib/perf/include/internal/mmap.h +++ b/tools/lib/perf/include/internal/mmap.h @@ -33,7 +33,7 @@ struct perf_mmap { bool overwrite; u64 flush; libperf_unmap_cb_t unmap_cb; - char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); + void *event_copy; struct perf_mmap *next; }; diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c index 2184814b37dd..91ae46aac378 100644 --- a/tools/lib/perf/mmap.c +++ b/tools/lib/perf/mmap.c @@ -51,6 +51,8 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, void perf_mmap__munmap(struct perf_mmap *map) { + free(map->event_copy); + map->event_copy = NULL; if (map && map->base != NULL) { munmap(map->base, perf_mmap__mmap_len(map)); map->base = NULL; @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, unsigned int len = min(sizeof(*event), size), cpy; void *dst = map->event_copy; + if (!dst) { + dst = malloc(PERF_SAMPLE_MAX_SIZE); + if (!dst) + return NULL; + map->event_copy = dst; + } + do { cpy = min(map->mask + 1 - (offset & map->mask), len); memcpy(dst, &data[offset & map->mask], cpy); From patchwork Tue Oct 24 22:23:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157725 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237434vqx; Tue, 24 Oct 2023 15:26:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGerqRvlyPipHLGhtxQQHxt0CsVI8BWj8biil+HQMiKCuanB2+y8zdBIH9RJjSA9e/YVcGT X-Received: by 2002:a17:90a:ba15:b0:27d:1a75:5b98 with SMTP id s21-20020a17090aba1500b0027d1a755b98mr22605474pjr.12.1698186363633; Tue, 24 Oct 2023 15:26:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186363; cv=none; d=google.com; s=arc-20160816; b=Ou5EsPuSt9atCIPIVo/H33ofrV8xVBF/KiiCufSrccYZWRsSR5Xd7iDHyDU7uM7NeQ t6p/EzRRLfuYyY/biKJqVLw9PDCsPlGXv5RcZKAHvzyUA8UVNbLBo5soOpuZ5J4T+L6j 51YAZaTnrIUp3TldRFSIOvwkk3ghvZtlseTAWIK7QxnTvPq24E3dMdpZxIhPHfHrNt2T d0b4b/0xBDZbHGgXX9cd2SZSm3mSvXbbODEJmSMMr4/Krz1ay2xII39wHxtOjDJtrxW5 HfKbB3UJjcLs114ZEe9OHlqK1xzXwtar0IzwKY24SPcIBePH+7QX5HXoobQqk6uViBx0 ZHpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=mFUu7p9S5YeiUqPkIKu4IgMBXIymBeuUApOrPlH1y/o=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=M/iiajbcq1TvIahqKww8oH2UmCVDGxVKG5/jwyqjvScEa0apThFfHtzHwdgS0TLJBF dVnrcKcgv94NXRVQdOaE95mutoiXxNb04T8Jo4MndsNvleFNUNkoj5kpWygxsLXY/EL7 NrSaK4fwGvoxAKV6Bzzd3ccVo4JEVYv3NqowXfgHR1aPqaddk8e3O3UqFd8It+SYmCcC fkT0yTGWS4UlFMwHC8kOLOJ/F1aaUJKuE9p2Bf6OouqUQhw7ayd+MZ477BO3bSmhwYkb waOpdNlCBzejfNd/oVsAN5d9WQNXy/w45NEoVWy17RDpEGRnJ25Mjk9nh04anDFFbN6r TvMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=X9yI6FUy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id ce21-20020a17090aff1500b00274274bf0ecsi8660013pjb.61.2023.10.24.15.26.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=X9yI6FUy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 0045580D07D0; Tue, 24 Oct 2023 15:25:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235068AbjJXWZu (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234965AbjJXWZ1 (ORCPT ); Tue, 24 Oct 2023 18:25:27 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C2BA1FC1 for ; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d8486b5e780so5794977276.0 for ; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186284; x=1698791084; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=mFUu7p9S5YeiUqPkIKu4IgMBXIymBeuUApOrPlH1y/o=; b=X9yI6FUy1ePkOXhvKAD9GqV+iHt4OfjBw2bHjlDjqYkkOzOO8SJo0aitaFi88R9q4d PcsXtoac0P1kCi+WPk/c/B3aRHDMGdDS8BJ0Po4dUsLXYoUQLMUqo+RAdJb7GtxmbNT9 1NHVgKxorsA+cswmelXKymelKOdD9jHsZIsOZJRqRZVGUY1nR9vfSbySycHz8irrqpLW GSiU4yVbpMRrQA8NZ1Jr0uOVSkSt8hd3hNpKvKymQMc7NcnuWXOJ0mSFrP/spgzB89fi 1gOCBSa3IQRmBuNnA6JkxpIpm7gccogElMpep9X92n9LAAdy7MKeeAPkD0eLHmJVzB3z SsSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186284; x=1698791084; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mFUu7p9S5YeiUqPkIKu4IgMBXIymBeuUApOrPlH1y/o=; b=ZC5ICiOxTYJgNtoiCdn8eItYeKYG2KatodKKo9XYD1Yd+BA3PO4PET/jI0e012Fmbu yydFPkQFGJj/WSZG0VnJR4R0mliu6Us9XTXQuqpc2qlstRP4QhA0Rs0PktM+iZQctvqV LoV/9YMua/XcoapRrhqEWvdUFg7GF+TVOrN61Dyy2VU3fbDvKCpZqtM1d+EQjELYY/z5 kuoan+YyWTnPkUIOeagGDT0FevzNTIymbRmMjPy3oOoCOu1Hn/ONTGJ/6ETPknlgopdr Leos2o6DDr+2PBdgB73ujV8k6H1L9bunKyokpOrSjn/NLyKTXppMOhm1Y05F+2jzLmEB Ew+w== X-Gm-Message-State: AOJu0Yx4EOe5KknvriiR6Dhxnk97o8OQ9d9RHczTN+vutwTiNdKiCrZJ UgoPAytPsafMt1X1Ru60fnW5KY2kc6j/ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:324b:0:b0:d9b:f88c:ca2b with SMTP id y72-20020a25324b000000b00d9bf88cca2bmr258286yby.11.1698186284456; Tue, 24 Oct 2023 15:24:44 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:17 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-15-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 14/50] perf mmap: Lazily initialize zstd streams From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:00 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677464270770165 X-GMAIL-MSGID: 1780677464270770165 Zstd streams create dictionaries that can require significant RAM, especially when there is one per-CPU. Tools like perf record won't use the streams without the -z option, and so the creation of the streams is pure overhead. Switch to creating the streams on first use. Signed-off-by: Ian Rogers --- tools/perf/builtin-record.c | 26 ++++++++++----- tools/perf/util/compress.h | 6 ++-- tools/perf/util/mmap.c | 5 ++- tools/perf/util/mmap.h | 1 - tools/perf/util/zstd.c | 63 +++++++++++++++++++------------------ 5 files changed, 58 insertions(+), 43 deletions(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 8ec818568662..9b4f3805ca92 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -270,7 +270,7 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused, static int record__aio_enabled(struct record *rec); static int record__comp_enabled(struct record *rec); -static size_t zstd_compress(struct perf_session *session, struct mmap *map, +static ssize_t zstd_compress(struct perf_session *session, struct mmap *map, void *dst, size_t dst_size, void *src, size_t src_size); #ifdef HAVE_AIO_SUPPORT @@ -405,9 +405,13 @@ static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size */ if (record__comp_enabled(aio->rec)) { - size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size, - mmap__mmap_len(map) - aio->size, - buf, size); + ssize_t compressed = zstd_compress(aio->rec->session, NULL, aio->data + aio->size, + mmap__mmap_len(map) - aio->size, + buf, size); + if (compressed < 0) + return (int)compressed; + + size = compressed; } else { memcpy(aio->data + aio->size, buf, size); } @@ -633,7 +637,13 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) struct record *rec = to; if (record__comp_enabled(rec)) { - size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size); + ssize_t compressed = zstd_compress(rec->session, map, map->data, + mmap__mmap_len(map), bf, size); + + if (compressed < 0) + return (int)compressed; + + size = compressed; bf = map->data; } @@ -1527,10 +1537,10 @@ static size_t process_comp_header(void *record, size_t increment) return size; } -static size_t zstd_compress(struct perf_session *session, struct mmap *map, +static ssize_t zstd_compress(struct perf_session *session, struct mmap *map, void *dst, size_t dst_size, void *src, size_t src_size) { - size_t compressed; + ssize_t compressed; size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1; struct zstd_data *zstd_data = &session->zstd_data; @@ -1539,6 +1549,8 @@ static size_t zstd_compress(struct perf_session *session, struct mmap *map, compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size, max_record_size, process_comp_header); + if (compressed < 0) + return compressed; if (map && map->file) { thread->bytes_transferred += src_size; diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h index 0cd3369af2a4..9eb6eb5bf038 100644 --- a/tools/perf/util/compress.h +++ b/tools/perf/util/compress.h @@ -3,6 +3,7 @@ #define PERF_COMPRESS_H #include +#include #ifdef HAVE_ZSTD_SUPPORT #include #endif @@ -21,6 +22,7 @@ struct zstd_data { #ifdef HAVE_ZSTD_SUPPORT ZSTD_CStream *cstream; ZSTD_DStream *dstream; + int comp_level; #endif }; @@ -29,7 +31,7 @@ struct zstd_data { int zstd_init(struct zstd_data *data, int level); int zstd_fini(struct zstd_data *data); -size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size, +ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size, void *src, size_t src_size, size_t max_record_size, size_t process_header(void *record, size_t increment)); @@ -48,7 +50,7 @@ static inline int zstd_fini(struct zstd_data *data __maybe_unused) } static inline -size_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused, +ssize_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused, void *dst __maybe_unused, size_t dst_size __maybe_unused, void *src __maybe_unused, size_t src_size __maybe_unused, size_t max_record_size __maybe_unused, diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c index 49093b21ee2d..122ee198a86e 100644 --- a/tools/perf/util/mmap.c +++ b/tools/perf/util/mmap.c @@ -295,15 +295,14 @@ int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, struct perf_cpu map->core.flush = mp->flush; - map->comp_level = mp->comp_level; #ifndef PYTHON_PERF - if (zstd_init(&map->zstd_data, map->comp_level)) { + if (zstd_init(&map->zstd_data, mp->comp_level)) { pr_debug2("failed to init mmap compressor, error %d\n", errno); return -1; } #endif - if (map->comp_level && !perf_mmap__aio_enabled(map)) { + if (mp->comp_level && !perf_mmap__aio_enabled(map)) { map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); if (map->data == MAP_FAILED) { diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h index f944c3cd5efa..0df6e1621c7e 100644 --- a/tools/perf/util/mmap.h +++ b/tools/perf/util/mmap.h @@ -39,7 +39,6 @@ struct mmap { #endif struct mmap_cpu_mask affinity_mask; void *data; - int comp_level; struct perf_data_file *file; struct zstd_data zstd_data; }; diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c index 48dd2b018c47..57027e0ac7b6 100644 --- a/tools/perf/util/zstd.c +++ b/tools/perf/util/zstd.c @@ -7,35 +7,9 @@ int zstd_init(struct zstd_data *data, int level) { - size_t ret; - - data->dstream = ZSTD_createDStream(); - if (data->dstream == NULL) { - pr_err("Couldn't create decompression stream.\n"); - return -1; - } - - ret = ZSTD_initDStream(data->dstream); - if (ZSTD_isError(ret)) { - pr_err("Failed to initialize decompression stream: %s\n", ZSTD_getErrorName(ret)); - return -1; - } - - if (!level) - return 0; - - data->cstream = ZSTD_createCStream(); - if (data->cstream == NULL) { - pr_err("Couldn't create compression stream.\n"); - return -1; - } - - ret = ZSTD_initCStream(data->cstream, level); - if (ZSTD_isError(ret)) { - pr_err("Failed to initialize compression stream: %s\n", ZSTD_getErrorName(ret)); - return -1; - } - + data->comp_level = level; + data->dstream = NULL; + data->cstream = NULL; return 0; } @@ -54,7 +28,7 @@ int zstd_fini(struct zstd_data *data) return 0; } -size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size, +ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size, void *src, size_t src_size, size_t max_record_size, size_t process_header(void *record, size_t increment)) { @@ -63,6 +37,21 @@ size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t ZSTD_outBuffer output; void *record; + if (!data->cstream) { + data->cstream = ZSTD_createCStream(); + if (data->cstream == NULL) { + pr_err("Couldn't create compression stream.\n"); + return -1; + } + + ret = ZSTD_initCStream(data->cstream, data->comp_level); + if (ZSTD_isError(ret)) { + pr_err("Failed to initialize compression stream: %s\n", + ZSTD_getErrorName(ret)); + return -1; + } + } + while (input.pos < input.size) { record = dst; size = process_header(record, 0); @@ -96,6 +85,20 @@ size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size ZSTD_inBuffer input = { src, src_size, 0 }; ZSTD_outBuffer output = { dst, dst_size, 0 }; + if (!data->dstream) { + data->dstream = ZSTD_createDStream(); + if (data->dstream == NULL) { + pr_err("Couldn't create decompression stream.\n"); + return 0; + } + + ret = ZSTD_initDStream(data->dstream); + if (ZSTD_isError(ret)) { + pr_err("Failed to initialize decompression stream: %s\n", + ZSTD_getErrorName(ret)); + return 0; + } + } while (input.pos < input.size) { ret = ZSTD_decompressStream(data->dstream, &output, &input); if (ZSTD_isError(ret)) { From patchwork Tue Oct 24 22:23:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157749 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238491vqx; Tue, 24 Oct 2023 15:28:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGM/8ZH5AEKidXx8uYJjhmg4YUfHu0T5VjS0A7ArkT8HOVi37yq7DPqPYtrMiwlVv57EyFL X-Received: by 2002:a17:903:1383:b0:1ca:4681:eb37 with SMTP id jx3-20020a170903138300b001ca4681eb37mr10489991plb.14.1698186518607; Tue, 24 Oct 2023 15:28:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186518; cv=none; d=google.com; s=arc-20160816; b=eVGCssXBDmTV9/ZL4Gl88/p2SSiI5k/tqc+Nn1U9r1sl9oWBDYkRLaC0HiZZpy/Gve +9vWrsFVrrUagqrzdzcEZjfwmrGqECkVGh4AzvCZ/k4+NUTsYT8y7ftYaSiAl6PLjMD7 LlULeBtOKjXm3VgGHqcfqPM9pgdE6lejGcK8+tM5jS4RFxfp0V77RHwNiGKf8qab6ZUj t2uMY/PvUAnVtZEqYWQ1K3vIV/Egoa0V3Sl6hYsGEpKB2LeAS7tjD8DkFvwOR+ZJNe0Y eH96zlrQ+5oawacNP8fCqRqKrpVUND+sdKLhl+UjNHDmUNHVioStEWQxr2fzlH99vy9b hEsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=WajoJckjAi5zxZaorb4dU2uWdDAXIk6mTnY41FG9EXU=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=BMjR6UhkrLO5xG/TVNBHTjhYsL7cJ5oXNBd6G4SZBX/aMOuADs46ZbP7BoaDT6la1S ApSIqZzZ8zONEh6UtmXQj82xSAZ4gSnLJCNqzEHQFLo0KZ2D5CUDl7zb55yWh2nCb5i3 1O/UjkNbs/rFltJjXWQEyH5A/e6SNaXiAMCN0UYVJ37DiaGcVjdTpek+ijey/6OJquep XEmn4+fCPtqTyoZw3D3LvZuyLfG0qe8UXuUG9JUM1Ab9t5McsArQXNvsDCj/B1yhJiw3 GGaMcZmv/AqiqKS6MPQyQujZ3Z9CmUppHY9TzxPe99BfHlAWghx0GT7azskBQRoTS25x +SAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JJ9VzC3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id l14-20020a170903244e00b001ca33bba8ecsi9752176pls.371.2023.10.24.15.28.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JJ9VzC3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 74A95801DD99; Tue, 24 Oct 2023 15:28:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344579AbjJXW1F (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344499AbjJXW0d (ORCPT ); Tue, 24 Oct 2023 18:26:33 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF0071FC9 for ; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9cad450d5fso5713242276.1 for ; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186286; x=1698791086; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=WajoJckjAi5zxZaorb4dU2uWdDAXIk6mTnY41FG9EXU=; b=JJ9VzC3bW43Azd5O2n6Of+NyrZ7Bb+RhUyU1rR3kznMY8YaBvpibNk77LFITLir0nC QUUq2cHXp5lAVByKtm2FPdnS3H61QBYhW3QyrNJkfdBiQtkEhaXZt63lmkiBGCooeq8i o6x1PEmm0/0b9AU/IdeQBlUWbT+WI2JtWKjFME+dKrIbYuKDhct8LlAOxzVF/HXP3gqt ucgspgiO9BqrQGG2VNBNd8JBohe3YWiLmHRNj8XwIUZ/RAdLVFVwlOFO7Qw70fUEfDT9 /fvYK0LHOSC9KgQlxb89RDWK+dSRVhlUnCG3OroUqxmEstbXfjhVjRipx2uVt8VwcKbl XtHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186286; x=1698791086; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WajoJckjAi5zxZaorb4dU2uWdDAXIk6mTnY41FG9EXU=; b=Mhq1exi4lRwiBazj6yrqRsGSEzVbs8zZ4OJGYOz/V12imX6xsRrquhP94zz561k40g 6pyQNLjVcOA9LHje/B1gdUAmoyNZJkkIKhg950J1Y5yO71p1UhIpneuh2zTtnxSmeboW tMiGjXmF9GBlfQwCKLvJeEgxGPCcTS8iOar2kc6UnQgX1EdLb55GTC6GFE9zunoWfYvD +ZiCAud2TCH58sT7NQuhQdmf/XM51ObzkAKU4xu2TEJCHoeUldFO7TBMrrh4FTN1tz/G rEwuBOKJ/7WdsJNPYujjjtG1jGvbKBgnOmOooa+8xw+1+Fc3P9g6mar41P+nXdaDDzVM mE7g== X-Gm-Message-State: AOJu0YxcikDxdsG544pHfZ8V9IfS8U6bYF8y5EufOikQ5DQjp2QAQxka M4ba4N5CU+/8KYcm/4+hqSfCzxJcu5xV X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:47ea:69a5 with SMTP id v3-20020a056902108300b00d9a47ea69a5mr366410ybu.1.1698186286686; Tue, 24 Oct 2023 15:24:46 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:18 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-16-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 15/50] perf machine thread: Remove exited threads by default From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:22 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677627061903283 X-GMAIL-MSGID: 1780677627061903283 struct thread values hold onto references to mmaps, dsos, etc. When a thread exits it is necessary to clean all of this memory up by removing the thread from the machine's threads. Some tools require this doesn't happen, such as auxtrace events, perf report if offcpu events exist or if a task list is being generated, so add a symbol_conf value to make the behavior optional. When an exited thread is left in the machine's threads, mark it as exited. This change relates to commit 40826c45eb0b ("perf thread: Remove notion of dead threads"). Dead threads were removed as they had a reference count of 0 and were difficult to reason about with the reference count checker. Here a thread is removed from threads when it exits, unless via symbol_conf the exited thread isn't remove and is marked as exited. Reference counting behaves as it normally does. Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 7 +++++++ tools/perf/util/machine.c | 10 +++++++--- tools/perf/util/session.c | 5 +++++ tools/perf/util/symbol_conf.h | 3 ++- tools/perf/util/thread.h | 14 ++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index dcedfe00f04d..749246817aed 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -1411,6 +1411,13 @@ int cmd_report(int argc, const char **argv) if (ret < 0) goto exit; + /* + * tasks_mode require access to exited threads to list those that are in + * the data file. Off-cpu events are synthesized after other events and + * reference exited threads. + */ + symbol_conf.keep_exited_threads = true; + annotation_options__init(&report.annotation_opts); ret = perf_config(report__config, &report); diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 90c750150b19..a985d004aa8d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2157,9 +2157,13 @@ int machine__process_exit_event(struct machine *machine, union perf_event *event if (dump_trace) perf_event__fprintf_task(event, stdout); - if (thread != NULL) - thread__put(thread); - + if (thread != NULL) { + if (symbol_conf.keep_exited_threads) + thread__set_exited(thread, /*exited=*/true); + else + machine__remove_thread(machine, thread); + } + thread__put(thread); return 0; } diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 1e9aa8ed15b6..c6afba7ab1a5 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -115,6 +115,11 @@ static int perf_session__open(struct perf_session *session, int repipe_fd) return -1; } + if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) { + /* Auxiliary events may reference exited threads, hold onto dead ones. */ + symbol_conf.keep_exited_threads = true; + } + if (perf_data__is_pipe(data)) return 0; diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 2b2fb9e224b0..6040286e07a6 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -43,7 +43,8 @@ struct symbol_conf { disable_add2line_warn, buildid_mmap2, guest_code, - lazy_load_kernel_maps; + lazy_load_kernel_maps, + keep_exited_threads; const char *vmlinux_name, *kallsyms_name, *source_prefix, diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index e79225a0ea46..0df775b5c110 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -36,13 +36,22 @@ struct thread_rb_node { }; DECLARE_RC_STRUCT(thread) { + /** @maps: mmaps associated with this thread. */ struct maps *maps; pid_t pid_; /* Not all tools update this */ + /** @tid: thread ID number unique to a machine. */ pid_t tid; + /** @ppid: parent process of the process this thread belongs to. */ pid_t ppid; int cpu; int guest_cpu; /* For QEMU thread */ refcount_t refcnt; + /** + * @exited: Has the thread had an exit event. Such threads are usually + * removed from the machine's threads but some events/tools require + * access to dead threads. + */ + bool exited; bool comm_set; int comm_len; struct list_head namespaces_list; @@ -189,6 +198,11 @@ static inline refcount_t *thread__refcnt(struct thread *thread) return &RC_CHK_ACCESS(thread)->refcnt; } +static inline void thread__set_exited(struct thread *thread, bool exited) +{ + RC_CHK_ACCESS(thread)->exited = exited; +} + static inline bool thread__comm_set(const struct thread *thread) { return RC_CHK_ACCESS(thread)->comm_set; From patchwork Tue Oct 24 22:23:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157732 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237731vqx; Tue, 24 Oct 2023 15:26:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEa8lmZHwMCpqS32TvGXN5O8wYz9OyF+wRMatAVUTlO05oO79fhtzErJLiB6yrkhJAqtzpW X-Received: by 2002:a17:90a:fe89:b0:27d:5a4b:6dc2 with SMTP id co9-20020a17090afe8900b0027d5a4b6dc2mr11008818pjb.11.1698186402530; Tue, 24 Oct 2023 15:26:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186402; cv=none; d=google.com; s=arc-20160816; b=SB7ryHgJVRZCRxiL9/z3bHWoWz1TVx+idpreDOkhEUtmz900panAtT/HIY7YpG6Sfc wqoPl/LSm0e/sp+X1bSO9g11cQSN0FbGPa2EsLQdw6TBpYLkyV5cnl9YLb3YO0rQMCNg 8FQAoyJ7GIzN8j3mGQCeRTP6Ui63b7HxRMp/e/7q4sp6wwlQ70CwB8MQwJlsOBFfxi+g oCKw83tQCpn/+LHLQrSM2UFqidMUHM+JjZvOOI1EZORi1sHr2LQwI1U3u5pA01p5b5SB PeECSniaF4pDNqxZQFC+PP5EVSKF1OcHAIc62dZcetj5mwBIvkliKIbLIUgGLB6dfmq1 hQVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=ATYLaWcN5bU+Dpgi7a7JWdfUJz5sZd2hiBE3rpvu+hs=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=CxPILCvxT9vAwKHN7HNUBEuqGuoc9pH5CRs1RVHrKfF8J6XqbZc68l6dQJRwffsYT7 V+eaOLmeC8pNZShor1vI0hHy9j0PIY0lRayEM3gSeJmr3QQ4tr8LUPaKKqGwvf13n74h SG+XCRA5t9iwnxrQxMUMbHeAcY2IheJR4cMkXbVsfq7QUF8vycXzrioYE8EWDcNPMFWW IUpwrgcQNwC+pHMCBrdRt3Rm5pS69t4No1rZCVl0ReRmwooCnucPdVUwMpOwCRnNwOby 4B+H2vzi1t2creflVDCeUWoeY2HTAva50NXEo6YtxY1h2cVsQvQi2B1I1hFsQBq9dMUB GzKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YX73kaQO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id lx18-20020a17090b4b1200b0027d702ca321si10649918pjb.124.2023.10.24.15.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YX73kaQO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 48361806AA22; Tue, 24 Oct 2023 15:26:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344545AbjJXW0C (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344586AbjJXWZf (ORCPT ); Tue, 24 Oct 2023 18:25:35 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32DE61FDB for ; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7ba10cb90so67236437b3.3 for ; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186289; x=1698791089; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ATYLaWcN5bU+Dpgi7a7JWdfUJz5sZd2hiBE3rpvu+hs=; b=YX73kaQOwuAStnFE3CSau5V0IlVcOWh41WNt+xkX/UsxmwQuGGrsZ4CXrp3e3AMPcK y2f9yD9kKb712LZF7vzSiQ2fvH/CLFP7tQxvWhB1/kOHI80Y9mB9qA/Fd98LJ0ZMuK3e p/FtL228WBsz1/KvF+g95duidgiIqJDbxrbQsx/wtXEMYI6jeEVjcRNCOdHN56t3Hlk+ XFUSEEUenG6MjLEJ3u3zFUDuRlVF0fnn1Is/M6KwfboSRDX+xTmNLM/ee4usdtT173Yz 7t/8+LBKGq7HlRqDHKLhxEeZgzc9sRkBXSppcO/juzkeEgFZ6f3NKpQZN7P5WButQJRD bsfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186289; x=1698791089; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ATYLaWcN5bU+Dpgi7a7JWdfUJz5sZd2hiBE3rpvu+hs=; b=ORCgdLjfTfjYtDhdUjCf7VKb1uhdFe4S8L7+Lt4dxllT46fcuX+0kpHzbNS00qwnCI BYL8x9JsYQ7ro63Ic83oenfDX3VjbqYYG0t9GMzhUyzQ2j5DfnRJg+0uyAx8R942CNXV olkCDeblMpIiEACHWnHmznQd2+ZYuWm/0MZ26g8BbxMQMH5BD0GrV2PCWRS8RIaW7zhL Jf8FYo3PO8OatDXc1P0VdZamtYSj+Cd/EUDmxuILWXQpXJk7uF98N7hUZpgGCR3+Ft6z CkozeVoiMttfmluqiPeZW2fXxvfeEzcNd4re9SApn9RFp+opiz+XExczpApy0P9O6gmN TBUg== X-Gm-Message-State: AOJu0YyJkLLW46IKS5mklIZn4xk746JytiWAOi3FpmJ9zUYg3HJsolV7 V1Rg5+W327nrGDHaxc8s4oY1IySV280c X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:4f4c:961b with SMTP id v3-20020a056902108300b00d9a4f4c961bmr353608ybu.1.1698186288663; Tue, 24 Oct 2023 15:24:48 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:19 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-17-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 16/50] tools api fs: Switch filename__read_str to use io.h From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:17 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677504992122928 X-GMAIL-MSGID: 1780677504992122928 filename__read_str has its own string reading code that allocates memory before reading into it. The memory allocated is sized at BUFSIZ that is 8kb. Most strings are short and so most of this 8kb is wasted. Refactor io__getline so that the newline character can be configurable and ignored in the case of filename__read_str. Code like build_caches_for_cpu in perf's header.c will read many strings and hold them in a data structure, in this case multiple strings per cache level per CPU. Using io.h's io__getline avoids the wasted memory as strings are temporarily read into a buffer on the stack before being copied to a buffer that grows 128 bytes at a time and is never sized larger than the string. For a 16 hyperthread system the memory consumption of "perf record true" is reduced by 180kb, primarily through saving memory when reading the cache information. Signed-off-by: Ian Rogers --- tools/lib/api/fs/fs.c | 56 +++++++++++-------------------------------- tools/lib/api/io.h | 9 +++++-- 2 files changed, 21 insertions(+), 44 deletions(-) diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 5cb0eeec2c8a..496812b5f1d2 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c @@ -16,6 +16,7 @@ #include #include "fs.h" +#include "../io.h" #include "debug-internal.h" #define _STR(x) #x @@ -344,53 +345,24 @@ int filename__read_ull(const char *filename, unsigned long long *value) return filename__read_ull_base(filename, value, 0); } -#define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */ - int filename__read_str(const char *filename, char **buf, size_t *sizep) { - size_t size = 0, alloc_size = 0; - void *bf = NULL, *nbf; - int fd, n, err = 0; - char sbuf[STRERR_BUFSIZE]; + struct io io; + char bf[128]; + int err; - fd = open(filename, O_RDONLY); - if (fd < 0) + io.fd = open(filename, O_RDONLY); + if (io.fd < 0) return -errno; - - do { - if (size == alloc_size) { - alloc_size += BUFSIZ; - nbf = realloc(bf, alloc_size); - if (!nbf) { - err = -ENOMEM; - break; - } - - bf = nbf; - } - - n = read(fd, bf + size, alloc_size - size); - if (n < 0) { - if (size) { - pr_warn("read failed %d: %s\n", errno, - strerror_r(errno, sbuf, sizeof(sbuf))); - err = 0; - } else - err = -errno; - - break; - } - - size += n; - } while (n > 0); - - if (!err) { - *sizep = size; - *buf = bf; + io__init(&io, io.fd, bf, sizeof(bf)); + *buf = NULL; + err = io__getline_nl(&io, buf, sizep, /*nl=*/-1); + if (err < 0) { + free(*buf); + *buf = NULL; } else - free(bf); - - close(fd); + err = 0; + close(io.fd); return err; } diff --git a/tools/lib/api/io.h b/tools/lib/api/io.h index a77b74c5fb65..50d33e14fb56 100644 --- a/tools/lib/api/io.h +++ b/tools/lib/api/io.h @@ -141,7 +141,7 @@ static inline int io__get_dec(struct io *io, __u64 *dec) } /* Read up to and including the first newline following the pattern of getline. */ -static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_len_out) +static inline ssize_t io__getline_nl(struct io *io, char **line_out, size_t *line_len_out, int nl) { char buf[128]; int buf_pos = 0; @@ -151,7 +151,7 @@ static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_l /* TODO: reuse previously allocated memory. */ free(*line_out); - while (ch != '\n') { + while (ch != nl) { ch = io__get_char(io); if (ch < 0) @@ -184,4 +184,9 @@ static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_l return -ENOMEM; } +static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_len_out) +{ + return io__getline_nl(io, line_out, line_len_out, /*nl=*/'\n'); +} + #endif /* __API_IO__ */ From patchwork Tue Oct 24 22:23:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157743 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238305vqx; Tue, 24 Oct 2023 15:28:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSoeAqiXtAGzxVc3W/GbrtEZTJx3F1h8zJLbYzUtehiXqok2OLQwkY8865O/HDJ9Dnw6/O X-Received: by 2002:a05:6359:2e01:b0:168:e862:70d0 with SMTP id ro1-20020a0563592e0100b00168e86270d0mr4464759rwb.14.1698186489966; Tue, 24 Oct 2023 15:28:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186489; cv=none; d=google.com; s=arc-20160816; b=jyDMk+Y2QH6yfGwsdddK5bkJO4ZF3qJ+Y8WvFLSbizOo2HJdfP6ZaJ7V7XfyZ/H8iL +13kM7yNKSJdsF0u81SSksOmYyo/0CS4Kk4iwWjRmzhw00m87fkT+y1bsouUhA3zMUNC P1HZB3syhiJnDD5pfoDpGnRFKxKkzhja3S6OkhIcNKakCKzNgwbf8Joej24lfK33PtZY fpPGZUsiZldOvQXSdUwYsKdKDpmHIoiKpQ69ZiL6a4OzXXTp8brbqqmW5uHN8YfAxxoG N3rkk8YCnEWvcP/16OmM2i/3zHQNaL33m8fkHLzMPS9CH5vUSVqqU5jkL92Ba8oquWSr gBGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=XKH/5K4bvubvIr9pw7ZYc/Z4ET3hwQ4HW4s3RX+AQbw=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=ZF/DhFHp0IrKAxHMRDzYhSv0F1lck9sDoez+G7y0vLiCPg7kpxnbPzwL2vDUur13X2 rzMjpa9TPj5L0VciL2M143rFfFFP+1jduEfVmi3dDXaDSuUtucqf60YmTxBS1WNgALp4 b/JiyX1YvRDzxrSxHyJT06gvbaKye1QQy9p5lqxVR53IUVbyJCklSGK0SH4h3kaEZi9P rvHnybMeKmq+XG//lcVfrOB4FMo2M8EIPlsxVjWWbdz4ZNF7AH7iyE+p59uP/DkL163O UvM+0uX0/tSVrxXBUW3rfc1yt1jMlQnmcC7CvbrJ1nzh3BNaTgqkboiVB6/vFvN1IWVN 0vnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ti0BxZOq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id r7-20020a655087000000b005b7c7efed1csi8652095pgp.295.2023.10.24.15.28.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ti0BxZOq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 06C45802541B; Tue, 24 Oct 2023 15:28:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344592AbjJXW1i (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344494AbjJXW1Y (ORCPT ); Tue, 24 Oct 2023 18:27:24 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DBFF1FDA for ; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a909b4e079so58021657b3.2 for ; Tue, 24 Oct 2023 15:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186291; x=1698791091; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=XKH/5K4bvubvIr9pw7ZYc/Z4ET3hwQ4HW4s3RX+AQbw=; b=ti0BxZOqkKM1O8RMMAUyNmmZ2o8mbX3dMm8Vjr/jsi/SElnTLRGvvf7tm75ekFnwiP aTja6tBcUnRlTYdAa/U5yaTaroVUtJWEYClh9UtPf3AR59z5jFFreWEiQzusSQnVGRY3 S3OLHaoUGOB4ZHSNhCYtNRDALBRJ6eGzSS4AWWWDf0TvXoUOCWwbmgX7ZsTzq0+S74/B Ahn6YidRiKy/TTgwUS9p/Cnai3YnI5tyiTz7S6UBux/y8JM1d2ch5KkkZPaseNvy7xx0 ZeX0cs0pJ6lhq1L9FybhTefYg7fLVyVZG5gYtjm/N/lm4c306xGJDGGw6kvBGfSvO6uU Edbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186291; x=1698791091; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XKH/5K4bvubvIr9pw7ZYc/Z4ET3hwQ4HW4s3RX+AQbw=; b=GsSvgl859Tocmw8iUcGeeb563eKVSNDEryb6aP1gyEE3nLgkVCKuxjNVmxrzQNp6v6 L7QAV3cU0GzHRrxOJ2pHPPe8hJiOUcsrUaaqpr5ZcKQLQSjT2TpwWXyKK6h88jsTq+6y JQxjObktZFdD8YbzqXiZrkKGYRrFXa/o51elgRpolmsoN7u9GcR6tJ6tkRlr7FRTozoc oIy+uCJ+xMU12wVdtAahPm2CxFDx8BevKbbk3k6UTfGhkNOLbB+Ub/WVz+QS+oi1C+Zg J4NfCLv0yHX+sZ53ts0cHj0Z5CIzYZsgY2XGo9faBS4K/rT2GE1MaZgRmq2KNuUi6gSw Us/g== X-Gm-Message-State: AOJu0Yy3FvSPJuxcEktqqBxINR3ofjmaZAYPJImrMd3sCHrytW6VsB7D 6G5xb6JL+xaZ3pU0NkOCh5HflXAIbTZh X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:eb92:0:b0:59b:ca80:919a with SMTP id u140-20020a0deb92000000b0059bca80919amr270216ywe.0.1698186291218; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:20 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-18-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 17/50] tools api fs: Avoid reading whole file for a 1 byte bool From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:07 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677596915263759 X-GMAIL-MSGID: 1780677596915263759 sysfs__read_bool used the first byte from a fully read file into a string. It then looked at the first byte's value. Avoid doing this and just read the first byte. Signed-off-by: Ian Rogers --- tools/lib/api/fs/fs.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 496812b5f1d2..4c35a689d1fc 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c @@ -447,15 +447,16 @@ int sysfs__read_str(const char *entry, char **buf, size_t *sizep) int sysfs__read_bool(const char *entry, bool *value) { - char *buf; - size_t size; - int ret; + struct io io; + char bf[16]; + int ret = 0; - ret = sysfs__read_str(entry, &buf, &size); - if (ret < 0) - return ret; + io.fd = open(entry, O_RDONLY); + if (io.fd < 0) + return -errno; - switch (buf[0]) { + io__init(&io, io.fd, bf, sizeof(bf)); + switch (io__get_char(&io)) { case '1': case 'y': case 'Y': @@ -469,8 +470,7 @@ int sysfs__read_bool(const char *entry, bool *value) default: ret = -1; } - - free(buf); + close(io.fd); return ret; } From patchwork Tue Oct 24 22:23:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157727 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237497vqx; Tue, 24 Oct 2023 15:26:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHOdkKI2WnV5LRn+7K/4AGnY0DX3sX8yf63tWmV+jYELp2F45JDOaqkHi5vMxTVhlKpBrkP X-Received: by 2002:a05:6870:1707:b0:1bb:84af:bf8f with SMTP id h7-20020a056870170700b001bb84afbf8fmr16078296oae.58.1698186370584; Tue, 24 Oct 2023 15:26:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186370; cv=none; d=google.com; s=arc-20160816; b=fq3REEOnjWvheFPPP5Ozvia448XqtnusfCNuQJhF9qn9EMRBDMi5WtBvyLVU/3qumq R3/rIe/2CAZLp5A5Q4iCBOKVY0NORPa6P3NRuke63kWCOktQbBiPQDvuZ4E37siKQyj/ vNxA6nPvLqijDlJRV1bvKuq3nEV2YGJEMfA866o8kEBZAjqc5qbvvaZaqLHd8ulBWD1+ EdFgT/Ara7OOLWSqY9NBV0Sjx7pN19E8b6mr5bnjJQBA7/Y32ExhLk8R/TOL+h+FhoJT 7Sxk2iUZo3er50mpT6FffimWL8QbV9DBJCtfOKYnSo8HlynpDwh7TpE6JdtdcKCgGjju PRDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=y47wBZgNJ/W0LVs6mrfqt3CyuKzt7vqoUfrqvfEiajsR755yYif4vv5yKnWPDNyAjw uLxy3x4HSXmXTGh6x1IojIQGJQMPRKvdvrVh1NvCjU/08aSY3z87i8ex26VnpQne+or2 gmQrroOvXjpQhkr2h+CjVzQog9F2ARHzWIDWj2lNDBTg9J2M4GjaolsrpBUnZz8PPOPR /c5fvbZsVw1M2zfcakBxRfs7/kmX7vnJ/NBGTWz3+V1x5w6O52kLi9MmbbaPElOAF1if f/DSlKFzKebGMxSNR3Dauqafkx+UUzSRhvs9CrNeTSFZjavGyONOk15HcLYS+ayqNptJ 8ekA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EU+aw5wB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id 4-20020a630204000000b00563f72935e3si8838122pgc.608.2023.10.24.15.26.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EU+aw5wB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 178D68021FFF; Tue, 24 Oct 2023 15:26:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344505AbjJXWZy (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234983AbjJXWZ3 (ORCPT ); Tue, 24 Oct 2023 18:25:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DADE610D4 for ; Tue, 24 Oct 2023 15:24:54 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da03c5ae220so243979276.1 for ; Tue, 24 Oct 2023 15:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186293; x=1698791093; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; b=EU+aw5wBe8FZvBL8crMRweg+EtN2PXjPDp24t4tZS6NGh7DtAdvSifsXhDpa7oPG2k KkOOlBOnHKnfaheuCAgsfj9gHTQ4vEBNywX9PstW6/fHnbM+k/a0K/Rmj/TtX16vgSHW PfNKFSB8NLH+JrFWJ0liIgra8hQLQtpmh5gug/6yNu2u3YHiT/A/UVZ4NJFSHHxEqkAV v4++5qYr61lq72+p72jh6f2VMbCdX3hxx4EKP23L3aFlSsmIFsUXqO+MitPidgKrY7YL x2pwSUk7v0woASAcnX9mwYdqqd7PcBY4Ju40gJRGohUmUgsyKaDNOAUDoEVgdNu/xR8S HI9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186293; x=1698791093; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; b=g2nDj8JgQyxkajFyrXcQD8ySDLlHLOSmZkEP0aCla0SAS7+bTEtwN0JGXxn5aDLQU9 M27Mi6JtXp5iok9akFue4zYlAyGMB2wLG6gdGQxdSFLmwehDhFoIbt8nNd9PdF7fkf75 3n2S08PUPZbOU0bk+oLJvOiItxjiG5sc+OH7iCcZ37oJDldG1AXHajqCxPoxIM0GZEfQ 1BxUf8vJpgR3JR/7zRjNT0tt2Ishx87gpyZ3VDbauMbXJ3TJIDBgCommq4uLdGPwc3rd pofhs2Ujy3/Z8DFN3rvYqVs1oo78jLprKe3HweyPlTpzNsCx0F8w2/fQLpukj0tT4e+/ QXng== X-Gm-Message-State: AOJu0YyAzMX0CunaliaZg0+39WrLpSQ6wtQJKbySeSjIHbSkDEljmtJN K76NtuiC90eQqP/bOYzbmQoEGMVpOm+r X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:34d1:0:b0:d9a:6360:485b with SMTP id b200-20020a2534d1000000b00d9a6360485bmr385698yba.2.1698186293540; Tue, 24 Oct 2023 15:24:53 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:21 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-19-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 18/50] tools lib api: Add io_dir an allocation free readdir alternative From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:08 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677471625676348 X-GMAIL-MSGID: 1780677471625676348 glibc's opendir allocates a minimum of 32kb, when called recursively for a directory tree the memory consumption can add up - nearly 300kb during perf start-up when processing modules. Add a stack allocated variant of readdir sized a little more than 1kb. Signed-off-by: Ian Rogers --- tools/lib/api/Makefile | 2 +- tools/lib/api/io_dir.h | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tools/lib/api/io_dir.h diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index 044860ac1ed1..186aa407de8c 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile @@ -99,7 +99,7 @@ install_lib: $(LIBFILE) $(call do_install_mkdir,$(libdir_SQ)); \ cp -fpR $(LIBFILE) $(DESTDIR)$(libdir_SQ) -HDRS := cpu.h debug.h io.h +HDRS := cpu.h debug.h io.h io_dir.h FD_HDRS := fd/array.h FS_HDRS := fs/fs.h fs/tracing_path.h INSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/api diff --git a/tools/lib/api/io_dir.h b/tools/lib/api/io_dir.h new file mode 100644 index 000000000000..8a70c0718df5 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +/* + * Lightweight directory reading library. + */ +#ifndef __API_IO_DIR__ +#define __API_IO_DIR__ + +#include +#include +#include +#include +#include + +struct io_dirent64 { + ino64_t d_ino; /* 64-bit inode number */ + off64_t d_off; /* 64-bit offset to next structure */ + unsigned short d_reclen; /* Size of this dirent */ + unsigned char d_type; /* File type */ + char d_name[NAME_MAX + 1]; /* Filename (null-terminated) */ +}; + +struct io_dir { + int dirfd; + ssize_t available_bytes; + struct io_dirent64 *next; + struct io_dirent64 buff[4]; +}; + +static inline void io_dir__init(struct io_dir *iod, int dirfd) +{ + iod->dirfd = dirfd; + iod->available_bytes = 0; +} + +static inline void io_dir__rewinddir(struct io_dir *iod) +{ + lseek(iod->dirfd, 0, SEEK_SET); + iod->available_bytes = 0; +} + +static inline struct io_dirent64 *io_dir__readdir(struct io_dir *iod) +{ + struct io_dirent64 *entry; + + if (iod->available_bytes <= 0) { + ssize_t rc = getdents64(iod->dirfd, iod->buff, sizeof(iod->buff)); + + if (rc <= 0) + return NULL; + iod->available_bytes = rc; + iod->next = iod->buff; + } + entry = iod->next; + iod->next = (struct io_dirent64 *)((char *)entry + entry->d_reclen); + iod->available_bytes -= entry->d_reclen; + return entry; +} + +static inline bool io_dir__is_dir(const struct io_dir *iod, const struct io_dirent64 *dent) +{ + if (dent->d_type == DT_UNKNOWN) { + struct stat st; + + if (fstatat(iod->dirfd, dent->d_name, &st, /*flags=*/0)) + return false; + + return S_ISDIR(st.st_mode); + } + return dent->d_type == DT_DIR; +} + +#endif From patchwork Tue Oct 24 22:23:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157742 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238300vqx; Tue, 24 Oct 2023 15:28:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IETPYPRkECtipOdzaawMXRclimwHhEWdUgounXq7oU2DyYDNAol7qd6lE+C+F93o1xapI2M X-Received: by 2002:a17:903:280b:b0:1ca:86b:7ed3 with SMTP id kp11-20020a170903280b00b001ca086b7ed3mr9524541plb.40.1698186489416; Tue, 24 Oct 2023 15:28:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186489; cv=none; d=google.com; s=arc-20160816; b=rDQhV/qIzXvYn3nb3xD4S9bYi1a2/XTyuXM02UO+CCBDW+3ssvkWIDdc/JaBc4EV5D zHABBiAYvGnAP5PLdqhtgUXRbX74IVtJDANoYMPhukWy+pBQazqzn47tybn0Hl6DGDl0 3LegRHfkfV+1ax2oUjAUmWutWLW1kjblbAB+zJexxKncDHGBcNq1EjSufM8isnu5jEGj sYW2oXj772CSgHA2K8LUwPibb7gr8dOMHvHqWm1cmcTy1078tcS7UvEEFWO8pZgX8rKz rhkXVMkiNhKEguVFlxXitbOZFf5BK04+un0RiYLFuWTGVlJVfzhTurQfIq2aKwHsYNIZ fwFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=RoZZ8YDCVCxLUMcPN0SyAz4FQGuYm4BBqKyuZJTny60=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=i4fvvttXSqmk5rWN+S0p7VG6ixPDLoe1Fo+VWDg/TcRoFknf1sHrPSoz8fHGbb32hE kYp1ETvm97HbgY/0GppfZLlkEs7uw2RsILlX55gypEEXwZJEsIObc8y7RQ/MgasdvZ9N kwo/dHRbydccT7t44Czex6n0L929tj35+PBZhdfuPRJRcbGgVfh0IepwbgabZGssXsms 6HY7OdvsNBhuv+n5hLJytQCzgFeGvMrJWWAGtW2/cYw3eYEoLisymB59S+dUIKzRjO8e qeShnIsGHJT8MJZdPmBe/3o6VQJ9/zsXW68sqqhQ1/CC5a9pB1JxUN/8dWWk8tOrCBD9 6zvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LxQ7TUcE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id j10-20020a170902690a00b001c46467a211si8894151plk.193.2023.10.24.15.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LxQ7TUcE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 03FF58025430; Tue, 24 Oct 2023 15:28:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344590AbjJXW1f (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344433AbjJXW1X (ORCPT ); Tue, 24 Oct 2023 18:27:23 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38FBB19A8 for ; Tue, 24 Oct 2023 15:24:57 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a81cd8d267so94137487b3.1 for ; Tue, 24 Oct 2023 15:24:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186295; x=1698791095; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=RoZZ8YDCVCxLUMcPN0SyAz4FQGuYm4BBqKyuZJTny60=; b=LxQ7TUcExIRiHdVin9naidfy0tXXS0gmjOw+K8Z6+6die0cYFfO8FhJCm2C6nLPP7L G0oV/niPGAWZIL1vGUbDHISIIrPsaQ4mdGq6odFWcNXdHGSj/xtzHUvmRbyyiGxnpl+m mJuvC8wN8X2szmgGGdMH0wuv2XoRobGwIoGUjr/AkdDaGL3NjhDwmvif/ljc2M/0Bd84 1AeQLpKUEJYEN94W1KAEi40E0vJsiqkEoBO15/+ydyqmei/7h4pBtMV6Nls6Y6M8nneV aPwXyNALAdqlZiSauZ2aGT3mDIpFUsYnAfblySVRf9KCPTd7LoQU+IJK3WW5PdUkPyhK zpkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186295; x=1698791095; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RoZZ8YDCVCxLUMcPN0SyAz4FQGuYm4BBqKyuZJTny60=; b=hKdGYb3HwRPFAqqOxfxR8NpkPzwUWegiX+7w8MhfKA8gFXyidH0w+Z9mWhRZzMBn6+ 5qbxzMy00vP/wqH+X52yE9MkawzWNxMsOvL+xjQFwTE5tXAH/t/p5tAi9hh87sYRPfvn EWRYOw/GQKOvaj62JOMEexRP1LFkFldMgmYuVi4HQME3kpQWqSV3NLw5V30XaAPblnh8 hZW057Rg/m7MfKRj2pJ85iCTT9MPjp/z3SzXJ+ApfqaB63AppZMm9jWxmmcxig2BoYEK Iefi8i7vP+qePz4WFQQRgMtENrfQf/2guHmVrHbeAZmGr5RRWTLdgMYoM08Xe4DeAjW/ r/AA== X-Gm-Message-State: AOJu0Yxpp83BiWysZOhhHICJqv7yrajotFf3tgxj8K+NJk33Lltwqjf5 xTECdzvQCH0mhIh0PgGKT9WjtK/Az/Je X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:690c:3142:b0:5a7:a98a:4af0 with SMTP id fc2-20020a05690c314200b005a7a98a4af0mr17210ywb.3.1698186295659; Tue, 24 Oct 2023 15:24:55 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:22 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-20-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 19/50] perf maps: Switch modules tree walk to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677596633504809 X-GMAIL-MSGID: 1780677596633504809 Compared to glibc's opendir/readdir this lowers the max RSS of perf record by 1.8MB on a Debian machine. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index a985d004aa8d..be3dab9d5253 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -36,6 +36,7 @@ #include // page_size #include "cgroup.h" #include "arm64-frame-pointer-unwind-support.h" +#include #include #include @@ -1552,25 +1553,21 @@ static int maps__set_module_path(struct maps *maps, const char *path, struct kmo static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth) { - struct dirent *dent; - DIR *dir = opendir(dir_name); + struct io_dirent64 *dent; + struct io_dir iod; int ret = 0; - if (!dir) { + io_dir__init(&iod, open(dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (iod.dirfd < 0) { pr_debug("%s: cannot open %s dir\n", __func__, dir_name); return -1; } - while ((dent = readdir(dir)) != NULL) { + while ((dent = io_dir__readdir(&iod)) != NULL) { char path[PATH_MAX]; - struct stat st; - /*sshfs might return bad dent->d_type, so we have to stat*/ path__join(path, sizeof(path), dir_name, dent->d_name); - if (stat(path, &st)) - continue; - - if (S_ISDIR(st.st_mode)) { + if (io_dir__is_dir(&iod, dent)) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; @@ -1603,7 +1600,7 @@ static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, i } out: - closedir(dir); + close(iod.dirfd); return ret; } From patchwork Tue Oct 24 22:23:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157726 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237452vqx; Tue, 24 Oct 2023 15:26:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGvu8a4Ca7/zLIVMOXw7Tuzi5yqcsUnCsUGK7bzIXp4dEeI/jr+O4jTqML9pNpVndg+bXuO X-Received: by 2002:a05:6a00:1a4e:b0:693:4108:1eb7 with SMTP id h14-20020a056a001a4e00b0069341081eb7mr11731969pfv.30.1698186366224; Tue, 24 Oct 2023 15:26:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186366; cv=none; d=google.com; s=arc-20160816; b=GoU3OgnmfolVFgS7kEF49I2bNdVaA0Q1cDdy/LNG2YZ0KPjbgFADUuIlvpRG753B7B N2yg11toqVnRqMZ1PLckp7npi1QC1zSwN94j+L2FR+Ua3hWzUgjtpmpadTdLBoDZoXnD eqC5/X4Kl9WWfBYTUhdmgdm6DcwKpwbedKa4nfJfM810SUonkJnUWV9LZXoCe2TysBmx dQLVdwdKnf9umGj0fh6MfGdxJam1nqmm8qhcbyzRhret5ANAEc9B4HxrI5QzvEAT3EAN Iho2kaT4uRlB0p/qZ6Cr1K1waaJexqqZ9nlf/18tgrF47zfKzzUN26tl0gGLV5oBQuyj xUcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=FTWwQ8YfXPTitLblRQYRVGGgeCbpOK/vEMTFXnHDMuM=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=RAamE2IVdFdkZ2JWbkNXXL8j4NbVe2r3Y6CR8pZtodaotfu+WREhgHZVawieYVa0gk OfNtLNCibaJ0/ZugTQS9BAjjCzTWah3CGH8Q5YRNukhO1mY6NR6PoaTGvPm6XJzmvA+i Bay9dfWDWZoztCirpa0SrJgmb/w5ponvz+PzK9x0+u6nL28ohdBlyXewPCq/BA4einBG 9vYDx2PJjjvwZ94cjvUk8D3C1qRyABfVNjjrpf9FTsWYs+ohVsGKqZNQ1DkuPBI/z0OY 4iycr+UO4PWzEyi77+zSVWmRlW7sfOZO8pc16C59IrGvGnBKnSFq2MOpm+DejZE1hhr8 tLng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cw1O5URq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id h7-20020a636c07000000b005b3b86ab19csi8739047pgc.107.2023.10.24.15.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cw1O5URq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7454A8026DDB; Tue, 24 Oct 2023 15:26:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344527AbjJXWZ6 (ORCPT + 27 others); Tue, 24 Oct 2023 18:25:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344566AbjJXWZe (ORCPT ); Tue, 24 Oct 2023 18:25:34 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F32C9210B for ; Tue, 24 Oct 2023 15:24:59 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da0631f977bso513269276.2 for ; Tue, 24 Oct 2023 15:24:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186298; x=1698791098; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=FTWwQ8YfXPTitLblRQYRVGGgeCbpOK/vEMTFXnHDMuM=; b=cw1O5URq2Mma06yy6NvGqZMvJqNIMy+ocLnn9th9g6iBkdDYqrsVIDc9HBx/pQPGKP E633Rh4yq9+3JZitBFsb9HDNLyuSD1g2+HM1XPhrDMqcXFDOtG+4dBA/i5tBOMJEw263 E7lla66VbrGC7vqW0OsakbQmF0i4p4YthUnNWnPna84nFa7fEneIciTQr6YkEXRLYHCj Lo03ccU0G4zr18pZqBkSy1/qv+ImmRarxUENWJUT5Vn09JJ8qyl2Spl5S6PPnjDiPds3 ARsP7u+BdgNKAeFiWlBaIq7327xBKVHTScmsyCWhBT6fMLFHc8gKulzBOgUWn99+Hkcx 2JIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186298; x=1698791098; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FTWwQ8YfXPTitLblRQYRVGGgeCbpOK/vEMTFXnHDMuM=; b=IfY6sQn0K3PBGWN7FndFV+EeBlJF1z/GbUDRccwNmcjlC4ro+EtdSTC0zl2n7F+PMF MY/vhHqd8hTb4eZkuHVY6NAbKUA6Kh6/a3oSuuNyrUsEfzjyYTno+5wKyyKvho1RAsNE QjjLk9Cs9UTuJ+GG3oeM8ptT7aQSdVG/wb+YZYQ68bbdFcgy67y2A+vgtOlxvKE/6G6e 3fAvL0IaE93S9zm8JiZ5EZccIS3cjZPIIznim+BQvv+8T6mKqy72apBhENkNtevAja2Y 2bAnFhSY8A8JUn2Q/4miJUsY63JTV0Qd3iP46baMNSOJlpCsMy7uJZf8nUVLd+5XZIfE yNEg== X-Gm-Message-State: AOJu0YxyHpgiTvk67nMzCGsKtarqeZqepfU1TMldFihurgHlN7RNuDe4 sIDO1u4JVQRYBWIRiwGRrxzY1JBcASMJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:3453:0:b0:d73:bcb7:7282 with SMTP id b80-20020a253453000000b00d73bcb77282mr270067yba.8.1698186297774; Tue, 24 Oct 2023 15:24:57 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:23 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-21-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 20/50] perf record: Be lazier in allocating lost samples buffer From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677467199080175 X-GMAIL-MSGID: 1780677467199080175 Wait until a lost sample occurs to allocate the lost samples buffer, often the buffer isn't necessary. This saves a 64kb allocation and 5.3kb of peak memory consumption. Signed-off-by: Ian Rogers --- tools/perf/builtin-record.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 9b4f3805ca92..b6c8c1371b39 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -1924,21 +1924,13 @@ static void __record__save_lost_samples(struct record *rec, struct evsel *evsel, static void record__read_lost_samples(struct record *rec) { struct perf_session *session = rec->session; - struct perf_record_lost_samples *lost; + struct perf_record_lost_samples *lost = NULL; struct evsel *evsel; /* there was an error during record__open */ if (session->evlist == NULL) return; - lost = zalloc(PERF_SAMPLE_MAX_SIZE); - if (lost == NULL) { - pr_debug("Memory allocation failed\n"); - return; - } - - lost->header.type = PERF_RECORD_LOST_SAMPLES; - evlist__for_each_entry(session->evlist, evsel) { struct xyarray *xy = evsel->core.sample_id; u64 lost_count; @@ -1961,6 +1953,14 @@ static void record__read_lost_samples(struct record *rec) } if (count.lost) { + if (!lost) { + lost = zalloc(PERF_SAMPLE_MAX_SIZE); + if (!lost) { + pr_debug("Memory allocation failed\n"); + return; + } + lost->header.type = PERF_RECORD_LOST_SAMPLES; + } __record__save_lost_samples(rec, evsel, lost, x, y, count.lost, 0); } @@ -1968,9 +1968,18 @@ static void record__read_lost_samples(struct record *rec) } lost_count = perf_bpf_filter__lost_count(evsel); - if (lost_count) + if (lost_count) { + if (!lost) { + lost = zalloc(PERF_SAMPLE_MAX_SIZE); + if (!lost) { + pr_debug("Memory allocation failed\n"); + return; + } + lost->header.type = PERF_RECORD_LOST_SAMPLES; + } __record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count, PERF_RECORD_MISC_LOST_SAMPLES_BPF); + } } out: free(lost); From patchwork Tue Oct 24 22:23:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157754 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2239323vqx; Tue, 24 Oct 2023 15:30:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG3vt2ZNEsKjRzQ4uPRlB1jzbCB5zyzYsBojJQppVrKAeTh7aTvtM1Ii62/3MRPGWmIKzJQ X-Received: by 2002:a05:6a00:853:b0:6be:b79:eca with SMTP id q19-20020a056a00085300b006be0b790ecamr16031942pfk.32.1698186637020; Tue, 24 Oct 2023 15:30:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186637; cv=none; d=google.com; s=arc-20160816; b=AwJb5M+pWJBlxPKXLHsFPS8JFvjPOSDrnAgBKD6Fyc0oVtB41f0miTvICo1riEmZQ9 qdJZIqYH7K7p6jdV76sECfdFB/KGFkVoOCgG15mOJam6MPMgzmiHQ3yhnkTSMTQAdugL AmAiMPNwClUTu/5sr4pXvhWJbYh0jV5XZZ1OhC9/Mhs4aYViEP54TIW+d+mpE2AVUVO1 JXidcPSDKSpbc7s3g5qt7666RAbjryB6DmqZR6szIXSN51f823/C72PWq8LatLycjrYe WFOruilQelzfaoMjuPUl72qda7GuooSQy/cnRK78n3u7T+x2B/qvrOeF3hKCCq8HBaPu m9ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=vGp/3cu2x+AKrnRU7KWdBupCS5op7rBGMNR9ttA6zXA=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=XMWs1K+s168Opi0Nj/VVh4mlm1BuACDAFwTCKq0lBeCc5j0do8cmlb23hexhSvef// wn/9IwBF/y+bsX/XMXvFcTd9a/uxSi4sMyzfxrecGheaDWvX4uK0+fMcFMN17FGgNHs2 VgAQA209jGv7f5xxHt1SpvIdTAOs7LtOcTidk5K/wXiyGw7NFLtWX06pT7oesBSyVUan ycqr4uH799vJxhqHrnQw+YXoonbosHUOkS5Z6tm1MKyVRr2lJGTZ5KBZEEhdCT2UiBqQ ND4alguD9Uhn+uBpfnETiVS9esui/5iAkfVo4j7CkJBGVulTC2SFMTMXQ8BC0LE52ZNc cgfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ho7PysMA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id w9-20020a056a0014c900b006b45d893739si9393298pfu.335.2023.10.24.15.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:30:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ho7PysMA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 4B6E080934B8; Tue, 24 Oct 2023 15:30:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344519AbjJXW3x (ORCPT + 27 others); Tue, 24 Oct 2023 18:29:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344521AbjJXW3t (ORCPT ); Tue, 24 Oct 2023 18:29:49 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68CCC211C for ; Tue, 24 Oct 2023 15:25:01 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a8d9dcdd2bso93987137b3.2 for ; Tue, 24 Oct 2023 15:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186300; x=1698791100; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=vGp/3cu2x+AKrnRU7KWdBupCS5op7rBGMNR9ttA6zXA=; b=ho7PysMAjwIJ3yIPe+0/b/7VBk4CN0/vnEqDCkDAR0t9wOPDgRfiS8RBAoZO2M9YUn nsrWvo7n5hqAdTf5tyKzhTFyR9051FJUHrbpqxXsrz/cRb1xgPg2I8whqIaomG7JZNF3 nwNZ4QAYTJgPF+C1zp75z9njx2mG5+uwzEdSCYjquYMwL7LUUkZnWYVnoNsucYQ05fBK 2N+xKhINliTVjQP5OrD93WsjUsKk1bH+7u1DzPpIcZ6Yw5id3WruyxXsg38VYgh7a5lo zq556AP8f3GodPF6g+gPZ93Y09BsiSyghWlltHWXIG5Ddr23Vb/juFmm+qYHS4IvfkFa Jhfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186300; x=1698791100; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vGp/3cu2x+AKrnRU7KWdBupCS5op7rBGMNR9ttA6zXA=; b=UtvbnZrFrUZfMzgInr3xDODR7mKIlweuT5yt/En4cwoiWRdbh4VZf3SCPDBIHkxyR0 ZccpwNla4XKgq9RB1yBLu7n2eAKnTtmbj1CfCHBYf1XTKEDBuCFGhAJsSoVrAmf3dDqI 51uVMk5+Hg43lkVUO9+xrFzcCU5wUKj7AHK5lpPItRd0b7Bsnk2i40jQkDRk3CHYliek dLqRA7DfHMFh1YrlJMuoyIPiVE/qrvVxQxMPIpuHUofc4HdOoeCe6UbBx2ukbzpKj9N7 kaUkKVNRJ6Zodu4xZKbhWJ7dV7cBAFDXUF9VMyA0wWGRBpAUWxqnJSXyCpC8ylNtfDWg e2ZQ== X-Gm-Message-State: AOJu0YxZtV4DfjDC+Eu67k4wRXTFNuciVAy9Db1hd5o3LgJt+IRq1KK4 JjMgWdjWg2Dkr25eGvWrnnvOwIBxxs2h X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:5206:0:b0:5a7:b672:4d88 with SMTP id g6-20020a815206000000b005a7b6724d88mr288080ywb.0.1698186299914; Tue, 24 Oct 2023 15:24:59 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:24 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-22-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 21/50] perf pmu: Switch to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:30:15 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677751124942418 X-GMAIL-MSGID: 1780677751124942418 Avoid DIR allocations when scanning sysfs by using io_dir for the readdir implementation, that allocates about 1kb on the stack. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 48 +++++++++++++++++------------------------- tools/perf/util/pmus.c | 30 ++++++++++---------------- 2 files changed, 30 insertions(+), 48 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index a967d25e899b..91ae0ce06ef0 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -184,19 +185,17 @@ static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_fo */ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) { - struct dirent *evt_ent; - DIR *format_dir; + struct io_dirent64 *evt_ent; + struct io_dir format_dir; int ret = 0; - format_dir = fdopendir(dirfd); - if (!format_dir) - return -EINVAL; + io_dir__init(&format_dir, dirfd); - while ((evt_ent = readdir(format_dir)) != NULL) { + while ((evt_ent = io_dir__readdir(&format_dir)) != NULL) { struct perf_pmu_format *format; char *name = evt_ent->d_name; - if (!strcmp(name, ".") || !strcmp(name, "..")) + if (io_dir__is_dir(&format_dir, evt_ent)) continue; format = perf_pmu__new_format(&pmu->format, name); @@ -223,7 +222,7 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) } } - closedir(format_dir); + close(format_dir.dirfd); return ret; } @@ -599,8 +598,8 @@ static inline bool pmu_alias_info_file(const char *name) static int pmu_aliases_parse(struct perf_pmu *pmu) { char path[PATH_MAX]; - struct dirent *evt_ent; - DIR *event_dir; + struct io_dirent64 *evt_ent; + struct io_dir event_dir; size_t len; int fd, dir_fd; @@ -615,13 +614,9 @@ static int pmu_aliases_parse(struct perf_pmu *pmu) return 0; } - event_dir = fdopendir(dir_fd); - if (!event_dir){ - close (dir_fd); - return -EINVAL; - } + io_dir__init(&event_dir, dir_fd); - while ((evt_ent = readdir(event_dir))) { + while ((evt_ent = io_dir__readdir(&event_dir))) { char *name = evt_ent->d_name; FILE *file; @@ -651,7 +646,6 @@ static int pmu_aliases_parse(struct perf_pmu *pmu) fclose(file); } - closedir(event_dir); close (dir_fd); pmu->sysfs_aliases_loaded = true; return 0; @@ -1879,10 +1873,9 @@ static void perf_pmu__del_caps(struct perf_pmu *pmu) */ int perf_pmu__caps_parse(struct perf_pmu *pmu) { - struct stat st; char caps_path[PATH_MAX]; - DIR *caps_dir; - struct dirent *evt_ent; + struct io_dir caps_dir; + struct io_dirent64 *evt_ent; int caps_fd; if (pmu->caps_initialized) @@ -1893,24 +1886,21 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) return -1; - if (stat(caps_path, &st) < 0) { + caps_fd = open(caps_path, O_CLOEXEC | O_DIRECTORY | O_RDONLY); + if (caps_fd == -1) { pmu->caps_initialized = true; return 0; /* no error if caps does not exist */ } - caps_dir = opendir(caps_path); - if (!caps_dir) - return -EINVAL; - - caps_fd = dirfd(caps_dir); + io_dir__init(&caps_dir, caps_fd); - while ((evt_ent = readdir(caps_dir)) != NULL) { + while ((evt_ent = io_dir__readdir(&caps_dir)) != NULL) { char *name = evt_ent->d_name; char value[128]; FILE *file; int fd; - if (!strcmp(name, ".") || !strcmp(name, "..")) + if (io_dir__is_dir(&caps_dir, evt_ent)) continue; fd = openat(caps_fd, name, O_RDONLY); @@ -1932,7 +1922,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) fclose(file); } - closedir(caps_dir); + close(caps_fd); pmu->caps_initialized = true; return pmu->nr_caps; diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index ce4931461741..65b23b98666b 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -3,10 +3,10 @@ #include #include #include +#include #include #include #include -#include #include #include #include @@ -184,8 +184,8 @@ static int pmus_cmp(void *priv __maybe_unused, static void pmu_read_sysfs(bool core_only) { int fd; - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus)) return; @@ -194,13 +194,9 @@ static void pmu_read_sysfs(bool core_only) if (fd < 0) return; - dir = fdopendir(fd); - if (!dir) { - close(fd); - return; - } + io_dir__init(&dir, fd); - while ((dent = readdir(dir))) { + while ((dent = io_dir__readdir(&dir)) != NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; if (core_only && !is_pmu_core(dent->d_name)) @@ -209,7 +205,7 @@ static void pmu_read_sysfs(bool core_only) perf_pmu__find2(fd, dent->d_name); } - closedir(dir); + close(fd); if (list_empty(&core_pmus)) { if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) pr_err("Failure to set up any core PMUs\n"); @@ -563,8 +559,8 @@ bool perf_pmus__supports_extended_type(void) char *perf_pmus__default_pmu_name(void) { int fd; - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; char *result = NULL; if (!list_empty(&core_pmus)) @@ -574,13 +570,9 @@ char *perf_pmus__default_pmu_name(void) if (fd < 0) return strdup("cpu"); - dir = fdopendir(fd); - if (!dir) { - close(fd); - return strdup("cpu"); - } + io_dir__init(&dir, fd); - while ((dent = readdir(dir))) { + while ((dent = io_dir__readdir(&dir)) != NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; if (is_pmu_core(dent->d_name)) { @@ -589,7 +581,7 @@ char *perf_pmus__default_pmu_name(void) } } - closedir(dir); + close(fd); return result ?: strdup("cpu"); } From patchwork Tue Oct 24 22:23:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157753 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2239003vqx; Tue, 24 Oct 2023 15:30:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IESLm7Z551pqO7piLVMtwXQp2UtWg58vLjwMZCQmuFzJPyiIkAuSVqub12r5zD4+fdZ1CJv X-Received: by 2002:a05:6a21:6810:b0:171:4f38:b475 with SMTP id wr16-20020a056a21681000b001714f38b475mr3875116pzb.58.1698186603512; Tue, 24 Oct 2023 15:30:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186603; cv=none; d=google.com; s=arc-20160816; b=1C4IU24Jjpf9r3aKFB06ko0xhQ/hlF7NHV3lRiUymKci/pWpit6QX1RF3yNusoikjO vwfqcj4LzT+EdTJP0rAXH9b7aZaPTDrvDAbZurqSGCW+KugpP4ifI/kMb/KfGPxVrocz SriTZDBtwSLhTVZEvTgyob4XsaNn444F79UMImik8uPu3xno+xmoCg1iffANuvzTg9xc OJs4tUbTbG07bAM1mkfNSQIz8UPXMyj2vD4MF79t6q5pWhMi7kwgiavUJg76m52EG1yq hFVKwtyQiEKOQzt3tKClg469l1nAtxYxYp2xurb6toPiXbfquekUefS+S07NLy0g3mVh f1IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=4cZX6FOP9Id6EVCdgsB1n601UiMDcX5xk8Ugm2UBQtA=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=IwnU/s5UD2e3qqSCDl+J2EoE2JRx2ZktivTFJy1QUzx8AY8hscR9wmvGb5zHIR8oC8 /k5YcDcS1lzZPIG5pl/fpK/HrqX0tCHTFBp9Y7P3jBBOkcwzKcb+9IPTIJtVllg3FU8B KmVyZsF0wErk6K8blJ+xHvr30/6p7YYAksBtcoBtJfClzsHC/5oXg0RCicgSObyrl5GB 09vFrNSrl5kXwPiupP6qTXbbqUCltDw7F53Xg6EEid8ItUyyXE5bOR7PskyASrY5bF13 sNPgmOFlEJ0l02tBqOLlSw9LqlQ6sOPenuZZYDOdCf+wpkiky627n7Dn3mW+irJ6UEOp 6hAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fqBwKDGO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id ck7-20020a056a00328700b006bf2e5f0840si8037358pfb.67.2023.10.24.15.30.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:30:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fqBwKDGO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id CB4A48026ACB; Tue, 24 Oct 2023 15:30:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344447AbjJXW3z (ORCPT + 27 others); Tue, 24 Oct 2023 18:29:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344552AbjJXW3t (ORCPT ); Tue, 24 Oct 2023 18:29:49 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D45D212E for ; Tue, 24 Oct 2023 15:25:03 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a45e7e0f9so5775538276.0 for ; Tue, 24 Oct 2023 15:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186302; x=1698791102; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=4cZX6FOP9Id6EVCdgsB1n601UiMDcX5xk8Ugm2UBQtA=; b=fqBwKDGOCT1JJF7Lj2boSkxpXCAnWxWXKUp8yd2xS3Fp0JFaquGdgMoicOfUwGuZDF H9K2FonFDUfPK9KHTBVqNSdRI5J9P/CEwWPO7qIhgfiC8e3OwPmla0U1dfiuuqfgq7ZO AE7BOhQdTTRK3YWK7TP8n/fq3518AsRJbk0TkhIidO7DNQ6b+BWJ+ZGrSePdW9bP4EB+ 1IrP29DhWSYktDWy5W8GtMGbDl4aKUPCIzHQQf378nGjo4OPuMWjFvZswvWiU5QEclte l/NGR7w4DGiSd72L1Hjl3IK5q76J9lMt3RV299d1aTqdLH56OASadjHKANAqmG/BM9Os buUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186302; x=1698791102; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4cZX6FOP9Id6EVCdgsB1n601UiMDcX5xk8Ugm2UBQtA=; b=iB/pm3m0bYUOSofhOikSYlU0t0PynQU5Kx2rW3IMq6CM7e1r5ASOVFk21BZ4xSs9QP xz6XqqdaY8/r5pFr0hLu5YLLbXj/q68gCSQOZBD3nkiAwbJlocVWmK1blJT9e4xsAdvZ eSsSPdcPqO5B6qR4CZjtBS27iSWiAGscYOI+DkKa48fetQGgNY2Z57piLNPfY5VvVpxJ H6wiTAqDTfj8wRNNxq3fDRNBeVxQPRvT1sxpzAv/sEZ8Ug6rPV12YTfiUebnTSpga9c8 jywmhvoZorpUMM/egUCh+DSjm1ypdi85lmYvZb7XkXbbrNZwbFt027XcpaXmGkqxYa1T KQOA== X-Gm-Message-State: AOJu0YzU/6TPswhqO0lHgB81ssaNB0Cqs6yHxTxfazm6nSyX1YDRQzL/ dcidkSVkTa+/Jn4Ifs922hDDMLWRm9GZ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:ad49:0:b0:d81:582b:4661 with SMTP id l9-20020a25ad49000000b00d81582b4661mr259838ybe.8.1698186302403; Tue, 24 Oct 2023 15:25:02 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:25 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-23-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 22/50] perf bpf: Don't synthesize BPF events when disabled From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:30:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677715648245223 X-GMAIL-MSGID: 1780677715648245223 If BPF sideband events are disabled on the command line, don't synthesize BPF events too. Signed-off-by: Ian Rogers --- tools/perf/util/bpf-event.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 38fcf3ba5749..830711cae30d 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -386,6 +386,9 @@ int perf_event__synthesize_bpf_events(struct perf_session *session, int err; int fd; + if (opts->no_bpf_event) + return 0; + event = malloc(sizeof(event->bpf) + KSYM_NAME_LEN + machine->id_hdr_size); if (!event) return -1; From patchwork Tue Oct 24 22:23:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157758 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2241553vqx; Tue, 24 Oct 2023 15:36:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFphBzTzkJgNgRpKeGTDudnybMf6MqrXh7gqI+/NuAhT8UAnaTEc3jFZ8zmLigHObY86eVB X-Received: by 2002:a05:6358:6a95:b0:168:f55d:4ef9 with SMTP id n21-20020a0563586a9500b00168f55d4ef9mr2977741rwh.28.1698186987296; Tue, 24 Oct 2023 15:36:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186987; cv=none; d=google.com; s=arc-20160816; b=Ph2eDkE/4V2uxWU4UEIML6jmio7yu0Jq+Q4KOAuPr1TMsRqyFL+M0foivr0oKF3PKK 4SUOr2ZmGXevb/ZuM0syMUKzcoGwprMIdi+bUPLebJqoOVmBm/FnSwqIi1Cbih3rIOvv ak1Kx4mxqcHhDuKCWvPXzNdIzWVln/R4oVjEP3u0mE1zA9ZHEEpFJrpuAnutBAHkTWHb SLovMiXw9aMvkjRjv9VVqeP5RlaNBCaEIy8NSswzvKbwqymOv44Ym7xMqwwic9aPta+q zmQVqBXS1/6TwlEOVAwBA2kghI/6upJR4OFSP5t7OKb+hEKtg8YDlferlGANfZGequo9 pdbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=Tu7+ha5Pu/Ntln1eJ+Wk/N1rQ4pn7e+WtxhSwRWOU6E=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=rQkVXArb/1ydMqDoaPcS8ijL/vNfnewFbfYFMsstA1Trf9T0Ctatnf0jITRVf3ydTr 7skW/q/nq3B9hFD/mrhT+TDXrqMy4CHdmnm01owsS7VNTEUg239ByfAj6cRL52sjgZil 0K51cRaiuI6jyqX4VQbGyihIIjcelsKVliT1fRW5WcKkzYSxgEUETWQcFskRKWnG4x1m cE1ZqwfUpwx2ZqDJr/FN+3JVm9N9I8WnUW424lfmn8ciuv1KeuY14wkcvkeCIVCKaPJM 2RvPonUd0Vpw/c91WReZofYLODxMriXCZj2lnDfX77XWZ/f/tA+KfEN008Kkp9+apuIc +2eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nWMr1oTw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id h7-20020a636c07000000b005ad10a010f6si8662434pgc.352.2023.10.24.15.36.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:36:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nWMr1oTw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 07E8E8025421; Tue, 24 Oct 2023 15:36:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344709AbjJXWgO (ORCPT + 27 others); Tue, 24 Oct 2023 18:36:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344706AbjJXWgC (ORCPT ); Tue, 24 Oct 2023 18:36:02 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 549F5213C for ; Tue, 24 Oct 2023 15:25:06 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a7b10c488cso62544877b3.2 for ; Tue, 24 Oct 2023 15:25:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186304; x=1698791104; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Tu7+ha5Pu/Ntln1eJ+Wk/N1rQ4pn7e+WtxhSwRWOU6E=; b=nWMr1oTw8roaNp+9Sylu+T0C3hxhXC7feCnrnJO42Ym/bW5suRUwgU5aH5oox+Blw2 opyC/uehKO8wdm6bpayS0ZHdEvCaEXUJnCP5YqnNWgKv1jws5+ls0UtZiMPGAh+EjGVS tR/hRFabU+/Zl0yQV1rDsZQr6ICwIDMzqQU3EftDDj1Jj1B9VdHBbQ6iDab33maBTpV0 WUJandZ00NdLY1NpXilU4zaWq3pq6Ug9Ztae7X2scryhQSfxwZHi0bMUAzp/t/VZ74fb z23Jr/ruVmZ3pV2zAFxHAidX/mj5QiUZmGPSChC87GmTfV/X3GYGkMC5g6yE7FBQL2lE agUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186304; x=1698791104; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Tu7+ha5Pu/Ntln1eJ+Wk/N1rQ4pn7e+WtxhSwRWOU6E=; b=dpY/VqDAfNzrxVE2v/ewPmAfXL6zL9ArCodL/YxQIO1IWDkp4vB88fCh6qlzXZ0H8i /UjAEctLaXj92Vp+/RvVIhPu4n91rtK/K7YQpXjuAnQHy6INUsX3mUkNWSSHrJHg8B9m fh9z0YoRSWuDIJUr9ZLZOpZS3ZfL22FO49cvzbRWo6mUR4KCThnEQyfeYzKkYJIUiDhg rI+9uUo7TOI0PO+YnDQpOiGY9JH7M63vLS8UAEzAdzEIPKfEJSBjtZ/6qWXUBUEYUSYJ q8VIts/SMSg6YhcV61/CVNDMnJPS0qjwL+++X8CaVnGJbbTJ0w92lUJSsaeuD8miYi/p ds3g== X-Gm-Message-State: AOJu0YxBPRYleDIbAArlLx3U06Ujp+Jd8my5usClKf209emkpdq/myGA +mnYJXVjKPW6Bt/0iqF+6G/lJjcKKXGY X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:d403:0:b0:5a7:acc1:5142 with SMTP id w3-20020a0dd403000000b005a7acc15142mr298455ywd.8.1698186304467; Tue, 24 Oct 2023 15:25:04 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:26 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-24-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 23/50] perf header: Switch mem topology to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:36:25 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678118209892614 X-GMAIL-MSGID: 1780678118209892614 Switch memory_node__read and build_mem_topology from opendir/readdir to io_dir__readdir, with smaller stack allocations. Reduces peak memory consumption of perf record by 10kb. Signed-off-by: Ian Rogers --- tools/perf/util/header.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index e86b9439ffee..55f63d2ee232 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -44,6 +44,7 @@ #include "build-id.h" #include "data.h" #include +#include #include "asm/bug.h" #include "tool.h" #include "time-utils.h" @@ -1341,11 +1342,11 @@ static int memory_node__read(struct memory_node *n, unsigned long idx) { unsigned int phys, size = 0; char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; #define for_each_memory(mem, dir) \ - while ((ent = readdir(dir))) \ + while ((ent = io_dir__readdir(&dir)) != NULL) \ if (strcmp(ent->d_name, ".") && \ strcmp(ent->d_name, "..") && \ sscanf(ent->d_name, "memory%u", &mem) == 1) @@ -1354,9 +1355,9 @@ static int memory_node__read(struct memory_node *n, unsigned long idx) "%s/devices/system/node/node%lu", sysfs__mountpoint(), idx); - dir = opendir(path); - if (!dir) { - pr_warning("failed: can't open memory sysfs data\n"); + io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (dir.dirfd < 0) { + pr_warning("failed: can't open memory sysfs data '%s'\n", path); return -1; } @@ -1368,20 +1369,20 @@ static int memory_node__read(struct memory_node *n, unsigned long idx) n->set = bitmap_zalloc(size); if (!n->set) { - closedir(dir); + close(dir.dirfd); return -ENOMEM; } n->node = idx; n->size = size; - rewinddir(dir); + io_dir__rewinddir(&dir); for_each_memory(phys, dir) { __set_bit(phys, n->set); } - closedir(dir); + close(dir.dirfd); return 0; } @@ -1404,8 +1405,8 @@ static int memory_node__sort(const void *a, const void *b) static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) { char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; int ret = 0; size_t cnt = 0, size = 0; struct memory_node *nodes = NULL; @@ -1413,14 +1414,14 @@ static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) scnprintf(path, PATH_MAX, "%s/devices/system/node/", sysfs__mountpoint()); - dir = opendir(path); - if (!dir) { + io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (dir.dirfd < 0) { pr_debug2("%s: couldn't read %s, does this arch have topology information?\n", __func__, path); return -1; } - while (!ret && (ent = readdir(dir))) { + while (!ret && (ent = io_dir__readdir(&dir))) { unsigned int idx; int r; @@ -1447,7 +1448,7 @@ static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) ret = memory_node__read(&nodes[cnt++], idx); } out: - closedir(dir); + close(dir.dirfd); if (!ret) { *cntp = cnt; *nodesp = nodes; From patchwork Tue Oct 24 22:23:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157734 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237800vqx; Tue, 24 Oct 2023 15:26:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFInvAq88DAvLTB/uDSRjugxx4M0QJs3N0/WZevtInU3PhRvr6gxmdO6MDM01M1oK4zUQjh X-Received: by 2002:a17:90a:164f:b0:27c:f016:49a2 with SMTP id x15-20020a17090a164f00b0027cf01649a2mr10766106pje.7.1698186412215; Tue, 24 Oct 2023 15:26:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186412; cv=none; d=google.com; s=arc-20160816; b=xwJIW3Oe2EcsyloPz0bG/Bd1hz68UKUasqngbphYvY4ZND13O8makypn+ZLfR41E2x 1exmfwIGvAmfioc9BpTnB5AoBgeqzcwY9xBw/upODgStbst2hXha12zSTtiARMPPSRHd 13j45jbrI452TkZo1yQQV9AUGutAnGc/1tYA5f/nW9MRTwPHiXqVkkAs6tewy1wCCKiz nWOIV98GROF096DosejlstDz0HxGqCi89MV23Wk/lT0j70s/285M0NIWlrvyqyfKtmZI ketextDbDgvZWgrmaJZVqSF20rQtRyK0Zn9P49pxwvyMeL3+VXVydlnxr/kSuno3ayGt K5mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=gjcnIwKrkFOXzuG1ICX1mOzbNKaTUzCZsIGT1LrnoLg=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=H1hnQiQNwSNFafe3YwFZoAfO5XVOn7hNkYsthLFvH6x3js7eNXStgqTys7A6H4olVr 8Ul+PH/kt2VAStnS/gmn1f3sqRzNoupVklAX0MMRvQH1O9jLVd41zd6fqbsQUyV/tunS KMfYNpQQFXecpbi5TYEljl7NCsvb+v6LLNbpCNchxrUbyR4Z7/cQjX6a6KRbR/A53LrM pn2DOlrw0Hq80VE0nXyps90ZGvicFRZOrRw+p93PJmLJJ3sXZOU8vBe3QtUyPS/RvTFJ lSIO+hRU/491JMBLdq7jWnX4aTqhtTtGeMGbAhx+GAAtt+eV+PJMHvKeOYRXQ3HdGMCZ YBpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="A/sLPVi1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id lk2-20020a17090b33c200b002744e9b7a22si11555098pjb.9.2023.10.24.15.26.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="A/sLPVi1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id DC9A28039444; Tue, 24 Oct 2023 15:26:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344570AbjJXW0K (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235011AbjJXWZo (ORCPT ); Tue, 24 Oct 2023 18:25:44 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 434AC268D for ; Tue, 24 Oct 2023 15:25:07 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a818c1d2c7so67909077b3.0 for ; Tue, 24 Oct 2023 15:25:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186307; x=1698791107; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=gjcnIwKrkFOXzuG1ICX1mOzbNKaTUzCZsIGT1LrnoLg=; b=A/sLPVi15utRxV3CjBU32/s92AtPA2bp/c2dp6YztWheHrUY8dLN9rFysIB6ht0nYQ WaV6Uhad7Nql1+tEZAlJJ+QE6gj0jKCwaPWRz8a8qJBRU+IwvNZ+xMO6GnEopU5JMuOl GZao9usDQGKNz9ogCpn3nbCzmOPK5HDVniMi+qQfYL7NHvMIy0E/Qh/u3IUrHHs86ui4 FEuUReCdpssCsJ/vrEQ1CpNYoblBJMRU+hLHWKbtnnq+U8N66cwgwOegbbHT8MNbPh1D XCjI1r2b/gVcoHcJ4dQ4Q6VbCIo3U0T867KEjPLR5Wi9Eh4y8nYuZCzmHcRdhJydrnbc NZwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186307; x=1698791107; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gjcnIwKrkFOXzuG1ICX1mOzbNKaTUzCZsIGT1LrnoLg=; b=YLNXQhcXtWZVacOmWG1o0ZSuJefBnWcWR/h0Ue67m9sjvVPrbs3RWNU54qKrwdvYrF dC5DuxkEW1mpJQrefLVm80VZJ0VFbWF8DZS8vASbuydT8F4nXPwOWqL9vvrkuzeQs9lf OYCvvGWzIFmKDBvYVrkrelk4c5D9AiuJS224QfEvspAASSXhPR412RsItwwTvjSY0Xyk me3nSMKyMe9e4KkybaPuPBIYZ9TdJC0CG8dZFnFgazva0bGigBmM+esvu2vXVh1v7lmP VNAg0N4Rug70D5pB/YSdLQW6i1PWa05LR5n0QR5qyy2DS2pSjOoieqDaUPyDn1wGtTlk KEKw== X-Gm-Message-State: AOJu0Yzqz+0eVoVVa5xAH1rns/IkPOV1WPrmYTvHAdqHhVSIsK5Wa4Jx MguraOE9vAK7V8Th1Rf2aLZ0ntXVKjwp X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:9196:0:b0:5a7:af47:9dda with SMTP id i144-20020a819196000000b005a7af479ddamr293519ywg.9.1698186306785; Tue, 24 Oct 2023 15:25:06 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:27 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-25-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 24/50] perf events: Remove scandir in thread synthesis From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:49 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677515469047555 X-GMAIL-MSGID: 1780677515469047555 This avoids scanddir reading the directory into memory that's allocated and instead allocates on the stack. Signed-off-by: Ian Rogers --- tools/perf/util/synthetic-events.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c index a0579c7d7b9e..7cc38f2a0e9e 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -38,6 +38,7 @@ #include /* To get things like MAP_HUGETLB even on older libc headers */ #include #include +#include #include #include #include @@ -751,10 +752,10 @@ static int __event__synthesize_thread(union perf_event *comm_event, bool needs_mmap, bool mmap_data) { char filename[PATH_MAX]; - struct dirent **dirent; + struct io_dir iod; + struct io_dirent64 *dent; pid_t tgid, ppid; int rc = 0; - int i, n; /* special case: only send one comm event using passed in pid */ if (!full) { @@ -786,16 +787,19 @@ static int __event__synthesize_thread(union perf_event *comm_event, snprintf(filename, sizeof(filename), "%s/proc/%d/task", machine->root_dir, pid); - n = scandir(filename, &dirent, filter_task, NULL); - if (n < 0) - return n; + io_dir__init(&iod, open(filename, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (iod.dirfd < 0) + return -1; - for (i = 0; i < n; i++) { + while ((dent = io_dir__readdir(&iod)) != NULL) { char *end; pid_t _pid; bool kernel_thread = false; - _pid = strtol(dirent[i]->d_name, &end, 10); + if (!isdigit(dent->d_name[0])) + continue; + + _pid = strtol(dent->d_name, &end, 10); if (*end) continue; @@ -829,9 +833,7 @@ static int __event__synthesize_thread(union perf_event *comm_event, } } - for (i = 0; i < n; i++) - zfree(&dirent[i]); - free(dirent); + close(iod.dirfd); return rc; } From patchwork Tue Oct 24 22:23:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157731 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237729vqx; Tue, 24 Oct 2023 15:26:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLite/SF4uuytQZJwG6pOyDCOhKnSsvr21LAzi5gORJoZ4dx92mmonqtiBemX9fX5tV8/2 X-Received: by 2002:a17:902:db12:b0:1c2:218c:3754 with SMTP id m18-20020a170902db1200b001c2218c3754mr16225112plx.53.1698186402366; Tue, 24 Oct 2023 15:26:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186402; cv=none; d=google.com; s=arc-20160816; b=GJoHhVIZEq1oMi0p7fgBljdHzAxbK9B1IIvJV/SbSGto3HTyC910CWaDqsjAngDzE9 ekkE/PcgZPgVuXc3sqCCdO3NkHjnDdGYZdgxdVwj5+NMDJCRwsuO5yal/y3jxEN/Vmbb +ZV4UUQ0OXoT5xgEIWHeXtfkYY4MMuOyyHlknoUj5wqNq7uPUNg3kDPUVOXne266SdoA VdX3By9AhXVBVfkUtFwp834Qh3OY/VT9qB11Wo7fI6MkofxF8r2g7LBICZuozcvO0Roj dYYki4jEwahgPoyEj3j0ifmyTxARabWkYgP7DEiHL6W7eOwup1jl8iHBI02ZU35Kjl37 6TtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=VkeMr7e2DGGbr085ZHHtUlFQYJENbgEbiqTYBQxfi7g=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=x1deQmo3USyBfggi1elW95LEte7koPd3MGh/y1MlZ9b+gg8Exl570/GI/Ie23+qIam tU77oVM/EpMUmVlLFgEZjM85CNuXk3n3jnU7V8OqVbWbm1nNklxJ3qRYXkUM8tgHuV9Q faGs5IRzse+yNUoBUfpmjp5q1l0/BClEonVuPL5oIW0/6Fr65kNTn7lCvGjgTt27Odt+ mh2wPPb5IaoSjK9WklMXlO1QiC0s0ZDeWzPREKt3banO33xtLebqmA+uO1TxhsYG3LjO EHMhO++nRzncU1t1iL2E9VH0cVxikDeVYxk26+BcPzhNNXEAlvCJ2ub2bi3Smdv9OZ0X 0EPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=N7gPJZLT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id j12-20020a170903024c00b001c9b15bf936si9288025plh.220.2023.10.24.15.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=N7gPJZLT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 9933A8021D07; Tue, 24 Oct 2023 15:26:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344527AbjJXW0P (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235041AbjJXWZq (ORCPT ); Tue, 24 Oct 2023 18:25:46 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 838C11998 for ; Tue, 24 Oct 2023 15:25:10 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da05c625cb9so768062276.2 for ; Tue, 24 Oct 2023 15:25:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186309; x=1698791109; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=VkeMr7e2DGGbr085ZHHtUlFQYJENbgEbiqTYBQxfi7g=; b=N7gPJZLTCgY34ySI2k0lhkTeRwmlC/PhUebEiyQWZiSWYbPOuQhaEt4D0LNNp5N7vR Cd2AI9B9CBDXIwNFW6KzMLAXmJLTxvBcQ2VejOc+TxtNuZSQ1GOaUyUzpRTbFScIbWbQ tF2I/KEv+sjzY0fYVfrJ0Fh+nkg+jw0YMPP5snDE0lqJBMV//42F/oRrDm8vQJlVhsAM tLt9Xwcu+CpwlzINT96fPBXU97vubMtwpYNOGd5RPomej58TNxBYp/b53DskmA6FpqMw LFUFCyMOvoOBY5RZOgjxHgH3ixMJAT6Bn7QmXGJlCcHcIlVCfmXNZ99yQQ3QpoLSqffD ydkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186309; x=1698791109; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VkeMr7e2DGGbr085ZHHtUlFQYJENbgEbiqTYBQxfi7g=; b=pwXgBH7uZHByacz31ODez6jVxRvMcPnLCIU8e3dt+BOCYbnU7DywdP/7oZ92Br6Ylc qAPDX4BCaB9LZ/5pMzhx/SzAEDprRiwYf4rneUia1yNgnPxdqGz2LP+k1VN2YWW3gNH2 szDMe9pX//wODGZgmBWKFCfCaH0fyldeNnuEvWzJgDQRnyHD2PKzo7VU0w3CEo/QNbmc 7w/+P6qeYi3ofZMs84j9b8UgdOOQknzDVW7uYveRspGqKOlLZAFeEvaIsV4C+fLP3N+a mcRDBs1ca3AFYJKajudDlO9HJHUV4XwElGQZMYylkjajExSyt87WCJIacfxdo7pf3iCv XBOw== X-Gm-Message-State: AOJu0YzqRKgCjP9Rv6vG67rGwLHyTnjhNclHtfPCU5oeNtPfgoU+wg5Z t3fah3wrz9LvnUcK1s+HEnVTlG5QmN7n X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:bccc:0:b0:d9a:c218:8177 with SMTP id l12-20020a25bccc000000b00d9ac2188177mr285569ybm.8.1698186309153; Tue, 24 Oct 2023 15:25:09 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:28 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-26-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 25/50] perf map: Simplify map_ip/unmap_ip and make map size smaller From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:38 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677505315958734 X-GMAIL-MSGID: 1780677505315958734 When mapping an IP it is either an identity mapping or a DSO relative mapping, so a single bit is required in the struct to identify this. The current code uses function pointers, adding 2 pointers per map and also pushing the size of a map beyond 1 cache line. Switch to using a byte to identify the mapping type (as well as priv and erange_warned), to avoid any masking. Change struct maps's layout to avoid holes. Before: ``` struct map { u64 start; /* 0 8 */ u64 end; /* 8 8 */ _Bool erange_warned:1; /* 16: 0 1 */ _Bool priv:1; /* 16: 1 1 */ /* XXX 6 bits hole, try to pack */ /* XXX 3 bytes hole, try to pack */ u32 prot; /* 20 4 */ u64 pgoff; /* 24 8 */ u64 reloc; /* 32 8 */ u64 (*map_ip)(const struct map *, u64); /* 40 8 */ u64 (*unmap_ip)(const struct map *, u64); /* 48 8 */ struct dso * dso; /* 56 8 */ /* --- cacheline 1 boundary (64 bytes) --- */ refcount_t refcnt; /* 64 4 */ u32 flags; /* 68 4 */ /* size: 72, cachelines: 2, members: 12 */ /* sum members: 68, holes: 1, sum holes: 3 */ /* sum bitfield members: 2 bits, bit holes: 1, sum bit holes: 6 bits */ /* last cacheline: 8 bytes */ }; ``` After: ``` struct map { u64 start; /* 0 8 */ u64 end; /* 8 8 */ u64 pgoff; /* 16 8 */ u64 reloc; /* 24 8 */ struct dso * dso; /* 32 8 */ refcount_t refcnt; /* 40 4 */ u32 prot; /* 44 4 */ u32 flags; /* 48 4 */ enum mapping_type mapping_type:8; /* 52: 0 4 */ /* Bitfield combined with next fields */ _Bool erange_warned; /* 53 1 */ _Bool priv; /* 54 1 */ /* size: 56, cachelines: 1, members: 11 */ /* padding: 1 */ /* last cacheline: 56 bytes */ }; ``` Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 3 +- tools/perf/util/map.c | 20 +-------- tools/perf/util/map.h | 83 +++++++++++++++++++----------------- tools/perf/util/symbol-elf.c | 6 +-- tools/perf/util/symbol.c | 6 +-- 5 files changed, 50 insertions(+), 68 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index be3dab9d5253..b6831a1f909d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1360,8 +1360,7 @@ __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) if (machine->vmlinux_map == NULL) return -ENOMEM; - map__set_map_ip(machine->vmlinux_map, identity__map_ip); - map__set_unmap_ip(machine->vmlinux_map, identity__map_ip); + map__set_mapping_type(machine->vmlinux_map, MAPPING_TYPE__IDENTITY); return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map); } diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index f64b83004421..54c67cb7ecef 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -109,8 +109,7 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) map__set_pgoff(map, pgoff); map__set_reloc(map, 0); map__set_dso(map, dso__get(dso)); - map__set_map_ip(map, map__dso_map_ip); - map__set_unmap_ip(map, map__dso_unmap_ip); + map__set_mapping_type(map, MAPPING_TYPE__DSO); map__set_erange_warned(map, false); refcount_set(map__refcnt(map), 1); } @@ -172,7 +171,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len, map__init(result, start, start + len, pgoff, dso); if (anon || no_dso) { - map->map_ip = map->unmap_ip = identity__map_ip; + map->mapping_type = MAPPING_TYPE__IDENTITY; /* * Set memory without DSO as loaded. All map__find_* @@ -630,18 +629,3 @@ struct maps *map__kmaps(struct map *map) } return kmap->kmaps; } - -u64 map__dso_map_ip(const struct map *map, u64 ip) -{ - return ip - map__start(map) + map__pgoff(map); -} - -u64 map__dso_unmap_ip(const struct map *map, u64 ip) -{ - return ip + map__start(map) - map__pgoff(map); -} - -u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip) -{ - return ip; -} diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 1b53d53adc86..3a3b7757da5f 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -16,23 +16,25 @@ struct dso; struct maps; struct machine; +enum mapping_type { + /* map__map_ip/map__unmap_ip are given as offsets in the DSO. */ + MAPPING_TYPE__DSO, + /* map__map_ip/map__unmap_ip are just the given ip value. */ + MAPPING_TYPE__IDENTITY, +}; + DECLARE_RC_STRUCT(map) { u64 start; u64 end; - bool erange_warned:1; - bool priv:1; - u32 prot; u64 pgoff; u64 reloc; - - /* ip -> dso rip */ - u64 (*map_ip)(const struct map *, u64); - /* dso rip -> ip */ - u64 (*unmap_ip)(const struct map *, u64); - struct dso *dso; refcount_t refcnt; + u32 prot; u32 flags; + enum mapping_type mapping_type:8; + bool erange_warned; + bool priv; }; struct kmap; @@ -41,38 +43,11 @@ struct kmap *__map__kmap(struct map *map); struct kmap *map__kmap(struct map *map); struct maps *map__kmaps(struct map *map); -/* ip -> dso rip */ -u64 map__dso_map_ip(const struct map *map, u64 ip); -/* dso rip -> ip */ -u64 map__dso_unmap_ip(const struct map *map, u64 ip); -/* Returns ip */ -u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip); - static inline struct dso *map__dso(const struct map *map) { return RC_CHK_ACCESS(map)->dso; } -static inline u64 map__map_ip(const struct map *map, u64 ip) -{ - return RC_CHK_ACCESS(map)->map_ip(map, ip); -} - -static inline u64 map__unmap_ip(const struct map *map, u64 ip) -{ - return RC_CHK_ACCESS(map)->unmap_ip(map, ip); -} - -static inline void *map__map_ip_ptr(struct map *map) -{ - return RC_CHK_ACCESS(map)->map_ip; -} - -static inline void* map__unmap_ip_ptr(struct map *map) -{ - return RC_CHK_ACCESS(map)->unmap_ip; -} - static inline u64 map__start(const struct map *map) { return RC_CHK_ACCESS(map)->start; @@ -123,6 +98,34 @@ static inline size_t map__size(const struct map *map) return map__end(map) - map__start(map); } +/* ip -> dso rip */ +static inline u64 map__dso_map_ip(const struct map *map, u64 ip) +{ + return ip - map__start(map) + map__pgoff(map); +} + +/* dso rip -> ip */ +static inline u64 map__dso_unmap_ip(const struct map *map, u64 ip) +{ + return ip + map__start(map) - map__pgoff(map); +} + +static inline u64 map__map_ip(const struct map *map, u64 ip) +{ + if ((RC_CHK_ACCESS(map)->mapping_type) == MAPPING_TYPE__DSO) + return map__dso_map_ip(map, ip); + else + return ip; +} + +static inline u64 map__unmap_ip(const struct map *map, u64 ip) +{ + if ((RC_CHK_ACCESS(map)->mapping_type) == MAPPING_TYPE__DSO) + return map__dso_unmap_ip(map, ip); + else + return ip; +} + /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */ u64 map__rip_2objdump(struct map *map, u64 rip); @@ -294,13 +297,13 @@ static inline void map__set_dso(struct map *map, struct dso *dso) RC_CHK_ACCESS(map)->dso = dso; } -static inline void map__set_map_ip(struct map *map, u64 (*map_ip)(const struct map *map, u64 ip)) +static inline void map__set_mapping_type(struct map *map, enum mapping_type type) { - RC_CHK_ACCESS(map)->map_ip = map_ip; + RC_CHK_ACCESS(map)->mapping_type = type; } -static inline void map__set_unmap_ip(struct map *map, u64 (*unmap_ip)(const struct map *map, u64 rip)) +static inline enum mapping_type map__mapping_type(struct map *map) { - RC_CHK_ACCESS(map)->unmap_ip = unmap_ip; + return RC_CHK_ACCESS(map)->mapping_type; } #endif /* __PERF_MAP_H */ diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 9e7eeaf616b8..4b934ed3bfd1 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1392,8 +1392,7 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, map__set_start(map, shdr->sh_addr + ref_reloc(kmap)); map__set_end(map, map__start(map) + shdr->sh_size); map__set_pgoff(map, shdr->sh_offset); - map__set_map_ip(map, map__dso_map_ip); - map__set_unmap_ip(map, map__dso_unmap_ip); + map__set_mapping_type(map, MAPPING_TYPE__DSO); /* Ensure maps are correctly ordered */ if (kmaps) { int err; @@ -1455,8 +1454,7 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, map__set_end(curr_map, map__start(curr_map) + shdr->sh_size); map__set_pgoff(curr_map, shdr->sh_offset); } else { - map__set_map_ip(curr_map, identity__map_ip); - map__set_unmap_ip(curr_map, identity__map_ip); + map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY); } curr_dso->symtab_type = dso->symtab_type; if (maps__insert(kmaps, curr_map)) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 82cc74b9358e..314c0263bf3c 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -956,8 +956,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, return -1; } - map__set_map_ip(curr_map, identity__map_ip); - map__set_unmap_ip(curr_map, identity__map_ip); + map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY); if (maps__insert(kmaps, curr_map)) { dso__put(ndso); return -1; @@ -1475,8 +1474,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map, map__set_start(map, map__start(new_map)); map__set_end(map, map__end(new_map)); map__set_pgoff(map, map__pgoff(new_map)); - map__set_map_ip(map, map__map_ip_ptr(new_map)); - map__set_unmap_ip(map, map__unmap_ip_ptr(new_map)); + map__set_mapping_type(map, map__mapping_type(new_map)); /* Ensure maps are correctly ordered */ map_ref = map__get(map); maps__remove(kmaps, map_ref); From patchwork Tue Oct 24 22:23:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157763 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2242148vqx; Tue, 24 Oct 2023 15:37:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEj2RtZSOsc0RGW6fRhuHlp+nRWmikGqdb7CJsAoxqmw6qlBOk5OZEwgXqqKPeaTTXoYBRo X-Received: by 2002:a05:6a00:1acd:b0:6be:43d5:6505 with SMTP id f13-20020a056a001acd00b006be43d56505mr12189883pfv.6.1698187070277; Tue, 24 Oct 2023 15:37:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187070; cv=none; d=google.com; s=arc-20160816; b=jxX0OADC8bEdVJQTusaKJ/EVt2AEX/7nXHsnRMYqPBNcnYwIIs/teHZJq6R3o2xf6K 5NbSdlpPoz0yy7EtbnhoBhfsqXkvJXkjU1bU7VmjJjJSduegFsIGc61AbGBmT18ceSap 5qWdNdu38VozZRmbav/SYtIbBfCj/sGy7YBFx0T6XAYwzaYPkjcnCpq29yTPgthMfbbZ SYN72GGqACRrWV5ybx70GXRKN6ggHayG+MSCwD2jq79bU9DRvsFqQ0qdi2oiYjb6ClC5 p7UmKYy2iA4BfPD/s++3xHeNBoioRIaGJMsh6+a31si9do0TuUciUVFKpi3Gh37mkADu q5Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=7vTFa96ZEcmOMRrSkowrskUF+opgcolRiQV7lf4w06o=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=E1KbBOViBRcCIfOesThAxtQ3REIMRcrAE0xh/FtQgz17zPWdQzN5oIY1kIt5nbgPrW dYlGFq5gSXdyGgxDnaru+6zcAFDAZR4uI2PHIuWljOsJXkujcNo66H3xplTrORY4leJ5 b2fAhmTGbuhUiInJKCYw+5me4nDpdDT4MvaJN41HlKC1ov4XTul2j4T/s02VOtHS3bsy 1P7a1TgeuQgfdfYVFFhjlFSxwchGeo3DItcZReewq6akIz7EOMdgiKLSk7BOAaskKGou 1+n27et33WhzCnBa4AXi/0DYT3UH3pGKSxN3CTEjl8oI1+AglAXaH209COGOPI9l06YX M++Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WwiL0kET; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id g10-20020aa796aa000000b006b728259b67si9001945pfk.177.2023.10.24.15.37.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:37:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WwiL0kET; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5070880219F1; Tue, 24 Oct 2023 15:37:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344748AbjJXWhH (ORCPT + 27 others); Tue, 24 Oct 2023 18:37:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344701AbjJXWgp (ORCPT ); Tue, 24 Oct 2023 18:36:45 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF2FE19AB for ; Tue, 24 Oct 2023 15:25:12 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da04fb79246so1026198276.2 for ; Tue, 24 Oct 2023 15:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186311; x=1698791111; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=7vTFa96ZEcmOMRrSkowrskUF+opgcolRiQV7lf4w06o=; b=WwiL0kET0a95L0luaxOKtH4R3VPSPwxnyqOk+vFT+lD/MzeCiYgObQYh729/vSSpGv l8dW9g0lS5N0Is1ijZzluZcmh5aykHF9Da8/sTNGfuZ/vZ6cBayNjPsDX/PNZCKWrbDi PkAwiSBTIjir6zl+hYw5UCD6R+Krwkgnaapfc+rQxhRONPnWe71PXeRjTSbept7lKEp7 WRoHna46e1UPiNYnTkEMggPbHLnTDHDsfgA0Mu/rJBIv3iVhmO+1fg4k7ZbAVwQAngFM nuM/AdNCt9uo/KnF1ObboCZPgrzn4ru7CHoekodr5CQl0B8aFI6d9dXhLzy75ir0qtqc Vw/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186311; x=1698791111; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7vTFa96ZEcmOMRrSkowrskUF+opgcolRiQV7lf4w06o=; b=r2FHATGP2tLtBNAvgdVkg0e4GE6btr+IbcKTIna6qaUln2bOIUZXnz5Bxa0FNQI4wt WP9U1MDll0eCdIjmd9dCaxJYEabvb0k6Po8pxtnpubqDEeGdoL2ksFjH0pu8WCyNvbBx JefLetot6CabOyB2yWbo4lLYsNwFTEjNef6kDDS/1SY8eU3C+V6N/wBddWJ4+ZD0rhcW I1b3fkQaEKXATXFcCTku0r0iWeYsXE50RgB3jybsa/nc7bHHnpNxrEJ3yGdl2Y3y4QqK IiZyV9yJc3ZwT672dftyDQHko4/nC8UkjyQ70rtqDjIxpR/sguinBnQZYerQqM5OLEX8 gDCQ== X-Gm-Message-State: AOJu0YwLH0llvc5SqmCZUTWh59ohJ6noFNcqUp4pP9OL1S4/CD8uVWLv 6JlqA4F+Q2YPmJHf9afwn0Wl+5cxhPCq X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:aaa9:0:b0:d9a:3a14:a5a2 with SMTP id t38-20020a25aaa9000000b00d9a3a14a5a2mr247702ybi.13.1698186311625; Tue, 24 Oct 2023 15:25:11 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:29 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-27-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 26/50] perf maps: Move symbol maps functions to maps.c From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:37:46 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678205156051046 X-GMAIL-MSGID: 1780678205156051046 Move the find and certain other symbol maps__* functions to maps.c for better abstraction. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 238 +++++++++++++++++++++++++++++++++++++ tools/perf/util/maps.h | 12 ++ tools/perf/util/symbol.c | 248 --------------------------------------- tools/perf/util/symbol.h | 1 - 4 files changed, 250 insertions(+), 249 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 233438c95b53..9a011aed4b75 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -475,3 +475,241 @@ struct map_rb_node *map_rb_node__next(struct map_rb_node *node) return rb_entry(next, struct map_rb_node, rb_node); } + +static int map__strcmp(const void *a, const void *b) +{ + const struct map *map_a = *(const struct map **)a; + const struct map *map_b = *(const struct map **)b; + const struct dso *dso_a = map__dso(map_a); + const struct dso *dso_b = map__dso(map_b); + int ret = strcmp(dso_a->short_name, dso_b->short_name); + + if (ret == 0 && map_a != map_b) { + /* + * Ensure distinct but name equal maps have an order in part to + * aid reference counting. + */ + ret = (int)map__start(map_a) - (int)map__start(map_b); + if (ret == 0) + ret = (int)((intptr_t)map_a - (intptr_t)map_b); + } + + return ret; +} + +static int map__strcmp_name(const void *name, const void *b) +{ + const struct dso *dso = map__dso(*(const struct map **)b); + + return strcmp(name, dso->short_name); +} + +void __maps__sort_by_name(struct maps *maps) +{ + qsort(maps__maps_by_name(maps), maps__nr_maps(maps), sizeof(struct map *), map__strcmp); +} + +static int map__groups__sort_by_name_from_rbtree(struct maps *maps) +{ + struct map_rb_node *rb_node; + struct map **maps_by_name = realloc(maps__maps_by_name(maps), + maps__nr_maps(maps) * sizeof(struct map *)); + int i = 0; + + if (maps_by_name == NULL) + return -1; + + up_read(maps__lock(maps)); + down_write(maps__lock(maps)); + + RC_CHK_ACCESS(maps)->maps_by_name = maps_by_name; + RC_CHK_ACCESS(maps)->nr_maps_allocated = maps__nr_maps(maps); + + maps__for_each_entry(maps, rb_node) + maps_by_name[i++] = map__get(rb_node->map); + + __maps__sort_by_name(maps); + + up_write(maps__lock(maps)); + down_read(maps__lock(maps)); + + return 0; +} + +static struct map *__maps__find_by_name(struct maps *maps, const char *name) +{ + struct map **mapp; + + if (maps__maps_by_name(maps) == NULL && + map__groups__sort_by_name_from_rbtree(maps)) + return NULL; + + mapp = bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), + sizeof(*mapp), map__strcmp_name); + if (mapp) + return *mapp; + return NULL; +} + +struct map *maps__find_by_name(struct maps *maps, const char *name) +{ + struct map_rb_node *rb_node; + struct map *map; + + down_read(maps__lock(maps)); + + + if (RC_CHK_ACCESS(maps)->last_search_by_name) { + const struct dso *dso = map__dso(RC_CHK_ACCESS(maps)->last_search_by_name); + + if (strcmp(dso->short_name, name) == 0) { + map = RC_CHK_ACCESS(maps)->last_search_by_name; + goto out_unlock; + } + } + /* + * If we have maps->maps_by_name, then the name isn't in the rbtree, + * as maps->maps_by_name mirrors the rbtree when lookups by name are + * made. + */ + map = __maps__find_by_name(maps, name); + if (map || maps__maps_by_name(maps) != NULL) + goto out_unlock; + + /* Fallback to traversing the rbtree... */ + maps__for_each_entry(maps, rb_node) { + struct dso *dso; + + map = rb_node->map; + dso = map__dso(map); + if (strcmp(dso->short_name, name) == 0) { + RC_CHK_ACCESS(maps)->last_search_by_name = map; + goto out_unlock; + } + } + map = NULL; + +out_unlock: + up_read(maps__lock(maps)); + return map; +} + +void maps__fixup_end(struct maps *maps) +{ + struct map_rb_node *prev = NULL, *curr; + + down_write(maps__lock(maps)); + + maps__for_each_entry(maps, curr) { + if (prev != NULL && !map__end(prev->map)) + map__set_end(prev->map, map__start(curr->map)); + + prev = curr; + } + + /* + * We still haven't the actual symbols, so guess the + * last map final address. + */ + if (curr && !map__end(curr->map)) + map__set_end(curr->map, ~0ULL); + + up_write(maps__lock(maps)); +} + +/* + * Merges map into maps by splitting the new map within the existing map + * regions. + */ +int maps__merge_in(struct maps *kmaps, struct map *new_map) +{ + struct map_rb_node *rb_node; + LIST_HEAD(merged); + int err = 0; + + maps__for_each_entry(kmaps, rb_node) { + struct map *old_map = rb_node->map; + + /* no overload with this one */ + if (map__end(new_map) < map__start(old_map) || + map__start(new_map) >= map__end(old_map)) + continue; + + if (map__start(new_map) < map__start(old_map)) { + /* + * |new...... + * |old.... + */ + if (map__end(new_map) < map__end(old_map)) { + /* + * |new......| -> |new..| + * |old....| -> |old....| + */ + map__set_end(new_map, map__start(old_map)); + } else { + /* + * |new.............| -> |new..| |new..| + * |old....| -> |old....| + */ + struct map_list_node *m = map_list_node__new(); + + if (!m) { + err = -ENOMEM; + goto out; + } + + m->map = map__clone(new_map); + if (!m->map) { + free(m); + err = -ENOMEM; + goto out; + } + + map__set_end(m->map, map__start(old_map)); + list_add_tail(&m->node, &merged); + map__add_pgoff(new_map, map__end(old_map) - map__start(new_map)); + map__set_start(new_map, map__end(old_map)); + } + } else { + /* + * |new...... + * |old.... + */ + if (map__end(new_map) < map__end(old_map)) { + /* + * |new..| -> x + * |old.........| -> |old.........| + */ + map__put(new_map); + new_map = NULL; + break; + } else { + /* + * |new......| -> |new...| + * |old....| -> |old....| + */ + map__add_pgoff(new_map, map__end(old_map) - map__start(new_map)); + map__set_start(new_map, map__end(old_map)); + } + } + } + +out: + while (!list_empty(&merged)) { + struct map_list_node *old_node; + + old_node = list_entry(merged.next, struct map_list_node, node); + list_del_init(&old_node->node); + if (!err) + err = maps__insert(kmaps, old_node->map); + map__put(old_node->map); + free(old_node); + } + + if (new_map) { + if (!err) + err = maps__insert(kmaps, new_map); + map__put(new_map); + } + return err; +} diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 83144e0645ed..a689149be8c4 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -21,6 +21,16 @@ struct map_rb_node { struct map *map; }; +struct map_list_node { + struct list_head node; + struct map *map; +}; + +static inline struct map_list_node *map_list_node__new(void) +{ + return malloc(sizeof(struct map_list_node)); +} + struct map_rb_node *maps__first(struct maps *maps); struct map_rb_node *map_rb_node__next(struct map_rb_node *node); struct map_rb_node *maps__find_node(struct maps *maps, struct map *map); @@ -133,4 +143,6 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map); void __maps__sort_by_name(struct maps *maps); +void maps__fixup_end(struct maps *maps); + #endif // __PERF_MAPS_H diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 314c0263bf3c..1cc42b8d8afb 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -48,11 +48,6 @@ static bool symbol__is_idle(const char *name); int vmlinux_path__nr_entries; char **vmlinux_path; -struct map_list_node { - struct list_head node; - struct map *map; -}; - struct symbol_conf symbol_conf = { .nanosecs = false, .use_modules = true, @@ -90,11 +85,6 @@ static enum dso_binary_type binary_type_symtab[] = { #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) -static struct map_list_node *map_list_node__new(void) -{ - return malloc(sizeof(struct map_list_node)); -} - static bool symbol_type__filter(char symbol_type) { symbol_type = toupper(symbol_type); @@ -270,29 +260,6 @@ void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms) curr->end = roundup(curr->start, 4096) + 4096; } -void maps__fixup_end(struct maps *maps) -{ - struct map_rb_node *prev = NULL, *curr; - - down_write(maps__lock(maps)); - - maps__for_each_entry(maps, curr) { - if (prev != NULL && !map__end(prev->map)) - map__set_end(prev->map, map__start(curr->map)); - - prev = curr; - } - - /* - * We still haven't the actual symbols, so guess the - * last map final address. - */ - if (curr && !map__end(curr->map)) - map__set_end(curr->map, ~0ULL); - - up_write(maps__lock(maps)); -} - struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name) { size_t namelen = strlen(name) + 1; @@ -1270,103 +1237,6 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) return 0; } -/* - * Merges map into maps by splitting the new map within the existing map - * regions. - */ -int maps__merge_in(struct maps *kmaps, struct map *new_map) -{ - struct map_rb_node *rb_node; - LIST_HEAD(merged); - int err = 0; - - maps__for_each_entry(kmaps, rb_node) { - struct map *old_map = rb_node->map; - - /* no overload with this one */ - if (map__end(new_map) < map__start(old_map) || - map__start(new_map) >= map__end(old_map)) - continue; - - if (map__start(new_map) < map__start(old_map)) { - /* - * |new...... - * |old.... - */ - if (map__end(new_map) < map__end(old_map)) { - /* - * |new......| -> |new..| - * |old....| -> |old....| - */ - map__set_end(new_map, map__start(old_map)); - } else { - /* - * |new.............| -> |new..| |new..| - * |old....| -> |old....| - */ - struct map_list_node *m = map_list_node__new(); - - if (!m) { - err = -ENOMEM; - goto out; - } - - m->map = map__clone(new_map); - if (!m->map) { - free(m); - err = -ENOMEM; - goto out; - } - - map__set_end(m->map, map__start(old_map)); - list_add_tail(&m->node, &merged); - map__add_pgoff(new_map, map__end(old_map) - map__start(new_map)); - map__set_start(new_map, map__end(old_map)); - } - } else { - /* - * |new...... - * |old.... - */ - if (map__end(new_map) < map__end(old_map)) { - /* - * |new..| -> x - * |old.........| -> |old.........| - */ - map__put(new_map); - new_map = NULL; - break; - } else { - /* - * |new......| -> |new...| - * |old....| -> |old....| - */ - map__add_pgoff(new_map, map__end(old_map) - map__start(new_map)); - map__set_start(new_map, map__end(old_map)); - } - } - } - -out: - while (!list_empty(&merged)) { - struct map_list_node *old_node; - - old_node = list_entry(merged.next, struct map_list_node, node); - list_del_init(&old_node->node); - if (!err) - err = maps__insert(kmaps, old_node->map); - map__put(old_node->map); - free(old_node); - } - - if (new_map) { - if (!err) - err = maps__insert(kmaps, new_map); - map__put(new_map); - } - return err; -} - static int dso__load_kcore(struct dso *dso, struct map *map, const char *kallsyms_filename) { @@ -2065,124 +1935,6 @@ int dso__load(struct dso *dso, struct map *map) return ret; } -static int map__strcmp(const void *a, const void *b) -{ - const struct map *map_a = *(const struct map **)a; - const struct map *map_b = *(const struct map **)b; - const struct dso *dso_a = map__dso(map_a); - const struct dso *dso_b = map__dso(map_b); - int ret = strcmp(dso_a->short_name, dso_b->short_name); - - if (ret == 0 && map_a != map_b) { - /* - * Ensure distinct but name equal maps have an order in part to - * aid reference counting. - */ - ret = (int)map__start(map_a) - (int)map__start(map_b); - if (ret == 0) - ret = (int)((intptr_t)map_a - (intptr_t)map_b); - } - - return ret; -} - -static int map__strcmp_name(const void *name, const void *b) -{ - const struct dso *dso = map__dso(*(const struct map **)b); - - return strcmp(name, dso->short_name); -} - -void __maps__sort_by_name(struct maps *maps) -{ - qsort(maps__maps_by_name(maps), maps__nr_maps(maps), sizeof(struct map *), map__strcmp); -} - -static int map__groups__sort_by_name_from_rbtree(struct maps *maps) -{ - struct map_rb_node *rb_node; - struct map **maps_by_name = realloc(maps__maps_by_name(maps), - maps__nr_maps(maps) * sizeof(struct map *)); - int i = 0; - - if (maps_by_name == NULL) - return -1; - - up_read(maps__lock(maps)); - down_write(maps__lock(maps)); - - RC_CHK_ACCESS(maps)->maps_by_name = maps_by_name; - RC_CHK_ACCESS(maps)->nr_maps_allocated = maps__nr_maps(maps); - - maps__for_each_entry(maps, rb_node) - maps_by_name[i++] = map__get(rb_node->map); - - __maps__sort_by_name(maps); - - up_write(maps__lock(maps)); - down_read(maps__lock(maps)); - - return 0; -} - -static struct map *__maps__find_by_name(struct maps *maps, const char *name) -{ - struct map **mapp; - - if (maps__maps_by_name(maps) == NULL && - map__groups__sort_by_name_from_rbtree(maps)) - return NULL; - - mapp = bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), - sizeof(*mapp), map__strcmp_name); - if (mapp) - return *mapp; - return NULL; -} - -struct map *maps__find_by_name(struct maps *maps, const char *name) -{ - struct map_rb_node *rb_node; - struct map *map; - - down_read(maps__lock(maps)); - - - if (RC_CHK_ACCESS(maps)->last_search_by_name) { - const struct dso *dso = map__dso(RC_CHK_ACCESS(maps)->last_search_by_name); - - if (strcmp(dso->short_name, name) == 0) { - map = RC_CHK_ACCESS(maps)->last_search_by_name; - goto out_unlock; - } - } - /* - * If we have maps->maps_by_name, then the name isn't in the rbtree, - * as maps->maps_by_name mirrors the rbtree when lookups by name are - * made. - */ - map = __maps__find_by_name(maps, name); - if (map || maps__maps_by_name(maps) != NULL) - goto out_unlock; - - /* Fallback to traversing the rbtree... */ - maps__for_each_entry(maps, rb_node) { - struct dso *dso; - - map = rb_node->map; - dso = map__dso(map); - if (strcmp(dso->short_name, name) == 0) { - RC_CHK_ACCESS(maps)->last_search_by_name = map; - goto out_unlock; - } - } - map = NULL; - -out_unlock: - up_read(maps__lock(maps)); - return map; -} - int dso__load_vmlinux(struct dso *dso, struct map *map, const char *vmlinux, bool vmlinux_allocated) { diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index af87c46b3f89..071837ddce2a 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -189,7 +189,6 @@ void __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym, void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym); void symbols__fixup_duplicate(struct rb_root_cached *symbols); void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms); -void maps__fixup_end(struct maps *maps); typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data); int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data, From patchwork Tue Oct 24 22:23:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157728 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237677vqx; Tue, 24 Oct 2023 15:26:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEMK/oZ+Yu1+pyKxQSbE4jGJbWK/rztoVlMtF+DAPX6tjoqUtmQERiCD9KmJyPcKsEw3L7/ X-Received: by 2002:a17:902:b48d:b0:1c5:de4c:f079 with SMTP id y13-20020a170902b48d00b001c5de4cf079mr10346298plr.33.1698186393694; Tue, 24 Oct 2023 15:26:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186393; cv=none; d=google.com; s=arc-20160816; b=zJHhq7jUNS8yYVhe2P8OHmzuxKEMxFctv5nUf+L/IaU8UbUVyuHE1iszPrRPV1I7Ar xXQ91vtJk1FC1GSxyqtp6PMn5L2x0Shxu2O5yk1dOSthPOaykvsnpECTVVzW/zDmNOxO mR1uwsT407dCkaBn4YFRKGdfutlN28/MZxzq8O3Ty+HOTFERTPVBw9PT6b/1iM4E14ww hOVpRMw0wiln4v8kJ+/QKaVryHXWXQRiS0XjphxH9GKL2tgLXViZ3xjzOVF1suv6P/2y CrwvCQQlP7u35IPAL4VX/AntyGjVybn7mhSzhfku9we7BYpC9BnC3UjY65wiwnGwsPv9 uvPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=e5GSFwgTEaHeeY2eroEgFXWdX43cKEeIAHSJl2+ftFs=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=IxMm73nUDBLEdDeqEAWT3yafNrvObokfY0RBqwYgfWc/GpZvNPWto5R8r6u9SKccyU hxk3+eNpk7nTZlRcHIGKV2TPa9GTb7SVK9nAwM1IfGjFEa2Gn70MsZv2i2IdYzV+mlKP F85I+GTeVhoK2Zq1YJwhD5IhmKT3wC5mAm+miUCOQvarXJG9R+JlP1mTvofpOmusJG2X PaDRnvFD77rhl5A89iRoLJ+t89pz3mDxeSLe4B8SZ5IG6lLeRrgRkWHrm4zQXKOer+ia XvxrIrghCsJ5Y7+Jpdid9G4MAhWH5B60tOo246OKSOuS38L4lXS4RzO0NJK+0rGZJgPv NCXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0I8qcJyc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id b9-20020a170903228900b001c61073b06bsi6141115plh.427.2023.10.24.15.26.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0I8qcJyc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 84E2F80AA265; Tue, 24 Oct 2023 15:26:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344513AbjJXW0U (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235051AbjJXWZr (ORCPT ); Tue, 24 Oct 2023 18:25:47 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 212A419AD for ; Tue, 24 Oct 2023 15:25:15 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a541b720aso5901684276.0 for ; Tue, 24 Oct 2023 15:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186314; x=1698791114; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=e5GSFwgTEaHeeY2eroEgFXWdX43cKEeIAHSJl2+ftFs=; b=0I8qcJyc3yn8lYPUk7aGbCk15kVD0PJnIqIpfMbU1cSVQQPlXP7U3OjyPmOQGxmjiX rjKs4Lo2BeOP+4NszZQAbgiABujNw5DuGX9G0+do0DukMj3YGTqsut8PsRGYFEBm2dhP sVzbPdXeDPLMHzkQ85eInORAXgCOG7Mux+CH/FD6ORP4rDjoCfKKFpr3KBcEvmZwI4Rx ZdQXu9y7Kt8PcmT54AEdsgCF1e/mISqGhqhrcmmRczT3x6QbW2XTiCsZM32b6pDATWW5 yEQY+LCNr7MO1+g5peninPgAi4fX4ND8OybYi1Ovx3cMuc2MguABw99JhAfadDBAmhdx yHfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186314; x=1698791114; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e5GSFwgTEaHeeY2eroEgFXWdX43cKEeIAHSJl2+ftFs=; b=jqs0H2Qjhqi0UHLi4tnhrhs0IKMWT+enojoKaa8UkrMS4hhE2cp3frprKVRLaxRHNC GFC8v0g0+GVvigS5C+ClBON1RzxPS8Vo47rvscTCW3QoYFLglPfCDS+VnhQ5pF0o+yLl kkJVBh/2y6rWXmcGDDOIcUMGTLuRq50LIhp68ibXI2aV9N9UNuNPaxwsHImoasdOqzhh XFbnfTaqszTT9ieCIeH4kxUY/iPaVWGNX40BRoLjpSgEi/MeIISkoRH9DjfFRXiTVnG8 7EYfN6UBJ5rErnFUD5Yf8cwQmEeT6HOI/b20sEJAj4U6FsuhXbw8xs8VQcAqheTuLEV6 2bnA== X-Gm-Message-State: AOJu0YwKA0DnUVxos+qQCR/ptCU+1pMBesVkcajzB9SpCrkLSHtQNMTV Iu4swQwQAZw9XOVSNi/d7oH2aSZnRM/w X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:544:b0:d13:856b:c10a with SMTP id z4-20020a056902054400b00d13856bc10amr287911ybs.3.1698186314186; Tue, 24 Oct 2023 15:25:14 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:30 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-28-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 27/50] perf thread: Add missing RC_CHK_ACCESS From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677496060042899 X-GMAIL-MSGID: 1780677496060042899 Comparing pointers without RC_CHK_ACCESS means the indirect object will be compared rather than the underlying maps when REFCNT_CHECKING is enabled. Fix by adding missing RC_CHK_ACCESS. Signed-off-by: Ian Rogers --- tools/perf/util/thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index fe5e6991ae4b..eae3041408a7 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -385,7 +385,7 @@ static int thread__clone_maps(struct thread *thread, struct thread *parent, bool if (thread__pid(thread) == thread__pid(parent)) return thread__prepare_access(thread); - if (thread__maps(thread) == thread__maps(parent)) { + if (RC_CHK_ACCESS(thread__maps(thread)) == RC_CHK_ACCESS(thread__maps(parent))) { pr_debug("broken map groups on thread %d/%d parent %d/%d\n", thread__pid(thread), thread__tid(thread), thread__pid(parent), thread__tid(parent)); From patchwork Tue Oct 24 22:23:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157762 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2242115vqx; Tue, 24 Oct 2023 15:37:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2/M+XYYslO3qOmXJUoNTxoj8tP+i8g0AyPJMzj5pulqu6AfMfTxBnxA/H8cMLG6zkplXs X-Received: by 2002:a17:90a:d443:b0:27d:549b:3e65 with SMTP id cz3-20020a17090ad44300b0027d549b3e65mr12262783pjb.49.1698187066394; Tue, 24 Oct 2023 15:37:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187066; cv=none; d=google.com; s=arc-20160816; b=nU6f9x1Yg0eclo8ZLxN3Mnvxr39GwB3WgBRx7h6JWIw8xFS9zAd1OO24bKJL83a/az Hk7nDqqgF8Y8WQ1KHtGsakS8W86Kw4GaSe6kqSP9m31tiggbEuLym8EV8/YDoQWYGhE1 zYM3lHWnb4Mpc7z7zwLeozfPNf/ejD1qfR+ztJpFLQNPJ8Mq+W2RrGRUcU2ikhUXUohJ uDsJ84TBsIBn/TFQ5qp35+UkZcVdi952qY0Slg7Vk8UTh/9gk9LTKToh+HV4gQQUnaD8 QYnyvQamHfOJ98mGQsd3qqbRQ1c9O0lrFkSxohSxJoqVAf5lCLDk9UhH31gKGcn0F+lD rcMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=pz/huDXZnCv4WpZbLBWJkVcH6X2J/pDveZLu7bi+8Bs=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=MW9ODt2UxebWUeR0KFutvoSsx9igEbVKH2GE4+1YMVAA362BFyKVPFo9+2gDDhoFvA Db7LB6dMyS4NDZ2OT9BS5VXH9P+uBksW+gV+E6VUpat8R/a/jnTSC480W1+5b5ZEBM91 I5i/E3H8CQWLMIK8W5qVQVx/NMAWuTVS9NwsLYXSGZGsGyIEsvcGm/puFZ0pI+iJTH+C CkpZrqf2zKgo61zHXem9v58JXdUlWUQzU8yWSXEXSjkxIVllpWhxK9SlCsLouGqk23ov 7iTUufr56c8w1ny8Oqzl3DmBRFUPFn8skA32qfnZeksw5AwZX5F26StAkEDQVqsFDiFB Kwkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nAo67VtD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id nl11-20020a17090b384b00b002749baa2edesi12346258pjb.24.2023.10.24.15.37.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:37:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nAo67VtD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D55AF8024B29; Tue, 24 Oct 2023 15:37:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344683AbjJXWhF (ORCPT + 27 others); Tue, 24 Oct 2023 18:37:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344697AbjJXWgo (ORCPT ); Tue, 24 Oct 2023 18:36:44 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF66C269F for ; Tue, 24 Oct 2023 15:25:17 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a839b31a0dso94320637b3.0 for ; Tue, 24 Oct 2023 15:25:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186316; x=1698791116; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=pz/huDXZnCv4WpZbLBWJkVcH6X2J/pDveZLu7bi+8Bs=; b=nAo67VtDE5EUgapv9HttFk8STcAU8mRbYsPzyTT8kb+YiMRx7jsKNFXhZNWQ1EbNHK E16SzbzZz/VeVoQy/cSRwLqf1nA8b4TMPBg4e/ohD5hcvy/izFP73XKRtgj5Q4YAmT8l LnYT1r067RIB+JMkDwH6JATjxa4yrI2xg1rJPWZOrZ5vAuBeuQblykeNTSbnt5YtgZD1 EdqBgTC+e9FxbXvZT949+ek7e6yru9XRhb76f85wqU1bd67bdZO4WiQQXHo/dW1eTayW svmDAs4nkreyKQLJm8h+GawxYhY3uVnCpE2j/UDZn0qNb0DvV8aUSjfpM0W3lVVWmrpR +MHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186316; x=1698791116; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pz/huDXZnCv4WpZbLBWJkVcH6X2J/pDveZLu7bi+8Bs=; b=F66B5kmBOAVmnxUZeC9L+tbA38aHTYnxv8GWVGoqAhaqGEJhUrKoU6zDL2s0JrG/ok nUbfjvtNYo/NwMw+ryI5iguQbnFl0yYUv4HVr7yOD24GCcObXfWFNEin1hP3mHu6SLFL QxK04oNKtQmBIfeZSa5AI9RNbm6DPS4jRmABTMfNG9MK4YXCscvU5iBy1UpDpkX3tZvA CDrDWCJF4qnk9SOJQxE/I/lt1aPzTCQI3oKa1lYNECXtaNGCww3u7KUpVZ7QZD5e+3e+ qUVfRoKCMtjlYx5to4KnWjVJE3X1707yRp6o7imLyrWEmaJeduZ21VzpGY3lO31f7Eha nR/g== X-Gm-Message-State: AOJu0YxWg34vmD27RneFKLxjYNjYHcBAXF3CKcCYl8mREa+2WKTUWoKD jctOXuXcuVqifvJS6+RuVapD2fkOA1wy X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:4e0e:0:b0:583:4f82:b9d9 with SMTP id c14-20020a814e0e000000b005834f82b9d9mr303273ywb.5.1698186316289; Tue, 24 Oct 2023 15:25:16 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:31 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-29-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 28/50] perf maps: Add maps__for_each_map to call a function on each entry From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:37:33 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678201381075205 X-GMAIL-MSGID: 1780678201381075205 Most current uses of maps don't take the rwsem introducing a risk that the maps will change during iteration. Introduce maps__for_each_map that iterates the entries under the read lock of the rwsem. This replaces the maps__for_each_entry macro that is moved into maps.c. maps__for_each_entry_safe will be replaced in a later change. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/event.c | 103 +++++++------ tools/perf/builtin-report.c | 54 ++++--- tools/perf/tests/maps.c | 61 +++++--- tools/perf/tests/vmlinux-kallsyms.c | 181 ++++++++++++----------- tools/perf/util/machine.c | 53 ++++--- tools/perf/util/maps.c | 102 ++++++++----- tools/perf/util/maps.h | 6 +- tools/perf/util/probe-event.c | 40 +++-- tools/perf/util/symbol.c | 36 ++--- tools/perf/util/synthetic-events.c | 118 ++++++++------- tools/perf/util/thread.c | 35 +++-- tools/perf/util/unwind-libunwind-local.c | 34 +++-- tools/perf/util/vdso.c | 35 +++-- 13 files changed, 508 insertions(+), 350 deletions(-) diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/event.c index 5741ffe47312..e65b7dbe27fb 100644 --- a/tools/perf/arch/x86/util/event.c +++ b/tools/perf/arch/x86/util/event.c @@ -14,66 +14,79 @@ #if defined(__x86_64__) -int perf_event__synthesize_extra_kmaps(struct perf_tool *tool, - perf_event__handler_t process, - struct machine *machine) +struct perf_event__synthesize_extra_kmaps_cb_args { + struct perf_tool *tool; + perf_event__handler_t process; + struct machine *machine; + union perf_event *event; +}; + +static int perf_event__synthesize_extra_kmaps_cb(struct map *map, void *data) { - int rc = 0; - struct map_rb_node *pos; - struct maps *kmaps = machine__kernel_maps(machine); - union perf_event *event = zalloc(sizeof(event->mmap) + - machine->id_hdr_size); + struct perf_event__synthesize_extra_kmaps_cb_args *args = data; + union perf_event *event = args->event; + struct kmap *kmap; + size_t size; - if (!event) { - pr_debug("Not enough memory synthesizing mmap event " - "for extra kernel maps\n"); - return -1; - } + if (!__map__is_extra_kernel_map(map)) + return 0; - maps__for_each_entry(kmaps, pos) { - struct kmap *kmap; - size_t size; - struct map *map = pos->map; + kmap = map__kmap(map); - if (!__map__is_extra_kernel_map(map)) - continue; + size = sizeof(event->mmap) - sizeof(event->mmap.filename) + + PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) + + args->machine->id_hdr_size; - kmap = map__kmap(map); + memset(event, 0, size); - size = sizeof(event->mmap) - sizeof(event->mmap.filename) + - PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) + - machine->id_hdr_size; + event->mmap.header.type = PERF_RECORD_MMAP; - memset(event, 0, size); + /* + * kernel uses 0 for user space maps, see kernel/perf_event.c + * __perf_event_mmap + */ + if (machine__is_host(args->machine)) + event->header.misc = PERF_RECORD_MISC_KERNEL; + else + event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; - event->mmap.header.type = PERF_RECORD_MMAP; + event->mmap.header.size = size; - /* - * kernel uses 0 for user space maps, see kernel/perf_event.c - * __perf_event_mmap - */ - if (machine__is_host(machine)) - event->header.misc = PERF_RECORD_MISC_KERNEL; - else - event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; + event->mmap.start = map__start(map); + event->mmap.len = map__size(map); + event->mmap.pgoff = map__pgoff(map); + event->mmap.pid = args->machine->pid; - event->mmap.header.size = size; + strlcpy(event->mmap.filename, kmap->name, PATH_MAX); - event->mmap.start = map__start(map); - event->mmap.len = map__size(map); - event->mmap.pgoff = map__pgoff(map); - event->mmap.pid = machine->pid; + if (perf_tool__process_synth_event(args->tool, event, args->machine, args->process) != 0) + return -1; - strlcpy(event->mmap.filename, kmap->name, PATH_MAX); + return 0; +} - if (perf_tool__process_synth_event(tool, event, machine, - process) != 0) { - rc = -1; - break; - } +int perf_event__synthesize_extra_kmaps(struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine) +{ + int rc; + struct maps *kmaps = machine__kernel_maps(machine); + struct perf_event__synthesize_extra_kmaps_cb_args args = { + .tool = tool, + .process = process, + .machine = machine, + .event = zalloc(sizeof(args.event->mmap) + machine->id_hdr_size), + }; + + if (!args.event) { + pr_debug("Not enough memory synthesizing mmap event " + "for extra kernel maps\n"); + return -1; } - free(event); + rc = maps__for_each_map(kmaps, perf_event__synthesize_extra_kmaps_cb, &args); + + free(args.event); return rc; } diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 749246817aed..d7f1f3c8cb7d 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -844,27 +844,47 @@ static struct task *tasks_list(struct task *task, struct machine *machine) return tasks_list(parent_task, machine); } -static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) +struct maps__fprintf_task_args { + int indent; + FILE *fp; + size_t printed; +}; + +static int maps__fprintf_task_cb(struct map *map, void *data) { - size_t printed = 0; - struct map_rb_node *rb_node; + struct maps__fprintf_task_args *args = data; + const struct dso *dso = map__dso(map); + u32 prot = map__prot(map); + int ret; - maps__for_each_entry(maps, rb_node) { - struct map *map = rb_node->map; - const struct dso *dso = map__dso(map); - u32 prot = map__prot(map); + ret = fprintf(args->fp, + "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n", + args->indent, "", map__start(map), map__end(map), + prot & PROT_READ ? 'r' : '-', + prot & PROT_WRITE ? 'w' : '-', + prot & PROT_EXEC ? 'x' : '-', + map__flags(map) ? 's' : 'p', + map__pgoff(map), + dso->id.ino, dso->name); - printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n", - indent, "", map__start(map), map__end(map), - prot & PROT_READ ? 'r' : '-', - prot & PROT_WRITE ? 'w' : '-', - prot & PROT_EXEC ? 'x' : '-', - map__flags(map) ? 's' : 'p', - map__pgoff(map), - dso->id.ino, dso->name); - } + if (ret < 0) + return ret; + + args->printed += ret; + return 0; +} + +static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) +{ + struct maps__fprintf_task_args args = { + .indent = indent, + .fp = fp, + .printed = 0, + }; + + maps__for_each_map(maps, maps__fprintf_task_cb, &args); - return printed; + return args.printed; } static void task__print_level(struct task *task, FILE *fp, int level) diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index 5bb1123a91a7..bb3fbfe5a73e 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -14,44 +14,59 @@ struct map_def { u64 end; }; +struct check_maps_cb_args { + struct map_def *merged; + unsigned int i; +}; + +static int check_maps_cb(struct map *map, void *data) +{ + struct check_maps_cb_args *args = data; + struct map_def *merged = &args->merged[args->i]; + + if (map__start(map) != merged->start || + map__end(map) != merged->end || + strcmp(map__dso(map)->name, merged->name) || + refcount_read(map__refcnt(map)) != 1) { + return 1; + } + args->i++; + return 0; +} + +static int failed_cb(struct map *map, void *data __maybe_unused) +{ + pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: %d\n", + map__start(map), + map__end(map), + map__dso(map)->name, + refcount_read(map__refcnt(map))); + + return 0; +} + static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps) { - struct map_rb_node *rb_node; - unsigned int i = 0; bool failed = false; if (maps__nr_maps(maps) != size) { pr_debug("Expected %d maps, got %d", size, maps__nr_maps(maps)); failed = true; } else { - maps__for_each_entry(maps, rb_node) { - struct map *map = rb_node->map; - - if (map__start(map) != merged[i].start || - map__end(map) != merged[i].end || - strcmp(map__dso(map)->name, merged[i].name) || - refcount_read(map__refcnt(map)) != 1) { - failed = true; - } - i++; - } + struct check_maps_cb_args args = { + .merged = merged, + .i = 0, + }; + failed = maps__for_each_map(maps, check_maps_cb, &args); } if (failed) { pr_debug("Expected:\n"); - for (i = 0; i < size; i++) { + for (unsigned int i = 0; i < size; i++) { pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: 1\n", merged[i].start, merged[i].end, merged[i].name); } pr_debug("Got:\n"); - maps__for_each_entry(maps, rb_node) { - struct map *map = rb_node->map; - - pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: %d\n", - map__start(map), - map__end(map), - map__dso(map)->name, - refcount_read(map__refcnt(map))); - } + maps__for_each_map(maps, failed_cb, NULL); } return failed ? TEST_FAIL : TEST_OK; } diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux-kallsyms.c index 1078a93b01aa..822f893e67d5 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -112,18 +112,92 @@ static bool is_ignored_symbol(const char *name, char type) return false; } +struct test__vmlinux_matches_kallsyms_cb_args { + struct machine kallsyms; + struct map *vmlinux_map; + bool header_printed; +}; + +static int test__vmlinux_matches_kallsyms_cb1(struct map *map, void *data) +{ + struct test__vmlinux_matches_kallsyms_cb_args *args = data; + struct dso *dso = map__dso(map); + /* + * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while + * the kernel will have the path for the vmlinux file being used, so use + * the short name, less descriptive but the same ("[kernel]" in both + * cases. + */ + struct map *pair = maps__find_by_name(args->kallsyms.kmaps, + (dso->kernel ? dso->short_name : dso->name)); + + if (pair) + map__set_priv(pair, 1); + else { + if (!args->header_printed) { + pr_info("WARN: Maps only in vmlinux:\n"); + args->header_printed = true; + } + map__fprintf(map, stderr); + } + return 0; +} + +static int test__vmlinux_matches_kallsyms_cb2(struct map *map, void *data) +{ + struct test__vmlinux_matches_kallsyms_cb_args *args = data; + struct map *pair; + u64 mem_start = map__unmap_ip(args->vmlinux_map, map__start(map)); + u64 mem_end = map__unmap_ip(args->vmlinux_map, map__end(map)); + + pair = maps__find(args->kallsyms.kmaps, mem_start); + if (pair == NULL || map__priv(pair)) + return 0; + + if (map__start(pair) == mem_start) { + struct dso *dso = map__dso(map); + + if (!args->header_printed) { + pr_info("WARN: Maps in vmlinux with a different name in kallsyms:\n"); + args->header_printed = true; + } + + pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", + map__start(map), map__end(map), map__pgoff(map), dso->name); + if (mem_end != map__end(pair)) + pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64, + map__start(pair), map__end(pair), map__pgoff(pair)); + pr_info(" %s\n", dso->name); + map__set_priv(pair, 1); + } + return 0; +} + +static int test__vmlinux_matches_kallsyms_cb3(struct map *map, void *data) +{ + struct test__vmlinux_matches_kallsyms_cb_args *args = data; + + if (!map__priv(map)) { + if (!args->header_printed) { + pr_info("WARN: Maps only in kallsyms:\n"); + args->header_printed = true; + } + map__fprintf(map, stderr); + } + return 0; +} + static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused, int subtest __maybe_unused) { int err = TEST_FAIL; struct rb_node *nd; struct symbol *sym; - struct map *kallsyms_map, *vmlinux_map; - struct map_rb_node *rb_node; - struct machine kallsyms, vmlinux; + struct map *kallsyms_map; + struct machine vmlinux; struct maps *maps; u64 mem_start, mem_end; - bool header_printed; + struct test__vmlinux_matches_kallsyms_cb_args args; /* * Step 1: @@ -131,7 +205,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused * Init the machines that will hold kernel, modules obtained from * both vmlinux + .ko files and from /proc/kallsyms split by modules. */ - machine__init(&kallsyms, "", HOST_KERNEL_ID); + machine__init(&args.kallsyms, "", HOST_KERNEL_ID); machine__init(&vmlinux, "", HOST_KERNEL_ID); maps = machine__kernel_maps(&vmlinux); @@ -143,7 +217,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused * load /proc/kallsyms. Also create the modules maps from /proc/modules * and find the .ko files that match them in /lib/modules/`uname -r`/. */ - if (machine__create_kernel_maps(&kallsyms) < 0) { + if (machine__create_kernel_maps(&args.kallsyms) < 0) { pr_debug("machine__create_kernel_maps failed"); err = TEST_SKIP; goto out; @@ -160,7 +234,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused * be compacted against the list of modules found in the "vmlinux" * code and with the one got from /proc/modules from the "kallsyms" code. */ - if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms") <= 0) { + if (machine__load_kallsyms(&args.kallsyms, "/proc/kallsyms") <= 0) { pr_debug("machine__load_kallsyms failed"); err = TEST_SKIP; goto out; @@ -174,7 +248,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused * to see if the running kernel was relocated by checking if it has the * same value in the vmlinux file we load. */ - kallsyms_map = machine__kernel_map(&kallsyms); + kallsyms_map = machine__kernel_map(&args.kallsyms); /* * Step 5: @@ -186,7 +260,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused goto out; } - vmlinux_map = machine__kernel_map(&vmlinux); + args.vmlinux_map = machine__kernel_map(&vmlinux); /* * Step 6: @@ -213,7 +287,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused * in the kallsyms dso. For the ones that are in both, check its names and * end addresses too. */ - map__for_each_symbol(vmlinux_map, sym, nd) { + map__for_each_symbol(args.vmlinux_map, sym, nd) { struct symbol *pair, *first_pair; sym = rb_entry(nd, struct symbol, rb_node); @@ -221,10 +295,10 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused if (sym->start == sym->end) continue; - mem_start = map__unmap_ip(vmlinux_map, sym->start); - mem_end = map__unmap_ip(vmlinux_map, sym->end); + mem_start = map__unmap_ip(args.vmlinux_map, sym->start); + mem_end = map__unmap_ip(args.vmlinux_map, sym->end); - first_pair = machine__find_kernel_symbol(&kallsyms, mem_start, NULL); + first_pair = machine__find_kernel_symbol(&args.kallsyms, mem_start, NULL); pair = first_pair; if (pair && UM(pair->start) == mem_start) { @@ -253,7 +327,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused */ continue; } else { - pair = machine__find_kernel_symbol_by_name(&kallsyms, sym->name, NULL); + pair = machine__find_kernel_symbol_by_name(&args.kallsyms, + sym->name, NULL); if (pair) { if (UM(pair->start) == mem_start) goto next_pair; @@ -267,7 +342,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused continue; } - } else if (mem_start == map__end(kallsyms.vmlinux_map)) { + } else if (mem_start == map__end(args.kallsyms.vmlinux_map)) { /* * Ignore aliases to _etext, i.e. to the end of the kernel text area, * such as __indirect_thunk_end. @@ -289,78 +364,18 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused if (verbose <= 0) goto out; - header_printed = false; - - maps__for_each_entry(maps, rb_node) { - struct map *map = rb_node->map; - struct dso *dso = map__dso(map); - /* - * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while - * the kernel will have the path for the vmlinux file being used, - * so use the short name, less descriptive but the same ("[kernel]" in - * both cases. - */ - struct map *pair = maps__find_by_name(kallsyms.kmaps, (dso->kernel ? - dso->short_name : - dso->name)); - if (pair) { - map__set_priv(pair, 1); - } else { - if (!header_printed) { - pr_info("WARN: Maps only in vmlinux:\n"); - header_printed = true; - } - map__fprintf(map, stderr); - } - } - - header_printed = false; - - maps__for_each_entry(maps, rb_node) { - struct map *pair, *map = rb_node->map; - - mem_start = map__unmap_ip(vmlinux_map, map__start(map)); - mem_end = map__unmap_ip(vmlinux_map, map__end(map)); + args.header_printed = false; + maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb1, &args); - pair = maps__find(kallsyms.kmaps, mem_start); - if (pair == NULL || map__priv(pair)) - continue; - - if (map__start(pair) == mem_start) { - struct dso *dso = map__dso(map); - - if (!header_printed) { - pr_info("WARN: Maps in vmlinux with a different name in kallsyms:\n"); - header_printed = true; - } - - pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", - map__start(map), map__end(map), map__pgoff(map), dso->name); - if (mem_end != map__end(pair)) - pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64, - map__start(pair), map__end(pair), map__pgoff(pair)); - pr_info(" %s\n", dso->name); - map__set_priv(pair, 1); - } - } - - header_printed = false; - - maps = machine__kernel_maps(&kallsyms); + args.header_printed = false; + maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb2, &args); - maps__for_each_entry(maps, rb_node) { - struct map *map = rb_node->map; + args.header_printed = false; + maps = machine__kernel_maps(&args.kallsyms); + maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb3, &args); - if (!map__priv(map)) { - if (!header_printed) { - pr_info("WARN: Maps only in kallsyms:\n"); - header_printed = true; - } - map__fprintf(map, stderr); - } - } out: - machine__exit(&kallsyms); + machine__exit(&args.kallsyms); machine__exit(&vmlinux); return err; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index b6831a1f909d..3c967295c9a3 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1286,33 +1286,46 @@ static u64 find_entry_trampoline(struct dso *dso) #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000 #define X86_64_ENTRY_TRAMPOLINE 0x6000 +struct machine__map_x86_64_entry_trampolines_args { + struct maps *kmaps; + bool found; +}; + +static int machine__map_x86_64_entry_trampolines_cb(struct map *map, void *data) +{ + struct machine__map_x86_64_entry_trampolines_args *args = data; + struct map *dest_map; + struct kmap *kmap = __map__kmap(map); + + if (!kmap || !is_entry_trampoline(kmap->name)) + return 0; + + dest_map = maps__find(args->kmaps, map__pgoff(map)); + if (dest_map != map) + map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map))); + + args->found = true; + return 0; +} + /* Map x86_64 PTI entry trampolines */ int machine__map_x86_64_entry_trampolines(struct machine *machine, struct dso *kernel) { - struct maps *kmaps = machine__kernel_maps(machine); + struct machine__map_x86_64_entry_trampolines_args args = { + .kmaps = machine__kernel_maps(machine), + .found = false, + }; int nr_cpus_avail, cpu; - bool found = false; - struct map_rb_node *rb_node; u64 pgoff; /* * In the vmlinux case, pgoff is a virtual address which must now be * mapped to a vmlinux offset. */ - maps__for_each_entry(kmaps, rb_node) { - struct map *dest_map, *map = rb_node->map; - struct kmap *kmap = __map__kmap(map); - - if (!kmap || !is_entry_trampoline(kmap->name)) - continue; + maps__for_each_map(args.kmaps, machine__map_x86_64_entry_trampolines_cb, &args); - dest_map = maps__find(kmaps, map__pgoff(map)); - if (dest_map != map) - map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map))); - found = true; - } - if (found || machine->trampolines_mapped) + if (args.found || machine->trampolines_mapped) return 0; pgoff = find_entry_trampoline(kernel); @@ -3395,16 +3408,8 @@ int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv) { struct maps *maps = machine__kernel_maps(machine); - struct map_rb_node *pos; - int err = 0; - maps__for_each_entry(maps, pos) { - err = fn(pos->map, priv); - if (err != 0) { - break; - } - } - return err; + return maps__for_each_map(maps, fn, priv); } bool machine__is_lock_function(struct machine *machine, u64 addr) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 9a011aed4b75..00e6589bba10 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -10,6 +10,9 @@ #include "ui/ui.h" #include "unwind.h" +#define maps__for_each_entry(maps, map) \ + for (map = maps__first(maps); map; map = map_rb_node__next(map)) + static void maps__init(struct maps *maps, struct machine *machine) { refcount_set(maps__refcnt(maps), 1); @@ -196,6 +199,21 @@ void maps__put(struct maps *maps) RC_CHK_PUT(maps); } +int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data) +{ + struct map_rb_node *pos; + int ret = 0; + + down_read(maps__lock(maps)); + maps__for_each_entry(maps, pos) { + ret = cb(pos->map, data); + if (ret) + break; + } + up_read(maps__lock(maps)); + return ret; +} + struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) { struct map *map = maps__find(maps, addr); @@ -210,31 +228,40 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) return NULL; } -struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp) -{ +struct maps__find_symbol_by_name_args { + struct map **mapp; + const char *name; struct symbol *sym; - struct map_rb_node *pos; +}; - down_read(maps__lock(maps)); +static int maps__find_symbol_by_name_cb(struct map *map, void *data) +{ + struct maps__find_symbol_by_name_args *args = data; - maps__for_each_entry(maps, pos) { - sym = map__find_symbol_by_name(pos->map, name); + args->sym = map__find_symbol_by_name(map, args->name); + if (!args->sym) + return 0; - if (sym == NULL) - continue; - if (!map__contains_symbol(pos->map, sym)) { - sym = NULL; - continue; - } - if (mapp != NULL) - *mapp = pos->map; - goto out; + if (!map__contains_symbol(map, args->sym)) { + args->sym = NULL; + return 0; } - sym = NULL; -out: - up_read(maps__lock(maps)); - return sym; + if (args->mapp != NULL) + *args->mapp = map__get(map); + return 1; +} + +struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp) +{ + struct maps__find_symbol_by_name_args args = { + .mapp = mapp, + .name = name, + .sym = NULL, + }; + + maps__for_each_map(maps, maps__find_symbol_by_name_cb, &args); + return args.sym; } int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) @@ -253,25 +280,34 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) return ams->ms.sym ? 0 : -1; } -size_t maps__fprintf(struct maps *maps, FILE *fp) -{ - size_t printed = 0; - struct map_rb_node *pos; +struct maps__fprintf_args { + FILE *fp; + size_t printed; +}; - down_read(maps__lock(maps)); +static int maps__fprintf_cb(struct map *map, void *data) +{ + struct maps__fprintf_args *args = data; - maps__for_each_entry(maps, pos) { - printed += fprintf(fp, "Map:"); - printed += map__fprintf(pos->map, fp); - if (verbose > 2) { - printed += dso__fprintf(map__dso(pos->map), fp); - printed += fprintf(fp, "--\n"); - } + args->printed += fprintf(args->fp, "Map:"); + args->printed += map__fprintf(map, args->fp); + if (verbose > 2) { + args->printed += dso__fprintf(map__dso(map), args->fp); + args->printed += fprintf(args->fp, "--\n"); } + return 0; +} - up_read(maps__lock(maps)); +size_t maps__fprintf(struct maps *maps, FILE *fp) +{ + struct maps__fprintf_args args = { + .fp = fp, + .printed = 0, + }; + + maps__for_each_map(maps, maps__fprintf_cb, &args); - return printed; + return args.printed; } int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index a689149be8c4..8ac30cdaf5bd 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -36,9 +36,6 @@ struct map_rb_node *map_rb_node__next(struct map_rb_node *node); struct map_rb_node *maps__find_node(struct maps *maps, struct map *map); struct map *maps__find(struct maps *maps, u64 addr); -#define maps__for_each_entry(maps, map) \ - for (map = maps__first(maps); map; map = map_rb_node__next(map)) - #define maps__for_each_entry_safe(maps, map, next) \ for (map = maps__first(maps), next = map_rb_node__next(map); map; \ map = next, next = map_rb_node__next(map)) @@ -81,6 +78,9 @@ static inline void __maps__zput(struct maps **map) #define maps__zput(map) __maps__zput(&map) +/* Iterate over map calling cb for each entry. */ +int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data); + static inline struct rb_root *maps__entries(struct maps *maps) { return &RC_CHK_ACCESS(maps)->entries; diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 1a5b7fa459b2..a1a796043691 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -149,10 +149,32 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, return 0; } +struct kernel_get_module_map_cb_args { + const char *module; + struct map *result; +}; + +static int kernel_get_module_map_cb(struct map *map, void *data) +{ + struct kernel_get_module_map_cb_args *args = data; + struct dso *dso = map__dso(map); + const char *short_name = dso->short_name; /* short_name is "[module]" */ + u16 short_name_len = dso->short_name_len; + + if (strncmp(short_name + 1, args->module, short_name_len - 2) == 0 && + args->module[short_name_len - 2] == '\0') { + args->result = map__get(map); + return 1; + } + return 0; +} + static struct map *kernel_get_module_map(const char *module) { - struct maps *maps = machine__kernel_maps(host_machine); - struct map_rb_node *pos; + struct kernel_get_module_map_cb_args args = { + .module = module, + .result = NULL, + }; /* A file path -- this is an offline module */ if (module && strchr(module, '/')) @@ -164,19 +186,9 @@ static struct map *kernel_get_module_map(const char *module) return map__get(map); } - maps__for_each_entry(maps, pos) { - /* short_name is "[module]" */ - struct dso *dso = map__dso(pos->map); - const char *short_name = dso->short_name; - u16 short_name_len = dso->short_name_len; + maps__for_each_map(machine__kernel_maps(host_machine), kernel_get_module_map_cb, &args); - if (strncmp(short_name + 1, module, - short_name_len - 2) == 0 && - module[short_name_len - 2] == '\0') { - return map__get(pos->map); - } - } - return NULL; + return args.result; } struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 1cc42b8d8afb..72f03b875478 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1114,33 +1114,35 @@ int compare_proc_modules(const char *from, const char *to) return ret; } +static int do_validate_kcore_modules_cb(struct map *old_map, void *data) +{ + struct rb_root *modules = data; + struct module_info *mi; + struct dso *dso; + + if (!__map__is_kmodule(old_map)) + return 0; + + dso = map__dso(old_map); + /* Module must be in memory at the same address */ + mi = find_module(dso->short_name, modules); + if (!mi || mi->start != map__start(old_map)) + return -EINVAL; + + return 0; +} + static int do_validate_kcore_modules(const char *filename, struct maps *kmaps) { struct rb_root modules = RB_ROOT; - struct map_rb_node *old_node; int err; err = read_proc_modules(filename, &modules); if (err) return err; - maps__for_each_entry(kmaps, old_node) { - struct map *old_map = old_node->map; - struct module_info *mi; - struct dso *dso; + err = maps__for_each_map(kmaps, do_validate_kcore_modules_cb, &modules); - if (!__map__is_kmodule(old_map)) { - continue; - } - dso = map__dso(old_map); - /* Module must be in memory at the same address */ - mi = find_module(dso->short_name, &modules); - if (!mi || mi->start != map__start(old_map)) { - err = -EINVAL; - goto out; - } - } -out: delete_modules(&modules); return err; } diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c index 7cc38f2a0e9e..cdab6aa04917 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -666,18 +666,74 @@ int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused, } #endif +struct perf_event__synthesize_modules_maps_cb_args { + struct perf_tool *tool; + perf_event__handler_t process; + struct machine *machine; + union perf_event *event; +}; + +static int perf_event__synthesize_modules_maps_cb(struct map *map, void *data) +{ + struct perf_event__synthesize_modules_maps_cb_args *args = data; + union perf_event *event = args->event; + struct dso *dso; + size_t size; + + if (!__map__is_kmodule(map)) + return 0; + + dso = map__dso(map); + if (symbol_conf.buildid_mmap2) { + size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64)); + event->mmap2.header.type = PERF_RECORD_MMAP2; + event->mmap2.header.size = (sizeof(event->mmap2) - + (sizeof(event->mmap2.filename) - size)); + memset(event->mmap2.filename + size, 0, args->machine->id_hdr_size); + event->mmap2.header.size += args->machine->id_hdr_size; + event->mmap2.start = map__start(map); + event->mmap2.len = map__size(map); + event->mmap2.pid = args->machine->pid; + + memcpy(event->mmap2.filename, dso->long_name, dso->long_name_len + 1); + + perf_record_mmap2__read_build_id(&event->mmap2, args->machine, false); + } else { + size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64)); + event->mmap.header.type = PERF_RECORD_MMAP; + event->mmap.header.size = (sizeof(event->mmap) - + (sizeof(event->mmap.filename) - size)); + memset(event->mmap.filename + size, 0, args->machine->id_hdr_size); + event->mmap.header.size += args->machine->id_hdr_size; + event->mmap.start = map__start(map); + event->mmap.len = map__size(map); + event->mmap.pid = args->machine->pid; + + memcpy(event->mmap.filename, dso->long_name, dso->long_name_len + 1); + } + + if (perf_tool__process_synth_event(args->tool, event, args->machine, args->process) != 0) + return -1; + + return 0; +} + int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process, struct machine *machine) { - int rc = 0; - struct map_rb_node *pos; + int rc; struct maps *maps = machine__kernel_maps(machine); - union perf_event *event; - size_t size = symbol_conf.buildid_mmap2 ? - sizeof(event->mmap2) : sizeof(event->mmap); + struct perf_event__synthesize_modules_maps_cb_args args = { + .tool = tool, + .process = process, + .machine = machine, + }; + size_t size = symbol_conf.buildid_mmap2 + ? sizeof(args.event->mmap2) + : sizeof(args.event->mmap); - event = zalloc(size + machine->id_hdr_size); - if (event == NULL) { + args.event = zalloc(size + machine->id_hdr_size); + if (args.event == NULL) { pr_debug("Not enough memory synthesizing mmap event " "for kernel modules\n"); return -1; @@ -688,53 +744,13 @@ int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t * __perf_event_mmap */ if (machine__is_host(machine)) - event->header.misc = PERF_RECORD_MISC_KERNEL; + args.event->header.misc = PERF_RECORD_MISC_KERNEL; else - event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; - - maps__for_each_entry(maps, pos) { - struct map *map = pos->map; - struct dso *dso; + args.event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; - if (!__map__is_kmodule(map)) - continue; + rc = maps__for_each_map(maps, perf_event__synthesize_modules_maps_cb, &args); - dso = map__dso(map); - if (symbol_conf.buildid_mmap2) { - size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64)); - event->mmap2.header.type = PERF_RECORD_MMAP2; - event->mmap2.header.size = (sizeof(event->mmap2) - - (sizeof(event->mmap2.filename) - size)); - memset(event->mmap2.filename + size, 0, machine->id_hdr_size); - event->mmap2.header.size += machine->id_hdr_size; - event->mmap2.start = map__start(map); - event->mmap2.len = map__size(map); - event->mmap2.pid = machine->pid; - - memcpy(event->mmap2.filename, dso->long_name, dso->long_name_len + 1); - - perf_record_mmap2__read_build_id(&event->mmap2, machine, false); - } else { - size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64)); - event->mmap.header.type = PERF_RECORD_MMAP; - event->mmap.header.size = (sizeof(event->mmap) - - (sizeof(event->mmap.filename) - size)); - memset(event->mmap.filename + size, 0, machine->id_hdr_size); - event->mmap.header.size += machine->id_hdr_size; - event->mmap.start = map__start(map); - event->mmap.len = map__size(map); - event->mmap.pid = machine->pid; - - memcpy(event->mmap.filename, dso->long_name, dso->long_name_len + 1); - } - - if (perf_tool__process_synth_event(tool, event, machine, process) != 0) { - rc = -1; - break; - } - } - - free(event); + free(args.event); return rc; } diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index eae3041408a7..2b938ccbfe3e 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -349,34 +349,33 @@ int thread__insert_map(struct thread *thread, struct map *map) return maps__insert(thread__maps(thread), map); } -static int __thread__prepare_access(struct thread *thread) +struct thread__prepare_access_maps_cb_args { + int err; + struct maps *maps; +}; + +static int thread__prepare_access_maps_cb(struct map *map, void *data) { bool initialized = false; - int err = 0; - struct maps *maps = thread__maps(thread); - struct map_rb_node *rb_node; - - down_read(maps__lock(maps)); - - maps__for_each_entry(maps, rb_node) { - err = unwind__prepare_access(thread__maps(thread), rb_node->map, &initialized); - if (err || initialized) - break; - } + struct thread__prepare_access_maps_cb_args *args = data; - up_read(maps__lock(maps)); + args->err = unwind__prepare_access(args->maps, map, &initialized); - return err; + return (args->err || initialized) ? 1 : 0; } static int thread__prepare_access(struct thread *thread) { - int err = 0; + struct thread__prepare_access_maps_cb_args args = { + .err = 0, + }; - if (dwarf_callchain_users) - err = __thread__prepare_access(thread); + if (dwarf_callchain_users) { + args.maps = thread__maps(thread); + maps__for_each_map(thread__maps(thread), thread__prepare_access_maps_cb, &args); + } - return err; + return args.err; } static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone) diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c index c0641882fd2f..228f1565bd0b 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -302,12 +302,31 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine, return 0; } +struct read_unwind_spec_eh_frame_maps_cb_args { + struct dso *dso; + u64 base_addr; +}; + +static int read_unwind_spec_eh_frame_maps_cb(struct map *map, void *data) +{ + + struct read_unwind_spec_eh_frame_maps_cb_args *args = data; + + if (map__dso(map) == args->dso && map__start(map) < args->base_addr) + args->base_addr = map__start(map); + + return 0; +} + + static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui, u64 *table_data, u64 *segbase, u64 *fde_count) { - struct map_rb_node *map_node; - u64 base_addr = UINT64_MAX; + struct read_unwind_spec_eh_frame_maps_cb_args args = { + .dso = dso, + .base_addr = UINT64_MAX, + }; int ret, fd; if (dso->data.eh_frame_hdr_offset == 0) { @@ -325,16 +344,11 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui, return -EINVAL; } - maps__for_each_entry(thread__maps(ui->thread), map_node) { - struct map *map = map_node->map; - u64 start = map__start(map); + maps__for_each_map(thread__maps(ui->thread), read_unwind_spec_eh_frame_maps_cb, &args); - if (map__dso(map) == dso && start < base_addr) - base_addr = start; - } - base_addr -= dso->data.elf_base_addr; + args.base_addr -= dso->data.elf_base_addr; /* Address of .eh_frame_hdr */ - *segbase = base_addr + dso->data.eh_frame_hdr_addr; + *segbase = args.base_addr + dso->data.eh_frame_hdr_addr; ret = unwind_spec_ehframe(dso, ui->machine, dso->data.eh_frame_hdr_offset, table_data, fde_count); if (ret) diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index ae3eee69b659..df8963796187 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -140,23 +140,34 @@ static struct dso *__machine__addnew_vdso(struct machine *machine, const char *s return dso; } +struct machine__thread_dso_type_maps_cb_args { + struct machine *machine; + enum dso_type dso_type; +}; + +static int machine__thread_dso_type_maps_cb(struct map *map, void *data) +{ + struct machine__thread_dso_type_maps_cb_args *args = data; + struct dso *dso = map__dso(map); + + if (!dso || dso->long_name[0] != '/') + return 0; + + args->dso_type = dso__type(dso, args->machine); + return (args->dso_type != DSO__TYPE_UNKNOWN) ? 1 : 0; +} + static enum dso_type machine__thread_dso_type(struct machine *machine, struct thread *thread) { - enum dso_type dso_type = DSO__TYPE_UNKNOWN; - struct map_rb_node *rb_node; - - maps__for_each_entry(thread__maps(thread), rb_node) { - struct dso *dso = map__dso(rb_node->map); + struct machine__thread_dso_type_maps_cb_args args = { + .machine = machine, + .dso_type = DSO__TYPE_UNKNOWN, + }; - if (!dso || dso->long_name[0] != '/') - continue; - dso_type = dso__type(dso, machine); - if (dso_type != DSO__TYPE_UNKNOWN) - break; - } + maps__for_each_map(thread__maps(thread), machine__thread_dso_type_maps_cb, &args); - return dso_type; + return args.dso_type; } #if BITS_PER_LONG == 64 From patchwork Tue Oct 24 22:23:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157733 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237765vqx; Tue, 24 Oct 2023 15:26:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVKRITrSHYzXZudubpWBpaIuGVj97WsqhJ3qIDpAYB2lANsTK4D7XTYlWJbwZj3ogknD2Q X-Received: by 2002:a05:6a21:7989:b0:174:2fe:f6f7 with SMTP id bh9-20020a056a21798900b0017402fef6f7mr3802950pzc.60.1698186407521; Tue, 24 Oct 2023 15:26:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186407; cv=none; d=google.com; s=arc-20160816; b=duz+EaWsfsoUAvnBiBdpTptxJRMCvqi1t0lfSb+7xVFF8apmR1HPGRph3jufx8Mbxb r0IcOobKh+dEmsR6yae5JtpIxTobHZ9ao2u4ovl6UqfZC8sluFfuR2ZmXkWmuNS1Coas zRbOKZMjs3A6/X/m4aAzMI0tXZQT8gUqdHOeUnE9bO2ZGdnT0CPZQs0k/ZUObOZI0FI2 9PCmL0V5q7Ptrtws4xrDTP1uGfguyReXaM46/JfJteZmubV+uOiuaGgTNEAUlnAFeiEI 4ZPVlhE/XcC9Xwr6F1C3bQqvwKSBb4l/egJkXIrrPE5ZND09YruK1734Wi0+8jnEn28R cLsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=pZmLuOzKOZWnfcppPkzxkV5fHZsLOBY0ABvxWYK/enc=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=AX44tM47h3mpcuU/pJU3YUu+OT/6ZBMskKzx4hiC3aBDdnqb4mOcTJbooFit/QKDy9 V7vR0g0iuooKPszCPCOJwhQD8dwLjBCZSjaH80LQR2SxvLII/Jc4LBm34cZmMO2hbeVB z0zQU6JNhbrfxzpXs6Kc58BHi6Y88O6Vxs0j6jhxHpXaYSTKUDsTWFMTGbxWF6aM9t5v UIVYH9ookeE7GcUwbQLX6dS1av0RP4pD6bbe0PTNDHw0bVhiXBdhY9yi5mB8X1Spz/Fe z4DEAtk+i01RGP8CpQKbRuw8Qem++apxAGC3tkUdudbJXh158AGgUxSNPav3PNhEB19A rXcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ID9QBfXd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id l1-20020a056a00140100b006be062ed5c6si9166723pfu.276.2023.10.24.15.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ID9QBfXd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 66ED380219C3; Tue, 24 Oct 2023 15:26:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344447AbjJXW0d (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344478AbjJXWZv (ORCPT ); Tue, 24 Oct 2023 18:25:51 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1523126BC for ; Tue, 24 Oct 2023 15:25:20 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7d261a84bso67525687b3.3 for ; Tue, 24 Oct 2023 15:25:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186318; x=1698791118; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=pZmLuOzKOZWnfcppPkzxkV5fHZsLOBY0ABvxWYK/enc=; b=ID9QBfXdKzQpRhPP1pLBWsBqXDwvD4tx5lGwebDU+tMCIdIec9UizeDmQVVi7Lj6T7 kTZCgylWzQWvQ+/0jyAFYrou+h05rcoG4IYZEEZzkP1S23FixAPK7SffQbeICeX91ifl tabqnjxixZu4V0JrSGuh/u0QIaI1funTcwQv9h6CYEDCYHohDWCnK+ZwOU8EdfdumYsC hGGbuaWdWer42j30Vg9Bfr5yHaQazN5nK5XIW9NVN7DdEY0MrA633MU3gXYvOZLW5WoP ULEjC/szs8H7+A+IbXehdDY+Pz3aiT418JdiCFZVbGwDTwjzmBm6KqhUlk0fPvYM1jrX CJWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186318; x=1698791118; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pZmLuOzKOZWnfcppPkzxkV5fHZsLOBY0ABvxWYK/enc=; b=NTQA9Q2mMt1Pe5N0SSD8XIcOKVXSeNOpDlOEnpCGOByakd4FVDEEI350SnugqVP9AS tXyqEG/z3bElf7J4X/w4jid/jPd2TgbBG4Nzy3OjBx8FxZhGzWLpqAe48+qRHtIushPG YTE7biAY2BrP6laLbWGoV67eUzfDQ5StZ8yQDqpU6Nx2hl+rB4S7AkPDDz1kAN99JvGg n0HbLXdU1zBHGDNJLeHFn/58ccQlogEbFClHDB/NzVDanb0KbiU+TPkEDBgE8V8Nb6nD QYrucA5eG6rPGNXKmaZgrRTNf1y4gtCdYPW3AGaUUp/N7PqJ0jgQwW9G5ixtilEIxfYU ULNg== X-Gm-Message-State: AOJu0YyM5QnXjQCwr5b3G4thd+rmZOi2qVTzCzhHpXFM49VCE1H4lznP toD3T/r6sySyNV/5xUivwsigIe0prl9i X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:ca8b:0:b0:589:a855:7af with SMTP id m133-20020a0dca8b000000b00589a85507afmr268607ywd.7.1698186318505; Tue, 24 Oct 2023 15:25:18 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:32 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-30-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 29/50] perf maps: Add remove maps function to remove a map based on callback From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:45 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677510475925037 X-GMAIL-MSGID: 1780677510475925037 Removing maps wasn't being done under the write lock. Similar to maps__for_each_map, iterate the entries but in this case remove the entry based on the result of the callback. If an entry was removed then maps_by_name also needs updating, so add missed flush. In dso__load_kcore, the test of map to save would always be false with REFCNT_CHECKING because of a missing RC_CHK_ACCESS. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 24 ++++++++++++++++++++++++ tools/perf/util/maps.h | 6 ++---- tools/perf/util/symbol.c | 24 ++++++++++++------------ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 00e6589bba10..f13fd3a9686b 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -13,6 +13,10 @@ #define maps__for_each_entry(maps, map) \ for (map = maps__first(maps); map; map = map_rb_node__next(map)) +#define maps__for_each_entry_safe(maps, map, next) \ + for (map = maps__first(maps), next = map_rb_node__next(map); map; \ + map = next, next = map_rb_node__next(map)) + static void maps__init(struct maps *maps, struct machine *machine) { refcount_set(maps__refcnt(maps), 1); @@ -214,6 +218,26 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data) return ret; } +void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data) +{ + struct map_rb_node *pos, *next; + unsigned int start_nr_maps; + + down_write(maps__lock(maps)); + + start_nr_maps = maps__nr_maps(maps); + maps__for_each_entry_safe(maps, pos, next) { + if (cb(pos->map, data)) { + __maps__remove(maps, pos); + --RC_CHK_ACCESS(maps)->nr_maps; + } + } + if (maps__maps_by_name(maps) && start_nr_maps != maps__nr_maps(maps)) + __maps__free_maps_by_name(maps); + + up_write(maps__lock(maps)); +} + struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) { struct map *map = maps__find(maps, addr); diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 8ac30cdaf5bd..b94ad5c8fea7 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -36,10 +36,6 @@ struct map_rb_node *map_rb_node__next(struct map_rb_node *node); struct map_rb_node *maps__find_node(struct maps *maps, struct map *map); struct map *maps__find(struct maps *maps, u64 addr); -#define maps__for_each_entry_safe(maps, map, next) \ - for (map = maps__first(maps), next = map_rb_node__next(map); map; \ - map = next, next = map_rb_node__next(map)) - DECLARE_RC_STRUCT(maps) { struct rb_root entries; struct rw_semaphore lock; @@ -80,6 +76,8 @@ static inline void __maps__zput(struct maps **map) /* Iterate over map calling cb for each entry. */ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data); +/* Iterate over map removing an entry if cb returns true. */ +void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data); static inline struct rb_root *maps__entries(struct maps *maps) { diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 72f03b875478..30da8a405d11 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1239,13 +1239,23 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) return 0; } +static bool remove_old_maps(struct map *map, void *data) +{ + const struct map *map_to_save = data; + + /* + * We need to preserve eBPF maps even if they are covered by kcore, + * because we need to access eBPF dso for source data. + */ + return RC_CHK_ACCESS(map) != RC_CHK_ACCESS(map_to_save) && !__map__is_bpf_prog(map); +} + static int dso__load_kcore(struct dso *dso, struct map *map, const char *kallsyms_filename) { struct maps *kmaps = map__kmaps(map); struct kcore_mapfn_data md; struct map *replacement_map = NULL; - struct map_rb_node *old_node, *next; struct machine *machine; bool is_64_bit; int err, fd; @@ -1292,17 +1302,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map, } /* Remove old maps */ - maps__for_each_entry_safe(kmaps, old_node, next) { - struct map *old_map = old_node->map; - - /* - * We need to preserve eBPF maps even if they are - * covered by kcore, because we need to access - * eBPF dso for source data. - */ - if (old_map != map && !__map__is_bpf_prog(old_map)) - maps__remove(kmaps, old_map); - } + maps__remove_maps(kmaps, remove_old_maps, map); machine->trampolines_mapped = false; /* Find the kernel map using the '_stext' symbol */ From patchwork Tue Oct 24 22:23:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157735 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237851vqx; Tue, 24 Oct 2023 15:27:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFH2FAtg70yK5XUUI5NbqVh2N9GcVnP2WAkdC2+abukXvolzsR49RsK6T17bcIb3Rt4+8BD X-Received: by 2002:a05:6300:8081:b0:17a:2f1:ce1 with SMTP id ap1-20020a056300808100b0017a02f10ce1mr3827508pzc.31.1698186422006; Tue, 24 Oct 2023 15:27:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186421; cv=none; d=google.com; s=arc-20160816; b=JhWCtaxI6mTdqEnA6+eySzoCIyLAik3PCRGIwyRB1olQwWmm40nWymR+JLa9vMLh5i 7UzqEWU0ofget0wOuOX27FmfUPsDrv/mCXjEm93sGZZK8ATh+niYW2bKX0EHhFASJbXG 3MUxzS1C604iOUoeJ7wUTcx+gNHlZBBLCDfmSG2hnQWNINWPLDZhaQ0iC/xUaTSERnci 2gNur0hjWBaJ8CJ61atnkJuIPe0KTK2EcjQ5GCrMzfliG5u23Xc4BdoyuSLViUOI2pMK /teCjTV8P4xMDefeUBQTx3Mw1CRSk9nnl27AQWmDuom9m+xdiFepPCm3X8gO0vqA9htt Yy5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=zKJ+MtJQ0m3usmVnmVc1PXHVWWmmWPISerzupGC9o9o=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=xH0j2sE6tL9yAbPHaVckTm2OGFD5AoxgaC4U7uhA+eOiqaIAUEUyyKL4SEDgEdE0j0 eah7lc7WtfiOJVNhW76ZkHKuDZJJqUwazBUWIW8HTW/9jsEMIrnhhu+313zU8PqymkKR B1Mp8VcaPpHF4aDBxg0zTDxMm6/J3MvDMO+c4JDM0HInnBhRq3+URNiujGIicImwycT+ Ysz0gfEdCf3NKiJx8uS/OUGrHBVsxPOPHoTz/4UbCOnRKldvTap3OPyHC+M2CZucjC/y DXd9ZObXoMbb/tZcLnQUZV+JWXlDlVJf8e9M+zFXFWOSVmItNwtqDImdoKA2VvvEeTIw iK1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WjFIzqui; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id e16-20020aa79810000000b006b2b53de986si8850597pfl.194.2023.10.24.15.27.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WjFIzqui; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 42A7C8021DB4; Tue, 24 Oct 2023 15:26:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344527AbjJXW0k (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344520AbjJXWZ6 (ORCPT ); Tue, 24 Oct 2023 18:25:58 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97CDD2706 for ; Tue, 24 Oct 2023 15:25:22 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a92864859bso45299357b3.0 for ; Tue, 24 Oct 2023 15:25:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186321; x=1698791121; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=zKJ+MtJQ0m3usmVnmVc1PXHVWWmmWPISerzupGC9o9o=; b=WjFIzquiFxSHREY+b5v7HReFVePOCy0rewrxOZVoMVT9dUg5oz4RrLCg4C3GTAf/9M 1XI2OOtYV8YYuvB9g9K5RrxU60ROgFhyrKCOzhFsrFD5K3U3l4S9J/EXpyWZ4/fYBnIj Q0j6kcbPv6Yy1Q6ZoCifsFccsqmhpJmn7CnnZ0G364+ArXWLdw3AXhlyYTFsslDdXovM oKZ8IiC7fi/c+RJOBZAhiaA3J+BGVVQ0q8dyIo4a8TwxBw0ouqvzE+Ss5CN5L4s27uUL zQZV6QnK2Ik0ronIPg+QwKoG0JWFHy+lMJ7/2sfvFL9TnQB+kC/8ljFqpDyZKbJBdj4G qq2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186321; x=1698791121; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zKJ+MtJQ0m3usmVnmVc1PXHVWWmmWPISerzupGC9o9o=; b=uz9A/qIVYv+aSsSFrlChmlU08NRKjpuOedgZVo4trWnikabkc8nXZnIwgiR81hKC7J Eadc9enhJ/p5v/FBjv2wYkiJ5tL37O+ArdeuViOgo1ip7rvm7LF89p2+D8aUkecQcrL8 rU6CgOURD+YS91MgCeHRZTCIBV2+EIXukefgezanFoSHGcHrvzn6LSmoa9DjZf2I0a6m xe64HnQP9RCcXc0vuli2LOZDwP5sovPlnnx0RNXbDMwWygQny3sJDOfGxeSTP3+yj6ea HMma53IApBIJRwXCT5Ev9pItVDhYFKUsoW4CqjvRLjV/t21+1KIABCLn4qV09LR+WFyz nILw== X-Gm-Message-State: AOJu0YwKHvh3NWA19ZcXrNlCg8fCogEHZUMglS9ixk/r3+jiT1C/WlAU PovqDY5LJJJ4JQ/XZcHZPbEw98Io2fRa X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:d644:0:b0:5a7:f73d:e69 with SMTP id y65-20020a0dd644000000b005a7f73d0e69mr271131ywd.4.1698186320786; Tue, 24 Oct 2023 15:25:20 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:33 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-31-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 30/50] perf debug: Expose debug file From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:56 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677525815823944 X-GMAIL-MSGID: 1780677525815823944 Some dumping call backs need to be passed a FILE*. Expose debug file via an accessor API for a consistent way to do this. Catch the unlikely failure of it not being set. Switch two cases where stderr was being used instead of debug_file. Signed-off-by: Ian Rogers --- tools/perf/util/debug.c | 22 +++++++++++++++------- tools/perf/util/debug.h | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index 88378c4c5dd9..e282b4ceb4d2 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -38,12 +38,21 @@ bool dump_trace = false, quiet = false; int debug_ordered_events; static int redirect_to_stderr; int debug_data_convert; -static FILE *debug_file; +static FILE *_debug_file; bool debug_display_time; +FILE *debug_file(void) +{ + if (!_debug_file) { + pr_warning_once("debug_file not set"); + debug_set_file(stderr); + } + return _debug_file; +} + void debug_set_file(FILE *file) { - debug_file = file; + _debug_file = file; } void debug_set_display_time(bool set) @@ -78,8 +87,8 @@ int veprintf(int level, int var, const char *fmt, va_list args) if (use_browser >= 1 && !redirect_to_stderr) { ui_helpline__vshow(fmt, args); } else { - ret = fprintf_time(debug_file); - ret += vfprintf(debug_file, fmt, args); + ret = fprintf_time(debug_file()); + ret += vfprintf(debug_file(), fmt, args); } } @@ -107,9 +116,8 @@ static int veprintf_time(u64 t, const char *fmt, va_list args) nsecs -= secs * NSEC_PER_SEC; usecs = nsecs / NSEC_PER_USEC; - ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ", - secs, usecs); - ret += vfprintf(stderr, fmt, args); + ret = fprintf(debug_file(), "[%13" PRIu64 ".%06" PRIu64 "] ", secs, usecs); + ret += vfprintf(debug_file(), fmt, args); return ret; } diff --git a/tools/perf/util/debug.h b/tools/perf/util/debug.h index f99468a7f681..de8870980d44 100644 --- a/tools/perf/util/debug.h +++ b/tools/perf/util/debug.h @@ -77,6 +77,7 @@ int eprintf_time(int level, int var, u64 t, const char *fmt, ...) __printf(4, 5) int veprintf(int level, int var, const char *fmt, va_list args); int perf_debug_option(const char *str); +FILE *debug_file(void); void debug_set_file(FILE *file); void debug_set_display_time(bool set); void perf_debug_setup(void); From patchwork Tue Oct 24 22:23:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157737 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237863vqx; Tue, 24 Oct 2023 15:27:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXsAIAlNy5yNjL6hKYOoazOY82+ebY7TfbrjogiENoi2mfbh+Rdf+BPFJc7m8AZIur6Nz4 X-Received: by 2002:a17:902:f550:b0:1ca:7dcf:a74 with SMTP id h16-20020a170902f55000b001ca7dcf0a74mr11893870plf.22.1698186424972; Tue, 24 Oct 2023 15:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186424; cv=none; d=google.com; s=arc-20160816; b=AT3zkVZIPCLbKSwodar0YV7xatpGZRSueEd3ddiKg4CA9tUJrN+V+JoVAZGrMGrs+a 8fccE5ZeJTPckXrMlLhvHh8CIs+5vMd8ZM775j26j4jUaJtHMvDBMUhA7Imc6shn2u2Y FvMCgcMcYV/avBV9POgdJ2kGHuK9wbtNe4FkTuWzpl4vxeC9iT0Cf8DvnkQTaXSgNkt4 GfqeJdxwBxQ2GbKExzBGxPvuQNck4P8IJBOZUfLrup95/O/ip7KggB6zxTy52qRRyrKa UBYNJWJog4iuo+YdvrNOaJInjsi+taTwtdvMh9JTzjAxa8FssM4VhvovW91+SrzaZEFN FlQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=8rha7reTjPPM+nzq3LN9RJmw1LRSN6MIopdCvqO71wM=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=l33aCWULAsb93IohBzQf+2cZViJS1h01+6JRoOFFPmTJQYnBvHqvmlHaiwUcdOawXb invMg0EvbRrAUnQFaooJ29PyHyV7y036NWXhNfVGQAj1fvE4RB2BqDsj3EQ9fWIddiN3 oKOcTLaGTMkfaYq4ZK8SfsSmdIhMMZdkMZEpHEI4MFkztA1eO5GzigRnt40ekjq3BbYK BGpVin/gRM2C83P9pH8b/Q1AvCvWTQdVeC8+oVhNZ5ynwBIvqDBivHjRjNr/FguJ544r IEUPT/ZEpNZUzT4rml9cBpxIOSlOI1pcKETuTO2ziSZZXHXODrv2CpqUxGYz4bo9MBPO vQ7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ost0D5Om; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id g8-20020a1709029f8800b001bbc80a2a3asi8873827plq.299.2023.10.24.15.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ost0D5Om; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3C6BD80219C0; Tue, 24 Oct 2023 15:27:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344500AbjJXW0g (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344518AbjJXWZ6 (ORCPT ); Tue, 24 Oct 2023 18:25:58 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A7BF19BA for ; Tue, 24 Oct 2023 15:25:24 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a8ee6a1801so63563787b3.3 for ; Tue, 24 Oct 2023 15:25:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186323; x=1698791123; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=8rha7reTjPPM+nzq3LN9RJmw1LRSN6MIopdCvqO71wM=; b=Ost0D5OmJOXGh20juf/FD6edHdPvqW/aaozKCA/p7a9Xmt2BL/grhJR1BT1OlnmYkR O1WK7OAbpP165tAHj8f120CT5kA/i8q+nEZRWKoFCc04uD4HghMXSAlPjaQ0OGTPTGYQ oCpIbKcHxFMgp/7H4V/igcOpjmZRz+cQym5q2czRrAHJ/cKAe3ieEmILlVVnKuYVb5um Wq3w1dZ56F5U4hjKxlPqNG7zbr6NV3lWFZJ9dIJMteDATZCd8shzvxMIGBc7jZdWL5Xe Z56LLihOfT4C//k3ursYKY7vVXWApYSBDaeEnq1HEo14yGiHZiq0es1bH52DJizT70Tj Y5YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186323; x=1698791123; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8rha7reTjPPM+nzq3LN9RJmw1LRSN6MIopdCvqO71wM=; b=C/618W/tOuDnhw0oYTT7ELlu++gHuRn7WfaqMQqLgzhwiBvIjWZzmuk+wAI0FyGc7U YsY/ImBfjSOLU/XDloL4hhKu6c2/BRbTOpRdXXjRr0silAAAls7yV+aYjMn9Z/tWMUf8 EtBLDsE3mu7Liu3jBD99xD3DiF2AHgTHMqwnbR5RFy0kn51szfQIPkEbUjObbgd8k2Br 7uFIdBrRg4UabsFb/DTH5gJ7ylEkl6NzO20ssf72oCQ0f9PeQz8G6MjlrT3S5qjlZwY3 epPHpKXgWM99pT9GGltHFUNvp86J3hdO5pmGFEKlNdTiXEtVarq7Yk0BcQSFUe7jzrJO f4/w== X-Gm-Message-State: AOJu0YyjP/ZVmkoSH0qwp5MecYE0dn5vp76sDnTj/YsGHIeKfuIVjUHs 9ZeIX+UvXiz73oeOciLpMuXKJaHa/Oo3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:d05:0:b0:d9a:5b63:a682 with SMTP id 5-20020a250d05000000b00d9a5b63a682mr239523ybn.13.1698186322987; Tue, 24 Oct 2023 15:25:22 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:34 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-32-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 31/50] perf maps: Refactor maps__fixup_overlappings From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677528736948177 X-GMAIL-MSGID: 1780677528736948177 Rename to maps__fixup_overlap_and_insert as the given mapping is always inserted. Factor out first_ending_after as a utility function. Minor variable name changes. Switch to using debug_file() rather than passing a debug FILE*. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 62 ++++++++++++++++++++++++---------------- tools/perf/util/maps.h | 2 +- tools/perf/util/thread.c | 3 +- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index f13fd3a9686b..40df08dd9bf3 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -334,20 +334,16 @@ size_t maps__fprintf(struct maps *maps, FILE *fp) return args.printed; } -int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) +/* + * Find first map where end > new->start. + * Same as find_vma() in kernel. + */ +static struct rb_node *first_ending_after(struct maps *maps, const struct map *map) { struct rb_root *root; struct rb_node *next, *first; - int err = 0; - - down_write(maps__lock(maps)); root = maps__entries(maps); - - /* - * Find first map where end > map->start. - * Same as find_vma() in kernel. - */ next = root->rb_node; first = NULL; while (next) { @@ -361,8 +357,22 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) } else next = next->rb_right; } + return first; +} + +/* + * Adds new to maps, if new overlaps existing entries then the existing maps are + * adjusted or removed so that new fits without overlapping any entries. + */ +int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) +{ + + struct rb_node *next; + int err = 0; + + down_write(maps__lock(maps)); - next = first; + next = first_ending_after(maps, new); while (next && !err) { struct map_rb_node *pos = rb_entry(next, struct map_rb_node, rb_node); next = rb_next(&pos->rb_node); @@ -371,27 +381,27 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) * Stop if current map starts after map->end. * Maps are ordered by start: next will not overlap for sure. */ - if (map__start(pos->map) >= map__end(map)) + if (map__start(pos->map) >= map__end(new)) break; if (verbose >= 2) { if (use_browser) { pr_debug("overlapping maps in %s (disable tui for more info)\n", - map__dso(map)->name); + map__dso(new)->name); } else { - fputs("overlapping maps:\n", fp); - map__fprintf(map, fp); - map__fprintf(pos->map, fp); + pr_debug("overlapping maps:\n"); + map__fprintf(new, debug_file()); + map__fprintf(pos->map, debug_file()); } } - rb_erase_init(&pos->rb_node, root); + rb_erase_init(&pos->rb_node, maps__entries(maps)); /* * Now check if we need to create new maps for areas not * overlapped by the new map: */ - if (map__start(map) > map__start(pos->map)) { + if (map__start(new) > map__start(pos->map)) { struct map *before = map__clone(pos->map); if (before == NULL) { @@ -399,7 +409,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) goto put_map; } - map__set_end(before, map__start(map)); + map__set_end(before, map__start(new)); err = __maps__insert(maps, before); if (err) { map__put(before); @@ -407,11 +417,11 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) } if (verbose >= 2 && !use_browser) - map__fprintf(before, fp); + map__fprintf(before, debug_file()); map__put(before); } - if (map__end(map) < map__end(pos->map)) { + if (map__end(new) < map__end(pos->map)) { struct map *after = map__clone(pos->map); if (after == NULL) { @@ -419,23 +429,25 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) goto put_map; } - map__set_start(after, map__end(map)); - map__add_pgoff(after, map__end(map) - map__start(pos->map)); - assert(map__map_ip(pos->map, map__end(map)) == - map__map_ip(after, map__end(map))); + map__set_start(after, map__end(new)); + map__add_pgoff(after, map__end(new) - map__start(pos->map)); + assert(map__map_ip(pos->map, map__end(new)) == + map__map_ip(after, map__end(new))); err = __maps__insert(maps, after); if (err) { map__put(after); goto put_map; } if (verbose >= 2 && !use_browser) - map__fprintf(after, fp); + map__fprintf(after, debug_file()); map__put(after); } put_map: map__put(pos->map); free(pos); } + /* Add the map. */ + err = __maps__insert(maps, new); up_write(maps__lock(maps)); return err; } diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index b94ad5c8fea7..62e94d443c02 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -133,7 +133,7 @@ struct addr_map_symbol; int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams); -int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp); +int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new); struct map *maps__find_by_name(struct maps *maps, const char *name); diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 2b938ccbfe3e..cab818af6787 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -345,8 +345,7 @@ int thread__insert_map(struct thread *thread, struct map *map) if (ret) return ret; - maps__fixup_overlappings(thread__maps(thread), map, stderr); - return maps__insert(thread__maps(thread), map); + return maps__fixup_overlap_and_insert(thread__maps(thread), map); } struct thread__prepare_access_maps_cb_args { From patchwork Tue Oct 24 22:23:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238266vqx; Tue, 24 Oct 2023 15:28:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHE45LWWIPnPri2smecHnMV+O3SGVlemE29Wgxsbjd6RAS6Xvk4nW9Vp7J8MmDC4u7sKJmx X-Received: by 2002:a05:6a21:328e:b0:17e:4454:edc3 with SMTP id yt14-20020a056a21328e00b0017e4454edc3mr4846009pzb.47.1698186484611; Tue, 24 Oct 2023 15:28:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186484; cv=none; d=google.com; s=arc-20160816; b=eW4aqIS+RLCOdaV/9y5Mtngdx7NEJvnk3SWjsTJjvMG5WGX20pBve9bP78coUaOhJ+ 1FpB3P9PGmzHZu4wzq/CWbTXvS4ixIXouYRFtWdU6ebTAw/KM/Eb5nQGyzmM2iFHQxDl 1g0aSNfdo/BlhKmDl1s0/QSKCeOVm+z1bV1tyH4w0UAgh7Mjm6ZbCKTBqQFXGLusCycu Yf/kkrIAtyKETdkTo4RlAxMTosyz3Nx4G+KF+He83NBHHAqfVJx12AlzttpbIh/Pid/h I0vUE7WJWTPVhqpwemZT3JmtpcvzqpEN1e7CIKGN8l5bdKDxo1KiUkTa3Z81MIHsIP0m U8fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=IxgK5fVHKp+mV0fGm6A00soSn6WBUBJ/2ysR29mR0ns=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=IRrwf72Y//sJwpUIgsjglgHFvbfcfnScAUIn28pDas5XfxyA3h7UhjgMsKLVAqRdl1 H5NtqsovVM1hucRtkVEU+Rn4FKoCSebAd3OSETU+CQw07iZUiH/Tl+VODUOxfN1ozvlo hRtJg/kLDR5pIdsK5lTa0X6DoerE6prGsQzwUoT68Gtw9xnZzdbJEzBGvELjBPBcKERz P2gfscyv2+OLEGL0hXBYsQlcGXrqb0s4zvJomZrbnJbRUWausPq+Y0b3FXKirKlUByF7 nUVILJ0B5dkoIvP505D/3yTHLpS3saogvDEdhJysPCAouyTyLivyEcbeFQKzIuVeKvcj AoEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zBCpTlFl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id y16-20020a056a00181000b006901a8f5dadsi9327940pfa.289.2023.10.24.15.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zBCpTlFl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id E03E080219D7; Tue, 24 Oct 2023 15:27:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344466AbjJXW1Z (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344534AbjJXW1W (ORCPT ); Tue, 24 Oct 2023 18:27:22 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8260010C6 for ; Tue, 24 Oct 2023 15:25:27 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a88f9a1cf7so64618377b3.3 for ; Tue, 24 Oct 2023 15:25:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186325; x=1698791125; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=IxgK5fVHKp+mV0fGm6A00soSn6WBUBJ/2ysR29mR0ns=; b=zBCpTlFl9akgkYZ47XY/LVVGF6Q8NqFwRlue7vNIvrQ3LKbNv2boVxYD2zUyndF4Gj NFrd1QOZwSUDjKu+LDP51pHt4h33mqD06GvKUl6PO77a5PkbFxYtxwuw1dz+LAMWR6E/ XhDR/cRLWootOK8KkU2yuKl2hor9858+hWjX6eg2dpliu0J1YftRt/YW2JjXqLu6Kdb7 i7Its/RxFcnh7XWJMxniVMf17Kaf0FxL22fTNvWt9TjRvGs2Ia7aWO8cnn2arX4WDO91 KnRjsVYKK95MFNbgQa6kicYf9K8wIRi7P989G+R5HurEZ0aAWJNqnJrilU/menxwqNPY Snpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186325; x=1698791125; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IxgK5fVHKp+mV0fGm6A00soSn6WBUBJ/2ysR29mR0ns=; b=q0Oh4Zng/da5v/4zFj6OIvZUEfFYvSIPtKVLP1eD+KqkgiIOGKWFYJk950ulIIR+QY Fego3U48UrkhMBOkatk9TwRamZ6jpExie595AaD/aYMe91+5ly+C+M62Silzz11gOqgn lHyJ+WHl78S/l1XCJs0R+/wX6vNkcnOOKjDxkrMTp/hpFQuPaPyNEF/x9fVjxEHPZBjX mL5UTDPcdTqSejWAW5qLlfZzPlbkuyGzTwmugnBUeA/ue4RTcA85Lwpp7JkOENMHNpZ4 LClKeDJ5YCxR3EspdqSQYEt+Ty3J69TH9T/MwHwfl5sQO5h+mg3rmmSoNaiM8IKoE/ZO pBrw== X-Gm-Message-State: AOJu0Ywtq17Gm/NeOUqqnq5wTerXNBvsPXfRXDkcB9W0VvFATHKTICAw VXFGeG0uX88QKgkPutMj9NpQVQQwgBhX X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:d644:0:b0:5a7:f73d:e69 with SMTP id y65-20020a0dd644000000b005a7f73d0e69mr271133ywd.4.1698186325326; Tue, 24 Oct 2023 15:25:25 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:35 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-33-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 32/50] perf maps: Do simple merge if given map doesn't overlap From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:59 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677591327751586 X-GMAIL-MSGID: 1780677591327751586 Simplify merge in for the simple case of a non-overlapping map. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 40df08dd9bf3..14e1a169433d 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -696,9 +696,20 @@ void maps__fixup_end(struct maps *maps) int maps__merge_in(struct maps *kmaps, struct map *new_map) { struct map_rb_node *rb_node; + struct rb_node *first; + bool overlaps; LIST_HEAD(merged); int err = 0; + down_read(maps__lock(kmaps)); + first = first_ending_after(kmaps, new_map); + overlaps = first && + map__start(rb_entry(first, struct map_rb_node, rb_node)->map) < map__end(new_map); + up_read(maps__lock(kmaps)); + + if (!overlaps) + return maps__insert(kmaps, new_map); + maps__for_each_entry(kmaps, rb_node) { struct map *old_map = rb_node->map; From patchwork Tue Oct 24 22:23:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157747 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238456vqx; Tue, 24 Oct 2023 15:28:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFCeO5uLv9ed6S7PiCTur4OeGDktTd/Mi883Y08obJpY546BEw4HI9P+KV3vq63ha8DFNT X-Received: by 2002:a05:6358:1aa3:b0:168:e660:83ba with SMTP id gm35-20020a0563581aa300b00168e66083bamr5219970rwb.12.1698186512992; Tue, 24 Oct 2023 15:28:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186512; cv=none; d=google.com; s=arc-20160816; b=VoYGlbX2nP6sYWfjNnwiVT7K52E7IF1RrHX7Vr1oBTVsAU2pkQN78qMFKU1vWRtRHi V2Z0pTSG4Dm/nbO2wvtsj5cxOHu20qJyS6xVNRvkYhaivf+ElA2lwIs4c8rzXNDq6Vxc Ro30SBpv/+5b+Q/lg++P2hB6ClmOZrs5beJunBhdZMx0GTHYFF9Zq9LrYvCX/k2SH78X +GB/uVIYLxFEwlDTokxs1ESSO4iL79zY3zm7hAZBcafzvI9ucXGTFMUb7A9hCCbNpKLE 9wNakdpYFox32Bnh6MGFQyek8mjnLNd3TAfSKRgRWPg6K8iHH2xZCOKsVvXhOxdiCjIb lMUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=Ndf4120/UMb6KGHcRZrmqdMAZ7pHFTlKKfpIyLscZJ4=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=uhgNs790uW8dagySxhasOAqWcBx3qw1PV3Qc3hCTlB6oHhrEMYrgZK8phVeLckkTSs TXCj3344cXMwvzECDDDLebR6eXsr8dsfHRCySO3pHzTKYn6y24gvFmEbYvWMFglaPUzO Fymr6QHQzLdobLgr/cAPfGNMzjxS/DNoW6YlpWqR4cqcc/bB3Fp66fI97Br3uViPjUMd 3cDqOTrpH1CPKou0+xkJiiZnr+V+PkU31C9tLmyfVlvLxFR4+8iZq2eyZVrPOdJheimr Aa11o/7VKvsYubaipj40/+4KfWjq4iOsqSWdy3d57dxjvVcyiLTHV9OolpM2x7Celq06 WeAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CoJ0f3p4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id n1-20020a632701000000b0055c8fd5fe00si8842716pgn.886.2023.10.24.15.28.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CoJ0f3p4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A57458025426; Tue, 24 Oct 2023 15:28:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344623AbjJXW2R (ORCPT + 27 others); Tue, 24 Oct 2023 18:28:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344585AbjJXW1u (ORCPT ); Tue, 24 Oct 2023 18:27:50 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A83CB273D for ; Tue, 24 Oct 2023 15:25:29 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d99ec34829aso5777361276.1 for ; Tue, 24 Oct 2023 15:25:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186327; x=1698791127; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Ndf4120/UMb6KGHcRZrmqdMAZ7pHFTlKKfpIyLscZJ4=; b=CoJ0f3p4aWl/uTgfDXZxl8+eaIvudN6Nao8BPWMSORZARBSZFCwvZPAr/gXvByaAO/ JeCAHIIa+cg1qiUoM8rRBcbkDo2s+MoeBBnsKz1YNjcD+JdtgMMJh/6HZk/U5MsAY6K0 RwGh1L+kd7rnxXribEcJrRiea2p86EHxXmI33TuRVO63zbTWxFDmoPnrJGIKiONJ6xk2 qyYVQHM/8JwBECj+cBnYt2shdoRZnNYMpAtqshG16eei8z7Fpu9mQbUnhGQujFFKX3D8 UX7J7E6W/ZOJXzeNqfsV90KEoSYlTsRd40ttVBKiSbdhY/I34EdtOl7HIWlBc+fZW/94 2wNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186327; x=1698791127; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ndf4120/UMb6KGHcRZrmqdMAZ7pHFTlKKfpIyLscZJ4=; b=CP/XVjvqBoCE/wAdbxS71yXWTMr1yVlBYNb8IjX8Oi9a9sH7+DBdwbC9baeE7dZbLE bCWMLJXj9CsIa7GDhhp06vb5OEwXxeFrdbtmyY4XuSYN6n4DInwUOkm01vczK0GsnU+q bXyHA2NcnJH5e0P0IxsE/2wSczER8evhClBxeHLvCj1sbFHCo68YqChjZm+1Y4IrvoTR vj/AcR91xrQk6Fkjd6HnZiWalyKtrvv02/a3MeJOrm2kQoGj+ao+dTWP6gGctIR6qDPj qMSXDzVXU/sLPCXAJNiPs0OVuEP/t2gDRcn6vWfSKUHk2Nx+T5DTcishJ4BufFaVckEw wd3Q== X-Gm-Message-State: AOJu0YzRRmGwd4b+VyyZxNa8ivkIIrdpVvQ8DRFA8/a6YSe6GfPLUHlx o4zHCKcv9o80HvHuSKg2Tp/LbMN5xnGC X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:c3b8:4274 with SMTP id v3-20020a056902108300b00d9ac3b84274mr348613ybu.7.1698186327431; Tue, 24 Oct 2023 15:25:27 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:36 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-34-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 33/50] perf maps: Rename clone to copy from From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677621469085333 X-GMAIL-MSGID: 1780677621469085333 Rename maps__clone to maps__copy_from to be more intention revealing of its behavior. Pass the underlying maps rather than the thread. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 6 +----- tools/perf/util/maps.h | 3 +-- tools/perf/util/thread.c | 2 +- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 3c967295c9a3..191e492539e5 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -454,7 +454,7 @@ static struct thread *findnew_guest_code(struct machine *machine, * Guest code can be found in hypervisor process at the same address * so copy host maps. */ - err = maps__clone(thread, thread__maps(host_thread)); + err = maps__copy_from(thread__maps(thread), thread__maps(host_thread)); thread__put(host_thread); if (err) goto out_err; diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 14e1a169433d..85bea2a6dca9 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -452,12 +452,8 @@ int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) return err; } -/* - * XXX This should not really _copy_ te maps, but refcount them. - */ -int maps__clone(struct thread *thread, struct maps *parent) +int maps__copy_from(struct maps *maps, struct maps *parent) { - struct maps *maps = thread__maps(thread); int err; struct map_rb_node *rb_node; diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 62e94d443c02..e4a49d6ff5cf 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -14,7 +14,6 @@ struct ref_reloc_sym; struct machine; struct map; struct maps; -struct thread; struct map_rb_node { struct rb_node rb_node; @@ -61,7 +60,7 @@ struct kmap { struct maps *maps__new(struct machine *machine); bool maps__empty(struct maps *maps); -int maps__clone(struct thread *thread, struct maps *parent); +int maps__copy_from(struct maps *maps, struct maps *parent); struct maps *maps__get(struct maps *maps); void maps__put(struct maps *maps); diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index cab818af6787..07b158aa3e44 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -390,7 +390,7 @@ static int thread__clone_maps(struct thread *thread, struct thread *parent, bool return 0; } /* But this one is new process, copy maps. */ - return do_maps_clone ? maps__clone(thread, thread__maps(parent)) : 0; + return do_maps_clone ? maps__copy_from(thread__maps(thread), thread__maps(parent)) : 0; } int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone) From patchwork Tue Oct 24 22:23:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157739 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238134vqx; Tue, 24 Oct 2023 15:27:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHb8lEDWcYpkCRiNK5cxWvwqPJf0dVWulSprpnkbfzHb8F8Cy+QVwjKvmynlAsAWbowIRQN X-Received: by 2002:a05:6a00:27a0:b0:690:c306:151a with SMTP id bd32-20020a056a0027a000b00690c306151amr11784420pfb.0.1698186466114; Tue, 24 Oct 2023 15:27:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186466; cv=none; d=google.com; s=arc-20160816; b=VlzGb2NAuJPd7eB+E27Yas7bstbZUNjKHCEg0v7oOC0HxjpBbYFpVPQGSnn53FsSmU zokiVbCrkVUHCG8mFnafE4Gb3p9T2rbhWNfPA9Jk0p9bUjZ2GVF3weUwHvdy6jP2ye6G StLTm2AmbvaCn9jaU/izMMPw8XfubJzUyUSbu9uBIswNwUsBOELpU+mDaSMQzOFV+SzJ pk+rYigEQU1GpPLAvm+RDboCLBj0DoN4ImsM1Ytnud0jE36p5ez2BphLlSH6HLeX454X c6F8TZ6c8cZgSmcQSfeu+KyAzsc/AUa3ii3LgUIjPUPA7K0d4TBH86i0rYge+8l7YK59 3DSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=g/BBcChnvHkmkYd/yKkYLm0W4V4ebv0lxjgGfrAGFAQ=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=opuiOmuImKC0hDct5jlvPLFVZTbUaPT4TKq6SqditDFy+BlihsYMWHXXwoV/jj5dVa lr3YWl1QMoDTsBTa+S93a23c0YYwA4i7lDbE7IalwSAmy97sk7VNUOc1AsmcTujVNBPl wa/bOgoc3uWv4MSUs76FwxJPCOdnNluBB2r8C/mfyDi9/QjsSGRjGMp08PSKv3xqO/7Q FBXkNiju/xI5T+YALGL9tbC8JBUcW+wsA0WVWmyplkUM/r3yytgcQxZHDRfuILIodyMK OfL3J8z8jIPlGkX1+muGs/0B0o8UsDi+KPAv079VXf3wWZxpahXym6Ybl/e8JRs7qsM3 3bfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IikWft0+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id k185-20020a6384c2000000b0056ad01ddc36si8864169pgd.509.2023.10.24.15.27.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IikWft0+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id AA1E98025412; Tue, 24 Oct 2023 15:27:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344632AbjJXW1c (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344573AbjJXW1X (ORCPT ); Tue, 24 Oct 2023 18:27:23 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 791A42D53 for ; Tue, 24 Oct 2023 15:25:31 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7af53bde4so65534927b3.0 for ; Tue, 24 Oct 2023 15:25:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186329; x=1698791129; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=g/BBcChnvHkmkYd/yKkYLm0W4V4ebv0lxjgGfrAGFAQ=; b=IikWft0+Z2JZYapaYzZducsS8U/UZgqXninSEVB+zlH+i8i0EAC/IiJDMsOg/YwMgg 1vdiLwhQB8Yhct/2Aljp2ItEUU6WxI7oGstRkTzDu0PCYsvzZ/lTNDb6bXKKdoglzboL X5+Wwh7+RRHtobAaoZiTBg81555viJ7u6zG8StT0D05c4sO/ON7lvLvRbYQ/k+za7Khu dDSITUE9C9XWQy4dc9A/Iso3ALZ+17oHeiZbFLgdLzByROS+AFa4XNvHHzW/ZcNuGiO7 Of5VeO1ZiCeEo7UN4PM5PnpzWWDDlNUA0QdUtI1e9jovCqgY2Fxh1cn6PQuErjwH7OsW e10Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186329; x=1698791129; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=g/BBcChnvHkmkYd/yKkYLm0W4V4ebv0lxjgGfrAGFAQ=; b=fnVfqrizROxgGACRA9Wlf95tHqp/U7XIwXIVOL0LLeDrKLh5f9JYvawct5X9BQp3u1 gMlvkGdCTLTqOBgaODkV/IWJxuTqswCg9GP05giqkZJ6Q+FsxYh+YAIFFhQXWRBlwYuE tq88gHx09BzjRbmHqnPdXZJ/eAi8sYJosSpLwJmpWouFGAvR80jaQHZEAMat30FkwFhZ XJf3RW5g40pE9Zd5/gwJeAglK8a6Nt/oFe54/xWGJdNuW8BQgntVAWFEJF3sh9nbI6Of 8jN1JPZRYexziE3+Uipi21L07RwdxCr9SfEojNAcoAgOGwEI9D+rzYrC7cPnlqTvc87m RjTQ== X-Gm-Message-State: AOJu0Yyk6Q2TCSdzNuC2dr45A9fzXeh4tLC8jTBQEGLwDhDnL9a97F78 FntO4x3Vs4X9NZp+fWgI/KEJWktNqawo X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:d954:0:b0:595:5cf0:a9b0 with SMTP id b81-20020a0dd954000000b005955cf0a9b0mr293421ywe.9.1698186329522; Tue, 24 Oct 2023 15:25:29 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:37 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-35-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 34/50] perf maps: Add maps__load_first From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677571604005837 X-GMAIL-MSGID: 1780677571604005837 Avoid bpf_lock_contention_touching the internal maps data structure by adding a helper function. As access is done directly on the map in maps, hold the read lock to stop it being removed. Signed-off-by: Ian Rogers --- tools/perf/util/bpf_lock_contention.c | 2 +- tools/perf/util/maps.c | 13 +++++++++++++ tools/perf/util/maps.h | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index e105245eb905..d9720a910330 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -317,7 +317,7 @@ int lock_contention_read(struct lock_contention *con) } /* make sure it loads the kernel map */ - map__load(maps__first(machine->kmaps)->map); + maps__load_first(machine->kmaps); prev_key = NULL; while (!bpf_map_get_next_key(fd, prev_key, &key)) { diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 85bea2a6dca9..9a84d26328a7 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -792,3 +792,16 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) } return err; } + +void maps__load_first(struct maps *maps) +{ + struct map_rb_node *first; + + down_read(maps__lock(maps)); + + first = maps__first(maps); + if (first) + map__load(first->map); + + up_read(maps__lock(maps)); +} diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index e4a49d6ff5cf..b7ab3ec61b7c 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -142,4 +142,6 @@ void __maps__sort_by_name(struct maps *maps); void maps__fixup_end(struct maps *maps); +void maps__load_first(struct maps *maps); + #endif // __PERF_MAPS_H From patchwork Tue Oct 24 22:23:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157748 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238463vqx; Tue, 24 Oct 2023 15:28:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFjyd6sCb4DIhkrzQOJ/YJyQV1cE4c93/pDtgvnPtXLaQaCYNlYXoUj0AaSiCCO/l76enbN X-Received: by 2002:a17:903:11c7:b0:1bb:77a2:edda with SMTP id q7-20020a17090311c700b001bb77a2eddamr14576759plh.36.1698186514032; Tue, 24 Oct 2023 15:28:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186514; cv=none; d=google.com; s=arc-20160816; b=uoVVPbbsibIsCH8iP4Sm/70Xr6A2VhrYwN3VNXsN68GCvmLAW6E6ECRicGoxQ5wxWl hREwP2Yl3FP1K3GgxQsIwKRQ8TdY2UHndBafUnWUKSCJtGvcxmPeA3OOfjka9myo/j3e ws3wcnz79SDBgDDHUhVhTBdmPgkFOpRBnfLOXFHjv9l6Z+0DItwcznJnzjgNTCctDR7W zHDBtCOmAEK5b0VuhybNiu0j1A5mX1D0fvZDyGBDhO3NYMGxyLloufYla6O7yjm19yOw KPoOAc9/gKjRNTB6BVNVhhtW/0q0mlmf8c6X1vYGscEb4buCQKBlTLSxleDkkroXvd8e SIMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=MiRI18wPFPsoj3esFQh9IhgomcBelp5JaqZwyCYVfWY=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=gwYIyFNy3QgjnNcNEV04Mx2UjELGk+3iYRUFB80Dieuu8nMvxyKXFA+H/4c/OtQ8KD rsrMibIYL4tXcu1kpWrM8xtPiiOSsKsi1aSpdDGYD0h/v85ReGTv1fqkSDHfupiZ9XcD R5ZxHuCIxl0428r2R+CfXLiHzB4wp/gMRPGz9ps7EeaHgFyEwgInPMWk9qnjxf911oiq tD32G98GiHdTURbp+b2ica/JFGO3Va4afo8wKjvJ3EMPvTnj3hcSPSZg4K7zEpBi+xBu VRg7TUyrbcsoauEWBzYGK1Rqq4R3g20OTi7yktfr03rckvzP7jTMNsmzhMIzmcs7eMlV PV+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zSTUS5jd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id h12-20020a170902eecc00b001ca7308e442si8519317plb.639.2023.10.24.15.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zSTUS5jd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id EEA2A8021D00; Tue, 24 Oct 2023 15:28:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344466AbjJXW2X (ORCPT + 27 others); Tue, 24 Oct 2023 18:28:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344644AbjJXW17 (ORCPT ); Tue, 24 Oct 2023 18:27:59 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A517A1700 for ; Tue, 24 Oct 2023 15:25:33 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9cb3a59a19so2859931276.2 for ; Tue, 24 Oct 2023 15:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186332; x=1698791132; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=MiRI18wPFPsoj3esFQh9IhgomcBelp5JaqZwyCYVfWY=; b=zSTUS5jdV7rKsOD7wncjVXcyqL1sOPHzXG2/a9kSRAwf3/XvbAHBV5zrQ8WsLWkVCk efbjbuAfjgH0FNr5pR251us6BjWcJHX8KZHGzNjnXM9N8F5NSklkreiQEKXnOUyNxP3x eq6QeDZ289YikNgPMqhf3xSLXd0fCmYu/+t9g8n1Zat1fJM0f52DAuEHuKdkgNTvtkSS n7kaGgZXC+gNxiwEcvEXbEs7GtKxegcL9JXCfVHjB2KInCWV3sqH+s+0qymhutcxAooN jVlDCUZXEcHqVf5uA817QVZWRlYKJPmgK1+MwEAUnj8P3EXu8KkLT/5C+lOqK37g/oEm 6gfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186332; x=1698791132; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MiRI18wPFPsoj3esFQh9IhgomcBelp5JaqZwyCYVfWY=; b=p6KQRxU9mgzaXnPvzvwXTuqRN1sZ1D55s6TIUUHuMMvov2jYvqiUQ7oYM3y3RkWlXy bTa0pQpds1up2Bm6o49vr3W8qEWglgnP73T26HVK33BI2USAp1nBvsAEBdFjkvWr9cWR X/MtAe4LJ0hdWZwZPhDyTjQUa0h42aDKAe6qF6GQyavnFpgLykC+tqhLD4AO2kE3Ha2Y t0IRxhgY0Xi0xkwB6+CntVZAiTFACNxla5B8BHdGsTjiRZTmsOpR5HXOLkXyHE/vPTrk NvrtcIz3dDCbrwFEofRKcVRG+MGaGTZxka5uSsBQ95YJWfRQcvmjrvdvRwW40I7BzcA3 75aw== X-Gm-Message-State: AOJu0YydrdDxFBz4/iOekgFfmkhKIOeVmAPXcgNzIPaEbyrgfg6NBqQs pHoI50P2eUOPu47Hvk9O1Hxx/477GyQw X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1743:b0:d9a:50d2:a8be with SMTP id bz3-20020a056902174300b00d9a50d2a8bemr262421ybb.1.1698186331901; Tue, 24 Oct 2023 15:25:31 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:38 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-36-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 35/50] perf maps: Add find next entry to give entry after the given map From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677622531001231 X-GMAIL-MSGID: 1780677622531001231 Use to remove map_rb_node use from machine.c. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 7 +++---- tools/perf/util/maps.c | 11 +++++++++++ tools/perf/util/maps.h | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 191e492539e5..ab345604f274 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1759,12 +1759,11 @@ int machine__create_kernel_maps(struct machine *machine) if (end == ~0ULL) { /* update end address of the kernel map using adjacent module address */ - struct map_rb_node *rb_node = maps__find_node(machine__kernel_maps(machine), - machine__kernel_map(machine)); - struct map_rb_node *next = map_rb_node__next(rb_node); + struct map *next = maps__find_next_entry(machine__kernel_maps(machine), + machine__kernel_map(machine)); if (next) - machine__set_kernel_mmap(machine, start, map__start(next->map)); + machine__set_kernel_mmap(machine, start, map__start(next)); } out_put: diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 9a84d26328a7..38d56709bd5e 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -662,6 +662,17 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) return map; } +struct map *maps__find_next_entry(struct maps *maps, struct map *map) +{ + struct map_rb_node *rb_node = maps__find_node(maps, map); + struct map_rb_node *next = map_rb_node__next(rb_node); + + if (next) + return next->map; + + return NULL; +} + void maps__fixup_end(struct maps *maps) { struct map_rb_node *prev = NULL, *curr; diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index b7ab3ec61b7c..84b42c8456e8 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -136,6 +136,8 @@ int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new); struct map *maps__find_by_name(struct maps *maps, const char *name); +struct map *maps__find_next_entry(struct maps *maps, struct map *map); + int maps__merge_in(struct maps *kmaps, struct map *new_map); void __maps__sort_by_name(struct maps *maps); From patchwork Tue Oct 24 22:23:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157736 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2237862vqx; Tue, 24 Oct 2023 15:27:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFEsSS+LkROBeEMwy8R3bMBlTnrILu9MVcgLggzNx0OYumY+aaia9I+ersO+8or6xuF9yK0 X-Received: by 2002:a05:6a20:e128:b0:14c:c511:387d with SMTP id kr40-20020a056a20e12800b0014cc511387dmr4993568pzb.9.1698186424554; Tue, 24 Oct 2023 15:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186424; cv=none; d=google.com; s=arc-20160816; b=gt9+BjpYaZZE7Yo12Y394HCLVNcjqnA0f/biMnPadfke/pCzunkXOu60RGPW0p6h6f no7U0Gu66VD98Y5GrD1svj1UDJDx7oXmF2meo+d4lb0TRpyHC8JWFuXAMIQz4up9vmD8 hFePj1xlIJxXmJk2DQ5joAWVXpnluOgKbk5sJA9uf89EEFVEB4eI4Y0vpBTVFmVnmFTz tkWd877VSkx+kQ+lK1vnXyXRg4Eo7WIijB3wV+U95g81/YYVKYRkWJoF9ceWX78edMiS DSnR+MAeCsB76YAFFsZQMmjwCNSoWCuK8xP5WFX+1e0B/1LMQi8nLDcmoZvXLCV+s5Qa 1uuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=nzIlX+P2Xnb51CqannJImstzqGSg/CO4GFxV9EhldOc=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=aBzFi+4V2s2rZOxldwkKTp9zCjf7zhcNwDKhb6PaVBsbwpQ5xm7i5fJYmyNgKFA3lv A01ES9100ROQZpHq8ysf3QB6nT1rHqixKlRusC2j3Q8SmV9OL4cF2bLCQKrMFK5wMCPO SL/75FqqREGw60KlTdIw2SDj9RkgGGE3bavPpM1X9ZZDa8l5tmN5S1SuwlYhMQ3exxDK 6O4tFY3Z4lM7OxzG4GEIGHl8OxHOzEdE5fsTeo+oyY1OWDFiOXNEBinodaQAuOWiL17V tRMumrel+bOoRnFYY1m+weDFA+3w5JXedzk5Smt3OrVvn/c39CSnhxXoxjqd/USomd9e j6UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XtlLXByk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id c2-20020a056a00248200b006be2d9914eesi9306708pfv.118.2023.10.24.15.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XtlLXByk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 6523880219D4; Tue, 24 Oct 2023 15:27:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344513AbjJXW0t (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344585AbjJXW0L (ORCPT ); Tue, 24 Oct 2023 18:26:11 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F7002D76 for ; Tue, 24 Oct 2023 15:25:35 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7cf717bacso67706637b3.1 for ; Tue, 24 Oct 2023 15:25:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186334; x=1698791134; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=nzIlX+P2Xnb51CqannJImstzqGSg/CO4GFxV9EhldOc=; b=XtlLXBykJVKWfk5yCU+rQn2Ll3gCSIsu1hHyuLDQqWdexiEcx0cDqPhM0Z1y+cxDP8 FCTxk5T2UFFxH52Wv8sLC5OtK45tol1MiXEuh4hecOSn95JZ1b/wpyGgr2hSBg+6t6/a e1Rt7+mLp+K7PMssJ8Aczt3mAMc6qWvofhfNCpjdXmb6WxHdsd6nhaTDZ+vgmB6dSMyn v3sLqdx7GtzSUtYarXbyZnXzkOBcdM8la3spVgZqMSNMd8U5mPz9F6iM40I33lw6c/AR X9nYb0oNOr+D5GIxoYY4NXtY/y96qK75aXcLWf/rArqs7r8cNUAYbzmJIl8c82kNCYyv LlDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186334; x=1698791134; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nzIlX+P2Xnb51CqannJImstzqGSg/CO4GFxV9EhldOc=; b=ZK07sLGO7o95JJxVoIIeZhIjV9/x6TIah4EofUTnBKfzfcF2Shg7QsqZlYj5Zjo0gO zaAdkL49vPKfY6mu1r+1aiLyaIXk/DfjZQmvqilpAlJfLyETXGPLXreA2dAmHaW7j2sB VnSmRGOuIZP8Sl0jojxPmjJAsWT3BKITpcspHW5AoDK9wG+3u6OnZI0NeV6bP91IUY00 1PyUdcXP4a+S0KvVcR2i/5b7RuWYAGu/GyFUmhlAhTFpeB68f9i6XOi/vUNtA6/+mkM8 VdADaZ8YbXNjjcKjC8Tgs0xyGYmYFG7i1a1WRZO+pdBQCozj91wYzYcCbu7gCfckKYKI xM5g== X-Gm-Message-State: AOJu0YztGFwYLbW8Mo9J582A+pC68rsHnzxra1N+LxwGE/HxLxxV7Pds mIJ5dG5qh40S9GPakfhkrnVBvjeonOFL X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:ca8b:0:b0:589:a855:7af with SMTP id m133-20020a0dca8b000000b00589a85507afmr268615ywd.7.1698186333865; Tue, 24 Oct 2023 15:25:33 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:39 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-37-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 36/50] perf maps: Reduce scope of map_rb_node and maps internals From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:00 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677528127586133 X-GMAIL-MSGID: 1780677528127586133 Avoid exposing the implementation of maps so that the internals can be refactored. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 90 ++++++++++++++++++++++++++---------------- tools/perf/util/maps.h | 23 ----------- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 38d56709bd5e..01c15d0b300a 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -10,6 +10,11 @@ #include "ui/ui.h" #include "unwind.h" +struct map_rb_node { + struct rb_node rb_node; + struct map *map; +}; + #define maps__for_each_entry(maps, map) \ for (map = maps__first(maps); map; map = map_rb_node__next(map)) @@ -17,6 +22,56 @@ for (map = maps__first(maps), next = map_rb_node__next(map); map; \ map = next, next = map_rb_node__next(map)) +static struct rb_root *maps__entries(struct maps *maps) +{ + return &RC_CHK_ACCESS(maps)->entries; +} + +static struct rw_semaphore *maps__lock(struct maps *maps) +{ + return &RC_CHK_ACCESS(maps)->lock; +} + +static struct map **maps__maps_by_name(struct maps *maps) +{ + return RC_CHK_ACCESS(maps)->maps_by_name; +} + +static struct map_rb_node *maps__first(struct maps *maps) +{ + struct rb_node *first = rb_first(maps__entries(maps)); + + if (first) + return rb_entry(first, struct map_rb_node, rb_node); + return NULL; +} + +static struct map_rb_node *map_rb_node__next(struct map_rb_node *node) +{ + struct rb_node *next; + + if (!node) + return NULL; + + next = rb_next(&node->rb_node); + + if (!next) + return NULL; + + return rb_entry(next, struct map_rb_node, rb_node); +} + +static struct map_rb_node *maps__find_node(struct maps *maps, struct map *map) +{ + struct map_rb_node *rb_node; + + maps__for_each_entry(maps, rb_node) { + if (rb_node->RC_CHK_ACCESS(map) == RC_CHK_ACCESS(map)) + return rb_node; + } + return NULL; +} + static void maps__init(struct maps *maps, struct machine *machine) { refcount_set(maps__refcnt(maps), 1); @@ -484,17 +539,6 @@ int maps__copy_from(struct maps *maps, struct maps *parent) return err; } -struct map_rb_node *maps__find_node(struct maps *maps, struct map *map) -{ - struct map_rb_node *rb_node; - - maps__for_each_entry(maps, rb_node) { - if (rb_node->RC_CHK_ACCESS(map) == RC_CHK_ACCESS(map)) - return rb_node; - } - return NULL; -} - struct map *maps__find(struct maps *maps, u64 ip) { struct rb_node *p; @@ -520,30 +564,6 @@ struct map *maps__find(struct maps *maps, u64 ip) return m ? m->map : NULL; } -struct map_rb_node *maps__first(struct maps *maps) -{ - struct rb_node *first = rb_first(maps__entries(maps)); - - if (first) - return rb_entry(first, struct map_rb_node, rb_node); - return NULL; -} - -struct map_rb_node *map_rb_node__next(struct map_rb_node *node) -{ - struct rb_node *next; - - if (!node) - return NULL; - - next = rb_next(&node->rb_node); - - if (!next) - return NULL; - - return rb_entry(next, struct map_rb_node, rb_node); -} - static int map__strcmp(const void *a, const void *b) { const struct map *map_a = *(const struct map **)a; diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 84b42c8456e8..d836d04c9402 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -15,11 +15,6 @@ struct machine; struct map; struct maps; -struct map_rb_node { - struct rb_node rb_node; - struct map *map; -}; - struct map_list_node { struct list_head node; struct map *map; @@ -30,9 +25,6 @@ static inline struct map_list_node *map_list_node__new(void) return malloc(sizeof(struct map_list_node)); } -struct map_rb_node *maps__first(struct maps *maps); -struct map_rb_node *map_rb_node__next(struct map_rb_node *node); -struct map_rb_node *maps__find_node(struct maps *maps, struct map *map); struct map *maps__find(struct maps *maps, u64 addr); DECLARE_RC_STRUCT(maps) { @@ -78,26 +70,11 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data) /* Iterate over map removing an entry if cb returns true. */ void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data); -static inline struct rb_root *maps__entries(struct maps *maps) -{ - return &RC_CHK_ACCESS(maps)->entries; -} - static inline struct machine *maps__machine(struct maps *maps) { return RC_CHK_ACCESS(maps)->machine; } -static inline struct rw_semaphore *maps__lock(struct maps *maps) -{ - return &RC_CHK_ACCESS(maps)->lock; -} - -static inline struct map **maps__maps_by_name(struct maps *maps) -{ - return RC_CHK_ACCESS(maps)->maps_by_name; -} - static inline unsigned int maps__nr_maps(const struct maps *maps) { return RC_CHK_ACCESS(maps)->nr_maps; From patchwork Tue Oct 24 22:23:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157765 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2242250vqx; Tue, 24 Oct 2023 15:38:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG5dk1okWME/w/1QqiO6mabFnCZrp7fJSQ1AO/DTQ4tYCESpNfET+hVZoMOQ+Ajt/O23/31 X-Received: by 2002:a05:6358:fac3:b0:168:d3b3:7173 with SMTP id ts3-20020a056358fac300b00168d3b37173mr10086381rwb.25.1698187085190; Tue, 24 Oct 2023 15:38:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187085; cv=none; d=google.com; s=arc-20160816; b=DxyPk8lzAGVW8miVqHEbN32L6ApWyGEVgHmRAXvuqUrJvoZWevxhXJc7rzdm3HkCPT 6aFonGSP//wZc8c1b17kE4g/aALDHRVpbqaOSb1NCo8Hk5Jpc7FdbAfw0hWtjYDrerKA NdFQeTddiyAwPBlJv4875pdPqqO+dfYx9KZlRu/LOZC/6BY3q35Gm05/cGwTfQhhf4E5 HU1nWkw90fFRcHBJfCoQfpm5zUXCmzGk5wgtCZU101kRW4wvb3pJ6+VNPzuRiCFUuhKF F1Oul2GNKoaLzr2rZhzrYiPH1eCsfEKf6CTHOJoyfgwgTQzYEz4TpL1sAD0jz0ZbY83V pP6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=zZGWaGIyb2MLTR6FKf9sQ21nNy6Say3HBAADxZHBZRA=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=apWTJqMpFZUl3X37V7NxWO3Aci6/Vr9UEQx7iXxMsDCQv5RPtcHUDuBBeUeUce1b8P ZljwZKoGTeZHyg2LnDvRtXhY/cUg5tu/VYQ9BxP3yF9C8yH0jDUg5ef695afjZhOb0SQ Iqe1P+0Ea6BZodYJ6DEPTXwFyaQFvfSKphajWAJ3vAYLFfPboIPZnMPdftAwnJamxn04 3ZS2nDxdpY/1u9PLXVfODFVll2XVER1Hnf9hcf7PbJ5BKZgfD9kDtwyn1iHMtlB+sMLT 51m79qHN7R+taBlTksaE01oPDglYpFNZxQ5XltMqi7L7YRKoYAbOKOoP5R+/Y0NI0Ofg a7Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pRRwvyFw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id y7-20020a636407000000b005859b2d8d7asi9020951pgb.4.2023.10.24.15.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:38:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pRRwvyFw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 25C548021236; Tue, 24 Oct 2023 15:38:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344679AbjJXWhB (ORCPT + 27 others); Tue, 24 Oct 2023 18:37:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344608AbjJXWgk (ORCPT ); Tue, 24 Oct 2023 18:36:40 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAA7730C7 for ; Tue, 24 Oct 2023 15:25:37 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59f61a639b9so68801927b3.1 for ; Tue, 24 Oct 2023 15:25:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186336; x=1698791136; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=zZGWaGIyb2MLTR6FKf9sQ21nNy6Say3HBAADxZHBZRA=; b=pRRwvyFwnHwcygLAejayEt2NNSybuNbRUx0dieNKbFFQqJu7pI8tjFYOrawnhYicxv 1k0s8yf+6VTLk4WCYSzaql5nS7MVqnV5XCK/g8ICGM/5ONMACRtQHl5LAKKoiJSkVzpN ZsZSKpI+3/nviSFk61QM/s2HdbcWWOt3eneMrl1frMOC92ZOB0zCHQxiH+B+NMIeWo6/ OFeWUFRa8I2PAcYmzsGZW5u14sz2uGvKKMCPjejuiVyNyeB+62N037HQ9wkSImkPutMQ n/7rf3FqhGekKlkSqe3kylPdv2yUruR3m8WaAT2A3bPBlQtfL+ujG18gdSUrRdMs0DqJ +uLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186336; x=1698791136; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zZGWaGIyb2MLTR6FKf9sQ21nNy6Say3HBAADxZHBZRA=; b=iF0NhXb42ayiOXxm3QBv9qYib3DX5PhiNTE0UFhLCAmEEgKUvUbnKo6SQHmbj5m9HK Vpq2g01QNoMjufQTTyYOSDFKxLdll33NJXOJFQxK82SJbxbj0lYbU8+a3+Im3lt1TRGJ ggzucvkpjLzPSQYqvvcnF+KbBe06fwKbut4tfrNeZ+a24QcCStmZ6vSeQzLSuCDQEJ+f wnyjUpf6YdsXhrIV823jto0hHmE8I6fUloTa/mZXmCfP7ZfDXaos2k4+8o6fIDc1c6/N pu+oha/tNSm8HUQ7VeLwPKX8l3M7pk1jr6VG7qLdi/HFXAhEDNL8rU+gI6PE96ry4f6V 0w3Q== X-Gm-Message-State: AOJu0YxZodC4Q2Ridl61+nkslqaHXJCoRbS1+1xCpPc/jKGd5Kuo9gwc pjpqwJIAkRjKhJ3LjmhR/SjBw2LmqXN7 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:e8c6:0:b0:5a7:b54e:bfc1 with SMTP id r189-20020a0de8c6000000b005a7b54ebfc1mr337662ywe.10.1698186335876; Tue, 24 Oct 2023 15:25:35 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:40 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-38-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 37/50] perf maps: Fix up overlaps during fixup_end From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:38:03 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678220683003446 X-GMAIL-MSGID: 1780678220683003446 Maps are sometimes made overlapping, in particular kernel maps. If the end of a map overlaps the start of the next, shorten the overlapping map. This should remove potential non-determinism in maps__find, ie finding maps by address. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 01c15d0b300a..fba95a00ecdf 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -700,7 +700,7 @@ void maps__fixup_end(struct maps *maps) down_write(maps__lock(maps)); maps__for_each_entry(maps, curr) { - if (prev != NULL && !map__end(prev->map)) + if (prev && (!map__end(prev->map) || map__end(prev->map) > map__start(curr->map))) map__set_end(prev->map, map__start(curr->map)); prev = curr; From patchwork Tue Oct 24 22:23:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157761 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2242018vqx; Tue, 24 Oct 2023 15:37:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFsHe1NM7WyyzXh0q8fm3NR08sFefTvcRqNwIQQmwLhAK/FsLuWFcPyZKyHA3Va43wOw/tg X-Received: by 2002:a05:6870:a105:b0:1ea:d8bb:411a with SMTP id m5-20020a056870a10500b001ead8bb411amr16345768oae.6.1698187055452; Tue, 24 Oct 2023 15:37:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187055; cv=none; d=google.com; s=arc-20160816; b=IX1/7aCbM6Xc+tnpljTWzUuqeaKy3r1Qp1tx8dgj1IeuvlOZFuvJg77z2QdK+bPhE6 6ak3e5fl0/X3jmw2P72mbT1C4D0sv2LZ9gERh6EAOl9DUmrEpMn4l5wIHhTNnARfNmnq N1v7pyAhf2NK3AM2TnfecCFKsblFc0UX/FLAvJ7lDH9Sw29urvhjxt6sCQxhvso7ru2/ +E8Tnbp6eM/r40TIf4ZcvyGwxKbL6QJWuv6Tn/gshHHpPrpivuTIIVbvJ6z+HyCYvLI3 44XIx65S8+nfcBg3c1Zxoo6JjoLT+x8BMG3Ntk47oDrjR/mMvCnljHhCmmIsTA6PNxDw PmtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=ry9FibHp4SMjVK/4XATvJ2DfFWHqLnxdLvRYM3t/z84=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=eW6Ax/KxY/qjQDuFpoSBrZ/VSXjRdSRepdFAxL1eRq/iB9Je8xXKoc61N+CSJIyU8H gdtRmpwQno8H9H7SChdlsscTyxbxuoRUd7rLdPo3x338Cb2aNPkL8+mwVeVEJG246Awc CIrOU88SrnclvkQf4SNByHXi10o4Ou+CunyPwG40sYp4zoMrhtyq1fqFNLfziJQVSE8w zdgoz2NVpBenCK562AjdEGh+RoR7xvQ7tI7KJBzRXaunSDkHOi2aAC2IIEW9BtGj/BlM cnZbss6iflWIpiwcc1RaoYSXWaqh/kj8CtbJGOC3rFx10wfD4Gpjuc9XTgpp843loRjo twtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZIqQ0JAJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id i135-20020a636d8d000000b0057759a5b7c6si9229578pgc.62.2023.10.24.15.37.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:37:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZIqQ0JAJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 99C7980267E1; Tue, 24 Oct 2023 15:37:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344689AbjJXWhZ (ORCPT + 27 others); Tue, 24 Oct 2023 18:37:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344686AbjJXWhC (ORCPT ); Tue, 24 Oct 2023 18:37:02 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6E5530DC for ; Tue, 24 Oct 2023 15:25:39 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7e4745acdso94128837b3.3 for ; Tue, 24 Oct 2023 15:25:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186338; x=1698791138; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ry9FibHp4SMjVK/4XATvJ2DfFWHqLnxdLvRYM3t/z84=; b=ZIqQ0JAJFykMXQ5eimVsbf1gpF5Vi7Zudy3Y9c4EmhywNE3f/lrsa8zhqZL4BJMhNl 1B4EaTsH/1AakDtSkqgHpbEUL9vWu9rIUXSSjgucutpbrJoELAr/9MR5lWgP9zE0bGde PhIGUm4h7uL9QQBDfSZSMf4PJBl6doWRYBoneJbT+EFunWoULPDh6qFWMOcbT/Kee+Dz TczERvbl0LsIGHSXDs8VamKrtsnlaJBB0XA4px2f86a3Kry0E2eeWGau10GkPksJVLio xUclhaggvkce43hyy1PsuC4t97IODldh1ITQGOg+svYssK2mPOqdtPEu2GLzxoezeE6d /hLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186338; x=1698791138; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ry9FibHp4SMjVK/4XATvJ2DfFWHqLnxdLvRYM3t/z84=; b=lwAp7eGmB2PUQzgx5uY/kZbIbsRrbA7f2aBj8EMaqdjpo+F+DZfZ5lR7cpqpfO3LCS aMiCTuJlRrMD/q0XLsbsUipDzSjR3CXUv4SAHI0C6Wuyt+/Va5BdSPt/3MHSHcJB218J MDM+kFv+PFIEQ7C/ofJLE2ugsnNOLXxIyzr5NCfhrmF1lWNMWdGHK7NNAbmS/3nJJAfE mEh/g+VPxAPCklSmcDira+3ecllMfxl3luERUfXrXF4xrch9cd14bOhxc68vM3EwzUFW 4Dt6m5t2Boqilm3GehLm1UogrxCJs9Mw4Yo6oveWaLrwUiR2VoWx3vjba3V1nt+rrdCl TIxg== X-Gm-Message-State: AOJu0YzksOmgiXptZp3cltCETE/UxAWIsX8BpJ50bJqHC2orlEbpti3L VpD3Rq6GlvDVLCUrCdMQUcV2sgTSGmp+ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:4aa:b0:d9a:4421:6ec5 with SMTP id r10-20020a05690204aa00b00d9a44216ec5mr251544ybs.3.1698186338281; Tue, 24 Oct 2023 15:25:38 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:41 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-39-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 38/50] perf maps: Switch from rbtree to lazily sorted array for addresses From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:37:34 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678189804234481 X-GMAIL-MSGID: 1780678189804234481 Maps is a collection of maps primarily sorted by the starting address of the map. Prior to this change the maps were held in an rbtree requiring 4 pointers per node. Prior to reference count checking, the rbnode was embedded in the map so 3 pointers per node were necessary. This change switches the rbtree to an array lazily sorted by address, much as the array sorting nodes by name. 1 pointer is needed per node, but to avoid excessive resizing the backing array may be twice the number of used elements. Meaning the memory overhead is roughly half that of the rbtree. For a perf record with "--no-bpf-event -g -a" of true, the memory overhead of perf inject is reduce fom 3.3MB to 3MB, so 10% or 300KB is saved. Map inserts always happen at the end of the array. The code tracks whether the insertion violates the sorting property. O(log n) rb-tree complexity is switched to O(1). Remove slides the array, so O(log n) rb-tree complexity is degraded to O(n). A find may need to sort the array using qsort which is O(n*log n), but in general the maps should be sorted and so average performance should be O(log n) as with the rbtree. An rbtree node consumes a cache line, but with the array 4 nodes fit on a cache line. Iteration is simplified to scanning an array rather than pointer chasing. Overall it is expected the performance after the change should be comparable to before, but with half of the memory consumed. To avoid a list and repeated logic around splitting maps, maps__merge_in is rewritten in terms of maps__fixup_overlap_and_insert. maps_merge_in splits the given mapping inserting remaining gaps. maps__fixup_overlap_and_insert splits the existing mappings, then adds the incoming mapping. By adding the new mapping first, then re-inserting the existing mappings the splitting behavior matches. Signed-off-by: Ian Rogers --- tools/perf/tests/maps.c | 3 + tools/perf/util/map.c | 1 + tools/perf/util/maps.c | 1183 +++++++++++++++++++++++---------------- tools/perf/util/maps.h | 54 +- 4 files changed, 757 insertions(+), 484 deletions(-) diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index bb3fbfe5a73e..b15417a0d617 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -156,6 +156,9 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest TEST_ASSERT_VAL("merge check failed", !ret); maps__zput(maps); + map__zput(map_kcore1); + map__zput(map_kcore2); + map__zput(map_kcore3); return TEST_OK; } diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 54c67cb7ecef..cf5a15db3a1f 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -168,6 +168,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len, if (dso == NULL) goto out_delete; + assert(!dso->kernel); map__init(result, start, start + len, pgoff, dso); if (anon || no_dso) { diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index fba95a00ecdf..06fdd8a7c2a2 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -10,286 +10,477 @@ #include "ui/ui.h" #include "unwind.h" -struct map_rb_node { - struct rb_node rb_node; - struct map *map; -}; - -#define maps__for_each_entry(maps, map) \ - for (map = maps__first(maps); map; map = map_rb_node__next(map)) +static void check_invariants(const struct maps *maps __maybe_unused) +{ +#ifndef NDEBUG + assert(RC_CHK_ACCESS(maps)->nr_maps <= RC_CHK_ACCESS(maps)->nr_maps_allocated); + for (unsigned int i = 0; i < RC_CHK_ACCESS(maps)->nr_maps; i++) { + struct map *map = RC_CHK_ACCESS(maps)->maps_by_address[i]; + + /* Check map is well-formed. */ + assert(map__end(map) == 0 || map__start(map) <= map__end(map)); + /* Expect at least 1 reference count. */ + assert(refcount_read(map__refcnt(map)) > 0); + + if (map__dso(map) && map__dso(map)->kernel) + assert(RC_CHK_EQUAL(map__kmap(map)->kmaps, maps)); + + if (i > 0) { + struct map *prev = RC_CHK_ACCESS(maps)->maps_by_address[i - 1]; + + /* If addresses are sorted... */ + if (RC_CHK_ACCESS(maps)->maps_by_address_sorted) { + /* Maps should be in start address order. */ + assert(map__start(prev) <= map__start(map)); + /* + * If the ends of maps aren't broken (during + * construction) then they should be ordered + * too. + */ + if (!RC_CHK_ACCESS(maps)->ends_broken) { + assert(map__end(prev) <= map__end(map)); + assert(map__end(prev) <= map__start(map) || + map__start(prev) == map__start(map)); + } + } + } + } + if (RC_CHK_ACCESS(maps)->maps_by_name) { + for (unsigned int i = 0; i < RC_CHK_ACCESS(maps)->nr_maps; i++) { + struct map *map = RC_CHK_ACCESS(maps)->maps_by_name[i]; -#define maps__for_each_entry_safe(maps, map, next) \ - for (map = maps__first(maps), next = map_rb_node__next(map); map; \ - map = next, next = map_rb_node__next(map)) + /* + * Maps by name maps should be in maps_by_address, so + * the reference count should be higher. + */ + assert(refcount_read(map__refcnt(map)) > 1); + } + } +#endif +} -static struct rb_root *maps__entries(struct maps *maps) +static struct map **maps__maps_by_address(const struct maps *maps) { - return &RC_CHK_ACCESS(maps)->entries; + return RC_CHK_ACCESS(maps)->maps_by_address; } -static struct rw_semaphore *maps__lock(struct maps *maps) +static void maps__set_maps_by_address(struct maps *maps, struct map **new) { - return &RC_CHK_ACCESS(maps)->lock; + RC_CHK_ACCESS(maps)->maps_by_address = new; + } -static struct map **maps__maps_by_name(struct maps *maps) +/* Not in the header, to aid reference counting. */ +static struct map **maps__maps_by_name(const struct maps *maps) { return RC_CHK_ACCESS(maps)->maps_by_name; + } -static struct map_rb_node *maps__first(struct maps *maps) +static void maps__set_maps_by_name(struct maps *maps, struct map **new) { - struct rb_node *first = rb_first(maps__entries(maps)); + RC_CHK_ACCESS(maps)->maps_by_name = new; - if (first) - return rb_entry(first, struct map_rb_node, rb_node); - return NULL; } -static struct map_rb_node *map_rb_node__next(struct map_rb_node *node) +static bool maps__maps_by_address_sorted(const struct maps *maps) { - struct rb_node *next; - - if (!node) - return NULL; - - next = rb_next(&node->rb_node); + return RC_CHK_ACCESS(maps)->maps_by_address_sorted; +} - if (!next) - return NULL; +static void maps__set_maps_by_address_sorted(struct maps *maps, bool value) +{ + RC_CHK_ACCESS(maps)->maps_by_address_sorted = value; +} - return rb_entry(next, struct map_rb_node, rb_node); +static bool maps__maps_by_name_sorted(const struct maps *maps) +{ + return RC_CHK_ACCESS(maps)->maps_by_name_sorted; } -static struct map_rb_node *maps__find_node(struct maps *maps, struct map *map) +static void maps__set_maps_by_name_sorted(struct maps *maps, bool value) { - struct map_rb_node *rb_node; + RC_CHK_ACCESS(maps)->maps_by_name_sorted = value; +} - maps__for_each_entry(maps, rb_node) { - if (rb_node->RC_CHK_ACCESS(map) == RC_CHK_ACCESS(map)) - return rb_node; - } - return NULL; +static struct rw_semaphore *maps__lock(struct maps *maps) +{ + /* + * When the lock is acquired or released the maps invariants should + * hold. + */ + check_invariants(maps); + return &RC_CHK_ACCESS(maps)->lock; } static void maps__init(struct maps *maps, struct machine *machine) { - refcount_set(maps__refcnt(maps), 1); init_rwsem(maps__lock(maps)); - RC_CHK_ACCESS(maps)->entries = RB_ROOT; + RC_CHK_ACCESS(maps)->maps_by_address = NULL; + RC_CHK_ACCESS(maps)->maps_by_name = NULL; RC_CHK_ACCESS(maps)->machine = machine; - RC_CHK_ACCESS(maps)->last_search_by_name = NULL; +#ifdef HAVE_LIBUNWIND_SUPPORT + RC_CHK_ACCESS(maps)->addr_space = NULL; + RC_CHK_ACCESS(maps)->unwind_libunwind_ops = NULL; +#endif + refcount_set(maps__refcnt(maps), 1); RC_CHK_ACCESS(maps)->nr_maps = 0; - RC_CHK_ACCESS(maps)->maps_by_name = NULL; + RC_CHK_ACCESS(maps)->nr_maps_allocated = 0; + RC_CHK_ACCESS(maps)->last_search_by_name_idx = 0; + RC_CHK_ACCESS(maps)->maps_by_address_sorted = true; + RC_CHK_ACCESS(maps)->maps_by_name_sorted = false; } -static void __maps__free_maps_by_name(struct maps *maps) +static void maps__exit(struct maps *maps) { - /* - * Free everything to try to do it from the rbtree in the next search - */ - for (unsigned int i = 0; i < maps__nr_maps(maps); i++) - map__put(maps__maps_by_name(maps)[i]); + struct map **maps_by_address = maps__maps_by_address(maps); + struct map **maps_by_name = maps__maps_by_name(maps); - zfree(&RC_CHK_ACCESS(maps)->maps_by_name); - RC_CHK_ACCESS(maps)->nr_maps_allocated = 0; + for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { + map__zput(maps_by_address[i]); + if (maps_by_name) + map__zput(maps_by_name[i]); + } + zfree(&maps_by_address); + zfree(&maps_by_name); + unwind__finish_access(maps); } -static int __maps__insert(struct maps *maps, struct map *map) +struct maps *maps__new(struct machine *machine) { - struct rb_node **p = &maps__entries(maps)->rb_node; - struct rb_node *parent = NULL; - const u64 ip = map__start(map); - struct map_rb_node *m, *new_rb_node; - - new_rb_node = malloc(sizeof(*new_rb_node)); - if (!new_rb_node) - return -ENOMEM; - - RB_CLEAR_NODE(&new_rb_node->rb_node); - new_rb_node->map = map__get(map); + struct maps *result; + RC_STRUCT(maps) *maps = zalloc(sizeof(*maps)); - while (*p != NULL) { - parent = *p; - m = rb_entry(parent, struct map_rb_node, rb_node); - if (ip < map__start(m->map)) - p = &(*p)->rb_left; - else - p = &(*p)->rb_right; - } + if (ADD_RC_CHK(result, maps)) + maps__init(result, machine); - rb_link_node(&new_rb_node->rb_node, parent, p); - rb_insert_color(&new_rb_node->rb_node, maps__entries(maps)); - return 0; + return result; } -int maps__insert(struct maps *maps, struct map *map) +static void maps__delete(struct maps *maps) { - int err; - const struct dso *dso = map__dso(map); - - down_write(maps__lock(maps)); - err = __maps__insert(maps, map); - if (err) - goto out; + maps__exit(maps); + RC_CHK_FREE(maps); +} - ++RC_CHK_ACCESS(maps)->nr_maps; +struct maps *maps__get(struct maps *maps) +{ + struct maps *result; - if (dso && dso->kernel) { - struct kmap *kmap = map__kmap(map); + if (RC_CHK_GET(result, maps)) + refcount_inc(maps__refcnt(maps)); - if (kmap) - kmap->kmaps = maps; - else - pr_err("Internal error: kernel dso with non kernel map\n"); - } + return result; +} +void maps__put(struct maps *maps) +{ + if (maps && refcount_dec_and_test(maps__refcnt(maps))) + maps__delete(maps); + else + RC_CHK_PUT(maps); +} +static void __maps__free_maps_by_name(struct maps *maps) +{ /* - * If we already performed some search by name, then we need to add the just - * inserted map and resort. + * Free everything to try to do it from the rbtree in the next search */ - if (maps__maps_by_name(maps)) { - if (maps__nr_maps(maps) > RC_CHK_ACCESS(maps)->nr_maps_allocated) { - int nr_allocate = maps__nr_maps(maps) * 2; - struct map **maps_by_name = realloc(maps__maps_by_name(maps), - nr_allocate * sizeof(map)); + for (unsigned int i = 0; i < maps__nr_maps(maps); i++) + map__put(maps__maps_by_name(maps)[i]); - if (maps_by_name == NULL) { - __maps__free_maps_by_name(maps); - err = -ENOMEM; - goto out; - } + zfree(&RC_CHK_ACCESS(maps)->maps_by_name); +} - RC_CHK_ACCESS(maps)->maps_by_name = maps_by_name; - RC_CHK_ACCESS(maps)->nr_maps_allocated = nr_allocate; +static int map__start_cmp(const void *a, const void *b) +{ + const struct map *map_a = *(const struct map * const *)a; + const struct map *map_b = *(const struct map * const *)b; + u64 map_a_start = map__start(map_a); + u64 map_b_start = map__start(map_b); + + if (map_a_start == map_b_start) { + u64 map_a_end = map__end(map_a); + u64 map_b_end = map__end(map_b); + + if (map_a_end == map_b_end) { + /* Ensure maps with the same addresses have a fixed order. */ + if (RC_CHK_ACCESS(map_a) == RC_CHK_ACCESS(map_b)) + return 0; + return (intptr_t)RC_CHK_ACCESS(map_a) > (intptr_t)RC_CHK_ACCESS(map_b) + ? 1 : -1; } - maps__maps_by_name(maps)[maps__nr_maps(maps) - 1] = map__get(map); - __maps__sort_by_name(maps); + return map_a_end > map_b_end ? 1 : -1; } - out: - up_write(maps__lock(maps)); - return err; + return map_a_start > map_b_start ? 1 : -1; } -static void __maps__remove(struct maps *maps, struct map_rb_node *rb_node) +static void __maps__sort_by_address(struct maps *maps) { - rb_erase_init(&rb_node->rb_node, maps__entries(maps)); - map__put(rb_node->map); - free(rb_node); + if (maps__maps_by_address_sorted(maps)) + return; + + qsort(maps__maps_by_address(maps), + maps__nr_maps(maps), + sizeof(struct map *), + map__start_cmp); + maps__set_maps_by_address_sorted(maps, true); } -void maps__remove(struct maps *maps, struct map *map) +static void maps__sort_by_address(struct maps *maps) { - struct map_rb_node *rb_node; - down_write(maps__lock(maps)); - if (RC_CHK_ACCESS(maps)->last_search_by_name == map) - RC_CHK_ACCESS(maps)->last_search_by_name = NULL; - - rb_node = maps__find_node(maps, map); - assert(rb_node->RC_CHK_ACCESS(map) == RC_CHK_ACCESS(map)); - __maps__remove(maps, rb_node); - if (maps__maps_by_name(maps)) - __maps__free_maps_by_name(maps); - --RC_CHK_ACCESS(maps)->nr_maps; + __maps__sort_by_address(maps); up_write(maps__lock(maps)); } -static void __maps__purge(struct maps *maps) +static int map__strcmp(const void *a, const void *b) { - struct map_rb_node *pos, *next; - - if (maps__maps_by_name(maps)) - __maps__free_maps_by_name(maps); + const struct map *map_a = *(const struct map * const *)a; + const struct map *map_b = *(const struct map * const *)b; + const struct dso *dso_a = map__dso(map_a); + const struct dso *dso_b = map__dso(map_b); + int ret = strcmp(dso_a->short_name, dso_b->short_name); - maps__for_each_entry_safe(maps, pos, next) { - rb_erase_init(&pos->rb_node, maps__entries(maps)); - map__put(pos->map); - free(pos); + if (ret == 0 && RC_CHK_ACCESS(map_a) != RC_CHK_ACCESS(map_b)) { + /* Ensure distinct but name equal maps have an order. */ + return map__start_cmp(a, b); } + return ret; } -static void maps__exit(struct maps *maps) +static int maps__sort_by_name(struct maps *maps) { + int err = 0; down_write(maps__lock(maps)); - __maps__purge(maps); + if (!maps__maps_by_name_sorted(maps)) { + struct map **maps_by_name = maps__maps_by_name(maps); + + if (!maps_by_name) { + maps_by_name = malloc(RC_CHK_ACCESS(maps)->nr_maps_allocated * + sizeof(*maps_by_name)); + if (!maps_by_name) + err = -ENOMEM; + else { + struct map **maps_by_address = maps__maps_by_address(maps); + unsigned int n = maps__nr_maps(maps); + + maps__set_maps_by_name(maps, maps_by_name); + for (unsigned int i = 0; i < n; i++) + maps_by_name[i] = map__get(maps_by_address[i]); + } + } + if (!err) { + qsort(maps_by_name, + maps__nr_maps(maps), + sizeof(struct map *), + map__strcmp); + maps__set_maps_by_name_sorted(maps, true); + } + } up_write(maps__lock(maps)); + return err; } -bool maps__empty(struct maps *maps) +static unsigned int maps__by_address_index(const struct maps *maps, const struct map *map) { - return !maps__first(maps); + struct map **maps_by_address = maps__maps_by_address(maps); + + if (maps__maps_by_address_sorted(maps)) { + struct map **mapp = + bsearch(&map, maps__maps_by_address(maps), maps__nr_maps(maps), + sizeof(*mapp), map__start_cmp); + + if (mapp) + return mapp - maps_by_address; + } else { + for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { + if (RC_CHK_ACCESS(maps_by_address[i]) == RC_CHK_ACCESS(map)) + return i; + } + } + pr_err("Map missing from maps"); + return -1; } -struct maps *maps__new(struct machine *machine) +static unsigned int maps__by_name_index(const struct maps *maps, const struct map *map) { - struct maps *result; - RC_STRUCT(maps) *maps = zalloc(sizeof(*maps)); + struct map **maps_by_name = maps__maps_by_name(maps); + + if (maps__maps_by_name_sorted(maps)) { + struct map **mapp = + bsearch(&map, maps_by_name, maps__nr_maps(maps), + sizeof(*mapp), map__strcmp); + + if (mapp) + return mapp - maps_by_name; + } else { + for (unsigned int i = 0; i < maps__nr_maps(maps); i++) { + if (RC_CHK_ACCESS(maps_by_name[i]) == RC_CHK_ACCESS(map)) + return i; + } + } + pr_err("Map missing from maps"); + return -1; +} - if (ADD_RC_CHK(result, maps)) - maps__init(result, machine); +static int __maps__insert(struct maps *maps, struct map *new) +{ + struct map **maps_by_address = maps__maps_by_address(maps); + struct map **maps_by_name = maps__maps_by_name(maps); + const struct dso *dso = map__dso(new); + unsigned int nr_maps = maps__nr_maps(maps); + unsigned int nr_allocate = RC_CHK_ACCESS(maps)->nr_maps_allocated; + + if (nr_maps + 1 > nr_allocate) { + nr_allocate = !nr_allocate ? 32 : nr_allocate * 2; + + maps_by_address = realloc(maps_by_address, nr_allocate * sizeof(new)); + if (!maps_by_address) + return -ENOMEM; + + maps__set_maps_by_address(maps, maps_by_address); + if (maps_by_name) { + maps_by_name = realloc(maps_by_name, nr_allocate * sizeof(new)); + if (!maps_by_name) { + /* + * If by name fails, just disable by name and it will + * recompute next time it is required. + */ + __maps__free_maps_by_name(maps); + } + maps__set_maps_by_name(maps, maps_by_name); + } + RC_CHK_ACCESS(maps)->nr_maps_allocated = nr_allocate; + } + /* Insert the value at the end. */ + maps_by_address[nr_maps] = map__get(new); + if (maps_by_name) + maps_by_name[nr_maps] = map__get(new); - return result; + nr_maps++; + RC_CHK_ACCESS(maps)->nr_maps = nr_maps; + + /* + * Recompute if things are sorted. If things are inserted in a sorted + * manner, for example by processing /proc/pid/maps, then no + * sorting/resorting will be necessary. + */ + if (nr_maps == 1) { + /* If there's just 1 entry then maps are sorted. */ + maps__set_maps_by_address_sorted(maps, true); + maps__set_maps_by_name_sorted(maps, maps_by_name != NULL); + } else { + /* Sorted if maps were already sorted and this map starts after the last one. */ + maps__set_maps_by_address_sorted(maps, + maps__maps_by_address_sorted(maps) && + map__end(maps_by_address[nr_maps - 2]) <= map__start(new)); + maps__set_maps_by_name_sorted(maps, false); + } + if (map__end(new) < map__start(new)) + RC_CHK_ACCESS(maps)->ends_broken = true; + if (dso && dso->kernel) { + struct kmap *kmap = map__kmap(new); + + if (kmap) + kmap->kmaps = maps; + else + pr_err("Internal error: kernel dso with non kernel map\n"); + } + check_invariants(maps); + return 0; } -static void maps__delete(struct maps *maps) +int maps__insert(struct maps *maps, struct map *map) { - maps__exit(maps); - unwind__finish_access(maps); - RC_CHK_FREE(maps); + int ret; + + down_write(maps__lock(maps)); + ret = __maps__insert(maps, map); + up_write(maps__lock(maps)); + return ret; } -struct maps *maps__get(struct maps *maps) +static void __maps__remove(struct maps *maps, struct map *map) { - struct maps *result; + struct map **maps_by_address = maps__maps_by_address(maps); + struct map **maps_by_name = maps__maps_by_name(maps); + unsigned int nr_maps = maps__nr_maps(maps); + unsigned int address_idx; + + /* Slide later mappings over the one to remove */ + address_idx = maps__by_address_index(maps, map); + map__put(maps_by_address[address_idx]); + memmove(&maps_by_address[address_idx], + &maps_by_address[address_idx + 1], + (nr_maps - address_idx - 1) * sizeof(*maps_by_address)); + + if (maps_by_name) { + unsigned int name_idx = maps__by_name_index(maps, map); + + map__put(maps_by_name[name_idx]); + memmove(&maps_by_name[name_idx], + &maps_by_name[name_idx + 1], + (nr_maps - name_idx - 1) * sizeof(*maps_by_name)); + } - if (RC_CHK_GET(result, maps)) - refcount_inc(maps__refcnt(maps)); + --RC_CHK_ACCESS(maps)->nr_maps; + check_invariants(maps); +} - return result; +void maps__remove(struct maps *maps, struct map *map) +{ + down_write(maps__lock(maps)); + __maps__remove(maps, map); + up_write(maps__lock(maps)); } -void maps__put(struct maps *maps) +bool maps__empty(struct maps *maps) { - if (maps && refcount_dec_and_test(maps__refcnt(maps))) - maps__delete(maps); - else - RC_CHK_PUT(maps); + return maps__nr_maps(maps) == 0; } int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data) { - struct map_rb_node *pos; + bool done = false; int ret = 0; - down_read(maps__lock(maps)); - maps__for_each_entry(maps, pos) { - ret = cb(pos->map, data); - if (ret) - break; + /* See locking/sorting note. */ + while (!done) { + down_read(maps__lock(maps)); + if (maps__maps_by_address_sorted(maps)) { + struct map **maps_by_address = maps__maps_by_address(maps); + unsigned int n = maps__nr_maps(maps); + + for (unsigned int i = 0; i < n; i++) { + struct map *map = maps_by_address[i]; + + ret = cb(map, data); + if (ret) + break; + } + done = true; + } + up_read(maps__lock(maps)); + if (!done) + maps__sort_by_address(maps); } - up_read(maps__lock(maps)); return ret; } void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data) { - struct map_rb_node *pos, *next; - unsigned int start_nr_maps; + struct map **maps_by_address; down_write(maps__lock(maps)); - start_nr_maps = maps__nr_maps(maps); - maps__for_each_entry_safe(maps, pos, next) { - if (cb(pos->map, data)) { - __maps__remove(maps, pos); - --RC_CHK_ACCESS(maps)->nr_maps; - } + maps_by_address = maps__maps_by_address(maps); + for (unsigned int i = 0; i < maps__nr_maps(maps);) { + if (cb(maps_by_address[i], data)) + __maps__remove(maps, maps_by_address[i]); + else + i++; } - if (maps__maps_by_name(maps) && start_nr_maps != maps__nr_maps(maps)) - __maps__free_maps_by_name(maps); - up_write(maps__lock(maps)); } @@ -300,7 +491,7 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) /* Ensure map is loaded before using map->map_ip */ if (map != NULL && map__load(map) >= 0) { if (mapp != NULL) - *mapp = map; + *mapp = map; // TODO: map_put on else path when find returns a get. return map__find_symbol(map, map__map_ip(map, addr)); } @@ -348,7 +539,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) if (ams->addr < map__start(ams->ms.map) || ams->addr >= map__end(ams->ms.map)) { if (maps == NULL) return -1; - ams->ms.map = maps__find(maps, ams->addr); + ams->ms.map = maps__find(maps, ams->addr); // TODO: map_get if (ams->ms.map == NULL) return -1; } @@ -393,24 +584,28 @@ size_t maps__fprintf(struct maps *maps, FILE *fp) * Find first map where end > new->start. * Same as find_vma() in kernel. */ -static struct rb_node *first_ending_after(struct maps *maps, const struct map *map) +static unsigned int first_ending_after(struct maps *maps, const struct map *map) { - struct rb_root *root; - struct rb_node *next, *first; + struct map **maps_by_address = maps__maps_by_address(maps); + int low = 0, high = (int)maps__nr_maps(maps) - 1, first = high + 1; + + assert(maps__maps_by_address_sorted(maps)); + if (low <= high && map__end(maps_by_address[0]) > map__start(map)) + return 0; - root = maps__entries(maps); - next = root->rb_node; - first = NULL; - while (next) { - struct map_rb_node *pos = rb_entry(next, struct map_rb_node, rb_node); + while (low <= high) { + int mid = (low + high) / 2; + struct map *pos = maps_by_address[mid]; - if (map__end(pos->map) > map__start(map)) { - first = next; - if (map__start(pos->map) <= map__start(map)) + if (map__end(pos) > map__start(map)) { + first = mid; + if (map__start(pos) <= map__start(map)) { + /* Entry overlaps map. */ break; - next = next->rb_left; + } + high = mid - 1; } else - next = next->rb_right; + low = mid + 1; } return first; } @@ -419,170 +614,248 @@ static struct rb_node *first_ending_after(struct maps *maps, const struct map *m * Adds new to maps, if new overlaps existing entries then the existing maps are * adjusted or removed so that new fits without overlapping any entries. */ -int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) +static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) { - - struct rb_node *next; + struct map **maps_by_address; int err = 0; - down_write(maps__lock(maps)); +sort_again: + if (!maps__maps_by_address_sorted(maps)) + __maps__sort_by_address(maps); - next = first_ending_after(maps, new); - while (next && !err) { - struct map_rb_node *pos = rb_entry(next, struct map_rb_node, rb_node); - next = rb_next(&pos->rb_node); + maps_by_address = maps__maps_by_address(maps); + /* + * Iterate through entries where the end of the existing entry is + * greater-than the new map's start. + */ + for (unsigned int i = first_ending_after(maps, new); i < maps__nr_maps(maps); ) { + struct map *pos = maps_by_address[i]; + struct map *before = NULL, *after = NULL; /* * Stop if current map starts after map->end. * Maps are ordered by start: next will not overlap for sure. */ - if (map__start(pos->map) >= map__end(new)) + if (map__start(pos) >= map__end(new)) break; - if (verbose >= 2) { - - if (use_browser) { - pr_debug("overlapping maps in %s (disable tui for more info)\n", - map__dso(new)->name); - } else { - pr_debug("overlapping maps:\n"); - map__fprintf(new, debug_file()); - map__fprintf(pos->map, debug_file()); - } + if (use_browser) { + pr_debug("overlapping maps in %s (disable tui for more info)\n", + map__dso(new)->name); + } else if (verbose >= 2) { + pr_debug("overlapping maps:\n"); + map__fprintf(new, debug_file()); + map__fprintf(pos, debug_file()); } - rb_erase_init(&pos->rb_node, maps__entries(maps)); /* * Now check if we need to create new maps for areas not * overlapped by the new map: */ - if (map__start(new) > map__start(pos->map)) { - struct map *before = map__clone(pos->map); + if (map__start(new) > map__start(pos)) { + /* Map starts within existing map. Need to shorten the existing map. */ + before = map__clone(pos); if (before == NULL) { err = -ENOMEM; - goto put_map; + goto out_err; } - map__set_end(before, map__start(new)); - err = __maps__insert(maps, before); - if (err) { - map__put(before); - goto put_map; - } if (verbose >= 2 && !use_browser) map__fprintf(before, debug_file()); - map__put(before); } - - if (map__end(new) < map__end(pos->map)) { - struct map *after = map__clone(pos->map); + if (map__end(new) < map__end(pos)) { + /* The new map isn't as long as the existing map. */ + after = map__clone(pos); if (after == NULL) { + map__zput(before); err = -ENOMEM; - goto put_map; + goto out_err; } map__set_start(after, map__end(new)); - map__add_pgoff(after, map__end(new) - map__start(pos->map)); - assert(map__map_ip(pos->map, map__end(new)) == - map__map_ip(after, map__end(new))); - err = __maps__insert(maps, after); - if (err) { - map__put(after); - goto put_map; - } + map__add_pgoff(after, map__end(new) - map__start(pos)); + assert(map__map_ip(pos, map__end(new)) == + map__map_ip(after, map__end(new))); + if (verbose >= 2 && !use_browser) map__fprintf(after, debug_file()); - map__put(after); } -put_map: - map__put(pos->map); - free(pos); + /* + * If adding one entry, for `before` or `after`, we can replace + * the existing entry. If both `before` and `after` are + * necessary than an insert is needed. If the existing entry + * entirely overlaps the existing entry it can just be removed. + */ + if (before) { + map__put(maps_by_address[i]); + maps_by_address[i] = before; + /* Maps are still ordered, go to next one. */ + i++; + if (after) { + __maps__insert(maps, after); + map__put(after); + if (!maps__maps_by_address_sorted(maps)) { + /* + * Sorting broken so invariants don't + * hold, sort and go again. + */ + goto sort_again; + } + /* + * Maps are still ordered, skip after and go to + * next one (terminate loop). + */ + i++; + } + } else if (after) { + map__put(maps_by_address[i]); + maps_by_address[i] = after; + /* Maps are ordered, go to next one. */ + i++; + } else { + __maps__remove(maps, pos); + /* + * Maps are ordered but no need to increase `i` as the + * later maps were moved down. + */ + } + check_invariants(maps); } /* Add the map. */ - err = __maps__insert(maps, new); - up_write(maps__lock(maps)); + __maps__insert(maps, new); +out_err: return err; } -int maps__copy_from(struct maps *maps, struct maps *parent) +int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) { int err; - struct map_rb_node *rb_node; + down_write(maps__lock(maps)); + err = __maps__fixup_overlap_and_insert(maps, new); + up_write(maps__lock(maps)); + return err; +} + +int maps__copy_from(struct maps *dest, struct maps *parent) +{ + /* Note, if struct map were immutable then cloning could use ref counts. */ + struct map **parent_maps_by_address; + int err = 0; + unsigned int n; + + down_write(maps__lock(dest)); down_read(maps__lock(parent)); - maps__for_each_entry(parent, rb_node) { - struct map *new = map__clone(rb_node->map); + parent_maps_by_address = maps__maps_by_address(parent); + n = maps__nr_maps(parent); + if (maps__empty(dest)) { + /* No existing mappings so just copy from parent to avoid reallocs in insert. */ + unsigned int nr_maps_allocated = RC_CHK_ACCESS(parent)->nr_maps_allocated; + struct map **dest_maps_by_address = + malloc(nr_maps_allocated * sizeof(struct map *)); + struct map **dest_maps_by_name = NULL; - if (new == NULL) { + if (!dest_maps_by_address) err = -ENOMEM; - goto out_unlock; + else { + if (maps__maps_by_name(parent)) { + dest_maps_by_name = + malloc(nr_maps_allocated * sizeof(struct map *)); + } + + RC_CHK_ACCESS(dest)->maps_by_address = dest_maps_by_address; + RC_CHK_ACCESS(dest)->maps_by_name = dest_maps_by_name; + RC_CHK_ACCESS(dest)->nr_maps_allocated = nr_maps_allocated; } - err = unwind__prepare_access(maps, new, NULL); - if (err) - goto out_unlock; + for (unsigned int i = 0; !err && i < n; i++) { + struct map *pos = parent_maps_by_address[i]; + struct map *new = map__clone(pos); - err = maps__insert(maps, new); - if (err) - goto out_unlock; + if (!new) + err = -ENOMEM; + else { + err = unwind__prepare_access(dest, new, NULL); + if (!err) { + dest_maps_by_address[i] = new; + if (dest_maps_by_name) + dest_maps_by_name[i] = map__get(new); + RC_CHK_ACCESS(dest)->nr_maps = i + 1; + } + } + if (err) + map__put(new); + } + maps__set_maps_by_address_sorted(dest, maps__maps_by_address_sorted(parent)); + if (!err) { + RC_CHK_ACCESS(dest)->last_search_by_name_idx = + RC_CHK_ACCESS(parent)->last_search_by_name_idx; + maps__set_maps_by_name_sorted(dest, + dest_maps_by_name && + maps__maps_by_name_sorted(parent)); + } else { + RC_CHK_ACCESS(dest)->last_search_by_name_idx = 0; + maps__set_maps_by_name_sorted(dest, false); + } + } else { + /* Unexpected copying to a maps containing entries. */ + for (unsigned int i = 0; !err && i < n; i++) { + struct map *pos = parent_maps_by_address[i]; + struct map *new = map__clone(pos); - map__put(new); + if (!new) + err = -ENOMEM; + else { + err = unwind__prepare_access(dest, new, NULL); + if (!err) + err = maps__insert(dest, new); + } + map__put(new); + } } - - err = 0; -out_unlock: up_read(maps__lock(parent)); + up_write(maps__lock(dest)); return err; } -struct map *maps__find(struct maps *maps, u64 ip) +static int map__addr_cmp(const void *key, const void *entry) { - struct rb_node *p; - struct map_rb_node *m; - + const u64 ip = *(const u64 *)key; + const struct map *map = *(const struct map * const *)entry; - down_read(maps__lock(maps)); - - p = maps__entries(maps)->rb_node; - while (p != NULL) { - m = rb_entry(p, struct map_rb_node, rb_node); - if (ip < map__start(m->map)) - p = p->rb_left; - else if (ip >= map__end(m->map)) - p = p->rb_right; - else - goto out; - } - - m = NULL; -out: - up_read(maps__lock(maps)); - return m ? m->map : NULL; + if (ip < map__start(map)) + return -1; + if (ip >= map__end(map)) + return 1; + return 0; } -static int map__strcmp(const void *a, const void *b) +struct map *maps__find(struct maps *maps, u64 ip) { - const struct map *map_a = *(const struct map **)a; - const struct map *map_b = *(const struct map **)b; - const struct dso *dso_a = map__dso(map_a); - const struct dso *dso_b = map__dso(map_b); - int ret = strcmp(dso_a->short_name, dso_b->short_name); - - if (ret == 0 && map_a != map_b) { - /* - * Ensure distinct but name equal maps have an order in part to - * aid reference counting. - */ - ret = (int)map__start(map_a) - (int)map__start(map_b); - if (ret == 0) - ret = (int)((intptr_t)map_a - (intptr_t)map_b); + struct map *result = NULL; + bool done = false; + + /* See locking/sorting note. */ + while (!done) { + down_read(maps__lock(maps)); + if (maps__maps_by_address_sorted(maps)) { + struct map **mapp = + bsearch(&ip, maps__maps_by_address(maps), maps__nr_maps(maps), + sizeof(*mapp), map__addr_cmp); + + if (mapp) + result = *mapp; // map__get(*mapp); + done = true; + } + up_read(maps__lock(maps)); + if (!done) + maps__sort_by_address(maps); } - - return ret; + return result; } static int map__strcmp_name(const void *name, const void *b) @@ -592,126 +865,113 @@ static int map__strcmp_name(const void *name, const void *b) return strcmp(name, dso->short_name); } -void __maps__sort_by_name(struct maps *maps) -{ - qsort(maps__maps_by_name(maps), maps__nr_maps(maps), sizeof(struct map *), map__strcmp); -} - -static int map__groups__sort_by_name_from_rbtree(struct maps *maps) -{ - struct map_rb_node *rb_node; - struct map **maps_by_name = realloc(maps__maps_by_name(maps), - maps__nr_maps(maps) * sizeof(struct map *)); - int i = 0; - - if (maps_by_name == NULL) - return -1; - - up_read(maps__lock(maps)); - down_write(maps__lock(maps)); - - RC_CHK_ACCESS(maps)->maps_by_name = maps_by_name; - RC_CHK_ACCESS(maps)->nr_maps_allocated = maps__nr_maps(maps); - - maps__for_each_entry(maps, rb_node) - maps_by_name[i++] = map__get(rb_node->map); - - __maps__sort_by_name(maps); - - up_write(maps__lock(maps)); - down_read(maps__lock(maps)); - - return 0; -} - -static struct map *__maps__find_by_name(struct maps *maps, const char *name) +struct map *maps__find_by_name(struct maps *maps, const char *name) { - struct map **mapp; + struct map *result = NULL; + bool done = false; - if (maps__maps_by_name(maps) == NULL && - map__groups__sort_by_name_from_rbtree(maps)) - return NULL; + /* See locking/sorting note. */ + while (!done) { + unsigned int i; - mapp = bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), - sizeof(*mapp), map__strcmp_name); - if (mapp) - return *mapp; - return NULL; -} + down_read(maps__lock(maps)); -struct map *maps__find_by_name(struct maps *maps, const char *name) -{ - struct map_rb_node *rb_node; - struct map *map; - - down_read(maps__lock(maps)); + /* First check last found entry. */ + i = RC_CHK_ACCESS(maps)->last_search_by_name_idx; + if (i < maps__nr_maps(maps) && maps__maps_by_name(maps)) { + struct dso *dso = map__dso(maps__maps_by_name(maps)[i]); + if (dso && strcmp(dso->short_name, name) == 0) { + result = maps__maps_by_name(maps)[i]; // TODO: map__get + done = true; + } + } - if (RC_CHK_ACCESS(maps)->last_search_by_name) { - const struct dso *dso = map__dso(RC_CHK_ACCESS(maps)->last_search_by_name); + /* Second search sorted array. */ + if (!done && maps__maps_by_name_sorted(maps)) { + struct map **mapp = + bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), + sizeof(*mapp), map__strcmp_name); - if (strcmp(dso->short_name, name) == 0) { - map = RC_CHK_ACCESS(maps)->last_search_by_name; - goto out_unlock; + if (mapp) { + result = *mapp; // TODO: map__get + i = mapp - maps__maps_by_name(maps); + RC_CHK_ACCESS(maps)->last_search_by_name_idx = i; + } + done = true; } - } - /* - * If we have maps->maps_by_name, then the name isn't in the rbtree, - * as maps->maps_by_name mirrors the rbtree when lookups by name are - * made. - */ - map = __maps__find_by_name(maps, name); - if (map || maps__maps_by_name(maps) != NULL) - goto out_unlock; - - /* Fallback to traversing the rbtree... */ - maps__for_each_entry(maps, rb_node) { - struct dso *dso; - - map = rb_node->map; - dso = map__dso(map); - if (strcmp(dso->short_name, name) == 0) { - RC_CHK_ACCESS(maps)->last_search_by_name = map; - goto out_unlock; + up_read(maps__lock(maps)); + if (!done) { + /* Sort and retry binary search. */ + if (maps__sort_by_name(maps)) { + /* + * Memory allocation failed do linear search + * through address sorted maps. + */ + struct map **maps_by_address; + unsigned int n; + + down_read(maps__lock(maps)); + maps_by_address = maps__maps_by_address(maps); + n = maps__nr_maps(maps); + for (i = 0; i < n; i++) { + struct map *pos = maps_by_address[i]; + struct dso *dso = map__dso(pos); + + if (dso && strcmp(dso->short_name, name) == 0) { + result = pos; // TODO: map__get + break; + } + } + up_read(maps__lock(maps)); + done = true; + } } } - map = NULL; - -out_unlock: - up_read(maps__lock(maps)); - return map; + return result; } struct map *maps__find_next_entry(struct maps *maps, struct map *map) { - struct map_rb_node *rb_node = maps__find_node(maps, map); - struct map_rb_node *next = map_rb_node__next(rb_node); + unsigned int i; + struct map *result = NULL; - if (next) - return next->map; + down_read(maps__lock(maps)); + i = maps__by_address_index(maps, map); + if (i < maps__nr_maps(maps)) + result = maps__maps_by_address(maps)[i]; // TODO: map__get - return NULL; + up_read(maps__lock(maps)); + return result; } void maps__fixup_end(struct maps *maps) { - struct map_rb_node *prev = NULL, *curr; + struct map **maps_by_address; + unsigned int n; down_write(maps__lock(maps)); + if (!maps__maps_by_address_sorted(maps)) + __maps__sort_by_address(maps); - maps__for_each_entry(maps, curr) { - if (prev && (!map__end(prev->map) || map__end(prev->map) > map__start(curr->map))) - map__set_end(prev->map, map__start(curr->map)); + maps_by_address = maps__maps_by_address(maps); + n = maps__nr_maps(maps); + for (unsigned int i = 1; i < n; i++) { + struct map *prev = maps_by_address[i - 1]; + struct map *curr = maps_by_address[i]; - prev = curr; + if (!map__end(prev) || map__end(prev) > map__start(curr)) + map__set_end(prev, map__start(curr)); } /* * We still haven't the actual symbols, so guess the * last map final address. */ - if (curr && !map__end(curr->map)) - map__set_end(curr->map, ~0ULL); + if (n > 0 && !map__end(maps_by_address[n - 1])) + map__set_end(maps_by_address[n - 1], ~0ULL); + + RC_CHK_ACCESS(maps)->ends_broken = false; up_write(maps__lock(maps)); } @@ -722,117 +982,92 @@ void maps__fixup_end(struct maps *maps) */ int maps__merge_in(struct maps *kmaps, struct map *new_map) { - struct map_rb_node *rb_node; - struct rb_node *first; - bool overlaps; - LIST_HEAD(merged); - int err = 0; + unsigned int first_after_, kmaps__nr_maps; + struct map **kmaps_maps_by_address; + struct map **merged_maps_by_address; + unsigned int merged_nr_maps_allocated; + + /* First try under a read lock. */ + while (true) { + down_read(maps__lock(kmaps)); + if (maps__maps_by_address_sorted(kmaps)) + break; - down_read(maps__lock(kmaps)); - first = first_ending_after(kmaps, new_map); - overlaps = first && - map__start(rb_entry(first, struct map_rb_node, rb_node)->map) < map__end(new_map); - up_read(maps__lock(kmaps)); + up_read(maps__lock(kmaps)); + + /* First after binary search requires sorted maps. Sort and try again. */ + maps__sort_by_address(kmaps); + } + first_after_ = first_ending_after(kmaps, new_map); + kmaps_maps_by_address = maps__maps_by_address(kmaps); - if (!overlaps) + if (first_after_ >= maps__nr_maps(kmaps) || + map__start(kmaps_maps_by_address[first_after_]) >= map__end(new_map)) { + /* No overlap so regular insert suffices. */ + up_read(maps__lock(kmaps)); return maps__insert(kmaps, new_map); + } + up_read(maps__lock(kmaps)); - maps__for_each_entry(kmaps, rb_node) { - struct map *old_map = rb_node->map; + /* Plain insert with a read-lock failed, try again now with the write lock. */ + down_write(maps__lock(kmaps)); + if (!maps__maps_by_address_sorted(kmaps)) + __maps__sort_by_address(kmaps); - /* no overload with this one */ - if (map__end(new_map) < map__start(old_map) || - map__start(new_map) >= map__end(old_map)) - continue; + first_after_ = first_ending_after(kmaps, new_map); + kmaps_maps_by_address = maps__maps_by_address(kmaps); + kmaps__nr_maps = maps__nr_maps(kmaps); - if (map__start(new_map) < map__start(old_map)) { - /* - * |new...... - * |old.... - */ - if (map__end(new_map) < map__end(old_map)) { - /* - * |new......| -> |new..| - * |old....| -> |old....| - */ - map__set_end(new_map, map__start(old_map)); - } else { - /* - * |new.............| -> |new..| |new..| - * |old....| -> |old....| - */ - struct map_list_node *m = map_list_node__new(); + if (first_after_ >= kmaps__nr_maps || + map__start(kmaps_maps_by_address[first_after_]) >= map__end(new_map)) { + /* No overlap so regular insert suffices. */ + up_write(maps__lock(kmaps)); + return maps__insert(kmaps, new_map); + } + /* Array to merge into, possibly 1 more for the sake of new_map. */ + merged_nr_maps_allocated = RC_CHK_ACCESS(kmaps)->nr_maps_allocated; + if (kmaps__nr_maps + 1 == merged_nr_maps_allocated) + merged_nr_maps_allocated++; + + merged_maps_by_address = malloc(merged_nr_maps_allocated * sizeof(*merged_maps_by_address)); + if (!merged_maps_by_address) { + up_write(maps__lock(kmaps)); + return -ENOMEM; + } + RC_CHK_ACCESS(kmaps)->maps_by_address = merged_maps_by_address; + RC_CHK_ACCESS(kmaps)->maps_by_address_sorted = true; + zfree(&RC_CHK_ACCESS(kmaps)->maps_by_name); + RC_CHK_ACCESS(kmaps)->maps_by_name_sorted = false; + RC_CHK_ACCESS(kmaps)->nr_maps_allocated = merged_nr_maps_allocated; - if (!m) { - err = -ENOMEM; - goto out; - } + /* Copy entries before the new_map that can't overlap. */ + for (unsigned int i = 0; i < first_after_; i++) + merged_maps_by_address[i] = map__get(kmaps_maps_by_address[i]); - m->map = map__clone(new_map); - if (!m->map) { - free(m); - err = -ENOMEM; - goto out; - } + RC_CHK_ACCESS(kmaps)->nr_maps = first_after_; - map__set_end(m->map, map__start(old_map)); - list_add_tail(&m->node, &merged); - map__add_pgoff(new_map, map__end(old_map) - map__start(new_map)); - map__set_start(new_map, map__end(old_map)); - } - } else { - /* - * |new...... - * |old.... - */ - if (map__end(new_map) < map__end(old_map)) { - /* - * |new..| -> x - * |old.........| -> |old.........| - */ - map__put(new_map); - new_map = NULL; - break; - } else { - /* - * |new......| -> |new...| - * |old....| -> |old....| - */ - map__add_pgoff(new_map, map__end(old_map) - map__start(new_map)); - map__set_start(new_map, map__end(old_map)); - } - } - } + /* Add the new map, it will be split when the later overlapping mappings are added. */ + __maps__insert(kmaps, new_map); -out: - while (!list_empty(&merged)) { - struct map_list_node *old_node; + /* Insert mappings after new_map, splitting new_map in the process. */ + for (unsigned int i = first_after_; i < kmaps__nr_maps; i++) + __maps__fixup_overlap_and_insert(kmaps, kmaps_maps_by_address[i]); - old_node = list_entry(merged.next, struct map_list_node, node); - list_del_init(&old_node->node); - if (!err) - err = maps__insert(kmaps, old_node->map); - map__put(old_node->map); - free(old_node); - } + /* Copy the maps from merged into kmaps. */ + for (unsigned int i = 0; i < kmaps__nr_maps; i++) + map__zput(kmaps_maps_by_address[i]); - if (new_map) { - if (!err) - err = maps__insert(kmaps, new_map); - map__put(new_map); - } - return err; + free(kmaps_maps_by_address); + up_write(maps__lock(kmaps)); + return 0; } void maps__load_first(struct maps *maps) { - struct map_rb_node *first; - down_read(maps__lock(maps)); - first = maps__first(maps); - if (first) - map__load(first->map); + if (maps__nr_maps(maps) > 0) + map__load(maps__maps_by_address(maps)[0]); up_read(maps__lock(maps)); } diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index d836d04c9402..df9dd5a0e3c0 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -25,21 +25,56 @@ static inline struct map_list_node *map_list_node__new(void) return malloc(sizeof(struct map_list_node)); } -struct map *maps__find(struct maps *maps, u64 addr); +/* + * Locking/sorting note: + * + * Sorting is done with the write lock, iteration and binary searching happens + * under the read lock requiring being sorted. There is a race between sorting + * releasing the write lock and acquiring the read lock for iteration/searching + * where another thread could insert and break the sorting of the maps. In + * practice inserting maps should be rare meaning that the race shouldn't lead + * to live lock. Removal of maps doesn't break being sorted. + */ DECLARE_RC_STRUCT(maps) { - struct rb_root entries; struct rw_semaphore lock; - struct machine *machine; - struct map *last_search_by_name; + /** + * @maps_by_address: array of maps sorted by their starting address if + * maps_by_address_sorted is true. + */ + struct map **maps_by_address; + /** + * @maps_by_name: optional array of maps sorted by their dso name if + * maps_by_name_sorted is true. + */ struct map **maps_by_name; - refcount_t refcnt; - unsigned int nr_maps; - unsigned int nr_maps_allocated; + struct machine *machine; #ifdef HAVE_LIBUNWIND_SUPPORT - void *addr_space; + void *addr_space; const struct unwind_libunwind_ops *unwind_libunwind_ops; #endif + refcount_t refcnt; + /** + * @nr_maps: number of maps_by_address, and possibly maps_by_name, + * entries that contain maps. + */ + unsigned int nr_maps; + /** + * @nr_maps_allocated: number of entries in maps_by_address and possibly + * maps_by_name. + */ + unsigned int nr_maps_allocated; + /** + * @last_search_by_name_idx: cache of last found by name entry's index + * as frequent searches for the same dso name are common. + */ + unsigned int last_search_by_name_idx; + /** @maps_by_address_sorted: is maps_by_address sorted. */ + bool maps_by_address_sorted; + /** @maps_by_name_sorted: is maps_by_name sorted. */ + bool maps_by_name_sorted; + /** @ends_broken: does the map contain a map where end values are unset/unsorted? */ + bool ends_broken; }; #define KMAP_NAME_LEN 256 @@ -102,6 +137,7 @@ size_t maps__fprintf(struct maps *maps, FILE *fp); int maps__insert(struct maps *maps, struct map *map); void maps__remove(struct maps *maps, struct map *map); +struct map *maps__find(struct maps *maps, u64 addr); struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp); struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp); @@ -117,8 +153,6 @@ struct map *maps__find_next_entry(struct maps *maps, struct map *map); int maps__merge_in(struct maps *kmaps, struct map *new_map); -void __maps__sort_by_name(struct maps *maps); - void maps__fixup_end(struct maps *maps); void maps__load_first(struct maps *maps); From patchwork Tue Oct 24 22:23:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157740 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238153vqx; Tue, 24 Oct 2023 15:27:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEttINcZ4oul/RjRpt13my3K1ANAATMWlnZObaRuYMhXS8FMcd8jT4vjCHQTf+gobnpZdZe X-Received: by 2002:a05:6a00:1389:b0:68f:d35d:217e with SMTP id t9-20020a056a00138900b0068fd35d217emr12257665pfg.2.1698186468392; Tue, 24 Oct 2023 15:27:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186468; cv=none; d=google.com; s=arc-20160816; b=Q07RF+Vim8DwFgXNG9xmrS89EgN3/9TO7hfW+iZjns7d6IG+IMnUfBsIETIsLTOGvv qn6XZSI0n/lDAJo0yHM57YEK2zWt+zchMgqM+aDR21CHLZE6eEfuygQJ47lTIiMtOht/ zbA/kVQLopxFufDvLP4mZN1XtabEaW9piS0tTTy4a9qyPTuAlydEL9ZLPAaP+awl4Ya/ WW4oID2cbpxqnaE5GIjBiJJddPtmCqTvhLeqjA6czcG9rhVusrqpylk/BByopQVybl74 JE+NCntZgDOn5frpZSo+iOXm7BtMThDQRH+nfMNvFg9j8JvTUBK466NuqK02F1RuapI4 HZgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=QoN6caU65OY1ObCT0xDOKeDiLbElopBLreETwsjwODA=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=MC1wepCpr/1dyM/PxPGgxLgv3nccewkBs4qaMyxp9CiR+KDRebOwwYrzWw6tt6gTwV xtOIi6CCUu9Wgn54nfkXAP7moDtqXjoxdSX8n9SDnmRk95JSmFFrwSZWqduflQWRz78O GYrL42VsbmiLoflqjSz2ECv7XppXyj3en0sOTAlZeGIC1QAHmE/7LM6wTI9w+2O24pqr rZQQsYsGCcrhAIL3bSgfZB6u4p51VAd6V3ycg/auWPhRnlrWijnPZ+ZlxSzWgctx3e3x QiVQrP7iqPVAeppYBbazMCMsPpcCPENCZ6hgS1Bqe9v6uPKrBJwqMHEYZtdHQ3bVc+Ru luWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=iyn0IFHk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id fd7-20020a056a002e8700b006be25b58596si9286220pfb.77.2023.10.24.15.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=iyn0IFHk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id E80DD80BEF1D; Tue, 24 Oct 2023 15:27:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344438AbjJXW1A (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344625AbjJXW03 (ORCPT ); Tue, 24 Oct 2023 18:26:29 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CCEF30EF for ; Tue, 24 Oct 2023 15:25:41 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7fb3f311bso64650707b3.2 for ; Tue, 24 Oct 2023 15:25:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186340; x=1698791140; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=QoN6caU65OY1ObCT0xDOKeDiLbElopBLreETwsjwODA=; b=iyn0IFHkuuyBIi5nwyEFFlld9VzXFpyHKy3cLX/gm/AttIf2WEZoCJyer6pfpkZCuY T7+VBBq9I5Ah0Tl6yHl/kFYtdOtblukdoOXxqDvviJ64XSBfo4VTICWTA1YYKZY2gGFw /FzgdIwiifCPJM/4lV892d6a446AYowhm8Uw9Ct09jy62p/5JkFDVKaDAkwFPDLlLJ9T CZ0mKhrQItTyF3JCqEsWnKkmM17UnM+cEyRQkbavTS9ZHk1NhdVAKwPWxSfTTjtLADop fPLGggTl5mRM8tYOZ1Wob5BI3C6LqEmclmUYOCIieocl0PgM7qzo3DBNF8olvG+vAspp YMTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186340; x=1698791140; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QoN6caU65OY1ObCT0xDOKeDiLbElopBLreETwsjwODA=; b=somQkyM2NwF+LYAMKul9L2mOQWAJhwCc2nZ8i+0jAhZ+EZwIRa26dSGjUgvQj+/1Qv KqW0sb7gtUT+pQf2KgOM6+VcQ+ztKjkYp18h030mZORATIO5/ee+QWoiGY1ZJKHoKxAJ fK20xkr8A4oQUNb8MiPkst2Yi+ChMkP1HC48xSimZGjcWZaUWfQVBkjij+tdqx7QbiXJ 4tYLh2KHsqKvzz82ia2lBna8XhKQvsDLf05QWe+lT2ZAAjEFkJ2krAmZ4e+eE1/dR2D/ XHiq/8QOOL7AmqWfjS/e7ddecwj/1PsFHc3Pdst1oS6HQO7gmSfXe6lg5cODTvoyHuZr SBnQ== X-Gm-Message-State: AOJu0YyLzfd4HZ81jQn7D2LwrRgmbAtlZUGZtCiYHXRhdyXvGsmyr9qy 8yBqaIU+MdOVO1vpAqFSU9JjHSeQ/Pxw X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:cb89:0:b0:586:50cf:e13f with SMTP id n131-20020a0dcb89000000b0058650cfe13fmr303997ywd.1.1698186340731; Tue, 24 Oct 2023 15:25:40 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:42 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-40-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 39/50] perf maps: Get map before returning in maps__find From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:33 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677574365525555 X-GMAIL-MSGID: 1780677574365525555 Finding a map is done under a lock, returning the map without a reference count means it can be removed without notice and causing uses after free. Grab a reference count to the map within the lock region and return this. Fix up locations that need a map__put following this. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/tests/dwarf-unwind.c | 1 + tools/perf/tests/vmlinux-kallsyms.c | 5 ++--- tools/perf/util/bpf-event.c | 1 + tools/perf/util/event.c | 4 ++-- tools/perf/util/machine.c | 22 ++++++++-------------- tools/perf/util/maps.c | 17 ++++++++++------- tools/perf/util/symbol.c | 3 ++- 7 files changed, 26 insertions(+), 27 deletions(-) diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86/tests/dwarf-unwind.c index 5bfec3345d59..c05c0a85dad4 100644 --- a/tools/perf/arch/x86/tests/dwarf-unwind.c +++ b/tools/perf/arch/x86/tests/dwarf-unwind.c @@ -34,6 +34,7 @@ static int sample_ustack(struct perf_sample *sample, } stack_size = map__end(map) - sp; + map__put(map); stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size; memcpy(buf, (void *) sp, stack_size); diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux-kallsyms.c index 822f893e67d5..e808e6fc8f76 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -151,10 +151,8 @@ static int test__vmlinux_matches_kallsyms_cb2(struct map *map, void *data) u64 mem_end = map__unmap_ip(args->vmlinux_map, map__end(map)); pair = maps__find(args->kallsyms.kmaps, mem_start); - if (pair == NULL || map__priv(pair)) - return 0; - if (map__start(pair) == mem_start) { + if (pair != NULL && !map__priv(pair) && map__start(pair) == mem_start) { struct dso *dso = map__dso(map); if (!args->header_printed) { @@ -170,6 +168,7 @@ static int test__vmlinux_matches_kallsyms_cb2(struct map *map, void *data) pr_info(" %s\n", dso->name); map__set_priv(pair, 1); } + map__put(pair); return 0; } diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 830711cae30d..d07fd5ffa823 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -63,6 +63,7 @@ static int machine__process_bpf_event_load(struct machine *machine, dso->bpf_prog.id = id; dso->bpf_prog.sub_id = i; dso->bpf_prog.env = env; + map__put(map); } } return 0; diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 68f45e9e63b6..198903157f9e 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -511,7 +511,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *ma struct addr_location al; addr_location__init(&al); - al.map = map__get(maps__find(machine__kernel_maps(machine), tp->addr)); + al.map = maps__find(machine__kernel_maps(machine), tp->addr); if (al.map && map__load(al.map) >= 0) { al.addr = map__map_ip(al.map, tp->addr); al.sym = map__find_symbol(al.map, al.addr); @@ -641,7 +641,7 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, return NULL; } al->maps = maps__get(maps); - al->map = map__get(maps__find(maps, al->addr)); + al->map = maps__find(maps, al->addr); if (al->map != NULL) { /* * Kernel maps might be changed when loading symbols so loading diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index ab345604f274..1112a9dbb21a 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -897,7 +897,6 @@ static int machine__process_ksymbol_register(struct machine *machine, struct symbol *sym; struct dso *dso; struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr); - bool put_map = false; int err = 0; if (!map) { @@ -914,12 +913,6 @@ static int machine__process_ksymbol_register(struct machine *machine, err = -ENOMEM; goto out; } - /* - * The inserted map has a get on it, we need to put to release - * the reference count here, but do it after all accesses are - * done. - */ - put_map = true; if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) { dso->binary_type = DSO_BINARY_TYPE__OOL; dso->data.file_size = event->ksymbol.len; @@ -953,8 +946,7 @@ static int machine__process_ksymbol_register(struct machine *machine, } dso__insert_symbol(dso, sym); out: - if (put_map) - map__put(map); + map__put(map); return err; } @@ -978,7 +970,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine, if (sym) dso__delete_symbol(dso, sym); } - + map__put(map); return 0; } @@ -1006,11 +998,11 @@ int machine__process_text_poke(struct machine *machine, union perf_event *event, perf_event__fprintf_text_poke(event, machine, stdout); if (!event->text_poke.new_len) - return 0; + goto out; if (cpumode != PERF_RECORD_MISC_KERNEL) { pr_debug("%s: unsupported cpumode - ignoring\n", __func__); - return 0; + goto out; } if (dso) { @@ -1033,7 +1025,8 @@ int machine__process_text_poke(struct machine *machine, union perf_event *event, pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n", event->text_poke.addr); } - +out: + map__put(map); return 0; } @@ -1301,9 +1294,10 @@ static int machine__map_x86_64_entry_trampolines_cb(struct map *map, void *data) return 0; dest_map = maps__find(args->kmaps, map__pgoff(map)); - if (dest_map != map) + if (RC_CHK_ACCESS(dest_map) != RC_CHK_ACCESS(map)) map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map))); + map__put(dest_map); args->found = true; return 0; } diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 06fdd8a7c2a2..28facfdac1d7 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -487,15 +487,18 @@ void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) { struct map *map = maps__find(maps, addr); + struct symbol *result = NULL; /* Ensure map is loaded before using map->map_ip */ if (map != NULL && map__load(map) >= 0) { - if (mapp != NULL) - *mapp = map; // TODO: map_put on else path when find returns a get. - return map__find_symbol(map, map__map_ip(map, addr)); - } + if (mapp) + *mapp = map; - return NULL; + result = map__find_symbol(map, map__map_ip(map, addr)); + if (!mapp) + map__put(map); + } + return result; } struct maps__find_symbol_by_name_args { @@ -539,7 +542,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) if (ams->addr < map__start(ams->ms.map) || ams->addr >= map__end(ams->ms.map)) { if (maps == NULL) return -1; - ams->ms.map = maps__find(maps, ams->addr); // TODO: map_get + ams->ms.map = maps__find(maps, ams->addr); if (ams->ms.map == NULL) return -1; } @@ -848,7 +851,7 @@ struct map *maps__find(struct maps *maps, u64 ip) sizeof(*mapp), map__addr_cmp); if (mapp) - result = *mapp; // map__get(*mapp); + result = map__get(*mapp); done = true; } up_read(maps__lock(maps)); diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 30da8a405d11..ad4819a24320 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -757,7 +757,6 @@ static int dso__load_all_kallsyms(struct dso *dso, const char *filename) static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) { - struct map *curr_map; struct symbol *pos; int count = 0; struct rb_root_cached old_root = dso->symbols; @@ -770,6 +769,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) *root = RB_ROOT_CACHED; while (next) { + struct map *curr_map; struct dso *curr_map_dso; char *module; @@ -796,6 +796,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) pos->end -= map__start(curr_map) - map__pgoff(curr_map); symbols__insert(&curr_map_dso->symbols, pos); ++count; + map__put(curr_map); } /* Symbols have been adjusted */ From patchwork Tue Oct 24 22:23:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157738 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238031vqx; Tue, 24 Oct 2023 15:27:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEZ1ec+gkBunckj9em+GjDxaW6CLTrupLmOjh/Y/EbHJrTq5CDhAOV3iqnntlO/Tc5ftFyn X-Received: by 2002:a17:903:11cf:b0:1ca:a07b:365 with SMTP id q15-20020a17090311cf00b001caa07b0365mr21979595plh.16.1698186450539; Tue, 24 Oct 2023 15:27:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186450; cv=none; d=google.com; s=arc-20160816; b=Bpel31IIkZJe0cWd+ffNMo7gFXU7cwqhjfRgiqcjDlZ5S4iBuLgtpSCQLKwGOazLqL hvRM1klx0AbQA7zYtqkl6Q7Wk3vO9RIqfbssqvDV/TSiWTAeON7OwwSWhP5fsHIWER83 8d4cVp1dxf7USIFSptuCo72wcWqLdtRbo4rK/ff1lgHKxB3o+1NmFw9LWiG0l5JzgRge S1Ovvs8bQMb7/PjTBD0DD+e7fhou4cqOcyaqICO6covTUQIolgxmVfl6nmmbi7nzVnUr A56vnESNB29DYVD8lVW0rt1LOEqy8MNBSGb34QJZUtJ9Cny8fcUtAEHUPgBCpcGxpHYb vY7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=RtLWFp/Ay9yY6/sZZC/VcUQilprFkB+Z9rXtdAZQ2Hk=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=GQq1MQBT34d2urQ0dWD4c3W1tV6KrDG4SWz6U8DLjgKq2TL4tHjwZLBuniIi2vKoTm Yn5TaHTMo1KYnABR3NMgUMgE7SgqiDUrZ3PvFSZ8YTL/SRByLPRpbWaNI+pqceZBXpWc GN1itG7HwYeV0cDbwPdPdQRPLkw8p+OglpqQkC1Hwz4SDMcPHm9NP9yE4kh6qt3ySbFP g81LfIrL4/gNqbABA8KRKbp+oo2Rkv32r5QfBIY7QILxiugO/zJJMki/qsdvls/cgY4O o1rPl6vGv5qoYVRPNkJuL0mrCX9ZU+EnleazDa5uqs0SidBk37e1m1cUGeWKohST6jca 6d7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vPvtXrnG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id p17-20020a631e51000000b005a073e0cc9asi9039725pgm.803.2023.10.24.15.27.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vPvtXrnG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id D14CE801E1FC; Tue, 24 Oct 2023 15:27:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344519AbjJXW05 (ORCPT + 27 others); Tue, 24 Oct 2023 18:26:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344618AbjJXW0X (ORCPT ); Tue, 24 Oct 2023 18:26:23 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 035A71BDF for ; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a3a38b96cso5574231276.0 for ; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186343; x=1698791143; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=RtLWFp/Ay9yY6/sZZC/VcUQilprFkB+Z9rXtdAZQ2Hk=; b=vPvtXrnG8y52U1DnsKfD3FlNUA/UY/g3ZmVg0L5CYQShxdXUf8wRf3pZB+jRG8gEmb sJ/8waadxrtY6OZHqiBYN7W7/0CejexF/FSHZJ5XkVbnO5Txt67fZLgV24iwCDBxm+Bb C7p47VBoIvJNXtDN/QboNm6yziMnKhwmtyVo8SvDLVcfWkWtB4hups1Jd77XqvOgu9a0 7uIho2d+zfBA2E8bAOqf9WFdFWtyorfkBNUcDYdinqWyIi8UHqWfq9ID0ZHHgDaqkN/5 Lpv+xLEdSbEh8cmKnuWm5zjYWHWWkVXrJSXtIRX8YHejpeMNJcu1yZqF+cEc899WtyS2 dgrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186343; x=1698791143; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RtLWFp/Ay9yY6/sZZC/VcUQilprFkB+Z9rXtdAZQ2Hk=; b=i9gISZUFIFjW9rHM1GwA+9taO5F3yXjNgPIiZlFxGzMlmSCDBbER2mmr9ZJikKh0gW 6CPAf0GcGt/8Ak6PyIrCTfDeCsoeARNJOxQ2unS1M/VXCJuD96n8TWrHTFid4mHWe1f0 YSAHQk0JKTcG4pB5HiVHzo62G7+Kh3ClDqNZkVfFeU9AULzUkA3TvP8iDPzAHuM9QlfD dVd2iJP0WaGgbTCBFpgfS53zy0UT5iU25ImA5G7MesamaeYYjhVr/C7SJV794IAMeP6n d+z7UQfJ8neZy7R5quUJ9MjPUc7KupUF+l/AsfdOVkFyeVGFW2946NgPYY0EdaXaDvHH oDXw== X-Gm-Message-State: AOJu0YwCfHK6K2tghZ1emHyTHLG3Gq4qcWTZ+lhZgIPRaFj+3VVygGti cuNIL2LXsfkZa6MjKRdvNeTNbJTwkr0U X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1888:b0:d9a:520f:1988 with SMTP id cj8-20020a056902188800b00d9a520f1988mr364100ybb.4.1698186343062; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:43 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-41-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 40/50] perf maps: Get map before returning in maps__find_by_name From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:20 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677555750120266 X-GMAIL-MSGID: 1780677555750120266 Finding a map is done under a lock, returning the map without a reference count means it can be removed without notice and causing uses after free. Grab a reference count to the map within the lock region and return this. Fix up locations that need a map__put following this. Signed-off-by: Ian Rogers --- tools/perf/tests/vmlinux-kallsyms.c | 5 +++-- tools/perf/util/machine.c | 6 ++++-- tools/perf/util/maps.c | 6 +++--- tools/perf/util/probe-event.c | 1 + tools/perf/util/symbol-elf.c | 4 +++- tools/perf/util/symbol.c | 18 +++++++++++------- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux-kallsyms.c index e808e6fc8f76..fecbf851bb2e 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -131,9 +131,10 @@ static int test__vmlinux_matches_kallsyms_cb1(struct map *map, void *data) struct map *pair = maps__find_by_name(args->kallsyms.kmaps, (dso->kernel ? dso->short_name : dso->name)); - if (pair) + if (pair) { map__set_priv(pair, 1); - else { + map__put(pair); + } else { if (!args->header_printed) { pr_info("WARN: Maps only in vmlinux:\n"); args->header_printed = true; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 1112a9dbb21a..d6b3f84cb935 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1538,8 +1538,10 @@ static int maps__set_module_path(struct maps *maps, const char *path, struct kmo return 0; long_name = strdup(path); - if (long_name == NULL) + if (long_name == NULL) { + map__put(map); return -ENOMEM; + } dso = map__dso(map); dso__set_long_name(dso, long_name, true); @@ -1553,7 +1555,7 @@ static int maps__set_module_path(struct maps *maps, const char *path, struct kmo dso->symtab_type++; dso->comp = m->comp; } - + map__put(map); return 0; } diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 28facfdac1d7..8a8c1f216b86 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -885,7 +885,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) struct dso *dso = map__dso(maps__maps_by_name(maps)[i]); if (dso && strcmp(dso->short_name, name) == 0) { - result = maps__maps_by_name(maps)[i]; // TODO: map__get + result = map__get(maps__maps_by_name(maps)[i]); done = true; } } @@ -897,7 +897,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) sizeof(*mapp), map__strcmp_name); if (mapp) { - result = *mapp; // TODO: map__get + result = map__get(*mapp); i = mapp - maps__maps_by_name(maps); RC_CHK_ACCESS(maps)->last_search_by_name_idx = i; } @@ -922,7 +922,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) struct dso *dso = map__dso(pos); if (dso && strcmp(dso->short_name, name) == 0) { - result = pos; // TODO: map__get + result = map__get(pos); break; } } diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index a1a796043691..be71abe8b9b0 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -358,6 +358,7 @@ static int kernel_get_module_dso(const char *module, struct dso **pdso) map = maps__find_by_name(machine__kernel_maps(host_machine), module_name); if (map) { dso = map__dso(map); + map__put(map); goto found; } pr_debug("Failed to find module %s.\n", module); diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 4b934ed3bfd1..5990e3fabdb5 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1470,8 +1470,10 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, dso__set_loaded(curr_dso); *curr_mapp = curr_map; *curr_dsop = curr_dso; - } else + } else { *curr_dsop = map__dso(curr_map); + map__put(curr_map); + } return 0; } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index ad4819a24320..3f31e868d883 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -814,7 +814,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, struct map *initial_map) { struct machine *machine; - struct map *curr_map = initial_map; + struct map *curr_map = map__get(initial_map); struct symbol *pos; int count = 0, moved = 0; struct rb_root_cached *root = &dso->symbols; @@ -858,13 +858,14 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, dso__set_loaded(curr_map_dso); } + map__zput(curr_map); curr_map = maps__find_by_name(kmaps, module); if (curr_map == NULL) { pr_debug("%s/proc/{kallsyms,modules} " "inconsistency while looking " "for \"%s\" module!\n", machine->root_dir, module); - curr_map = initial_map; + curr_map = map__get(initial_map); goto discard_symbol; } curr_map_dso = map__dso(curr_map); @@ -888,7 +889,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, * symbols at this point. */ goto discard_symbol; - } else if (curr_map != initial_map) { + } else if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map)) { char dso_name[PATH_MAX]; struct dso *ndso; @@ -899,7 +900,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, } if (count == 0) { - curr_map = initial_map; + map__zput(curr_map); + curr_map = map__get(initial_map); goto add_symbol; } @@ -913,6 +915,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, kernel_range++); ndso = dso__new(dso_name); + map__zput(curr_map); if (ndso == NULL) return -1; @@ -926,6 +929,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY); if (maps__insert(kmaps, curr_map)) { + map__zput(curr_map); dso__put(ndso); return -1; } @@ -936,7 +940,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, pos->end -= delta; } add_symbol: - if (curr_map != initial_map) { + if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map)) { struct dso *curr_map_dso = map__dso(curr_map); rb_erase_cached(&pos->rb_node, root); @@ -951,12 +955,12 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, symbol__delete(pos); } - if (curr_map != initial_map && + if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map) && dso->kernel == DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(maps__machine(kmaps))) { dso__set_loaded(map__dso(curr_map)); } - + map__put(curr_map); return count + moved; } From patchwork Tue Oct 24 22:23:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157746 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238408vqx; Tue, 24 Oct 2023 15:28:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHWeJBIAaRJJKM74X8YXXHKD2E1phz5Q4G/FGr10PdT4E8+mwAIv++Ek6SEuHSCv5o6yyju X-Received: by 2002:a05:6358:410:b0:168:e61d:8db2 with SMTP id 16-20020a056358041000b00168e61d8db2mr4522902rwd.8.1698186508005; Tue, 24 Oct 2023 15:28:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186507; cv=none; d=google.com; s=arc-20160816; b=Qvfr9O4JKlK2JiC8b2QC4zQYdrbYenc4H8S1Xz6MVjjvA2+lFE2Lg8pTxLjscrtx/o CvsDOhXC2aHXNKK7cbRVCbxnq3iHxf+U84DoGqY+jMPMD1A3kwY7W7wLZbw3OdVc6LmF x96XrAWhrRu0qCCwVQ/II0cLdaYwW/BIs/xzKWhL+oIY3ypvgiZlP0QkT0H5vBBBWe78 x2tEJV60kKXv6RSBp6lidPSiPBxofvMPjZth9O0cUrLfhY3TwDMKIarsnakKdu82uh+n q4YEXOATscGnNsqZvgzh83fMRcMRCFmcfbEmWsBHCsyGOq41Xo0kSkzbhvbvGhatj7zg 52Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=f/kNgXH1Uh+lkDKn4udeU01EF+mxZdpavmj5nGxyL2A=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=uqG8+d0T/FBYUOS0Q90oMVKHl5gkDNxdzWIZxAS0REe0KxqO3l3rKKvBs4HQh/q6rF qels1OnCmtr16SS7PIBZAN1hZjXkRJfslJfgO5zu6F5yiLS9+YrcIo8+pLwXMD1NNh1r 6jzBDgIP1FGZN0fY9yCdXZKs2qCXeXxMJqtwW7qyFncg2HkkLZDtYBnywj+BDn7pX7b1 P5YCOlIGojJWtelZN9QcLYE5Vy+uJZfRPjvstveFkmSeBAl3JYi8pwn3V2Z14WJ79u0k ZuUCm4tlNI4iXAr5XGqJw7pfBTbHtCC2j/O9vPETcmTukn14txGcmHwoKQAtBFHQPYjL fvAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XvJXOZLy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id i123-20020a636d81000000b005b8f0c8ddbasi1502756pgc.243.2023.10.24.15.28.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XvJXOZLy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id B44028024B12; Tue, 24 Oct 2023 15:28:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344655AbjJXW2A (ORCPT + 27 others); Tue, 24 Oct 2023 18:28:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344660AbjJXW1f (ORCPT ); Tue, 24 Oct 2023 18:27:35 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F163510F4 for ; Tue, 24 Oct 2023 15:25:45 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a8ee6a1801so63570507b3.3 for ; Tue, 24 Oct 2023 15:25:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186345; x=1698791145; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=f/kNgXH1Uh+lkDKn4udeU01EF+mxZdpavmj5nGxyL2A=; b=XvJXOZLyXI3pgqCPjcPyjhB/+6CS50YS/z6g04SRTQdp3wZ+Ct5TwGaohC3c+kvMFW VxPrRlzdeSmaqV44r0kHCXNm5yBNk3UfUCVXUMmtcJwc650v8sdh1I0WVDFB/OwZ3x2o 3KbU1FRFVi+WAxU9u3xo4sTczU97ySsf7Y544dtcz/lbS6TLSCRoYhnlpBwHOeeZH5Cy sSOdnBbDOrhzdbRlwSBujoXqW/mk8bw7/x3yElFW+MPDWEHiAtvtx3Zis5Hih/3T1Z1L jTPQTU0iBufLjZTqaBXalfjhpwfGuG8Bi+Pl+jeCDD7kk19uxzHogOYK8hlwrgZL6BLU wvFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186345; x=1698791145; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=f/kNgXH1Uh+lkDKn4udeU01EF+mxZdpavmj5nGxyL2A=; b=cH+RXUP7ixVClAqPRGmJvwT+sxHy31FFqPBOZRjJMPIEEs0BUwkcbDn5xz6vYxYt1G w6VFUsb/I74kia+TPArsQ3ZEM7M+5c16FPZm5c/QNNnlm8BlgNg7LusIhwfzqXA8lqH5 ma2VLOGOC0srJO4iWG7c8wgJCFF5jfZ0CJAM5SE9zC7gdLAH8zqjjMGa879gg96+7uLS ciPtCgZICvD9cTxrOSZzxQlYJQELQGYkrAJzPSm3tr34R0/Gsvdr1T/7KPXw8z1aEI/W gZzm1eZjw+Z+TtDijsQBByGUDBzgCNXoInYI3yGee6SmXwclk6aLz/VOrAUIaKZqQU9H 9b/A== X-Gm-Message-State: AOJu0YwtuKPwK+L0N7JN0XmCoBuuFO7TS26s7Pxn3b9LLO7Zm0ww5lio Yg+VpqIc9npp/rUSOzc84YROZGvmGvVW X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:aa45:0:b0:da0:3e46:8ba5 with SMTP id s63-20020a25aa45000000b00da03e468ba5mr57420ybi.8.1698186345124; Tue, 24 Oct 2023 15:25:45 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:44 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-42-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 41/50] perf maps: Get map before returning in maps__find_next_entry From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:25 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677616112376488 X-GMAIL-MSGID: 1780677616112376488 Finding a map is done under a lock, returning the map without a reference count means it can be removed without notice and causing uses after free. Grab a reference count to the map within the lock region and return this. Fix up locations that need a map__put following this. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 4 +++- tools/perf/util/maps.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index d6b3f84cb935..42d73f00f9c1 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1758,8 +1758,10 @@ int machine__create_kernel_maps(struct machine *machine) struct map *next = maps__find_next_entry(machine__kernel_maps(machine), machine__kernel_map(machine)); - if (next) + if (next) { machine__set_kernel_mmap(machine, start, map__start(next)); + map__put(next); + } } out_put: diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 8a8c1f216b86..b3937e734cbf 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -942,7 +942,7 @@ struct map *maps__find_next_entry(struct maps *maps, struct map *map) down_read(maps__lock(maps)); i = maps__by_address_index(maps, map); if (i < maps__nr_maps(maps)) - result = maps__maps_by_address(maps)[i]; // TODO: map__get + result = map__get(maps__maps_by_address(maps)[i]); up_read(maps__lock(maps)); return result; From patchwork Tue Oct 24 22:23:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157759 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2241676vqx; Tue, 24 Oct 2023 15:36:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFfDSBDIBdvJMbiXk5DSV5wEQSWYJhUlGVnc5cUNkVZU92LmLrRPXkBYrNkUgAe1UDKb86A X-Received: by 2002:a05:6358:410:b0:168:e61d:8db2 with SMTP id 16-20020a056358041000b00168e61d8db2mr4534799rwd.8.1698187004888; Tue, 24 Oct 2023 15:36:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187004; cv=none; d=google.com; s=arc-20160816; b=fVbT1vL9EFraVwBdE5+aR9DYnmqEEZ7d8MeZoqu1yNYQVPT0o5OFVq7TdiOI3KDxzA NA+v1enhTohxqV9Mlv4pWeyBt8pYM/7lquW+/L9mrwsAYLAnj6uItaLw198CDSldSr4U qXU3Xi0feyIBLMqX6TD+44D4nElgadB3bjxuEOEca7FCIZZgbsMVh1TjVbvTtSac7Zl3 wGlrOaYztsVv7qYtlSivVV7GlfbSJeMUwZ4feVuSnTK8rdP5kh1HDIQtfP8wHHArcvoT Te3pVHkZia8JIXtT2fE6lI5yTXDTWwnEjfT3wPZ7fHhSl5us75gcgr7yL8rTqp5IZAm0 5cVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=fIA52HHCCZO7HTocHOlHj5fS36x5lLk23UHRc4pwoQY=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=g9Bp/fKC1/tcr3sSB1/bsCYcWJlfEGQcPOWmmtxBCpiG6qGBMLpcVBdhLPqpkqTz27 LzWApFUbi5iV5c/7I1S9eGjh8Vmwk6I3aDmGY7xYHjKOTSVQyK2hQd6vSi32qLEPnD6e CX9/9K1Dc+sXjvhlD6Ko+OiTzLuuDHHbOWgWcmdPjE+pg3wsQZiM6+cg6M7jFi3/qeZN uG6mjamBr8tdbHIFImsH3NVifBxyy3ASC2johqanyWPUF2VUtJC+5Dc92ouHSysLPal9 gVTHjNBxe4pdR4GlrlsiE6rBLNRu+bxhTfWEfPgg8rxtIoa2j5W9NnSltpwzdyZjNwBh a/HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oFMx+vPQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id bw37-20020a056a0204a500b005b861397405si9384728pgb.134.2023.10.24.15.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:36:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oFMx+vPQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 049F68021D07; Tue, 24 Oct 2023 15:36:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344697AbjJXWg1 (ORCPT + 27 others); Tue, 24 Oct 2023 18:36:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344696AbjJXWgJ (ORCPT ); Tue, 24 Oct 2023 18:36:09 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E04E1BE3 for ; Tue, 24 Oct 2023 15:25:48 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da05b786f1dso668184276.2 for ; Tue, 24 Oct 2023 15:25:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186347; x=1698791147; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=fIA52HHCCZO7HTocHOlHj5fS36x5lLk23UHRc4pwoQY=; b=oFMx+vPQa8FtCEvkpbNYv9X9kErj0tvDEfBsgIscQTQkG6H9QjJHOetVRx6DRTw6xC fZ9r+A4diSqS+fj+SBJiGidH81hR2436yp60T4CFsuA8Va81ls06cZtKf7cBBsH10TNi FuV6BRrmLxp51mDT6548EXF7bnp7YrLoUDYmGz29gRmDywa5I+JWgg8V6UJWCrgweIvr trZwnyHdm2LMtFW+ZUhpRfjX5ItZUVXKNIm8TsBAYis336bcWTasxgfOqA0boTUYPND4 3nsIRYWQZCZfCK2wfI5magLAEo5Va+jxvjWkAttwMHPOA265abtlgi2EA0gYQ5ntHN6r 8znQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186347; x=1698791147; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fIA52HHCCZO7HTocHOlHj5fS36x5lLk23UHRc4pwoQY=; b=ovxlWFxz1nMEIYAlGXnhSdFKNs2AXLmqzTu7eSvXyuG+dII1xwHTzQGHHAYDue9NVP a6AFkbOcgR2gSgFGIX8ZT/Y8RDEqr+0kWGeHq5w6QM4VB1CvEjZChjoPypipQm/GUF+O P1xbUwaacJ8Hl/isVaOgT7PJIw96HVm4ZSod/PAuPmopJj2wXrsQFCZgK9YKQcYhE5QN iCrtuC/Brp++USHNF3v+Zgl92zXGFYJ/6VnqyZmDY9ZkPKZui9u9AjjUvE1eWVJkRLPM NuyRoJHDm4c8FTDJX5MhPXZZYcwSKy1+FgydxTTocGahLQzi1gRO62bchKE9WL/Wg0Ov dpuQ== X-Gm-Message-State: AOJu0YwTglczqri5u/0q1WOMcGveZp3OKP8kwi6EvE8nIyw5jSktGYmo PmjcYrH8CkxGsWqoOHpu2XX3oWNh9TRa X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:40c7:0:b0:da0:289e:c056 with SMTP id n190-20020a2540c7000000b00da0289ec056mr87188yba.8.1698186347464; Tue, 24 Oct 2023 15:25:47 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:45 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-43-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 42/50] perf maps: Hide maps internals From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:36:42 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678136369787297 X-GMAIL-MSGID: 1780678136369787297 Move the struct into the C file. Add maps__equal to work around exposing the struct for reference count checking. Add accessors for the unwind_libunwind_ops. Move maps_list_node to its only use in symbol.c. Signed-off-by: Ian Rogers --- tools/perf/tests/thread-maps-share.c | 8 +- tools/perf/util/callchain.c | 2 +- tools/perf/util/maps.c | 96 +++++++++++++++++++++++ tools/perf/util/maps.h | 97 +++--------------------- tools/perf/util/symbol.c | 10 +++ tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- tools/perf/util/unwind-libunwind.c | 7 +- 8 files changed, 123 insertions(+), 101 deletions(-) diff --git a/tools/perf/tests/thread-maps-share.c b/tools/perf/tests/thread-maps-share.c index 7fa6f7c568e2..e9ecd30a5c05 100644 --- a/tools/perf/tests/thread-maps-share.c +++ b/tools/perf/tests/thread-maps-share.c @@ -46,9 +46,9 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 4); /* test the maps pointer is shared */ - TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t1))); - TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t2))); - TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t3))); + TEST_ASSERT_VAL("maps don't match", maps__equal(maps, thread__maps(t1))); + TEST_ASSERT_VAL("maps don't match", maps__equal(maps, thread__maps(t2))); + TEST_ASSERT_VAL("maps don't match", maps__equal(maps, thread__maps(t3))); /* * Verify the other leader was created by previous call. @@ -73,7 +73,7 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s other_maps = thread__maps(other); TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(other_maps)), 2); - TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(other_maps, thread__maps(other_leader))); + TEST_ASSERT_VAL("maps don't match", maps__equal(other_maps, thread__maps(other_leader))); /* release thread group */ thread__put(t3); diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 99cce43ba152..4878879f0d5c 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -1157,7 +1157,7 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node * if (al->map == NULL) goto out; } - if (RC_CHK_EQUAL(al->maps, machine__kernel_maps(machine))) { + if (maps__equal(al->maps, machine__kernel_maps(machine))) { if (machine__is_host(machine)) { al->cpumode = PERF_RECORD_MISC_KERNEL; al->level = 'k'; diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index b3937e734cbf..41e9e39b1b4c 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -6,9 +6,63 @@ #include "dso.h" #include "map.h" #include "maps.h" +#include "rwsem.h" #include "thread.h" #include "ui/ui.h" #include "unwind.h" +#include + +/* + * Locking/sorting note: + * + * Sorting is done with the write lock, iteration and binary searching happens + * under the read lock requiring being sorted. There is a race between sorting + * releasing the write lock and acquiring the read lock for iteration/searching + * where another thread could insert and break the sorting of the maps. In + * practice inserting maps should be rare meaning that the race shouldn't lead + * to live lock. Removal of maps doesn't break being sorted. + */ + +DECLARE_RC_STRUCT(maps) { + struct rw_semaphore lock; + /** + * @maps_by_address: array of maps sorted by their starting address if + * maps_by_address_sorted is true. + */ + struct map **maps_by_address; + /** + * @maps_by_name: optional array of maps sorted by their dso name if + * maps_by_name_sorted is true. + */ + struct map **maps_by_name; + struct machine *machine; +#ifdef HAVE_LIBUNWIND_SUPPORT + void *addr_space; + const struct unwind_libunwind_ops *unwind_libunwind_ops; +#endif + refcount_t refcnt; + /** + * @nr_maps: number of maps_by_address, and possibly maps_by_name, + * entries that contain maps. + */ + unsigned int nr_maps; + /** + * @nr_maps_allocated: number of entries in maps_by_address and possibly + * maps_by_name. + */ + unsigned int nr_maps_allocated; + /** + * @last_search_by_name_idx: cache of last found by name entry's index + * as frequent searches for the same dso name are common. + */ + unsigned int last_search_by_name_idx; + /** @maps_by_address_sorted: is maps_by_address sorted. */ + bool maps_by_address_sorted; + /** @maps_by_name_sorted: is maps_by_name sorted. */ + bool maps_by_name_sorted; + /** @ends_broken: does the map contain a map where end values are unset/unsorted? */ + bool ends_broken; +}; static void check_invariants(const struct maps *maps __maybe_unused) { @@ -103,6 +157,43 @@ static void maps__set_maps_by_name_sorted(struct maps *maps, bool value) RC_CHK_ACCESS(maps)->maps_by_name_sorted = value; } +struct machine *maps__machine(const struct maps *maps) +{ + return RC_CHK_ACCESS(maps)->machine; +} + +unsigned int maps__nr_maps(const struct maps *maps) +{ + return RC_CHK_ACCESS(maps)->nr_maps; +} + +refcount_t *maps__refcnt(struct maps *maps) +{ + return &RC_CHK_ACCESS(maps)->refcnt; +} + +#ifdef HAVE_LIBUNWIND_SUPPORT +void *maps__addr_space(const struct maps *maps) +{ + return RC_CHK_ACCESS(maps)->addr_space; +} + +void maps__set_addr_space(struct maps *maps, void *addr_space) +{ + RC_CHK_ACCESS(maps)->addr_space = addr_space; +} + +const struct unwind_libunwind_ops *maps__unwind_libunwind_ops(const struct maps *maps) +{ + return RC_CHK_ACCESS(maps)->unwind_libunwind_ops; +} + +void maps__set_unwind_libunwind_ops(struct maps *maps, const struct unwind_libunwind_ops *ops) +{ + RC_CHK_ACCESS(maps)->unwind_libunwind_ops = ops; +} +#endif + static struct rw_semaphore *maps__lock(struct maps *maps) { /* @@ -440,6 +531,11 @@ bool maps__empty(struct maps *maps) return maps__nr_maps(maps) == 0; } +bool maps__equal(struct maps *a, struct maps *b) +{ + return RC_CHK_EQUAL(a, b); +} + int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data) { bool done = false; diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index df9dd5a0e3c0..4bcba136ffe5 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -3,80 +3,15 @@ #define __PERF_MAPS_H #include -#include #include #include #include -#include "rwsem.h" -#include struct ref_reloc_sym; struct machine; struct map; struct maps; -struct map_list_node { - struct list_head node; - struct map *map; -}; - -static inline struct map_list_node *map_list_node__new(void) -{ - return malloc(sizeof(struct map_list_node)); -} - -/* - * Locking/sorting note: - * - * Sorting is done with the write lock, iteration and binary searching happens - * under the read lock requiring being sorted. There is a race between sorting - * releasing the write lock and acquiring the read lock for iteration/searching - * where another thread could insert and break the sorting of the maps. In - * practice inserting maps should be rare meaning that the race shouldn't lead - * to live lock. Removal of maps doesn't break being sorted. - */ - -DECLARE_RC_STRUCT(maps) { - struct rw_semaphore lock; - /** - * @maps_by_address: array of maps sorted by their starting address if - * maps_by_address_sorted is true. - */ - struct map **maps_by_address; - /** - * @maps_by_name: optional array of maps sorted by their dso name if - * maps_by_name_sorted is true. - */ - struct map **maps_by_name; - struct machine *machine; -#ifdef HAVE_LIBUNWIND_SUPPORT - void *addr_space; - const struct unwind_libunwind_ops *unwind_libunwind_ops; -#endif - refcount_t refcnt; - /** - * @nr_maps: number of maps_by_address, and possibly maps_by_name, - * entries that contain maps. - */ - unsigned int nr_maps; - /** - * @nr_maps_allocated: number of entries in maps_by_address and possibly - * maps_by_name. - */ - unsigned int nr_maps_allocated; - /** - * @last_search_by_name_idx: cache of last found by name entry's index - * as frequent searches for the same dso name are common. - */ - unsigned int last_search_by_name_idx; - /** @maps_by_address_sorted: is maps_by_address sorted. */ - bool maps_by_address_sorted; - /** @maps_by_name_sorted: is maps_by_name sorted. */ - bool maps_by_name_sorted; - /** @ends_broken: does the map contain a map where end values are unset/unsorted? */ - bool ends_broken; -}; - #define KMAP_NAME_LEN 256 struct kmap { @@ -100,36 +35,22 @@ static inline void __maps__zput(struct maps **map) #define maps__zput(map) __maps__zput(&map) +bool maps__equal(struct maps *a, struct maps *b); + /* Iterate over map calling cb for each entry. */ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data); /* Iterate over map removing an entry if cb returns true. */ void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data); -static inline struct machine *maps__machine(struct maps *maps) -{ - return RC_CHK_ACCESS(maps)->machine; -} - -static inline unsigned int maps__nr_maps(const struct maps *maps) -{ - return RC_CHK_ACCESS(maps)->nr_maps; -} - -static inline refcount_t *maps__refcnt(struct maps *maps) -{ - return &RC_CHK_ACCESS(maps)->refcnt; -} +struct machine *maps__machine(const struct maps *maps); +unsigned int maps__nr_maps(const struct maps *maps); +refcount_t *maps__refcnt(struct maps *maps); #ifdef HAVE_LIBUNWIND_SUPPORT -static inline void *maps__addr_space(struct maps *maps) -{ - return RC_CHK_ACCESS(maps)->addr_space; -} - -static inline const struct unwind_libunwind_ops *maps__unwind_libunwind_ops(const struct maps *maps) -{ - return RC_CHK_ACCESS(maps)->unwind_libunwind_ops; -} +void *maps__addr_space(const struct maps *maps); +void maps__set_addr_space(struct maps *maps, void *addr_space); +const struct unwind_libunwind_ops *maps__unwind_libunwind_ops(const struct maps *maps); +void maps__set_unwind_libunwind_ops(struct maps *maps, const struct unwind_libunwind_ops *ops); #endif size_t maps__fprintf(struct maps *maps, FILE *fp); diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 3f31e868d883..a264d152a0ef 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -63,6 +63,16 @@ struct symbol_conf symbol_conf = { .res_sample = 0, }; +struct map_list_node { + struct list_head node; + struct map *map; +}; + +static struct map_list_node *map_list_node__new(void) +{ + return malloc(sizeof(struct map_list_node)); +} + static enum dso_binary_type binary_type_symtab[] = { DSO_BINARY_TYPE__KALLSYMS, DSO_BINARY_TYPE__GUEST_KALLSYMS, diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 07b158aa3e44..c59ab4d79163 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -383,7 +383,7 @@ static int thread__clone_maps(struct thread *thread, struct thread *parent, bool if (thread__pid(thread) == thread__pid(parent)) return thread__prepare_access(thread); - if (RC_CHK_ACCESS(thread__maps(thread)) == RC_CHK_ACCESS(thread__maps(parent))) { + if (maps__equal(thread__maps(thread), thread__maps(parent))) { pr_debug("broken map groups on thread %d/%d parent %d/%d\n", thread__pid(thread), thread__tid(thread), thread__pid(parent), thread__tid(parent)); diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c index 228f1565bd0b..b69dc3a447db 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -706,7 +706,7 @@ static int _unwind__prepare_access(struct maps *maps) { void *addr_space = unw_create_addr_space(&accessors, 0); - RC_CHK_ACCESS(maps)->addr_space = addr_space; + maps__set_addr_space(maps, addr_space); if (!addr_space) { pr_err("unwind: Can't create unwind address space.\n"); return -ENOMEM; diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c index 76cd63de80a8..2728eb4f13ea 100644 --- a/tools/perf/util/unwind-libunwind.c +++ b/tools/perf/util/unwind-libunwind.c @@ -12,11 +12,6 @@ struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops; struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops; struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops; -static void unwind__register_ops(struct maps *maps, struct unwind_libunwind_ops *ops) -{ - RC_CHK_ACCESS(maps)->unwind_libunwind_ops = ops; -} - int unwind__prepare_access(struct maps *maps, struct map *map, bool *initialized) { const char *arch; @@ -60,7 +55,7 @@ int unwind__prepare_access(struct maps *maps, struct map *map, bool *initialized return 0; } out_register: - unwind__register_ops(maps, ops); + maps__set_unwind_libunwind_ops(maps, ops); err = maps__unwind_libunwind_ops(maps)->prepare_access(maps); if (initialized) From patchwork Tue Oct 24 22:23:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157750 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238495vqx; Tue, 24 Oct 2023 15:28:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVAju3qwoFSM91IWM7TJkVoTbneix6mETPeDqr5sIGUwTl6kQBX4OscGpug9vl1HhU9EPT X-Received: by 2002:a17:902:da83:b0:1c7:4973:7b34 with SMTP id j3-20020a170902da8300b001c749737b34mr12520760plx.50.1698186518817; Tue, 24 Oct 2023 15:28:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186518; cv=none; d=google.com; s=arc-20160816; b=uBvP9y4FlpE1MHUyUJzgaQXOLlTCO/GDBbcvdlhWAwCD/fEFGtHt7ujbfbRHvn5kyq qAjMi79r8sVVFkDKgClj6NIxiIl1prE4zDOdh46H98Itmy2NrRehaTAM3cfhtsZQuPt4 ojX4tK4SOCZ3/rFZWiySjLGhG43+IBWrNALGXT0Th8wWICr19zgH6RX516lum3KvF9uF Rgg5zKbPRgTvmH63sqQq9b2kO6Ig184YItKvyIg8kR2jmQQwy37klRLfTmthD3tB6lub tbGujoHY1cxDOTe+qnhbq1TIfLzhfwtEJlKHTCH78MVQWbQ48rB6ybyUfGMlATc0Wosj X+wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=KF5V0xK2Lq20qZdw/Kvq8/AiFWZ8gPx81P4SISP1cMg=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=iM9FGQPLnS4taGvaLxeBuY3V72nqR4vF6x0it61XFLxce3nMaLwe3EAkluMwEsC3kL 0xQaM6gZTzme4qZP/SCOL9Rztyi5mVGwmYDyr8mpKMtXPKLGM8HhkWiz9Vjr3E2L4GHn 12GcSlBXHQk+ShC1i5kuTVSE2KzuUo6eikxN77GrmgIsphNh37w7LBFUj/o3pCHi6yvz 2Q0n20udBv+QrEIRrzI7qGmrysNnrlySrm4RS9qpQAKgWXjQG2gIHLoldbDNseWrVpFc NU8lDmUQ+vjKbPAO+ndov6bEgAbBY8eWS4bUJw+sq2QDWH2OiDlbZ1zMdsH1LFEpM9JY xp+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PDoKSkai; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id v10-20020a170902b7ca00b001b87bd2f7b0si8581997plz.402.2023.10.24.15.28.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PDoKSkai; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 7A7C18082878; Tue, 24 Oct 2023 15:28:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344514AbjJXW1T (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344531AbjJXW0f (ORCPT ); Tue, 24 Oct 2023 18:26:35 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F75519B0 for ; Tue, 24 Oct 2023 15:25:50 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a92864859bso45302977b3.0 for ; Tue, 24 Oct 2023 15:25:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186350; x=1698791150; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=KF5V0xK2Lq20qZdw/Kvq8/AiFWZ8gPx81P4SISP1cMg=; b=PDoKSkaivxThTnIPOnsPmCIN2WrnTiC/G1oFvPQZnObsne4v8Fnkt11X9zR1X3SxKx NcnqThhriSs99HGL1Go4iZJplH4vxD2Yomiv49I2/Ppkl3GNjT1wNLA7Gx/GoRl2dApp 7a+57Ix5QPU8PxvwEj020HjRPXuMw99Sk/XxfdHTs8aeLjkZF11TWkyADAzdyY4T0lit tRyJ+A+OLLZaHmyRR5IR/sthOvNQv4YCoIN+7pXhHGxOf8dMEN2iJouit+8otPCCAt4m QNVK3RJNjiers2FuKcaKXBReaSZHz+igmitIHkp1yyF/bvgfhHnBxiQmvsUUoMlYglmE ry6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186350; x=1698791150; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KF5V0xK2Lq20qZdw/Kvq8/AiFWZ8gPx81P4SISP1cMg=; b=AV09mhNAq21UNzNUu3F62oaZ7cvRGEfL2BVuRJD9U/55JZOYYY7l7tBqgl/KG7ecYS WmBqYp13VJTiiUj7vD8gk2Wsz1yo7e8dROXLlW3o9j9t3mSVC1hCE1b+yfOwzt0HSiIi oiScJglW/XmGmg1mBf1iu7t7WU0JZ84FcR4ZJ5f1PYPpiwefgnYPLE3S92DhW85o2yIH Mh9Tl/GrW39mcE6p2us/WpaKfb8v2mgFKH9lqUGZFVHyd7uhb70CdFbyRAPE2o//dCq0 cjtvNV+agRAdtX5qL39onLiZRFj2QBrsa9NB9pqD9QMraZx/FYxoq70ffJRzhnGntnvx nzWA== X-Gm-Message-State: AOJu0YxzaOBayV4u9n7Pgj1L2zuXrJKXA9oEYwcrkzYAu4QeQfGuN27a lBs7a5FVOBpCbXyEqlwP6DFYVzKf1/mU X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:4e84:0:b0:5a8:2fb9:aeab with SMTP id c126-20020a814e84000000b005a82fb9aeabmr277735ywb.3.1698186349788; Tue, 24 Oct 2023 15:25:49 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:46 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-44-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 43/50] perf maps: Locking tidy up of nr_maps From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:01 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677627233799507 X-GMAIL-MSGID: 1780677627233799507 After this change maps__nr_maps is only used by tests, existing users are migrated to maps__empty. Compute maps__empty under the read lock. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 10 ++++++++-- tools/perf/util/maps.h | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 42d73f00f9c1..f9c77119af22 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -441,7 +441,7 @@ static struct thread *findnew_guest_code(struct machine *machine, return NULL; /* Assume maps are set up if there are any */ - if (maps__nr_maps(thread__maps(thread))) + if (!maps__empty(thread__maps(thread))) return thread; host_thread = machine__find_thread(host_machine, -1, pid); diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 41e9e39b1b4c..725f5d73e93a 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -528,7 +528,13 @@ void maps__remove(struct maps *maps, struct map *map) bool maps__empty(struct maps *maps) { - return maps__nr_maps(maps) == 0; + bool res; + + down_read(maps__lock(maps)); + res = maps__nr_maps(maps) == 0; + up_read(maps__lock(maps)); + + return res; } bool maps__equal(struct maps *a, struct maps *b) @@ -851,7 +857,7 @@ int maps__copy_from(struct maps *dest, struct maps *parent) parent_maps_by_address = maps__maps_by_address(parent); n = maps__nr_maps(parent); - if (maps__empty(dest)) { + if (maps__nr_maps(dest) == 0) { /* No existing mappings so just copy from parent to avoid reallocs in insert. */ unsigned int nr_maps_allocated = RC_CHK_ACCESS(parent)->nr_maps_allocated; struct map **dest_maps_by_address = diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 4bcba136ffe5..d9aa62ed968a 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -43,8 +43,8 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data) void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data); struct machine *maps__machine(const struct maps *maps); -unsigned int maps__nr_maps(const struct maps *maps); -refcount_t *maps__refcnt(struct maps *maps); +unsigned int maps__nr_maps(const struct maps *maps); /* Test only. */ +refcount_t *maps__refcnt(struct maps *maps); /* Test only. */ #ifdef HAVE_LIBUNWIND_SUPPORT void *maps__addr_space(const struct maps *maps); From patchwork Tue Oct 24 22:23:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157764 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2242213vqx; Tue, 24 Oct 2023 15:38:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9NN8jNOmgc3whzO7C0ESD0X5m67+dR5F1nBXkdAtX3jsWBxb+DQsEJcodY+YlWaYbQ9B3 X-Received: by 2002:a05:6a20:6621:b0:137:74f8:62ee with SMTP id n33-20020a056a20662100b0013774f862eemr3248256pzh.18.1698187080810; Tue, 24 Oct 2023 15:38:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187080; cv=none; d=google.com; s=arc-20160816; b=u5tBw0HRYxjHXBdNFU/4Vg9/qZRouLMgLK2dkPfhs4o7vLl92slBqIqiaKIlPlN4vE khMc0HXF7Sg36HX9xbdQ+9g4gsJdThskPyqhoM6dA5yxLNRuqVfx1t0bEesySK/F5Rst feS0kZaZkrfGruL1woST/P3spPUH5sByBCWJ+NFZq1uqjqe022dCTKsfFEpVAX4eWuDW 8B1cFbwsNYgVaYQtUxigMMlkYebxdlourbSBCDSXatbXu9IojHucts49NNNClyUiGObx /MfcDLIz2kJZLRxjz21L4bdP50buGMoEjPNSOcQq/xV4y/EiAwOdogXTIH3zYEXgnRja z2mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=UNZsWL7q61tQxw4sAWHRVjc8YNOyPUB86ZxyQzS9cbI=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=qjCNEe2wwdvtRfwS1VUSUj0BwCkjUweJHeASNobu9jtHK9dS4/r3Gaa6eQ5lfZXCuO QyiSOkAXS2IQpQBcZBds/ZGvZt1vlsKPSdvQdSQi3N6t8M/xw7GoMgGmnwlxa6gd3EdJ GdaRUeDlh7y+btyTXTIkdXlg+wp4BA+9aT1q1+4TxoR56CEm7HGgDfn7C89TpQJZuArl mqVPj1HUQ6PGHuu8WQi8i9W3bTPOrV8BwRg9GXOn7TdqUeaf18QVvzUM9xZRqh7Kclab JDlwKV3SwuT7wzune35s31biCumrlEYz7VnMM7y7fT4Ev4ScZHnCrEC89PsTI+swJdDX NJBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Zn8XNsJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id k66-20020a633d45000000b005b8ee1c0c68si1599656pga.605.2023.10.24.15.38.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:38:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Zn8XNsJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 7B5FC80219FC; Tue, 24 Oct 2023 15:37:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344591AbjJXWhV (ORCPT + 27 others); Tue, 24 Oct 2023 18:37:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344588AbjJXWg6 (ORCPT ); Tue, 24 Oct 2023 18:36:58 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB6121FC1 for ; Tue, 24 Oct 2023 15:25:52 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a8ebc70d33so62725127b3.1 for ; Tue, 24 Oct 2023 15:25:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186352; x=1698791152; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=UNZsWL7q61tQxw4sAWHRVjc8YNOyPUB86ZxyQzS9cbI=; b=Zn8XNsJfLmg04e+nCbVxxpzHEOoCqg8El1s9vNUzY5D+1jvYoGYevXsCmJBGxhbwkT SkDFIscTsJXX/kigFwQzv14uBhoP+3XQvU5Q5AMF0u6HAdemwSc/M7GvW5xU4JRmTwdc fZNBJuQ45L85BWqbosnh7vlkreIddIbBKVnLxhgs7wgHN0p2mTXlTAV1JiAVh0Tm9IEx OCU0BNGgblUj3tFE4j8u9tv+GVNWOYjC2UxmJpGohFsmAx9S0a/LpFrt0TJtHA0tgtgw YG8TJtnYq0qHm6o8lk0hqyx/uo40fkhjQkSQOJgBcSspuJvMgqmy5FBq70eEtThIgg4r 0Y0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186352; x=1698791152; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UNZsWL7q61tQxw4sAWHRVjc8YNOyPUB86ZxyQzS9cbI=; b=IzL+nc+2QWG5oVfcvQ64PKoGhQfY5Y3rWxtBtDKrJbisOPmWwbFz00XdVCw5AcmvS3 epJUpEXU76j9hmu9ilPCSkwqQKYhrCjwKuMT1C90ghvwSPN/l30HfXW2vxy2s6EI/9Xr LW7/N5IvmGjN8NSp49+UAiHkoAuTezYp5gUQBtYvddll6WJhUxMbchNf+qm6sgMsRZ4F CG2pApwwgppIMlEzsoPWf/IGLwXwUcRHWkG9r9jDOu+JU2RHvPIs8YiSmW1FguWKOd/R RzgVvcWC3KuUkIRZWV4K4lls3urxgGre850xOFCeiU4ayMMXMCGyGPVyyCKX4Wun2OWB OOAg== X-Gm-Message-State: AOJu0YzVzJplBLUh8YLmhsYRQHBQ1DZTKQxDQjpCgQVfsrs2VU3tMjL3 obQO9AwGncGdbJS09bpAwlSdZQj/3yt3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:c785:0:b0:da0:4c77:e77c with SMTP id w127-20020a25c785000000b00da04c77e77cmr46443ybe.13.1698186352004; Tue, 24 Oct 2023 15:25:52 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:47 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-45-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 44/50] perf dso: Reorder variables to save space in struct dso From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:37:58 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678216706036019 X-GMAIL-MSGID: 1780678216706036019 Save 40 bytes and move from 8 to 7 cache lines. Make variable dwfl dependent on being a powerpc build. Squeeze bits of int/enum types when appropriate. Remove holes/padding by reordering variables. Before: ``` struct dso { struct mutex lock; /* 0 40 */ struct list_head node; /* 40 16 */ struct rb_node rb_node __attribute__((__aligned__(8))); /* 56 24 */ /* --- cacheline 1 boundary (64 bytes) was 16 bytes ago --- */ struct rb_root * root; /* 80 8 */ struct rb_root_cached symbols; /* 88 16 */ struct symbol * * symbol_names; /* 104 8 */ size_t symbol_names_len; /* 112 8 */ struct rb_root_cached inlined_nodes; /* 120 16 */ /* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */ struct rb_root_cached srclines; /* 136 16 */ struct { u64 addr; /* 152 8 */ struct symbol * symbol; /* 160 8 */ } last_find_result; /* 152 16 */ void * a2l; /* 168 8 */ char * symsrc_filename; /* 176 8 */ unsigned int a2l_fails; /* 184 4 */ enum dso_space_type kernel; /* 188 4 */ /* --- cacheline 3 boundary (192 bytes) --- */ _Bool is_kmod; /* 192 1 */ /* XXX 3 bytes hole, try to pack */ enum dso_swap_type needs_swap; /* 196 4 */ enum dso_binary_type symtab_type; /* 200 4 */ enum dso_binary_type binary_type; /* 204 4 */ enum dso_load_errno load_errno; /* 208 4 */ u8 adjust_symbols:1; /* 212: 0 1 */ u8 has_build_id:1; /* 212: 1 1 */ u8 header_build_id:1; /* 212: 2 1 */ u8 has_srcline:1; /* 212: 3 1 */ u8 hit:1; /* 212: 4 1 */ u8 annotate_warned:1; /* 212: 5 1 */ u8 auxtrace_warned:1; /* 212: 6 1 */ u8 short_name_allocated:1; /* 212: 7 1 */ u8 long_name_allocated:1; /* 213: 0 1 */ u8 is_64_bit:1; /* 213: 1 1 */ /* XXX 6 bits hole, try to pack */ _Bool sorted_by_name; /* 214 1 */ _Bool loaded; /* 215 1 */ u8 rel; /* 216 1 */ /* XXX 7 bytes hole, try to pack */ struct build_id bid; /* 224 32 */ /* --- cacheline 4 boundary (256 bytes) --- */ u64 text_offset; /* 256 8 */ u64 text_end; /* 264 8 */ const char * short_name; /* 272 8 */ const char * long_name; /* 280 8 */ u16 long_name_len; /* 288 2 */ u16 short_name_len; /* 290 2 */ /* XXX 4 bytes hole, try to pack */ void * dwfl; /* 296 8 */ struct auxtrace_cache * auxtrace_cache; /* 304 8 */ int comp; /* 312 4 */ /* XXX 4 bytes hole, try to pack */ /* --- cacheline 5 boundary (320 bytes) --- */ struct { struct rb_root cache; /* 320 8 */ int fd; /* 328 4 */ int status; /* 332 4 */ u32 status_seen; /* 336 4 */ /* XXX 4 bytes hole, try to pack */ u64 file_size; /* 344 8 */ struct list_head open_entry; /* 352 16 */ u64 elf_base_addr; /* 368 8 */ u64 debug_frame_offset; /* 376 8 */ /* --- cacheline 6 boundary (384 bytes) --- */ u64 eh_frame_hdr_addr; /* 384 8 */ u64 eh_frame_hdr_offset; /* 392 8 */ } data; /* 320 80 */ struct { u32 id; /* 400 4 */ u32 sub_id; /* 404 4 */ struct perf_env * env; /* 408 8 */ } bpf_prog; /* 400 16 */ union { void * priv; /* 416 8 */ u64 db_id; /* 416 8 */ }; /* 416 8 */ struct nsinfo * nsinfo; /* 424 8 */ struct dso_id id; /* 432 24 */ /* --- cacheline 7 boundary (448 bytes) was 8 bytes ago --- */ refcount_t refcnt; /* 456 4 */ char name[]; /* 460 0 */ /* size: 464, cachelines: 8, members: 49 */ /* sum members: 440, holes: 4, sum holes: 18 */ /* sum bitfield members: 10 bits, bit holes: 1, sum bit holes: 6 bits */ /* padding: 4 */ /* forced alignments: 1 */ /* last cacheline: 16 bytes */ } __attribute__((__aligned__(8))); ``` After: ``` struct dso { struct mutex lock; /* 0 40 */ struct list_head node; /* 40 16 */ struct rb_node rb_node __attribute__((__aligned__(8))); /* 56 24 */ /* --- cacheline 1 boundary (64 bytes) was 16 bytes ago --- */ struct rb_root * root; /* 80 8 */ struct rb_root_cached symbols; /* 88 16 */ struct symbol * * symbol_names; /* 104 8 */ size_t symbol_names_len; /* 112 8 */ struct rb_root_cached inlined_nodes; /* 120 16 */ /* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */ struct rb_root_cached srclines; /* 136 16 */ struct { u64 addr; /* 152 8 */ struct symbol * symbol; /* 160 8 */ } last_find_result; /* 152 16 */ struct build_id bid; /* 168 32 */ /* --- cacheline 3 boundary (192 bytes) was 8 bytes ago --- */ u64 text_offset; /* 200 8 */ u64 text_end; /* 208 8 */ const char * short_name; /* 216 8 */ const char * long_name; /* 224 8 */ void * a2l; /* 232 8 */ char * symsrc_filename; /* 240 8 */ struct nsinfo * nsinfo; /* 248 8 */ /* --- cacheline 4 boundary (256 bytes) --- */ struct auxtrace_cache * auxtrace_cache; /* 256 8 */ union { void * priv; /* 264 8 */ u64 db_id; /* 264 8 */ }; /* 264 8 */ struct { struct perf_env * env; /* 272 8 */ u32 id; /* 280 4 */ u32 sub_id; /* 284 4 */ } bpf_prog; /* 272 16 */ struct { struct rb_root cache; /* 288 8 */ struct list_head open_entry; /* 296 16 */ u64 file_size; /* 312 8 */ /* --- cacheline 5 boundary (320 bytes) --- */ u64 elf_base_addr; /* 320 8 */ u64 debug_frame_offset; /* 328 8 */ u64 eh_frame_hdr_addr; /* 336 8 */ u64 eh_frame_hdr_offset; /* 344 8 */ int fd; /* 352 4 */ int status; /* 356 4 */ u32 status_seen; /* 360 4 */ } data; /* 288 80 */ /* XXX last struct has 4 bytes of padding */ struct dso_id id; /* 368 24 */ /* --- cacheline 6 boundary (384 bytes) was 8 bytes ago --- */ unsigned int a2l_fails; /* 392 4 */ int comp; /* 396 4 */ refcount_t refcnt; /* 400 4 */ enum dso_load_errno load_errno; /* 404 4 */ u16 long_name_len; /* 408 2 */ u16 short_name_len; /* 410 2 */ enum dso_binary_type symtab_type:8; /* 412: 0 4 */ enum dso_binary_type binary_type:8; /* 412: 8 4 */ enum dso_space_type kernel:2; /* 412:16 4 */ enum dso_swap_type needs_swap:2; /* 412:18 4 */ /* Bitfield combined with next fields */ _Bool is_kmod:1; /* 414: 4 1 */ u8 adjust_symbols:1; /* 414: 5 1 */ u8 has_build_id:1; /* 414: 6 1 */ u8 header_build_id:1; /* 414: 7 1 */ u8 has_srcline:1; /* 415: 0 1 */ u8 hit:1; /* 415: 1 1 */ u8 annotate_warned:1; /* 415: 2 1 */ u8 auxtrace_warned:1; /* 415: 3 1 */ u8 short_name_allocated:1; /* 415: 4 1 */ u8 long_name_allocated:1; /* 415: 5 1 */ u8 is_64_bit:1; /* 415: 6 1 */ /* XXX 1 bit hole, try to pack */ _Bool sorted_by_name; /* 416 1 */ _Bool loaded; /* 417 1 */ u8 rel; /* 418 1 */ char name[]; /* 419 0 */ /* size: 424, cachelines: 7, members: 48 */ /* sum members: 415 */ /* sum bitfield members: 31 bits, bit holes: 1, sum bit holes: 1 bits */ /* padding: 5 */ /* paddings: 1, sum paddings: 4 */ /* forced alignments: 1 */ /* last cacheline: 40 bytes */ } __attribute__((__aligned__(8))); ``` Signed-off-by: Ian Rogers --- tools/perf/util/dso.h | 84 +++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 3759de8c2267..8bdc17d78b02 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -158,66 +158,66 @@ struct dso { u64 addr; struct symbol *symbol; } last_find_result; - void *a2l; - char *symsrc_filename; - unsigned int a2l_fails; - enum dso_space_type kernel; - bool is_kmod; - enum dso_swap_type needs_swap; - enum dso_binary_type symtab_type; - enum dso_binary_type binary_type; - enum dso_load_errno load_errno; - u8 adjust_symbols:1; - u8 has_build_id:1; - u8 header_build_id:1; - u8 has_srcline:1; - u8 hit:1; - u8 annotate_warned:1; - u8 auxtrace_warned:1; - u8 short_name_allocated:1; - u8 long_name_allocated:1; - u8 is_64_bit:1; - bool sorted_by_name; - bool loaded; - u8 rel; struct build_id bid; u64 text_offset; u64 text_end; const char *short_name; const char *long_name; - u16 long_name_len; - u16 short_name_len; + void *a2l; + char *symsrc_filename; +#if defined(__powerpc__) void *dwfl; /* DWARF debug info */ +#endif + struct nsinfo *nsinfo; struct auxtrace_cache *auxtrace_cache; - int comp; - + union { /* Tool specific area */ + void *priv; + u64 db_id; + }; + /* bpf prog information */ + struct { + struct perf_env *env; + u32 id; + u32 sub_id; + } bpf_prog; /* dso data file */ struct { struct rb_root cache; - int fd; - int status; - u32 status_seen; - u64 file_size; struct list_head open_entry; + u64 file_size; u64 elf_base_addr; u64 debug_frame_offset; u64 eh_frame_hdr_addr; u64 eh_frame_hdr_offset; + int fd; + int status; + u32 status_seen; } data; - /* bpf prog information */ - struct { - u32 id; - u32 sub_id; - struct perf_env *env; - } bpf_prog; - - union { /* Tool specific area */ - void *priv; - u64 db_id; - }; - struct nsinfo *nsinfo; struct dso_id id; + unsigned int a2l_fails; + int comp; refcount_t refcnt; + enum dso_load_errno load_errno; + u16 long_name_len; + u16 short_name_len; + enum dso_binary_type symtab_type:8; + enum dso_binary_type binary_type:8; + enum dso_space_type kernel:2; + enum dso_swap_type needs_swap:2; + bool is_kmod:1; + u8 adjust_symbols:1; + u8 has_build_id:1; + u8 header_build_id:1; + u8 has_srcline:1; + u8 hit:1; + u8 annotate_warned:1; + u8 auxtrace_warned:1; + u8 short_name_allocated:1; + u8 long_name_allocated:1; + u8 is_64_bit:1; + bool sorted_by_name; + bool loaded; + u8 rel; char name[]; }; From patchwork Tue Oct 24 22:23:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157752 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238766vqx; Tue, 24 Oct 2023 15:29:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrSetame+GtvsZJFPvAGgyrLgb3OTQ0Gdw4qdnJhtm+phXajPIqWeyYalS1c7u/VyMkngR X-Received: by 2002:a17:90a:fa8f:b0:268:2658:3b01 with SMTP id cu15-20020a17090afa8f00b0026826583b01mr10191763pjb.39.1698186565228; Tue, 24 Oct 2023 15:29:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186565; cv=none; d=google.com; s=arc-20160816; b=TucMQmLZ+M/WgXUSVNWUjDEYpmvsz2ENllpAVrGPh3QYmXCVYOuJmLG5Ggs9M8vEkw k7NjUoXD3y+sdEngcZ8YGUS06nlVlPbp6Sekht41NAfSm5nJj9ndOkGFVsMxDsA8j96M RvPGU0bvhGOQxJeTDLdq0krpdvZ06kga73/6miVFQho6AcYIuQIsRqs9g5lJfr46JXkC FM9hdM8WqdJReqDhFO3E+F9WERLNX/ZumJAtLJAia/sRNjtO0IQqghlVrr/7Il+gKQcy Lw3/W44yTGAG2jqimmEkjUr0PU7miyTnBuBuHFzndWmpBPDVE/vIO75rXoAtBcArk5GK gUmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=fIRcu/3WTvtsQSdzqjdau/PJdT4wd5hzv4KiweKRc9Q=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=dPA5m/qW7Bbz4kQUUrkgpfrzyM9uIiwzquyrfOazzACVbSfPz4qi4ClIRUXPZI3aKV BQuFo8KvHH5PGtxXWa83QaYxCmzqyXFnlsZYCeFkjACFhhcnSVu5UYksUptd0CfMRVJb UWRnZUddkSrq03CAS42dVTdTIincz5O+hoO5+9XkjuuCg3pGjus9FHOUqdH7xnif2jzQ dLcZlIDlYINJYZEbditVmBOcNKZTCyGHY5gnDz71Q4s+9tPVWT1vzfnUKYrHyHIbnOai Ey8f6fsVACjC+GY4C47+kdce3vIv5xiOpwbwSZeaccTkp2UEiGWHfTd5JoAJHL0z7IR0 Upkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=KEHxNGDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id a2-20020a17090a854200b0026d0f7c6b17si11767903pjw.30.2023.10.24.15.29.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:29:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=KEHxNGDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 499E1809C115; Tue, 24 Oct 2023 15:29:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344613AbjJXW2g (ORCPT + 27 others); Tue, 24 Oct 2023 18:28:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344610AbjJXW2P (ORCPT ); Tue, 24 Oct 2023 18:28:15 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E2111FF7 for ; Tue, 24 Oct 2023 15:25:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9c4ae201e0so252759276.1 for ; Tue, 24 Oct 2023 15:25:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186354; x=1698791154; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=fIRcu/3WTvtsQSdzqjdau/PJdT4wd5hzv4KiweKRc9Q=; b=KEHxNGDmceKF1GzjwfJ/hvIZ5MVUiNH7xP5GbJs7S4/R6dZBesKAuLRTZ8FcMj0uoU 4ZIBJciWaJqpkCkEsmIXQKKBvEUkU1X4XJhytBpd8HeFnyAX57Vo3sCSE+Q+rqhyF4Jj MBp/5lEGVeg4cwdyS5wtgU3kc8OnO4E3fnCjv29GtgMA9uycuIvgQvnKkQacWdwmKWQF dkK03Prpw6AsmJqOALF12/rWBJwo5LfeCJ03Cbobh886ZrO5V/YxjISJEbIkkRd2LDW9 H/1j4QY4Ak3qYN1LsQmCb6Fa6S1cwfFQILbnfyVeNVV2FoI51nDOALjNPzR1UrzaOtpi /e8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186354; x=1698791154; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fIRcu/3WTvtsQSdzqjdau/PJdT4wd5hzv4KiweKRc9Q=; b=IcLDNq7Fzv8Y+wD+BtFV5Y6U/tf76w0LvZuetjBFwGx8kHXPSVPAdfh9F8XK3sgKNR rAKD269F1X/OjkkXCZ1WWbY5i+NZ29iKPlvypSYrDwFJwSFzLiv18GjsFlQJgJ3YPzIX dfS92f5fIKDZR1p+7eWBrBDJQWdKtrtKV/u7EzpWfTgjd1H7LF3LMVRQm79gRg9ZeqfD NQdIvwlIji6FhOMwHq0LAfYNcscGcc70cUUtB6Gs/sZvmkPEAEs9w7gX1ftZ2TcKpet+ liT3GXtlECtr/tJtzDcqhy8qHkWKZwJM6olePS0Sea8qBxipjUDAADbkjV7dH8r7mc6v yQTA== X-Gm-Message-State: AOJu0Yxui5IMjAu2vtjPZPH+LosIEfjefbNl3/yZR/whEgX0SGcw3iAQ SW1cDa6eXhiVkCNkNtJdmLbov6Es7zdj X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:d1ca:0:b0:d9c:f811:eb9e with SMTP id i193-20020a25d1ca000000b00d9cf811eb9emr321690ybg.4.1698186354204; Tue, 24 Oct 2023 15:25:54 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:48 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-46-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 45/50] perf report: Sort child tasks by tid From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:29:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677676129520933 X-GMAIL-MSGID: 1780677676129520933 Commit 91e467bc568f ("perf machine: Use hashtable for machine threads") made the iteration of thread tids unordered. The perf report --tasks output now shows child threads in an order determined by the hashing. For example, in this snippet tid 3 appears after tid 256 even though they have the same ppid 2: ``` $ perf report --tasks % pid tid ppid comm 0 0 -1 |swapper 2 2 0 | kthreadd 256 256 2 | kworker/12:1H-k 693761 693761 2 | kworker/10:1-mm 1301762 1301762 2 | kworker/1:1-mm_ 1302530 1302530 2 | kworker/u32:0-k 3 3 2 | rcu_gp ... ``` The output is easier to read if threads appear numerically increasing. To allow for this, read all threads into a list then sort with a comparator that orders by the child task's of the first common parent. The list creation and deletion are created as utilities on machine. The indentation is possible by counting the number of parents a child has. With this change the output for the same data file is now like: ``` $ perf report --tasks % pid tid ppid comm 0 0 -1 |swapper 1 1 0 | systemd 823 823 1 | systemd-journal 853 853 1 | systemd-udevd 3230 3230 1 | systemd-timesyn 3236 3236 1 | auditd 3239 3239 3236 | audisp-syslog 3321 3321 1 | accounts-daemon ... ``` Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 203 ++++++++++++++++++++---------------- tools/perf/util/machine.c | 30 ++++++ tools/perf/util/machine.h | 10 ++ 3 files changed, 155 insertions(+), 88 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index d7f1f3c8cb7d..3731e9bf7000 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -815,35 +816,6 @@ static void tasks_setup(struct report *rep) rep->tool.no_warn = true; } -struct task { - struct thread *thread; - struct list_head list; - struct list_head children; -}; - -static struct task *tasks_list(struct task *task, struct machine *machine) -{ - struct thread *parent_thread, *thread = task->thread; - struct task *parent_task; - - /* Already listed. */ - if (!list_empty(&task->list)) - return NULL; - - /* Last one in the chain. */ - if (thread__ppid(thread) == -1) - return task; - - parent_thread = machine__find_thread(machine, -1, thread__ppid(thread)); - if (!parent_thread) - return ERR_PTR(-ENOENT); - - parent_task = thread__priv(parent_thread); - thread__put(parent_thread); - list_add_tail(&task->list, &parent_task->children); - return tasks_list(parent_task, machine); -} - struct maps__fprintf_task_args { int indent; FILE *fp; @@ -887,89 +859,144 @@ static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) return args.printed; } -static void task__print_level(struct task *task, FILE *fp, int level) +static int thread_level(struct machine *machine, const struct thread *thread) { - struct thread *thread = task->thread; - struct task *child; - int comm_indent = fprintf(fp, " %8d %8d %8d |%*s", - thread__pid(thread), thread__tid(thread), - thread__ppid(thread), level, ""); + struct thread *parent_thread; + int res; - fprintf(fp, "%s\n", thread__comm_str(thread)); + if (thread__tid(thread) <= 0) + return 0; - maps__fprintf_task(thread__maps(thread), comm_indent, fp); + if (thread__ppid(thread) <= 0) + return 1; - if (!list_empty(&task->children)) { - list_for_each_entry(child, &task->children, list) - task__print_level(child, fp, level + 1); + parent_thread = machine__find_thread(machine, -1, thread__ppid(thread)); + if (!parent_thread) { + pr_err("Missing parent thread of %d\n", thread__tid(thread)); + return 0; } + res = 1 + thread_level(machine, parent_thread); + thread__put(parent_thread); + return res; } -static int tasks_print(struct report *rep, FILE *fp) +static void task__print_level(struct machine *machine, struct thread *thread, FILE *fp) { - struct perf_session *session = rep->session; - struct machine *machine = &session->machines.host; - struct task *tasks, *task; - unsigned int nr = 0, itask = 0, i; - struct rb_node *nd; - LIST_HEAD(list); + int level = thread_level(machine, thread); + int comm_indent = fprintf(fp, " %8d %8d %8d |%*s", + thread__pid(thread), thread__tid(thread), + thread__ppid(thread), level, ""); - /* - * No locking needed while accessing machine->threads, - * because --tasks is single threaded command. - */ + fprintf(fp, "%s\n", thread__comm_str(thread)); - /* Count all the threads. */ - for (i = 0; i < THREADS__TABLE_SIZE; i++) - nr += machine->threads[i].nr; + maps__fprintf_task(thread__maps(thread), comm_indent, fp); +} - tasks = malloc(sizeof(*tasks) * nr); - if (!tasks) - return -ENOMEM; +static int task_list_cmp(void *priv, const struct list_head *la, const struct list_head *lb) +{ + struct machine *machine = priv; + struct thread_list *task_a = list_entry(la, struct thread_list, list); + struct thread_list *task_b = list_entry(lb, struct thread_list, list); + struct thread *a = task_a->thread; + struct thread *b = task_b->thread; + int level_a, level_b, res; + + /* Compare a and b to root. */ + if (thread__tid(a) == thread__tid(b)) + return 0; - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; + if (thread__tid(a) == 0) + return -1; - for (nd = rb_first_cached(&threads->entries); nd; - nd = rb_next(nd)) { - task = tasks + itask++; + if (thread__tid(b) == 0) + return 1; - task->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread; - INIT_LIST_HEAD(&task->children); - INIT_LIST_HEAD(&task->list); - thread__set_priv(task->thread, task); - } + /* If parents match sort by tid. */ + if (thread__ppid(a) == thread__ppid(b)) { + return thread__tid(a) < thread__tid(b) + ? -1 + : (thread__tid(a) > thread__tid(b) ? 1 : 0); } /* - * Iterate every task down to the unprocessed parent - * and link all in task children list. Task with no - * parent is added into 'list'. + * Find a and b such that if they are a child of each other a and b's + * tid's match, otherwise a and b have a common parent and distinct + * tid's to sort by. First make the depths of the threads match. */ - for (itask = 0; itask < nr; itask++) { - task = tasks + itask; - - if (!list_empty(&task->list)) - continue; - - task = tasks_list(task, machine); - if (IS_ERR(task)) { - pr_err("Error: failed to process tasks\n"); - free(tasks); - return PTR_ERR(task); + level_a = thread_level(machine, a); + level_b = thread_level(machine, b); + a = thread__get(a); + b = thread__get(b); + for (int i = level_a; i > level_b; i--) { + struct thread *parent = machine__find_thread(machine, -1, thread__ppid(a)); + + thread__put(a); + if (!parent) { + pr_err("Missing parent thread of %d\n", thread__tid(a)); + thread__put(b); + return -1; } + a = parent; + } + for (int i = level_b; i > level_a; i--) { + struct thread *parent = machine__find_thread(machine, -1, thread__ppid(b)); - if (task) - list_add_tail(&task->list, &list); + thread__put(b); + if (!parent) { + pr_err("Missing parent thread of %d\n", thread__tid(b)); + thread__put(a); + return 1; + } + b = parent; + } + /* Search up to a common parent. */ + while (thread__ppid(a) != thread__ppid(b)) { + struct thread *parent; + + parent = machine__find_thread(machine, -1, thread__ppid(a)); + thread__put(a); + if (!parent) + pr_err("Missing parent thread of %d\n", thread__tid(a)); + a = parent; + parent = machine__find_thread(machine, -1, thread__ppid(b)); + thread__put(b); + if (!parent) + pr_err("Missing parent thread of %d\n", thread__tid(b)); + b = parent; + if (!a || !b) + return !a && !b ? 0 : (!a ? -1 : 1); + } + if (thread__tid(a) == thread__tid(b)) { + /* a is a child of b or vice-versa, deeper levels appear later. */ + res = level_a < level_b ? -1 : (level_a > level_b ? 1 : 0); + } else { + /* Sort by tid now the parent is the same. */ + res = thread__tid(a) < thread__tid(b) ? -1 : 1; } + thread__put(a); + thread__put(b); + return res; +} + +static int tasks_print(struct report *rep, FILE *fp) +{ + struct machine *machine = &rep->session->machines.host; + LIST_HEAD(tasks); + int ret; - fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm"); + ret = machine__thread_list(machine, &tasks); + if (!ret) { + struct thread_list *task; - list_for_each_entry(task, &list, list) - task__print_level(task, fp, 0); + list_sort(machine, &tasks, task_list_cmp); - free(tasks); - return 0; + fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm"); + + list_for_each_entry(task, &tasks, list) + task__print_level(machine, task->thread, fp); + } + thread_list__delete(&tasks); + return ret; } static int __cmd_report(struct report *rep) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index f9c77119af22..6d7a505850c8 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -3258,6 +3258,36 @@ int machines__for_each_thread(struct machines *machines, return rc; } + +static int thread_list_cb(struct thread *thread, void *data) +{ + struct list_head *list = data; + struct thread_list *entry = malloc(sizeof(*entry)); + + if (!entry) + return -ENOMEM; + + entry->thread = thread__get(thread); + list_add_tail(&entry->list, list); + return 0; +} + +int machine__thread_list(struct machine *machine, struct list_head *list) +{ + return machine__for_each_thread(machine, thread_list_cb, list); +} + +void thread_list__delete(struct list_head *list) +{ + struct thread_list *pos, *next; + + list_for_each_entry_safe(pos, next, list, list) { + thread__zput(pos->thread); + list_del(&pos->list); + free(pos); + } +} + pid_t machine__get_current_tid(struct machine *machine, int cpu) { if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz) diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 1279acda6a8a..b738ce84817b 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -280,6 +280,16 @@ int machines__for_each_thread(struct machines *machines, int (*fn)(struct thread *thread, void *p), void *priv); +struct thread_list { + struct list_head list; + struct thread *thread; +}; + +/* Make a list of struct thread_list based on threads in the machine. */ +int machine__thread_list(struct machine *machine, struct list_head *list); +/* Free up the nodes within the thread_list list. */ +void thread_list__delete(struct list_head *list); + pid_t machine__get_current_tid(struct machine *machine, int cpu); int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, pid_t tid); From patchwork Tue Oct 24 22:23:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157744 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238330vqx; Tue, 24 Oct 2023 15:28:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3bSGfCXmdDHktBoZbpXXC7B/bPtp3dvH69eoyV3+5E0v0ikESIDKBNFyQpfaqTpmjp7mz X-Received: by 2002:a17:902:d506:b0:1c2:1068:1f4f with SMTP id b6-20020a170902d50600b001c210681f4fmr14342318plg.17.1698186494430; Tue, 24 Oct 2023 15:28:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186494; cv=none; d=google.com; s=arc-20160816; b=Tgct3LW03rBoFcN2Id1dprOAllXPnPO2Z7c8GlxyBM0hn6lkRUtxUxU4DLrUgRyX8z RqoFU1EPxoaAZn7C64lYazV1U7hxSOyZW8yav3okMFZdXrxXImTr3zTjKw44sTLFBLo7 p460JybTsVjGHdR68jObxr+88T3woagrVjUqhYAIXSxqD07srpmCgrSxz9x1190oWZtX cTYFBpFpA1dAa3KPBLf0ExkocF6jF8KbERDU9FizlPKe1hnxolD+B8mo4omRbaCXY+D+ 5neUWb609Sp6rSCoGHajAF7n4Yq7NmG3MChKbmS8oIWePJBSvU6SSL/PkMY5fAFUVvP0 phsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=zfK3LtQ16zflZ7X6ZyHLcdZIKlRk/T+4AhzfwFPXJCs=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=vL5u/6TM0OfLfkppAdDq6DCQ86/WQV/uBgWQR/DjJP2EHvjY1QumK7CVpqbVL4OqKz PW0lZGSLRHDJuv2iJKuZYFGq8amLrhKc+JczeEAcvecjrHQyR3rrFPy6LWX/UQMvQXCr UTJU4l1FiB2WcKhwkIyStE3qAype4O/NERLyGijJpsBHCzLXmvY6F4KivBdhDC3L2W+g SX1ECKPUakUWVHFRz+SXEWpt/QpOLa7Ekd5MMcMDr1tV5qxMc03nkIxckRbOd066nBN2 GJKatDo0W3KGX+0u9VOtnHYm5odAitodT72rgVE0wqCsz5KnqdWCuDDlDuDfnR2u35p5 UFpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=RV9yKrDE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id z7-20020a1709028f8700b001c9c89fd36dsi8704532plo.203.2023.10.24.15.28.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=RV9yKrDE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id DDF6680219DC; Tue, 24 Oct 2023 15:28:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344624AbjJXW1w (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344621AbjJXW1b (ORCPT ); Tue, 24 Oct 2023 18:27:31 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3A7C2123 for ; Tue, 24 Oct 2023 15:25:57 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a824ef7a83so63952917b3.0 for ; Tue, 24 Oct 2023 15:25:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186356; x=1698791156; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=zfK3LtQ16zflZ7X6ZyHLcdZIKlRk/T+4AhzfwFPXJCs=; b=RV9yKrDEYbXWbK14zY0h2vfn3sAT9COKGFiwGstrJyhnFT52zdLDk7d1OIuh8fQ09r 4iTla9UXEXP8WZcemGiFT5IVEyE7UIQDEZ9mT5KZoHs0Q3fASFL4HGkNvp9S8C6eHHmQ PufFX1ZstrOYfV/QK9EA02rpS8MwRMOeIUv+DoiOcNiNUihkchVYsOBz4LyKjBB8ouHj XcMB11cZNS8OaDVyfhSaWTUXd9oxowEAHsYZX7358tk93oaIQArrGpjiBKrgg38khrXo zhmXsgpBpwUHfNTt7ldI/ftSZ14RdPB49xp45OcO0BPOW2izuoPeed5re4146xn7IVK4 4Bdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186356; x=1698791156; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zfK3LtQ16zflZ7X6ZyHLcdZIKlRk/T+4AhzfwFPXJCs=; b=LnHovuL+gIo8Y4yubY0fqyutU218PTCOhjHgiTqZ+6vc3tbw2yMTn0q/TVrUEIestm 2A5WDyZMC8BOMgNpS02/pWfX/swxghFCPkJ73j2ibeN6TZOzAaHRmz+qsiytSrvo/QCY h5wHWq5U0eJgvTBslMgyW/R0Z7jwefROJArsZ/h/j62wU0/IiZnsIrVFXSgYqvDmqaFB 6k9ds50ikj5EWFT1MN7yGb4h8PI0Tc5j+bKiRQQ0MO4BE1CSB8IP78Q4uWd0ulrUc3UY c/3mWex2pzNvxZkf7ySUujR9Oe120Qb2vCcGB+hXm5m+BdFTe7NVpP7OgMEkQBg3slt3 Apxw== X-Gm-Message-State: AOJu0YyrKVnCo+8MaATbJGByY6+RLNM48talGjFSZRyvxM+TXgwKvOq7 /eVQI8TI1mZnZIUPq/8Y56I900oqjrBD X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:47ea:69a5 with SMTP id v3-20020a056902108300b00d9a47ea69a5mr366432ybu.1.1698186356515; Tue, 24 Oct 2023 15:25:56 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:49 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-47-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 46/50] perf trace: Ignore thread hashing in summary From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:11 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677601107746938 X-GMAIL-MSGID: 1780677601107746938 Commit 91e467bc568f ("perf machine: Use hashtable for machine threads") made the iteration of thread tids unordered. The perf trace --summary output sorts and prints each hash bucket, rather than all threads globally. Change this behavior by turn all threads into a list, sort the list by number of trace events then by tids, finally print the list. This also allows the rbtree in threads to be not accessed outside of machine. Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 41 +++++++++++++++++++++---------------- tools/perf/util/rb_resort.h | 5 ----- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index e541d0e2777a..e9ff78b331fe 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include @@ -4314,34 +4315,38 @@ static unsigned long thread__nr_events(struct thread_trace *ttrace) return ttrace ? ttrace->nr_events : 0; } -DEFINE_RESORT_RB(threads, - (thread__nr_events(thread__priv(a->thread)) < - thread__nr_events(thread__priv(b->thread))), - struct thread *thread; -) +static int trace_nr_events_cmp(void *priv __maybe_unused, + const struct list_head *la, + const struct list_head *lb) { - entry->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread; + struct thread_list *a = list_entry(la, struct thread_list, list); + struct thread_list *b = list_entry(lb, struct thread_list, list); + unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread)); + unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread)); + + if (a_nr_events != b_nr_events) + return a_nr_events < b_nr_events ? -1 : 1; + + /* Identical number of threads, place smaller tids first. */ + return thread__tid(a->thread) < thread__tid(b->thread) + ? -1 + : (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0); } static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) { size_t printed = trace__fprintf_threads_header(fp); - struct rb_node *nd; - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); + LIST_HEAD(threads); - if (threads == NULL) { - fprintf(fp, "%s", "Error sorting output by nr_events!\n"); - return 0; - } + if (machine__thread_list(trace->host, &threads) == 0) { + struct thread_list *pos; - resort_rb__for_each_entry(nd, threads) - printed += trace__fprintf_thread(fp, threads_entry->thread, trace); + list_sort(NULL, &threads, trace_nr_events_cmp); - resort_rb__delete(threads); + list_for_each_entry(pos, &threads, list) + printed += trace__fprintf_thread(fp, pos->thread, trace); } + thread_list__delete(&threads); return printed; } diff --git a/tools/perf/util/rb_resort.h b/tools/perf/util/rb_resort.h index 376e86cb4c3c..d927a0d25052 100644 --- a/tools/perf/util/rb_resort.h +++ b/tools/perf/util/rb_resort.h @@ -143,9 +143,4 @@ struct __name##_sorted *__name = __name##_sorted__new DECLARE_RESORT_RB(__name)(&__ilist->rblist.entries.rb_root, \ __ilist->rblist.nr_entries) -/* For 'struct machine->threads' */ -#define DECLARE_RESORT_RB_MACHINE_THREADS(__name, __machine, hash_bucket) \ - DECLARE_RESORT_RB(__name)(&__machine->threads[hash_bucket].entries.rb_root, \ - __machine->threads[hash_bucket].nr) - #endif /* _PERF_RESORT_RB_H_ */ From patchwork Tue Oct 24 22:23:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157751 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238595vqx; Tue, 24 Oct 2023 15:28:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFd9sBqSZPa8KM/jeoJUkHevjvQHXTIpuPXsiLb77B6SqvwOKOPLSWAO5LxGUVF+O/Ew62J X-Received: by 2002:a05:6a21:798a:b0:14d:4ab5:5e34 with SMTP id bh10-20020a056a21798a00b0014d4ab55e34mr3588626pzc.51.1698186535012; Tue, 24 Oct 2023 15:28:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186534; cv=none; d=google.com; s=arc-20160816; b=LWWdWGcg+fmShc9+nldPPznv2MbUvJF6gr/jVFiNTB6bZnUiBUwuOx2XznXUba004S EuKEEB6KIx2gcO6NMUSM2DO8ad2ZNT8Z8wM11LQgDy2fc3sT9cqRecrGejrxp/IWYzCs dlfQhjcIIcUwUC9E47Z0HvFc6zYundfvVZydSKvwQKYrAieY7bRm2itr8bogvyWzWPTT 4q+r9RBFlNoBjBeQm3BevokPieRA0QPrDKD1w6gCj2W+BqZO5Xd1davwU+Iojn7Dj1g6 5+n8gEbjB4DDQF7dRbMjkuOkIXjVLiAhdu00wrYIMCheNb/cB+YEs+wPWxOo6cUS4xdR FUMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=NbxTM76xqJorkOJkA8b8csCodDzMUvbQpbWTOjkDtY8=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=GnFbOL/kjk56Gib6VPayw1S03Sa6ViHHVCdIScF/10wzspiWrSe7PaLutkwH0xtkr4 QaS6xskUMFlNlfrQHlIJmP37cgnq++RKfdFUlglHOKC+VqNVc9d3q3FiP0OQtUAi7cc9 gmJYnWhOS37Tc3R0U9XA6fePrflhZ9OEyuPjkXIPkgoyDJ2jsCKqgCiJ3zvs6pKJTRIj jbhFn3KbVm9S2q2Tbxdc4iCRisqkMv4qzno5BR38eFiSfH4Th9dAReWwB+JE0dx77lKs Ppn5VRMkzS9JIpfa14ZEewfxXy34WyW744S1DHrvmc7eM08RR7pMas08O5+hFKA1Ro58 vxug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rpYDtR5J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id d38-20020a631d66000000b00578b37ad24bsi8873269pgm.619.2023.10.24.15.28.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rpYDtR5J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id ECB88802542C; Tue, 24 Oct 2023 15:28:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344546AbjJXW2l (ORCPT + 27 others); Tue, 24 Oct 2023 18:28:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344627AbjJXW2S (ORCPT ); Tue, 24 Oct 2023 18:28:18 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FCFC1708 for ; Tue, 24 Oct 2023 15:25:59 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a909b4e079so58031037b3.2 for ; Tue, 24 Oct 2023 15:25:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186359; x=1698791159; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=NbxTM76xqJorkOJkA8b8csCodDzMUvbQpbWTOjkDtY8=; b=rpYDtR5JPG1CYbQSVc3Eb9sUIcTDJspUBSSZMewAlkB5arZfbDpqaRHs6oQzoNAgrV xUqP8OuuDuRIr7wXrw7xfA4ypmlBYjgjIN5aRM2Y22OAd0Bq57s62GdCmRK4yNVQ+wrd f48AtAiNsuawowuSE/4fov4a6DmCAogKssgkp9eNQpjPgZkC6u761+mYulbmMpF4dcRE GQweSo5W246hfvF2ryMJRdsNh9OL5UxQCRfAINfh9DsQvwGnnR//kl0nRocC7qcJf4uv IYGi7ApvaMN+xBxMXKEbH/gFEofhyPEb6RDr9JAj4n4g7F6lIQhKpoa6+jXjhgnwAoXi k7bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186359; x=1698791159; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NbxTM76xqJorkOJkA8b8csCodDzMUvbQpbWTOjkDtY8=; b=ldp2qI4xMkTMJ0QLPWyugcTeNI/hrHdkALfZzglV4VTHBmtWFvcboeOViNdgSMClmj pKtO9mw50X7b9Ojs2IJoJA9EPptWJ2+2QOSK9Mgc9R1I6kKrhmuL0PGPAMArkJ8NNzkD UHwu6dVmeH528pYAXKQt+kmhnTs4qy/yEFLUHrghoCuUdMi8Km93rg/9T2s/owKMphU6 blaWkHwHNVLMSVsW7IqNJr8w3m2wmREQWLH0hRoHO9cMBDVsgXzYskb9dr9ZAFeHrBls OxG4Qv9+ePTQWJoi2+DzRpdOyqsP2MagIUezZ1gMXtjzrmpoSWRLxsucZ3aJZNuk+XCz WHeQ== X-Gm-Message-State: AOJu0YzLiTyPISrEhuuCfSYbAkWFDJUFsIs6tRTGJdyO8yDVotpl+PE7 lWISOLnc0Ol4S73cnQ6rgOW+r9wbKHcP X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:c5c1:0:b0:d9a:ce79:6298 with SMTP id v184-20020a25c5c1000000b00d9ace796298mr231994ybe.2.1698186358799; Tue, 24 Oct 2023 15:25:58 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:50 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-48-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 47/50] perf machine: Move fprintf to for_each loop and a callback From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:53 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677644085800207 X-GMAIL-MSGID: 1780677644085800207 Avoid exposing the threads data structure by switching to the callback machine__for_each_thread approach. machine__fprintf is only used in tests and verbose >3 output so don't turn to list and sort. Add machine__threads_nr to be refactored later. Note, all existing *_fprintf routines ignore fprintf errors. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 43 ++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 6d7a505850c8..7e19303d1aa6 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1114,29 +1114,40 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) return printed; } -size_t machine__fprintf(struct machine *machine, FILE *fp) +struct machine_fprintf_cb_args { + FILE *fp; + size_t printed; +}; + +static int machine_fprintf_cb(struct thread *thread, void *data) { - struct rb_node *nd; - size_t ret; - int i; + struct machine_fprintf_cb_args *args = data; - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; + /* TODO: handle fprintf errors. */ + args->printed += thread__fprintf(thread, args->fp); + return 0; +} - down_read(&threads->lock); +static size_t machine__threads_nr(const struct machine *machine) +{ + size_t nr = 0; - ret = fprintf(fp, "Threads: %u\n", threads->nr); + for (int i = 0; i < THREADS__TABLE_SIZE; i++) + nr += machine->threads[i].nr; - for (nd = rb_first_cached(&threads->entries); nd; - nd = rb_next(nd)) { - struct thread *pos = rb_entry(nd, struct thread_rb_node, rb_node)->thread; + return nr; +} - ret += thread__fprintf(pos, fp); - } +size_t machine__fprintf(struct machine *machine, FILE *fp) +{ + struct machine_fprintf_cb_args args = { + .fp = fp, + .printed = 0, + }; + size_t ret = fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)); - up_read(&threads->lock); - } - return ret; + machine__for_each_thread(machine, machine_fprintf_cb, &args); + return ret + args.printed; } static struct dso *machine__get_kernel(struct machine *machine) From patchwork Tue Oct 24 22:23:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2238346vqx; Tue, 24 Oct 2023 15:28:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFj4IllANZefWmubI7k+IabKLmrJOLNclrOvrBiRqbsgGl35x/B4v8JcqslPSXUIUXqzifC X-Received: by 2002:a17:902:db12:b0:1c2:218c:3754 with SMTP id m18-20020a170902db1200b001c2218c3754mr16228638plx.53.1698186497231; Tue, 24 Oct 2023 15:28:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186497; cv=none; d=google.com; s=arc-20160816; b=W9g1STfCLi8uaLpRY4D43MH/ItVMMf/BsAV6KLgIA8mJGMdQxPUTU9deEqM+i/ga8x z6jP3rRrVlZp4SUoJ+qWcp02KKergNjSWYCIwxNFhOVxO0ziBM3ANaw5PzPF3lulYNiO YesyxkFYXz1lkpeamBRvdWQv9Bg/lz92rhUJPXOgBpJE//BqrTO4SdUQblWlviUWE+2Q dOgbCucNT5EfmNmdUnDab64AGRldfnDJfGiaQsxr2rHk7ennSo1YD0rffzA9O1HM4qML is9zRMyKvB7gI8O4qWxbLBL9McSnBMUlA4bdC86Py7naZrHHy0U3MjOfukhsN94q+LnQ QQjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=aO4t0OrO5M9nzlFoW7WXAJBPeF1LGPznAtpuwiNn4pU=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=heLNR2xDhcIVC1vFxCffV0aC3Fgv7V0GPYshCvaeAsdhemfPfvxcm8Xs6bZHCmp8xL 01kKQRcx3PshsV86+jewaPyxztZudiHwYPCH9LTK+P5JZKlVrVMzx796y6NGDroUKr5j 7+NUEOE0KzBSxsQ3HEKHJXvbkTwT7k40xpgHD5FNAx9cQB3O18lpQMo+ZrFZJ45OXnOk DXMs5RE6OdtpkI64lYuGevfGl1z+f86J486H2hj/d5gy4uOz12R5/XjoqU6Eo8N9P22O al61JSAUAYjMq/4/7CIlyNA64M1TfbzR8vLBr/jEZ5URD9BXn/snDIwUe3OiNzHuUzmw 6haA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wR9yxlnY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id j12-20020a170902da8c00b001cadc96944fsi5933685plx.495.2023.10.24.15.28.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wR9yxlnY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A0CF480219D3; Tue, 24 Oct 2023 15:28:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344494AbjJXW1z (ORCPT + 27 others); Tue, 24 Oct 2023 18:27:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344641AbjJXW1d (ORCPT ); Tue, 24 Oct 2023 18:27:33 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C73E01995 for ; Tue, 24 Oct 2023 15:26:01 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9cafa90160so5746288276.2 for ; Tue, 24 Oct 2023 15:26:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186361; x=1698791161; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=aO4t0OrO5M9nzlFoW7WXAJBPeF1LGPznAtpuwiNn4pU=; b=wR9yxlnYWaFMxvgXkEiQOPgjngfg8BQqMARuRYD6TPz8/qA+iXPewMkd4sPILtWrKd 9mxNitQf2rHOdeuSPIHvQsY5NZur1gIsB2o82oSwMquFg23Ib35k8AO59fY51n2yDotH yBGKwybUecfp2WGsHtqMh37Viin7vneo9zySYb3SLamxy12NgUObxBtgvliYjFCShVZY lum+HhGRU3CZ7FVjLXZH27+9pJkCKxXXO8e7hHu1ZfE0MkfoHep2gMzzKORV1lwgrd8n dWk9wbSIyoHehlZKAHStTqJgp+gUKWNU5Ob02CEmZMBvkwqjVuKYV21HmqwYv7uCktiK cWdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186361; x=1698791161; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aO4t0OrO5M9nzlFoW7WXAJBPeF1LGPznAtpuwiNn4pU=; b=EU5hCnenV8qsGH3YX0ZAXHV/mIqBKMpZPE2t/twVp+kEooTUawhr+GwM/j1dS/7Yv1 QKCZFkYYGrJzXJ1s0tbn/9GxnVJoLEoLrMOndiTUofdRMUvMtq7ekmsRrxMkgbCWY9/x rhNlFbrHeSuBldiULxCF/xMu99royNWqeP30wB9YUVu3SwJCsUr5PLNXx9jldLUg8dbH 8CfYw1Z2ylwmxj9F/VVgr5k8M3JSolwnC34KIu69eiKOaT13sAMsmd8zHsmoCenSRXOs ROZEJ0+Tgo/tEJhjBi+rqdRYp5STtiBuqhcC8EWmp6Kglux43M/UP8TpBfhlJAtswQYI TigA== X-Gm-Message-State: AOJu0Yx2XuMSTGZ6HmHmEdIGqI3wfvDOXOZE56usNVBRaISIZ245pStd 0eX17jUXfpaCmnudOV3Edkqlvdkk5tiU X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:105:b0:da0:3da9:ce08 with SMTP id o5-20020a056902010500b00da03da9ce08mr62325ybh.10.1698186360967; Tue, 24 Oct 2023 15:26:00 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:51 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-49-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 48/50] perf threads: Move threads to its own files From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:28:13 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677604527769145 X-GMAIL-MSGID: 1780677604527769145 Move threads out of machine and move thread_rb_node into the C file. This hides the implementation of threads from the rest of the code allowing for it to be refactored. Locking discipline is tightened up in this change. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/bpf_lock_contention.c | 8 +- tools/perf/util/machine.c | 287 ++++---------------------- tools/perf/util/machine.h | 20 +- tools/perf/util/thread.c | 2 +- tools/perf/util/thread.h | 6 - tools/perf/util/threads.c | 244 ++++++++++++++++++++++ tools/perf/util/threads.h | 35 ++++ 8 files changed, 325 insertions(+), 278 deletions(-) create mode 100644 tools/perf/util/threads.c create mode 100644 tools/perf/util/threads.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 96058f949ec9..2cb39c3cf46d 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -71,6 +71,7 @@ perf-y += ordered-events.o perf-y += namespaces.o perf-y += comm.o perf-y += thread.o +perf-y += threads.o perf-y += thread_map.o perf-y += parse-events-flex.o perf-y += parse-events-bison.o diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index d9720a910330..52bbd6db2831 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -209,7 +209,7 @@ static const char *lock_contention_get_name(struct lock_contention *con, /* do not update idle comm which contains CPU number */ if (pid) { - struct thread *t = __machine__findnew_thread(machine, /*pid=*/-1, pid); + struct thread *t = machine__findnew_thread(machine, /*pid=*/-1, pid); if (t == NULL) return name; @@ -301,9 +301,9 @@ int lock_contention_read(struct lock_contention *con) return -1; if (con->aggr_mode == LOCK_AGGR_TASK) { - struct thread *idle = __machine__findnew_thread(machine, - /*pid=*/0, - /*tid=*/0); + struct thread *idle = machine__findnew_thread(machine, + /*pid=*/0, + /*tid=*/0); thread__set_comm(idle, "swapper", /*timestamp=*/0); } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 7e19303d1aa6..36231b5a86aa 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -44,9 +44,6 @@ #include #include -static void __machine__remove_thread(struct machine *machine, struct thread_rb_node *nd, - struct thread *th, bool lock); - static struct dso *machine__kernel_dso(struct machine *machine) { return map__dso(machine->vmlinux_map); @@ -59,35 +56,6 @@ static void dsos__init(struct dsos *dsos) init_rwsem(&dsos->lock); } -static void machine__threads_init(struct machine *machine) -{ - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; - threads->entries = RB_ROOT_CACHED; - init_rwsem(&threads->lock); - threads->nr = 0; - threads->last_match = NULL; - } -} - -static int thread_rb_node__cmp_tid(const void *key, const struct rb_node *nd) -{ - int to_find = (int) *((pid_t *)key); - - return to_find - (int)thread__tid(rb_entry(nd, struct thread_rb_node, rb_node)->thread); -} - -static struct thread_rb_node *thread_rb_node__find(const struct thread *th, - struct rb_root *tree) -{ - pid_t to_find = thread__tid(th); - struct rb_node *nd = rb_find(&to_find, tree, thread_rb_node__cmp_tid); - - return rb_entry(nd, struct thread_rb_node, rb_node); -} - static int machine__set_mmap_name(struct machine *machine) { if (machine__is_host(machine)) @@ -121,7 +89,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) RB_CLEAR_NODE(&machine->rb_node); dsos__init(&machine->dsos); - machine__threads_init(machine); + threads__init(&machine->threads); machine->vdso_info = NULL; machine->env = NULL; @@ -222,27 +190,11 @@ static void dsos__exit(struct dsos *dsos) void machine__delete_threads(struct machine *machine) { - struct rb_node *nd; - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; - down_write(&threads->lock); - nd = rb_first_cached(&threads->entries); - while (nd) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - - nd = rb_next(nd); - __machine__remove_thread(machine, trb, trb->thread, false); - } - up_write(&threads->lock); - } + threads__remove_all_threads(&machine->threads); } void machine__exit(struct machine *machine) { - int i; - if (machine == NULL) return; @@ -255,12 +207,7 @@ void machine__exit(struct machine *machine) zfree(&machine->current_tid); zfree(&machine->kallsyms_filename); - machine__delete_threads(machine); - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; - - exit_rwsem(&threads->lock); - } + threads__exit(&machine->threads); } void machine__delete(struct machine *machine) @@ -527,7 +474,7 @@ static void machine__update_thread_pid(struct machine *machine, if (thread__pid(th) == thread__tid(th)) return; - leader = __machine__findnew_thread(machine, thread__pid(th), thread__pid(th)); + leader = machine__findnew_thread(machine, thread__pid(th), thread__pid(th)); if (!leader) goto out_err; @@ -561,160 +508,55 @@ static void machine__update_thread_pid(struct machine *machine, goto out_put; } -/* - * Front-end cache - TID lookups come in blocks, - * so most of the time we dont have to look up - * the full rbtree: - */ -static struct thread* -__threads__get_last_match(struct threads *threads, struct machine *machine, - int pid, int tid) -{ - struct thread *th; - - th = threads->last_match; - if (th != NULL) { - if (thread__tid(th) == tid) { - machine__update_thread_pid(machine, th, pid); - return thread__get(th); - } - thread__put(threads->last_match); - threads->last_match = NULL; - } - - return NULL; -} - -static struct thread* -threads__get_last_match(struct threads *threads, struct machine *machine, - int pid, int tid) -{ - struct thread *th = NULL; - - if (perf_singlethreaded) - th = __threads__get_last_match(threads, machine, pid, tid); - - return th; -} - -static void -__threads__set_last_match(struct threads *threads, struct thread *th) -{ - thread__put(threads->last_match); - threads->last_match = thread__get(th); -} - -static void -threads__set_last_match(struct threads *threads, struct thread *th) -{ - if (perf_singlethreaded) - __threads__set_last_match(threads, th); -} - /* * Caller must eventually drop thread->refcnt returned with a successful * lookup/new thread inserted. */ -static struct thread *____machine__findnew_thread(struct machine *machine, - struct threads *threads, - pid_t pid, pid_t tid, - bool create) +static struct thread *__machine__findnew_thread(struct machine *machine, + pid_t pid, + pid_t tid, + bool create) { - struct rb_node **p = &threads->entries.rb_root.rb_node; - struct rb_node *parent = NULL; - struct thread *th; - struct thread_rb_node *nd; - bool leftmost = true; + struct thread *th = threads__find(&machine->threads, tid); + bool created; - th = threads__get_last_match(threads, machine, pid, tid); - if (th) + if (th) { + machine__update_thread_pid(machine, th, pid); return th; - - while (*p != NULL) { - parent = *p; - th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) == tid) { - threads__set_last_match(threads, th); - machine__update_thread_pid(machine, th, pid); - return thread__get(th); - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else { - p = &(*p)->rb_right; - leftmost = false; - } } - if (!create) return NULL; - th = thread__new(pid, tid); - if (th == NULL) - return NULL; - - nd = malloc(sizeof(*nd)); - if (nd == NULL) { - thread__put(th); - return NULL; - } - nd->thread = th; - - rb_link_node(&nd->rb_node, parent, p); - rb_insert_color_cached(&nd->rb_node, &threads->entries, leftmost); - /* - * We have to initialize maps separately after rb tree is updated. - * - * The reason is that we call machine__findnew_thread within - * thread__init_maps to find the thread leader and that would screwed - * the rb tree. - */ - if (thread__init_maps(th, machine)) { - pr_err("Thread init failed thread %d\n", pid); - rb_erase_cached(&nd->rb_node, &threads->entries); - RB_CLEAR_NODE(&nd->rb_node); - free(nd); - thread__put(th); - return NULL; - } - /* - * It is now in the rbtree, get a ref - */ - threads__set_last_match(threads, th); - ++threads->nr; - - return thread__get(th); -} + th = threads__findnew(&machine->threads, pid, tid, &created); + if (created) { + /* + * We have to initialize maps separately after rb tree is + * updated. + * + * The reason is that we call machine__findnew_thread within + * thread__init_maps to find the thread leader and that would + * screwed the rb tree. + */ + if (thread__init_maps(th, machine)) { + pr_err("Thread init failed thread %d\n", pid); + threads__remove(&machine->threads, th); + thread__put(th); + return NULL; + } + } else + machine__update_thread_pid(machine, th, pid); -struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) -{ - return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true); + return th; } -struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, - pid_t tid) +struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) { - struct threads *threads = machine__threads(machine, tid); - struct thread *th; - - down_write(&threads->lock); - th = __machine__findnew_thread(machine, pid, tid); - up_write(&threads->lock); - return th; + return __machine__findnew_thread(machine, pid, tid, /*create=*/true); } -struct thread *machine__find_thread(struct machine *machine, pid_t pid, - pid_t tid) +struct thread *machine__find_thread(struct machine *machine, pid_t pid, pid_t tid) { - struct threads *threads = machine__threads(machine, tid); - struct thread *th; - - down_read(&threads->lock); - th = ____machine__findnew_thread(machine, threads, pid, tid, false); - up_read(&threads->lock); - return th; + return __machine__findnew_thread(machine, pid, tid, /*create=*/false); } /* @@ -1128,23 +970,13 @@ static int machine_fprintf_cb(struct thread *thread, void *data) return 0; } -static size_t machine__threads_nr(const struct machine *machine) -{ - size_t nr = 0; - - for (int i = 0; i < THREADS__TABLE_SIZE; i++) - nr += machine->threads[i].nr; - - return nr; -} - size_t machine__fprintf(struct machine *machine, FILE *fp) { struct machine_fprintf_cb_args args = { .fp = fp, .printed = 0, }; - size_t ret = fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)); + size_t ret = fprintf(fp, "Threads: %zu\n", threads__nr(&machine->threads)); machine__for_each_thread(machine, machine_fprintf_cb, &args); return ret + args.printed; @@ -2066,36 +1898,9 @@ int machine__process_mmap_event(struct machine *machine, union perf_event *event return 0; } -static void __machine__remove_thread(struct machine *machine, struct thread_rb_node *nd, - struct thread *th, bool lock) -{ - struct threads *threads = machine__threads(machine, thread__tid(th)); - - if (!nd) - nd = thread_rb_node__find(th, &threads->entries.rb_root); - - if (threads->last_match && RC_CHK_EQUAL(threads->last_match, th)) - threads__set_last_match(threads, NULL); - - if (lock) - down_write(&threads->lock); - - BUG_ON(refcount_read(thread__refcnt(th)) == 0); - - thread__put(nd->thread); - rb_erase_cached(&nd->rb_node, &threads->entries); - RB_CLEAR_NODE(&nd->rb_node); - --threads->nr; - - free(nd); - - if (lock) - up_write(&threads->lock); -} - void machine__remove_thread(struct machine *machine, struct thread *th) { - return __machine__remove_thread(machine, NULL, th, true); + return threads__remove(&machine->threads, th); } int machine__process_fork_event(struct machine *machine, union perf_event *event, @@ -3229,23 +3034,7 @@ int machine__for_each_thread(struct machine *machine, int (*fn)(struct thread *thread, void *p), void *priv) { - struct threads *threads; - struct rb_node *nd; - int rc = 0; - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - threads = &machine->threads[i]; - for (nd = rb_first_cached(&threads->entries); nd; - nd = rb_next(nd)) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - - rc = fn(trb->thread, priv); - if (rc != 0) - return rc; - } - } - return rc; + return threads__for_each_thread(&machine->threads, fn, priv); } int machines__for_each_thread(struct machines *machines, diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index b738ce84817b..e28c787616fe 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -7,6 +7,7 @@ #include "maps.h" #include "dsos.h" #include "rwsem.h" +#include "threads.h" struct addr_location; struct branch_stack; @@ -28,16 +29,6 @@ extern const char *ref_reloc_sym_names[]; struct vdso_info; -#define THREADS__TABLE_BITS 8 -#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) - -struct threads { - struct rb_root_cached entries; - struct rw_semaphore lock; - unsigned int nr; - struct thread *last_match; -}; - struct machine { struct rb_node rb_node; pid_t pid; @@ -48,7 +39,7 @@ struct machine { char *root_dir; char *mmap_name; char *kallsyms_filename; - struct threads threads[THREADS__TABLE_SIZE]; + struct threads threads; struct vdso_info *vdso_info; struct perf_env *env; struct dsos dsos; @@ -69,12 +60,6 @@ struct machine { bool trampolines_mapped; }; -static inline struct threads *machine__threads(struct machine *machine, pid_t tid) -{ - /* Cast it to handle tid == -1 */ - return &machine->threads[(unsigned int)tid % THREADS__TABLE_SIZE]; -} - /* * The main kernel (vmlinux) map */ @@ -220,7 +205,6 @@ bool machine__is(struct machine *machine, const char *arch); bool machine__normalized_is(struct machine *machine, const char *arch); int machine__nr_cpus_avail(struct machine *machine); -struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid); struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid); struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id); diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index c59ab4d79163..1aa8962dcf52 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -26,7 +26,7 @@ int thread__init_maps(struct thread *thread, struct machine *machine) if (pid == thread__tid(thread) || pid == -1) { thread__set_maps(thread, maps__new(machine)); } else { - struct thread *leader = __machine__findnew_thread(machine, pid, pid); + struct thread *leader = machine__findnew_thread(machine, pid, pid); if (leader) { thread__set_maps(thread, maps__get(thread__maps(leader))); diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 0df775b5c110..4b8f3e9e513b 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -3,7 +3,6 @@ #define __PERF_THREAD_H #include -#include #include #include #include @@ -30,11 +29,6 @@ struct lbr_stitch { struct callchain_cursor_node *prev_lbr_cursor; }; -struct thread_rb_node { - struct rb_node rb_node; - struct thread *thread; -}; - DECLARE_RC_STRUCT(thread) { /** @maps: mmaps associated with this thread. */ struct maps *maps; diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c new file mode 100644 index 000000000000..d984ec939c7b --- /dev/null +++ b/tools/perf/util/threads.c @@ -0,0 +1,244 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "threads.h" +#include "machine.h" +#include "thread.h" + +struct thread_rb_node { + struct rb_node rb_node; + struct thread *thread; +}; + +static struct threads_table_entry *threads__table(struct threads *threads, pid_t tid) +{ + /* Cast it to handle tid == -1 */ + return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE]; +} + +void threads__init(struct threads *threads) +{ + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + + table->entries = RB_ROOT_CACHED; + init_rwsem(&table->lock); + table->nr = 0; + table->last_match = NULL; + } +} + +void threads__exit(struct threads *threads) +{ + threads__remove_all_threads(threads); + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + + exit_rwsem(&table->lock); + } +} + +size_t threads__nr(struct threads *threads) +{ + size_t nr = 0; + + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + + down_read(&table->lock); + nr += table->nr; + up_read(&table->lock); + } + return nr; +} + +/* + * Front-end cache - TID lookups come in blocks, + * so most of the time we dont have to look up + * the full rbtree: + */ +static struct thread *__threads_table_entry__get_last_match(struct threads_table_entry *table, + pid_t tid) +{ + struct thread *th, *res = NULL; + + th = table->last_match; + if (th != NULL) { + if (thread__tid(th) == tid) + res = thread__get(th); + } + return res; +} + +static void __threads_table_entry__set_last_match(struct threads_table_entry *table, + struct thread *th) +{ + thread__put(table->last_match); + table->last_match = thread__get(th); +} + +static void threads_table_entry__set_last_match(struct threads_table_entry *table, + struct thread *th) +{ + down_write(&table->lock); + __threads_table_entry__set_last_match(table, th); + up_write(&table->lock); +} + +struct thread *threads__find(struct threads *threads, pid_t tid) +{ + struct threads_table_entry *table = threads__table(threads, tid); + struct rb_node **p; + struct thread *res = NULL; + + down_read(&table->lock); + res = __threads_table_entry__get_last_match(table, tid); + if (res) + return res; + + p = &table->entries.rb_root.rb_node; + while (*p != NULL) { + struct rb_node *parent = *p; + struct thread *th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; + + if (thread__tid(th) == tid) { + res = thread__get(th); + break; + } + + if (tid < thread__tid(th)) + p = &(*p)->rb_left; + else + p = &(*p)->rb_right; + } + up_read(&table->lock); + if (res) + threads_table_entry__set_last_match(table, res); + return res; +} + +struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created) +{ + struct threads_table_entry *table = threads__table(threads, tid); + struct rb_node **p; + struct rb_node *parent = NULL; + struct thread *res = NULL; + struct thread_rb_node *nd; + bool leftmost = true; + + *created = false; + down_write(&table->lock); + p = &table->entries.rb_root.rb_node; + while (*p != NULL) { + struct thread *th; + + parent = *p; + th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; + + if (thread__tid(th) == tid) { + __threads_table_entry__set_last_match(table, th); + res = thread__get(th); + goto out_unlock; + } + + if (tid < thread__tid(th)) + p = &(*p)->rb_left; + else { + leftmost = false; + p = &(*p)->rb_right; + } + } + nd = malloc(sizeof(*nd)); + if (nd == NULL) + goto out_unlock; + res = thread__new(pid, tid); + if (!res) + free(nd); + else { + *created = true; + nd->thread = thread__get(res); + rb_link_node(&nd->rb_node, parent, p); + rb_insert_color_cached(&nd->rb_node, &table->entries, leftmost); + ++table->nr; + __threads_table_entry__set_last_match(table, res); + } +out_unlock: + up_write(&table->lock); + return res; +} + +void threads__remove_all_threads(struct threads *threads) +{ + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + struct rb_node *nd; + + down_write(&table->lock); + __threads_table_entry__set_last_match(table, NULL); + nd = rb_first_cached(&table->entries); + while (nd) { + struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); + + nd = rb_next(nd); + thread__put(trb->thread); + rb_erase_cached(&trb->rb_node, &table->entries); + RB_CLEAR_NODE(&trb->rb_node); + --table->nr; + + free(trb); + } + assert(table->nr == 0); + up_write(&table->lock); + } +} + +void threads__remove(struct threads *threads, struct thread *thread) +{ + struct rb_node **p; + struct threads_table_entry *table = threads__table(threads, thread__tid(thread)); + pid_t tid = thread__tid(thread); + + down_write(&table->lock); + if (table->last_match && RC_CHK_EQUAL(table->last_match, thread)) + __threads_table_entry__set_last_match(table, NULL); + + p = &table->entries.rb_root.rb_node; + while (*p != NULL) { + struct rb_node *parent = *p; + struct thread_rb_node *nd = rb_entry(parent, struct thread_rb_node, rb_node); + struct thread *th = nd->thread; + + if (RC_CHK_EQUAL(th, thread)) { + thread__put(nd->thread); + rb_erase_cached(&nd->rb_node, &table->entries); + RB_CLEAR_NODE(&nd->rb_node); + --table->nr; + free(nd); + break; + } + + if (tid < thread__tid(th)) + p = &(*p)->rb_left; + else + p = &(*p)->rb_right; + } + up_write(&table->lock); +} + +int threads__for_each_thread(struct threads *threads, + int (*fn)(struct thread *thread, void *data), + void *data) +{ + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + struct rb_node *nd; + + for (nd = rb_first_cached(&table->entries); nd; nd = rb_next(nd)) { + struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); + int rc = fn(trb->thread, data); + + if (rc != 0) + return rc; + } + } + return 0; + +} diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h new file mode 100644 index 000000000000..ed67de627578 --- /dev/null +++ b/tools/perf/util/threads.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_THREADS_H +#define __PERF_THREADS_H + +#include +#include "rwsem.h" + +struct thread; + +#define THREADS__TABLE_BITS 8 +#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) + +struct threads_table_entry { + struct rb_root_cached entries; + struct rw_semaphore lock; + unsigned int nr; + struct thread *last_match; +}; + +struct threads { + struct threads_table_entry table[THREADS__TABLE_SIZE]; +}; + +void threads__init(struct threads *threads); +void threads__exit(struct threads *threads); +size_t threads__nr(struct threads *threads); +struct thread *threads__find(struct threads *threads, pid_t tid); +struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created); +void threads__remove_all_threads(struct threads *threads); +void threads__remove(struct threads *threads, struct thread *thread); +int threads__for_each_thread(struct threads *threads, + int (*fn)(struct thread *thread, void *data), + void *data); + +#endif /* __PERF_THREADS_H */ From patchwork Tue Oct 24 22:23:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157760 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2241811vqx; Tue, 24 Oct 2023 15:37:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQEGqNvx0Jz6Rff8kplVlBxLLbxvZvkp806J288fpZP3kY0i61qIJ/3NECwxJQpGi+IHuT X-Received: by 2002:a05:6359:7384:b0:168:e902:de23 with SMTP id uz4-20020a056359738400b00168e902de23mr4314594rwb.9.1698187024101; Tue, 24 Oct 2023 15:37:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698187024; cv=none; d=google.com; s=arc-20160816; b=yXsR8XHMz86hQ+QqZ0ED91GMDlkPz+Jqk1BWE2lc/jbOUycjGobTsuC1zOEeJ9C+88 B1PE9nsWO5V7W8gJ4RbYK+r8WAh3alMvq/J2FDt1Wd4ngV2WwpXTx2xph+yY6fIp1iRH qOnTxymz/QwoiR0tZlGgdxACCoXvVyWs6BteN2xQitQ5SqwpwKRAFkS739Y5oPjJDVQo LwmG5PsCo4lGV9OoW1mZsZtIw71N4X1LPd5NZF+DCo5o1kOEdzVRX3iDySSxW71nAR1w L9JNmILpedDF/MPpJclPTQsHUYnibsMxOENu/D/8IvvQBsn2t4hTGrjmZnA4H4oLYBix +G0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=ax4wGMolFNFmvAbtQbFOoT88EvyDd7TbOe2UXPEasko=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=z49J7PQv1CyFxwNNqLyV+1vPNECLeKWjLSQZbuqlk4O6EuZGMYlrm5A9bWbj+sUvRR TCAHljni1Jz2K90hRD55+RtR+bt/KySYl3aubE7mswCzP1lS5ZywPgW+FlSdf7qpS2Om kufBfEm+RdhQwzS/fxxq7N03oHbm6gK3By37aYOUpTyIgidikjxpKA52VLJIXzQJss7W Yjj3UbqvIDAx+Nl48J2PU+Dzo8roxYions/Y/zxg1HMGuu1KJ/SxdiYWWbTKWP45FVDF QfnyuU8aUk2kYZxqJXSKWpn4cVGW0hEQXlZbJ88nspeUld6C3pYYKk64KY1xWrLHv704 VJQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YfowOvCu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id bv125-20020a632e83000000b005859c81f1e9si9058926pgb.229.2023.10.24.15.37.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:37:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YfowOvCu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5BF2480219C3; Tue, 24 Oct 2023 15:37:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344675AbjJXWgt (ORCPT + 27 others); Tue, 24 Oct 2023 18:36:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344662AbjJXWg2 (ORCPT ); Tue, 24 Oct 2023 18:36:28 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE251325B for ; Tue, 24 Oct 2023 15:26:04 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da0631f977bso514163276.2 for ; Tue, 24 Oct 2023 15:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186363; x=1698791163; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ax4wGMolFNFmvAbtQbFOoT88EvyDd7TbOe2UXPEasko=; b=YfowOvCuZhHULKo4/Orsria5zBpULDar8L1uotSLOtlWhkXsAKVeVjpeLPajKuNBXY msNGFgD7DbZ2z6iGEaULOE0qjZh9OBixYS5F3doRziyBebBdryGHCHZF6/L9mUH4R6yW m5Rzd8L1jYihFBiIQZHpCeA1NoJ6RQkcHCnk1vORMyWIT221USmoYgOIDScWwEZ1Svdm X69iR98au8Pt9H3m0oJ7F8CZvYsl4e8MW0618+G+tf/U0zl0lbRJvYQVzh1tImuy5eKL yT62rfTrRG53XorAE4sWKPvrl++mIMH3Jvr+ubHvMae3ZCxZK+Q7g0YddwPRxcDz50Zy NfLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186363; x=1698791163; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ax4wGMolFNFmvAbtQbFOoT88EvyDd7TbOe2UXPEasko=; b=ZfMp00SCiWxxyIxDG+bblryf8tJI/HheGfTz+KLvKS4FQpy56xaXg6+yjiy4gFxkhm s41s0lYF5tXgwMP9d0ykiGolJaJWbVsELEkRdWI4+7w2YkQaKX0zSzQjhixH8stdXzva SPJ9tGXyvcnJ+Oo3UAHOe/gFHVkBwzLEb5FsNJJMOFETnbpq4ISh0ovg6WlyTH4mD84G sX/Ku3cxDSzlCjl1rBIg4uwq/y4Y4rxo7payhBuyv4UnPOrNPC6+kTB4Xov+EscBNLlj kjl1Ihsej/qAlaqM8X5zpgqQ6kBfz6aRIrswZcRcSWeMHBj3tb+LyOIfiIJYks1Hmh1X MvKA== X-Gm-Message-State: AOJu0YyqJNJvmp2PUDkcp0qsi1GKovViilJI/milhDS8KQJiVy4kDzko dzJx5cXL2l0ECEDFNk2jnH6zlYoFVHlK X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:e74b:0:b0:d8b:737f:8246 with SMTP id e72-20020a25e74b000000b00d8b737f8246mr252913ybh.2.1698186363248; Tue, 24 Oct 2023 15:26:03 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:52 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-50-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 49/50] perf threads: Switch from rbtree to hashmap From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:37:01 -0700 (PDT) X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780678156777376382 X-GMAIL-MSGID: 1780678156777376382 The rbtree provides a sorting on entries but this is unused. Switch to using hashmap for O(1) rather than O(log n) find/insert/remove complexity. Signed-off-by: Ian Rogers --- tools/perf/util/threads.c | 146 ++++++++++++-------------------------- tools/perf/util/threads.h | 6 +- 2 files changed, 47 insertions(+), 105 deletions(-) diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c index d984ec939c7b..55923be53180 100644 --- a/tools/perf/util/threads.c +++ b/tools/perf/util/threads.c @@ -3,25 +3,30 @@ #include "machine.h" #include "thread.h" -struct thread_rb_node { - struct rb_node rb_node; - struct thread *thread; -}; - static struct threads_table_entry *threads__table(struct threads *threads, pid_t tid) { /* Cast it to handle tid == -1 */ return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE]; } +static size_t key_hash(long key, void *ctx __maybe_unused) +{ + /* The table lookup removes low bit entropy, but this is just ignored here. */ + return key; +} + +static bool key_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 == key2; +} + void threads__init(struct threads *threads) { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; - table->entries = RB_ROOT_CACHED; + hashmap__init(&table->shard, key_hash, key_equal, NULL); init_rwsem(&table->lock); - table->nr = 0; table->last_match = NULL; } } @@ -32,6 +37,7 @@ void threads__exit(struct threads *threads) for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; + hashmap__clear(&table->shard); exit_rwsem(&table->lock); } } @@ -44,7 +50,7 @@ size_t threads__nr(struct threads *threads) struct threads_table_entry *table = &threads->table[i]; down_read(&table->lock); - nr += table->nr; + nr += hashmap__size(&table->shard); up_read(&table->lock); } return nr; @@ -86,28 +92,13 @@ static void threads_table_entry__set_last_match(struct threads_table_entry *tabl struct thread *threads__find(struct threads *threads, pid_t tid) { struct threads_table_entry *table = threads__table(threads, tid); - struct rb_node **p; - struct thread *res = NULL; + struct thread *res; down_read(&table->lock); res = __threads_table_entry__get_last_match(table, tid); - if (res) - return res; - - p = &table->entries.rb_root.rb_node; - while (*p != NULL) { - struct rb_node *parent = *p; - struct thread *th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) == tid) { - res = thread__get(th); - break; - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else - p = &(*p)->rb_right; + if (!res) { + if (hashmap__find(&table->shard, tid, &res)) + res = thread__get(res); } up_read(&table->lock); if (res) @@ -118,49 +109,25 @@ struct thread *threads__find(struct threads *threads, pid_t tid) struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created) { struct threads_table_entry *table = threads__table(threads, tid); - struct rb_node **p; - struct rb_node *parent = NULL; struct thread *res = NULL; - struct thread_rb_node *nd; - bool leftmost = true; *created = false; down_write(&table->lock); - p = &table->entries.rb_root.rb_node; - while (*p != NULL) { - struct thread *th; - - parent = *p; - th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) == tid) { - __threads_table_entry__set_last_match(table, th); - res = thread__get(th); - goto out_unlock; - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else { - leftmost = false; - p = &(*p)->rb_right; - } - } - nd = malloc(sizeof(*nd)); - if (nd == NULL) - goto out_unlock; res = thread__new(pid, tid); - if (!res) - free(nd); - else { - *created = true; - nd->thread = thread__get(res); - rb_link_node(&nd->rb_node, parent, p); - rb_insert_color_cached(&nd->rb_node, &table->entries, leftmost); - ++table->nr; - __threads_table_entry__set_last_match(table, res); + if (res) { + if (hashmap__add(&table->shard, tid, res)) { + /* Add failed. Assume a race so find other entry. */ + thread__put(res); + res = NULL; + if (hashmap__find(&table->shard, tid, &res)) + res = thread__get(res); + } else { + res = thread__get(res); + *created = true; + } + if (res) + __threads_table_entry__set_last_match(table, res); } -out_unlock: up_write(&table->lock); return res; } @@ -169,57 +136,32 @@ void threads__remove_all_threads(struct threads *threads) { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; - struct rb_node *nd; + struct hashmap_entry *cur, *tmp; + size_t bkt; down_write(&table->lock); __threads_table_entry__set_last_match(table, NULL); - nd = rb_first_cached(&table->entries); - while (nd) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - - nd = rb_next(nd); - thread__put(trb->thread); - rb_erase_cached(&trb->rb_node, &table->entries); - RB_CLEAR_NODE(&trb->rb_node); - --table->nr; + hashmap__for_each_entry_safe((&table->shard), cur, tmp, bkt) { + struct thread *old_value; - free(trb); + hashmap__delete(&table->shard, cur->key, /*old_key=*/NULL, &old_value); + thread__put(old_value); } - assert(table->nr == 0); up_write(&table->lock); } } void threads__remove(struct threads *threads, struct thread *thread) { - struct rb_node **p; struct threads_table_entry *table = threads__table(threads, thread__tid(thread)); - pid_t tid = thread__tid(thread); + struct thread *old_value; down_write(&table->lock); if (table->last_match && RC_CHK_EQUAL(table->last_match, thread)) __threads_table_entry__set_last_match(table, NULL); - p = &table->entries.rb_root.rb_node; - while (*p != NULL) { - struct rb_node *parent = *p; - struct thread_rb_node *nd = rb_entry(parent, struct thread_rb_node, rb_node); - struct thread *th = nd->thread; - - if (RC_CHK_EQUAL(th, thread)) { - thread__put(nd->thread); - rb_erase_cached(&nd->rb_node, &table->entries); - RB_CLEAR_NODE(&nd->rb_node); - --table->nr; - free(nd); - break; - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else - p = &(*p)->rb_right; - } + hashmap__delete(&table->shard, thread__tid(thread), /*old_key=*/NULL, &old_value); + thread__put(old_value); up_write(&table->lock); } @@ -229,11 +171,11 @@ int threads__for_each_thread(struct threads *threads, { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; - struct rb_node *nd; + struct hashmap_entry *cur; + size_t bkt; - for (nd = rb_first_cached(&table->entries); nd; nd = rb_next(nd)) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - int rc = fn(trb->thread, data); + hashmap__for_each_entry((&table->shard), cur, bkt) { + int rc = fn((struct thread *)cur->pvalue, data); if (rc != 0) return rc; diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h index ed67de627578..d03bd91a7769 100644 --- a/tools/perf/util/threads.h +++ b/tools/perf/util/threads.h @@ -2,7 +2,7 @@ #ifndef __PERF_THREADS_H #define __PERF_THREADS_H -#include +#include "hashmap.h" #include "rwsem.h" struct thread; @@ -11,9 +11,9 @@ struct thread; #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) struct threads_table_entry { - struct rb_root_cached entries; + /* Key is tid, value is struct thread. */ + struct hashmap shard; struct rw_semaphore lock; - unsigned int nr; struct thread *last_match; }; From patchwork Tue Oct 24 22:23:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 157757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2240274vqx; Tue, 24 Oct 2023 15:32:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwssl1IPaRnytRny2FO6Q40Nv8vObfeh7mKbV87ZFBcdhGBudD7tizJKwhJMEMopqej9vY X-Received: by 2002:a05:6a21:35c4:b0:137:23f1:4281 with SMTP id ba4-20020a056a2135c400b0013723f14281mr3659176pzc.12.1698186769193; Tue, 24 Oct 2023 15:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186769; cv=none; d=google.com; s=arc-20160816; b=lr8EqP9xQS3YWF++rLUbA8p7eXux2T7aMU7f8wHUa8O4qIy4GAFy+Lo2uCWOy8lwU0 2v8BbBNhRZ8HA+gJcPTVwBWbPYLLXNdJoRWKpv2zDPqBTYZ/XLYOdRz07UOgTKEo/E8+ DHpnhg8QgArCdoWKRpWx9mk428uhmU0mqmBKNSkFhszlXXTzPoVDhmCTtAstieuhsNHq K1sbz1lM0ssNC5wDdyQQT5+U+ybaU13NUyHBbHbPCFY/0j/PR/F0zjFsd4yyWUT8Nzth xXD9BCDy6u3VxNlG0Ug6c8s91C5GrM7VGf8AthOrQh05Mkouy4wzP8349XQ1N0xHW5jt kwcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=5sjI1f5vh005/1ecdkhswjpcyMDjRf+co04ydutakFc=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=X98sUs/6Lk83f5Q5fXwOpWHX9OgTdMWuGJWfZxrmRWomDO542+YIVbU40QArCrfje6 eJDAW8uvpWAjbM/tsuoMGNomE7mf9c4xCn25ZlHi8gLLM0WTcDpleM1mwc/ozgdCvkeJ pgCrDb+oJheyuDiHbNfIM0DWgJ25S5bKPod3ce/iJkpb4BhqVEeLlIaMtFwJ3uGnmS8r NbgAyFrRyz0tMuTxCqNWGMESKA2CsWIBZec+b16ZEiqfoOk50xNq8x017gYSp3V1+tII iQ5HJDYwNWKEIqJq/TgcSDE8+ZhG0+pvCYAiDBkh+ICLCeoCDkFt0C/bkJ0SwrC1yJ8o 5IKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="T8zHFj/O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id ga16-20020a17090b039000b0027000086c93si8899827pjb.102.2023.10.24.15.32.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:32:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="T8zHFj/O"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 523728024B15; Tue, 24 Oct 2023 15:32:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344509AbjJXWch (ORCPT + 27 others); Tue, 24 Oct 2023 18:32:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344515AbjJXWce (ORCPT ); Tue, 24 Oct 2023 18:32:34 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B9A82106 for ; Tue, 24 Oct 2023 15:32:09 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da05c625cb9so771924276.2 for ; Tue, 24 Oct 2023 15:32:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186728; x=1698791528; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=5sjI1f5vh005/1ecdkhswjpcyMDjRf+co04ydutakFc=; b=T8zHFj/OHVjxmE05D5BJ81aNeynwMc6CaVRPxPYfjq3TZLWjCO/t2e4VRGeiijxxHr Fl6dmf5od/bGTyYZoI2YpgJCdESo007yg573epmyX48R2DVZSjLA+zv/YkzQDLTaS7fg L7UKpvvzBsZ/4IpS1hTf9wno/5EzN7N0jvmsg3YAyT2NGaAwKwdbZVRza4jSz/nBjlYe KS6X98rkNrMJNGhmjI6lZs8t4r1B21miEBxXTrhIEqURKzPkDKmFhpgsIeGNZKis1U09 zUnqnAVTSmP3F1P3IXHpn0sLXJA8LDcEdrSBWDlK0dwEUg80g/Y7xKEwXbpZEmTTkpbz tx/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186728; x=1698791528; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5sjI1f5vh005/1ecdkhswjpcyMDjRf+co04ydutakFc=; b=P3RvO6LWsY7iMEmkRsKpMFt2YxElEGaisvDm7sP7vXr+BqZVxmqd2s4ET61NPNDT8q Q2ZPI5qKTFc2cEJgNvC6DMuNwUKyaCQYMyWL55hk9y8O0ZfI8WEfocysd3BjQo9AJt11 DcyQPXzcgBZf2yrF6MiBhLe8GBijP47aDwbYsVDnyKiPvpWeMXW50EQ7JzB0KyIgdpNv ciXUO2xbGf97a6ZcIoL3pERHptzcH4Q08OjIyM9xAcbiF7xCHd1Iqnp4CTT+RLbwJx2q bCyOpCfJxofchnc6G+jDc+QuCZ+XOSNVBQGFYrbtEd2U2AuAJyou+jMdgpHH28+z21Rp xcOA== X-Gm-Message-State: AOJu0Yy0OzGu0hMGlJu2GQ5u1az7vtTTQqv4KZYLS+UyoqnRt2m53pXy qKsVOZKl1s0FMgB0P0Kzh97EmCQVqu6X X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:181e:b0:da0:622b:553b with SMTP id cf30-20020a056902181e00b00da0622b553bmr29800ybb.12.1698186365779; Tue, 24 Oct 2023 15:26:05 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:53 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-51-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 50/50] perf threads: Reduce table size from 256 to 8 From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:32:46 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780677889579939794 X-GMAIL-MSGID: 1780677889579939794 The threads data structure is an array of hashmaps, previously rbtrees. The two levels allows for a fixed outer array where access is guarded by rw_semaphores. Commit 91e467bc568f ("perf machine: Use hashtable for machine threads") sized the outer table at 256 entries to avoid future scalability problems, however, this means the threads struct is sized at 30,720 bytes. As the hashmaps allow O(1) access for the common find/insert/remove operations, lower the number of entries to 8. This reduces the size overhead to 960 bytes. Signed-off-by: Ian Rogers --- tools/perf/util/threads.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h index d03bd91a7769..da68d2223f18 100644 --- a/tools/perf/util/threads.h +++ b/tools/perf/util/threads.h @@ -7,7 +7,7 @@ struct thread; -#define THREADS__TABLE_BITS 8 +#define THREADS__TABLE_BITS 3 #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) struct threads_table_entry {