Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2185575rda; Tue, 24 Oct 2023 15:26:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFQVtz54Qls4I6bDccl4W/+jMYCT6j8hsh744U0rJCJe73pUtoxoxO7cM6qhFg9ziNb4HiZ X-Received: by 2002:a05:6a20:daa3:b0:14e:b4d5:782d with SMTP id iy35-20020a056a20daa300b0014eb4d5782dmr4908790pzb.2.1698186377734; Tue, 24 Oct 2023 15:26:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186377; cv=none; d=google.com; s=arc-20160816; b=oVj/zTQ/pY2MUF8ggDskhv/cY4srsj9OyI9TBcT7qrHzK4xgO+ix7zRibfykFaYWRm QftuVNvHc+7VXW4JaXE/OuFCMglGNI4GlD3O+hJLWfx+ltYkgEqNgRI6vNJ37w/v8wFF 7bdkFdGxML2P0tKn0i7SbDyaJTYYjV0XF9dbgz9zZpwn4LCF1jJS299RXxXE7TuvrbHM akG8VsrdqGy3ZH6OEiJfMMEsX2cRD56tmdbS7YDbKoxGM5qOu2guBUxzdvcLQUSlefhR 03N8XKCIpdKsKResSWbSj4LCX0ZzqjbmrcbfJAhUTehTHgUDhxLO2i65Tjm4SPpzUCKJ A7aA== 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=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=y47wBZgNJ/W0LVs6mrfqt3CyuKzt7vqoUfrqvfEiajsR755yYif4vv5yKnWPDNyAjw uLxy3x4HSXmXTGh6x1IojIQGJQMPRKvdvrVh1NvCjU/08aSY3z87i8ex26VnpQne+or2 gmQrroOvXjpQhkr2h+CjVzQog9F2ARHzWIDWj2lNDBTg9J2M4GjaolsrpBUnZz8PPOPR /c5fvbZsVw1M2zfcakBxRfs7/kmX7vnJ/NBGTWz3+V1x5w6O52kLi9MmbbaPElOAF1if f/DSlKFzKebGMxSNR3Dauqafkx+UUzSRhvs9CrNeTSFZjavGyONOk15HcLYS+ayqNptJ 8ekA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EU+aw5wB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id ds18-20020a17090b08d200b002613720fcd5si11706087pjb.35.2023.10.24.15.26.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EU+aw5wB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 4916A80CF536; Tue, 24 Oct 2023 15:26:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344501AbjJXWZx (ORCPT + 99 others); Tue, 24 Oct 2023 18:25:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234983AbjJXWZ3 (ORCPT ); Tue, 24 Oct 2023 18:25:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DADE610D4 for ; Tue, 24 Oct 2023 15:24:54 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da03c5ae220so243979276.1 for ; Tue, 24 Oct 2023 15:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186293; x=1698791093; 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=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; b=EU+aw5wBe8FZvBL8crMRweg+EtN2PXjPDp24t4tZS6NGh7DtAdvSifsXhDpa7oPG2k KkOOlBOnHKnfaheuCAgsfj9gHTQ4vEBNywX9PstW6/fHnbM+k/a0K/Rmj/TtX16vgSHW PfNKFSB8NLH+JrFWJ0liIgra8hQLQtpmh5gug/6yNu2u3YHiT/A/UVZ4NJFSHHxEqkAV v4++5qYr61lq72+p72jh6f2VMbCdX3hxx4EKP23L3aFlSsmIFsUXqO+MitPidgKrY7YL x2pwSUk7v0woASAcnX9mwYdqqd7PcBY4Ju40gJRGohUmUgsyKaDNOAUDoEVgdNu/xR8S HI9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186293; x=1698791093; 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=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; b=g2nDj8JgQyxkajFyrXcQD8ySDLlHLOSmZkEP0aCla0SAS7+bTEtwN0JGXxn5aDLQU9 M27Mi6JtXp5iok9akFue4zYlAyGMB2wLG6gdGQxdSFLmwehDhFoIbt8nNd9PdF7fkf75 3n2S08PUPZbOU0bk+oLJvOiItxjiG5sc+OH7iCcZ37oJDldG1AXHajqCxPoxIM0GZEfQ 1BxUf8vJpgR3JR/7zRjNT0tt2Ishx87gpyZ3VDbauMbXJ3TJIDBgCommq4uLdGPwc3rd pofhs2Ujy3/Z8DFN3rvYqVs1oo78jLprKe3HweyPlTpzNsCx0F8w2/fQLpukj0tT4e+/ QXng== X-Gm-Message-State: AOJu0YyAzMX0CunaliaZg0+39WrLpSQ6wtQJKbySeSjIHbSkDEljmtJN K76NtuiC90eQqP/bOYzbmQoEGMVpOm+r X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:34d1:0:b0:d9a:6360:485b with SMTP id b200-20020a2534d1000000b00d9a6360485bmr385698yba.2.1698186293540; Tue, 24 Oct 2023 15:24:53 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:21 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-19-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 18/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 , 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:15 -0700 (PDT) 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 | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 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..8a70c0718df5 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,72 @@ +/* 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, const 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; + + return S_ISDIR(st.st_mode); + } + return dent->d_type == DT_DIR; +} + +#endif -- 2.42.0.758.gaed0368e0e-goog