Skip to content

Libbpf userspace function libbpf_num_possible_cpus

0.0.4

Is a helper function to get the number of possible CPUs that the host kernel supports and expects.

Definition

int libbpf_num_possible_cpus(void);

Return

Number of possible CPUs; or error code on failure.

Usage

This function is useful when working with per-CPU maps, as it allows you to allocate the right amount of memory for the values array or to know the max index of maps like BPF_MAP_TYPE_PERF_EVENT_ARRAY which are index based on CPU index.

Example

int ncpus = libbpf_num_possible_cpus();
if (ncpus < 0) {
        // error handling
}
long values[ncpus];
bpf_map_lookup_elem(per_cpu_map_fd, key, values);