Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3427959rdh; Mon, 27 Nov 2023 14:11:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IH2OPPC82iRDAdsuTJFXsu8fcDnCF2cTDjMI7Fu4TOrKI11gX1UnJK8qjUQaC/ImKpRP+3W X-Received: by 2002:a05:6808:1392:b0:3b8:45cf:9b2 with SMTP id c18-20020a056808139200b003b845cf09b2mr17615933oiw.20.1701123079745; Mon, 27 Nov 2023 14:11:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123079; cv=none; d=google.com; s=arc-20160816; b=tFOQN09SWdwaUQ9B0GHFY0k+cmUOh8xkraWe4BMHUVxnDwVnZNSRujm3KesNsQaH26 DOtBHio9V/9AG/SaTi+AYj1fo24LCNDYRhDAS13Y/08yr3dNBGMeG06fD9Qo0uBZunln imcgAE/kdkdyWSoxoucwocivqeNWsIR4h60h5znDHTVeQeK/F4NbPaMGaLGM/mCCqBiH C11sKyEHm761atU4O3acn3qbkXTSiq74SNyb7seRHueD99Y7CRPGozcOWWqng5r79XHI 7X2F5bGOtNwORiqHM3ToC9J0Iay+hCSMGwOreV1GKxr/vCdY4Zp81dFBkMA4Z1VAQmxR bOVA== 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=jTXjNUPwbxQAEQgmTX6mZhuaXcNBEgojUyDX7h5oWL4=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=Y4iA/f8Qx8B6hQMxIzznMIXqdgKz6x+pt/YfyZs7oIs83e8bhKOG+qClyxbL+2nHFZ Ls/F5DTPVVP/WeW4VQIx0l6em6XhhbZGptpEmO7j0ggTnxsFmpzpyOcJM9xzMJeiEC4i i7qWt2I06y79RsDPYRlDtAcZgVdAPycxnhnj+e0ZgV3o8fN5HcNG6ZouAtPWEnUiOFPp ucXgw5D+fYfqvia21o+AWFV0ziqVSVNHR2hJIKUpc7D+gwc7WfzuAg8Zh0HiaXyahhRD NeYM5wsdHa7tPCKI3E320/hA/UxWxZLv3mo3CzVQDMqYhuvGNkGrLJ1y+7ZFnzzMAONi 0m4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gaFHynnw; 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 w20-20020a634754000000b005c1cd1e03d9si10599032pgk.112.2023.11.27.14.11.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:11:19 -0800 (PST) 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=gaFHynnw; 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 EAD1D80870EC; Mon, 27 Nov 2023 14:10:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233838AbjK0WKY (ORCPT + 99 others); Mon, 27 Nov 2023 17:10:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233571AbjK0WJ7 (ORCPT ); Mon, 27 Nov 2023 17:09:59 -0500 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 212FB1BD8 for ; Mon, 27 Nov 2023 14:09:50 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d04540d5aaso21623517b3.1 for ; Mon, 27 Nov 2023 14:09:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122989; x=1701727789; 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=jTXjNUPwbxQAEQgmTX6mZhuaXcNBEgojUyDX7h5oWL4=; b=gaFHynnwMcnRMOSKlWtWX99TjWcgrYlOUg5hj3kibRIit5u3rZHZtDBzwTcH3s44p1 jrcY2LS0XuLUCHUNBAmbtgCxfbujEW+egZh3E9Fm1eMtm9I4lBxGDtiSRDSrjvuoefiK vy5ZW4xMp/+E/XTK1N9NnnhjH72/Q/phwB1XvBOnO0MgLdX5Qp7pfdrfHG9GA5TA0JWK 6R8MSGNgJ79dxjcX8PSrSBPvl1WTjgxI38lBM/uaz0q2EEVK2/q4gbKcmuoWMHruPGvt LsolBCI0l9dxAGJrp3QShhQDfrEeeJ9y1MSnu+gtcwbScqhUvXCYjopNdAr9ym/jNPUc Vs+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122989; x=1701727789; 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=jTXjNUPwbxQAEQgmTX6mZhuaXcNBEgojUyDX7h5oWL4=; b=gtPjW7OcqOvclJDYNmv+agL1yUmcwxRz4fnZhBcGAxMtyEWOdRIfhfJ/ozENs6k/2x 9r14U6PUHsaLsfCPlpxEHCeAlVHIxPfdKKt/HLNN53iOyYz5kHuh4RvaeGG4D+24K3iP CCLRck5/P9FyItOTzSZ1JC3yRz/ZgBQE3usBo1L46XjMbpZ3mt1aZyX9w6rrXDDQWsp4 whQsT+ACZ3J8A2HwlpjiryHbbbqWVCv73MFhVOeeYH93Uf8qKfhxHqaQIezvfNDZHR9c tlMz8A2AfDwCQKIwLv/HU8TjkU4v8FTSHOHlKam7idxnJPy7fq47OgxTZX+6ALHAv9WB vAzw== X-Gm-Message-State: AOJu0Yx7ElzNIn/FJcIG7PoytyrYuRd2Iol5HDJgunuNsYmO7R/bRtOO tMl8tKX93Rk1GZcioAZOCvaqg3goSJAD X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a81:be14:0:b0:5d0:a744:7194 with SMTP id i20-20020a81be14000000b005d0a7447194mr137899ywn.2.1701122989163; Mon, 27 Nov 2023 14:09:49 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:23 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-12-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 11/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 , 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, Guilherme Amadio 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,T_SCC_BODY_TEXT_LINE, 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]); Mon, 27 Nov 2023 14:10:48 -0800 (PST) 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.43.0.rc1.413.gea7ed67945-goog