From patchwork Tue Jan 10 22:20:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41681 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2997166wrt; Tue, 10 Jan 2023 14:23:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXvxV+r90UYBbJGhdS9PVXtdhQBpAaCb169qKdJ6OUkxa6jsmbeN04HqCRjIHdQZWQYXEfw2 X-Received: by 2002:a05:6402:1005:b0:47e:5b70:dea5 with SMTP id c5-20020a056402100500b0047e5b70dea5mr63350623edu.18.1673389399897; Tue, 10 Jan 2023 14:23:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389399; cv=none; d=google.com; s=arc-20160816; b=XxiqUagX75TaG1ZTWmtKPhHRs2eJ+LLFuo2cnsHveVLPVNTEDoXWlXTrhZ8/YmReTF ijfa7tGmRUzH/7NUOcdV22EQURaRMBiksNP5OLqefh6Fez4kPJMNwJAgtM/BeMeKF1WO cMlOwUNtWqLeuEqm4eC7/uCcxwyraV3SZX0KgRORrCpwVk9cfUb7DG+Z5YRLOYBj92w6 opQr0NxkzqNSQFU3Kd+X4fmr5DAeObQ7zuchP3HybSR5UfCqQvmQWHuYa3U1IK51rCZH IpMSKSLquD3IpAaGoJznBr5S9fc4IZxcOXxjTwMJf5RvjsQMUsZ7QA3SBPUwNH26oO6f I+3w== 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=4sg2Aa5uCHWcDC8jm9J4EAv2OJFGtXnNHMXMT9mO4Gw=; b=HZwLF5WX+O7FNp/xUaiFoUXjG1T5kBZK5Afsx1LawuqBtzxs9dJGh4RYqTXOBMkYEb zI2xGulxLX4RbxKkKtI05HqbUVKFEHaa7pgBr2MQFFRkRF8v4R0I9iDZ0FSdJdnzvQYv wksK2ZsEtpbktaGa6suIvZxo1pOWHozg38tvYgNXoQYLtASgCYHcojNdTqRArU770WXG XRTn/NxtsCQgCbh5lKhxVX+sUddYLYsOVBD8RiGX9QlJg8hkpUpUMYTlyLikdylMKQ+y iSVcaAjaeXg+0Kt3atq5Oepb9PIrmtWHJPCSve960i6pVDrfrHpw00J0206e5P/rPLxQ vwOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=AW29Ki0i; 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 z16-20020a056402275000b00483b197bbe2si16874828edd.122.2023.01.10.14.22.55; Tue, 10 Jan 2023 14:23:19 -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=AW29Ki0i; 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 S235385AbjAJWW1 (ORCPT + 99 others); Tue, 10 Jan 2023 17:22:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235236AbjAJWVu (ORCPT ); Tue, 10 Jan 2023 17:21:50 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8309863F7D for ; Tue, 10 Jan 2023 14:21:13 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-487b0bf1117so141681217b3.5 for ; Tue, 10 Jan 2023 14:21:13 -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=4sg2Aa5uCHWcDC8jm9J4EAv2OJFGtXnNHMXMT9mO4Gw=; b=AW29Ki0ipdxS14sshMxXhqz4FZFcNVmZ3WvoBGdx8Bla7+yjxNrYSNmR1ihsyWRsrf X60XFLMewiYm9IGefUm4eEzu+c8CeyIivN4qFu4JpwnYwB3jA2QMO7bOOM3Ko7g1jq8K C5RnWXn1KhH66oHffsmy/GqlYcVahiBR3eqjUSNwhLqIpkauqFmAyRmy+3bD+cnZC0dZ KFqMPnIG2GLgHMH6PYQiwuTfPPwsF8vA5I0JxA/flSZfhMIfeqXMaRE2xMVljQClk+CN NAwV6KvZTl/6ClD/vaGcAe7VaWC8QwAk0u7Hs7wmIUwxTE33GdGdAYSdTQfnpepXNN/1 khRg== 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=4sg2Aa5uCHWcDC8jm9J4EAv2OJFGtXnNHMXMT9mO4Gw=; b=quQGUIJnIRJcBLOpYedVJsaGlUh7bOlsgx0SJ983BvnwyURHTEYcKt6uT31Sh1VZTM IBOOhzG5zVeapbF3BcOpitduHnsATktvdQOXvnIULP8wNSYL0u6FKyr1PozUutdKtvqx oaFyD+FpTmkzg3NmlfjmBNKsU31rkWld/f7B5H0rpXA2G1nYUNZnyxqDXf0vNE0ItACi 8xzNYy0uMlp1xpkr3sd1/UemsepvyhY267U61hJuyXA1QQ93N+dXeEIt/4ydUWU4eSza MXtOWyQQdoLfivrqzlaqO33qbIfM5qQMuf93XpLN9yzr3xBV5//PNUUYN9s6u2uSwZOu 33ag== X-Gm-Message-State: AFqh2kpsV1pXnuTe0RIE9P9PChn//xcjMWc9JwxL4VHZ8Rnx8iy69YMi iu+9kNIUs5Q9OhPMiZPbWtM1M7esWE+5 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a25:449:0:b0:7b6:f8d5:82f4 with SMTP id 70-20020a250449000000b007b6f8d582f4mr2181307ybe.646.1673389264926; Tue, 10 Jan 2023 14:21:04 -0800 (PST) Date: Tue, 10 Jan 2023 14:20:01 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-6-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 5/7] tools lib api: Tweak strbuf allocation size computation 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?1754675963292655707?= X-GMAIL-MSGID: =?utf-8?q?1754675963292655707?= alloc_nr gives an estimate of the actual memory behind an allocation but isn't accurate. Use malloc_usable_size to accurately set the strbuf alloc, which potentially avoids realloc calls. Signed-off-by: Ian Rogers --- tools/lib/api/strbuf.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tools/lib/api/strbuf.c b/tools/lib/api/strbuf.c index eafa2c01f46a..a3d7f96d8b9f 100644 --- a/tools/lib/api/strbuf.c +++ b/tools/lib/api/strbuf.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -42,7 +43,6 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz) return res; } -#define alloc_nr(x) (((x)+16)*3/2) int strbuf_grow(struct strbuf *sb, size_t extra) { char *buf; @@ -54,9 +54,6 @@ int strbuf_grow(struct strbuf *sb, size_t extra) if (nr <= sb->len) return -E2BIG; - if (alloc_nr(sb->alloc) > nr) - nr = alloc_nr(sb->alloc); - /* * Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is * a static variable. Thus we have to avoid passing it to realloc. @@ -66,10 +63,9 @@ int strbuf_grow(struct strbuf *sb, size_t extra) return -ENOMEM; sb->buf = buf; - sb->alloc = nr; + sb->alloc = malloc_usable_size(buf); return 0; } -#undef alloc_nr int strbuf_addch(struct strbuf *sb, int c) {