From mboxrd@z Thu Jan 1 00:00:00 1970 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 269F1C4167B for ; Sat, 28 Oct 2023 21:30:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229865AbjJ1VaN (ORCPT ); Sat, 28 Oct 2023 17:30:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjJ1VaM (ORCPT ); Sat, 28 Oct 2023 17:30:12 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA0F2CA; Sat, 28 Oct 2023 14:30:09 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95391C433C8; Sat, 28 Oct 2023 21:30:07 +0000 (UTC) Date: Sat, 28 Oct 2023 17:30:05 -0400 From: Steven Rostedt To: Christophe JAILLET Cc: Davidlohr Bueso , "Paul E. McKenney" , Josh Triplett , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, rcu@vger.kernel.org Subject: Re: [PATCH] refscale: Optimize process_durations() Message-ID: <20231028173005.5d266f7a@rorschach.local.home> In-Reply-To: References: X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 28 Oct 2023 19:04:44 +0200 Christophe JAILLET wrote: > process_durations() is not a hot path, but there is no good reason to > iterate over and over the data already in 'buf'. > > Using a seq_buf saves some useless strcat() and the need of a temp buffer. > Data is written directly at the correct place. > Agreed. > Signed-off-by: Christophe JAILLET > --- > kernel/rcu/refscale.c | 20 +++++++++++++------- > 1 file changed, 13 insertions(+), 7 deletions(-) > > diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c > index 2c2648a3ad30..861485d865ec 100644 > --- a/kernel/rcu/refscale.c > +++ b/kernel/rcu/refscale.c > @@ -28,6 +28,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -890,31 +891,36 @@ static u64 process_durations(int n) > { > int i; > struct reader_task *rt; > - char buf1[64]; > + struct seq_buf s; > char *buf; > u64 sum = 0; > > buf = kmalloc(800 + 64, GFP_KERNEL); > if (!buf) > return 0; > - buf[0] = 0; > + > + seq_buf_init(&s, buf, 800 + 64); > + > sprintf(buf, "Experiment #%d (Format: :)", > exp_idx); > > for (i = 0; i < n && !torture_must_stop(); i++) { > rt = &(reader_tasks[i]); > - sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); > > if (i % 5 == 0) > - strcat(buf, "\n"); > - if (strlen(buf) >= 800) { > + seq_buf_putc(&s, '\n'); I was confused here thinking it was originally adding a '\n' to buf1 on i % 5, but it's adding it to buf! Yeah, using seq_buf is much less confusing and then less error prone. Reviewed-by: Steven Rostedt (Google) -- Steve > + > + if (seq_buf_used(&s) >= 800) { > + seq_buf_terminate(&s); > pr_alert("%s", buf); > - buf[0] = 0; > + seq_buf_clear(&s); > } > - strcat(buf, buf1); > + > + seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns); > > sum += rt->last_duration_ns; > } > + seq_buf_terminate(&s); > pr_alert("%s\n", buf); > > kfree(buf);