public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fs/proc/array.c: slightly improve render_sigset_t
@ 2016-09-20 22:28 Rasmus Villemoes
  2016-09-21  0:16 ` Kees Cook
  2016-11-09 21:40 ` Andrei Vagin
  0 siblings, 2 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2016-09-20 22:28 UTC (permalink / raw)
  To: Andrew Morton, Kees Cook; +Cc: linux-kernel, Rasmus Villemoes

format_decode and vsnprintf occasionally show up in perf top, so I
went looking for places that might not need the full printf
power. With the help of kprobes, I gathered some statistics on which
format strings we mostly pass to vsnprintf. On a trivial desktop
workload, I hit "%x" 25% of the time, so something apparently reads
/proc/pid/status (which does 5*16 printf("%x") calls) a lot.

With this patch, reading /proc/pid/status is 30% faster according to
this microbenchmark:

	char buf[4096];
	int i, fd;
	for (i = 0; i < 10000; ++i) {
		fd = open("/proc/self/status", O_RDONLY);
		read(fd, buf, sizeof(buf));
		close(fd);
	}

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 fs/proc/array.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/proc/array.c b/fs/proc/array.c
index 88c7de12197b..7f73b689a15c 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -251,7 +251,7 @@ void render_sigset_t(struct seq_file *m, const char *header,
 		if (sigismember(set, i+2)) x |= 2;
 		if (sigismember(set, i+3)) x |= 4;
 		if (sigismember(set, i+4)) x |= 8;
-		seq_printf(m, "%x", x);
+		seq_putc(m, hex_asc[x]);
 	} while (i >= 4);
 
 	seq_putc(m, '\n');
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs/proc/array.c: slightly improve render_sigset_t
  2016-09-20 22:28 [PATCH] fs/proc/array.c: slightly improve render_sigset_t Rasmus Villemoes
@ 2016-09-21  0:16 ` Kees Cook
  2016-09-21 20:51   ` Rasmus Villemoes
  2016-10-05 22:48   ` Rasmus Villemoes
  2016-11-09 21:40 ` Andrei Vagin
  1 sibling, 2 replies; 6+ messages in thread
From: Kees Cook @ 2016-09-21  0:16 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: Andrew Morton, LKML

On Tue, Sep 20, 2016 at 3:28 PM, Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
> format_decode and vsnprintf occasionally show up in perf top, so I
> went looking for places that might not need the full printf
> power. With the help of kprobes, I gathered some statistics on which
> format strings we mostly pass to vsnprintf. On a trivial desktop
> workload, I hit "%x" 25% of the time, so something apparently reads
> /proc/pid/status (which does 5*16 printf("%x") calls) a lot.
>
> With this patch, reading /proc/pid/status is 30% faster according to
> this microbenchmark:
>
>         char buf[4096];
>         int i, fd;
>         for (i = 0; i < 10000; ++i) {
>                 fd = open("/proc/self/status", O_RDONLY);
>                 read(fd, buf, sizeof(buf));
>                 close(fd);
>         }
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

Heheh. Nice. :)

Acked-by: Kees Cook <keescook@chromium.org>

Out of curiosity, what other stuff ended up near the top? I'd be
curious to see your kprobes too.

-Kees

> ---
>  fs/proc/array.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/proc/array.c b/fs/proc/array.c
> index 88c7de12197b..7f73b689a15c 100644
> --- a/fs/proc/array.c
> +++ b/fs/proc/array.c
> @@ -251,7 +251,7 @@ void render_sigset_t(struct seq_file *m, const char *header,
>                 if (sigismember(set, i+2)) x |= 2;
>                 if (sigismember(set, i+3)) x |= 4;
>                 if (sigismember(set, i+4)) x |= 8;
> -               seq_printf(m, "%x", x);
> +               seq_putc(m, hex_asc[x]);
>         } while (i >= 4);
>
>         seq_putc(m, '\n');
> --
> 2.1.4
>



-- 
Kees Cook
Nexus Security

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs/proc/array.c: slightly improve render_sigset_t
  2016-09-21  0:16 ` Kees Cook
@ 2016-09-21 20:51   ` Rasmus Villemoes
  2016-10-05 22:48   ` Rasmus Villemoes
  1 sibling, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2016-09-21 20:51 UTC (permalink / raw)
  To: Kees Cook; +Cc: Andrew Morton, LKML

On Wed, Sep 21 2016, Kees Cook <keescook@chromium.org> wrote:

> On Tue, Sep 20, 2016 at 3:28 PM, Rasmus Villemoes
> <linux@rasmusvillemoes.dk> wrote:
>> format_decode and vsnprintf occasionally show up in perf top, so I
>> went looking for places that might not need the full printf
>> power. With the help of kprobes, I gathered some statistics on which
>> format strings we mostly pass to vsnprintf. On a trivial desktop
>> workload, I hit "%x" 25% of the time, so something apparently reads
>> /proc/pid/status (which does 5*16 printf("%x") calls) a lot.
>>
>> With this patch, reading /proc/pid/status is 30% faster according to
>> this microbenchmark:
>>
>>         char buf[4096];
>>         int i, fd;
>>         for (i = 0; i < 10000; ++i) {
>>                 fd = open("/proc/self/status", O_RDONLY);
>>                 read(fd, buf, sizeof(buf));
>>                 close(fd);
>>         }
>>
>> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
>
> Heheh. Nice. :)
>
> Acked-by: Kees Cook <keescook@chromium.org>
>
> Out of curiosity, what other stuff ended up near the top? I'd be
> curious to see your kprobes too.

I don't have the results from the other machine handy, but it very much
depends on what one is doing. I ran a 'find $HOME ...', and "%.2x" ended
up accounting for 99%. Turns out ecryptfs does a lot of bin-to-hex
conversions, but very inefficiently (calling sprintf("%.2x") in a
loop...). Patch sent.

Other than that, the top consists of stuff like

  "%c%c " (/proc/pid/smaps, VmFlags:)
  " %s" (/proc/cpuinfo, the flags: line)
  "%*s" (maybe from seq_pad, but not sure who the user of that is)

and a lot of other short strings which are rather hard to trace to their
source, but presumably most are related to some /proc file.

The kprobe is just

  echo 'p:vsnprintf vsnprintf fmt=+0(%dx):string' > /sys/kernel/debug/tracing/kprobe_events

This doesn't escape the strings, so embedded newlines mess up the trace
buffer slightly. I used this little piece of line noise to extract the
format strings.

  use File::Slurp;
  my $txt = read_file($ARGV[0]);
  while ($txt =~ m/fmt="(.*?)"\n/mg) {
      $_ = $1;
      s/\\/\\\\/g; s/\n/\\n/g;
      s/\t/\\t/g;  s/"/\\"/g;
      print "\"$_\"\n";
  }

Rasmus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs/proc/array.c: slightly improve render_sigset_t
  2016-09-21  0:16 ` Kees Cook
  2016-09-21 20:51   ` Rasmus Villemoes
@ 2016-10-05 22:48   ` Rasmus Villemoes
  2016-10-06 22:35     ` Andrew Morton
  1 sibling, 1 reply; 6+ messages in thread
From: Rasmus Villemoes @ 2016-10-05 22:48 UTC (permalink / raw)
  To: Kees Cook; +Cc: Andrew Morton, LKML

On Wed, Sep 21 2016, Kees Cook <keescook@chromium.org> wrote:

> On Tue, Sep 20, 2016 at 3:28 PM, Rasmus Villemoes
> <linux@rasmusvillemoes.dk> wrote:
>
> Heheh. Nice. :)
>
> Acked-by: Kees Cook <keescook@chromium.org>
>

Andrew, ping?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs/proc/array.c: slightly improve render_sigset_t
  2016-10-05 22:48   ` Rasmus Villemoes
@ 2016-10-06 22:35     ` Andrew Morton
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2016-10-06 22:35 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: Kees Cook, LKML

On Thu, 06 Oct 2016 00:48:59 +0200 Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote:

> On Wed, Sep 21 2016, Kees Cook <keescook@chromium.org> wrote:
> 
> > On Tue, Sep 20, 2016 at 3:28 PM, Rasmus Villemoes
> > <linux@rasmusvillemoes.dk> wrote:
> >
> > Heheh. Nice. :)
> >
> > Acked-by: Kees Cook <keescook@chromium.org>
> >
> 
> Andrew, ping?

Yup, I have this queued for processing after -rc1.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs/proc/array.c: slightly improve render_sigset_t
  2016-09-20 22:28 [PATCH] fs/proc/array.c: slightly improve render_sigset_t Rasmus Villemoes
  2016-09-21  0:16 ` Kees Cook
@ 2016-11-09 21:40 ` Andrei Vagin
  1 sibling, 0 replies; 6+ messages in thread
From: Andrei Vagin @ 2016-11-09 21:40 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: Andrew Morton, Kees Cook, LKML

On Tue, Sep 20, 2016 at 3:28 PM, Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
> format_decode and vsnprintf occasionally show up in perf top, so I
> went looking for places that might not need the full printf
> power. With the help of kprobes, I gathered some statistics on which
> format strings we mostly pass to vsnprintf. On a trivial desktop
> workload, I hit "%x" 25% of the time, so something apparently reads
> /proc/pid/status (which does 5*16 printf("%x") calls) a lot.
>
> With this patch, reading /proc/pid/status is 30% faster according to
> this microbenchmark:
>
>         char buf[4096];
>         int i, fd;
>         for (i = 0; i < 10000; ++i) {
>                 fd = open("/proc/self/status", O_RDONLY);
>                 read(fd, buf, sizeof(buf));
>                 close(fd);
>         }
>

Acked-by: Andrei Vagin <avagin@openvz.org>

Andrew, could you replace my patch
"proc-optimize-render_sigset_t.patch" to this one.

Thanks,
Andrei

> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  fs/proc/array.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/proc/array.c b/fs/proc/array.c
> index 88c7de12197b..7f73b689a15c 100644
> --- a/fs/proc/array.c
> +++ b/fs/proc/array.c
> @@ -251,7 +251,7 @@ void render_sigset_t(struct seq_file *m, const char *header,
>                 if (sigismember(set, i+2)) x |= 2;
>                 if (sigismember(set, i+3)) x |= 4;
>                 if (sigismember(set, i+4)) x |= 8;
> -               seq_printf(m, "%x", x);
> +               seq_putc(m, hex_asc[x]);
>         } while (i >= 4);
>
>         seq_putc(m, '\n');
> --
> 2.1.4
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-11-09 21:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-20 22:28 [PATCH] fs/proc/array.c: slightly improve render_sigset_t Rasmus Villemoes
2016-09-21  0:16 ` Kees Cook
2016-09-21 20:51   ` Rasmus Villemoes
2016-10-05 22:48   ` Rasmus Villemoes
2016-10-06 22:35     ` Andrew Morton
2016-11-09 21:40 ` Andrei Vagin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox