xref: /freebsd/lib/libkvm/kvm_getpcpu.3 (revision 1f50ad62db8e9c3425ac0162be0645e7beeba1ce)
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.\"
29794a9a6cSJohn Baldwin.\" $FreeBSD$
30794a9a6cSJohn Baldwin.\"
31494deaf8SEnji Cooper.Dd March 15, 2017
32794a9a6cSJohn Baldwin.Dt KVM_GETPCPU 3
33794a9a6cSJohn Baldwin.Os
34794a9a6cSJohn Baldwin.Sh NAME
35494deaf8SEnji Cooper.Nm kvm_dpcpu_setcpu ,
36794a9a6cSJohn Baldwin.Nm kvm_getmaxcpu ,
37794a9a6cSJohn Baldwin.Nm kvm_getpcpu
38794a9a6cSJohn Baldwin.Nd access per-CPU data
39794a9a6cSJohn Baldwin.Sh LIBRARY
40794a9a6cSJohn Baldwin.Lb libkvm
41794a9a6cSJohn Baldwin.Sh SYNOPSIS
42794a9a6cSJohn Baldwin.In sys/param.h
43794a9a6cSJohn Baldwin.In sys/pcpu.h
44794a9a6cSJohn Baldwin.In sys/sysctl.h
45794a9a6cSJohn Baldwin.In kvm.h
46794a9a6cSJohn Baldwin.Ft int
47ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu "kvm_t *kd" "u_int cpu"
48ccd8bad0SRobert Watson.Ft int
49794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu "kvm_t *kd"
509292aad4SGleb Smirnoff.Ft int
519292aad4SGleb Smirnoff.Fn kvm_getncpus "kvm_t *kd"
52794a9a6cSJohn Baldwin.Ft void *
53794a9a6cSJohn Baldwin.Fn kvm_getpcpu "kvm_t *kd" "int cpu"
542a213404SGleb Smirnoff.Ft ssize_t
5519b5cffeSGleb Smirnoff.Fn kvm_read_zpcpu "kvm_t *kd" "u_long base" "void *buf" "size_t size" "int cpu"
562a213404SGleb Smirnoff.Ft uint64_t
572a213404SGleb Smirnoff.Fn kvm_counter_u64_fetch "kvm_t *kd" "u_long base"
58794a9a6cSJohn Baldwin.Sh DESCRIPTION
59794a9a6cSJohn BaldwinThe
60ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu ,
61ccd8bad0SRobert Watson.Fn kvm_getmaxcpu ,
62794a9a6cSJohn Baldwinand
63794a9a6cSJohn Baldwin.Fn kvm_getpcpu
64794a9a6cSJohn Baldwinfunctions are used to access the per-CPU data of active processors in the
65794a9a6cSJohn Baldwinkernel indicated by
66794a9a6cSJohn Baldwin.Fa kd .
67ccd8bad0SRobert WatsonPer-CPU storage comes in two flavours: data stored directly in a
68ccd8bad0SRobert Watson.Vt "struct pcpu"
69ccd8bad0SRobert Watsonassociated with each CPU, and dynamic per-CPU storage (DPCPU), in which a
70ccd8bad0SRobert Watsonsingle kernel symbol refers to different data depending on what CPU it is
71ccd8bad0SRobert Watsonaccessed from.
72ccd8bad0SRobert Watson.Pp
73794a9a6cSJohn BaldwinThe
74794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu
75794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel.
76ccd8bad0SRobert Watson.Pp
77794a9a6cSJohn BaldwinThe
789292aad4SGleb Smirnoff.Fn kvm_getncpus
799292aad4SGleb Smirnofffunction returns the current number of CPUs in the kernel.
809292aad4SGleb Smirnoff.Pp
819292aad4SGleb SmirnoffThe
82794a9a6cSJohn Baldwin.Fn kvm_getpcpu
83794a9a6cSJohn Baldwinfunction returns a buffer holding the per-CPU data for a single CPU.
84794a9a6cSJohn BaldwinThis buffer is described by the
85794a9a6cSJohn Baldwin.Vt "struct pcpu"
86794a9a6cSJohn Baldwintype.
87794a9a6cSJohn BaldwinThe caller is responsible for releasing the buffer via a call to
88794a9a6cSJohn Baldwin.Xr free 3
89794a9a6cSJohn Baldwinwhen it is no longer needed.
90794a9a6cSJohn BaldwinIf
91794a9a6cSJohn Baldwin.Fa cpu
92794a9a6cSJohn Baldwinis not active, then
93794a9a6cSJohn Baldwin.Dv NULL
94794a9a6cSJohn Baldwinis returned instead.
95ccd8bad0SRobert Watson.Pp
962a213404SGleb SmirnoffThe
972a213404SGleb Smirnoff.Fn kvm_read_zpcpu
982a213404SGleb Smirnofffunction is used to obtain private per-CPU copy from a
992a213404SGleb Smirnoff.Dv UMA_ZONE_PCPU
1002a213404SGleb Smirnoff.Xr zone 9 .
1012a213404SGleb SmirnoffIt takes
1022a213404SGleb Smirnoff.Fa base
1032a213404SGleb Smirnoffargument as base address of an allocation and copyies
1042a213404SGleb Smirnoff.Fa size
1052a213404SGleb Smirnoffbytes into
1062a213404SGleb Smirnoff.Fa buf
1072a213404SGleb Smirnofffrom the part of allocation that is private to
1082a213404SGleb Smirnoff.Fa cpu .
1092a213404SGleb Smirnoff.Pp
1102a213404SGleb SmirnoffThe
1112a213404SGleb Smirnoff.Fn kvm_counter_u64_fetch
1122a213404SGleb Smirnofffunction fetches value of a
1132a213404SGleb Smirnoff.Xr counter 9
1142a213404SGleb Smirnoffpointed by
1152a213404SGleb Smirnoff.Fa base
1162a213404SGleb Smirnoffaddress.
1172a213404SGleb Smirnoff.Pp
118ccd8bad0SRobert WatsonSymbols for dynamic per-CPU data are accessed via
119ccd8bad0SRobert Watson.Xr kvm_nlist 3
120ccd8bad0SRobert Watsonas with other symbols.
121ccd8bad0SRobert Watson.Nm libkvm
122ccd8bad0SRobert Watsonmaintains a notion of the "current CPU", set by
123*1f50ad62SEnji Cooper.Fn kvm_dpcpu_setcpu ,
124ccd8bad0SRobert Watsonwhich defaults to 0.
125ccd8bad0SRobert WatsonOnce another CPU is selected,
126ccd8bad0SRobert Watson.Xr kvm_nlist 3
127ccd8bad0SRobert Watsonwill return pointers to that data on the appropriate CPU.
128cd11f01eSJohn Baldwin.Sh CACHING
129ccd8bad0SRobert Watson.Fn kvm_getmaxcpu
130ccd8bad0SRobert Watsonand
13163d46d1dSUlrich Spörlein.Fn kvm_getpcpu
132ccd8bad0SRobert Watsoncache the nlist values for various kernel variables which are
133cd11f01eSJohn Baldwinreused in successive calls.
134cd11f01eSJohn BaldwinYou may call either function with
135cd11f01eSJohn Baldwin.Fa kd
136cd11f01eSJohn Baldwinset to
137cd11f01eSJohn Baldwin.Dv NULL
138cd11f01eSJohn Baldwinto clear this cache.
139794a9a6cSJohn Baldwin.Sh RETURN VALUES
140794a9a6cSJohn BaldwinOn success, the
141794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu
142794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel.
143794a9a6cSJohn BaldwinIf an error occurs,
144794a9a6cSJohn Baldwinit returns -1 instead.
145794a9a6cSJohn Baldwin.Pp
146794a9a6cSJohn BaldwinOn success, the
147794a9a6cSJohn Baldwin.Fn kvm_getpcpu
148794a9a6cSJohn Baldwinfunction returns a pointer to an allocated buffer or
149794a9a6cSJohn Baldwin.Dv NULL .
150794a9a6cSJohn BaldwinIf an error occurs,
151794a9a6cSJohn Baldwinit returns -1 instead.
152794a9a6cSJohn Baldwin.Pp
153ccd8bad0SRobert WatsonOn success, the
154ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu
155ccd8bad0SRobert Watsoncall returns 0; if an error occurs, it returns -1 instead.
156ccd8bad0SRobert Watson.Pp
1572a213404SGleb SmirnoffOn success, the
1582a213404SGleb Smirnoff.Fn kvm_read_zpcpu
1592a213404SGleb Smirnofffunction returns number of bytes copied.
1602a213404SGleb SmirnoffIf an error occurs, it returns -1 instead.
1612a213404SGleb Smirnoff.Pp
162ccd8bad0SRobert WatsonIf any function encounters an error,
163794a9a6cSJohn Baldwinthen an error message may be retrieved via
164794a9a6cSJohn Baldwin.Xr kvm_geterr 3 .
165794a9a6cSJohn Baldwin.Sh SEE ALSO
166794a9a6cSJohn Baldwin.Xr free 3 ,
1672a213404SGleb Smirnoff.Xr kvm 3 ,
1682a213404SGleb Smirnoff.Xr counter 9 ,
1692a213404SGleb Smirnoff.Xr zone 9
170