Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4302828rdb; Mon, 11 Dec 2023 15:25:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHMr+7krAxvqRMMpOI5y1JbbXHdZhSEWe/DCvh6p6w3ewE2rWCj4Jnl2RcNUnjtXUwccMlV X-Received: by 2002:a05:6359:6a6:b0:170:406d:42e7 with SMTP id ei38-20020a05635906a600b00170406d42e7mr5569180rwb.39.1702337150007; Mon, 11 Dec 2023 15:25:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702337149; cv=none; d=google.com; s=arc-20160816; b=GphQAWc+zpe/Z9yEtM5qhhnB2nvVUxFj7XudOp12YrUQcNrli0NPYU5UIcRKVigAXm HvL5C4v6PPgIMfgOQzE0AnaTBaU512lDrXUcAKx98kAfzEkgypv/99smeDpfuHDKsK+S jD9mRBDauU8i6ANXRdtadraTrW3TlfMtndoOaP1tJOcd9opKNqEwFe2WKFU1gx23hKur 4EWXjwSw7kTrkuCN9mb71PG8WHLk8tWo+gFi9uA+4Y2ZqjaAUFhQuE3cMuQpZn5agp4v pyubA9Y+Tj1GAThOQd/CLFcGcVopWhtKbLmWa71LzCW69v3YXdnsu76Oh2RyhDFLpgZ0 /giA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=TmtGCGfMkqvIInKkbNqfvKCt9MiM+j4M9PfxsEbS4KI=; fh=9QhaDQogLdawoxsWypKjVVByFXJdfqCA8m5VVhOPqGs=; b=nMTZw1PsfQ8yz/SbIZfJiYPX89ieMnHiLMSzHds8F1/Q+itM3Kq2fS7kP2pxvbMKkT hnxeP7ukKRWLy3KvCnlKqCwxcPMt3vtHedxV/eIX08leOZq6wa11WmmZXsnwA3jysaSS Co5f7m8h9rZAeuf4pFjPOPWoSqqzME2VOt945I+tVC0rIC8/eH5n7Ixyl5X+7UGXGL6Y r3GvHYoz9Qt3rGwNb6VTPJo0lMmNZf0jS6JHMQEVbQ9EGbPd+P7HKyjGy0oUlQ3OVoZq pJ7lb+2dZtHnqtG8cD17mP2qzSqloBipEFtXnVUOgL+zEvDHVS+bA9S1m0a5g3wbFj1b se7A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id c20-20020a631c54000000b005c677919049si6555669pgm.757.2023.12.11.15.25.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 15:25:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 2F3C480782A7; Mon, 11 Dec 2023 15:25:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345163AbjLKXZD convert rfc822-to-8bit (ORCPT + 99 others); Mon, 11 Dec 2023 18:25:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjLKXZB (ORCPT ); Mon, 11 Dec 2023 18:25:01 -0500 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BF3ECB; Mon, 11 Dec 2023 15:25:08 -0800 (PST) Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-28abb389323so46025a91.2; Mon, 11 Dec 2023 15:25:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702337107; x=1702941907; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9Ho6u7NxirxlqwmCKHqrmmg+mYP2l9JT9kgSjmqZWLw=; b=sd1UACZypw4oVt1q66MjSPPSfYyZPHjSP+bn4z4/d+sxZutu/PH9Xl7gzX90AeV4rt UGhqO4DJRIAsQ6G/zTF7x6z8Ne72rL0JeemEUvp8h95nkEL/mivvZ3HrnJACpIXpRXtT ej/zkC6OIiFyiQELnmP3SjvSDeFrBM1qZBPoO0DUftplrPgO0YROcR4LdBKRhoAlT1LS Fr08SKWqmamrsB2P7kG/W9CLPlcarSHxSFFq+3z3xUcGXuPjjx2uSdWRRt4ybqUQEy7O +BZCF3Xk56h82izluZ5xT/Rvg6pb++zv19WywvYQ89/RfXzUwxujkGX13ATn+Lf3iHa3 +8fA== X-Gm-Message-State: AOJu0Yzj5tVD2MpoMrffUdeO/ypZxVnggxSncYxzYYpK68BetK+lHy4g 4M1WGYUU6ZFiPXeJvIMk3/wSHMz8Kr1rqH8A/Fw= X-Received: by 2002:a17:90a:d994:b0:286:6cc1:5fd0 with SMTP id d20-20020a17090ad99400b002866cc15fd0mr2387175pjv.83.1702337107352; Mon, 11 Dec 2023 15:25:07 -0800 (PST) MIME-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> <20231207050433.1426834-3-irogers@google.com> In-Reply-To: <20231207050433.1426834-3-irogers@google.com> From: Namhyung Kim Date: Mon, 11 Dec 2023 15:24:56 -0800 Message-ID: Subject: Re: [PATCH v1 2/6] tools lib api: Add io_dir an allocation free readdir alternative To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Mon, 11 Dec 2023 15:25:20 -0800 (PST) On Wed, Dec 6, 2023 at 9:04 PM Ian Rogers wrote: > > 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. > > As getdents64 may be missing from libc, add support using syscall. Unfortunately my alpine build has: In file included from util/machine.c:2: /build/libapi/include/api/io_dir.h:17:23: error: conflicting types for 'getdents'; have 'ssize_t(int, void *, size_t)' {aka 'long int(int, void *, long unsigned int)'} 17 | static inline ssize_t getdents64(int fd, void *dirp, size_t count) | ^~~~~~~~~~ /usr/include/dirent.h:52:5: note: previous declaration of 'getdents' with type 'int(int, struct dirent *, size_t)' {aka 'int(int, struct dirent *, long unsigned int)'} 52 | int getdents(int, struct dirent *, size_t); | ^~~~~~~~ Thanks, Namhyung > > Signed-off-by: Ian Rogers > --- > tools/lib/api/Makefile | 2 +- > tools/lib/api/io_dir.h | 84 ++++++++++++++++++++++++++++++++++++++ > tools/perf/Makefile.config | 4 ++ > 3 files changed, 89 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..9b702497e05c > --- /dev/null > +++ b/tools/lib/api/io_dir.h > @@ -0,0 +1,84 @@ > +/* 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 > + > +#ifndef HAVE_GETDENTS64 > +#include > + > +static inline ssize_t getdents64(int fd, void *dirp, size_t count) > +{ > + return syscall(SYS_getdents64, fd, dirp, count); > +} > +#endif > + > +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 > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config > index aa55850fbc21..1cef1ab4ddb7 100644 > --- a/tools/perf/Makefile.config > +++ b/tools/perf/Makefile.config > @@ -357,6 +357,10 @@ ifeq ($(feature-stackprotector-all), 1) > CORE_CFLAGS += -fstack-protector-all > endif > > +ifeq ($(feature-getdents64), 1) > + CFLAGS += -DHAVE_GETDENTS64 > +endif > + > ifeq ($(DEBUG),0) > ifeq ($(feature-fortify-source), 1) > CORE_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 > -- > 2.43.0.rc2.451.g8631bc7472-goog >