diff mbox series

[v2] refscale: Optimize process_durations()

Message ID 77d6d8ee94d7149e6c33b10b78b43daf48ab8c70.1721546569.git.christophe.jaillet@wanadoo.fr (mailing list archive)
State Accepted
Commit 678fa49a6281a5372293fc5d0d9d73a2eb380367
Headers show
Series [v2] refscale: Optimize process_durations() | expand

Commit Message

Christophe JAILLET July 21, 2024, 7:23 a.m. UTC
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.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This patch is compile tested only.
I introduced a bug in v1 (see 1st item in the changes below), so it should
definitively be tested by someone!

v1 was un-usable because seq_buf_putc() was not exported. It is now
available since v6.7.

Because of the changes, I don't know if the previous R-b should kept.
In case, it was:
	Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
	Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>

Changes in v2:
  - Use seq_buf_printf() alseo for the first string, otherwise it would
    get lost
  - Use seq_buf_str() instead of seq_buf_terminate() because the API has
    changed

v1: https://lore.kernel.org/all/bbbab32e3e104bdc2238724a6a4a85e539f49ddd.1698512661.git.christophe.jaillet@wanadoo.fr/
---
 kernel/rcu/refscale.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

Comments

Paul E. McKenney July 21, 2024, 4:44 p.m. UTC | #1
On Sun, Jul 21, 2024 at 09:23:46AM +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.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> This patch is compile tested only.
> I introduced a bug in v1 (see 1st item in the changes below), so it should
> definitively be tested by someone!
> 
> v1 was un-usable because seq_buf_putc() was not exported. It is now
> available since v6.7.
> 
> Because of the changes, I don't know if the previous R-b should kept.
> In case, it was:
> 	Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> 	Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>

It would be good for them to take another look.  In the meantime:

Tested-by: Paul E. McKenney <paulmck@kernel.org>

> Changes in v2:
>   - Use seq_buf_printf() alseo for the first string, otherwise it would
>     get lost
>   - Use seq_buf_str() instead of seq_buf_terminate() because the API has
>     changed
> 
> v1: https://lore.kernel.org/all/bbbab32e3e104bdc2238724a6a4a85e539f49ddd.1698512661.git.christophe.jaillet@wanadoo.fr/
> ---
>  kernel/rcu/refscale.c | 25 ++++++++++++++-----------
>  1 file changed, 14 insertions(+), 11 deletions(-)
> 
> diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c
> index f4ea5b1ec068..cfec0648e141 100644
> --- a/kernel/rcu/refscale.c
> +++ b/kernel/rcu/refscale.c
> @@ -28,6 +28,7 @@
>  #include <linux/rcupdate_trace.h>
>  #include <linux/reboot.h>
>  #include <linux/sched.h>
> +#include <linux/seq_buf.h>
>  #include <linux/spinlock.h>
>  #include <linux/smp.h>
>  #include <linux/stat.h>
> @@ -891,32 +892,34 @@ 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;
> -	sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
> -		exp_idx);
> +	seq_buf_init(&s, buf, 800 + 64);
> +
> +	seq_buf_printf(&s, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
> +		       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) {
> -			pr_alert("%s", buf);
> -			buf[0] = 0;
> +			seq_buf_putc(&s, '\n');
> +
> +		if (seq_buf_used(&s) >= 800) {
> +			pr_alert("%s", seq_buf_str(&s));
> +			seq_buf_clear(&s);
>  		}
> -		strcat(buf, buf1);
> +
> +		seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns);
>  
>  		sum += rt->last_duration_ns;
>  	}
> -	pr_alert("%s\n", buf);
> +	pr_alert("%s\n", seq_buf_str(&s));
>  
>  	kfree(buf);
>  	return sum;
> -- 
> 2.45.2
> 
>
diff mbox series

Patch

diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c
index f4ea5b1ec068..cfec0648e141 100644
--- a/kernel/rcu/refscale.c
+++ b/kernel/rcu/refscale.c
@@ -28,6 +28,7 @@ 
 #include <linux/rcupdate_trace.h>
 #include <linux/reboot.h>
 #include <linux/sched.h>
+#include <linux/seq_buf.h>
 #include <linux/spinlock.h>
 #include <linux/smp.h>
 #include <linux/stat.h>
@@ -891,32 +892,34 @@  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;
-	sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
-		exp_idx);
+	seq_buf_init(&s, buf, 800 + 64);
+
+	seq_buf_printf(&s, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
+		       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) {
-			pr_alert("%s", buf);
-			buf[0] = 0;
+			seq_buf_putc(&s, '\n');
+
+		if (seq_buf_used(&s) >= 800) {
+			pr_alert("%s", seq_buf_str(&s));
+			seq_buf_clear(&s);
 		}
-		strcat(buf, buf1);
+
+		seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns);
 
 		sum += rt->last_duration_ns;
 	}
-	pr_alert("%s\n", buf);
+	pr_alert("%s\n", seq_buf_str(&s));
 
 	kfree(buf);
 	return sum;