Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3427335rdh; Mon, 27 Nov 2023 14:10:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IHcmvAiynmnQzfkNMhTvuk7kKi15grsiymx+Xco/bMD1teVarOW0qNtU1maEX6ZKcdnqU1E X-Received: by 2002:a05:6a20:a122:b0:18c:95f1:20bf with SMTP id q34-20020a056a20a12200b0018c95f120bfmr5320965pzk.47.1701123013655; Mon, 27 Nov 2023 14:10:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123013; cv=none; d=google.com; s=arc-20160816; b=Uk0FI/kCq1pyl3BmP5zgIn/JJRM2sr/9GQjR4h2lmx3Ew3obdc1UWq7lt9lv7PQ7Rd DOaLjI19XFdSc17VS9U0AqMgttBSdFOLvoHoX8prPB0rBFgmwBwyUKqtigU+LUtenT3+ 9a5O7NX7WrednW9wSfXUDdpbZxp2p5laxoWlPWbdnW13oJ8mHDDz9++XwIxveK61bZnS xXLQQlMrBrQic0r0B5zP+zhz8jflRa3Dk31BROxEEajLqRzyXLYy2mKcv22d+TzUTOQ8 PvtVrvo/3wwOHvWeZwI2VXAfV3Kx/ZVLJPf5yFmEGz9mIf9Ip1GzS58X+xYxhvST4Mcq mOdg== 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=MEmgO09+w7D/FXE2RqMfg/xWGErZ4jMQFLdNI3fBx44=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=BA0tgE8lYqsI+AI0VSgjXsGBCuG580R/OekHD3N9B9qhO4bH4kzR0daE02g6BlAdAc wmHmyHsEoxv1tNJ4m6ER6Wy6T4HBkBAleX+y5j0onuUPma/zNGmfq3/Eak3xPcKrVZiD jVVNAYr30IGBu3JBplDeG8vfkkWBAKXCfbhhgQRcQ5gWuK0ZRHmyb4CeXOG3/bzdmwxb gkMCXJLJ1jBBSw0+KLqALKjIQPorVjtfMcZ+Iqr1dcWMpuaLaJf/oRNhgZppl5/TDbpa ygUaKJ7tPpGW9iKb9hcMXZVMqLrkwtznZmXajhbDFhwICKATu8TFPx40JK30VEi+6tLU 7Pgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YheQzlfs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id u22-20020a63d356000000b005b95fbb1750si10671789pgi.113.2023.11.27.14.10.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:10:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YheQzlfs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id B078282B9288; Mon, 27 Nov 2023 14:10:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233725AbjK0WJj (ORCPT + 99 others); Mon, 27 Nov 2023 17:09:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233605AbjK0WJ2 (ORCPT ); Mon, 27 Nov 2023 17:09:28 -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 3991BD63 for ; Mon, 27 Nov 2023 14:09:35 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5cd573c2cccso29210047b3.1 for ; Mon, 27 Nov 2023 14:09:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122974; x=1701727774; 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=MEmgO09+w7D/FXE2RqMfg/xWGErZ4jMQFLdNI3fBx44=; b=YheQzlfsb9155A0nVfE/EJ9mhvmONOIFZ+JLlSedA7E2YsQBZM6fiprcBmWAmQUEs4 KXqJhg0eNCW7DORy9Y+W2kJV8E9AsKTXfP1nOHUubRdIyX4WDUimtYL/RtuIIMLfUcdA 5yPAYK/QfVeQ5PsF5oF8P9qmIZrGV/j3pSEL99UT/i5UPjaus+q+AYQ2wDcu7F/f3eEA cWhK5rsMhlFoIi9clg1MuKQHFCe0TGvZm4QbkVHBHuZeJ6Dy8K2J3I/BQV4AWB3xEx9M EG8OizUGXZZNqARwJBn+YrZz0+tG3Wz95PO2h2mMDGyV+dTHEUKhUrpN9j2LPDigdouo ePdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122974; x=1701727774; 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=MEmgO09+w7D/FXE2RqMfg/xWGErZ4jMQFLdNI3fBx44=; b=liO6IQV3/YWHY1Cf6OU4WNbNLCcpF5CD5nSqHyuDKkmknFOvI7zE547IS3F1hFi1jR O2/1TiZTQ7Fs9NuBsrhVGz3CWA90F6UmF07wssb78ghNY86kAv6hwH1ArpoW1L0MIK2y PoN7G1KkTm7E/G27Yr6rwE/vbFSRtkJBqkNeaifpkeyrg4nkenfOftTC2/4ZZY1PPq49 9ew4NbrmssWQIHwkaE1ff/xzCMPuboc/KXDfNWvsrp434KV7wDlkooZaYriSm/bElYRK sJKr6ll18oC453/7wnZXIKKlv1LGqukEJ87PI8A3RdzJFI7RDe0hg/jB3LmI1AeFXaVe tsDg== X-Gm-Message-State: AOJu0YzdHOseJx3jtRErqbCYkzk25occldH8fuaDHcRspNzr0Q9dkerA 3hjqAjEzqV+RAPAqoUoJLZrvy18Oi0aY X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:690c:2b0a:b0:5d0:8fb3:559c with SMTP id em10-20020a05690c2b0a00b005d08fb3559cmr120925ywb.0.1701122974473; Mon, 27 Nov 2023 14:09:34 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:18 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-7-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 06/50] tools lib api: Add io_dir an allocation free readdir alternative 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 howler.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 (howler.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:10:07 -0800 (PST) glibc's opendir allocates a minimum of 32kb, when called recursively for a directory tree the memory consumption can add up - nearly 300kb during perf start-up when processing modules. Add a stack allocated variant of readdir sized a little more than 1kb. Signed-off-by: Ian Rogers --- tools/lib/api/Makefile | 2 +- tools/lib/api/io_dir.h | 75 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tools/lib/api/io_dir.h diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index 044860ac1ed1..186aa407de8c 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile @@ -99,7 +99,7 @@ install_lib: $(LIBFILE) $(call do_install_mkdir,$(libdir_SQ)); \ cp -fpR $(LIBFILE) $(DESTDIR)$(libdir_SQ) -HDRS := cpu.h debug.h io.h +HDRS := cpu.h debug.h io.h io_dir.h FD_HDRS := fd/array.h FS_HDRS := fs/fs.h fs/tracing_path.h INSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/api diff --git a/tools/lib/api/io_dir.h b/tools/lib/api/io_dir.h new file mode 100644 index 000000000000..f3479006edb6 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +/* + * Lightweight directory reading library. + */ +#ifndef __API_IO_DIR__ +#define __API_IO_DIR__ + +#include +#include +#include +#include +#include + +struct io_dirent64 { + ino64_t d_ino; /* 64-bit inode number */ + off64_t d_off; /* 64-bit offset to next structure */ + unsigned short d_reclen; /* Size of this dirent */ + unsigned char d_type; /* File type */ + char d_name[NAME_MAX + 1]; /* Filename (null-terminated) */ +}; + +struct io_dir { + int dirfd; + ssize_t available_bytes; + struct io_dirent64 *next; + struct io_dirent64 buff[4]; +}; + +static inline void io_dir__init(struct io_dir *iod, int dirfd) +{ + iod->dirfd = dirfd; + iod->available_bytes = 0; +} + +static inline void io_dir__rewinddir(struct io_dir *iod) +{ + lseek(iod->dirfd, 0, SEEK_SET); + iod->available_bytes = 0; +} + +static inline struct io_dirent64 *io_dir__readdir(struct io_dir *iod) +{ + struct io_dirent64 *entry; + + if (iod->available_bytes <= 0) { + ssize_t rc = getdents64(iod->dirfd, iod->buff, sizeof(iod->buff)); + + if (rc <= 0) + return NULL; + iod->available_bytes = rc; + iod->next = iod->buff; + } + entry = iod->next; + iod->next = (struct io_dirent64 *)((char *)entry + entry->d_reclen); + iod->available_bytes -= entry->d_reclen; + return entry; +} + +static inline bool io_dir__is_dir(const struct io_dir *iod, struct io_dirent64 *dent) +{ + if (dent->d_type == DT_UNKNOWN) { + struct stat st; + + if (fstatat(iod->dirfd, dent->d_name, &st, /*flags=*/0)) + return false; + + if (S_ISDIR(st.st_mode)) { + dent->d_type = DT_DIR; + return true; + } + } + return dent->d_type == DT_DIR; +} + +#endif -- 2.43.0.rc1.413.gea7ed67945-goog