Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp492715rdb; Thu, 30 Nov 2023 09:57:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IEhYpqATEUgO0x+jclFqonERnSbw0DaF9Qp7XT2Ws8+Etd/Ntx6ycrRJ+61ajm173sEdEBB X-Received: by 2002:a05:6a20:7493:b0:189:bde9:71aa with SMTP id p19-20020a056a20749300b00189bde971aamr26933890pzd.48.1701367033198; Thu, 30 Nov 2023 09:57:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701367033; cv=none; d=google.com; s=arc-20160816; b=XMBpdkm1U1n2JieaToLaC5+pnzkyorTOurTfYbXVlb/X23NzJegEucjKMVdXbGtYMi ZoXz7lVX7Nx9hhqyxwvTEZZjXyjzd1ADxYrvBxjrBGsY6CbqvhQrh09PPES8u3dBpGW1 8/YMoipyrlCXeK5vtbObqY9btP3zelMV2yV5VRChdnd3AdU8/diM003OWTRfE7KAwMEK GiGf6Qe+ybXeprbdIrdVGsLbMyx0Sv8yF/FvOxJNibW+KfknS1t1KUsbJQtDdzRW2PVb DDCDtPap41vLigGI03VGwao71ZMOVowh9YilKjVqDLSvnN+IyEzodY+hZBdVO0TJuOyp qvGQ== 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 :dkim-signature; bh=XSbGF5t6T+9UlC48SI7S6PeC5mxJiIPR6wB9iBHA1fQ=; fh=7OGn+erAB0UB1A1Dqg2PTzyT0B4ue+K9G2U4PxVLTqo=; b=OwfQDyTJa43Lnk+yfmvM8C1fXl0tg63t2TCsCxGI+sEF6nXCx3K7kqzJ7GYXwPyI0M UznqWiloMQvQx6VO+JfkY5GXvwejzqJq0yyfNem5vFiAcPZEREH6YA6KRSFFMsSO/ndc 5OgeVM9Q3amIAGhe8uYrorx/Gi1IEJGpeVxDR5naiMlaeetbD0G7ZWzlLo2Tri1JWnY+ PgthlKGan6IYYFyYsNskWFR0jHePZVN5/dh9e01OOnrjVNhnydn2ro7LkOh6/LKI1wOz nheI59ItKtsucta4z/kcGYD/j2d2T1ESGVJBriDBWjPpkpRSpLTCPr+xFk0apG5ynXkt al8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wmlleAvw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id p23-20020a056a000a1700b006cbe5e18d29si1758040pfh.36.2023.11.30.09.57.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 09:57:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wmlleAvw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id A137680CC129; Thu, 30 Nov 2023 09:57:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231486AbjK3R4x (ORCPT + 99 others); Thu, 30 Nov 2023 12:56:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232085AbjK3R4v (ORCPT ); Thu, 30 Nov 2023 12:56:51 -0500 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B91110F1 for ; Thu, 30 Nov 2023 09:56:56 -0800 (PST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-50bc4eeeadaso50e87.1 for ; Thu, 30 Nov 2023 09:56:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701367014; x=1701971814; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XSbGF5t6T+9UlC48SI7S6PeC5mxJiIPR6wB9iBHA1fQ=; b=wmlleAvw2aLYGVOMkvpQBOqUhtgYLqtTvJPGpDqpglBHXuAEkA7iCd7RwtczQyEXc6 5+fSR+uJft1CMDWqBlUNV+LZmvHUqJ9uMEJxi+P3Hls42BtVItRqowbNQsNqsaGMzX8P EBj+kKQdSW7wX96+N7FSlG0Efx+ESf2sc799YSeQlf0nuVqMLYcMD+c+C91g6nc6ADk0 Wm3V6JxawawidbnMkJpFMtVCaaK3PmdWSIUF1kymKkvKW0qNE4gJ58VcpFlaUftXbu88 vQaWAowcTDvkerNAbKqmHdQLuFNCd7ugAEs+P3+igeYJKmhqhfG3wBTydy184mkihwQf pm1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701367014; x=1701971814; 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=XSbGF5t6T+9UlC48SI7S6PeC5mxJiIPR6wB9iBHA1fQ=; b=utxtySX6eLCKFrZvwPFHuGe4HtMXzmLYcbKmJheOht8SuN9DGG/bCtBabkqGH6kbfd W5KvgtpluIb3Ub9kjdQFM3oaXLWLmlzOOxKuZQsDnWaFkN5CeD3WS4chcTtQCNlEfUWS NwBICyzAqt4Ui+0BwPrmmdgBm4KED4ziaw1YNsfDAPPe2/J0rg/GTMVM/Rm94coAGnbr +EtoiD6OcWnnfIt450vukg/ruZ1DtLrzpSFYh9o98X5A9DYNKaD9YTURqdKVdarIZ1+/ ZAJEaDWJTZ7dlNN0eTAdkCKa2E5vqnE1rbuY0ACygpE6nXJM/J/lJgHCBSM0GG3N3OPj GVwQ== X-Gm-Message-State: AOJu0YzYahSaFcWYER9YjI8lxjH4iQqUHmLTAue72c/PZ1u3VPPpqBuD 63bJFjqsROEoy+yy79GTmAlzQ1tsPkzfkMbGHGervw== X-Received: by 2002:a19:5f57:0:b0:505:7c88:9e45 with SMTP id a23-20020a195f57000000b005057c889e45mr127635lfj.0.1701367013906; Thu, 30 Nov 2023 09:56:53 -0800 (PST) MIME-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> <20231127220902.1315692-7-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Thu, 30 Nov 2023 09:56:42 -0800 Message-ID: Subject: Re: [PATCH v5 06/50] tools lib api: Add io_dir an allocation free readdir alternative To: Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , 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" Content-Transfer-Encoding: quoted-printable 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Thu, 30 Nov 2023 09:57:10 -0800 (PST) On Thu, Nov 30, 2023 at 9:21=E2=80=AFAM Arnaldo Carvalho de Melo wrote: > > Em Mon, Nov 27, 2023 at 02:08:18PM -0800, Ian Rogers escreveu: > > 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. > > Now, on some systems: > > CC /tmp/build/perf/pmu-events/pmu-events.o > In file included from util/machine.c:39: > /tmp/build/perf/libapi/include/api/io_dir.h: In function =E2=80=98io_dir_= _readdir=E2=80=99: > /tmp/build/perf/libapi/include/api/io_dir.h:46:16: error: implicit declar= ation of function =E2=80=98getdents64=E2=80=99; did you mean =E2=80=98geten= tropy=E2=80=99? [-Werror=3Dimplicit-function-declaration] > ssize_t rc =3D getdents64(iod->dirfd, iod->buff, sizeof(iod->buff)); > ^~~~~~~~~~ > getentropy > CC /tmp/build/perf/tests/workloads/brstack.o > CC /tmp/build/perf/tests/workloads/datasym.o > CC /tmp/build/perf/util/maps.o Sorry for that. Is it a _GNU_SOURCE issue? I thought we generally had _GNU_SOURCE defined on the command line for reallocarray. Maybe we shouldn't define this on the command line and do it immediately before the relevant include with: #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif like in tools/perf/util/intel-pt-decoder/intel-pt-decoder.c Thanks, Ian > > > 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 :=3D cpu.h debug.h io.h > > +HDRS :=3D cpu.h debug.h io.h io_dir.h > > FD_HDRS :=3D fd/array.h > > FS_HDRS :=3D fs/fs.h fs/tracing_path.h > > INSTALL_HDRS_PFX :=3D $(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 =3D dirfd; > > + iod->available_bytes =3D 0; > > +} > > + > > +static inline void io_dir__rewinddir(struct io_dir *iod) > > +{ > > + lseek(iod->dirfd, 0, SEEK_SET); > > + iod->available_bytes =3D 0; > > +} > > + > > +static inline struct io_dirent64 *io_dir__readdir(struct io_dir *iod) > > +{ > > + struct io_dirent64 *entry; > > + > > + if (iod->available_bytes <=3D 0) { > > + ssize_t rc =3D getdents64(iod->dirfd, iod->buff, sizeof(i= od->buff)); > > + > > + if (rc <=3D 0) > > + return NULL; > > + iod->available_bytes =3D rc; > > + iod->next =3D iod->buff; > > + } > > + entry =3D iod->next; > > + iod->next =3D (struct io_dirent64 *)((char *)entry + entry->d_rec= len); > > + iod->available_bytes -=3D 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 =3D=3D DT_UNKNOWN) { > > + struct stat st; > > + > > + if (fstatat(iod->dirfd, dent->d_name, &st, /*flags=3D*/0)= ) > > + return false; > > + > > + if (S_ISDIR(st.st_mode)) { > > + dent->d_type =3D DT_DIR; > > + return true; > > + } > > + } > > + return dent->d_type =3D=3D DT_DIR; > > +} > > + > > +#endif > > -- > > 2.43.0.rc1.413.gea7ed67945-goog > > > > -- > > - Arnaldo