Skip to contentSkip to conversation

proc: add seq_put_decimal_ull_align to speed up /proc/pid/smaps

PATCH Superseded Andrei Vagin 4 participants 9 messages Jan 29, 2018 – Feb 12, 2018

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 proc: add seq_put_decimal_ull_align to speed up /proc/pid/smaps

seq_put_decimal_ull_align(m, str, val, width) is equivalent of seq_printf(m, "%s%*d", str, width, val), but it works much faster.

== test_smaps.py
num = 0
with open("/proc/1/smaps") as f:

        for x in xrange(10000):
                data = f.read()
                f.seek(0, 0)

==

== Before patch ==

$ time python test_smaps.py
real    0m4.593s
user    0m0.398s
sys     0m4.158s

== After patch ==

$ time python test_smaps.py
real    0m3.828s
user    0m0.413s
sys     0m3.408s
$ perf -g record python test_smaps.py
== Before patch ==
-   79.01%     3.36%  python   [kernel.kallsyms]    [k] show_smap.isra.33
   - 75.65% show_smap.isra.33
      + 48.85% seq_printf
      + 15.75% __walk_page_range
      + 9.70% show_map_vma.isra.23
        0.61% seq_puts

== After patch ==
- 75.51% 4.62% python [kernel.kallsyms] [k] show_smap.isra.33
- 70.88% show_smap.isra.33
+ 24.82% seq_put_decimal_ull_aligned
+ 19.78% __walk_page_range
+ 12.74% seq_printf
+ 11.08% show_map_vma.isra.23
+ 1.68% seq_puts

Cc Alexey Dobriyan <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
---
 fs/proc/meminfo.c        |  15 +-----
 fs/proc/task_mmu.c       | 124 +++++++++++++++++++----------------------------
 fs/seq_file.c            |  24 +++++++--
 include/linux/kernel.h   |   2 +-
 include/linux/seq_file.h |   2 +
 lib/vsprintf.c           |  18 +++++--
 6 files changed, 88 insertions(+), 97 deletions(-)
fs/proc/meminfo.c +1 -14
index 6bb20f864259..2b197ce8b99f 100644
@@ -26,20 +26,7 @@ void __attribute__((weak)) arch_report_meminfo(struct seq_file *m)
 
 static void show_val_kb(struct seq_file *m, const char *s, unsigned long num)
 {
-	char v[32];
-	static const char blanks[7] = {' ', ' ', ' ', ' ',' ', ' ', ' '};
-	int len;
-
-	len = num_to_str(v, sizeof(v), num << (PAGE_SHIFT - 10));
-
-	seq_write(m, s, 16);
-
-	if (len > 0) {
-		if (len < 8)
-			seq_write(m, blanks, 8 - len);
-
-		seq_write(m, v, len);
-	}
+	seq_put_decimal_ull_aligned(m, s, num << (PAGE_SHIFT - 10), 8);
 	seq_write(m, " kB\n", 4);
 }
 
fs/proc/task_mmu.c +51 -73

157 lines not on this page · raw message

fs/seq_file.c +19 -5
index d2885220ecf7..55c89eb95a90 100644
@@ -677,8 +677,8 @@ EXPORT_SYMBOL(seq_puts);
  * This routine is very quick when you show lots of numbers.
  * In usual cases, it will be better to use seq_printf(). It's easier to read.
  */
-void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
-			 unsigned long long num)
+void seq_put_decimal_ull_aligned(struct seq_file *m, const char *delimiter,
+			 unsigned long long num, int width)
 {
 	int len;
 
@@ -692,15 +692,23 @@ void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
 	memcpy(m->buf + m->count, delimiter, len);
 	m->count += len;
 
-	if (m->count + 1 >= m->size)
+	if (!width)
+		width = 1;
+
+	if (m->count + width >= m->size)
 		goto overflow;
 
 	if (num < 10) {
+		int i;
+
+		for (i = 0; i < width - 1; i++)
+			m->buf[m->count++] = ' ';
+
 		m->buf[m->count++] = num + '0';
 		return;
 	}
 
-	len = num_to_str(m->buf + m->count, m->size - m->count, num);
+	len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
 	if (!len)
 		goto overflow;
 
@@ -710,6 +718,12 @@ void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
 overflow:
 	seq_set_overflow(m);
 }
+
+void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
+			 unsigned long long num)
+{
+	return seq_put_decimal_ull_aligned(m, delimiter, num, 0);
+}
 EXPORT_SYMBOL(seq_put_decimal_ull);
 
 /**
@@ -784,7 +798,7 @@ void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num
 		return;
 	}
 
-	len = num_to_str(m->buf + m->count, m->size - m->count, num);
+	len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
 	if (!len)
 		goto overflow;
 
include/linux/kernel.h +1 -1
index ce51455e2adf..6485a32db7d5 100644
@@ -439,7 +439,7 @@ extern long simple_strtol(const char *,char **,unsigned int);
 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
 extern long long simple_strtoll(const char *,char **,unsigned int);
 
-extern int num_to_str(char *buf, int size, unsigned long long num);
+extern int num_to_str(char *buf, int size, unsigned long long num, int width);
 
 /* lib/printf utilities */
 
include/linux/seq_file.h +2 -0
index 53f238934d7f..521de9e41227 100644
@@ -118,6 +118,8 @@ __printf(2, 3)
 void seq_printf(struct seq_file *m, const char *fmt, ...);
 void seq_putc(struct seq_file *m, char c);
 void seq_puts(struct seq_file *m, const char *s);
+void seq_put_decimal_ull_aligned(struct seq_file *m, const char *delimiter,
+			 unsigned long long num, int width);
 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
 			 unsigned long long num);
 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num);
lib/vsprintf.c +14 -4
index 01c3957b2de6..b8015e165a54 100644
@@ -337,7 +337,7 @@ char *put_dec(char *buf, unsigned long long n)
  *
  * If speed is not important, use snprintf(). It's easy to read the code.
  */
-int num_to_str(char *buf, int size, unsigned long long num)
+int num_to_str(char *buf, int size, unsigned long long num, int width)
 {
 	/* put_dec requires 2-byte alignment of the buffer. */
 	char tmp[sizeof(num) * 3] __aligned(2);
@@ -351,11 +351,21 @@ int num_to_str(char *buf, int size, unsigned long long num)
 		len = put_dec(tmp, num) - tmp;
 	}
 
-	if (len > size)
+	if (len > size || width > size)
 		return 0;
+
+	if (width > len) {
+		width = width - len;
+		for (idx = 0; idx < width; idx++)
+			buf[idx] = ' ';
+	} else {
+		width = 0;
+	}
+
 	for (idx = 0; idx < len; ++idx)
-		buf[idx] = tmp[len - idx - 1];
-	return len;
+		buf[idx + width] = tmp[len - idx - 1];
+
+	return len + width;
 }
 
 #define SIGN	1		/* unsigned/signed, must be 1 */
Signature · 1 line
2.13.6
#1 lore raw

PATCH 2/4 proc: replace seq_printf on seq_putc to speed up /proc/pid/smaps

seq_putc() works much faster than seq_printf()

$ time python test_smaps.py

== Before patch ==

real    0m3.828s
user    0m0.413s
sys     0m3.408s

== After patch ==
real 0m3.405s
user 0m0.401s
sys 0m3.003s

== Before patch ==
- 75.51% 4.62% python [kernel.kallsyms] [k] show_smap.isra.33
- 70.88% show_smap.isra.33
+ 24.82% seq_put_decimal_ull_aligned
+ 19.78% __walk_page_range
+ 12.74% seq_printf
+ 11.08% show_map_vma.isra.23
+ 1.68% seq_puts

== After patch ==
- 69.16% 5.70% python [kernel.kallsyms] [k] show_smap.isra.33
- 63.46% show_smap.isra.33
+ 25.98% seq_put_decimal_ull_aligned
+ 20.90% __walk_page_range
+ 12.60% show_map_vma.isra.23
1.56% seq_putc
+ 1.55% seq_puts

Cc Alexey Dobriyan <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
---
 fs/proc/task_mmu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/task_mmu.c +3 -2
index cc0aaf3a7315..d7a429132a1f 100644
@@ -685,8 +685,9 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
 		if (!mnemonics[i][0])
 			continue;
 		if (vma->vm_flags & (1UL << i)) {
-			seq_printf(m, "%c%c ",
-				   mnemonics[i][0], mnemonics[i][1]);
+			seq_putc(m, mnemonics[i][0]);
+			seq_putc(m, mnemonics[i][1]);
+			seq_putc(m, ' ');
 		}
 	}
 	seq_putc(m, '\n');
Signature · 1 line
2.13.6
#2 lore raw

PATCH 3/4 proc: optimize single-symbol delimiters to spead up seq_put_decimal_ull

In reply to Andrei Vagin

A delimiter is a string which is printed before a number.
A syngle-symbol delimiters can be printed by set_putc() and this works faster than printing by set_puts().

== test_proc.c

int main(int argc, char **argv)

{
	int n, i, fd;
	char buf[16384];

	n = atoi(argv[1]);
	for (i = 0; i < n; i++) {
		fd = open(argv[2], O_RDONLY);
		if (fd < 0)
			return 1;
		if (read(fd, buf, sizeof(buf)) <= 0)
			return 1;
		close(fd);
	}

	return 0;
}

==

$ time ./test_proc  1000000 /proc/1/stat

== Before patch ==
real 0m3.820s
user 0m0.337s
sys 0m3.394s

== After patch ==
real 0m3.110s
user 0m0.324s
sys 0m2.700s

---
 fs/seq_file.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
fs/seq_file.c +12 -12
index 55c89eb95a90..d14b023bc0f8 100644
@@ -685,12 +685,12 @@ void seq_put_decimal_ull_aligned(struct seq_file *m, const char *delimiter,
 	if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
 		goto overflow;
 
-	len = strlen(delimiter);
-	if (m->count + len >= m->size)
-		goto overflow;
-
-	memcpy(m->buf + m->count, delimiter, len);
-	m->count += len;
+	if (delimiter && delimiter[0]) {
+		if (delimiter[1] == 0)
+			seq_putc(m, delimiter[0]);
+		else
+			seq_puts(m, delimiter);
+	}
 
 	if (!width)
 		width = 1;
@@ -778,12 +778,12 @@ void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num
 	if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
 		goto overflow;
 
-	len = strlen(delimiter);
-	if (m->count + len >= m->size)
-		goto overflow;
-
-	memcpy(m->buf + m->count, delimiter, len);
-	m->count += len;
+	if (delimiter && delimiter[0]) {
+		if (delimiter[1] == 0)
+			seq_putc(m, delimiter[0]);
+		else
+			seq_puts(m, delimiter);
+	}
 
 	if (m->count + 2 >= m->size)
 		goto overflow;
Signature · 1 line
2.13.6
#3 lore raw

PATCH 4/4 proc: replace seq_printf by seq_put_smth to speed up /proc/pid/status

In reply to Andrei Vagin

seq_printf() works slower than seq_puts, seq_puts, etc.

== test_proc.c
int main(int argc, char **argv)

{
	int n, i, fd;
	char buf[16384];

	n = atoi(argv[1]);
	for (i = 0; i < n; i++) {
		fd = open(argv[2], O_RDONLY);
		if (fd < 0)
			return 1;
		if (read(fd, buf, sizeof(buf)) <= 0)
			return 1;
		close(fd);
	}

	return 0;
}

==

$ time ./test_proc  1000000 /proc/1/status

== Before path ==
real 0m5.171s
user 0m0.328s
sys 0m4.783s

== After patch ==
real 0m4.761s
user 0m0.334s
sys 0m4.366s

---
 fs/proc/array.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
fs/proc/array.c +11 -5
index d67a72dcb92c..f22dbfab6ab0 100644
@@ -187,7 +187,8 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
 	task_unlock(p);
 	rcu_read_unlock();
 
-	seq_printf(m, "State:\t%s", get_task_state(p));
+	seq_puts(m, "State:\t");
+	seq_puts(m, get_task_state(p));
 
 	seq_put_decimal_ull(m, "\nTgid:\t", tgid);
 	seq_put_decimal_ull(m, "\nNgid:\t", ngid);
@@ -313,8 +314,8 @@ static void render_cap_t(struct seq_file *m, const char *header,
 
 	seq_puts(m, header);
 	CAP_FOR_EACH_U32(__capi) {
-		seq_printf(m, "%08x",
-			   a->cap[CAP_LAST_U32 - __capi]);
+		seq_put_hex_ll(m, NULL,
+			   a->cap[CAP_LAST_U32 - __capi], 8);
 	}
 	seq_putc(m, '\n');
 }
@@ -368,7 +369,8 @@ static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
 
 static inline void task_core_dumping(struct seq_file *m, struct mm_struct *mm)
 {
-	seq_printf(m, "CoreDumping:\t%d\n", !!mm->core_state);
+	seq_put_decimal_ull(m, "CoreDumping:\t", !!mm->core_state);
+	seq_putc(m, '\n');
 }
 
 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
@@ -504,7 +506,11 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	/* convert nsec -> ticks */
 	start_time = nsec_to_clock_t(task->real_start_time);
 
-	seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
+	seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
+	seq_puts(m, " (");
+	seq_puts(m, tcomm);
+	seq_puts(m, ") ");
+	seq_putc(m, state);
 	seq_put_decimal_ll(m, " ", ppid);
 	seq_put_decimal_ll(m, " ", pgid);
 	seq_put_decimal_ll(m, " ", sid);
Signature · 1 line
2.13.6
#4 lore raw
In reply to Andrei Vagin
Quoted 2 lines · Andrei Vagin

seq_put_decimal_ull_align(m, str, val, width) is equivalent of seq_printf(m, "%s%*d", str, width, val), but it works much faster.

It is called "minimum width", not alignment.

#5 lore raw
Quoted 5 lines · Alexey Dobriyan
Quoted 2 lines · Andrei Vagin

seq_put_decimal_ull_align(m, str, val, width) is equivalent of seq_printf(m, "%s%*d", str, width, val), but it works much faster.

It is called "minimum width", not alignment.

Yes, you are right. And it's used to align numbers in output files.

Maybe you could suggest a better name for this function?

Thanks,
Andrei

#6 lore raw
Quoted 10 lines · Andrei Vagin
Quoted 5 lines · Alexey Dobriyan
Quoted 2 lines · Andrei Vagin

seq_put_decimal_ull_align(m, str, val, width) is equivalent of seq_printf(m, "%s%*d", str, width, val), but it works much faster.

It is called "minimum width", not alignment.

Yes, you are right. And it's used to align numbers in output files.

Maybe you could suggest a better name for this function?

I honestly don't know.
Contemplating printing integers backwards.

maintainer
#7 lore raw
In reply to Andrei Vagin
Quoted 11 lines · On Mon, 29 Jan 2018 13:09:07 -0800 Andrei Vagin <[email protected]> wrote:
Quoted 5 lines · Alexey Dobriyan
Quoted 2 lines · Andrei Vagin

seq_put_decimal_ull_align(m, str, val, width) is equivalent of seq_printf(m, "%s%*d", str, width, val), but it works much faster.

It is called "minimum width", not alignment.

Yes, you are right. And it's used to align numbers in output files.

Maybe you could suggest a better name for this function?

_width? _min_width_? _pad?

#8 lore raw
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Mon, 29 Jan 2018 13:09:07 -0800 Andrei Vagin <[email protected]> wrote:
Quoted 4 lines · Alexey Dobriyan
Quoted 2 lines · Andrei Vagin

seq_put_decimal_ull_align(m, str, val, width) is equivalent of seq_printf(m, "%s%*d", str, width, val), but it works much faster.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

It is called "minimum width", not alignment.

Yes, you are right. And it's used to align numbers in output files.

Maybe you could suggest a better name for this function?

_width? _min_width_? _pad?

I chose the first one. Thank you. The second version of these patches has to be in your mailbox.

Thanks,
Andrei