From 597bdeb4ab7396c43935eded15f82e3e100b3ff3 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Tue, 3 Nov 2015 10:44:42 +0000 Subject: perf test: Keep test result clean if '-v' not set According to [1], 'perf test' should avoid output too much information if '-v' is not set, only 'Ok', 'FAIL' or 'Skip' need to be printed. This patch removes several messages sent directly to stderr to make the output clean. Before this patch: # perf test dummy 23: Test using a dummy software event to keep tracking : (not supported) Ok After this patch: # perf test dummy 23: Test using a dummy software event to keep tracking : Skip [1] http://lkml.kernel.org/r/20151020134155.GE4400@redhat.com Signed-off-by: Wang Nan Acked-by: Namhyung Kim Tested-by: Arnaldo Carvalho de Melo Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446547486-229499-2-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/attr.c | 3 +-- tools/perf/tests/code-reading.c | 8 ++++---- tools/perf/tests/keep-tracking.c | 4 ++-- tools/perf/tests/llvm.c | 11 ++++------- tools/perf/tests/switch-tracking.c | 4 ++-- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/tools/perf/tests/attr.c b/tools/perf/tests/attr.c index 2dfc9ad0e6f2..638875a0960a 100644 --- a/tools/perf/tests/attr.c +++ b/tools/perf/tests/attr.c @@ -171,6 +171,5 @@ int test__attr(void) !lstat(path_perf, &st)) return run_dir(path_dir, path_perf); - fprintf(stderr, " (omitted)"); - return 0; + return TEST_SKIP; } diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c index 49b1959dda41..a767a6400c5c 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -613,16 +613,16 @@ int test__code_reading(void) case TEST_CODE_READING_OK: return 0; case TEST_CODE_READING_NO_VMLINUX: - fprintf(stderr, " (no vmlinux)"); + pr_debug("no vmlinux\n"); return 0; case TEST_CODE_READING_NO_KCORE: - fprintf(stderr, " (no kcore)"); + pr_debug("no kcore\n"); return 0; case TEST_CODE_READING_NO_ACCESS: - fprintf(stderr, " (no access)"); + pr_debug("no access\n"); return 0; case TEST_CODE_READING_NO_KERNEL_OBJ: - fprintf(stderr, " (no kernel obj)"); + pr_debug("no kernel obj\n"); return 0; default: return -1; diff --git a/tools/perf/tests/keep-tracking.c b/tools/perf/tests/keep-tracking.c index 4d4b9837b630..a2e2269aa093 100644 --- a/tools/perf/tests/keep-tracking.c +++ b/tools/perf/tests/keep-tracking.c @@ -90,8 +90,8 @@ int test__keep_tracking(void) evsel->attr.enable_on_exec = 0; if (perf_evlist__open(evlist) < 0) { - fprintf(stderr, " (not supported)"); - err = 0; + pr_debug("Unable to open dummy and cycles event\n"); + err = TEST_SKIP; goto out_err; } diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c index 52d55971f66f..512d3620e9f9 100644 --- a/tools/perf/tests/llvm.c +++ b/tools/perf/tests/llvm.c @@ -36,7 +36,7 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz) static int test__bpf_parsing(void *obj_buf __maybe_unused, size_t obj_buf_sz __maybe_unused) { - fprintf(stderr, " (skip bpf parsing)"); + pr_debug("Skip bpf parsing\n"); return 0; } #endif @@ -55,7 +55,7 @@ int test__llvm(void) * and clang is not found in $PATH, and this is not perf test -v */ if (verbose == 0 && !llvm_param.user_set_param && llvm__search_clang()) { - fprintf(stderr, " (no clang, try 'perf test -v LLVM')"); + pr_debug("No clang and no verbosive, skip this test\n"); return TEST_SKIP; } @@ -86,11 +86,8 @@ int test__llvm(void) err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz); verbose = old_verbose; - if (err) { - if (!verbose) - fprintf(stderr, " (use -v to see error message)"); - return -1; - } + if (err) + return TEST_FAIL; err = test__bpf_parsing(obj_buf, obj_buf_sz); free(obj_buf); diff --git a/tools/perf/tests/switch-tracking.c b/tools/perf/tests/switch-tracking.c index e698742d4fec..a02af503100c 100644 --- a/tools/perf/tests/switch-tracking.c +++ b/tools/perf/tests/switch-tracking.c @@ -366,7 +366,7 @@ int test__switch_tracking(void) /* Third event */ if (!perf_evlist__can_select_event(evlist, sched_switch)) { - fprintf(stderr, " (no sched_switch)"); + pr_debug("No sched_switch\n"); err = 0; goto out; } @@ -442,7 +442,7 @@ int test__switch_tracking(void) } if (perf_evlist__open(evlist) < 0) { - fprintf(stderr, " (not supported)"); + pr_debug("Not supported\n"); err = 0; goto out; } -- cgit v1.2.3 From 7a0119468c9c2deff24ef24e1b4d2c1bd1523fd5 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Tue, 3 Nov 2015 10:44:43 +0000 Subject: perf bpf: Mute libbpf when '-v' not set According to [1], libbpf should be muted. This patch reset info and warning message level to ensure libbpf doesn't output anything even if error happened. [1] http://lkml.kernel.org/r/20151020151255.GF5119@kernel.org Committer note: Before: Testing it with an incompatible kernel version in the .c file that generated foo.o: [root@zoo ~]# perf record -e /tmp/foo.o sleep 1 libbpf: load bpf program failed: Invalid argument libbpf: -- BEGIN DUMP LOG --- libbpf: libbpf: -- END LOG -- libbpf: failed to load program 'fork=_do_fork' libbpf: failed to load object '/tmp/foo.o' event syntax error: '/tmp/foo.o' \___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel? (add -v to see detail) Run 'perf list' for a list of valid events Usage: perf record [] [] or: perf record [] -- [] -e, --event event selector. use 'perf list' to list available events [root@zoo ~]# After: [root@zoo ~]# perf record -e /tmp/foo.o sleep 1 event syntax error: '/tmp/foo.o' \___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel? (add -v to see detail) Run 'perf list' for a list of valid events Usage: perf record [] [] or: perf record [] -- [] -e, --event event selector. use 'perf list' to list available events [root@zoo ~]# This, BTW, need fixing to emit a proper message by validating the version in the foo.o "version" ELF section against the running kernel, warning the user instead of asking the kernel to load a binary that it will refuse due to unmatching kernel version. Signed-off-by: Wang Nan Tested-by: Arnaldo Carvalho de Melo Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446547486-229499-3-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/bpf-loader.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index ba6f7526b282..0c5d174245bb 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -26,8 +26,8 @@ static int libbpf_##name(const char *fmt, ...) \ return ret; \ } -DEFINE_PRINT_FN(warning, 0) -DEFINE_PRINT_FN(info, 0) +DEFINE_PRINT_FN(warning, 1) +DEFINE_PRINT_FN(info, 1) DEFINE_PRINT_FN(debug, 1) struct bpf_prog_priv { -- cgit v1.2.3 From 715f8db9102f1ab40ea4a87bedfe86399323698f Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Tue, 3 Nov 2015 20:21:05 +0900 Subject: tools lib bpf: Fix compiler warning on CentOS 6 CC libbpf.o cc1: warnings being treated as errors libbpf.c: In function 'bpf_program__title': libbpf.c:1037: error: declaration of 'dup' shadows a global declaration /usr/include/unistd.h:528: error: shadowed declaration is here mv: cannot stat `./.libbpf.o.tmp': No such file or directory make[3]: *** [libbpf.o] Error 1 make[2]: *** [libbpf-in.o] Error 2 make[1]: *** [/linux/tools/lib/bpf/libbpf.a] Error 2 make[1]: *** Waiting for unfinished jobs.... Signed-off-by: Namhyung Kim Cc: David Ahern Cc: Jiri Olsa Cc: Peter Zijlstra Cc: Wang Nan Link: http://lkml.kernel.org/r/1446549665-2342-1-git-send-email-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 4252fc22f78f..9f3c8cf9249b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1034,12 +1034,12 @@ int bpf_program__get_private(struct bpf_program *prog, void **ppriv) return 0; } -const char *bpf_program__title(struct bpf_program *prog, bool dup) +const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) { const char *title; title = prog->section_name; - if (dup) { + if (needs_copy) { title = strdup(title); if (!title) { pr_warning("failed to strdup program title\n"); diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index f16170c95ffd..bc80af03c6f4 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -63,7 +63,7 @@ int bpf_program__set_private(struct bpf_program *prog, void *priv, int bpf_program__get_private(struct bpf_program *prog, void **ppriv); -const char *bpf_program__title(struct bpf_program *prog, bool dup); +const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); int bpf_program__fd(struct bpf_program *prog); -- cgit v1.2.3 From 4579ecc8b3e0c611b5c8f6ca7f7b07d1412d8a7b Mon Sep 17 00:00:00 2001 From: Andi Kleen Date: Mon, 2 Nov 2015 17:50:20 -0800 Subject: perf stat: Move sw clock metrics printout to stat-shadow The sw clock metrics printing was missed in the earlier move to stat-shadow of all the other metric printouts. Move it too. v2: Fix metrics printing in this version to make bisect safe. Signed-off-by: Andi Kleen Acked-by: Jiri Olsa Link: http://lkml.kernel.org/r/1446515428-7450-2-git-send-email-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 8 +++----- tools/perf/util/stat-shadow.c | 5 +++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 2f438f76cceb..8a788956b6a3 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -493,6 +493,7 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) double msecs = avg / 1e6; const char *fmt_v, *fmt_n; char name[25]; + int cpu = cpu_map__id_to_cpu(id); fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; fmt_n = csv_output ? "%s" : "%-25s"; @@ -517,11 +518,8 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) if (csv_output || stat_config.interval) return; - if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) - fprintf(output, " # %8.3f CPUs utilized ", - avg / avg_stats(&walltime_nsecs_stats)); - else - fprintf(output, " "); + perf_stat__print_shadow_stats(output, evsel, avg, cpu, + stat_config.aggr_mode); } static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c index 2a5d8d7698ae..6ac03146889d 100644 --- a/tools/perf/util/stat-shadow.c +++ b/tools/perf/util/stat-shadow.c @@ -413,6 +413,11 @@ void perf_stat__print_shadow_stats(FILE *out, struct perf_evsel *evsel, ratio = total / avg; fprintf(out, " # %8.0f cycles / elision ", ratio); + } else if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) { + if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0) + fprintf(out, " # %8.3f CPUs utilized ", avg / ratio); + else + fprintf(out, " "); } else if (runtime_nsecs_stats[cpu].n != 0) { char unit = 'M'; -- cgit v1.2.3 From eedfcb4bc5f0185a14fc9e1e5a9770ad833ea5a4 Mon Sep 17 00:00:00 2001 From: Andi Kleen Date: Mon, 2 Nov 2015 17:50:21 -0800 Subject: perf stat: Use common printout function to avoid duplicated code Instead of every caller deciding whether to call abs or nsec printout do it all in a single central function. No functional changes. Signed-off-by: Andi Kleen Acked-by: Jiri Olsa Link: http://lkml.kernel.org/r/1446515428-7450-3-git-send-email-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 57 +++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 8a788956b6a3..b74ee0f2e714 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -493,7 +493,6 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) double msecs = avg / 1e6; const char *fmt_v, *fmt_n; char name[25]; - int cpu = cpu_map__id_to_cpu(id); fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; fmt_n = csv_output ? "%s" : "%-25s"; @@ -514,12 +513,6 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) if (evsel->cgrp) fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); - - if (csv_output || stat_config.interval) - return; - - perf_stat__print_shadow_stats(output, evsel, avg, cpu, - stat_config.aggr_mode); } static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) @@ -527,7 +520,6 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) FILE *output = stat_config.output; double sc = evsel->scale; const char *fmt; - int cpu = cpu_map__id_to_cpu(id); if (csv_output) { fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s"; @@ -540,9 +532,6 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) aggr_printout(evsel, id, nr); - if (stat_config.aggr_mode == AGGR_GLOBAL) - cpu = 0; - fprintf(output, fmt, avg, csv_sep); if (evsel->unit) @@ -554,12 +543,24 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) if (evsel->cgrp) fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); +} - if (csv_output || stat_config.interval) - return; +static void printout(int id, int nr, struct perf_evsel *counter, double uval) +{ + int cpu = cpu_map__id_to_cpu(id); + + if (stat_config.aggr_mode == AGGR_GLOBAL) + cpu = 0; + + if (nsec_counter(counter)) + nsec_printout(id, nr, counter, uval); + else + abs_printout(id, nr, counter, uval); - perf_stat__print_shadow_stats(output, evsel, avg, cpu, - stat_config.aggr_mode); + if (!csv_output && !stat_config.interval) + perf_stat__print_shadow_stats(stat_config.output, counter, + uval, cpu, + stat_config.aggr_mode); } static void print_aggr(char *prefix) @@ -615,12 +616,7 @@ static void print_aggr(char *prefix) continue; } uval = val * counter->scale; - - if (nsec_counter(counter)) - nsec_printout(id, nr, counter, uval); - else - abs_printout(id, nr, counter, uval); - + printout(id, nr, counter, uval); if (!csv_output) print_noise(counter, 1.0); @@ -651,11 +647,7 @@ static void print_aggr_thread(struct perf_evsel *counter, char *prefix) fprintf(output, "%s", prefix); uval = val * counter->scale; - - if (nsec_counter(counter)) - nsec_printout(thread, 0, counter, uval); - else - abs_printout(thread, 0, counter, uval); + printout(thread, 0, counter, uval); if (!csv_output) print_noise(counter, 1.0); @@ -705,11 +697,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix) } uval = avg * counter->scale; - - if (nsec_counter(counter)) - nsec_printout(-1, 0, counter, uval); - else - abs_printout(-1, 0, counter, uval); + printout(-1, 0, counter, uval); print_noise(counter, avg); @@ -762,12 +750,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix) } uval = val * counter->scale; - - if (nsec_counter(counter)) - nsec_printout(cpu, 0, counter, uval); - else - abs_printout(cpu, 0, counter, uval); - + printout(cpu, 0, counter, uval); if (!csv_output) print_noise(counter, 1.0); print_running(run, ena); -- cgit v1.2.3 From cb8382e05817a8104ea0edb63b8e37b8fbecd14c Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Wed, 4 Nov 2015 15:08:11 +0100 Subject: perf tools: Insert split maps correctly into origin group When new maps are cloned out of split map they are added into origin map's group, but their groups pointer is not updated. This could lead to a segfault, because map->groups is expected to be always set as reported by Markus: __map__is_kernel (map=map@entry=0x1abb7a0) at util/map.c:238 238 return __machine__kernel_map(map->groups->machine, map->type) = (gdb) bt #0 __map__is_kernel (map=map@entry=0x1abb7a0) at util/map.c:238 #1 0x00000000004393e4 in symbol_filter (map=map@entry=0x1abb7a0, sym=sym@entry #2 0x00000000004fcd4d in dso__load_sym (dso=dso@entry=0x166dae0, map=map@entry #3 0x00000000004a64e0 in dso__load (dso=0x166dae0, map=map@entry=0x1abb7a0, fi #4 0x00000000004b941f in map__load (filter=0x4393c0 , map=groups pointer update. It takes no lock as opposed to existing map_groups__insert, as maps__fixup_overlappings(), where it is being called, already has the necessary lock held. Using __map_groups__insert to add new maps after map split. Reported-by: Markus Trippelsdorf Signed-off-by: Jiri Olsa Tested-by: Markus Trippelsdorf Cc: Andrew Morton Cc: Peter Zijlstra Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/20151104140811.GA32664@krava.brq.redhat.com Fixes: cfc5acd4c80b ("perf top: Filter symbols based on __map__is_kernel(map)") Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/map.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 4e38c396a897..afc6b56cf749 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -644,6 +644,12 @@ size_t map_groups__fprintf(struct map_groups *mg, FILE *fp) return printed; } +static void __map_groups__insert(struct map_groups *mg, struct map *map) +{ + __maps__insert(&mg->maps[map->type], map); + map->groups = mg; +} + static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) { struct rb_root *root; @@ -682,7 +688,7 @@ static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp } before->end = map->start; - __maps__insert(maps, before); + __map_groups__insert(pos->groups, before); if (verbose >= 2) map__fprintf(before, fp); } @@ -696,7 +702,7 @@ static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp } after->start = map->end; - __maps__insert(maps, after); + __map_groups__insert(pos->groups, after); if (verbose >= 2) map__fprintf(after, fp); } -- cgit v1.2.3 From 98916392a0bdfd71668b711fd93915791f45df02 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Wed, 4 Nov 2015 16:25:32 -0300 Subject: perf tools: Allow shuffling the build tests Those tests take a long time and sometimes we stop it, so allow randomly shuffling the tests so that we have a better chance of running more of them in partial 'make build-test' runs. Using it just on the 'build-test' target, i.e.: make -C tools/perf build-test Is equivalent to: make SHUF=1 -C tools/perf -f tests/make Cc: Adrian Hunter Cc: David Ahern Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Link: http://lkml.kernel.org/n/tip-ey7461i9q4k8u0987j8guun6@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/Makefile | 2 +- tools/perf/tests/make | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 480546d5f13b..dcd9a70c7193 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile @@ -78,7 +78,7 @@ clean: # The build-test target is not really parallel, don't print the jobs info: # build-test: - @$(MAKE) -f tests/make --no-print-directory + @$(MAKE) SHUF=1 -f tests/make --no-print-directory # # All other targets get passed through: diff --git a/tools/perf/tests/make b/tools/perf/tests/make index 2cbd0c6901e3..8ea3dffc5065 100644 --- a/tools/perf/tests/make +++ b/tools/perf/tests/make @@ -221,6 +221,11 @@ test_O = $(if $(test_$1),$(test_$1),$(test_default_O)) all: +ifdef SHUF +run := $(shell shuf -e $(run)) +run_O := $(shell shuf -e $(run_O)) +endif + ifdef DEBUG d := $(info run $(run)) d := $(info run_O $(run_O)) -- cgit v1.2.3 From 59f41af980f95cbd556a6dc2e064b412abc439cf Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Wed, 4 Nov 2015 11:20:04 +0000 Subject: perf llvm: Pass number of configured CPUs to clang compiler This patch introduces a new macro "__NR_CPUS__" to perf's embedded clang compiler, which represent the number of configured CPUs in this system. BPF programs can use this macro to create a map with the same number of system CPUs. For example: struct bpf_map_def SEC("maps") pmu_map = { .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, .key_size = sizeof(int), .value_size = sizeof(u32), .max_entries = __NR_CPUS__, }; Signed-off-by: Wang Nan Cc: Alexei Starovoitov Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446636007-239722-2-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 4f6a4780bd5f..80eecefc604f 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -11,10 +11,11 @@ #include "cache.h" #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ - "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \ - "$KERNEL_INC_OPTIONS -Wno-unused-value " \ - "-Wno-pointer-sign -working-directory " \ - "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -" + "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ + "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \ + "-Wno-unused-value -Wno-pointer-sign " \ + "-working-directory $WORKING_DIR " \ + "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -" struct llvm_param llvm_param = { .clang_path = "clang", @@ -326,8 +327,8 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz) { - int err; - char clang_path[PATH_MAX]; + int err, nr_cpus_avail; + char clang_path[PATH_MAX], nr_cpus_avail_str[64]; const char *clang_opt = llvm_param.clang_opt; const char *template = llvm_param.clang_bpf_cmd_template; char *kbuild_dir = NULL, *kbuild_include_opts = NULL; @@ -354,6 +355,17 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, */ get_kbuild_opts(&kbuild_dir, &kbuild_include_opts); + nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF); + if (nr_cpus_avail <= 0) { + pr_err( +"WARNING:\tunable to get available CPUs in this system: %s\n" +" \tUse 128 instead.\n", strerror(errno)); + nr_cpus_avail = 128; + } + snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", + nr_cpus_avail); + + force_set_env("NR_CPUS", nr_cpus_avail_str); force_set_env("CLANG_EXEC", clang_path); force_set_env("CLANG_OPTIONS", clang_opt); force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts); -- cgit v1.2.3 From 4a4f66a1a7031675745812729ade94ad1caf1db6 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Wed, 4 Nov 2015 11:20:05 +0000 Subject: perf llvm: Pass LINUX_VERSION_CODE to BPF program when compiling Arnaldo suggests to make LINUX_VERSION_CODE works like __func__ and __FILE__ so user don't need to care setting right linux version too much. In this patch, perf llvm transfers LINUX_VERSION_CODE macro through clang cmdline. [1] http://lkml.kernel.org/r/20151029223744.GK2923@kernel.org Committer notes: Before, forgetting to update the version: # uname -r 4.3.0-rc1+ # cat bpf.c __attribute__((section("fork=_do_fork"), used)) int fork(void *ctx) { return 1; } char _license[] __attribute__((section("license"), used)) = "GPL"; int _version __attribute__((section("version"), used)) = 0x40200; # # perf record -e bpf.c sleep 1 event syntax error: 'bpf.c' \___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel? (add -v to see detail) Run 'perf list' for a list of valid events Usage: perf record [] [] or: perf record [] -- [] -e, --event event selector. use 'perf list' to list available events # After: # grep version bpf.c int _version __attribute__((section("version"), used)) = LINUX_VERSION_CODE; # perf record -e bpf.c sleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.017 MB perf.data ] # perf evlist -v perf_bpf_probe:fork: type: 2, size: 112, config: 0x5ee, { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|CPU|PERIOD|RAW, disabled: 1, inherit: 1, mmap: 1, comm: 1, enable_on_exec: 1, task: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1 # Suggested-and-Tested-by: Arnaldo Carvalho de Melo Signed-off-by: Wang Nan Cc: Alexei Starovoitov Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446636007-239722-3-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 80eecefc604f..8ee25bea1b24 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -12,6 +12,7 @@ #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ + "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \ "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \ "-Wno-unused-value -Wno-pointer-sign " \ "-working-directory $WORKING_DIR " \ @@ -324,11 +325,33 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) pr_debug("include option is set to %s\n", *kbuild_include_opts); } +static unsigned long +fetch_kernel_version(void) +{ + struct utsname utsname; + int version, patchlevel, sublevel, err; + + if (uname(&utsname)) + return 0; + + err = sscanf(utsname.release, "%d.%d.%d", + &version, &patchlevel, &sublevel); + + if (err != 3) { + pr_debug("Unablt to get kernel version from uname '%s'\n", + utsname.release); + return 0; + } + + return (version << 16) + (patchlevel << 8) + sublevel; +} + int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz) { int err, nr_cpus_avail; char clang_path[PATH_MAX], nr_cpus_avail_str[64]; + char linux_version_code_str[64]; const char *clang_opt = llvm_param.clang_opt; const char *template = llvm_param.clang_bpf_cmd_template; char *kbuild_dir = NULL, *kbuild_include_opts = NULL; @@ -365,7 +388,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", nr_cpus_avail); + snprintf(linux_version_code_str, sizeof(linux_version_code_str), + "0x%lx", fetch_kernel_version()); + force_set_env("NR_CPUS", nr_cpus_avail_str); + force_set_env("LINUX_VERSION_CODE", linux_version_code_str); force_set_env("CLANG_EXEC", clang_path); force_set_env("CLANG_OPTIONS", clang_opt); force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts); -- cgit v1.2.3 From 0c77c04aa9c2b3a154f5cb0559feaee3fb7a6935 Mon Sep 17 00:00:00 2001 From: Yunlong Song Date: Thu, 5 Nov 2015 14:56:34 +0800 Subject: tools lib bpf: Change FEATURE-DUMP to FEATURE-DUMP.libbpf Commit ed63f34c026e9a60d17fa750ecdfe3f600d49393 ("perf tools: Make perf depend on libbpf") triggers the build of libbpf when building the perf tools, dynamically creating FEATURE-DUMP.libbpf. It failed to update the tools/lib/bpf/.gitignore file to have that prefix, fix it. Signed-off-by: Yunlong Song Cc: Adrian Hunter Cc: Alexei Starovoitov Cc: David Ahern Cc: Jiri Olsa Cc: Kan Liang Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Wang Nan Fixes: 65f041bee783 ("tools lib bpf: Use FEATURE_USER to allow building in the same dir as perf") Link: http://lkml.kernel.org/r/1446706594-4142-1-git-send-email-yunlong.song@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/.gitignore | 2 +- tools/lib/bpf/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/.gitignore b/tools/lib/bpf/.gitignore index 812aeedaea38..f81e549ddfdb 100644 --- a/tools/lib/bpf/.gitignore +++ b/tools/lib/bpf/.gitignore @@ -1,2 +1,2 @@ libbpf_version.h -FEATURE-DUMP +FEATURE-DUMP.libbpf diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile index fc9af57b666e..a3caaf3eafbd 100644 --- a/tools/lib/bpf/Makefile +++ b/tools/lib/bpf/Makefile @@ -180,7 +180,7 @@ config-clean: clean: $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \ $(RM) LIBBPF-CFLAGS - $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP + $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf -- cgit v1.2.3 From 956959f6b7a982b2e789a7a8fa1de437074a5eb9 Mon Sep 17 00:00:00 2001 From: Peter Feiner Date: Wed, 4 Nov 2015 09:21:46 -0800 Subject: perf trace: Fix documentation for -i The -i flag was incorrectly listed as a short flag for --no-inherit. It should have only been listed as a short flag for --input. This documentation error has existed since the --input flag was introduced in 6810fc915f7a89d8134edb3996dbbf8eac386c26 (perf trace: Add option to analyze events in a file versus live). Signed-off-by: Peter Feiner Cc: David Ahern Link: http://lkml.kernel.org/r/1446657706-14518-1-git-send-email-pfeiner@google.com Fixes: 6810fc915f7a ("perf trace: Add option to analyze events in a file versus live") Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/Documentation/perf-trace.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/perf/Documentation/perf-trace.txt b/tools/perf/Documentation/perf-trace.txt index 7ea078658a87..13293de8869f 100644 --- a/tools/perf/Documentation/perf-trace.txt +++ b/tools/perf/Documentation/perf-trace.txt @@ -62,7 +62,6 @@ OPTIONS --verbose=:: Verbosity level. --i:: --no-inherit:: Child tasks do not inherit counters. -- cgit v1.2.3 From 98d3b258ede2cdac31a2728543f652964e597e79 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Thu, 5 Nov 2015 13:19:25 +0000 Subject: perf tools: Fix find_perf_probe_point_from_map() which incorrectly returns success It is possible that find_perf_probe_point_from_map() fails to find a symbol but still returns 0 because of an small error when coding: find_perf_probe_point_from_map() set 'ret' to error code at first, but also use it to hold return value of kernel_get_symbol_address_by_name(). This patch resets 'ret' to error even kernel_get_symbol_address_by_name() success, so if !sym, the whole function returns error correctly. Signed-off-by: Wang Nan Cc: Jiri Olsa Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446729565-27592-3-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/probe-event.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index b51a8bfb40f9..e659c4faddec 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -1905,6 +1905,9 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, sym = __find_kernel_function(addr, &map); } } + + /* ret may has be overwritten so reset it */ + ret = -ENOENT; if (!sym) goto out; -- cgit v1.2.3 From 0014de172d228e450377d1fd079d94e67128d27f Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Mon, 2 Nov 2015 12:10:25 +0100 Subject: perf sched latency: Fix thread pid reuse issue The latency subcommand holds a tree of working atoms sorted by thread's pid/tid. If there's new thread with same pid and tid, the old working atom is found and assert bug condition is hit in search function: thread_atoms_search: Assertion `!(thread != atoms->thread)' failed Changing the sort function to use thread object pointers together with pid and tid check. This way new thread will never find old one with same pid/tid. Link: http://lkml.kernel.org/n/tip-o4doazhhv0zax5zshkg8hnys@git.kernel.org Reported-by: Mohit Agrawal Signed-off-by: Jiri Olsa Acked-by: Namhyung Kim Cc: David Ahern Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1446462625-15807-1-git-send-email-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-sched.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 0ee6d900e100..e3d3e32c0a93 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -1203,12 +1203,13 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_ static int pid_cmp(struct work_atoms *l, struct work_atoms *r) { + if (l->thread == r->thread) + return 0; if (l->thread->tid < r->thread->tid) return -1; if (l->thread->tid > r->thread->tid) return 1; - - return 0; + return (int)(l->thread - r->thread); } static int avg_cmp(struct work_atoms *l, struct work_atoms *r) -- cgit v1.2.3 From e0547311133159bf95f7998726e4e4932d78d8ce Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Thu, 5 Nov 2015 15:40:45 +0100 Subject: perf stat: Make stat options global So they can be used in perf stat record command in following patch. Signed-off-by: Jiri Olsa Tested-by: Kan Liang Cc: David Ahern Cc: Namhyung Kim Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1446734469-11352-2-git-send-email-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 163 +++++++++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 81 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index b74ee0f2e714..e77880b5094d 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -122,6 +122,9 @@ static bool forever = false; static struct timespec ref_time; static struct cpu_map *aggr_map; static aggr_get_id_t aggr_get_id; +static bool append_file; +static const char *output_name; +static int output_fd; static volatile int done = 0; @@ -927,6 +930,67 @@ static int stat__set_big_num(const struct option *opt __maybe_unused, return 0; } +static const struct option stat_options[] = { + OPT_BOOLEAN('T', "transaction", &transaction_run, + "hardware transaction statistics"), + OPT_CALLBACK('e', "event", &evsel_list, "event", + "event selector. use 'perf list' to list available events", + parse_events_option), + OPT_CALLBACK(0, "filter", &evsel_list, "filter", + "event filter", parse_filter), + OPT_BOOLEAN('i', "no-inherit", &no_inherit, + "child tasks do not inherit counters"), + OPT_STRING('p', "pid", &target.pid, "pid", + "stat events on existing process id"), + OPT_STRING('t', "tid", &target.tid, "tid", + "stat events on existing thread id"), + OPT_BOOLEAN('a', "all-cpus", &target.system_wide, + "system-wide collection from all CPUs"), + OPT_BOOLEAN('g', "group", &group, + "put the counters into a counter group"), + OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"), + OPT_INCR('v', "verbose", &verbose, + "be more verbose (show counter open errors, etc)"), + OPT_INTEGER('r', "repeat", &run_count, + "repeat command and print average + stddev (max: 100, forever: 0)"), + OPT_BOOLEAN('n', "null", &null_run, + "null run - dont start any counters"), + OPT_INCR('d', "detailed", &detailed_run, + "detailed run - start a lot of events"), + OPT_BOOLEAN('S', "sync", &sync_run, + "call sync() before starting a run"), + OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, + "print large numbers with thousands\' separators", + stat__set_big_num), + OPT_STRING('C', "cpu", &target.cpu_list, "cpu", + "list of cpus to monitor in system-wide"), + OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, + "disable CPU count aggregation", AGGR_NONE), + OPT_STRING('x', "field-separator", &csv_sep, "separator", + "print counts with custom separator"), + OPT_CALLBACK('G', "cgroup", &evsel_list, "name", + "monitor event in cgroup name only", parse_cgroups), + OPT_STRING('o', "output", &output_name, "file", "output file name"), + OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), + OPT_INTEGER(0, "log-fd", &output_fd, + "log output to fd, instead of stderr"), + OPT_STRING(0, "pre", &pre_cmd, "command", + "command to run prior to the measured command"), + OPT_STRING(0, "post", &post_cmd, "command", + "command to run after to the measured command"), + OPT_UINTEGER('I', "interval-print", &stat_config.interval, + "print counts at regular interval in ms (>= 10)"), + OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, + "aggregate counts per processor socket", AGGR_SOCKET), + OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, + "aggregate counts per physical processor core", AGGR_CORE), + OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, + "aggregate counts per thread", AGGR_THREAD), + OPT_UINTEGER('D', "delay", &initial_delay, + "ms to wait before starting measurement after program start"), + OPT_END() +}; + static int perf_stat__get_socket(struct cpu_map *map, int cpu) { return cpu_map__get_socket(map, cpu, NULL); @@ -1174,69 +1238,6 @@ static int add_default_attributes(void) int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) { - bool append_file = false; - int output_fd = 0; - const char *output_name = NULL; - const struct option options[] = { - OPT_BOOLEAN('T', "transaction", &transaction_run, - "hardware transaction statistics"), - OPT_CALLBACK('e', "event", &evsel_list, "event", - "event selector. use 'perf list' to list available events", - parse_events_option), - OPT_CALLBACK(0, "filter", &evsel_list, "filter", - "event filter", parse_filter), - OPT_BOOLEAN('i', "no-inherit", &no_inherit, - "child tasks do not inherit counters"), - OPT_STRING('p', "pid", &target.pid, "pid", - "stat events on existing process id"), - OPT_STRING('t', "tid", &target.tid, "tid", - "stat events on existing thread id"), - OPT_BOOLEAN('a', "all-cpus", &target.system_wide, - "system-wide collection from all CPUs"), - OPT_BOOLEAN('g', "group", &group, - "put the counters into a counter group"), - OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"), - OPT_INCR('v', "verbose", &verbose, - "be more verbose (show counter open errors, etc)"), - OPT_INTEGER('r', "repeat", &run_count, - "repeat command and print average + stddev (max: 100, forever: 0)"), - OPT_BOOLEAN('n', "null", &null_run, - "null run - dont start any counters"), - OPT_INCR('d', "detailed", &detailed_run, - "detailed run - start a lot of events"), - OPT_BOOLEAN('S', "sync", &sync_run, - "call sync() before starting a run"), - OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, - "print large numbers with thousands\' separators", - stat__set_big_num), - OPT_STRING('C', "cpu", &target.cpu_list, "cpu", - "list of cpus to monitor in system-wide"), - OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, - "disable CPU count aggregation", AGGR_NONE), - OPT_STRING('x', "field-separator", &csv_sep, "separator", - "print counts with custom separator"), - OPT_CALLBACK('G', "cgroup", &evsel_list, "name", - "monitor event in cgroup name only", parse_cgroups), - OPT_STRING('o', "output", &output_name, "file", "output file name"), - OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), - OPT_INTEGER(0, "log-fd", &output_fd, - "log output to fd, instead of stderr"), - OPT_STRING(0, "pre", &pre_cmd, "command", - "command to run prior to the measured command"), - OPT_STRING(0, "post", &post_cmd, "command", - "command to run after to the measured command"), - OPT_UINTEGER('I', "interval-print", &stat_config.interval, - "print counts at regular interval in ms (>= 10)"), - OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, - "aggregate counts per processor socket", AGGR_SOCKET), - OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, - "aggregate counts per physical processor core", AGGR_CORE), - OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, - "aggregate counts per thread", AGGR_THREAD), - OPT_UINTEGER('D', "delay", &initial_delay, - "ms to wait before starting measurement after program start"), - OPT_END() - }; const char * const stat_usage[] = { "perf stat [] []", NULL @@ -1252,7 +1253,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) if (evsel_list == NULL) return -ENOMEM; - argc = parse_options(argc, argv, options, stat_usage, + argc = parse_options(argc, argv, stat_options, stat_usage, PARSE_OPT_STOP_AT_NON_OPTION); interval = stat_config.interval; @@ -1262,14 +1263,14 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) if (output_name && output_fd) { fprintf(stderr, "cannot use both --output and --log-fd\n"); - parse_options_usage(stat_usage, options, "o", 1); - parse_options_usage(NULL, options, "log-fd", 0); + parse_options_usage(stat_usage, stat_options, "o", 1); + parse_options_usage(NULL, stat_options, "log-fd", 0); goto out; } if (output_fd < 0) { fprintf(stderr, "argument to --log-fd must be a > 0\n"); - parse_options_usage(stat_usage, options, "log-fd", 0); + parse_options_usage(stat_usage, stat_options, "log-fd", 0); goto out; } @@ -1309,8 +1310,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) /* User explicitly passed -B? */ if (big_num_opt == 1) { fprintf(stderr, "-B option not supported with -x\n"); - parse_options_usage(stat_usage, options, "B", 1); - parse_options_usage(NULL, options, "x", 1); + parse_options_usage(stat_usage, stat_options, "B", 1); + parse_options_usage(NULL, stat_options, "x", 1); goto out; } else /* Nope, so disable big number formatting */ big_num = false; @@ -1318,11 +1319,11 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) big_num = false; if (!argc && target__none(&target)) - usage_with_options(stat_usage, options); + usage_with_options(stat_usage, stat_options); if (run_count < 0) { pr_err("Run count must be a positive number\n"); - parse_options_usage(stat_usage, options, "r", 1); + parse_options_usage(stat_usage, stat_options, "r", 1); goto out; } else if (run_count == 0) { forever = true; @@ -1332,8 +1333,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) { fprintf(stderr, "The --per-thread option is only available " "when monitoring via -p -t options.\n"); - parse_options_usage(NULL, options, "p", 1); - parse_options_usage(NULL, options, "t", 1); + parse_options_usage(NULL, stat_options, "p", 1); + parse_options_usage(NULL, stat_options, "t", 1); goto out; } @@ -1347,9 +1348,9 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) fprintf(stderr, "both cgroup and no-aggregation " "modes only available in system-wide mode\n"); - parse_options_usage(stat_usage, options, "G", 1); - parse_options_usage(NULL, options, "A", 1); - parse_options_usage(NULL, options, "a", 1); + parse_options_usage(stat_usage, stat_options, "G", 1); + parse_options_usage(NULL, stat_options, "A", 1); + parse_options_usage(NULL, stat_options, "a", 1); goto out; } @@ -1361,12 +1362,12 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) if (perf_evlist__create_maps(evsel_list, &target) < 0) { if (target__has_task(&target)) { pr_err("Problems finding threads of monitor\n"); - parse_options_usage(stat_usage, options, "p", 1); - parse_options_usage(NULL, options, "t", 1); + parse_options_usage(stat_usage, stat_options, "p", 1); + parse_options_usage(NULL, stat_options, "t", 1); } else if (target__has_cpu(&target)) { perror("failed to parse CPUs map"); - parse_options_usage(stat_usage, options, "C", 1); - parse_options_usage(NULL, options, "a", 1); + parse_options_usage(stat_usage, stat_options, "C", 1); + parse_options_usage(NULL, stat_options, "a", 1); } goto out; } @@ -1381,7 +1382,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) if (interval && interval < 100) { if (interval < 10) { pr_err("print interval must be >= 10ms\n"); - parse_options_usage(stat_usage, options, "I", 1); + parse_options_usage(stat_usage, stat_options, "I", 1); goto out; } else pr_warning("print interval < 100ms. " -- cgit v1.2.3 From 62ec9b3f02a9bccaf699bd4691db98f779c3075f Mon Sep 17 00:00:00 2001 From: Andi Kleen Date: Thu, 5 Nov 2015 19:06:07 -0800 Subject: perf annotate: Inform the user about objdump failures in --stdio When the browser fails to annotate it is difficult for users to find out what went wrong. Add some errors for objdump failures that are displayed in the UI. Note it would be even better to handle these errors smarter, like falling back to the binary when the debug info is somehow corrupted. But for now just giving a better error is an improvement. Committer note: This works for --stdio, where errors just scroll by the screen: # perf annotate --stdio intel_idle Failure running objdump --start-address=0xffffffff81418290 --stop-address=0xffffffff814183ae -l -d --no-show-raw -S -C /root/.debug/.build-id/28/2777c262e6b3c0451375163c9a81c893218ab1 2>/dev/null|grep -v /root/.debug/.build-id/28/2777c262e6b3c0451375163c9a81c893218ab1|expand Percent | Source code & Disassembly of vmlinux for cycles:pp ------------------------------------------------------------------ And with that one can use that command line to try to find out more about what happened instead of getting a blank screen, an improvement. We need tho to improve this further to get it to work with other UIs, like --tui and --gtk, where it continues showing a blank screen, no messages, as the pr_err() used is enough just for --stdio. Signed-off-by: Andi Kleen Tested-by: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Namhyung Kim Link: http://lkml.kernel.org/r/1446779167-18949-1-git-send-email-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/annotate.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 0fc8d7a2fea5..f2974da0185a 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1084,6 +1084,7 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) struct kcore_extract kce; bool delete_extract = false; int lineno = 0; + int nline; if (filename) symbol__join_symfs(symfs_filename, filename); @@ -1179,6 +1180,9 @@ fallback: ret = decompress_to_file(m.ext, symfs_filename, fd); + if (ret) + pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename); + free(m.ext); close(fd); @@ -1204,13 +1208,25 @@ fallback: pr_debug("Executing: %s\n", command); file = popen(command, "r"); - if (!file) + if (!file) { + pr_err("Failure running %s\n", command); + /* + * If we were using debug info should retry with + * original binary. + */ goto out_remove_tmp; + } - while (!feof(file)) + nline = 0; + while (!feof(file)) { if (symbol__parse_objdump_line(sym, map, file, privsize, &lineno) < 0) break; + nline++; + } + + if (nline == 0) + pr_err("No output from %s\n", command); /* * kallsyms does not have symbol sizes so there may a nop at the end. -- cgit v1.2.3 From 0a62f6869f2768687af2c94d97f3b2fcf5b73367 Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Fri, 6 Nov 2015 17:30:03 +0900 Subject: perf probe: Cleanup find_perf_probe_point_from_map to reduce redundancy In find_perf_probe_point_from_map(), the 'ret' variable is initialized with -ENOENT but overwritten by the return code of kernel_get_symbol_address_by_name(), and after that it is re-initialized with -ENOENT again. Setting ret=-ENOENT twice looks a bit redundant. This avoids the overwriting and just returns -ENOENT if some error happens to simplify the code. Signed-off-by: Masami Hiramatsu Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Cc: Zefan Li Link: http://lkml.kernel.org/n/tip-ufp1zgbktzmttcputozneomd@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/probe-event.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index e659c4faddec..03875f9154e7 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -1895,9 +1895,8 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, sym = map__find_symbol(map, addr, NULL); } else { if (tp->symbol && !addr) { - ret = kernel_get_symbol_address_by_name(tp->symbol, - &addr, true, false); - if (ret < 0) + if (kernel_get_symbol_address_by_name(tp->symbol, + &addr, true, false) < 0) goto out; } if (addr) { @@ -1906,8 +1905,6 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, } } - /* ret may has be overwritten so reset it */ - ret = -ENOENT; if (!sym) goto out; -- cgit v1.2.3 From 6371ca3b541c82d8aa6a9002bd52d92bcdda5944 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:49:37 +0000 Subject: bpf tools: Improve libbpf error reporting In this patch, a series of libbpf specific error numbers and libbpf_strerror() are introduced to help reporting errors. Functions are updated to pass correct the error number through the CHECK_ERR() macro. All users of bpf_object__open{_buffer}() and bpf_program__title() in perf are modified accordingly. In addition, due to the error codes changing, bpf__strerror_load() is also modified to use them. bpf__strerror_head() is also changed accordingly so it can parse libbpf errors. bpf_loader_strerror() is introduced for that purpose, and will be improved by the following patch. load_program() is improved not to dump log buffer if it is empty. log buffer is also used to deduce whether the error was caused by an invalid program or other problem. v1 -> v2: - Using macro for error code. - Fetch error message based on array index, eliminate for-loop. - Use log buffer to detect the reason of failure. 3 new error code are introduced to replace LIBBPF_ERRNO__LOAD. In v1: # perf record -e ./test_ill_program.o ls event syntax error: './test_ill_program.o' \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object SKIP # perf record -e ./test_kversion_nomatch_program.o ls event syntax error: './test_kversion_nomatch_program.o' \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object SKIP # perf record -e ./test_big_program.o ls event syntax error: './test_big_program.o' \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object SKIP In v2: # perf record -e ./test_ill_program.o ls event syntax error: './test_ill_program.o' \___ Kernel verifier blocks program loading SKIP # perf record -e ./test_kversion_nomatch_program.o event syntax error: './test_kversion_nomatch_program.o' \___ Incorrect kernel version SKIP (Will be further improved by following patches) # perf record -e ./test_big_program.o event syntax error: './test_big_program.o' \___ Program too big SKIP Signed-off-by: Wang Nan Tested-by: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446817783-86722-2-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 159 ++++++++++++++++++++++++++++------------- tools/lib/bpf/libbpf.h | 20 ++++++ tools/perf/tests/llvm.c | 2 +- tools/perf/util/bpf-loader.c | 33 +++++++-- tools/perf/util/parse-events.c | 4 +- 5 files changed, 159 insertions(+), 59 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 9f3c8cf9249b..07b492d3dfaa 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -61,6 +61,60 @@ void libbpf_set_print(libbpf_print_fn_t warn, __pr_debug = debug; } +#define STRERR_BUFSIZE 128 + +#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) +#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) +#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) + +static const char *libbpf_strerror_table[NR_ERRNO] = { + [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", + [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", + [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", + [ERRCODE_OFFSET(ENDIAN)] = "Endian missmatch", + [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", + [ERRCODE_OFFSET(RELOC)] = "Relocation failed", + [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", + [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", + [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", +}; + +int libbpf_strerror(int err, char *buf, size_t size) +{ + if (!buf || !size) + return -1; + + err = err > 0 ? err : -err; + + if (err < __LIBBPF_ERRNO__START) { + int ret; + + ret = strerror_r(err, buf, size); + buf[size - 1] = '\0'; + return ret; + } + + if (err < __LIBBPF_ERRNO__END) { + const char *msg; + + msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; + snprintf(buf, size, "%s", msg); + buf[size - 1] = '\0'; + return 0; + } + + snprintf(buf, size, "Unknown libbpf error %d", err); + buf[size - 1] = '\0'; + return -1; +} + +#define CHECK_ERR(action, err, out) do { \ + err = action; \ + if (err) \ + goto out; \ +} while(0) + + /* Copied from tools/perf/util/util.h */ #ifndef zfree # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) @@ -258,7 +312,7 @@ static struct bpf_object *bpf_object__new(const char *path, obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); if (!obj) { pr_warning("alloc memory failed for %s\n", path); - return NULL; + return ERR_PTR(-ENOMEM); } strcpy(obj->path, path); @@ -305,7 +359,7 @@ static int bpf_object__elf_init(struct bpf_object *obj) if (obj_elf_valid(obj)) { pr_warning("elf init: internal error\n"); - return -EEXIST; + return -LIBBPF_ERRNO__LIBELF; } if (obj->efile.obj_buf_sz > 0) { @@ -331,14 +385,14 @@ static int bpf_object__elf_init(struct bpf_object *obj) if (!obj->efile.elf) { pr_warning("failed to open %s as ELF file\n", obj->path); - err = -EINVAL; + err = -LIBBPF_ERRNO__LIBELF; goto errout; } if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { pr_warning("failed to get EHDR from %s\n", obj->path); - err = -EINVAL; + err = -LIBBPF_ERRNO__FORMAT; goto errout; } ep = &obj->efile.ehdr; @@ -346,7 +400,7 @@ static int bpf_object__elf_init(struct bpf_object *obj) if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) { pr_warning("%s is not an eBPF object file\n", obj->path); - err = -EINVAL; + err = -LIBBPF_ERRNO__FORMAT; goto errout; } @@ -374,14 +428,14 @@ bpf_object__check_endianness(struct bpf_object *obj) goto mismatch; break; default: - return -EINVAL; + return -LIBBPF_ERRNO__ENDIAN; } return 0; mismatch: pr_warning("Error: endianness mismatch.\n"); - return -EINVAL; + return -LIBBPF_ERRNO__ENDIAN; } static int @@ -402,7 +456,7 @@ bpf_object__init_kversion(struct bpf_object *obj, if (size != sizeof(kver)) { pr_warning("invalid kver section in %s\n", obj->path); - return -EINVAL; + return -LIBBPF_ERRNO__FORMAT; } memcpy(&kver, data, sizeof(kver)); obj->kern_version = kver; @@ -444,7 +498,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { pr_warning("failed to get e_shstrndx from %s\n", obj->path); - return -EINVAL; + return -LIBBPF_ERRNO__FORMAT; } while ((scn = elf_nextscn(elf, scn)) != NULL) { @@ -456,7 +510,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (gelf_getshdr(scn, &sh) != &sh) { pr_warning("failed to get section header from %s\n", obj->path); - err = -EINVAL; + err = -LIBBPF_ERRNO__FORMAT; goto out; } @@ -464,7 +518,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (!name) { pr_warning("failed to get section name from %s\n", obj->path); - err = -EINVAL; + err = -LIBBPF_ERRNO__FORMAT; goto out; } @@ -472,7 +526,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (!data) { pr_warning("failed to get section data from %s(%s)\n", name, obj->path); - err = -EINVAL; + err = -LIBBPF_ERRNO__FORMAT; goto out; } pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n", @@ -495,7 +549,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (obj->efile.symbols) { pr_warning("bpf: multiple SYMTAB in %s\n", obj->path); - err = -EEXIST; + err = -LIBBPF_ERRNO__FORMAT; } else obj->efile.symbols = data; } else if ((sh.sh_type == SHT_PROGBITS) && @@ -504,7 +558,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj) err = bpf_object__add_program(obj, data->d_buf, data->d_size, name, idx); if (err) { - char errmsg[128]; + char errmsg[STRERR_BUFSIZE]; + strerror_r(-err, errmsg, sizeof(errmsg)); pr_warning("failed to alloc program %s (%s): %s", name, obj->path, errmsg); @@ -576,7 +631,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, if (!gelf_getrel(data, i, &rel)) { pr_warning("relocation: failed to get %d reloc\n", i); - return -EINVAL; + return -LIBBPF_ERRNO__FORMAT; } insn_idx = rel.r_offset / sizeof(struct bpf_insn); @@ -587,20 +642,20 @@ bpf_program__collect_reloc(struct bpf_program *prog, &sym)) { pr_warning("relocation: symbol %"PRIx64" not found\n", GELF_R_SYM(rel.r_info)); - return -EINVAL; + return -LIBBPF_ERRNO__FORMAT; } if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", insn_idx, insns[insn_idx].code); - return -EINVAL; + return -LIBBPF_ERRNO__RELOC; } map_idx = sym.st_value / sizeof(struct bpf_map_def); if (map_idx >= nr_maps) { pr_warning("bpf relocation: map_idx %d large than %d\n", (int)map_idx, (int)nr_maps - 1); - return -EINVAL; + return -LIBBPF_ERRNO__RELOC; } prog->reloc_desc[i].insn_idx = insn_idx; @@ -683,7 +738,7 @@ bpf_program__relocate(struct bpf_program *prog, int *map_fds) if (insn_idx >= (int)prog->insns_cnt) { pr_warning("relocation out of range: '%s'\n", prog->section_name); - return -ERANGE; + return -LIBBPF_ERRNO__RELOC; } insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; insns[insn_idx].imm = map_fds[map_idx]; @@ -721,7 +776,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) if (!obj_elf_valid(obj)) { pr_warning("Internal error: elf object is closed\n"); - return -EINVAL; + return -LIBBPF_ERRNO__INTERNAL; } for (i = 0; i < obj->efile.nr_reloc; i++) { @@ -734,21 +789,21 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) if (shdr->sh_type != SHT_REL) { pr_warning("internal error at %d\n", __LINE__); - return -EINVAL; + return -LIBBPF_ERRNO__INTERNAL; } prog = bpf_object__find_prog_by_idx(obj, idx); if (!prog) { pr_warning("relocation failed: no %d section\n", idx); - return -ENOENT; + return -LIBBPF_ERRNO__RELOC; } err = bpf_program__collect_reloc(prog, nr_maps, shdr, data, obj->efile.symbols); if (err) - return -EINVAL; + return err; } return 0; } @@ -777,13 +832,23 @@ load_program(struct bpf_insn *insns, int insns_cnt, goto out; } - ret = -EINVAL; + ret = -LIBBPF_ERRNO__LOAD; pr_warning("load bpf program failed: %s\n", strerror(errno)); - if (log_buf) { + if (log_buf && log_buf[0] != '\0') { + ret = -LIBBPF_ERRNO__VERIFY; pr_warning("-- BEGIN DUMP LOG ---\n"); pr_warning("\n%s\n", log_buf); pr_warning("-- END LOG --\n"); + } else { + if (insns_cnt >= BPF_MAXINSNS) { + pr_warning("Program too large (%d insns), at most %d insns\n", + insns_cnt, BPF_MAXINSNS); + ret = -LIBBPF_ERRNO__PROG2BIG; + } else if (log_buf) { + pr_warning("log buffer is empty\n"); + ret = -LIBBPF_ERRNO__KVER; + } } out: @@ -831,7 +896,7 @@ static int bpf_object__validate(struct bpf_object *obj) if (obj->kern_version == 0) { pr_warning("%s doesn't provide kernel version\n", obj->path); - return -EINVAL; + return -LIBBPF_ERRNO__KVERSION; } return 0; } @@ -840,32 +905,28 @@ static struct bpf_object * __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) { struct bpf_object *obj; + int err; if (elf_version(EV_CURRENT) == EV_NONE) { pr_warning("failed to init libelf for %s\n", path); - return NULL; + return ERR_PTR(-LIBBPF_ERRNO__LIBELF); } obj = bpf_object__new(path, obj_buf, obj_buf_sz); - if (!obj) - return NULL; + if (IS_ERR(obj)) + return obj; - if (bpf_object__elf_init(obj)) - goto out; - if (bpf_object__check_endianness(obj)) - goto out; - if (bpf_object__elf_collect(obj)) - goto out; - if (bpf_object__collect_reloc(obj)) - goto out; - if (bpf_object__validate(obj)) - goto out; + CHECK_ERR(bpf_object__elf_init(obj), err, out); + CHECK_ERR(bpf_object__check_endianness(obj), err, out); + CHECK_ERR(bpf_object__elf_collect(obj), err, out); + CHECK_ERR(bpf_object__collect_reloc(obj), err, out); + CHECK_ERR(bpf_object__validate(obj), err, out); bpf_object__elf_finish(obj); return obj; out: bpf_object__close(obj); - return NULL; + return ERR_PTR(err); } struct bpf_object *bpf_object__open(const char *path) @@ -922,6 +983,8 @@ int bpf_object__unload(struct bpf_object *obj) int bpf_object__load(struct bpf_object *obj) { + int err; + if (!obj) return -EINVAL; @@ -931,18 +994,16 @@ int bpf_object__load(struct bpf_object *obj) } obj->loaded = true; - if (bpf_object__create_maps(obj)) - goto out; - if (bpf_object__relocate(obj)) - goto out; - if (bpf_object__load_progs(obj)) - goto out; + + CHECK_ERR(bpf_object__create_maps(obj), err, out); + CHECK_ERR(bpf_object__relocate(obj), err, out); + CHECK_ERR(bpf_object__load_progs(obj), err, out); return 0; out: bpf_object__unload(obj); pr_warning("failed to load object '%s'\n", obj->path); - return -EINVAL; + return err; } void bpf_object__close(struct bpf_object *obj) @@ -990,7 +1051,7 @@ const char * bpf_object__get_name(struct bpf_object *obj) { if (!obj) - return NULL; + return ERR_PTR(-EINVAL); return obj->path; } @@ -1043,7 +1104,7 @@ const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) title = strdup(title); if (!title) { pr_warning("failed to strdup program title\n"); - return NULL; + return ERR_PTR(-ENOMEM); } } diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index bc80af03c6f4..30a40e9fa503 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -10,6 +10,26 @@ #include #include +#include + +enum libbpf_errno { + __LIBBPF_ERRNO__START = 4000, + + /* Something wrong in libelf */ + LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START, + LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */ + LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */ + LIBBPF_ERRNO__ENDIAN, /* Endian missmatch */ + LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */ + LIBBPF_ERRNO__RELOC, /* Relocation failed */ + LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */ + LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ + LIBBPF_ERRNO__PROG2BIG, /* Program too big */ + LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ + __LIBBPF_ERRNO__END, +}; + +int libbpf_strerror(int err, char *buf, size_t size); /* * In include/linux/compiler-gcc.h, __printf is defined. However diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c index 512d3620e9f9..8f713f6d32f3 100644 --- a/tools/perf/tests/llvm.c +++ b/tools/perf/tests/llvm.c @@ -27,7 +27,7 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz) struct bpf_object *obj; obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL); - if (!obj) + if (IS_ERR(obj)) return -1; bpf_object__close(obj); return 0; diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 0c5d174245bb..c46256b1f5fd 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -59,9 +59,9 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source) } else obj = bpf_object__open(filename); - if (!obj) { + if (IS_ERR(obj)) { pr_debug("bpf: failed to load %s\n", filename); - return ERR_PTR(-EINVAL); + return obj; } return obj; @@ -96,9 +96,9 @@ config_bpf_program(struct bpf_program *prog) int err; config_str = bpf_program__title(prog, false); - if (!config_str) { + if (IS_ERR(config_str)) { pr_debug("bpf: unable to get title for program\n"); - return -EINVAL; + return PTR_ERR(config_str); } priv = calloc(sizeof(*priv), 1); @@ -308,13 +308,34 @@ int bpf__foreach_tev(struct bpf_object *obj, return 0; } +static int +bpf_loader_strerror(int err, char *buf, size_t size) +{ + char sbuf[STRERR_BUFSIZE]; + const char *msg; + + if (!buf || !size) + return -1; + + err = err > 0 ? err : -err; + + if (err >= __LIBBPF_ERRNO__START) + return libbpf_strerror(err, buf, size); + + msg = strerror_r(err, sbuf, sizeof(sbuf)); + snprintf(buf, size, "%s", msg); + buf[size - 1] = '\0'; + return 0; +} + #define bpf__strerror_head(err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\ return 0;\ if (err < 0)\ err = -err;\ - emsg = strerror_r(err, sbuf, sizeof(sbuf));\ + bpf_loader_strerror(err, sbuf, sizeof(sbuf));\ + emsg = sbuf;\ switch (err) {\ default:\ scnprintf(buf, size, "%s", emsg);\ @@ -345,8 +366,6 @@ int bpf__strerror_load(struct bpf_object *obj __maybe_unused, int err, char *buf, size_t size) { bpf__strerror_head(err, buf, size); - bpf__strerror_entry(EINVAL, "%s: Are you root and runing a CONFIG_BPF_SYSCALL kernel?", - emsg) bpf__strerror_end(buf, size); return 0; } diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index bee60583839a..c75b25d5e28c 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -632,11 +632,11 @@ int parse_events_load_bpf(struct parse_events_evlist *data, struct bpf_object *obj; obj = bpf__prepare_load(bpf_file_name, source); - if (IS_ERR(obj) || !obj) { + if (IS_ERR(obj)) { char errbuf[BUFSIZ]; int err; - err = obj ? PTR_ERR(obj) : -EINVAL; + err = PTR_ERR(obj); if (err == -ENOTSUP) snprintf(errbuf, sizeof(errbuf), -- cgit v1.2.3 From 45825d8ab8ef6287f5d05aea141419d8d4278852 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:49:38 +0000 Subject: bpf tools: Add new API bpf_object__get_kversion() bpf_object__get_kversion() can be used to fetch value of object's 'version' section. Following patch will use it for error reporting. Signed-off-by: Wang Nan Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446817783-86722-3-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 8 ++++++++ tools/lib/bpf/libbpf.h | 1 + 2 files changed, 9 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 07b492d3dfaa..e176bad19bcb 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1055,6 +1055,14 @@ bpf_object__get_name(struct bpf_object *obj) return obj->path; } +unsigned int +bpf_object__get_kversion(struct bpf_object *obj) +{ + if (!obj) + return 0; + return obj->kern_version; +} + struct bpf_program * bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 30a40e9fa503..c9a9aef2806c 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -56,6 +56,7 @@ void bpf_object__close(struct bpf_object *object); int bpf_object__load(struct bpf_object *obj); int bpf_object__unload(struct bpf_object *obj); const char *bpf_object__get_name(struct bpf_object *obj); +unsigned int bpf_object__get_kversion(struct bpf_object *obj); struct bpf_object *bpf_object__next(struct bpf_object *prev); #define bpf_object__for_each_safe(pos, tmp) \ -- cgit v1.2.3 From 07bc5c699a3d8fe5e26dbcd72e4103c7988055ba Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:55:35 +0000 Subject: perf tools: Make fetch_kernel_version() publicly available There are 2 places in llvm-utils.c which find kernel version information through uname. This patch extracts the uname related code into a fetch_kernel_version() function and puts it into util.h so it can be reused. Signed-off-by: Wang Nan Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446818135-87310-1-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/llvm-utils.c | 49 +++++++++++++++----------------------------- tools/perf/util/util.c | 30 +++++++++++++++++++++++++++ tools/perf/util/util.h | 3 +++ 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 8ee25bea1b24..00724d496d38 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -4,7 +4,6 @@ */ #include -#include #include "util.h" #include "debug.h" #include "llvm-utils.h" @@ -216,18 +215,19 @@ static int detect_kbuild_dir(char **kbuild_dir) const char *suffix_dir = ""; char *autoconf_path; - struct utsname utsname; int err; if (!test_dir) { - err = uname(&utsname); - if (err) { - pr_warning("uname failed: %s\n", strerror(errno)); + /* _UTSNAME_LENGTH is 65 */ + char release[128]; + + err = fetch_kernel_version(NULL, release, + sizeof(release)); + if (err) return -EINVAL; - } - test_dir = utsname.release; + test_dir = release; prefix_dir = "/lib/modules/"; suffix_dir = "/build"; } @@ -325,38 +325,18 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) pr_debug("include option is set to %s\n", *kbuild_include_opts); } -static unsigned long -fetch_kernel_version(void) -{ - struct utsname utsname; - int version, patchlevel, sublevel, err; - - if (uname(&utsname)) - return 0; - - err = sscanf(utsname.release, "%d.%d.%d", - &version, &patchlevel, &sublevel); - - if (err != 3) { - pr_debug("Unablt to get kernel version from uname '%s'\n", - utsname.release); - return 0; - } - - return (version << 16) + (patchlevel << 8) + sublevel; -} - int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz) { + size_t obj_buf_sz; + void *obj_buf = NULL; int err, nr_cpus_avail; - char clang_path[PATH_MAX], nr_cpus_avail_str[64]; + unsigned int kernel_version; char linux_version_code_str[64]; const char *clang_opt = llvm_param.clang_opt; - const char *template = llvm_param.clang_bpf_cmd_template; + char clang_path[PATH_MAX], nr_cpus_avail_str[64]; char *kbuild_dir = NULL, *kbuild_include_opts = NULL; - void *obj_buf = NULL; - size_t obj_buf_sz; + const char *template = llvm_param.clang_bpf_cmd_template; if (!template) template = CLANG_BPF_CMD_DEFAULT_TEMPLATE; @@ -388,8 +368,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", nr_cpus_avail); + if (fetch_kernel_version(&kernel_version, NULL, 0)) + kernel_version = 0; + snprintf(linux_version_code_str, sizeof(linux_version_code_str), - "0x%lx", fetch_kernel_version()); + "0x%x", kernel_version); force_set_env("NR_CPUS", nr_cpus_avail_str); force_set_env("LINUX_VERSION_CODE", linux_version_code_str); diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index cd12c25e4ea4..47b1e36c7ea0 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -3,6 +3,7 @@ #include "debug.h" #include #include +#include #ifdef HAVE_BACKTRACE_SUPPORT #include #endif @@ -665,3 +666,32 @@ bool find_process(const char *name) closedir(dir); return ret ? false : true; } + +int +fetch_kernel_version(unsigned int *puint, char *str, + size_t str_size) +{ + struct utsname utsname; + int version, patchlevel, sublevel, err; + + if (uname(&utsname)) + return -1; + + if (str && str_size) { + strncpy(str, utsname.release, str_size); + str[str_size - 1] = '\0'; + } + + err = sscanf(utsname.release, "%d.%d.%d", + &version, &patchlevel, &sublevel); + + if (err != 3) { + pr_debug("Unablt to get kernel version from uname '%s'\n", + utsname.release); + return -1; + } + + if (puint) + *puint = (version << 16) + (patchlevel << 8) + sublevel; + return 0; +} diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 4cfb913aa9e0..2665126267dc 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -350,4 +350,7 @@ static inline char *asprintf_expr_not_in_ints(const char *var, size_t nints, int int get_stack_size(const char *str, unsigned long *_size); +int fetch_kernel_version(unsigned int *puint, + char *str, size_t str_sz); + #endif /* GIT_COMPAT_UTIL_H */ -- cgit v1.2.3 From d3e0ce393057cfa907a0c4fe7b1ff56d5c30cca5 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:58:09 +0000 Subject: perf bpf: Improve BPF related error messages A series of bpf loader related error codes were introduced to help error reporting. Functions were improved to return these new error codes. Functions which return pointers were adjusted to encode error codes into return value using the ERR_PTR() interface. bpf_loader_strerror() was improved to convert these error messages to strings. It checks the error codes and calls libbpf_strerror() and strerror_r() accordingly, so caller don't need to consider checking the range of the error code. In bpf__strerror_load(), print kernel version of running kernel and the object's 'version' section to notify user how to fix his/her program. v1 -> v2: Use macro for error code. Fetch error message based on array index, eliminate for-loop. Print version strings. Before: # perf record -e ./test_kversion_nomatch_program.o sleep 1 event syntax error: './test_kversion_nomatch_program.o' \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object SKIP After: # perf record -e ./test_kversion_nomatch_program.o ls event syntax error: './test_kversion_nomatch_program.o' \___ 'version' (4.4.0) doesn't match running kernel (4.3.0) SKIP Signed-off-by: Wang Nan Tested-by: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446818289-87444-1-git-send-email-wangnan0@huawei.com [ Add 'static inline' to bpf__strerror_prepare_load() when LIBBPF is disabled ] Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/bpf-loader.c | 88 ++++++++++++++++++++++++++++++++++++------ tools/perf/util/bpf-loader.h | 23 +++++++++++ tools/perf/util/parse-events.c | 7 ++-- tools/perf/util/util.h | 5 +++ 4 files changed, 108 insertions(+), 15 deletions(-) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index c46256b1f5fd..e3afa1b60bb5 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -53,7 +53,7 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source) err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz); if (err) - return ERR_PTR(err); + return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE); obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename); free(obj_buf); } else @@ -113,14 +113,14 @@ config_bpf_program(struct bpf_program *prog) if (err < 0) { pr_debug("bpf: '%s' is not a valid config string\n", config_str); - err = -EINVAL; + err = -BPF_LOADER_ERRNO__CONFIG; goto errout; } if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) { pr_debug("bpf: '%s': group for event is set and not '%s'.\n", config_str, PERF_BPF_PROBE_GROUP); - err = -EINVAL; + err = -BPF_LOADER_ERRNO__GROUP; goto errout; } else if (!pev->group) pev->group = strdup(PERF_BPF_PROBE_GROUP); @@ -132,9 +132,9 @@ config_bpf_program(struct bpf_program *prog) } if (!pev->event) { - pr_debug("bpf: '%s': event name is missing\n", + pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n", config_str); - err = -EINVAL; + err = -BPF_LOADER_ERRNO__EVENTNAME; goto errout; } pr_debug("bpf: config '%s' is ok\n", config_str); @@ -285,7 +285,7 @@ int bpf__foreach_tev(struct bpf_object *obj, (void **)&priv); if (err || !priv) { pr_debug("bpf: failed to get private field\n"); - return -EINVAL; + return -BPF_LOADER_ERRNO__INTERNAL; } pev = &priv->pev; @@ -308,6 +308,18 @@ int bpf__foreach_tev(struct bpf_object *obj, return 0; } +#define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START) +#define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c) +#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START) + +static const char *bpf_loader_strerror_table[NR_ERRNO] = { + [ERRCODE_OFFSET(CONFIG)] = "Invalid config string", + [ERRCODE_OFFSET(GROUP)] = "Invalid group name", + [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string", + [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error", + [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet", +}; + static int bpf_loader_strerror(int err, char *buf, size_t size) { @@ -322,10 +334,21 @@ bpf_loader_strerror(int err, char *buf, size_t size) if (err >= __LIBBPF_ERRNO__START) return libbpf_strerror(err, buf, size); - msg = strerror_r(err, sbuf, sizeof(sbuf)); - snprintf(buf, size, "%s", msg); + if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) { + msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)]; + snprintf(buf, size, "%s", msg); + buf[size - 1] = '\0'; + return 0; + } + + if (err >= __BPF_LOADER_ERRNO__END) + snprintf(buf, size, "Unknown bpf loader error %d", err); + else + snprintf(buf, size, "%s", + strerror_r(err, sbuf, sizeof(sbuf))); + buf[size - 1] = '\0'; - return 0; + return -1; } #define bpf__strerror_head(err, buf, size) \ @@ -351,21 +374,62 @@ bpf_loader_strerror(int err, char *buf, size_t size) }\ buf[size - 1] = '\0'; +int bpf__strerror_prepare_load(const char *filename, bool source, + int err, char *buf, size_t size) +{ + size_t n; + int ret; + + n = snprintf(buf, size, "Failed to load %s%s: ", + filename, source ? " from source" : ""); + if (n >= size) { + buf[size - 1] = '\0'; + return 0; + } + buf += n; + size -= n; + + ret = bpf_loader_strerror(err, buf, size); + buf[size - 1] = '\0'; + return ret; +} + int bpf__strerror_probe(struct bpf_object *obj __maybe_unused, int err, char *buf, size_t size) { bpf__strerror_head(err, buf, size); bpf__strerror_entry(EEXIST, "Probe point exist. Try use 'perf probe -d \"*\"'"); - bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0\n"); - bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file\n"); + bpf__strerror_entry(EACCES, "You need to be root"); + bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0"); + bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file"); bpf__strerror_end(buf, size); return 0; } -int bpf__strerror_load(struct bpf_object *obj __maybe_unused, +int bpf__strerror_load(struct bpf_object *obj, int err, char *buf, size_t size) { bpf__strerror_head(err, buf, size); + case LIBBPF_ERRNO__KVER: { + unsigned int obj_kver = bpf_object__get_kversion(obj); + unsigned int real_kver; + + if (fetch_kernel_version(&real_kver, NULL, 0)) { + scnprintf(buf, size, "Unable to fetch kernel version"); + break; + } + + if (obj_kver != real_kver) { + scnprintf(buf, size, + "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")", + KVER_PARAM(obj_kver), + KVER_PARAM(real_kver)); + break; + } + + scnprintf(buf, size, "Failed to load program for unknown reason"); + break; + } bpf__strerror_end(buf, size); return 0; } diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index ccd8d7fd79d3..5eb3629eed8b 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -8,9 +8,21 @@ #include #include #include +#include #include "probe-event.h" #include "debug.h" +enum bpf_loader_errno { + __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100, + /* Invalid config string */ + BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START, + BPF_LOADER_ERRNO__GROUP, /* Invalid group name */ + BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */ + BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */ + BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */ + __BPF_LOADER_ERRNO__END, +}; + struct bpf_object; #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" @@ -19,6 +31,8 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, #ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load(const char *filename, bool source); +int bpf__strerror_prepare_load(const char *filename, bool source, + int err, char *buf, size_t size); void bpf__clear(void); @@ -67,6 +81,15 @@ __bpf_strerror(char *buf, size_t size) return 0; } +static inline +int bpf__strerror_prepare_load(const char *filename __maybe_unused, + bool source __maybe_unused, + int err __maybe_unused, + char *buf, size_t size) +{ + return __bpf_strerror(buf, size); +} + static inline int bpf__strerror_probe(struct bpf_object *obj __maybe_unused, int err __maybe_unused, diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index c75b25d5e28c..e48d9da75707 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -642,9 +642,10 @@ int parse_events_load_bpf(struct parse_events_evlist *data, snprintf(errbuf, sizeof(errbuf), "BPF support is not compiled"); else - snprintf(errbuf, sizeof(errbuf), - "BPF object file '%s' is invalid", - bpf_file_name); + bpf__strerror_prepare_load(bpf_file_name, + source, + -err, errbuf, + sizeof(errbuf)); data->error->help = strdup("(add -v to see detail)"); data->error->str = strdup(errbuf); diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 2665126267dc..dcc659017976 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -352,5 +352,10 @@ int get_stack_size(const char *str, unsigned long *_size); int fetch_kernel_version(unsigned int *puint, char *str, size_t str_sz); +#define KVER_VERSION(x) (((x) >> 16) & 0xff) +#define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff) +#define KVER_SUBLEVEL(x) ((x) & 0xff) +#define KVER_FMT "%d.%d.%d" +#define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x) #endif /* GIT_COMPAT_UTIL_H */ -- cgit v1.2.3 From b31de018a6284a25e0fdfeb028e724f8417ec3b1 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:49:41 +0000 Subject: perf test: Enhance the LLVM test: update basic BPF test program This patch replaces the original toy BPF program with the previously introduced bpf-script-example.c. Dynamically embeddeding it into 'llvm-src-base.c'. The newly introduced BPF program attaches a BPF program to 'sys_epoll_pwait()'. perf itself never use that syscall, so further test can verify their result with it. The program would generate 1 sample in every 2 calls of epoll_pwait() system call. Since the resulting BPF object is useful per se for further tests, test_llvm__fetch_bpf_obj() is introduced for creating BPF objects from source. The LLVM test was rewritten to use it. Committer note: Running it: [root@zoo wb]# perf test -v LLVM 35: Test LLVM searching and compiling : --- start --- test child forked, pid 17740 Kernel build dir is set to /lib/modules/4.3.0-rc1+/build set env: KBUILD_DIR=/lib/modules/4.3.0-rc1+/build unset env: KBUILD_OPTS include option is set to -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.9.2/include -I/home/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/git/linux/include -Iinclude -I/home/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/git/linux/include/uapi -Iinclude/generated/uapi -include /home/git/linux/include/linux/kconfig.h set env: NR_CPUS=4 set env: LINUX_VERSION_CODE=0x40300 set env: CLANG_EXEC=/usr/libexec/icecc/bin/clang set env: CLANG_OPTIONS=-xc set env: KERNEL_INC_OPTIONS= -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.9.2/include -I/home/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/git/linux/include -Iinclude -I/home/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/git/linux/include/uapi -Iinclude/generated/uapi -include /home/git/linux/include/linux/kconfig.h set env: WORKING_DIR=/lib/modules/4.3.0-rc1+/build set env: CLANG_SOURCE=- llvm compiling command template: echo '/* * bpf-script-example.c * Test basic LLVM building */ #ifndef LINUX_VERSION_CODE # error Need LINUX_VERSION_CODE # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' #endif #define BPF_ANY 0 #define BPF_MAP_TYPE_ARRAY 2 #define BPF_FUNC_map_lookup_elem 1 #define BPF_FUNC_map_update_elem 2 static void *(*bpf_map_lookup_elem)(void *map, void *key) = (void *) BPF_FUNC_map_lookup_elem; static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) = (void *) BPF_FUNC_map_update_elem; struct bpf_map_def { unsigned int type; unsigned int key_size; unsigned int value_size; unsigned int max_entries; }; #define SEC(NAME) __attribute__((section(NAME), used)) struct bpf_map_def SEC("maps") flip_table = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 1, }; SEC("func=sys_epoll_pwait") int bpf_func__sys_epoll_pwait(void *ctx) { int ind =0; int *flag = bpf_map_lookup_elem(&flip_table, &ind); int new_flag; if (!flag) return 0; /* flip flag and store back */ new_flag = !*flag; bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY); return new_flag; } char _license[] SEC("license") = "GPL"; int _version SEC("version") = LINUX_VERSION_CODE; ' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o - test child finished with 0 ---- end ---- Test LLVM searching and compiling: Ok [root@zoo wb]# Signed-off-by: Wang Nan Tested-by: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446817783-86722-6-git-send-email-wangnan0@huawei.com Signed-off-by: He Kuang Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/Build | 9 ++- tools/perf/tests/bpf-script-example.c | 4 ++ tools/perf/tests/llvm.c | 131 ++++++++++++++++++++++++++-------- tools/perf/tests/llvm.h | 16 +++++ 4 files changed, 129 insertions(+), 31 deletions(-) create mode 100644 tools/perf/tests/llvm.h diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 50de2253cff6..6c095b3d92a9 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -31,9 +31,16 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o +perf-y += llvm.o llvm-src-base.o perf-y += topology.o +$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c + $(call rule_mkdir) + $(Q)echo '#include ' > $@ + $(Q)echo 'const char test_llvm__bpf_base_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c index 410a70b93b93..0ec9c2c03164 100644 --- a/tools/perf/tests/bpf-script-example.c +++ b/tools/perf/tests/bpf-script-example.c @@ -1,3 +1,7 @@ +/* + * bpf-script-example.c + * Test basic LLVM building + */ #ifndef LINUX_VERSION_CODE # error Need LINUX_VERSION_CODE # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c index 8f713f6d32f3..05683c5f183e 100644 --- a/tools/perf/tests/llvm.c +++ b/tools/perf/tests/llvm.c @@ -2,6 +2,7 @@ #include #include #include +#include "llvm.h" #include "tests.h" #include "debug.h" @@ -11,16 +12,6 @@ static int perf_config_cb(const char *var, const char *val, return perf_default_config(var, val, arg); } -/* - * Randomly give it a "version" section since we don't really load it - * into kernel - */ -static const char test_bpf_prog[] = - "__attribute__((section(\"do_fork\"), used)) " - "int fork(void *ctx) {return 0;} " - "char _license[] __attribute__((section(\"license\"), used)) = \"GPL\";" - "int _version __attribute__((section(\"version\"), used)) = 0x40100;"; - #ifdef HAVE_LIBBPF_SUPPORT static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz) { @@ -28,25 +19,47 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz) obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL); if (IS_ERR(obj)) - return -1; + return TEST_FAIL; bpf_object__close(obj); - return 0; + return TEST_OK; } #else static int test__bpf_parsing(void *obj_buf __maybe_unused, size_t obj_buf_sz __maybe_unused) { pr_debug("Skip bpf parsing\n"); - return 0; + return TEST_OK; } #endif -int test__llvm(void) +static struct { + const char *source; + const char *desc; +} bpf_source_table[__LLVM_TESTCASE_MAX] = { + [LLVM_TESTCASE_BASE] = { + .source = test_llvm__bpf_base_prog, + .desc = "Basic BPF llvm compiling test", + }, +}; + + +int +test_llvm__fetch_bpf_obj(void **p_obj_buf, + size_t *p_obj_buf_sz, + enum test_llvm__testcase index, + bool force) { - char *tmpl_new, *clang_opt_new; - void *obj_buf; - size_t obj_buf_sz; - int err, old_verbose; + const char *source; + const char *desc; + const char *tmpl_old, *clang_opt_old; + char *tmpl_new = NULL, *clang_opt_new = NULL; + int err, old_verbose, ret = TEST_FAIL; + + if (index >= __LLVM_TESTCASE_MAX) + return TEST_FAIL; + + source = bpf_source_table[index].source; + desc = bpf_source_table[index].desc; perf_config(perf_config_cb, NULL); @@ -54,42 +67,100 @@ int test__llvm(void) * Skip this test if user's .perfconfig doesn't set [llvm] section * and clang is not found in $PATH, and this is not perf test -v */ - if (verbose == 0 && !llvm_param.user_set_param && llvm__search_clang()) { + if (!force && (verbose == 0 && + !llvm_param.user_set_param && + llvm__search_clang())) { pr_debug("No clang and no verbosive, skip this test\n"); return TEST_SKIP; } - old_verbose = verbose; /* * llvm is verbosity when error. Suppress all error output if * not 'perf test -v'. */ + old_verbose = verbose; if (verbose == 0) verbose = -1; + *p_obj_buf = NULL; + *p_obj_buf_sz = 0; + if (!llvm_param.clang_bpf_cmd_template) - return -1; + goto out; if (!llvm_param.clang_opt) llvm_param.clang_opt = strdup(""); - err = asprintf(&tmpl_new, "echo '%s' | %s", test_bpf_prog, - llvm_param.clang_bpf_cmd_template); + err = asprintf(&tmpl_new, "echo '%s' | %s%s", source, + llvm_param.clang_bpf_cmd_template, + old_verbose ? "" : " 2>/dev/null"); if (err < 0) - return -1; + goto out; err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt); if (err < 0) - return -1; + goto out; + tmpl_old = llvm_param.clang_bpf_cmd_template; llvm_param.clang_bpf_cmd_template = tmpl_new; + clang_opt_old = llvm_param.clang_opt; llvm_param.clang_opt = clang_opt_new; - err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz); + + err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz); + + llvm_param.clang_bpf_cmd_template = tmpl_old; + llvm_param.clang_opt = clang_opt_old; verbose = old_verbose; if (err) - return TEST_FAIL; + goto out; + + ret = TEST_OK; +out: + free(tmpl_new); + free(clang_opt_new); + if (ret != TEST_OK) + pr_debug("Failed to compile test case: '%s'\n", desc); + return ret; +} - err = test__bpf_parsing(obj_buf, obj_buf_sz); - free(obj_buf); - return err; +int test__llvm(void) +{ + enum test_llvm__testcase i; + + for (i = 0; i < __LLVM_TESTCASE_MAX; i++) { + int ret; + void *obj_buf = NULL; + size_t obj_buf_sz = 0; + + ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, + i, false); + + if (ret == TEST_OK) { + ret = test__bpf_parsing(obj_buf, obj_buf_sz); + if (ret != TEST_OK) + pr_debug("Failed to parse test case '%s'\n", + bpf_source_table[i].desc); + } + free(obj_buf); + + switch (ret) { + case TEST_SKIP: + return TEST_SKIP; + case TEST_OK: + break; + default: + /* + * Test 0 is the basic LLVM test. If test 0 + * fail, the basic LLVM support not functional + * so the whole test should fail. If other test + * case fail, it can be fixed by adjusting + * config so don't report error. + */ + if (i == 0) + return TEST_FAIL; + else + return TEST_SKIP; + } + } + return TEST_OK; } diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h new file mode 100644 index 000000000000..bd63cee687b5 --- /dev/null +++ b/tools/perf/tests/llvm.h @@ -0,0 +1,16 @@ +#ifndef PERF_TEST_LLVM_H +#define PERF_TEST_LLVM_H + +#include /* for size_t */ +#include /* for bool */ + +extern const char test_llvm__bpf_base_prog[]; + +enum test_llvm__testcase { + LLVM_TESTCASE_BASE, + __LLVM_TESTCASE_MAX, +}; + +int test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz, + enum test_llvm__testcase index, bool force); +#endif -- cgit v1.2.3 From 7af3f3d55b80cce40ad94b6b8e173dccedaf25e6 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:49:42 +0000 Subject: perf test: Enhance the LLVM tests: add kbuild test This patch adds a kbuild testcase to check whether kernel headers can be correctly found. For example: # mv /lib/modules/4.3.0-rc5{,.bak} # perf test LLVM 38: Test LLVM searching and compiling : Skip # perf test -v LLVM ... :11:10: fatal error: 'uapi/linux/fs.h' file not found #include ^ 1 error generated. ERROR: unable to compile - Hint: Check error message shown above. Hint: You can also pre-compile it into .o using: clang -target bpf -O2 -c - with proper -I and -D options. Failed to compile test case: 'Test kbuild searching' test child finished with -2 Signed-off-by: Wang Nan Tested-by: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446817783-86722-7-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/Build | 9 ++++++++- tools/perf/tests/bpf-script-test-kbuild.c | 21 +++++++++++++++++++++ tools/perf/tests/llvm.c | 4 ++++ tools/perf/tests/llvm.h | 2 ++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tools/perf/tests/bpf-script-test-kbuild.c diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 6c095b3d92a9..a47b21193fb2 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -31,7 +31,7 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o llvm-src-base.o +perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o perf-y += topology.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c @@ -41,6 +41,13 @@ $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ $(Q)echo ';' >> $@ +$(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c + $(call rule_mkdir) + $(Q)echo '#include ' > $@ + $(Q)echo 'const char test_llvm__bpf_test_kbuild_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif diff --git a/tools/perf/tests/bpf-script-test-kbuild.c b/tools/perf/tests/bpf-script-test-kbuild.c new file mode 100644 index 000000000000..3626924740d8 --- /dev/null +++ b/tools/perf/tests/bpf-script-test-kbuild.c @@ -0,0 +1,21 @@ +/* + * bpf-script-test-kbuild.c + * Test include from kernel header + */ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define SEC(NAME) __attribute__((section(NAME), used)) + +#include +#include + +SEC("func=vfs_llseek") +int bpf_func__vfs_llseek(void *ctx) +{ + return 0; +} + +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE; diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c index 05683c5f183e..bc4cf507cde5 100644 --- a/tools/perf/tests/llvm.c +++ b/tools/perf/tests/llvm.c @@ -40,6 +40,10 @@ static struct { .source = test_llvm__bpf_base_prog, .desc = "Basic BPF llvm compiling test", }, + [LLVM_TESTCASE_KBUILD] = { + .source = test_llvm__bpf_test_kbuild_prog, + .desc = "Test kbuild searching", + }, }; diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h index bd63cee687b5..d91d8f44efee 100644 --- a/tools/perf/tests/llvm.h +++ b/tools/perf/tests/llvm.h @@ -5,9 +5,11 @@ #include /* for bool */ extern const char test_llvm__bpf_base_prog[]; +extern const char test_llvm__bpf_test_kbuild_prog[]; enum test_llvm__testcase { LLVM_TESTCASE_BASE, + LLVM_TESTCASE_KBUILD, __LLVM_TESTCASE_MAX, }; -- cgit v1.2.3 From ba1fae431e74bb427a699187434142fd3fe98390 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 13:49:43 +0000 Subject: perf test: Add 'perf test BPF' This patch adds BPF testcase for testing BPF event filtering. By utilizing the result of 'perf test LLVM', this patch compiles the eBPF sample program then test its ability. The BPF script in 'perf test LLVM' lets only 50% samples generated by epoll_pwait() to be captured. This patch runs that system call for 111 times, so the result should contain 56 samples. Signed-off-by: Wang Nan Tested-by: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446817783-86722-8-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/Build | 1 + tools/perf/tests/bpf.c | 209 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/tests.h | 1 + tools/perf/util/bpf-loader.c | 24 ++++- tools/perf/util/bpf-loader.h | 10 ++ 6 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 tools/perf/tests/bpf.c diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index a47b21193fb2..f41ebf8849fe 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -32,6 +32,7 @@ perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o +perf-y += bpf.o perf-y += topology.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c diff --git a/tools/perf/tests/bpf.c b/tools/perf/tests/bpf.c new file mode 100644 index 000000000000..ec16f7812c8b --- /dev/null +++ b/tools/perf/tests/bpf.c @@ -0,0 +1,209 @@ +#include +#include +#include +#include +#include "tests.h" +#include "llvm.h" +#include "debug.h" +#define NR_ITERS 111 + +#ifdef HAVE_LIBBPF_SUPPORT + +static int epoll_pwait_loop(void) +{ + int i; + + /* Should fail NR_ITERS times */ + for (i = 0; i < NR_ITERS; i++) + epoll_pwait(-(i + 1), NULL, 0, 0, NULL); + return 0; +} + +static struct { + enum test_llvm__testcase prog_id; + const char *desc; + const char *name; + const char *msg_compile_fail; + const char *msg_load_fail; + int (*target_func)(void); + int expect_result; +} bpf_testcase_table[] = { + { + LLVM_TESTCASE_BASE, + "Test basic BPF filtering", + "[basic_bpf_test]", + "fix 'perf test LLVM' first", + "load bpf object failed", + &epoll_pwait_loop, + (NR_ITERS + 1) / 2, + }, +}; + +static int do_test(struct bpf_object *obj, int (*func)(void), + int expect) +{ + struct record_opts opts = { + .target = { + .uid = UINT_MAX, + .uses_mmap = true, + }, + .freq = 0, + .mmap_pages = 256, + .default_interval = 1, + }; + + char pid[16]; + char sbuf[STRERR_BUFSIZE]; + struct perf_evlist *evlist; + int i, ret = TEST_FAIL, err = 0, count = 0; + + struct parse_events_evlist parse_evlist; + struct parse_events_error parse_error; + + bzero(&parse_error, sizeof(parse_error)); + bzero(&parse_evlist, sizeof(parse_evlist)); + parse_evlist.error = &parse_error; + INIT_LIST_HEAD(&parse_evlist.list); + + err = parse_events_load_bpf_obj(&parse_evlist, &parse_evlist.list, obj); + if (err || list_empty(&parse_evlist.list)) { + pr_debug("Failed to add events selected by BPF\n"); + if (!err) + return TEST_FAIL; + } + + snprintf(pid, sizeof(pid), "%d", getpid()); + pid[sizeof(pid) - 1] = '\0'; + opts.target.tid = opts.target.pid = pid; + + /* Instead of perf_evlist__new_default, don't add default events */ + evlist = perf_evlist__new(); + if (!evlist) { + pr_debug("No ehough memory to create evlist\n"); + return TEST_FAIL; + } + + err = perf_evlist__create_maps(evlist, &opts.target); + if (err < 0) { + pr_debug("Not enough memory to create thread/cpu maps\n"); + goto out_delete_evlist; + } + + perf_evlist__splice_list_tail(evlist, &parse_evlist.list); + evlist->nr_groups = parse_evlist.nr_groups; + + perf_evlist__config(evlist, &opts); + + err = perf_evlist__open(evlist); + if (err < 0) { + pr_debug("perf_evlist__open: %s\n", + strerror_r(errno, sbuf, sizeof(sbuf))); + goto out_delete_evlist; + } + + err = perf_evlist__mmap(evlist, opts.mmap_pages, false); + if (err < 0) { + pr_debug("perf_evlist__mmap: %s\n", + strerror_r(errno, sbuf, sizeof(sbuf))); + goto out_delete_evlist; + } + + perf_evlist__enable(evlist); + (*func)(); + perf_evlist__disable(evlist); + + for (i = 0; i < evlist->nr_mmaps; i++) { + union perf_event *event; + + while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { + const u32 type = event->header.type; + + if (type == PERF_RECORD_SAMPLE) + count ++; + } + } + + if (count != expect) + pr_debug("BPF filter result incorrect\n"); + + ret = TEST_OK; + +out_delete_evlist: + perf_evlist__delete(evlist); + return ret; +} + +static struct bpf_object * +prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name) +{ + struct bpf_object *obj; + + obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name); + if (IS_ERR(obj)) { + pr_debug("Compile BPF program failed.\n"); + return NULL; + } + return obj; +} + +static int __test__bpf(int index) +{ + int ret; + void *obj_buf; + size_t obj_buf_sz; + struct bpf_object *obj; + + ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, + bpf_testcase_table[index].prog_id, + true); + if (ret != TEST_OK || !obj_buf || !obj_buf_sz) { + pr_debug("Unable to get BPF object, %s\n", + bpf_testcase_table[index].msg_compile_fail); + if (index == 0) + return TEST_SKIP; + else + return TEST_FAIL; + } + + obj = prepare_bpf(obj_buf, obj_buf_sz, + bpf_testcase_table[index].name); + if (!obj) { + ret = TEST_FAIL; + goto out; + } + + ret = do_test(obj, + bpf_testcase_table[index].target_func, + bpf_testcase_table[index].expect_result); +out: + bpf__clear(); + return ret; +} + +int test__bpf(void) +{ + unsigned int i; + int err; + + if (geteuid() != 0) { + pr_debug("Only root can run BPF test\n"); + return TEST_SKIP; + } + + for (i = 0; i < ARRAY_SIZE(bpf_testcase_table); i++) { + err = __test__bpf(i); + + if (err != TEST_OK) + return err; + } + + return TEST_OK; +} + +#else +int test__bpf(void) +{ + pr_debug("Skip BPF test because BPF support is not compiled\n"); + return TEST_SKIP; +} +#endif diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 66f72d3d6677..7b0120abc137 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -165,6 +165,10 @@ static struct test generic_tests[] = { .desc = "Test topology in session", .func = test_session_topology, }, + { + .desc = "Test BPF filter", + .func = test__bpf, + }, { .func = NULL, }, diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index c80486969f83..3c8734a3abbc 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -66,6 +66,7 @@ int test__fdarray__add(void); int test__kmod_path__parse(void); int test__thread_map(void); int test__llvm(void); +int test__bpf(void); int test_session_topology(void); #if defined(__arm__) || defined(__aarch64__) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index e3afa1b60bb5..4c50411371db 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -34,10 +34,32 @@ struct bpf_prog_priv { struct perf_probe_event pev; }; +static bool libbpf_initialized; + +struct bpf_object * +bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name) +{ + struct bpf_object *obj; + + if (!libbpf_initialized) { + libbpf_set_print(libbpf_warning, + libbpf_info, + libbpf_debug); + libbpf_initialized = true; + } + + obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name); + if (IS_ERR(obj)) { + pr_debug("bpf: failed to load buffer\n"); + return ERR_PTR(-EINVAL); + } + + return obj; +} + struct bpf_object *bpf__prepare_load(const char *filename, bool source) { struct bpf_object *obj; - static bool libbpf_initialized; if (!libbpf_initialized) { libbpf_set_print(libbpf_warning, diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index 5eb3629eed8b..9caf3ae4acf3 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -34,6 +34,9 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source); int bpf__strerror_prepare_load(const char *filename, bool source, int err, char *buf, size_t size); +struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, + const char *name); + void bpf__clear(void); int bpf__probe(struct bpf_object *obj); @@ -55,6 +58,13 @@ bpf__prepare_load(const char *filename __maybe_unused, return ERR_PTR(-ENOTSUP); } +static inline struct bpf_object * +bpf__prepare_load_buffer(void *obj_buf __maybe_unused, + size_t obj_buf_sz __maybe_unused) +{ + return ERR_PTR(-ENOTSUP); +} + static inline void bpf__clear(void) { } static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} -- cgit v1.2.3 From 345c99a303e1d97b407bf99190314a878d59ca92 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Fri, 6 Nov 2015 17:03:38 -0300 Subject: perf test: Do not be case sensitive when searching for matching tests Before: # perf test llvm # perf test LLVM 35: Test LLVM searching and compiling : Ok # After # perf test llvm 35: Test LLVM searching and compiling : Ok # Cc: Adrian Hunter Cc: David Ahern Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Link: http://lkml.kernel.org/n/tip-c1u05npqbf6epse17ovfejoj@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/builtin-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 7b0120abc137..80c442eab767 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -196,7 +196,7 @@ static bool perf_test__matches(struct test *test, int curr, int argc, const char continue; } - if (strstr(test->desc, argv[i])) + if (strcasestr(test->desc, argv[i])) return true; } -- cgit v1.2.3 From 2fd59077755c44dbbd9b2fa89cf988235a3a6a2b Mon Sep 17 00:00:00 2001 From: "Paul E. McKenney" Date: Wed, 4 Nov 2015 05:48:38 -0800 Subject: perf: Disable IRQs across RCU RS CS that acquires scheduler lock The perf_lock_task_context() function disables preemption across its RCU read-side critical section because that critical section acquires a scheduler lock. If there was a preemption during that RCU read-side critical section, the rcu_read_unlock() could attempt to acquire scheduler locks, resulting in deadlock. However, recent optimizations to expedited grace periods mean that IPI handlers that execute during preemptible RCU read-side critical sections can now cause the subsequent rcu_read_unlock() to acquire scheduler locks. Disabling preemption does nothiing to prevent these IPI handlers from executing, so these optimizations introduced a deadlock. In theory, this deadlock could be avoided by pulling all wakeups and printk()s out from rnp->lock critical sections, but in practice this would re-introduce some RCU CPU stall warning bugs. Given that acquiring scheduler locks entails disabling interrupts, these deadlocks can be avoided by disabling interrupts (instead of disabling preemption) across any RCU read-side critical that acquires scheduler locks and holds them across the rcu_read_unlock(). This commit therefore makes this change for perf_lock_task_context(). Reported-by: Dave Jones Reported-by: Peter Zijlstra Signed-off-by: Paul E. McKenney Signed-off-by: Peter Zijlstra (Intel) Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Linus Torvalds Cc: Stephane Eranian Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/20151104134838.GR29027@linux.vnet.ibm.com Signed-off-by: Ingo Molnar --- kernel/events/core.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index ea02109aee77..f8e5c443d74e 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1050,13 +1050,13 @@ retry: /* * One of the few rules of preemptible RCU is that one cannot do * rcu_read_unlock() while holding a scheduler (or nested) lock when - * part of the read side critical section was preemptible -- see + * part of the read side critical section was irqs-enabled -- see * rcu_read_unlock_special(). * * Since ctx->lock nests under rq->lock we must ensure the entire read - * side critical section is non-preemptible. + * side critical section has interrupts disabled. */ - preempt_disable(); + local_irq_save(*flags); rcu_read_lock(); ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); if (ctx) { @@ -1070,21 +1070,22 @@ retry: * if so. If we locked the right context, then it * can't get swapped on us any more. */ - raw_spin_lock_irqsave(&ctx->lock, *flags); + raw_spin_lock(&ctx->lock); if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { - raw_spin_unlock_irqrestore(&ctx->lock, *flags); + raw_spin_unlock(&ctx->lock); rcu_read_unlock(); - preempt_enable(); + local_irq_restore(*flags); goto retry; } if (!atomic_inc_not_zero(&ctx->refcount)) { - raw_spin_unlock_irqrestore(&ctx->lock, *flags); + raw_spin_unlock(&ctx->lock); ctx = NULL; } } rcu_read_unlock(); - preempt_enable(); + if (!ctx) + local_irq_restore(*flags); return ctx; } -- cgit v1.2.3 From b71b437eedaed985062492565d9d421d975ae845 Mon Sep 17 00:00:00 2001 From: Peter Zijlstra Date: Mon, 2 Nov 2015 10:50:51 +0100 Subject: perf: Fix inherited events vs. tracepoint filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Arnaldo reported that tracepoint filters seem to misbehave (ie. not apply) on inherited events. The fix is obvious; filters are only set on the actual (parent) event, use the normal pattern of using this parent event for filters. This is safe because each child event has a reference to it. Reported-by: Arnaldo Carvalho de Melo Tested-by: Arnaldo Carvalho de Melo Signed-off-by: Peter Zijlstra (Intel) Cc: Adrian Hunter Cc: Arnaldo Carvalho de Melo Cc: David Ahern Cc: Frédéric Weisbecker Cc: Jiri Olsa Cc: Jiri Olsa Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Thomas Gleixner Cc: Wang Nan Cc: stable@vger.kernel.org Link: http://lkml.kernel.org/r/20151102095051.GN17308@twins.programming.kicks-ass.net Signed-off-by: Ingo Molnar --- kernel/events/core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index f8e5c443d74e..98a4b9db7f37 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6909,6 +6909,10 @@ static int perf_tp_filter_match(struct perf_event *event, { void *record = data->raw->data; + /* only top level events have filters set */ + if (event->parent) + event = event->parent; + if (likely(!event->filter) || filter_match_preds(event->filter, record)) return 1; return 0; -- cgit v1.2.3 From 3d07b3bed8eac28bbb336cd9304f1974d9a425fb Mon Sep 17 00:00:00 2001 From: Yunlong Song Date: Mon, 9 Nov 2015 11:10:03 +0800 Subject: perf test: Add llvm-src-base.c and llvm-src-kbuild.c to .gitignore Commit b31de018a6284a25e0fdfeb028e724f8417ec3b1 ("perf test: Enhance the LLVM test: update basic BPF test program") dynamically creates file llvm-src-base.c during the perf building. Similarly, the commit 7af3f3d55b80cce40ad94b6b8e173dccedaf25e6 ("perf test: Enhance the LLVM tests: add kbuild test") dynamically creates file llvm-src-kbuild.c during the perf building. Add them to .gitignore. Signed-off-by: Yunlong Song Tested-by: Arnaldo Carvalho de Melo Cc: Adrian Hunter Cc: Alexei Starovoitov Cc: David Ahern Cc: Jiri Olsa Cc: Kan Liang Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Wang Nan Fixes: b31de018a628 ("perf test: Enhance the LLVM test: update basic BPF test program") Fixes: 7af3f3d55b80 ("perf test: Enhance the LLVM tests: add kbuild test") Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tools/perf/tests/.gitignore diff --git a/tools/perf/tests/.gitignore b/tools/perf/tests/.gitignore new file mode 100644 index 000000000000..489fc9ffbcb0 --- /dev/null +++ b/tools/perf/tests/.gitignore @@ -0,0 +1,2 @@ +llvm-src-base.c +llvm-src-kbuild.c -- cgit v1.2.3 From 4a4c03c1bb286bf0def7ae506bc076285f58d8b6 Mon Sep 17 00:00:00 2001 From: Michael Petlan Date: Mon, 9 Nov 2015 16:33:31 +0100 Subject: perf annotate: Support full source file paths for srcline fix The --full-paths option did not show the full source file paths in the 'perf annotate' tool, because the value of the option was not propagated into the related functions. With this patch the value of the --full-paths option is known to the function that composes the srcline string, so it prints the full path when necessary. Committer Note: This affects annotate when the --print-line option is used: # perf annotate -h 2>&1 | grep print-line -l, --print-line print matching source lines (may be slow) Looking just at the lines that should be affected by this change: Before: # perf annotate --print-line --full-paths --stdio fput | grep '\.[ch]:[0-9]\+' 94.44 atomic64_64.h:114 5.56 file_table.c:265 file_table.c:265 5.56 : ffffffff81219a00: callq ffffffff81769360 <__fentry__> atomic64_64.h:114 94.44 : ffffffff81219a05: lock decq 0x38(%rdi) After: # perf annotate --print-line --full-paths --stdio fput | grep '\.[ch]:[0-9]\+' 94.44 /home/git/linux/arch/x86/include/asm/atomic64_64.h:114 5.56 /home/git/linux/fs/file_table.c:265 /home/git/linux/fs/file_table.c:265 5.56 : ffffffff81219a00: callq ffffffff81769360 <__fentry__> /home/git/linux/arch/x86/include/asm/atomic64_64.h:114 94.44 : ffffffff81219a05: lock decq 0x38(%rdi) # Signed-off-by: Michael Petlan Tested-by: Arnaldo Carvalho de Melo Link: http://permalink.gmane.org/gmane.linux.kernel.perf.user/2365 Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/annotate.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index f2974da0185a..1dd1949b0e79 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1620,6 +1620,7 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map, len = symbol__size(sym); if (print_lines) { + srcline_full_filename = full_paths; symbol__get_source_line(sym, map, evsel, &source_line, len); print_summary(&source_line, dso->long_name); } -- cgit v1.2.3 From e87b49116dedba3464fd8d0ec9393b4841167334 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 9 Nov 2015 17:12:03 -0300 Subject: perf session: Add missing newlines to some pr_err() calls Before: [acme@zoo linux]$ perf evlist WARNING: The perf.data file's data size field is 0 which is unexpected. Was the 'perf record' command properly terminated? non matching sample_type[acme@zoo linux]$ After: [acme@zoo linux]$ perf evlist WARNING: The perf.data file's data size field is 0 which is unexpected. Was the 'perf record' command properly terminated? non matching sample_type [acme@zoo linux]$ Cc: Adrian Hunter Cc: David Ahern Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Link: http://lkml.kernel.org/n/tip-wscok3a2s7yrj8156oc2r6qe@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/session.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 428149bc64d2..c35ffdd360fe 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -29,7 +29,7 @@ static int perf_session__open(struct perf_session *session) struct perf_data_file *file = session->file; if (perf_session__read_header(session) < 0) { - pr_err("incompatible file format (rerun with -v to learn more)"); + pr_err("incompatible file format (rerun with -v to learn more)\n"); return -1; } @@ -37,17 +37,17 @@ static int perf_session__open(struct perf_session *session) return 0; if (!perf_evlist__valid_sample_type(session->evlist)) { - pr_err("non matching sample_type"); + pr_err("non matching sample_type\n"); return -1; } if (!perf_evlist__valid_sample_id_all(session->evlist)) { - pr_err("non matching sample_id_all"); + pr_err("non matching sample_id_all\n"); return -1; } if (!perf_evlist__valid_read_format(session->evlist)) { - pr_err("non matching read_format"); + pr_err("non matching read_format\n"); return -1; } -- cgit v1.2.3 From 421fd0845eaeecce6b3806f7f0c0d67d1f9ad108 Mon Sep 17 00:00:00 2001 From: Wang Nan Date: Fri, 6 Nov 2015 09:50:15 +0000 Subject: perf probe: Verify parameters in two functions On kernel with only one out of CONFIG_KPROBE_EVENTS and CONFIG_UPROBE_EVENTS enabled, 'perf probe -d' causes a segfault because perf_del_probe_events() calls probe_file__get_events() with a negative fd. This patch fixes it by adding parameter validation at the entry of probe_file__get_events() and probe_file__get_rawlist(). Since they are both non-static public functions (in .h file), parameter verifying is required. v1 -> v2: Verify fd at the head of probe_file__get_rawlist() instead of checking at call site (suggested by Masami and Arnaldo at [1,2]). [1] http://lkml.kernel.org/r/50399556C9727B4D88A595C8584AAB37526048E3@GSjpTKYDCembx32.service.hitachi.net [2] http://lkml.kernel.org/r/20151105155830.GV13236@kernel.org Signed-off-by: Wang Nan Acked-by: Masami Hiramatsu Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446803415-83382-1-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/probe-file.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c index 89dbeb92c68e..e3b3b92e4458 100644 --- a/tools/perf/util/probe-file.c +++ b/tools/perf/util/probe-file.c @@ -138,6 +138,9 @@ struct strlist *probe_file__get_rawlist(int fd) char *p; struct strlist *sl; + if (fd < 0) + return NULL; + sl = strlist__new(NULL, NULL); fp = fdopen(dup(fd), "r"); @@ -271,6 +274,9 @@ int probe_file__get_events(int fd, struct strfilter *filter, const char *p; int ret = -ENOENT; + if (!plist) + return -EINVAL; + namelist = __probe_file__get_namelist(fd, true); if (!namelist) return -ENOENT; -- cgit v1.2.3 From 5602ea09c19e85557f2b4d30be1d6ba349b7a038 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Wed, 11 Nov 2015 12:54:42 -0300 Subject: tools include: Add compiler.h to list.h list.h needs WRITE_ONCE() since 7f5f873c6a07 ("rculist: Use WRITE_ONCE() when deleting from reader-visible list") add it before including the kernel's list.h file. This fixes builds of 'make perf-tar-src-pkg' perf tool tarball builds, i.e. out of tree builds. Cc: Adrian Hunter Cc: David Ahern Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Link: http://lkml.kernel.org/n/tip-e0rb8f7jwz0jn24ttyick9u6@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/include/linux/list.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/include/linux/list.h b/tools/include/linux/list.h index 76b014c96893..a017f1595676 100644 --- a/tools/include/linux/list.h +++ b/tools/include/linux/list.h @@ -1,3 +1,4 @@ +#include #include #include -- cgit v1.2.3 From 41ac18ebfc429ce3f4d369ef07447d652999a0cd Mon Sep 17 00:00:00 2001 From: Huang Rui Date: Wed, 4 Nov 2015 17:43:53 +0800 Subject: perf/x86/intel/rapl: Remove the unused RAPL_EVENT_DESC() macro Signed-off-by: Huang Rui Cc: Andy Lutomirski Cc: Arnaldo Carvalho de Melo Cc: Borislav Petkov Cc: Dasaratharaman Chandramouli Cc: Fengguang Wu Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Tony Li Link: http://lkml.kernel.org/r/1446630233-3166-1-git-send-email-ray.huang@amd.com Signed-off-by: Ingo Molnar --- arch/x86/kernel/cpu/perf_event_intel_rapl.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c index 81431c0f0614..ed446bdcbf31 100644 --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c @@ -107,12 +107,6 @@ static ssize_t __rapl_##_var##_show(struct kobject *kobj, \ static struct kobj_attribute format_attr_##_var = \ __ATTR(_name, 0444, __rapl_##_var##_show, NULL) -#define RAPL_EVENT_DESC(_name, _config) \ -{ \ - .attr = __ATTR(_name, 0444, rapl_event_show, NULL), \ - .config = _config, \ -} - #define RAPL_CNTR_WIDTH 32 /* 32-bit rapl counters */ #define RAPL_EVENT_ATTR_STR(_name, v, str) \ -- cgit v1.2.3