]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man2/getcpu.2
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man2 / getcpu.2
CommitLineData
7c4ddad1 1.\" SPDX-License-Identifier: Linux-man-pages-1-para
2297bf0e 2.\"
7c4ddad1 3.\" This man page is Copyright (C) 2006 Andi Kleen <ak@muc.de>.
4366109b 4.\"
80997bc3 5.\" 2008, mtk, various edits
4784c377 6.\"
4c1c5274 7.TH getcpu 2 (date) "Linux man-pages (unreleased)"
80997bc3
MK
8.SH NAME
9getcpu \- determine CPU and NUMA node on which the calling thread is running
db24e10d
AC
10.SH LIBRARY
11Standard C library
8fc3b2cf 12.RI ( libc ", " \-lc )
80997bc3
MK
13.SH SYNOPSIS
14.nf
c9eec92c
MK
15.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16.B #include <sched.h>
c6d039a3 17.P
0a0b3f85
AC
18.BI "int getcpu(unsigned int *_Nullable " cpu ", \
19unsigned int *_Nullable " node );
80997bc3
MK
20.fi
21.SH DESCRIPTION
22The
23.BR getcpu ()
24system call identifies the processor and node on which the calling
25thread or process is currently running and writes them into the
26integers pointed to by the
27.I cpu
28and
29.I node
30arguments.
31The processor is a unique small integer identifying a CPU.
32The node is a unique small identifier identifying a NUMA node.
33When either
34.I cpu
35or
36.I node
37is NULL nothing is written to the respective pointer.
c6d039a3 38.P
80997bc3
MK
39The information placed in
40.I cpu
33a0ccb2 41is guaranteed to be current only at the time of the call:
80997bc3
MK
42unless the CPU affinity has been fixed using
43.BR sched_setaffinity (2),
44the kernel might change the CPU at any time.
45(Normally this does not happen
46because the scheduler tries to minimize movements between CPUs to
47keep caches hot, but it is possible.)
0ab8aeec 48The caller must allow for the possibility that the information returned in
80997bc3
MK
49.I cpu
50and
51.I node
ded312a3 52is no longer current by the time the call returns.
47297adb 53.SH RETURN VALUE
178df36f
MF
54On success, 0 is returned.
55On error, \-1 is returned, and
56.I errno
f6a4078b 57is set to indicate the error.
178df36f
MF
58.SH ERRORS
59.TP
60.B EFAULT
61Arguments point outside the calling process's address space.
3113c7f3 62.SH STANDARDS
4131356c
AC
63Linux.
64.SH HISTORY
65Linux 2.6.19 (x86-64 and i386),
66glibc 2.29.
c9eec92c
MK
67.\"
68.SS C library/kernel differences
69The kernel system call has a third argument:
c6d039a3 70.P
c9eec92c
MK
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
c6d039a3 77.P
80997bc3
MK
78The
79.I tcache
c9eec92c
MK
80argument is unused since Linux 2.6.24,
81and (when invoking the system call directly)
82should be specified as NULL,
83unless portability to Linux 2.6.23 or earlier is required.
c6d039a3 84.P
80997bc3
MK
85.\" commit 4307d1e5ada595c87f9a4d16db16ba5edb70dcb1
86.\" Author: Ingo Molnar <mingo@elte.hu>
87.\" Date: Wed Nov 7 18:37:48 2007 +0100
88.\" x86: ignore the sys_getcpu() tcache parameter
c9eec92c
MK
89In Linux 2.6.23 and earlier, if the
90.I tcache
91argument was non-NULL,
80997bc3
MK
92then it specified a pointer to a caller-allocated buffer in thread-local
93storage that was used to provide a caching mechanism for
94.BR getcpu ().
95Use of the cache could speed
96.BR getcpu ()
97calls, at the cost that there was a very small chance that
98the returned information would be out of date.
99The caching mechanism was considered to cause problems when
100migrating threads between CPUs, and so the argument is now ignored.
101.\"
b324e17d 102.\" ===== Before Linux 2.6.24: =====
80997bc3
MK
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.
b324e17d 118.\" The information placed in the cache can change between Linux releases.
c17a5c8b 119.\"
80997bc3
MK
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.
33a0ccb2 127.\" However, the cached information is updated only once per jiffy (see
80997bc3
MK
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.
4131356c
AC
133.SH NOTES
134Linux 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).)
137The intention of
138.BR getcpu ()
139is to allow programs to make optimizations with per-CPU data
140or for NUMA optimization.
80997bc3 141.SH SEE ALSO
80997bc3 142.BR mbind (2),
f0c34053 143.BR sched_setaffinity (2),
80997bc3 144.BR set_mempolicy (2),