xref: /freebsd/lib/libkvm/kvm_getpcpu.3 (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1.\" Copyright (c) 2008 Yahoo!, Inc.
2.\" All rights reserved.
3.\" Written by: John Baldwin <jhb@FreeBSD.org>
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\" 3. Neither the name of the author nor the names of any co-contributors
14.\"    may be used to endorse or promote products derived from this software
15.\"    without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.Dd March 15, 2017
30.Dt KVM_GETPCPU 3
31.Os
32.Sh NAME
33.Nm kvm_dpcpu_setcpu ,
34.Nm kvm_getmaxcpu ,
35.Nm kvm_getpcpu
36.Nd access per-CPU data
37.Sh LIBRARY
38.Lb libkvm
39.Sh SYNOPSIS
40.In sys/param.h
41.In sys/pcpu.h
42.In sys/sysctl.h
43.In kvm.h
44.Ft int
45.Fn kvm_dpcpu_setcpu "kvm_t *kd" "u_int cpu"
46.Ft int
47.Fn kvm_getmaxcpu "kvm_t *kd"
48.Ft int
49.Fn kvm_getncpus "kvm_t *kd"
50.Ft void *
51.Fn kvm_getpcpu "kvm_t *kd" "int cpu"
52.Ft ssize_t
53.Fn kvm_read_zpcpu "kvm_t *kd" "u_long base" "void *buf" "size_t size" "int cpu"
54.Ft uint64_t
55.Fn kvm_counter_u64_fetch "kvm_t *kd" "u_long base"
56.Sh DESCRIPTION
57The
58.Fn kvm_dpcpu_setcpu ,
59.Fn kvm_getmaxcpu ,
60and
61.Fn kvm_getpcpu
62functions are used to access the per-CPU data of active processors in the
63kernel indicated by
64.Fa kd .
65Per-CPU storage comes in two flavours: data stored directly in a
66.Vt "struct pcpu"
67associated with each CPU, and dynamic per-CPU storage (DPCPU), in which a
68single kernel symbol refers to different data depending on what CPU it is
69accessed from.
70.Pp
71The
72.Fn kvm_getmaxcpu
73function returns the maximum number of CPUs supported by the kernel.
74.Pp
75The
76.Fn kvm_getncpus
77function returns the current number of CPUs in the kernel.
78.Pp
79The
80.Fn kvm_getpcpu
81function returns a buffer holding the per-CPU data for a single CPU.
82This buffer is described by the
83.Vt "struct pcpu"
84type.
85The caller is responsible for releasing the buffer via a call to
86.Xr free 3
87when it is no longer needed.
88If
89.Fa cpu
90is not active, then
91.Dv NULL
92is returned instead.
93.Pp
94The
95.Fn kvm_read_zpcpu
96function is used to obtain private per-CPU copy from a
97.Dv UMA_ZONE_PCPU
98.Xr zone 9 .
99It takes
100.Fa base
101argument as base address of an allocation and copyies
102.Fa size
103bytes into
104.Fa buf
105from the part of allocation that is private to
106.Fa cpu .
107.Pp
108The
109.Fn kvm_counter_u64_fetch
110function fetches value of a
111.Xr counter 9
112pointed by
113.Fa base
114address.
115.Pp
116Symbols for dynamic per-CPU data are accessed via
117.Xr kvm_nlist 3
118as with other symbols.
119.Nm libkvm
120maintains a notion of the "current CPU", set by
121.Fn kvm_dpcpu_setcpu ,
122which defaults to 0.
123Once another CPU is selected,
124.Xr kvm_nlist 3
125will return pointers to that data on the appropriate CPU.
126.Sh CACHING
127.Fn kvm_getmaxcpu
128and
129.Fn kvm_getpcpu
130cache the nlist values for various kernel variables which are
131reused in successive calls.
132You may call either function with
133.Fa kd
134set to
135.Dv NULL
136to clear this cache.
137.Sh RETURN VALUES
138On success, the
139.Fn kvm_getmaxcpu
140function returns the maximum number of CPUs supported by the kernel.
141If an error occurs,
142it returns -1 instead.
143.Pp
144On success, the
145.Fn kvm_getpcpu
146function returns a pointer to an allocated buffer or
147.Dv NULL .
148If an error occurs,
149it returns -1 instead.
150.Pp
151On success, the
152.Fn kvm_dpcpu_setcpu
153call returns 0; if an error occurs, it returns -1 instead.
154.Pp
155On success, the
156.Fn kvm_read_zpcpu
157function returns number of bytes copied.
158If an error occurs, it returns -1 instead.
159.Pp
160If any function encounters an error,
161then an error message may be retrieved via
162.Xr kvm_geterr 3 .
163.Sh SEE ALSO
164.Xr free 3 ,
165.Xr kvm 3 ,
166.Xr counter 9 ,
167.Xr zone 9
168