Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2185722rda; Tue, 24 Oct 2023 15:26:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF1khFX5V4g7xIhhgDEldc2g0zn4i9PEGhUNTsxDGZo4tQN46LXc9cYcmQHQINcFW+M525W X-Received: by 2002:a05:6870:5487:b0:1e9:b47e:66c3 with SMTP id f7-20020a056870548700b001e9b47e66c3mr16098174oan.35.1698186403977; Tue, 24 Oct 2023 15:26:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186403; cv=none; d=google.com; s=arc-20160816; b=w6ztGZZOy7bk492uv72tAHkRSQa86m+rn0oykhxqtF4rfK5V/KKnwzw8vm9ruvklEf TIq9S8OID/lKw4yfX6bql6q2dVWVjqK5besVydLdyOQzDDqyqoVw+YnGNca5JSoDPTa/ ldZhH1Lffnymru8OJeRT+YKRkD0ZmqDWkr4mPlpIu/QZoq0DcqxnX7MzCXSqmHcCFWe9 TI5ijqMOw96ecsHt5Xc54wypsZca+ZYU5U+Rb57gjaOnri3MGSmJwdZowBle8dplj1IP Y3QDS4iwb5zY1ArtCYI2huUjL9edOH/xXCAnHVcuTU9sSFkij3CEqRndJqpOLhgreRec S3iA== 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 Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id cb21-20020a056a02071500b0057777451a91si9668815pgb.581.2023.10.24.15.26.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:43 -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 487CC8093D4E; Tue, 24 Oct 2023 15:26:27 -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 S1344568AbjJXW0K (ORCPT + 99 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 Content-Type: text/plain; charset="UTF-8" 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:27 -0700 (PDT) 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; } -- 2.42.0.758.gaed0368e0e-goog