Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1891864rdf; Sun, 5 Nov 2023 19:53:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHYvfnlCN8j2uyPRTeICCYqn3HRj6oTwjpTTf+8L2an3WpOfLeuZEs94Ek0bXLVLcaymj// X-Received: by 2002:a05:6a20:1595:b0:17b:3822:e5ea with SMTP id h21-20020a056a20159500b0017b3822e5eamr28167886pzj.19.1699242810989; Sun, 05 Nov 2023 19:53:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699242810; cv=none; d=google.com; s=arc-20160816; b=ZwmGtZ8sGermtyu/WS2m3n4jR4unPhTwsHLu0a+ZNtqDUaE2+m0WTV6Z3nwzzFBwMa 8YrnrqEHol3upzuKXJWU7vsU74vCIdjkKybtymGu28TvxavuLugp/8CS65D4yFcdn7Nn pClED9ZyV+1bjOrA9QRcrDel7nEvntU6pqq8W1YtQ8K3FBGl6hUsqSI6gSxAXG8SzDIg eEGhaqDoQR+7XXAk2+/PpBfYjRymFPHp64S45uKvt7lIXUAtS8S4LHe3fbipzVUBglg2 5A3++smDhftVb9Favecd5AOX9Q3DoHvnlKvRgHyKcr364uwaF4+QZaeJSxKzBJ9ubsm4 XYTA== 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=Xcd5r52d987s3eB5Kyam4tVdZsrxWQgZW5/kJNyMulw=; fh=mapERlogJ/cQt0xtLiOgnrZ5zpp0wMkpfbiUTcLZi4s=; b=Que7zP22gpmJXhzYTDvGaTGIPFeZW6NmljnJ46CkEzfBYO15U6KXlC2j8kl12lIhke zHBIF5gJcuqQMil4CiHCH5hmlyuVwZh5PNLYIn3ZwrCZXeFbSkJPi0e7U3CyUYQxNc3P wWQBxd5uxm5rdf++GD1Cp06CsL3ie2YqM+3MqDxUG3KO8Hxpl2E/1xGvV6lUOYBlcCEh 4CyUYjCNO43OGExSR2djH/EEJWNacYiSU3Ac90Rm20pmNAkkQTW6VrfOD9RxXN2O94MD 08SnG00UJa+VUPY2RB0MKksq9ATKjcIkSHLRqR0z/mwsCBhaFXCtzP0+9BgA+Gc12xwK naYQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id mq14-20020a17090b380e00b0027d74e8a50bsi7758818pjb.69.2023.11.05.19.53.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 19:53:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id CA9A880AA250; Sun, 5 Nov 2023 19:53:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230151AbjKFDxP convert rfc822-to-8bit (ORCPT + 99 others); Sun, 5 Nov 2023 22:53:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjKFDxO (ORCPT ); Sun, 5 Nov 2023 22:53:14 -0500 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27293B8; Sun, 5 Nov 2023 19:53:12 -0800 (PST) Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6b5af4662b7so3228675b3a.3; Sun, 05 Nov 2023 19:53:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699242791; x=1699847591; 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=V9fuperXbeRFYlZWP5/ntLp8L3b3+pqukS7QhZ37B4g=; b=gKMVxxTad4M2cJgx46voGyaaaAJ2PLdgkl1H3dmnquvQqLImuYmNNWItpqgzs6CFwS P8dln463ICN/OfwB6QkhHUnymrZPQYiWFgtPTYyEqyV+H2DHB270SEyiYA9HcwIq5A48 1ofScY/5UFqwkfvVVCHfY+2hu89pdcnEZPmdFYQvRFHZOjhxH9vzBq5KSOP4Zx8FCxgG m9opSPXAtiSYtFBFPDEyciyIc9gRTPbolb+q+EM0yTMq7DXhOXnG4X/FDCOvVFw4EtpG Eel4s2PVhvw/sNE3iOUwcM3qdU3yCeBV8Ejd0gjV7rl407QX/EnU59HJCt/lIkDxwa/k Adgg== X-Gm-Message-State: AOJu0YyG++K4+2gY1atcFLBdlWquVZGQMYtbYQUp8QfdBI6P1PZGOlVW XRnshl5rpIMu2bIBPdWWinu2n06OCF1plX2K7X4= X-Received: by 2002:a05:6a20:54a3:b0:13d:17cc:5bca with SMTP id i35-20020a056a2054a300b0013d17cc5bcamr26548829pzk.18.1699242791516; Sun, 05 Nov 2023 19:53:11 -0800 (PST) MIME-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> <20231102175735.2272696-7-irogers@google.com> In-Reply-To: <20231102175735.2272696-7-irogers@google.com> From: Namhyung Kim Date: Sun, 5 Nov 2023 19:53:00 -0800 Message-ID: Subject: Re: [PATCH v4 06/53] tools api fs: Switch filename__read_str to use io.h To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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 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 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]); Sun, 05 Nov 2023 19:53:28 -0800 (PST) On Thu, Nov 2, 2023 at 10:58 AM Ian Rogers wrote: > > filename__read_str has its own string reading code that allocates > memory before reading into it. The memory allocated is sized at BUFSIZ > that is 8kb. Most strings are short and so most of this 8kb is > wasted. > > Refactor io__getline so that the newline character can be configurable > and ignored in the case of filename__read_str. > > Code like build_caches_for_cpu in perf's header.c will read many > strings and hold them in a data structure, in this case multiple > strings per cache level per CPU. Using io.h's io__getline avoids the > wasted memory as strings are temporarily read into a buffer on the > stack before being copied to a buffer that grows 128 bytes at a time > and is never sized larger than the string. > > For a 16 hyperthread system the memory consumption of "perf record > true" is reduced by 180kb, primarily through saving memory when > reading the cache information. > > Signed-off-by: Ian Rogers > --- [SNIP] > diff --git a/tools/lib/api/io.h b/tools/lib/api/io.h > index a77b74c5fb65..50d33e14fb56 100644 > --- a/tools/lib/api/io.h > +++ b/tools/lib/api/io.h > @@ -141,7 +141,7 @@ static inline int io__get_dec(struct io *io, __u64 *dec) > } > > /* Read up to and including the first newline following the pattern of getline. */ You may want to update the comment as well. > -static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_len_out) > +static inline ssize_t io__getline_nl(struct io *io, char **line_out, size_t *line_len_out, int nl) How about io__getdelim() similar to POSIX? Thanks, Namhyung > { > char buf[128]; > int buf_pos = 0; > @@ -151,7 +151,7 @@ static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_l > > /* TODO: reuse previously allocated memory. */ > free(*line_out); > - while (ch != '\n') { > + while (ch != nl) { > ch = io__get_char(io); > > if (ch < 0) > @@ -184,4 +184,9 @@ static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_l > return -ENOMEM; > } > > +static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_len_out) > +{ > + return io__getline_nl(io, line_out, line_len_out, /*nl=*/'\n'); > +} > + > #endif /* __API_IO__ */ > -- > 2.42.0.869.gea05f2083d-goog >