1 .\" This man page is Copyright (C) 2006 Andi Kleen <ak@muc.de>.
3 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
4 .\" Permission is granted to distribute possibly modified copies
5 .\" of this page provided the header is included verbatim,
6 .\" and in case of nontrivial modification author and date
7 .\" of the modification is added to the header.
10 .\" 2008, mtk, various edits
12 .TH GETCPU 2 2021-03-22 "Linux" "Linux Programmer's Manual"
14 getcpu \- determine CPU and NUMA node on which the calling thread is running
17 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
20 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node );
25 system call identifies the processor and node on which the calling
26 thread or process is currently running and writes them into the
27 integers pointed to by the
32 The processor is a unique small integer identifying a CPU.
33 The node is a unique small identifier identifying a NUMA node.
38 is NULL nothing is written to the respective pointer.
40 The information placed in
42 is guaranteed to be current only at the time of the call:
43 unless the CPU affinity has been fixed using
44 .BR sched_setaffinity (2),
45 the kernel might change the CPU at any time.
46 (Normally this does not happen
47 because the scheduler tries to minimize movements between CPUs to
48 keep caches hot, but it is possible.)
49 The caller must allow for the possibility that the information returned in
53 is no longer current by the time the call returns.
55 On success, 0 is returned.
56 On error, \-1 is returned, and
58 is set to indicate the error.
62 Arguments point outside the calling process's address space.
65 was added in kernel 2.6.19 for x86-64 and i386.
66 Library support was added in glibc 2.29
67 (Earlier glibc versions did not provide a wrapper for this system call,
68 necessitating the use of
74 Linux makes a best effort to make this call as fast as possible.
75 (On some architectures, this is done via an implementation in the
79 is to allow programs to make optimizations with per-CPU data
80 or for NUMA optimization.
82 .SS C library/kernel differences
83 The kernel system call has a third argument:
87 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node ,
88 .BI " struct getcpu_cache *" tcache );
94 argument is unused since Linux 2.6.24,
95 and (when invoking the system call directly)
96 should be specified as NULL,
97 unless portability to Linux 2.6.23 or earlier is required.
99 .\" commit 4307d1e5ada595c87f9a4d16db16ba5edb70dcb1
100 .\" Author: Ingo Molnar <mingo@elte.hu>
101 .\" Date: Wed Nov 7 18:37:48 2007 +0100
102 .\" x86: ignore the sys_getcpu() tcache parameter
103 In Linux 2.6.23 and earlier, if the
105 argument was non-NULL,
106 then it specified a pointer to a caller-allocated buffer in thread-local
107 storage that was used to provide a caching mechanism for
109 Use of the cache could speed
111 calls, at the cost that there was a very small chance that
112 the returned information would be out of date.
113 The caching mechanism was considered to cause problems when
114 migrating threads between CPUs, and so the argument is now ignored.
116 .\" ===== Before kernel 2.6.24: =====
118 .\" is a pointer to a
119 .\" .IR "struct getcpu_cache"
120 .\" that is used as a cache by
122 .\" The caller should put the cache into a thread-local variable
123 .\" if the process is multithreaded,
124 .\" because the cache cannot be shared between different threads.
127 .\" If it is not NULL
129 .\" will use it to speed up operation.
130 .\" The information inside the cache is private to the system call
131 .\" and should not be accessed by the user program.
132 .\" The information placed in the cache can change between kernel releases.
134 .\" When no cache is specified
137 .\" but always retrieve the current CPU and node information.
141 .\" However, the cached information is updated only once per jiffy (see
143 .\" This means that the information could theoretically be out of date,
144 .\" although in practice the scheduler's attempt to maintain
145 .\" soft CPU affinity means that the information is unlikely to change
146 .\" over the course of the caching interval.
149 .BR sched_setaffinity (2),
150 .BR set_mempolicy (2),
151 .BR sched_getcpu (3),