Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

libsymbols kallsyms: Parse using io api

'perf record' will call kallsyms__parse 4 times during startup and
process megabytes of data. This changes kallsyms__parse to use the io
library rather than fgets to improve performance of the user code by
over 8%.

Before:

Running 'internals/kallsyms-parse' benchmark:
Average kallsyms__parse took: 103.988 ms (+- 0.203 ms)

After:

Running 'internals/kallsyms-parse' benchmark:
Average kallsyms__parse took: 95.571 ms (+- 0.006 ms)

For a workload like:

$ perf record /bin/true
Run under 'perf record -e cycles:u -g' the time goes from:
Before
30.10% 1.67% perf perf [.] kallsyms__parse
After
25.55% 20.04% perf perf [.] kallsyms__parse

So a little under 5% of the start-up time is removed. A lot of what
remains is on the kernel side, but caching kallsyms within perf would at
least impact memory footprint.

Committer notes:

The internal/kallsyms-parse bench is run using:

[root@five ~]# perf bench internals kallsyms-parse
# Running 'internals/kallsyms-parse' benchmark:
Average kallsyms__parse took: 80.381 ms (+- 0.115 ms)
[root@five ~]#

And this pre-existing test uses these routines to parse kallsyms and
then compare with the info obtained from the matching ELF symtab:

[root@five ~]# perf test vmlinux
1: vmlinux symtab matches kallsyms : Ok
[root@five ~]#

Also we can't remove hex2u64() in this patch as this breaks the build:

/usr/bin/ld: /tmp/build/perf/perf-in.o: in function `modules__parse':
/home/acme/git/perf/tools/perf/util/symbol.c:607: undefined reference to `hex2u64'
/usr/bin/ld: /home/acme/git/perf/tools/perf/util/symbol.c:607: undefined reference to `hex2u64'
/usr/bin/ld: /tmp/build/perf/perf-in.o: in function `dso__load_perf_map':
/home/acme/git/perf/tools/perf/util/symbol.c:1477: undefined reference to `hex2u64'
/usr/bin/ld: /home/acme/git/perf/tools/perf/util/symbol.c:1483: undefined reference to `hex2u64'
collect2: error: ld returned 1 exit status

Leave it there, move it in the next patch.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lore.kernel.org/lkml/20200501221315.54715-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
53df2b93 51876bd4

+52 -46
+3
tools/lib/api/io.h
··· 7 7 #ifndef __API_IO__ 8 8 #define __API_IO__ 9 9 10 + #include <stdlib.h> 11 + #include <unistd.h> 12 + 10 13 struct io { 11 14 /* File descriptor being read/ */ 12 15 int fd;
+49 -46
tools/lib/symbol/kallsyms.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include "symbol/kallsyms.h" 3 + #include "api/io.h" 3 4 #include <stdio.h> 4 - #include <stdlib.h> 5 + #include <sys/stat.h> 6 + #include <fcntl.h> 5 7 6 8 u8 kallsyms2elf_type(char type) 7 9 { 8 10 type = tolower(type); 9 11 return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT; 10 - } 11 - 12 - bool kallsyms__is_function(char symbol_type) 13 - { 14 - symbol_type = toupper(symbol_type); 15 - return symbol_type == 'T' || symbol_type == 'W'; 16 12 } 17 13 18 14 /* ··· 24 28 return p - ptr; 25 29 } 26 30 31 + bool kallsyms__is_function(char symbol_type) 32 + { 33 + symbol_type = toupper(symbol_type); 34 + return symbol_type == 'T' || symbol_type == 'W'; 35 + } 36 + 37 + static void read_to_eol(struct io *io) 38 + { 39 + int ch; 40 + 41 + for (;;) { 42 + ch = io__get_char(io); 43 + if (ch < 0 || ch == '\n') 44 + return; 45 + } 46 + } 47 + 27 48 int kallsyms__parse(const char *filename, void *arg, 28 49 int (*process_symbol)(void *arg, const char *name, 29 50 char type, u64 start)) 30 51 { 31 - char *line = NULL; 32 - size_t n; 33 - int err = -1; 34 - FILE *file = fopen(filename, "r"); 52 + struct io io; 53 + char bf[BUFSIZ]; 54 + int err; 35 55 36 - if (file == NULL) 37 - goto out_failure; 56 + io.fd = open(filename, O_RDONLY, 0); 57 + 58 + if (io.fd < 0) 59 + return -1; 60 + 61 + io__init(&io, io.fd, bf, sizeof(bf)); 38 62 39 63 err = 0; 40 - 41 - while (!feof(file)) { 42 - u64 start; 43 - int line_len, len; 64 + while (!io.eof) { 65 + __u64 start; 66 + int ch; 67 + size_t i; 44 68 char symbol_type; 45 - char *symbol_name; 69 + char symbol_name[KSYM_NAME_LEN + 1]; 46 70 47 - line_len = getline(&line, &n, file); 48 - if (line_len < 0 || !line) 49 - break; 50 - 51 - line[--line_len] = '\0'; /* \n */ 52 - 53 - len = hex2u64(line, &start); 54 - 55 - /* Skip the line if we failed to parse the address. */ 56 - if (!len) 71 + if (io__get_hex(&io, &start) != ' ') { 72 + read_to_eol(&io); 57 73 continue; 58 - 59 - len++; 60 - if (len + 2 >= line_len) 61 - continue; 62 - 63 - symbol_type = line[len]; 64 - len += 2; 65 - symbol_name = line + len; 66 - len = line_len - len; 67 - 68 - if (len >= KSYM_NAME_LEN) { 69 - err = -1; 70 - break; 71 74 } 75 + symbol_type = io__get_char(&io); 76 + if (io__get_char(&io) != ' ') { 77 + read_to_eol(&io); 78 + continue; 79 + } 80 + for (i = 0; i < sizeof(symbol_name); i++) { 81 + ch = io__get_char(&io); 82 + if (ch < 0 || ch == '\n') 83 + break; 84 + symbol_name[i] = ch; 85 + } 86 + symbol_name[i] = '\0'; 72 87 73 88 err = process_symbol(arg, symbol_name, symbol_type, start); 74 89 if (err) 75 90 break; 76 91 } 77 92 78 - free(line); 79 - fclose(file); 93 + close(io.fd); 80 94 return err; 81 - 82 - out_failure: 83 - return -1; 84 95 }