procfs: add seq_put_hex_ll to speed up /proc/pid/maps
Subsystems (3)
PATCH 1/2 procfs: add seq_put_hex_ll to speed up /proc/pid/maps
seq_put_hex_ll() prints a number in hexadecimal notation and works faster than seq_printf().
== test.py
num = 0
with open("/proc/1/maps") as f:
while num < 10000 :
data = f.read()
f.seek(0, 0)
num = num + 1==
== Before patch ==
$ time python test.py
real 0m1.561s
user 0m0.257s
sys 0m1.302s
== After patch ==
$ time python test.py
real 0m0.986s
user 0m0.279s
sys 0m0.707s
$ perf -g record python test.py:
== Before patch ==
- 67.42% 2.82% python [kernel.kallsyms] [k] show_map_vma.isra.22
- 64.60% show_map_vma.isra.22
- 44.98% seq_printf
- seq_vprintf
- vsnprintf
+ 14.85% number
+ 12.22% format_decode
5.56% memcpy_erms
+ 15.06% seq_path
+ 4.42% seq_pad
+ 2.45% __GI___libc_read
== After patch ==
- 47.35% 3.38% python [kernel.kallsyms] [k] show_map_vma.isra.23
- 43.97% show_map_vma.isra.23
+ 20.84% seq_path
- 15.73% show_vma_header_prefix
10.55% seq_put_hex_ll
+ 2.65% seq_put_decimal_ull
0.95% seq_putc
+ 6.96% seq_pad
+ 2.94% __GI___libc_read
--- fs/proc/task_mmu.c | 21 ++++++++------ fs/seq_file.c | 74 ++++++++++++++++++++++++++++++++++++++++-------- include/linux/seq_file.h | 3 ++ 3 files changed, 77 insertions(+), 21 deletions(-)
fs/proc/task_mmu.c +12 -9
@@ -282,15 +282,18 @@ static void show_vma_header_prefix(struct seq_file *m, dev_t dev, unsigned long ino) { seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); -seq_printf (m,"%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu " , - start, - end, - flags &VM_READ ?'r' :'-' , - flags &VM_WRITE ?'w' :'-' , - flags &VM_EXEC ?'x' :'-' , - flags &VM_MAYSHARE ?'s' :'p' , - pgoff, -MAJOR (dev),MINOR (dev), ino); +seq_put_hex_ll (m,NULL , start,8 ); +seq_put_hex_ll (m,"-" , end,8 ); +seq_putc (m,' ' ); +seq_putc (m, flags &VM_READ ?'r' :'-' ); +seq_putc (m, flags &VM_WRITE ?'w' :'-' ); +seq_putc (m, flags &VM_EXEC ?'x' :'-' ); +seq_putc (m, flags &VM_MAYSHARE ?'s' :'p' ); +seq_put_hex_ll (m," " , pgoff,8 ); +seq_put_hex_ll (m," " ,MAJOR (dev),2 ); +seq_put_hex_ll (m,":" ,MINOR (dev),2 ); +seq_put_decimal_ull (m," " , ino); +seq_putc (m,' ' ); } static void
fs/seq_file.c +62 -12
98 lines not on this page · raw message
include/linux/seq_file.h +3 -0
@@ -121,6 +121,9 @@ void seq_puts(struct seq_file *m, const char *s);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); +void seq_put_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width); +void seq_escape (struct seq_file *m,const char *s,const char *esc); void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
Signature · 1 line
2.13.6
PATCH 2/2 procfs: optimize seq_pad() to speed up /proc/pid/maps
seq_printf() is slow and it can be replaced by memset() in this case.
== test.py
num = 0
with open("/proc/1/maps") as f:
while num < 10000 :
data = f.read()
f.seek(0, 0)
num = num + 1==
== Before patch ==
$ time python test.py
real 0m0.986s
user 0m0.279s
sys 0m0.707s
== After patch ==
$ time python test.py real 0m0.932s user 0m0.261s sys 0m0.669s
$ perf record -g python test.py
== Before patch ==
- 47.35% 3.38% python [kernel.kallsyms] [k] show_map_vma.isra.23
- 43.97% show_map_vma.isra.23
+ 20.84% seq_path
- 15.73% show_vma_header_prefix
+ 6.96% seq_pad
+ 2.94% __GI___libc_read== After patch ==
- 44.01% 0.34% python [kernel.kallsyms] [k] show_pid_map
- 43.67% show_pid_map
- 42.91% show_map_vma.isra.23
+ 21.55% seq_path
- 15.68% show_vma_header_prefix
+ 2.08% seq_pad
0.55% seq_putc
--- fs/seq_file.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
fs/seq_file.c +8 -2
@@ -829,8 +829,14 @@ EXPORT_SYMBOL(seq_write);void seq_pad (struct seq_file *m,char c) {int size = m->pad_until - m->count; -if (size >0 ) -seq_printf (m,"%*s" , size,"" ); +if (size >0 ) { +if (size + m->count > m->size) { +seq_set_overflow (m); +return ; + } +memset (m->buf + m->count,' ' , size); + m->count += size; + }if (c)seq_putc (m, c); }
Signature · 1 line
2.13.6
Andrew Morton
maintainerQuoted 20 lines · On Fri, 12 Jan 2018 10:58:11 -0800 Andrei Vagin <[email protected]> wrote:
seq_put_hex_ll()prints a number in hexadecimal notation and works faster thanseq_printf()....
fs/seq_file.c +9
-0@@ -670,6 +670,26 @@ void seq_puts(struct seq_file *m, const char *s) }EXPORT_SYMBOL (seq_puts); +static inline void seq_put_delimeter (struct seq_file *m,const char *delimiter) +{ +int len; + +if (!delimiter || !delimiter[0 ]) +return ; + +if (delimiter[1 ] ==0 ) +return seq_putc (m, delimiter[0 ]);
fs/seq_file.c void seq_puts(struct seq_file *m, const char *s) · line 691
Quoted 10 lines
fs/seq_file.c +10
-0+ + len =strlen (delimiter); +if (m->count + len >= m->size) { +seq_set_overflow (m); +return ; + } + +memcpy (m->buf + m->count, delimiter, len); + m->count += len; +}
Can we please have a nice comment describing this function's role and behaviour?
I don't think the inline' is needed or desirable - gcc can figure that out, and with three callsites a noinline' would be more justified!
That `return seq_putc(...)' will generate a warning in some situations
- seq_putc() returns void. Let's split it into 'seq_putc(...); return;' please.
Quoted 29 lines
fs/seq_file.c +29
-0+/** +* seq_put_hex_ll - put a number in hexadecimal notation +* @m: seq_file identifying the buffer to which data should be written +* @delimiter: a string which is printed before the number +* @v: the number +* @width: a minimum field width +* +* seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "0x08llx", v) +* +* 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_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width) +{ +int i, len; + +seq_put_delimeter (m, delimiter); + + len = (sizeof (v) *8 -__builtin_clzll (v) +3 ) /4 ; + +if (unlikely (len ==0 )) + len =1 ; + +if (len < width) + len = width; + +if (m->count + len > m->size) + goto overflow;
fs/seq_file.c void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, · line 768
Quoted 10 lines
fs/seq_file.c +10
-0+ +for (i = len -1 ; i >=0 ; i--) { + m->buf[m->count + i] = hex_asc[0xf & v]; + v = v >>4 ; + } + m->count += len; +return ; +overflow: +seq_set_overflow (m); +}
I don't think we need the goto. Just do "seq_set_overflow(m); return;".
Quoted 27 lines · Andrew Morton
Quoted 25 lines · On Fri, 12 Jan 2018 10:58:11 -0800 Andrei Vagin <[email protected]> wrote:
seq_put_hex_ll()prints a number in hexadecimal notation and works faster thanseq_printf()....
fs/seq_file.c +14
-0@@ -670,6 +670,26 @@ void seq_puts(struct seq_file *m, const char *s) }EXPORT_SYMBOL (seq_puts); +static inline void seq_put_delimeter (struct seq_file *m,const char *delimiter) +{ +int len; + +if (!delimiter || !delimiter[0 ]) +return ; + +if (delimiter[1 ] ==0 ) +return seq_putc (m, delimiter[0 ]); + + len =strlen (delimiter); +if (m->count + len >= m->size) { +seq_set_overflow (m); +return ;
Quoted 8 lines
fs/seq_file.c void seq_puts(struct seq_file *m, const char *s) · line 691
Quoted 5 lines
fs/seq_file.c +5
-0+ } + +memcpy (m->buf + m->count, delimiter, len); + m->count += len; +}Can we please have a nice comment describing this function's role and behaviour?
seq_put_decimal_* and seq_put_hex_ll prints a string before printing a number. Originaly it was just one symbol, it is probably a reason why it is called delimeter.
I added an optimization for a case when delimiter is one symbol, and found that it sinificantly affect perfomance (about 13% for /proc/pid/maps):
Without this optimization:
[root@fc24 ~]# time python test.py
real 0m9.105s
user 0m2.200s
sys 0m6.901s
With this optimization:
[root@fc24 ~]# time python test.py
real 0m8.097s
user 0m1.994s
sys 0m6.102s
If inline is replaced by noinline
[root@fc24 ~]# time python test.py
real 0m8.263s
user 0m2.058s
sys 0m6.200s
[root@fc24 ~]# cat test.py
#!/usr/bin/env python2
num = 0
with open("/proc/1/maps") as f:
for x in xrange(100000):
data = f.read()
f.seek(0, 0)Andrew, thank you for the review, I will send a fixed patch soon.
Quoted 50 lines
I don't think the
inline' is needed or desirable - gcc can figure that out, and with three callsites anoinline' would be more justified!That `return
seq_putc(...)' will generate a warning in some situations
-seq_putc()returns void. Let's split it into 'seq_putc(...); return;' please.Quoted 29 lines
fs/seq_file.c +29
-0+/** +* seq_put_hex_ll - put a number in hexadecimal notation +* @m: seq_file identifying the buffer to which data should be written +* @delimiter: a string which is printed before the number +* @v: the number +* @width: a minimum field width +* +* seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "0x08llx", v) +* +* 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_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width) +{ +int i, len; + +seq_put_delimeter (m, delimiter); + + len = (sizeof (v) *8 -__builtin_clzll (v) +3 ) /4 ; + +if (unlikely (len ==0 )) + len =1 ; + +if (len < width) + len = width; + +if (m->count + len > m->size) + goto overflow;fs/seq_file.c void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, · line 768
Quoted 10 lines
fs/seq_file.c +10
-0+ +for (i = len -1 ; i >=0 ; i--) { + m->buf[m->count + i] = hex_asc[0xf & v]; + v = v >>4 ; + } + m->count += len; +return ; +overflow: +seq_set_overflow (m); +}I don't think we need the goto. Just do "
seq_set_overflow(m); return;".
PATCH v2 1/1 procfs: add seq_put_hex_ll to speed up /proc/pid/maps
In reply to Andrew Mortonseq_put_hex_ll() prints a number in hexadecimal notation and works faster than seq_printf().
== test.py
num = 0
with open("/proc/1/maps") as f:
while num < 10000 :
data = f.read()
f.seek(0, 0)
num = num + 1==
== Before patch ==
$ time python test.py
real 0m1.561s
user 0m0.257s
sys 0m1.302s
== After patch ==
$ time python test.py
real 0m0.986s
user 0m0.279s
sys 0m0.707s
$ perf -g record python test.py:
== Before patch ==
- 67.42% 2.82% python [kernel.kallsyms] [k] show_map_vma.isra.22
- 64.60% show_map_vma.isra.22
- 44.98% seq_printf
- seq_vprintf
- vsnprintf
+ 14.85% number
+ 12.22% format_decode
5.56% memcpy_erms
+ 15.06% seq_path
+ 4.42% seq_pad
+ 2.45% __GI___libc_read
== After patch ==
- 47.35% 3.38% python [kernel.kallsyms] [k] show_map_vma.isra.23
- 43.97% show_map_vma.isra.23
+ 20.84% seq_path
- 15.73% show_vma_header_prefix
10.55% seq_put_hex_ll
+ 2.65% seq_put_decimal_ull
0.95% seq_putc
+ 6.96% seq_pad
+ 2.94% __GI___libc_read
v2: don't create a separate function to print delimiter and a few fixes to make code more readable.
--- fs/proc/task_mmu.c | 21 ++++++++++++--------- fs/seq_file.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/seq_file.h | 3 +++ 3 files changed, 60 insertions(+), 9 deletions(-)
fs/proc/task_mmu.c +12 -9
@@ -282,15 +282,18 @@ static void show_vma_header_prefix(struct seq_file *m, dev_t dev, unsigned long ino) { seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); -seq_printf (m,"%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu " , - start, - end, - flags &VM_READ ?'r' :'-' , - flags &VM_WRITE ?'w' :'-' , - flags &VM_EXEC ?'x' :'-' , - flags &VM_MAYSHARE ?'s' :'p' , - pgoff, -MAJOR (dev),MINOR (dev), ino); +seq_put_hex_ll (m,NULL , start,8 ); +seq_put_hex_ll (m,"-" , end,8 ); +seq_putc (m,' ' ); +seq_putc (m, flags &VM_READ ?'r' :'-' ); +seq_putc (m, flags &VM_WRITE ?'w' :'-' ); +seq_putc (m, flags &VM_EXEC ?'x' :'-' ); +seq_putc (m, flags &VM_MAYSHARE ?'s' :'p' ); +seq_put_hex_ll (m," " , pgoff,8 ); +seq_put_hex_ll (m," " ,MAJOR (dev),2 ); +seq_put_hex_ll (m,":" ,MINOR (dev),2 ); +seq_put_decimal_ull (m," " , ino); +seq_putc (m,' ' ); } static void
fs/seq_file.c +45 -0
51 lines not on this page · raw message
include/linux/seq_file.h +3 -0
@@ -121,6 +121,9 @@ void seq_puts(struct seq_file *m, const char *s);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); +void seq_put_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width); +void seq_escape (struct seq_file *m,const char *s,const char *esc); void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
Signature · 1 line
2.13.6
Quoted 1 line · Andrei Vagin
+ *
seq_put_hex_ll(m, "", v, 8)is equal toseq_printf(m, "0x08llx", v)
No, it is not. Scratch "0x".
fs/seq_file.c void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, · line 728
Quoted 2 lines
fs/seq_file.c +2
-0+void seq_put_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width)
I understand that "unsigned long long" mimics decimal counterpart, but in thie case everything is "unsigned long" including ->vm_pgoff.
Also, width should be unsigned for the common case of %08lx (and "len" too)
Quoted 30 lines
fs/seq_file.c +30
-0+{ +int i, len; + +if (delimiter && delimiter[0 ]) { +if (delimiter[1 ] ==0 ) +seq_putc (m, delimiter[0 ]); +else +seq_puts (m, delimiter); + } + +/* If x is 0, the result of __builtin_clzll is undefined */ +if (v ==0 ) + len =1 ; +else + len = (sizeof (v) *8 -__builtin_clzll (v) +3 ) /4 ; + +if (len < width) + len = width; + +if (m->count + len > m->size) { +seq_set_overflow (m); +return ; + } + +for (i = len -1 ; i >=0 ; i--) { + m->buf[m->count + i] = hex_asc[0xf & v]; + v = v >>4 ; + } + m->count += len; +}
Quoted 4 lines · Alexey Dobriyan
Quoted 1 line · Andrei Vagin
+ *
seq_put_hex_ll(m, "", v, 8)is equal toseq_printf(m, "0x08llx", v)No, it is not. Scratch "0x".
will fix
Quoted 6 lines
fs/seq_file.c void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, · line 728
Quoted 3 lines
fs/seq_file.c +2
-0+void seq_put_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width)I understand that "unsigned long long" mimics decimal counterpart, but in thie case everything is "unsigned long" including ->vm_pgoff.
I've checked that the performance is the same for "unsigned long long" and "unsigned long". Do we have other reasons to use "unsigned long" rather than "unsigned long long"?
Quoted 3 lines
Also, width should be unsigned for the common case of %08lx (and "len" too)
will fix
Thank you for the review.
Quoted 4 lines
Quoted 4 lines
fs/seq_file.c +3
-0+{ +int i, len; +
Quoted 14 lines · Andrei Vagin
Quoted 4 lines · Alexey Dobriyan
Quoted 1 line · Andrei Vagin
+ *
seq_put_hex_ll(m, "", v, 8)is equal toseq_printf(m, "0x08llx", v)No, it is not. Scratch "0x".
will fix
Quoted 6 lines
fs/seq_file.c void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, · line 728
Quoted 3 lines
fs/seq_file.c +2
-0+void seq_put_hex_ll (struct seq_file *m,const char *delimiter, +unsigned long long v,int width)I understand that "unsigned long long" mimics decimal counterpart, but in thie case everything is "unsigned long" including ->vm_pgoff.
Quoted 3 lines
I've checked that the performance is the same for "unsigned long long" and "unsigned long". Do we have other reasons to use "unsigned long" rather than "unsigned long long"?
On 32-bit those types are different.
But I guess as seq_put_decimal is ull, it is worse to deviate.