namespaces.7: ffix
[man-pages.git] / man2 / getcpu.2
blob021cecedbe83547fcfb332a50d18ad42e21cb784
1 .\" This man page is Copyright (C) 2006 Andi Kleen <ak@muc.de>.
2 .\"
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.
8 .\" %%%LICENSE_END
9 .\"
10 .\" 2008, mtk, various edits
11 .\"
12 .TH GETCPU 2 2021-03-22 "Linux" "Linux Programmer's Manual"
13 .SH NAME
14 getcpu \- determine CPU and NUMA node on which the calling thread is running
15 .SH SYNOPSIS
16 .nf
17 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
18 .B #include <sched.h>
19 .PP
20 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node );
21 .fi
22 .SH DESCRIPTION
23 The
24 .BR getcpu ()
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
28 .I cpu
29 and
30 .I node
31 arguments.
32 The processor is a unique small integer identifying a CPU.
33 The node is a unique small identifier identifying a NUMA node.
34 When either
35 .I cpu
37 .I node
38 is NULL nothing is written to the respective pointer.
39 .PP
40 The information placed in
41 .I cpu
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
50 .I cpu
51 and
52 .I node
53 is no longer current by the time the call returns.
54 .SH RETURN VALUE
55 On success, 0 is returned.
56 On error, \-1 is returned, and
57 .I errno
58 is set to indicate the error.
59 .SH ERRORS
60 .TP
61 .B EFAULT
62 Arguments point outside the calling process's address space.
63 .SH VERSIONS
64 .BR getcpu ()
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
69 .BR syscall (2).)
70 .SH CONFORMING TO
71 .BR getcpu ()
72 is Linux-specific.
73 .SH NOTES
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
76 .BR vdso (7).)
77 The intention of
78 .BR getcpu ()
79 is to allow programs to make optimizations with per-CPU data
80 or for NUMA optimization.
81 .\"
82 .SS C library/kernel differences
83 The kernel system call has a third argument:
84 .PP
85 .in +4n
86 .nf
87 .BI "int getcpu(unsigned int *" cpu ", unsigned int *" node ,
88 .BI "           struct getcpu_cache *" tcache );
89 .fi
90 .in
91 .PP
92 The
93 .I 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.
98 .PP
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
104 .I tcache
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
108 .BR getcpu ().
109 Use of the cache could speed
110 .BR getcpu ()
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: =====
117 .\" .I tcache
118 .\" is a pointer to a
119 .\" .IR "struct getcpu_cache"
120 .\" that is used as a cache by
121 .\" .BR getcpu ().
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.
125 .\" .I tcache
126 .\" can be NULL.
127 .\" If it is not NULL
128 .\" .BR getcpu ()
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
135 .\" .BR getcpu ()
136 .\" will be slower,
137 .\" but always retrieve the current CPU and node information.
138 .\" With a cache
139 .\" .BR getcpu ()
140 .\" is faster.
141 .\" However, the cached information is updated only once per jiffy (see
142 .\" .BR time (7)).
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.
147 .SH SEE ALSO
148 .BR mbind (2),
149 .BR sched_setaffinity (2),
150 .BR set_mempolicy (2),
151 .BR sched_getcpu (3),
152 .BR cpuset (7),
153 .BR vdso (7)