Skip to contentSkip to conversation

fs/proc/array.c: slightly improve render_sigset_t

PATCH Applied Rasmus Villemoes 4 participants 6 messages Sep 20, 2016 – Nov 9, 2016 Proc filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/proc/array.c: slightly improve render_sigset_t

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);
	}
---
 fs/proc/array.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/array.c +1 -1
index 88c7de12197b..7f73b689a15c 100644
@@ -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');
Signature · 1 line
2.1.4
Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · 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);
        }
Quoted 1 line

Heheh. Nice. :)

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

-Kees

Quoted 20 lines
---
 fs/proc/array.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/array.c +1 -2
index 88c7de12197b..7f73b689a15c 100644
@@ -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

Signature · 2 lines
Kees Cook
Nexus Security
#2 lore raw
Quoted 20 lines · Kees Cook
Quoted 18 lines · 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);
        }
Quoted 8 lines
Quoted 1 line

Heheh. Nice. :)

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

Acked-by on v1 1/1
#5 lore raw
In reply to Rasmus Villemoes
Quoted 10 lines · 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:

Quoted 8 lines
        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);
        }

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

Thanks,
Andrei

Quoted 21 lines
---
 fs/proc/array.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/array.c +1 -2
index 88c7de12197b..7f73b689a15c 100644
@@ -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