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 2022-09-09 "Linux man-pages (unreleased)"
14 getcpu \- determine CPU and NUMA node on which the calling thread is running
17 .RI ( libc ", " \-lc )
20 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
23 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node );
28 system call identifies the processor and node on which the calling
29 thread or process is currently running and writes them into the
30 integers pointed to by the
35 The processor is a unique small integer identifying a CPU.
36 The node is a unique small identifier identifying a NUMA node.
41 is NULL nothing is written to the respective pointer.
43 The information placed in
45 is guaranteed to be current only at the time of the call:
46 unless the CPU affinity has been fixed using
47 .BR sched_setaffinity (2),
48 the kernel might change the CPU at any time.
49 (Normally this does not happen
50 because the scheduler tries to minimize movements between CPUs to
51 keep caches hot, but it is possible.)
52 The caller must allow for the possibility that the information returned in
56 is no longer current by the time the call returns.
58 On success, 0 is returned.
59 On error, \-1 is returned, and
61 is set to indicate the error.
65 Arguments point outside the calling process's address space.
68 was added in kernel 2.6.19 for x86-64 and i386.
69 Library support was added in glibc 2.29
70 (Earlier glibc versions did not provide a wrapper for this system call,
71 necessitating the use of
77 Linux makes a best effort to make this call as fast as possible.
78 (On some architectures, this is done via an implementation in the
82 is to allow programs to make optimizations with per-CPU data
83 or for NUMA optimization.
85 .SS C library/kernel differences
86 The kernel system call has a third argument:
90 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node ,
91 .BI " struct getcpu_cache *" tcache );
97 argument is unused since Linux 2.6.24,
98 and (when invoking the system call directly)
99 should be specified as NULL,
100 unless portability to Linux 2.6.23 or earlier is required.
102 .\" commit 4307d1e5ada595c87f9a4d16db16ba5edb70dcb1
103 .\" Author: Ingo Molnar <mingo@elte.hu>
104 .\" Date: Wed Nov 7 18:37:48 2007 +0100
105 .\" x86: ignore the sys_getcpu() tcache parameter
106 In Linux 2.6.23 and earlier, if the
108 argument was non-NULL,
109 then it specified a pointer to a caller-allocated buffer in thread-local
110 storage that was used to provide a caching mechanism for
112 Use of the cache could speed
114 calls, at the cost that there was a very small chance that
115 the returned information would be out of date.
116 The caching mechanism was considered to cause problems when
117 migrating threads between CPUs, and so the argument is now ignored.
119 .\" ===== Before kernel 2.6.24: =====
121 .\" is a pointer to a
122 .\" .IR "struct getcpu_cache"
123 .\" that is used as a cache by
125 .\" The caller should put the cache into a thread-local variable
126 .\" if the process is multithreaded,
127 .\" because the cache cannot be shared between different threads.
130 .\" If it is not NULL
132 .\" will use it to speed up operation.
133 .\" The information inside the cache is private to the system call
134 .\" and should not be accessed by the user program.
135 .\" The information placed in the cache can change between kernel releases.
137 .\" When no cache is specified
140 .\" but always retrieve the current CPU and node information.
144 .\" However, the cached information is updated only once per jiffy (see
146 .\" This means that the information could theoretically be out of date,
147 .\" although in practice the scheduler's attempt to maintain
148 .\" soft CPU affinity means that the information is unlikely to change
149 .\" over the course of the caching interval.
152 .BR sched_setaffinity (2),
153 .BR set_mempolicy (2),
154 .BR sched_getcpu (3),