proc: add seq_put_decimal_ull_width to speed up /proc/pid/smaps
seq_put_decimal_ull_w(m, str, val, width) prints a decimal number with a specified minimal field width. It 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_w + 19.78% __walk_page_range + 12.74% seq_printf + 11.08% show_map_vma.isra.23 + 1.68% seq_puts [akpm@linux-foundation.org: fix drivers/of/unittest.c build] Link: http://lkml.kernel.org/r/20180212074931.7227-1-avagin@openvz.org Signed-off-by: Andrei Vagin <avagin@openvz.org> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:

committed by
Linus Torvalds

parent
2acddbe816
commit
d1be35cb6f
@@ -336,7 +336,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, unsigned int width)
|
||||
{
|
||||
/* put_dec requires 2-byte alignment of the buffer. */
|
||||
char tmp[sizeof(num) * 3] __aligned(2);
|
||||
@@ -350,11 +350,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 */
|
||||
|
Reference in New Issue
Block a user