Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

getcpu(2)

FieldValue
Numbers (x64 / i386 / arm64-generic / arm32)309 / 318 / 168 / 345 (cached kernel tables; hub agrees)
Since (kernel)2.6.19 for x86-64 and i386 (man VERSIONS); hub agrees
Man pageman2/getcpu.2 (local cache, dated 2021-03-22)
glibc wrappergetcpu@@GLIBC_2.29 (nm -D on glibc 2.41; man page agrees: wrapper added in glibc 2.29)
Aliases coverednone
Ledger IDSC-getcpu

Purpose

getcpu() identifies the CPU and NUMA node the calling thread is currently running on, writing small integers to cpu and node (either may be NULL to skip). The result is current only at call time: without a fixed sched_setaffinity(2) mask the scheduler may migrate the thread at any moment, so callers must treat the value as advisory. The intended uses are per-CPU data structures and NUMA placement optimizations.

Synopsis

#include <sched.h>
int getcpu(unsigned int *cpu, unsigned int *node);

Arguments and behavior

ArgumentBehavior
cpuReceives the CPU id; NULL skips.
nodeReceives the NUMA node id; NULL skips.

The raw kernel slot has a third argument, struct getcpu_cache *tcache, a caller-allocated thread-local cache: it was honored before Linux 2.6.24 and is now ignored and must be NULL (the glibc wrapper passes none).

Return value and errors

Returns 0 on success; -1 with errno: EFAULT (arguments point outside the address space).

Flags and constants

No flag arguments.

Security and notes

  • The kernel makes a best effort to make the call fast; on some architectures it is implemented in the vDSO, so the “syscall” may not trap at all (sched_getcpu(3) is the usual C library front door).
  • Any thread may call it; there is no capability or privilege gate.

References