Debian Codegraph @

upstream/debian-unstable/man2/getcpu.2

raw
1 .\" Copyright 2006, Andi Kleen <[email protected]>
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-1-para
5 .\"
6 .TH getcpu 2 2026-02-08 "Linux man-pages 6.18"
7 .SH NAME
8 getcpu \- determine CPU and NUMA node on which the calling thread is running
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ,\~ \-lc )
12 .SH SYNOPSIS
13 .nf
14 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
15 .B #include <sched.h>
16 .P
17 .BI "int getcpu(unsigned int *_Nullable " cpu ", unsigned int *_Nullable " node );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR getcpu ()
22 system call identifies the processor and node on which the calling
23 thread or process is currently running and writes them into the
24 integers pointed to by the
25 .I cpu
26 and
27 .I node
28 arguments.
29 The processor is a unique small integer identifying a CPU.
30 The node is a unique small identifier identifying a NUMA node.
31 When either
32 .I cpu
33 or
34 .I node
35 is NULL nothing is written to the respective pointer.
36 .P
37 The information placed in
38 .I cpu
39 is guaranteed to be current only at the time of the call:
40 unless the CPU affinity has been fixed using
41 .BR sched_setaffinity (2),
42 the kernel might change the CPU at any time.
43 (Normally this does not happen
44 because the scheduler tries to minimize movements between CPUs to
45 keep caches hot, but it is possible.)
46 The caller must allow for the possibility that the information returned in
47 .I cpu
48 and
49 .I node
50 is no longer current by the time the call returns.
51 .SH RETURN VALUE
52 On success, 0 is returned.
53 On error, \-1 is returned, and
54 .I errno
55 is set to indicate the error.
56 .SH ERRORS
57 .TP
58 .B EFAULT
59 Arguments point outside the calling process's address space.
60 .SH STANDARDS
61 Linux.
62 .SH HISTORY
63 Linux 2.6.19 (x86-64 and i386),
64 glibc 2.29.
65 .\"
66 .SS C library/kernel differences
67 The kernel system call has a third argument:
68 .P
69 .in +4n
70 .nf
71 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node ,
72 .BI " struct getcpu_cache *" tcache );
73 .fi
74 .in
75 .P
76 The
77 .I tcache
78 argument is unused since Linux 2.6.24,
79 and (when invoking the system call directly)
80 should be specified as NULL,
81 unless portability to Linux 2.6.23 or earlier is required.
82 .P
83 .\" commit 4307d1e5ada595c87f9a4d16db16ba5edb70dcb1
84 .\" Author: Ingo Molnar <[email protected]>
85 .\" Date: Wed Nov 7 18:37:48 2007 +0100
86 .\" x86: ignore the sys_getcpu() tcache parameter
87 In Linux 2.6.23 and earlier, if the
88 .I tcache
89 argument was non-NULL,
90 then it specified a pointer to a caller-allocated buffer in thread-local
91 storage that was used to provide a caching mechanism for
92 .BR getcpu ().
93 Use of the cache could speed
94 .BR getcpu ()
95 calls, at the cost that there was a very small chance that
96 the returned information would be out of date.
97 The caching mechanism was considered to cause problems when
98 migrating threads between CPUs, and so the argument is now ignored.
99 .\"
100 .\" ===== Before Linux 2.6.24: =====
101 .\" .I tcache
102 .\" is a pointer to a
103 .\" .IR "struct getcpu_cache"
104 .\" that is used as a cache by
105 .\" .BR getcpu ().
106 .\" The caller should put the cache into a thread-local variable
107 .\" if the process is multithreaded,
108 .\" because the cache cannot be shared between different threads.
109 .\" .I tcache
110 .\" can be NULL.
111 .\" If it is not NULL
112 .\" .BR getcpu ()
113 .\" will use it to speed up operation.
114 .\" The information inside the cache is private to the system call
115 .\" and should not be accessed by the user program.
116 .\" The information placed in the cache can change between Linux releases.
117 .\"
118 .\" When no cache is specified
119 .\" .BR getcpu ()
120 .\" will be slower,
121 .\" but always retrieve the current CPU and node information.
122 .\" With a cache
123 .\" .BR getcpu ()
124 .\" is faster.
125 .\" However, the cached information is updated only once per jiffy (see
126 .\" .BR time (7)).
127 .\" This means that the information could theoretically be out of date,
128 .\" although in practice the scheduler's attempt to maintain
129 .\" soft CPU affinity means that the information is unlikely to change
130 .\" over the course of the caching interval.
131 .SH NOTES
132 Linux makes a best effort to make this call as fast as possible.
133 (On some architectures, this is done via an implementation in the
134 .BR vdso (7).)
135 The intention of
136 .BR getcpu ()
137 is to allow programs to make optimizations with per-CPU data
138 or for NUMA optimization.
139 .SH SEE ALSO
140 .BR mbind (2),
141 .BR sched_setaffinity (2),
142 .BR set_mempolicy (2),
143 .BR sched_getcpu (3),
144 .BR cpuset (7),
145 .BR vdso (7)