1getcpu(2) System Calls Manual getcpu(2)
2
3
4
6 getcpu - determine CPU and NUMA node on which the calling thread is
7 running
8
10 Standard C library (libc, -lc)
11
13 #define _GNU_SOURCE /* See feature_test_macros(7) */
14 #include <sched.h>
15
16 int getcpu(unsigned int *_Nullable cpu, unsigned int *_Nullable node);
17
19 The getcpu() system call identifies the processor and node on which the
20 calling thread or process is currently running and writes them into the
21 integers pointed to by the cpu and node arguments. The processor is a
22 unique small integer identifying a CPU. The node is a unique small
23 identifier identifying a NUMA node. When either cpu or node is NULL
24 nothing is written to the respective pointer.
25
26 The information placed in cpu is guaranteed to be current only at the
27 time of the call: unless the CPU affinity has been fixed using
28 sched_setaffinity(2), the kernel might change the CPU at any time.
29 (Normally this does not happen because the scheduler tries to minimize
30 movements between CPUs to keep caches hot, but it is possible.) The
31 caller must allow for the possibility that the information returned in
32 cpu and node is no longer current by the time the call returns.
33
35 On success, 0 is returned. On error, -1 is returned, and errno is set
36 to indicate the error.
37
39 EFAULT Arguments point outside the calling process's address space.
40
42 Linux.
43
45 Linux 2.6.19 (x86-64 and i386), glibc 2.29.
46
47 C library/kernel differences
48 The kernel system call has a third argument:
49
50 int getcpu(unsigned int *cpu, unsigned int *node,
51 struct getcpu_cache *tcache);
52
53 The tcache argument is unused since Linux 2.6.24, and (when invoking
54 the system call directly) should be specified as NULL, unless portabilā
55 ity to Linux 2.6.23 or earlier is required.
56
57 In Linux 2.6.23 and earlier, if the tcache argument was non-NULL, then
58 it specified a pointer to a caller-allocated buffer in thread-local
59 storage that was used to provide a caching mechanism for getcpu(). Use
60 of the cache could speed getcpu() calls, at the cost that there was a
61 very small chance that the returned information would be out of date.
62 The caching mechanism was considered to cause problems when migrating
63 threads between CPUs, and so the argument is now ignored.
64
66 Linux makes a best effort to make this call as fast as possible. (On
67 some architectures, this is done via an implementation in the vdso(7).)
68 The intention of getcpu() is to allow programs to make optimizations
69 with per-CPU data or for NUMA optimization.
70
72 mbind(2), sched_setaffinity(2), set_mempolicy(2), sched_getcpu(3),
73 cpuset(7), vdso(7)
74
75
76
77Linux man-pages 6.04 2023-03-30 getcpu(2)