getcpu(2)
β¨ getcpu(2) β the manual page, but make it cute β¨
okay bestie, so this one is from the System Calls Manual (that's the 2 in getcpu(2), iykyk π«Ά), and it's literally about finding out which CPU you're vibing on right now. let's go π
πΈ NAME πΈ
getcpu β figure out which CPU and which NUMA node the thread that's calling this is currently running on π¦
π LIBRARY π
Standard C library (that's libc, and yeah, link it with -lc) β¨
π SYNOPSIS π
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sched.h>
int getcpu(unsigned int *_Nullable cpu, unsigned int *_Nullable node);
no cap, that's the whole shape of it π
π DESCRIPTION π
so the getcpu() system call is the little system call that could: it figures out the processor and the node that the calling thread (or process) is currently running on, and writes those into the integers that cpu and node point at π§
- the processor is a unique small integer that identifies a CPU.
- the node is a unique small identifier that identifies a NUMA node.
and here's the cute part: if either cpu or node is NULL, then nothing gets written to that respective pointer. it just... doesn't touch it. respectful king behavior honestly π
β οΈ bestie listen. the info put incpuis guaranteed to be current only at the time of the call. unless the CPU affinity has been fixed usingsched_setaffinity(2), the kernel might change the CPU at any time. (normally this doesn't happen because the scheduler tries to minimize movements between CPUs to keep caches hot, but it TOTALLY is possible.) so the caller has to allow for the possibility that whatever came back incpuandnodeis already stale by the time the call returns. it's giving "screenshot at 2:47am" energy π
π₯ RETURN VALUE π₯
- success? you get
0back. slay. π - error? you get
-1, anderrnogets set to tell you what went wrong π
π ERRORS π
EFAULTβ the arguments point outside the calling process's address space. that's it, that's the only one listed. she's simple, she's iconic β¨
π³οΈπ STANDARDS π³οΈπ
Linux. that's the whole standard. just Linux π
π°οΈ HISTORY π°οΈ
- Linux 2.6.19 (on x86-64 and i386), glibc 2.29. vintage but make it fashion π
π§΅ C library/kernel differences π§΅
plot twist: the kernel system call actually has a third argument:
int getcpu(unsigned int *cpu, unsigned int *node,
struct getcpu_cache *tcache);
the tcache argument is unused since Linux 2.6.24, and (when you're invoking the system call directly) it should be specified as NULL, unless you actually need portability to Linux 2.6.23 or earlier. yes, earlier. we're going back in time for this one π³οΈπ
and in Linux 2.6.23 and earlier? if tcache was non-NULL, then it specified a pointer to a caller-allocated buffer in thread-local storage that was used to provide a caching mechanism for getcpu(). using the cache could speed up getcpu() calls, but at the cost that there was a very small chance that the returned information would be out of date. the caching mechanism was considered to cause problems when migrating threads between CPUs, and so the argument is now ignored. rip tcache, you would've loved being useful πͺ¦π
π NOTES π
Linux makes a best effort to make this call as fast as possible. (on some architectures, this is done via an implementation in the vdso(7). fancy π)
the intention of getcpu() is to let programs make optimizations with per-CPU data, or for NUMA optimization. main character energy: it exists so your code can be fast β‘
π SEE ALSO π
and that's the whole page, bestie π«Ά this was Linux man-pages 6.20, dated 2026-02-08, and the page itself is getcpu(2). go check your CPU, you're probably on a good one πβ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-1-para
- Copyright 2006, Andi Kleen <ak@muc.de>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms