Debian Codegraph @

upstream/debian-trixie/man2/getcpu.2

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