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

perf stat: Ignore error thread when enabling system-wide --per-thread

If we execute 'perf stat --per-thread' with non-root account (even set
kernel.perf_event_paranoid = -1 yet), it reports the error:

jinyao@skl:~$ perf stat --per-thread
Error:
You may not have permission to collect system-wide stats.

Consider tweaking /proc/sys/kernel/perf_event_paranoid,
which controls use of the performance events system by
unprivileged users (without CAP_SYS_ADMIN).

The current value is 2:

-1: Allow use of (almost) all events by all users
Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
Disallow raw tracepoint access by users without CAP_SYS_ADMIN
>= 1: Disallow CPU event access by users without CAP_SYS_ADMIN
>= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN

To make this setting permanent, edit /etc/sysctl.conf too, e.g.:

kernel.perf_event_paranoid = -1

Perhaps the ptrace rule doesn't allow to trace some processes. But anyway
the global --per-thread mode had better ignore such errors and continue
working on other threads.

This patch will record the index of error thread in perf_evsel__open()
and remove this thread before retrying.

For example (run with non-root, kernel.perf_event_paranoid isn't set):

jinyao@skl:~$ perf stat --per-thread
^C
Performance counter stats for 'system wide':

vmstat-3458 6.171984 cpu-clock:u (msec) # 0.000 CPUs utilized
perf-3670 0.515599 cpu-clock:u (msec) # 0.000 CPUs utilized
vmstat-3458 1,163,643 cycles:u # 0.189 GHz
perf-3670 40,881 cycles:u # 0.079 GHz
vmstat-3458 1,410,238 instructions:u # 1.21 insn per cycle
perf-3670 3,536 instructions:u # 0.09 insn per cycle
vmstat-3458 288,937 branches:u # 46.814 M/sec
perf-3670 936 branches:u # 1.815 M/sec
vmstat-3458 15,195 branch-misses:u # 5.26% of all branches
perf-3670 76 branch-misses:u # 8.12% of all branches

12.651675247 seconds time elapsed

Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1516117388-10120-1-git-send-email-yao.jin@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jin Yao and committed by
Arnaldo Carvalho de Melo
ab6c79b8 853745f5

+18 -1
+13 -1
tools/perf/builtin-stat.c
··· 637 637 if (verbose > 0) 638 638 ui__warning("%s\n", msg); 639 639 goto try_again; 640 - } 640 + } else if (target__has_per_thread(&target) && 641 + evsel_list->threads && 642 + evsel_list->threads->err_thread != -1) { 643 + /* 644 + * For global --per-thread case, skip current 645 + * error thread. 646 + */ 647 + if (!thread_map__remove(evsel_list->threads, 648 + evsel_list->threads->err_thread)) { 649 + evsel_list->threads->err_thread = -1; 650 + goto try_again; 651 + } 652 + } 641 653 642 654 perf_evsel__open_strerror(counter, &target, 643 655 errno, msg, sizeof(msg));
+3
tools/perf/util/evsel.c
··· 1915 1915 goto fallback_missing_features; 1916 1916 } 1917 1917 out_close: 1918 + if (err) 1919 + threads->err_thread = thread; 1920 + 1918 1921 do { 1919 1922 while (--thread >= 0) { 1920 1923 close(FD(evsel, cpu, thread));
+1
tools/perf/util/thread_map.c
··· 32 32 size_t size = (nr - start) * sizeof(map->map[0]); 33 33 34 34 memset(&map->map[start], 0, size); 35 + map->err_thread = -1; 35 36 } 36 37 37 38 static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
+1
tools/perf/util/thread_map.h
··· 14 14 struct thread_map { 15 15 refcount_t refcnt; 16 16 int nr; 17 + int err_thread; 17 18 struct thread_map_data map[]; 18 19 }; 19 20