Skip to content

Commit 685439a

Browse files
namhyungacmel
authored andcommitted
perf record: Add cgroup support for off-cpu profiling
This covers two different use cases. The first one is cgroup filtering given by -G/--cgroup option which controls the off-cpu profiling for tasks in the given cgroups only. The other use case is cgroup sampling which is enabled by --all-cgroups option and it adds PERF_SAMPLE_CGROUP to the sample_type to set the cgroup id of the task in the sample data. Example output. $ sudo perf record -a --off-cpu --all-cgroups sleep 1 $ sudo perf report --stdio -s comm,cgroup --call-graph=no ... # Samples: 144 of event 'offcpu-time' # Event count (approx.): 48452045427 # # Children Self Command Cgroup # ........ ........ ............... .......................................... # 61.57% 5.60% Chrome_ChildIOT /user.slice/user-657345.slice/user@657345.service/app.slice/... 29.51% 7.38% Web Content /user.slice/user-657345.slice/user@657345.service/app.slice/... 17.48% 1.59% Chrome_IOThread /user.slice/user-657345.slice/user@657345.service/app.slice/... 16.48% 4.12% pipewire-pulse /user.slice/user-657345.slice/user@657345.service/session.slice/... 14.48% 2.07% perf /user.slice/user-657345.slice/user@657345.service/app.slice/... 14.30% 7.15% CompositorTileW /user.slice/user-657345.slice/user@657345.service/app.slice/... 13.33% 6.67% Timer /user.slice/user-657345.slice/user@657345.service/app.slice/... ... Signed-off-by: Namhyung Kim <namhyung@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Acked-by: Ian Rogers <irogers@google.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Blake Jones <blakejones@google.com> Cc: Hao Luo <haoluo@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/20220518224725.742882-6-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent b36888f commit 685439a

4 files changed

Lines changed: 85 additions & 5 deletions

File tree

tools/perf/builtin-record.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ static int record__config_text_poke(struct evlist *evlist)
892892

893893
static int record__config_off_cpu(struct record *rec)
894894
{
895-
return off_cpu_prepare(rec->evlist, &rec->opts.target);
895+
return off_cpu_prepare(rec->evlist, &rec->opts.target, &rec->opts);
896896
}
897897

898898
static bool record__kcore_readable(struct machine *machine)

tools/perf/util/bpf_off_cpu.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include "util/evlist.h"
66
#include "util/off_cpu.h"
77
#include "util/perf-hooks.h"
8+
#include "util/record.h"
89
#include "util/session.h"
910
#include "util/target.h"
1011
#include "util/cpumap.h"
1112
#include "util/thread_map.h"
13+
#include "util/cgroup.h"
1214
#include <bpf/bpf.h>
1315

1416
#include "bpf_skel/off_cpu.skel.h"
@@ -24,6 +26,7 @@ struct off_cpu_key {
2426
u32 tgid;
2527
u32 stack_id;
2628
u32 state;
29+
u64 cgroup_id;
2730
};
2831

2932
union off_cpu_data {
@@ -116,10 +119,11 @@ static void check_sched_switch_args(void)
116119
}
117120
}
118121

119-
int off_cpu_prepare(struct evlist *evlist, struct target *target)
122+
int off_cpu_prepare(struct evlist *evlist, struct target *target,
123+
struct record_opts *opts)
120124
{
121125
int err, fd, i;
122-
int ncpus = 1, ntasks = 1;
126+
int ncpus = 1, ntasks = 1, ncgrps = 1;
123127

124128
if (off_cpu_config(evlist) < 0) {
125129
pr_err("Failed to config off-cpu BPF event\n");
@@ -143,6 +147,21 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target)
143147
bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
144148
}
145149

150+
if (evlist__first(evlist)->cgrp) {
151+
ncgrps = evlist->core.nr_entries - 1; /* excluding a dummy */
152+
bpf_map__set_max_entries(skel->maps.cgroup_filter, ncgrps);
153+
154+
if (!cgroup_is_v2("perf_event"))
155+
skel->rodata->uses_cgroup_v1 = true;
156+
}
157+
158+
if (opts->record_cgroup) {
159+
skel->rodata->needs_cgroup = true;
160+
161+
if (!cgroup_is_v2("perf_event"))
162+
skel->rodata->uses_cgroup_v1 = true;
163+
}
164+
146165
set_max_rlimit();
147166
check_sched_switch_args();
148167

@@ -178,6 +197,29 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target)
178197
}
179198
}
180199

200+
if (evlist__first(evlist)->cgrp) {
201+
struct evsel *evsel;
202+
u8 val = 1;
203+
204+
skel->bss->has_cgroup = 1;
205+
fd = bpf_map__fd(skel->maps.cgroup_filter);
206+
207+
evlist__for_each_entry(evlist, evsel) {
208+
struct cgroup *cgrp = evsel->cgrp;
209+
210+
if (cgrp == NULL)
211+
continue;
212+
213+
if (!cgrp->id && read_cgroup_id(cgrp) < 0) {
214+
pr_err("Failed to read cgroup id of %s\n",
215+
cgrp->name);
216+
goto out;
217+
}
218+
219+
bpf_map_update_elem(fd, &cgrp->id, &val, BPF_ANY);
220+
}
221+
}
222+
181223
err = off_cpu_bpf__attach(skel);
182224
if (err) {
183225
pr_err("Failed to attach off-cpu BPF skeleton\n");
@@ -275,6 +317,8 @@ int off_cpu_write(struct perf_session *session)
275317
/* calculate sample callchain data array length */
276318
n += len + 2;
277319
}
320+
if (sample_type & PERF_SAMPLE_CGROUP)
321+
data.array[n++] = key.cgroup_id;
278322
/* TODO: handle more sample types */
279323

280324
size = n * sizeof(u64);

tools/perf/util/bpf_skel/off_cpu.bpf.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct offcpu_key {
2626
__u32 tgid;
2727
__u32 stack_id;
2828
__u32 state;
29+
__u64 cgroup_id;
2930
};
3031

3132
struct {
@@ -63,6 +64,13 @@ struct {
6364
__uint(max_entries, 1);
6465
} task_filter SEC(".maps");
6566

67+
struct {
68+
__uint(type, BPF_MAP_TYPE_HASH);
69+
__uint(key_size, sizeof(__u64));
70+
__uint(value_size, sizeof(__u8));
71+
__uint(max_entries, 1);
72+
} cgroup_filter SEC(".maps");
73+
6674
/* old kernel task_struct definition */
6775
struct task_struct___old {
6876
long state;
@@ -71,8 +79,11 @@ struct task_struct___old {
7179
int enabled = 0;
7280
int has_cpu = 0;
7381
int has_task = 0;
82+
int has_cgroup = 0;
7483

7584
const volatile bool has_prev_state = false;
85+
const volatile bool needs_cgroup = false;
86+
const volatile bool uses_cgroup_v1 = false;
7687

7788
/*
7889
* Old kernel used to call it task_struct->state and now it's '__state'.
@@ -92,6 +103,18 @@ static inline int get_task_state(struct task_struct *t)
92103
return BPF_CORE_READ(t_old, state);
93104
}
94105

106+
static inline __u64 get_cgroup_id(struct task_struct *t)
107+
{
108+
struct cgroup *cgrp;
109+
110+
if (uses_cgroup_v1)
111+
cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_event_cgrp_id], cgroup);
112+
else
113+
cgrp = BPF_CORE_READ(t, cgroups, dfl_cgrp);
114+
115+
return BPF_CORE_READ(cgrp, kn, id);
116+
}
117+
95118
static inline int can_record(struct task_struct *t, int state)
96119
{
97120
/* kernel threads don't have user stack */
@@ -120,6 +143,15 @@ static inline int can_record(struct task_struct *t, int state)
120143
return 0;
121144
}
122145

146+
if (has_cgroup) {
147+
__u8 *ok;
148+
__u64 cgrp_id = get_cgroup_id(t);
149+
150+
ok = bpf_map_lookup_elem(&cgroup_filter, &cgrp_id);
151+
if (!ok)
152+
return 0;
153+
}
154+
123155
return 1;
124156
}
125157

@@ -156,6 +188,7 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *prev,
156188
.tgid = next->tgid,
157189
.stack_id = pelem->stack_id,
158190
.state = pelem->state,
191+
.cgroup_id = needs_cgroup ? get_cgroup_id(next) : 0,
159192
};
160193
__u64 delta = ts - pelem->timestamp;
161194
__u64 *total;

tools/perf/util/off_cpu.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
struct evlist;
55
struct target;
66
struct perf_session;
7+
struct record_opts;
78

89
#define OFFCPU_EVENT "offcpu-time"
910

1011
#ifdef HAVE_BPF_SKEL
11-
int off_cpu_prepare(struct evlist *evlist, struct target *target);
12+
int off_cpu_prepare(struct evlist *evlist, struct target *target,
13+
struct record_opts *opts);
1214
int off_cpu_write(struct perf_session *session);
1315
#else
1416
static inline int off_cpu_prepare(struct evlist *evlist __maybe_unused,
15-
struct target *target __maybe_unused)
17+
struct target *target __maybe_unused,
18+
struct record_opts *opts __maybe_unused)
1619
{
1720
return -1;
1821
}

0 commit comments

Comments
 (0)