Skip to contentSkip to conversation

kernel/irq/proc: performance: replace seq_printf with seq_put_decimal_ull_width

PATCH Applied David Wang 3 participants 4 messages Nov 3, 2024 – Dec 11, 2024

Subsystems (13)
Arm (32-bit) x86 PowerPC RISC-V +9 more subsystems
lore
Share & tools
lore

David Wang

author
#0 lore raw

PATCH 1/1 kernel/irq/proc: performance: replace seq_printf with seq_put_decimal_ull_width

seq_printf is costy, when stress reading /proc/interrupts, profiling indicates seq_printf takes about ~47% of show_interrupts samples:

    show_interrupts(94.495% 5166019/5466991)
	seq_printf(47.429% 2450210/5166019)
	    vsnprintf(89.232% 2186366/2450210)
		format_decode(24.005% 524831/2186366)
		number(19.488% 426084/2186366)
		memcpy_orig(3.739% 81753/2186366)
		...
	_raw_spin_unlock_irqrestore(26.643% 1376379/5166019)
	mtree_load(8.059% 416304/5166019)

On a system with n CPUs and m interrupts, there will be n*m decimal values yielded via seq_printf(.."%10u "..) which is less efficient than seq_put_decimal_ull_width, stress reading /proc/interrupts indicates ~30% performance improvement with this patch, and profiling data shows:

	show_interrupts(92.221% 3609371/3913823)
	    _raw_spin_unlock_irqrestore(39.123% 1412078/3609371)
	    mtree_load(11.942% 431036/3609371)
	    seq_put_decimal_ull_width(11.635% 419958/3609371)
	    seq_printf(9.025% 325754/3609371)
	    ...

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

seq_put_decimal_ull_width(..," ", v, 10) is equivalent to seq_printf(.., " %10llu", v), not "%10llu "; Hence there is space adjustment along.

---
 arch/alpha/kernel/irq.c     |   8 +--
 arch/arm/kernel/smp.c       |   4 +-
 arch/arm64/kernel/smp.c     |   3 +-
 arch/csky/kernel/smp.c      |   4 +-
 arch/loongarch/kernel/smp.c |   2 +-
 arch/parisc/kernel/irq.c    |  34 ++++++------
 arch/powerpc/kernel/irq.c   |  44 ++++++++--------
 arch/riscv/kernel/smp.c     |   3 +-
 arch/sh/kernel/irq.c        |   4 +-
 arch/sparc/kernel/irq_32.c  |  12 ++---
 arch/sparc/kernel/irq_64.c  |   4 +-
 arch/x86/kernel/irq.c       | 100 ++++++++++++++++++------------------
 arch/xtensa/kernel/irq.c    |   2 +-
 arch/xtensa/kernel/smp.c    |   4 +-
 kernel/irq/proc.c           |   6 ++-
 15 files changed, 120 insertions(+), 114 deletions(-)
arch/alpha/kernel/irq.c +4 -4
index c67047c5d830..0dbb8d3ed647 100644
@@ -72,14 +72,14 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 	int j;
 
 #ifdef CONFIG_SMP
-	seq_puts(p, "IPI: ");
+	seq_puts(p, "IPI:");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10lu ", cpu_data[j].ipi_count);
+		seq_put_decimal_ull_width(p, " ", cpu_data[j].ipi_count, 10);
 	seq_putc(p, '\n');
 #endif
-	seq_puts(p, "PMI: ");
+	seq_puts(p, "PMI:");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10lu ", per_cpu(irq_pmi_count, j));
+		seq_put_decimal_ull_width(p, " ", per_cpu(irq_pmi_count, j), 10);
 	seq_puts(p, "          Performance Monitoring\n");
 	seq_printf(p, "ERR: %10lu\n", irq_err_count);
 	return 0;
arch/arm/kernel/smp.c +2 -2
index 3431c0553f45..1b0680477ae1 100644
@@ -551,10 +551,10 @@ void show_ipi_list(struct seq_file *p, int prec)
 		if (!ipi_desc[i])
 			continue;
 
-		seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
+		seq_printf(p, "%*s%u:", prec - 1, "IPI", i);
 
 		for_each_online_cpu(cpu)
-			seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
+			seq_put_decimal_ull_width(p, " ", irq_desc_kstat_cpu(ipi_desc[i], cpu), 10);
 
 		seq_printf(p, " %s\n", ipi_types[i]);
 	}
arch/arm64/kernel/smp.c +2 -1
index 3b3f6b56e733..35734a30ed8d 100644
@@ -844,7 +844,8 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
 			   prec >= 4 ? " " : "");
 		for_each_online_cpu(cpu)
-			seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
+			seq_put_decimal_ull_width(p, " ",
+						  irq_desc_kstat_cpu(ipi_desc[i], cpu), 10);
 		seq_printf(p, "      %s\n", ipi_types[i]);
 	}
 
arch/csky/kernel/smp.c +2 -2
index 92dbbf3e0205..65f399afc3f0 100644
@@ -112,8 +112,8 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
 			   prec >= 4 ? " " : "");
 		for_each_online_cpu(cpu)
-			seq_printf(p, "%10lu ",
-				per_cpu_ptr(&ipi_data, cpu)->stats[i]);
+			seq_put_decimal_ull_width(p, " ",
+						  per_cpu_ptr(&ipi_data, cpu)->stats[i], 10);
 		seq_printf(p, " %s\n", ipi_names[i]);
 	}
 
arch/loongarch/kernel/smp.c +1 -1
index 9afc2d8b3414..86664adfb3cf 100644
@@ -82,7 +82,7 @@ void show_ipi_list(struct seq_file *p, int prec)
 	for (i = 0; i < NR_IPI; i++) {
 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i, prec >= 4 ? " " : "");
 		for_each_online_cpu(cpu)
-			seq_printf(p, "%10u ", per_cpu(irq_stat, cpu).ipi_irqs[i]);
+			seq_put_decimal_ull_width(p, " ", per_cpu(irq_stat, cpu).ipi_irqs[i], 10);
 		seq_printf(p, " LoongArch  %d  %s\n", i + 1, ipi_types[i]);
 	}
 }
arch/parisc/kernel/irq.c +18 -16
index dff66be65d29..b060d7e6da46 100644
@@ -133,40 +133,42 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 	int j;
 
 #ifdef CONFIG_DEBUG_STACKOVERFLOW
-	seq_printf(p, "%*s: ", prec, "STK");
+	seq_printf(p, "%*s:", prec, "STK");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", irq_stats(j)->kernel_stack_usage);
+		seq_put_decimal_ull_width(p, " ", irq_stats(j)->kernel_stack_usage, 10);
 	seq_puts(p, "  Kernel stack usage\n");
 # ifdef CONFIG_IRQSTACKS
-	seq_printf(p, "%*s: ", prec, "IST");
+	seq_printf(p, "%*s:", prec, "IST");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", irq_stats(j)->irq_stack_usage);
+		seq_put_decimal_ull_width(p, " ", irq_stats(j)->irq_stack_usage, 10);
 	seq_puts(p, "  Interrupt stack usage\n");
 # endif
 #endif
 #ifdef CONFIG_SMP
 	if (num_online_cpus() > 1) {
-		seq_printf(p, "%*s: ", prec, "RES");
+		seq_printf(p, "%*s:", prec, "RES");
 		for_each_online_cpu(j)
-			seq_printf(p, "%10u ", irq_stats(j)->irq_resched_count);
+			seq_put_decimal_ull_width(p, " ",
+						  irq_stats(j)->irq_resched_count, 10);
 		seq_puts(p, "  Rescheduling interrupts\n");
-		seq_printf(p, "%*s: ", prec, "CAL");
+		seq_printf(p, "%*s:", prec, "CAL");
 		for_each_online_cpu(j)
-			seq_printf(p, "%10u ", irq_stats(j)->irq_call_count);
+			seq_put_decimal_ull_width(p, " ",
+						  irq_stats(j)->irq_call_count, 10);
 		seq_puts(p, "  Function call interrupts\n");
 	}
 #endif
-	seq_printf(p, "%*s: ", prec, "UAH");
+	seq_printf(p, "%*s:", prec, "UAH");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", irq_stats(j)->irq_unaligned_count);
+		seq_put_decimal_ull_width(p, " ", irq_stats(j)->irq_unaligned_count, 10);
 	seq_puts(p, "  Unaligned access handler traps\n");
-	seq_printf(p, "%*s: ", prec, "FPA");
+	seq_printf(p, "%*s:", prec, "FPA");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", irq_stats(j)->irq_fpassist_count);
+		seq_put_decimal_ull_width(p, " ", irq_stats(j)->irq_fpassist_count, 10);
 	seq_puts(p, "  Floating point assist traps\n");
-	seq_printf(p, "%*s: ", prec, "TLB");
+	seq_printf(p, "%*s:", prec, "TLB");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", irq_stats(j)->irq_tlb_count);
+		seq_put_decimal_ull_width(p, " ", irq_stats(j)->irq_tlb_count, 10);
 	seq_puts(p, "  TLB shootdowns\n");
 	return 0;
 }
@@ -195,10 +197,10 @@ int show_interrupts(struct seq_file *p, void *v)
 		action = desc->action;
 		if (!action)
 			goto skip;
-		seq_printf(p, "%3d: ", i);
+		seq_printf(p, "%3d:", i);
 
 		for_each_online_cpu(j)
-			seq_printf(p, "%10u ", irq_desc_kstat_cpu(desc, j));
+			seq_put_decimal_ull_width(p, " ", irq_desc_kstat_cpu(desc, j), 10);
 
 		seq_printf(p, " %14s", irq_desc_get_chip(desc)->name);
 #ifndef PARISC_IRQ_CR16_COUNTS
arch/powerpc/kernel/irq.c +22 -22

91 lines not on this page · raw message

arch/riscv/kernel/smp.c +2 -1
index c180a647a30e..f1e9c3db094c 100644
@@ -226,7 +226,8 @@ void show_ipi_stats(struct seq_file *p, int prec)
 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
 			   prec >= 4 ? " " : "");
 		for_each_online_cpu(cpu)
-			seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
+			seq_put_decimal_ull_width(p, " ",
+						  irq_desc_kstat_cpu(ipi_desc[i], cpu), 10);
 		seq_printf(p, " %s\n", ipi_names[i]);
 	}
 }
arch/sh/kernel/irq.c +2 -2
index 4e6835de54cf..9022d8af9d68 100644
@@ -43,9 +43,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 {
 	int j;
 
-	seq_printf(p, "%*s: ", prec, "NMI");
+	seq_printf(p, "%*s:", prec, "NMI");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", per_cpu(irq_stat.__nmi_count, j));
+		seq_put_decimal_ull_width(p, " ", per_cpu(irq_stat.__nmi_count, j), 10);
 	seq_printf(p, "  Non-maskable interrupts\n");
 
 	seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
arch/sparc/kernel/irq_32.c +6 -6
index 8605dd710f3c..5210991429d5 100644
@@ -199,18 +199,18 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 	int j;
 
 #ifdef CONFIG_SMP
-	seq_printf(p, "RES: ");
+	seq_printf(p, "RES:");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", cpu_data(j).irq_resched_count);
+		seq_put_decimal_ull_width(p, " ", cpu_data(j).irq_resched_count, 10);
 	seq_printf(p, "     IPI rescheduling interrupts\n");
-	seq_printf(p, "CAL: ");
+	seq_printf(p, "CAL:");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", cpu_data(j).irq_call_count);
+		seq_put_decimal_ull_width(p, " ", cpu_data(j).irq_call_count, 10);
 	seq_printf(p, "     IPI function call interrupts\n");
 #endif
-	seq_printf(p, "NMI: ");
+	seq_printf(p, "NMI:");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", cpu_data(j).counter);
+		seq_put_decimal_ull_width(p, " ", cpu_data(j).counter, 10);
 	seq_printf(p, "     Non-maskable interrupts\n");
 	return 0;
 }
arch/sparc/kernel/irq_64.c +2 -2
index 01ee800efde3..9ab6e79b617b 100644
@@ -304,9 +304,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 {
 	int j;
 
-	seq_printf(p, "NMI: ");
+	seq_printf(p, "NMI:");
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
+		seq_put_decimal_ull_width(p, " ", cpu_data(j).__nmi_count, 10);
 	seq_printf(p, "     Non-maskable interrupts\n");
 	return 0;
 }
arch/x86/kernel/irq.c +50 -50

181 lines not on this page · raw message

arch/xtensa/kernel/irq.c +1 -1
index b1e410f6b5ab..c4f46989f9b7 100644
@@ -58,7 +58,7 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 #if XTENSA_FAKE_NMI
 	seq_printf(p, "%*s:", prec, "NMI");
 	for_each_online_cpu(cpu)
-		seq_printf(p, " %10lu", per_cpu(nmi_count, cpu));
+		seq_put_decimal_ull_width(p, " ", per_cpu(nmi_count, cpu), 10);
 	seq_puts(p, "   Non-maskable interrupts\n");
 #endif
 	return 0;
arch/xtensa/kernel/smp.c +2 -2
index 94a23f100726..71ec9eced8b9 100644
@@ -453,8 +453,8 @@ void show_ipi_list(struct seq_file *p, int prec)
 	for (i = 0; i < IPI_MAX; ++i) {
 		seq_printf(p, "%*s:", prec, ipi_text[i].short_text);
 		for_each_online_cpu(cpu)
-			seq_printf(p, " %10lu",
-					per_cpu(ipi_data, cpu).ipi_count[i]);
+			seq_put_decimal_ull_width(p, " ",
+						  per_cpu(ipi_data, cpu).ipi_count[i], 10);
 		seq_printf(p, "   %s\n", ipi_text[i].long_text);
 	}
 }
kernel/irq/proc.c +4 -2
index 9081ada81c3d..988ce781e813 100644
@@ -494,9 +494,11 @@ int show_interrupts(struct seq_file *p, void *v)
 	if (!desc->action || irq_desc_is_chained(desc) || !desc->kstat_irqs)
 		goto outsparse;
 
-	seq_printf(p, "%*d: ", prec, i);
+	seq_printf(p, "%*d:", prec, i);
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0);
+		seq_put_decimal_ull_width(p, " ",
+					  desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0,
+					  10);
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
Signature · 1 line
2.39.2
#1 lore raw

David!

On Sun, Nov 03 2024 at 16:05, David Wang wrote:

$Subject: [PATCH] kernel/irq/proc: performance: ...

That's not a valid subsystem prefix.

Quoted 2 lines

seq_printf is costy, when stress reading /proc/interrupts, profiling indicates seq_printf takes about ~47% of show_interrupts samples:

Also please follow the documentation for denoting functions in change logs:

https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#function-references-in-changelogs

Quoted 7 lines
 arch/alpha/kernel/irq.c     |   8 +--
 arch/arm/kernel/smp.c       |   4 +-
 arch/arm64/kernel/smp.c     |   3 +-
 arch/csky/kernel/smp.c      |   4 +-
 arch/loongarch/kernel/smp.c |   2 +-
 arch/parisc/kernel/irq.c    |  34 ++++++------
 arch/powerpc/kernel/irq.c   |  44 ++++++++--------
Quoted 8 lines
 arch/riscv/kernel/smp.c     |   3 +-
 arch/sh/kernel/irq.c        |   4 +-
 arch/sparc/kernel/irq_32.c  |  12 ++---
 arch/sparc/kernel/irq_64.c  |   4 +-
 arch/x86/kernel/irq.c       | 100 ++++++++++++++++++------------------
 arch/xtensa/kernel/irq.c    |   2 +-
 arch/xtensa/kernel/smp.c    |   4 +-
 kernel/irq/proc.c           |   6 ++-

There is no dependency on these changes. So please split them up into seperate patches for core and the individual architectures.

Thanks,

tglx

author
#2 lore raw

Hi,

Quoted 7 lines · Thomas Gleixner

David!

On Sun, Nov 03 2024 at 16:05, David Wang wrote:

$Subject: [PATCH] kernel/irq/proc: performance: ...

That's not a valid subsystem prefix.

copy that~

Quoted 9 lines
Quoted 3 lines

seq_printf is costy, when stress reading /proc/interrupts, profiling indicates seq_printf takes about ~47% of show_interrupts samples:

Also please follow the documentation for denoting functions in change logs:

https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#function-references-in-changelogs

copy that~

Quoted 16 lines
Quoted 7 lines
 arch/alpha/kernel/irq.c     |   8 +--
 arch/arm/kernel/smp.c       |   4 +-
 arch/arm64/kernel/smp.c     |   3 +-
 arch/csky/kernel/smp.c      |   4 +-
 arch/loongarch/kernel/smp.c |   2 +-
 arch/parisc/kernel/irq.c    |  34 ++++++------
 arch/powerpc/kernel/irq.c   |  44 ++++++++--------
Quoted 8 lines
 arch/riscv/kernel/smp.c     |   3 +-
 arch/sh/kernel/irq.c        |   4 +-
 arch/sparc/kernel/irq_32.c  |  12 ++---
 arch/sparc/kernel/irq_64.c  |   4 +-
 arch/x86/kernel/irq.c       | 100 ++++++++++++++++++------------------
 arch/xtensa/kernel/irq.c    |   2 +-
 arch/xtensa/kernel/smp.c    |   4 +-
 kernel/irq/proc.c           |   6 ++-
Quoted 2 lines

There is no dependency on these changes. So please split them up into seperate patches for core and the individual architectures.an

Thanks for all the review, I will make a patchset for this.

Quoted 4 lines

Thanks,

tglx

David

#3 lore raw
In reply to David Wang
This patch was applied to riscv/linux.git (fixes) by Michael Ellerman <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Michael Ellerman <[email protected]>:

Quoted 13 lines · On Sun, 3 Nov 2024 16:05:52 +0800 you wrote:

seq_printf is costy, when stress reading /proc/interrupts, profiling indicates seq_printf takes about ~47% of show_interrupts samples:

    show_interrupts(94.495% 5166019/5466991)
	seq_printf(47.429% 2450210/5166019)
	    vsnprintf(89.232% 2186366/2450210)
		format_decode(24.005% 524831/2186366)
		number(19.488% 426084/2186366)
		memcpy_orig(3.739% 81753/2186366)
		...
	_raw_spin_unlock_irqrestore(26.643% 1376379/5166019)
	mtree_load(8.059% 416304/5166019)
Quoted 1 line

[...]

Here is the summary with links:
- kernel/irq/proc: performance: replace seq_printf with seq_put_decimal_ull_width

    https://git.kernel.org/riscv/c/5b881c1f8379

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html