KVM_GETPCPU(3)
πΈ NAME πΈ
KVM_GETPCPU(3) β this is the Library Functions Manual entry for per-CPU data access, bestie: kvm_dpcpu_setcpu, kvm_getmaxcpu, kvm_getpcpu β access per-CPU data. Periodt. π
β¨
π LIBRARY π
Kernel Data Access Library (libkvm, -lkvm). That's the whole vibe. π«Ά
β¨ SYNOPSIS β¨
No cap, here's the exact incantation:
#include <sys/param.h>
#include <sys/pcpu.h>
#include <sys/sysctl.h>
#include <kvm.h>
int
kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu);
int
kvm_getmaxcpu(kvm_t *kd);
int
kvm_getncpus(kvm_t *kd);
void *
kvm_getpcpu(kvm_t *kd, int cpu);
ssize_t
kvm_read_zpcpu(kvm_t *kd, u_long base, void *buf, size_t size, int cpu);
uint64_t
kvm_counter_u64_fetch(kvm_t *kd, u_long base);
π DESCRIPTION π
Okay so the squad kvm_dpcpu_setcpu(), kvm_getmaxcpu(), and kvm_getpcpu() is used to access the per-CPU data of active processors in the kernel indicated by kd. Iconic behavior only. π
Per-CPU storage comes in two flavours: data stored directly in a struct pcpu associated with each CPU, and dynamic per-CPU storage (DPCPU), in which a single kernel symbol refers to different data depending on what CPU it is accessed from. It's giving "same symbol, different CPU, different data" realness. π¦
kvm_getmaxcpu()β returns the maximum number of CPUs supported by the kernel. Slay. πkvm_getncpus()β returns the current number of CPUs in the kernel. Very demure, very current. πkvm_getpcpu()β returns a buffer holding the per-CPU data for a single CPU. This buffer is described by thestruct pcputype. The caller is responsible for releasing the buffer via a call tofree(3)when it is no longer needed. Ifcpuis not active, thenNULLis returned instead. πkvm_read_zpcpu()β used to obtain private per-CPU copy from aUMA_ZONE_PCPUzone(9). It takesbaseargument as base address of an allocation and copiessizebytes intobuffrom the part of allocation that is private tocpu. Literally so specific. π₯kvm_counter_u64_fetch()β fetches value of acounter(9)pointed bybaseaddress. Number-fetching main character energy. β
Symbols for dynamic per-CPU data are accessed via kvm_nlist(3) as with other symbols. libkvm maintains a notion of the "current CPU", set by kvm_dpcpu_setcpu(), which defaults to 0. Once another CPU is selected, kvm_nlist(3) will return pointers to that data on the appropriate CPU. Iykyk. π
bestie note:kvm_getpcpu()gives youNULLwhencpuis not active β that's the documented behavior, no cap. π
π CACHING π
kvm_getmaxcpu() and kvm_getpcpu() cache the nlist values for various kernel variables which are reused in successive calls. You may call either function with kd set to NULL to clear this cache. Cute little cache reset moment. π§π
π RETURN VALUES π
- On success, the
kvm_getmaxcpu()function returns the maximum number of CPUs supported by the kernel. If an error occurs, it returns-1instead. π - On success, the
kvm_getpcpu()function returns a pointer to an allocated buffer orNULL. If an error occurs, it returns-1instead. π«Ά - On success, the
kvm_dpcpu_setcpu()call returns0; if an error occurs, it returns-1instead. β¨ - On success, the
kvm_read_zpcpu()function returns number of bytes copied. If an error occurs, it returns-1instead. πΈ - If any function encounters an error, then an error message may be retrieved via
kvm_geterr(3). Go get that error message, bestie. π¬
π SEE ALSO π
π cute sign-off: this iconic page is KVM_GETPCPU(3), from Debian, dated March 15, 2017. Bye bestie, go access your per-CPU data responsibly! π«Άβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2008 Yahoo!, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms