From nobody Mon Sep 15 19:47:59 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D35E5C54EBC for ; Tue, 10 Jan 2023 22:22:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235028AbjAJWW2 (ORCPT ); Tue, 10 Jan 2023 17:22:28 -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-Google-Smtp-Source: AMrXdXul//OabQOCLWyx1Luc6Hl+ueTmgO396xd2lzHTTOh6r4Z7uBXUeTt67VSekVDq2442YXd8T9SPuWGF 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; } =20 -#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 <=3D sb->len) return -E2BIG; =20 - if (alloc_nr(sb->alloc) > nr) - nr =3D alloc_nr(sb->alloc); - /* * Note that sb->buf =3D=3D strbuf_slopbuf if sb->alloc =3D=3D 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; =20 sb->buf =3D buf; - sb->alloc =3D nr; + sb->alloc =3D malloc_usable_size(buf); return 0; } -#undef alloc_nr =20 int strbuf_addch(struct strbuf *sb, int c) { --=20 2.39.0.314.g84b9a713c41-goog