tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c- Extension
.c- Size
- 4124 bytes
- Lines
- 157
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf/bpf_core_read.h
Detected Declarations
struct percpu_attach_counterstruct attach_counterfunction cgroup_idfunction create_percpu_attach_counterfunction create_attach_counterfunction BPF_PROGfunction BPF_PROGfunction BPF_PROG
Annotated Snippet
struct percpu_attach_counter {
/* Previous percpu state, to figure out if we have new updates */
__u64 prev;
/* Current percpu state */
__u64 state;
};
struct attach_counter {
/* State propagated through children, pending aggregation */
__u64 pending;
/* Total state, including all cpus and all children */
__u64 state;
};
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__uint(max_entries, 1024);
__type(key, __u64);
__type(value, struct percpu_attach_counter);
} percpu_attach_counters SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, __u64);
__type(value, struct attach_counter);
} attach_counters SEC(".maps");
extern void css_rstat_updated(
struct cgroup_subsys_state *css, int cpu) __ksym;
extern void css_rstat_flush(struct cgroup_subsys_state *css) __ksym;
static uint64_t cgroup_id(struct cgroup *cgrp)
{
return cgrp->kn->id;
}
static int create_percpu_attach_counter(__u64 cg_id, __u64 state)
{
struct percpu_attach_counter pcpu_init = {.state = state, .prev = 0};
return bpf_map_update_elem(&percpu_attach_counters, &cg_id,
&pcpu_init, BPF_NOEXIST);
}
static int create_attach_counter(__u64 cg_id, __u64 state, __u64 pending)
{
struct attach_counter init = {.state = state, .pending = pending};
return bpf_map_update_elem(&attach_counters, &cg_id,
&init, BPF_NOEXIST);
}
SEC("tp_btf/cgroup_attach_task")
int BPF_PROG(counter, struct cgroup *dst_cgrp, const char *path,
struct task_struct *task, bool threadgroup)
{
__u64 cg_id = cgroup_id(dst_cgrp);
struct percpu_attach_counter *pcpu_counter = bpf_map_lookup_elem(
&percpu_attach_counters,
&cg_id);
if (pcpu_counter)
pcpu_counter->state += 1;
else if (create_percpu_attach_counter(cg_id, 1))
return 0;
css_rstat_updated(&dst_cgrp->self, bpf_get_smp_processor_id());
return 0;
}
SEC("fentry/bpf_rstat_flush")
int BPF_PROG(flusher, struct cgroup *cgrp, struct cgroup *parent, int cpu)
{
struct percpu_attach_counter *pcpu_counter;
struct attach_counter *total_counter, *parent_counter;
__u64 cg_id = cgroup_id(cgrp);
__u64 parent_cg_id = parent ? cgroup_id(parent) : 0;
__u64 state;
__u64 delta = 0;
/* Add CPU changes on this level since the last flush */
pcpu_counter = bpf_map_lookup_percpu_elem(&percpu_attach_counters,
&cg_id, cpu);
if (pcpu_counter) {
state = pcpu_counter->state;
delta += state - pcpu_counter->prev;
pcpu_counter->prev = state;
}
Annotation
- Immediate include surface: `vmlinux.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf/bpf_core_read.h`.
- Detected declarations: `struct percpu_attach_counter`, `struct attach_counter`, `function cgroup_id`, `function create_percpu_attach_counter`, `function create_attach_counter`, `function BPF_PROG`, `function BPF_PROG`, `function BPF_PROG`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.