Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1436169rdb; Wed, 6 Dec 2023 21:05:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IHP3oWaU2ew1PQuoolLoa5mtjTDQ35APpVOCSTPY2pVsHPXgn+c9FT7MjRkQHAGasFunSWp X-Received: by 2002:a17:90b:17c7:b0:285:b239:f4ed with SMTP id me7-20020a17090b17c700b00285b239f4edmr1445747pjb.34.1701925508266; Wed, 06 Dec 2023 21:05:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701925508; cv=none; d=google.com; s=arc-20160816; b=UZ1xWuRjHM3tNx2v+eVj61rv6Yj++6ftLog+IHANV1AFNPtj9jOrgWZ7rxJK4FIfH2 1x8ewEkCX/vOQW6zQvGch7HRlDTz7NuxaaGMuV+DUgNmYHYo8p736b8yGC8K0zhfTQau O7Y9j+mMwIRzME3ZNRjJ8eu91EW5pBLR2KKuUN+CEiomRn42WUDWSGwjrwxDRIY/hYOb s3XD++c2i23x+JSXq84M5yhcNo3ikRXlPi+F9BHuirLZf9IZdb3UmEMhz5VhuxOmC6eq 9PDS66xasZ21XL+WpPij0CV/nf05rJntvCYAWKU0dT+dv5DkSa5tFjHVlOpkr9F2VoK5 1DMw== 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=HLfF4gdeUWNr8FF8ACObQvFWnydI6Js6QIkQSQodKEI=; fh=QzUk4cZ6Uwc0ZODL+YdB/QCafLy0XaBsfa3jMegGZmQ=; b=skpGqFQttOhgsTOW07Bz4kqaqxEd3VHi6u7m1DmuzYh3At7hn+fYwCHnL2koMNV5Ak EpQd0rRC2m2/Pb0SZrKAemo5ovoWi4c2ehBziChotcCF1wuU4qHRReJU7+zExuD8LEFp VTt8dNzpC8EYHEAxAlolk5kSbkLzd4eDVtJIrkyqNBKc5A79EBjFDj6S6uEVWOK/JOzv x1+doJh136RFrAd9g3R+fuZHEDA2cpynVOOP41WJ8y1EWaYOGG32q8PZfN6o3qMwTln1 jI3JGapd6JGGCva0k3wWe3NyMeVkqC23aSuHRjn9eEl7qXT6mWwk9du/jFFOnIcueZcv obQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="16JKr/OA"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id cp6-20020a17090afb8600b00286adfd9206si431156pjb.103.2023.12.06.21.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 21:05:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="16JKr/OA"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 588B280DA9AE; Wed, 6 Dec 2023 21:05:05 -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 S230291AbjLGFEu (ORCPT + 99 others); Thu, 7 Dec 2023 00:04:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230264AbjLGFEr (ORCPT ); Thu, 7 Dec 2023 00:04:47 -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 96DEDD44 for ; Wed, 6 Dec 2023 21:04:53 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d3a1e5f8d6so3973157b3.3 for ; Wed, 06 Dec 2023 21:04:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925493; x=1702530293; 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=HLfF4gdeUWNr8FF8ACObQvFWnydI6Js6QIkQSQodKEI=; b=16JKr/OAlmIySJ2uZLXZC4p70yZiecAN8EhnkomFKLRuI8/eJ+HXfBmc9iI1Cq67rv 1T+ncKFZBzub0uuI67C/Rrf8+HL+kC1+1EqjdnqUxmJ3pi9L9/Rk2RPxBbsbizJ52Fkg 11JhkT1Z12L1LsPQQielsZdIbFLpz+B5rV8wxCJ7BEomwakbQlHHrx+ZFVgY/akc2lNG dafpmPc5KgqTa9V9dx9FVoqVmKL+WvcmeyWJhwIfhmjUc07tO+YB+PbDQrLwND2A+HNR oYA0PxhpM3VYtNN19I+CY83SNFgJ6ep0KgUE3vrs2pFRS3r2YQyxym5EvW8sJNcj1I6J WxCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925493; x=1702530293; 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=HLfF4gdeUWNr8FF8ACObQvFWnydI6Js6QIkQSQodKEI=; b=iEhRYRed2mT5NVF2wk98XY4NYHbb1aEdx088aizpD8EvgIMRDJgkAJlhF2xfLB/Uvf hXYUaqT2xGEJBLKzb41prVzORAFu9fNMTNnWEl7rTOLKiJjQHaUpzXKbeQYcW36U4cVE 0KFiRQidsVRJqiGgG2nDpjr4joX+gNr3im7Jet09Rk3o//xNAKbDHz8zYeMTSuk3V2h8 OqQ/LJPwTteFXKpUIYeWrjVeo/d0nTrJTkcvckycbFMRbexySW+JGndSwF1EqmegLWbw DNIVFHDnFjR6NAAEomClZ3FzZ5SLAjgDBb/UXNJFozjREwSgb6Jj/zvBuJp4gIAPCy8n HMDQ== X-Gm-Message-State: AOJu0YwNce6hrN376jGGUQUkvO/EjI7Jq+IWqbDn+BbtSKX+D1yyYcvj Yh/dlHSM7oB0x6diEVLjH5igrTGRPypa X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:ad14:0:b0:5d5:6a4c:b0b3 with SMTP id l20-20020a81ad14000000b005d56a4cb0b3mr26965ywh.8.1701925492804; Wed, 06 Dec 2023 21:04:52 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:29 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-3-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 2/6] 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 , "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:05 -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. As getdents64 may be missing from libc, add support using syscall. 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