Message ID | 20231102175735.2272696-15-irogers@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp533671vqu; Thu, 2 Nov 2023 10:59:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IErYYLzuj9ocAhqCIJpSLdtD6Xrg0jA5MYOizv5ovTH4V454a+oFJ3Q3gM4AB/0tFtNM9Pk X-Received: by 2002:a05:6a00:2d81:b0:68f:b015:ea99 with SMTP id fb1-20020a056a002d8100b0068fb015ea99mr580897pfb.10.1698947984879; Thu, 02 Nov 2023 10:59:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698947984; cv=none; d=google.com; s=arc-20160816; b=OiGPXaY2quC5W3UZEcu0FRzXJqh/hRslZqYbvv/e4qsL+cxTVU3V26H/t6/8dYNQ1Y R4STEb3vPMrl73KT2mMfcZqHiQirA5bec9PeZbzkvjEzKvAgIIDyeI2tkcXMy6KHNO0i 6OneLHYEnoa7hWPpDYR0t5K6NAM/wP22PYXt5ESzXpZkKtg9pWjmM454JKMRorDIhlbU I8a5n07xUPPpOr79v47Arl51g5axN6t/T4B6lLrXXyPkJp2oSJodpCkvUQgl8V/xTnXM U1j5Ts5+0ysCmYTqv6KQD8EzgGsxH8IeE8UoRU972ervQqlfzWQSumVyfW53VvlDjIDN i/DQ== 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=WMTfdeCRVpj8dwEZx7O7EjXiQYBzATWSFM6Du/0vAx8=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=YDgMm37+Y5W9s+0Y2uFe0V52b++aDPY435Sr+7dMY3pgACFZVNcGayym+NSbjKSK2a hHRqAZoz3n7VTqgUHHKjsNOWAyD8iF3oCq3gOujk91y81epSORe+OVoFraHphmKaX7MI 8AAAkm0sj9KI05XPPk8wxT+kt/aHYSVjcljTo62u0Xp/pl6c1ll19vaCXfTxKJbXeydE WGCsUxg3sdxHurfBZLF/n3EwmunuY6pqQy+xOd3YJPW/xZyJU10Y1kG7Z+xtlXDXcfwX R4JXlabhMOvZdTb4Wbtq/YKKhUC0BCWBXK6Y+DGxNJityRPRrC0hYSRaSYN69iaPB8oq S8VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="q/XVPfZO"; 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 y4-20020a636404000000b005acf1d3b2a2si70143pgb.174.2023.11.02.10.59.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 10:59:44 -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="q/XVPfZO"; 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 4491282B1C6C; Thu, 2 Nov 2023 10:59:43 -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 S1377192AbjKBR7a (ORCPT <rfc822;heyuhang3455@gmail.com> + 35 others); Thu, 2 Nov 2023 13:59:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376349AbjKBR6w (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 2 Nov 2023 13:58:52 -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 5710E10F9 for <linux-kernel@vger.kernel.org>; Thu, 2 Nov 2023 10:58:29 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da0cb98f66cso1415101276.2 for <linux-kernel@vger.kernel.org>; Thu, 02 Nov 2023 10:58:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947908; x=1699552708; 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=WMTfdeCRVpj8dwEZx7O7EjXiQYBzATWSFM6Du/0vAx8=; b=q/XVPfZOz+2YgLqPd9ekIDeuJHAZW0x/f4zoCiwtpkYHYr4soTzVQ3KEOAeHmHuvL5 2NpKtaqzwFW4EaQt9iHOk8HoZhuB6BGTs+C0RVhScaA6w51BuuZ2YFXSglKPhf5QOb7M /qRGNhF+DxuRYCKPYRnUHtLg/8aWO08X65NqcM2WkUZxiBXOapTRUMMVv3p1HITZSw6M JTQmuKDXNmtumuHX5s2KN++PnkTSxoYNKBMp5+GxtbtwLcBniu+1eQU5Hbbyx0lfGlRN uBmECYLembHfafeE9gIaRyy+dLcEGbahnjJIp9de8ultgEmjPNh36KNnaEi0WEKTX2ab smlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947908; x=1699552708; 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=WMTfdeCRVpj8dwEZx7O7EjXiQYBzATWSFM6Du/0vAx8=; b=OahwpmAqueU30gHQPMx4TeerUWqnn/G9lmXrgmqr1TZEW6Xfv2nNf9yx09SwG+hYgR VbJ+Y4i2UUGSLZT6+TP8+eOMqsLDw/ZISM9iKtYI40gfMQjl3h4hUJU6CK5NwRdHJx5W hk8XKJSGSUYwbsICIdZNBAEhbWGTAd8oj+ixc7jlV7Jr7dPClH9oPyatx3JG+tGUfMFS kFommXhDaTUImiXM17JU6CVADP8iuRqGWZnBkUuYVfnI58/aMMlTXtTB3nWwIvmwfavY hxhOii7zKff3ALUdJjGmCf4PIlRHHHB2L0ZkxazbRaOmUCzg3CYdrCdz0TQSHvWrUDJQ FGfA== X-Gm-Message-State: AOJu0Yyb89eHxFV+1Qg2KZ/ZdjewXPqJV2bo8jQ8yGUWjuk5DcqUIsoc WS1o9EdQ1AxXUMuSQItZJQBfEj7PKA+0 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a25:8706:0:b0:d9a:be7b:283c with SMTP id a6-20020a258706000000b00d9abe7b283cmr407391ybl.0.1698947908307; Thu, 02 Nov 2023 10:58:28 -0700 (PDT) Date: Thu, 2 Nov 2023 10:56:56 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-15-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 14/53] perf events: Remove scandir in thread synthesis From: Ian Rogers <irogers@google.com> To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>, Arnaldo Carvalho de Melo <acme@kernel.org>, Mark Rutland <mark.rutland@arm.com>, Alexander Shishkin <alexander.shishkin@linux.intel.com>, Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>, Ian Rogers <irogers@google.com>, Adrian Hunter <adrian.hunter@intel.com>, Nick Terrell <terrelln@fb.com>, Kan Liang <kan.liang@linux.intel.com>, Andi Kleen <ak@linux.intel.com>, Kajol Jain <kjain@linux.ibm.com>, Athira Rajeev <atrajeev@linux.vnet.ibm.com>, Huacai Chen <chenhuacai@kernel.org>, Masami Hiramatsu <mhiramat@kernel.org>, Vincent Whitchurch <vincent.whitchurch@axis.com>, "Steinar H. Gunderson" <sesse@google.com>, Liam Howlett <liam.howlett@oracle.com>, Miguel Ojeda <ojeda@kernel.org>, Colin Ian King <colin.i.king@gmail.com>, Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong <yangjihong1@huawei.com>, Ming Wang <wangming01@loongson.cn>, James Clark <james.clark@arm.com>, K Prateek Nayak <kprateek.nayak@amd.com>, Sean Christopherson <seanjc@google.com>, Leo Yan <leo.yan@linaro.org>, Ravi Bangoria <ravi.bangoria@amd.com>, German Gomez <german.gomez@arm.com>, Changbin Du <changbin.du@huawei.com>, Paolo Bonzini <pbonzini@redhat.com>, Li Dong <lidong@vivo.com>, Sandipan Das <sandipan.das@amd.com>, liuwenyu <liuwenyu7@huawei.com>, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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,T_SCC_BODY_TEXT_LINE,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: <linux-kernel.vger.kernel.org> 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]); Thu, 02 Nov 2023 10:59:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781476082143104213 X-GMAIL-MSGID: 1781476082143104213 |
Series |
Improvements to memory use
|
|
Commit Message
Ian Rogers
Nov. 2, 2023, 5:56 p.m. UTC
This avoids scanddir reading the directory into memory that's
allocated and instead allocates on the stack.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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 <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ #include <api/fs/fs.h> #include <api/io.h> +#include <api/io_dir.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> @@ -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; }