xref: /freebsd/lib/libkvm/kvm_getpcpu.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1794a9a6cSJohn Baldwin.\" Copyright (c) 2008 Yahoo!, Inc.
2794a9a6cSJohn Baldwin.\" All rights reserved.
3794a9a6cSJohn Baldwin.\" Written by: John Baldwin <jhb@FreeBSD.org>
4794a9a6cSJohn Baldwin.\"
5794a9a6cSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without
6794a9a6cSJohn Baldwin.\" modification, are permitted provided that the following conditions
7794a9a6cSJohn Baldwin.\" are met:
8794a9a6cSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright
9794a9a6cSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer.
10794a9a6cSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright
11794a9a6cSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer in the
12794a9a6cSJohn Baldwin.\"    documentation and/or other materials provided with the distribution.
13794a9a6cSJohn Baldwin.\" 3. Neither the name of the author nor the names of any co-contributors
14794a9a6cSJohn Baldwin.\"    may be used to endorse or promote products derived from this software
15794a9a6cSJohn Baldwin.\"    without specific prior written permission.
16794a9a6cSJohn Baldwin.\"
17794a9a6cSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18794a9a6cSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19794a9a6cSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20794a9a6cSJohn Baldwin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21794a9a6cSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22794a9a6cSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23794a9a6cSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24794a9a6cSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25794a9a6cSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26794a9a6cSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27794a9a6cSJohn Baldwin.\" SUCH DAMAGE.
28794a9a6cSJohn Baldwin.\"
29494deaf8SEnji Cooper.Dd March 15, 2017
30794a9a6cSJohn Baldwin.Dt KVM_GETPCPU 3
31794a9a6cSJohn Baldwin.Os
32794a9a6cSJohn Baldwin.Sh NAME
33494deaf8SEnji Cooper.Nm kvm_dpcpu_setcpu ,
34794a9a6cSJohn Baldwin.Nm kvm_getmaxcpu ,
35794a9a6cSJohn Baldwin.Nm kvm_getpcpu
36794a9a6cSJohn Baldwin.Nd access per-CPU data
37794a9a6cSJohn Baldwin.Sh LIBRARY
38794a9a6cSJohn Baldwin.Lb libkvm
39794a9a6cSJohn Baldwin.Sh SYNOPSIS
40794a9a6cSJohn Baldwin.In sys/param.h
41794a9a6cSJohn Baldwin.In sys/pcpu.h
42794a9a6cSJohn Baldwin.In sys/sysctl.h
43794a9a6cSJohn Baldwin.In kvm.h
44794a9a6cSJohn Baldwin.Ft int
45ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu "kvm_t *kd" "u_int cpu"
46ccd8bad0SRobert Watson.Ft int
47794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu "kvm_t *kd"
489292aad4SGleb Smirnoff.Ft int
499292aad4SGleb Smirnoff.Fn kvm_getncpus "kvm_t *kd"
50794a9a6cSJohn Baldwin.Ft void *
51794a9a6cSJohn Baldwin.Fn kvm_getpcpu "kvm_t *kd" "int cpu"
522a213404SGleb Smirnoff.Ft ssize_t
5319b5cffeSGleb Smirnoff.Fn kvm_read_zpcpu "kvm_t *kd" "u_long base" "void *buf" "size_t size" "int cpu"
542a213404SGleb Smirnoff.Ft uint64_t
552a213404SGleb Smirnoff.Fn kvm_counter_u64_fetch "kvm_t *kd" "u_long base"
56794a9a6cSJohn Baldwin.Sh DESCRIPTION
57794a9a6cSJohn BaldwinThe
58ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu ,
59ccd8bad0SRobert Watson.Fn kvm_getmaxcpu ,
60794a9a6cSJohn Baldwinand
61794a9a6cSJohn Baldwin.Fn kvm_getpcpu
62794a9a6cSJohn Baldwinfunctions are used to access the per-CPU data of active processors in the
63794a9a6cSJohn Baldwinkernel indicated by
64794a9a6cSJohn Baldwin.Fa kd .
65ccd8bad0SRobert WatsonPer-CPU storage comes in two flavours: data stored directly in a
66ccd8bad0SRobert Watson.Vt "struct pcpu"
67ccd8bad0SRobert Watsonassociated with each CPU, and dynamic per-CPU storage (DPCPU), in which a
68ccd8bad0SRobert Watsonsingle kernel symbol refers to different data depending on what CPU it is
69ccd8bad0SRobert Watsonaccessed from.
70ccd8bad0SRobert Watson.Pp
71794a9a6cSJohn BaldwinThe
72794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu
73794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel.
74ccd8bad0SRobert Watson.Pp
75794a9a6cSJohn BaldwinThe
769292aad4SGleb Smirnoff.Fn kvm_getncpus
779292aad4SGleb Smirnofffunction returns the current number of CPUs in the kernel.
789292aad4SGleb Smirnoff.Pp
799292aad4SGleb SmirnoffThe
80794a9a6cSJohn Baldwin.Fn kvm_getpcpu
81794a9a6cSJohn Baldwinfunction returns a buffer holding the per-CPU data for a single CPU.
82794a9a6cSJohn BaldwinThis buffer is described by the
83794a9a6cSJohn Baldwin.Vt "struct pcpu"
84794a9a6cSJohn Baldwintype.
85794a9a6cSJohn BaldwinThe caller is responsible for releasing the buffer via a call to
86794a9a6cSJohn Baldwin.Xr free 3
87794a9a6cSJohn Baldwinwhen it is no longer needed.
88794a9a6cSJohn BaldwinIf
89794a9a6cSJohn Baldwin.Fa cpu
90794a9a6cSJohn Baldwinis not active, then
91794a9a6cSJohn Baldwin.Dv NULL
92794a9a6cSJohn Baldwinis returned instead.
93ccd8bad0SRobert Watson.Pp
942a213404SGleb SmirnoffThe
952a213404SGleb Smirnoff.Fn kvm_read_zpcpu
962a213404SGleb Smirnofffunction is used to obtain private per-CPU copy from a
972a213404SGleb Smirnoff.Dv UMA_ZONE_PCPU
982a213404SGleb Smirnoff.Xr zone 9 .
992a213404SGleb SmirnoffIt takes
1002a213404SGleb Smirnoff.Fa base
1012a213404SGleb Smirnoffargument as base address of an allocation and copyies
1022a213404SGleb Smirnoff.Fa size
1032a213404SGleb Smirnoffbytes into
1042a213404SGleb Smirnoff.Fa buf
1052a213404SGleb Smirnofffrom the part of allocation that is private to
1062a213404SGleb Smirnoff.Fa cpu .
1072a213404SGleb Smirnoff.Pp
1082a213404SGleb SmirnoffThe
1092a213404SGleb Smirnoff.Fn kvm_counter_u64_fetch
1102a213404SGleb Smirnofffunction fetches value of a
1112a213404SGleb Smirnoff.Xr counter 9
1122a213404SGleb Smirnoffpointed by
1132a213404SGleb Smirnoff.Fa base
1142a213404SGleb Smirnoffaddress.
1152a213404SGleb Smirnoff.Pp
116ccd8bad0SRobert WatsonSymbols for dynamic per-CPU data are accessed via
117ccd8bad0SRobert Watson.Xr kvm_nlist 3
118ccd8bad0SRobert Watsonas with other symbols.
119ccd8bad0SRobert Watson.Nm libkvm
120ccd8bad0SRobert Watsonmaintains a notion of the "current CPU", set by
121*1f50ad62SEnji Cooper.Fn kvm_dpcpu_setcpu ,
122ccd8bad0SRobert Watsonwhich defaults to 0.
123ccd8bad0SRobert WatsonOnce another CPU is selected,
124ccd8bad0SRobert Watson.Xr kvm_nlist 3
125ccd8bad0SRobert Watsonwill return pointers to that data on the appropriate CPU.
126cd11f01eSJohn Baldwin.Sh CACHING
127ccd8bad0SRobert Watson.Fn kvm_getmaxcpu
128ccd8bad0SRobert Watsonand
12963d46d1dSUlrich Spörlein.Fn kvm_getpcpu
130ccd8bad0SRobert Watsoncache the nlist values for various kernel variables which are
131cd11f01eSJohn Baldwinreused in successive calls.
132cd11f01eSJohn BaldwinYou may call either function with
133cd11f01eSJohn Baldwin.Fa kd
134cd11f01eSJohn Baldwinset to
135cd11f01eSJohn Baldwin.Dv NULL
136cd11f01eSJohn Baldwinto clear this cache.
137794a9a6cSJohn Baldwin.Sh RETURN VALUES
138794a9a6cSJohn BaldwinOn success, the
139794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu
140794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel.
141794a9a6cSJohn BaldwinIf an error occurs,
142794a9a6cSJohn Baldwinit returns -1 instead.
143794a9a6cSJohn Baldwin.Pp
144794a9a6cSJohn BaldwinOn success, the
145794a9a6cSJohn Baldwin.Fn kvm_getpcpu
146794a9a6cSJohn Baldwinfunction returns a pointer to an allocated buffer or
147794a9a6cSJohn Baldwin.Dv NULL .
148794a9a6cSJohn BaldwinIf an error occurs,
149794a9a6cSJohn Baldwinit returns -1 instead.
150794a9a6cSJohn Baldwin.Pp
151ccd8bad0SRobert WatsonOn success, the
152ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu
153ccd8bad0SRobert Watsoncall returns 0; if an error occurs, it returns -1 instead.
154ccd8bad0SRobert Watson.Pp
1552a213404SGleb SmirnoffOn success, the
1562a213404SGleb Smirnoff.Fn kvm_read_zpcpu
1572a213404SGleb Smirnofffunction returns number of bytes copied.
1582a213404SGleb SmirnoffIf an error occurs, it returns -1 instead.
1592a213404SGleb Smirnoff.Pp
160ccd8bad0SRobert WatsonIf any function encounters an error,
161794a9a6cSJohn Baldwinthen an error message may be retrieved via
162794a9a6cSJohn Baldwin.Xr kvm_geterr 3 .
163794a9a6cSJohn Baldwin.Sh SEE ALSO
164794a9a6cSJohn Baldwin.Xr free 3 ,
1652a213404SGleb Smirnoff.Xr kvm 3 ,
1662a213404SGleb Smirnoff.Xr counter 9 ,
1672a213404SGleb Smirnoff.Xr zone 9
168