perf offcpu: Check process id for the given workload
authorNamhyung Kim <namhyung@kernel.org>
Thu, 11 Aug 2022 18:54:53 +0000 (11:54 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 11 Aug 2022 20:56:47 +0000 (17:56 -0300)
Current task filter checks task->pid which is different for each
thread.  But we want to profile all the threads in the process.  So
let's compare process id (or thread-group id: tgid) instead.

Before:
  $ sudo perf record --off-cpu -- perf bench sched messaging -t
  $ sudo perf report --stat | grep -A1 offcpu
  offcpu-time stats:
            SAMPLE events:        2

After:
  $ sudo perf record --off-cpu -- perf bench sched messaging -t
  $ sudo perf report --stat | grep -A1 offcpu
  offcpu-time stats:
            SAMPLE events:      850

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Blake Jones <blakejones@google.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Milian Wolff <milian.wolff@kdab.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/r/20220811185456.194721-2-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/bpf_off_cpu.c
tools/perf/util/bpf_skel/off_cpu.bpf.c

index f289b77..7dbcb02 100644 (file)
@@ -78,6 +78,7 @@ static void off_cpu_start(void *arg)
                u8 val = 1;
 
                skel->bss->has_task = 1;
+               skel->bss->uses_tgid = 1;
                fd = bpf_map__fd(skel->maps.task_filter);
                pid = perf_thread_map__pid(evlist->core.threads, 0);
                bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
index cc6d7fd..143a8b7 100644 (file)
@@ -85,6 +85,7 @@ int enabled = 0;
 int has_cpu = 0;
 int has_task = 0;
 int has_cgroup = 0;
+int uses_tgid = 0;
 
 const volatile bool has_prev_state = false;
 const volatile bool needs_cgroup = false;
@@ -144,7 +145,12 @@ static inline int can_record(struct task_struct *t, int state)
 
        if (has_task) {
                __u8 *ok;
-               __u32 pid = t->pid;
+               __u32 pid;
+
+               if (uses_tgid)
+                       pid = t->tgid;
+               else
+                       pid = t->pid;
 
                ok = bpf_map_lookup_elem(&task_filter, &pid);
                if (!ok)