Skip to content

Commit

Permalink
bperf: Do not use BPF_CORE_READ (facebookincubator#196)
Browse files Browse the repository at this point in the history
Summary:

Use direct BTF access to be faster.

We need bpf_rdonly_cast() here, to cast "struct cgroup_subsys_state *"
to "struct cgroup *"

Reviewed By: anakryiko

Differential Revision: D51837046
  • Loading branch information
liu-song-6 authored and facebook-github-bot committed Dec 5, 2023
1 parent a2e5704 commit f12fdb8
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions hbt/src/perf_event/bpf/bperf_leader_cgroup.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ int cpu_cnt = 0;

#define PF_IDLE 0x00000002

void *bpf_rdonly_cast(void *, __u32) __ksym __weak;

static void update_cgroup_output(struct bpf_perf_event_value* diff_val) {
struct task_struct* task = bpf_get_current_task_btf();
struct cgroup* cgrp = BPF_CORE_READ(task, cgroups, dfl_cgrp);
struct cgroup* cgrp = task->cgroups->dfl_cgrp;
__u32 i, level;

/* sched_switch tracepoint is triggered before current task is updated.
Expand All @@ -65,10 +67,11 @@ static void update_cgroup_output(struct bpf_perf_event_value* diff_val) {
return;

for (level = 0; level < MAX_CGROUP_LEVELS; level++) {
__u64 id = BPF_CORE_READ(cgrp, kn, id);
__u64 id = cgrp->kn->id;
struct bpf_perf_event_value* val;

cgrp = (struct cgroup*)BPF_CORE_READ(cgrp, self.parent);
cgrp = bpf_rdonly_cast(cgrp->self.parent,
bpf_core_type_id_kernel(struct cgroup));
if (cgrp == NULL)
break;

Expand Down

0 comments on commit f12fdb8

Please sign in to comment.