[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230110222003.1591436-5-irogers@google.com>
Date: Tue, 10 Jan 2023 14:20:00 -0800
From: Ian Rogers <irogers@...gle.com>
To: Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>,
Namhyung Kim <namhyung@...nel.org>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Tom Rix <trix@...hat.com>, Nicolas Schier <nicolas@...sle.eu>,
Masahiro Yamada <masahiroy@...nel.org>,
Athira Rajeev <atrajeev@...ux.vnet.ibm.com>,
Christy Lee <christylee@...com>,
Andrii Nakryiko <andrii@...nel.org>,
Ravi Bangoria <ravi.bangoria@....com>,
Leo Yan <leo.yan@...aro.org>,
Yang Jihong <yangjihong1@...wei.com>,
Qi Liu <liuqi115@...wei.com>,
James Clark <james.clark@....com>,
Adrian Hunter <adrian.hunter@...el.com>,
"Masami Hiramatsu (Google)" <mhiramat@...nel.org>,
Kan Liang <kan.liang@...ux.intel.com>,
Sean Christopherson <seanjc@...gle.com>,
Zhengjun Xing <zhengjun.xing@...ux.intel.com>,
Rob Herring <robh@...nel.org>, Xin Gao <gaoxin@...rlc.com>,
Zechuan Chen <chenzechuan1@...wei.com>,
Jason Wang <wangborong@...rlc.com>,
Christophe JAILLET <christophe.jaillet@...adoo.fr>,
Stephane Eranian <eranian@...gle.com>,
German Gomez <german.gomez@....com>,
linux-kernel@...r.kernel.org, linux-perf-users@...r.kernel.org,
bpf@...r.kernel.org, llvm@...ts.linux.dev
Cc: Ian Rogers <irogers@...gle.com>
Subject: [PATCH v1 4/7] tools lib api: Minor strbuf_read improvements
If a read is smaller than the remaining space, don't grow the buffer
as it is likely we've reached the end of the file. Make the grow
amounts a single page rather than just over 2 once the null terminator
is included.
Signed-off-by: Ian Rogers <irogers@...gle.com>
---
tools/lib/api/strbuf.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/tools/lib/api/strbuf.c b/tools/lib/api/strbuf.c
index 4639b2d02e62..eafa2c01f46a 100644
--- a/tools/lib/api/strbuf.c
+++ b/tools/lib/api/strbuf.c
@@ -143,25 +143,28 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
size_t oldalloc = sb->alloc;
int ret;
- ret = strbuf_grow(sb, hint ? hint : 8192);
+ ret = strbuf_grow(sb, hint ? hint : 4095);
if (ret)
return ret;
for (;;) {
- ssize_t cnt;
+ ssize_t read_size;
+ size_t sb_remaining = sb->alloc - sb->len - 1;
- cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
- if (cnt < 0) {
+ read_size = read(fd, sb->buf + sb->len, sb_remaining);
+ if (read_size < 0) {
if (oldalloc == 0)
strbuf_release(sb);
else
strbuf_setlen(sb, oldlen);
- return cnt;
+ return read_size;
}
- if (!cnt)
+ if (read_size == 0)
break;
- sb->len += cnt;
- ret = strbuf_grow(sb, 8192);
+ sb->len += read_size;
+ if ((size_t)read_size < sb_remaining)
+ continue;
+ ret = strbuf_grow(sb, 4095);
if (ret)
return ret;
}
--
2.39.0.314.g84b9a713c41-goog
Powered by blists - more mailing lists