From patchwork Tue Jan 10 22:20:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41680 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2996978wrt; Tue, 10 Jan 2023 14:22:44 -0800 (PST) X-Google-Smtp-Source: AMrXdXvmQ+zciKO+3Q0FB7J1guLQ7KliBTl4RCI2WYONiuQh9fiEH1NpuSm9ChwrhTjuVobvSj7v X-Received: by 2002:a17:902:f791:b0:192:5ec4:6656 with SMTP id q17-20020a170902f79100b001925ec46656mr72310585pln.3.1673389364607; Tue, 10 Jan 2023 14:22:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389364; cv=none; d=google.com; s=arc-20160816; b=hqKHXyC4O6IL64Dd1OqCqy6EQgn1TQNEv5SaNeqmapsvCHIlNraZl03wraYhM1wkZ+ XMO2CqoGiN+DjxP5ogT+lFXBEBRPpAhj8n/z8EYzi0EPE/DlDuijSNClR7K/LSvA4Wx4 M71wRrBypFYAYkK+Jay9rDvBb2JmrwYyDr9l5SxHE+TrfW/hTtm6qk0ztYEhNmLlasXU ipONp8J46SFXaLg8gL8TS71nonEMBvEFCXKKQ7w5l3WKWoykakYLPHZkfo/KRXjUu1+V SFPoo9Z8RDz3InnYIqJQ4W51g4l2M79WxXgJDE+ZfMcZcSQt3py+OVn+rrmKCrtFv8no B0yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=b4cDFSVYG43croOnEV27nNQKcdSJ0iRzIK+gxT25DWQ=; b=HOqQa200bOtLpJOT5bYQETCv9GVu29JxFtw0q69zZ6atA5D6VJ0GBLm1+XBMDwQ7/g HIDKR1bnHB/7q9OLzNjtse/kUZK0Le9CkO7TsIG1jzEHkgWA7SXZvOBArwzwJpso9mKO ZgZFIQ+pAKV12hhik9VjPpbL8fMeY4GXEgWbl4ywXpZ+TC9vqVXhRpm5VcqH/ZeIoXds wGr+jrS/axCj0+DcB+tKnYe7DxB3lB1vDQFw0IMGAOsiy0+9oPcLegCkOu+0eFazeyef fjHZO9M5RbRrr026Op4tW1zlaMqWKbICqM1QV06BMH8jld2/o/aoH1hBw70xmQIlDkJO jH3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oWwqPFHv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q6-20020a17090311c600b0018938988ea9si13137011plh.520.2023.01.10.14.22.31; Tue, 10 Jan 2023 14:22:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oWwqPFHv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235189AbjAJWVy (ORCPT + 99 others); Tue, 10 Jan 2023 17:21:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235259AbjAJWVV (ORCPT ); Tue, 10 Jan 2023 17:21:21 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06E12101D6 for ; Tue, 10 Jan 2023 14:20:57 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 11-20020a25050b000000b007b7968a6423so14250800ybf.13 for ; Tue, 10 Jan 2023 14:20:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=b4cDFSVYG43croOnEV27nNQKcdSJ0iRzIK+gxT25DWQ=; b=oWwqPFHv+dZCSeNo+f2delVO4bkS5CysJ1Fb9cTlI9zPxRA7uKaiJ7fzrQAE/gTePN yQdJfBq26Z53aSri1L4flICMFvFNUevMxBBdhNvHv8Ijb0Oku7b8R3i1GIZwAS7jcJ22 jMmWxbFtkBzDL/R9uaLOc+rwZEKLhEkSnycWiK+bJoz5ZUvP8k/r3gzwpyc58iJZ/qIR uFv/RK4kFlujjtJHI9F8csecrHrl/8oIz/UTVfp75WDyghQbB2noKVROCgEIL9/hytzp 05/J55rtJDAGcO6Q4BwMKuBUc0CZ06wbMj2tK5aFbb8NTedwer0JBGvwhthbuu/kH2CO qRSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc: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=b4cDFSVYG43croOnEV27nNQKcdSJ0iRzIK+gxT25DWQ=; b=v8yJgBnlAbfPahz2UL5L00cZhIHn3/MtSzqdTyS01dfG8Zu+OE754HGaNNkTibOCig edyzwsMNCT2573IEBuqcKz3n4VyjIv+Hk7axlR93MZQoi5HgS6ismI8usaLp4zG+ASO1 yYgGyDAIdSHWaZh3Ox3AqFNLtYgB5u0vKcCoKIZNZDaTRusUwZp089tE5+ybTWrOBRtJ PoWDOyggX+RaJDhUtIdaxBMRMzCnFZi6NNLox9N1gGkoGZjrV2FHEVCFVdNJ9Nd8FS38 eZYcYE221Zv59JRXxXvIF65fjAQ4TjVxacyT7pP/rL4ZjgkkPaoJ/6iwpWILgssmpmMt /VzA== X-Gm-Message-State: AFqh2kq6s8RJAMG3E/2HYpNRafG0Jv9WHsUKpS3M4KPYzy/cJl7FcGIP cayPSUAIIgAtPLj1AO4loFLG0V1JbMPN X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a25:9a02:0:b0:7ba:f028:b325 with SMTP id x2-20020a259a02000000b007baf028b325mr1547877ybn.452.1673389256480; Tue, 10 Jan 2023 14:20:56 -0800 (PST) Date: Tue, 10 Jan 2023 14:20:00 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-5-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 4/7] tools lib api: Minor strbuf_read improvements From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754675926509340526?= X-GMAIL-MSGID: =?utf-8?q?1754675926509340526?= 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 --- 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; }