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: 13095717 X-Patchwork-Delegate: bpf@iogearbox.net 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 A5D80C54EBC for ; Tue, 10 Jan 2023 22:22:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234860AbjAJWV7 (ORCPT ); Tue, 10 Jan 2023 17:21:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234837AbjAJWV3 (ORCPT ); Tue, 10 Jan 2023 17:21:29 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B1A92F791 for ; Tue, 10 Jan 2023 14:21:05 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id r8-20020a252b08000000b007b989d5e105so10839411ybr.11 for ; Tue, 10 Jan 2023 14:21:05 -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=s4OfmQBsfI9SVhV/oOoBY3JCn5uO8wKSsV45e1eGfHDmMcxSVcftRvVRQUaxC2of85 2e616CfsMFdbkwZ7LT2XVsm8WmY5SFwtQTJtK0r82FxhnRb7ABhIUdjw+UzHxTin5GEB mLAM7pFfba8cbCOyUysW548/co8PV6O397Jv9K8pqZAmI3ZrLBWinU04UWoQCdRWinr+ JFsQEH4gfb5JSP7bud6kR9jnQlzTewN815HbWMdOLsAC3y1pKRPcdW66yU4gJ/5E+9Ze q2y1+yaLbCN6l+VjgKyi9xQ9mzXIb0I7NNWM6TjBz9OVesATSw1Fu++j/8ejICnF7s9X I1eg== X-Gm-Message-State: AFqh2kpaWVv+Ucs+S77MyUgyMyZqX7AOG22XT82Zl4N2y0Wee4v9hqKm xsCkGuXvH3V2wNosxHrzVTaIX2RnfcYE 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: bpf@vger.kernel.org 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) {