Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp553713rdb; Thu, 2 Nov 2023 10:59:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVCVKx14tp2f2qu0r7y2uMVnZe5ydcKvajSPuf8WY9M5ZwYxr2SpRRXZqiYLaGTO2NCwy0 X-Received: by 2002:a05:6a20:7d83:b0:15e:9923:3e35 with SMTP id v3-20020a056a207d8300b0015e99233e35mr285134pzj.19.1698947982164; Thu, 02 Nov 2023 10:59:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698947982; cv=none; d=google.com; s=arc-20160816; b=QhU769s3uhVewTpXyGqt36baj1rA9P1I9/nFsz3UPlTOOOGYS3x5lLf2QziO6NmaZe Pjy4NLufNQr03ekUcKPVtis4MhKjElrcWgE1xGOsrJBtLNJErA9Q5xpzop40ig01e0ZF /xPAWiH56ixZq1Yi3YtU17cHULvnD2wmFCr52sxQ34MURov9nB+3B4hoFiM7g/P7CNYb Sm0YrPPlQRS8S0Xjdc30mlYGtZpJRMwLk40vdQ8cJQyaGvmH69W/wDkCd249HcsDFF/W UZ4AQhD2QJVnzV0IYMNU+s7HDWpfNn4JuLR96Gk8xLVnhnx3dP8pegTm1jhaPBQLL5fW mXvA== 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 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 Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id cn1-20020a056a020a8100b00589884fef91si51127pgb.740.2023.11.02.10.59.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 10:59:42 -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="q/XVPfZO"; 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 1D7C882B1C5E; Thu, 2 Nov 2023 10:59:41 -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 S1377189AbjKBR7a (ORCPT + 99 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 ); 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 ; Thu, 2 Nov 2023 10:58:29 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da0cb98f66cso1415101276.2 for ; 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 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 , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , 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: 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:41 -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.869.gea05f2083d-goog