samples/bpf/cpustat_user.c
Source file repositories/reference/linux-study-clean/samples/bpf/cpustat_user.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/cpustat_user.c- Extension
.c- Size
- 5808 bytes
- Lines
- 252
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- 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
errno.hstdio.hstdlib.hsignal.hsched.hstring.hunistd.hfcntl.hlocale.hsys/types.hsys/stat.hsys/time.hsys/wait.hbpf/bpf.hbpf/libbpf.h
Detected Declarations
struct cpu_stat_datafunction cpu_stat_printfunction cpu_stat_updatefunction cpu_stat_inject_cpu_idle_eventfunction cpu_stat_inject_cpu_frequency_eventfunction int_exitfunction main
Annotated Snippet
struct cpu_stat_data {
unsigned long cstate[MAX_CSTATE_ENTRIES];
unsigned long pstate[MAX_PSTATE_ENTRIES];
};
static struct cpu_stat_data stat_data[MAX_CPU];
static void cpu_stat_print(void)
{
int i, j;
char state_str[sizeof("cstate-9")];
struct cpu_stat_data *data;
/* Clear screen */
printf("\033[2J");
/* Header */
printf("\nCPU states statistics:\n");
printf("%-10s ", "state(ms)");
for (i = 0; i < MAX_CSTATE_ENTRIES; i++) {
sprintf(state_str, "cstate-%d", i);
printf("%-11s ", state_str);
}
for (i = 0; i < MAX_PSTATE_ENTRIES; i++) {
sprintf(state_str, "pstate-%d", i);
printf("%-11s ", state_str);
}
printf("\n");
for (j = 0; j < MAX_CPU; j++) {
data = &stat_data[j];
printf("CPU-%-6d ", j);
for (i = 0; i < MAX_CSTATE_ENTRIES; i++)
printf("%-11lu ", data->cstate[i] / 1000000);
for (i = 0; i < MAX_PSTATE_ENTRIES; i++)
printf("%-11lu ", data->pstate[i] / 1000000);
printf("\n");
}
}
static void cpu_stat_update(int cstate_fd, int pstate_fd)
{
unsigned long key, value;
int c, i;
for (c = 0; c < MAX_CPU; c++) {
for (i = 0; i < MAX_CSTATE_ENTRIES; i++) {
key = c * MAX_CSTATE_ENTRIES + i;
bpf_map_lookup_elem(cstate_fd, &key, &value);
stat_data[c].cstate[i] = value;
}
for (i = 0; i < MAX_PSTATE_ENTRIES; i++) {
key = c * MAX_PSTATE_ENTRIES + i;
bpf_map_lookup_elem(pstate_fd, &key, &value);
stat_data[c].pstate[i] = value;
}
}
}
/*
* This function is copied from 'idlestat' tool function
* idlestat_wake_all() in idlestate.c.
*
* It sets the self running task affinity to cpus one by one so can wake up
* the specific CPU to handle scheduling; this results in all cpus can be
* waken up once and produce ftrace event 'trace_cpu_idle'.
*/
static int cpu_stat_inject_cpu_idle_event(void)
{
int rcpu, i, ret;
cpu_set_t cpumask;
cpu_set_t original_cpumask;
ret = sysconf(_SC_NPROCESSORS_CONF);
if (ret < 0)
return -1;
rcpu = sched_getcpu();
if (rcpu < 0)
return -1;
/* Keep track of the CPUs we will run on */
sched_getaffinity(0, sizeof(original_cpumask), &original_cpumask);
Annotation
- Immediate include surface: `errno.h`, `stdio.h`, `stdlib.h`, `signal.h`, `sched.h`, `string.h`, `unistd.h`, `fcntl.h`.
- Detected declarations: `struct cpu_stat_data`, `function cpu_stat_print`, `function cpu_stat_update`, `function cpu_stat_inject_cpu_idle_event`, `function cpu_stat_inject_cpu_frequency_event`, `function int_exit`, `function main`.
- Atlas domain: Support Tooling And Documentation / samples.
- 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.