在现代的服务器中,基本上 CPU 采用的都是多核 NUMA 架构。对于网络而言,一个网络包从物理网卡驱动出来之后,并到达对应的应用层 socket,最好都在同一个 CPU 上,因为这样的性能最好(没有内存 cache miss 的性能损耗)。
在 eBPF 中,有两个帮助函数能够获取 CPU ID 和 NUMA Node ID。
bpf-helpers 中能找到它们。
CPU: bpf_get_smp_processor_id
获取当前的处理器 ID,即 /proc/cpuinfo
里 process
number。不过,只有在内核线程调度的抢占功能被关闭的情况下,该函数才能够获取到稳定的 CPU ID;因为线程会被调度,eBPF 程序所在的线程会被调度到其它 CPU 上。
该函数的文档说明:
u32 bpf_get_smp_processor_id(void)
Description
Get the SMP (symmetric multiprocessing) processor
id. Note that all programs run with preemption
disabled, which means that the SMP processor id is
stable