Skip to content

Commit f21cb52

Browse files
namhyungacmel
authored andcommitted
perf stat: Support old kernels for bperf cgroup counting
The recent change in the cgroup will break the backward compatiblity in the BPF program. It should support both old and new kernels using BPF CO-RE technique. Like the task_struct->__state handling in the offcpu analysis, we can check the field name in the cgroup struct. Signed-off-by: Namhyung Kim <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Jiri Olsa <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Song Liu <[email protected]> Cc: Tejun Heo <[email protected]> Cc: [email protected] Cc: [email protected] Cc: zefan li <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 9c9155a commit f21cb52

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,40 @@ struct {
4343
__uint(value_size, sizeof(struct bpf_perf_event_value));
4444
} cgrp_readings SEC(".maps");
4545

46+
/* new kernel cgroup definition */
47+
struct cgroup___new {
48+
int level;
49+
struct cgroup *ancestors[];
50+
} __attribute__((preserve_access_index));
51+
52+
/* old kernel cgroup definition */
53+
struct cgroup___old {
54+
int level;
55+
u64 ancestor_ids[];
56+
} __attribute__((preserve_access_index));
57+
4658
const volatile __u32 num_events = 1;
4759
const volatile __u32 num_cpus = 1;
4860

4961
int enabled = 0;
5062
int use_cgroup_v2 = 0;
5163
int perf_subsys_id = -1;
5264

65+
static inline __u64 get_cgroup_v1_ancestor_id(struct cgroup *cgrp, int level)
66+
{
67+
/* recast pointer to capture new type for compiler */
68+
struct cgroup___new *cgrp_new = (void *)cgrp;
69+
70+
if (bpf_core_field_exists(cgrp_new->ancestors)) {
71+
return BPF_CORE_READ(cgrp_new, ancestors[level], kn, id);
72+
} else {
73+
/* recast pointer to capture old type for compiler */
74+
struct cgroup___old *cgrp_old = (void *)cgrp;
75+
76+
return BPF_CORE_READ(cgrp_old, ancestor_ids[level]);
77+
}
78+
}
79+
5380
static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
5481
{
5582
struct task_struct *p = (void *)bpf_get_current_task();
@@ -77,7 +104,7 @@ static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
77104
break;
78105

79106
// convert cgroup-id to a map index
80-
cgrp_id = BPF_CORE_READ(cgrp, ancestors[i], kn, id);
107+
cgrp_id = get_cgroup_v1_ancestor_id(cgrp, i);
81108
elem = bpf_map_lookup_elem(&cgrp_idx, &cgrp_id);
82109
if (!elem)
83110
continue;

0 commit comments

Comments
 (0)