Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1436635rdb; Wed, 6 Dec 2023 21:06:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGE3OJAXBLBnDZrGUeVbvV79jkc4P6VqMmi7zQkpgsEqGbaakQ9TnO77iw37/ZXHzNmSOGv X-Received: by 2002:a05:6a20:2449:b0:18f:97c:6152 with SMTP id t9-20020a056a20244900b0018f097c6152mr2369236pzc.79.1701925581737; Wed, 06 Dec 2023 21:06:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701925581; cv=none; d=google.com; s=arc-20160816; b=ValUMLGEz359h53HFZqv1t6EwcQtp7POq7YUBl5adzEv6XJgRvYb0/C3yEcq31llEz jRZN+n8lFFcTtUYD6Nttv9lFmE62Km3RSUYXKSbfoDsodkt+pytSzEjRSHAymE9IMi4L auACsE4DQfriciPk/s4EkP5sMheR/tGgTvQEbmTeDzchPkRFoxvTWCaSakWPUfJ8C1Aq BIAKm5+fyA3V+9c7rtZR7C/Yor3GyRY3eldzJriX1S1sH9d64jn+2bEyZEb6X4GJurbL 1T5ilIWtF8hsC0JsRC+oHi3IPlzcKv102H33cQCU9tHooMnQ+WLPQiIHYLmXmgFM4B2V +ARA== 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=bNmJSk2Mf8wiupmA7otv2vwMpu3HCNA090aeI5fVLaE=; fh=QzUk4cZ6Uwc0ZODL+YdB/QCafLy0XaBsfa3jMegGZmQ=; b=TPHyU0Rp4xt66SMkSmnxqi7MDqNkw7GNWTPkkEIqPtFyBCQCSv8Vosdu3D7RWrGonl 1hzJErr3reLv5tdmt/mgHupCw1wYrbhfkc8VE7QjFGh0g6jqZJOVk9bneYB6H/s33bUE cSOJzx7gHotqpI8xcYxF+7/k/5MGzd9j/RsXju0YN8WW9AG1DCzrwGQSrWLONAlNoONz qGYVSALTGeDB2lSotyoHVi5dZuP2ZAhigavEV59WEt00mjbF4cFZl+5+C8OOd79DAFAb weFdQYv5OFkBs0EXz+RwJroOn/c9nykLqoxsneB7bDf4PSWIs0cE6sJEXmQaG0Tzx2C2 qVQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=j66J+ZPk; 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 Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q33-20020a17090a752400b0028672a7b3casi439074pjk.29.2023.12.06.21.06.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 21:06:21 -0800 (PST) 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=j66J+ZPk; 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 6E8D2808D282; Wed, 6 Dec 2023 21:05:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230428AbjLGFFJ (ORCPT + 99 others); Thu, 7 Dec 2023 00:05:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230338AbjLGFE5 (ORCPT ); Thu, 7 Dec 2023 00:04:57 -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 CE270D51 for ; Wed, 6 Dec 2023 21:05:02 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d3eafe3d17so1929387b3.1 for ; Wed, 06 Dec 2023 21:05:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925502; x=1702530302; 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=bNmJSk2Mf8wiupmA7otv2vwMpu3HCNA090aeI5fVLaE=; b=j66J+ZPkvukEjBSMTiJqbYvNks4a/6dOQ4AghyY/zuocABJaYCry4143ccTRt7IO12 cYV7ElMH/wyxsT8O+mscRYokLeg4Kelm1c9kkCufTmMQS2xSB+Y0uaQERrli8gjNFT2R ftAMltvudLWurWANfRAedFRT8HD2S6bAk+OebcjhQihsyFq+tM08ZT8za14+WtLh70CA OaWS6ZeQ3qRNuWnKY9MbEyDIbXlFf8PGQ+vbtHtJ/Zc4F0GZ7u+oAoCRPO5RVw0M6MAH +WefPPlJiERBxJHnEyZIo4M+9OOJK2wN5gnItQK6bCV74Sb4M/miQNoil+CRlfirz9cf Tpkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925502; x=1702530302; 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=bNmJSk2Mf8wiupmA7otv2vwMpu3HCNA090aeI5fVLaE=; b=v+xQ86awFBA0e6U5Ijn04wINuoW1yV4L3vVtDeubZFq414AaJktNqq+AqsQNDTAAPi 2B3UR6ZKUjfzQq8M9DNhp/L1prIKfi/8APymhsyQCfIuRIVeElFDT0zA9Lg+Vf8Gu7ga XYX0JXNT/O9EZiAD7hlsIsuGPycXz/ExG0U2Y7kmS4wlsITKWUBALCHjHpgAE+6gNPW9 I7qawYzC8oFm2xdH/dVUA4zEvCynF3qYS3ICRSYqx6Tx9nJ8ul+AfUFplAxNZqw5dj8/ tWVkOD22rfOBYvXWXe5IaE/3z11WWER7/qWS03FANTNR5QG1kft3EADBDdIofDM5j9EK XSYg== X-Gm-Message-State: AOJu0Yx7BMlK8MCQISo65UvxFgORKYSRF5cn3drj00IRbCDSQpt3z5in nDbm/yKI9MmjRC5l9vbmVM0Dhk9OeIIA X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:3144:0:b0:db5:4a00:a153 with SMTP id x65-20020a253144000000b00db54a00a153mr46968ybx.4.1701925501849; Wed, 06 Dec 2023 21:05:01 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:33 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-7-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 6/6] 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 , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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,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 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]); Wed, 06 Dec 2023 21:05:35 -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.rc2.451.g8631bc7472-goog