]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getcpu.2
grantpt.3: SYNOPSIS: Explicitly show #define _XOPEN_SOURCE requirement
[thirdparty/man-pages.git] / man2 / getcpu.2
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 2019-03-06 "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 .B #include <linux/getcpu.h>
18 .PP
19 .BI "int getcpu(unsigned *" cpu ", unsigned *" node \
20 ", struct getcpu_cache *" tcache );
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
36 or
37 .I node
38 is NULL nothing is written to the respective pointer.
39 .PP
40 The third argument to this system call is nowadays unused,
41 and should be specified as NULL
42 unless portability to Linux 2.6.23 or earlier is required (see NOTES).
43 .PP
44 The information placed in
45 .I cpu
46 is guaranteed to be current only at the time of the call:
47 unless the CPU affinity has been fixed using
48 .BR sched_setaffinity (2),
49 the kernel might change the CPU at any time.
50 (Normally this does not happen
51 because the scheduler tries to minimize movements between CPUs to
52 keep caches hot, but it is possible.)
53 The caller must allow for the possibility that the information returned in
54 .I cpu
55 and
56 .I node
57 is no longer current by the time the call returns.
58 .SH RETURN VALUE
59 On success, 0 is returned.
60 On error, \-1 is returned, and
61 .I errno
62 is set appropriately.
63 .SH ERRORS
64 .TP
65 .B EFAULT
66 Arguments point outside the calling process's address space.
67 .SH VERSIONS
68 .BR getcpu ()
69 was added in kernel 2.6.19 for x86-64 and i386.
70 Library support was added in glibc 2.29
71 (Earlier glibc versions did not provide a wrapper for this system call,
72 necessitating the use of
73 .BR syscall (2).)
74 .SH CONFORMING TO
75 .BR getcpu ()
76 is Linux-specific.
77 .SH NOTES
78 Linux makes a best effort to make this call as fast as possible.
79 (On some architectures, this is done via an implementation in the
80 .BR vdso (7).)
81 The intention of
82 .BR getcpu ()
83 is to allow programs to make optimizations with per-CPU data
84 or for NUMA optimization.
85 .PP
86 The
87 .I tcache
88 argument is unused since Linux 2.6.24.
89 .\" commit 4307d1e5ada595c87f9a4d16db16ba5edb70dcb1
90 .\" Author: Ingo Molnar <mingo@elte.hu>
91 .\" Date: Wed Nov 7 18:37:48 2007 +0100
92 .\" x86: ignore the sys_getcpu() tcache parameter
93 In earlier kernels,
94 if this argument was non-NULL,
95 then it specified a pointer to a caller-allocated buffer in thread-local
96 storage that was used to provide a caching mechanism for
97 .BR getcpu ().
98 Use of the cache could speed
99 .BR getcpu ()
100 calls, at the cost that there was a very small chance that
101 the returned information would be out of date.
102 The caching mechanism was considered to cause problems when
103 migrating threads between CPUs, and so the argument is now ignored.
104 .\"
105 .\" ===== Before kernel 2.6.24: =====
106 .\" .I tcache
107 .\" is a pointer to a
108 .\" .IR "struct getcpu_cache"
109 .\" that is used as a cache by
110 .\" .BR getcpu ().
111 .\" The caller should put the cache into a thread-local variable
112 .\" if the process is multithreaded,
113 .\" because the cache cannot be shared between different threads.
114 .\" .I tcache
115 .\" can be NULL.
116 .\" If it is not NULL
117 .\" .BR getcpu ()
118 .\" will use it to speed up operation.
119 .\" The information inside the cache is private to the system call
120 .\" and should not be accessed by the user program.
121 .\" The information placed in the cache can change between kernel releases.
122 .\"
123 .\" When no cache is specified
124 .\" .BR getcpu ()
125 .\" will be slower,
126 .\" but always retrieve the current CPU and node information.
127 .\" With a cache
128 .\" .BR getcpu ()
129 .\" is faster.
130 .\" However, the cached information is updated only once per jiffy (see
131 .\" .BR time (7)).
132 .\" This means that the information could theoretically be out of date,
133 .\" although in practice the scheduler's attempt to maintain
134 .\" soft CPU affinity means that the information is unlikely to change
135 .\" over the course of the caching interval.
136 .SH SEE ALSO
137 .BR mbind (2),
138 .BR sched_setaffinity (2),
139 .BR set_mempolicy (2),
140 .BR sched_getcpu (3),
141 .BR cpuset (7),
142 .BR vdso (7)