xref: /openbsd-src/share/man/man9/kstat_create.9 (revision 139410fb3e68569c0361f09ac28029baf78c1fe9)
1*139410fbSjsg.\" $OpenBSD: kstat_create.9,v 1.7 2022/09/10 08:50:53 jsg Exp $
2b3ebe46dSdlg.\"
3b3ebe46dSdlg.\" Copyright (c) 2020 David Gwynne <dlg@openbsd.org>
4b3ebe46dSdlg.\"
5b3ebe46dSdlg.\" Permission to use, copy, modify, and distribute this software for any
6b3ebe46dSdlg.\" purpose with or without fee is hereby granted, provided that the above
7b3ebe46dSdlg.\" copyright notice and this permission notice appear in all copies.
8b3ebe46dSdlg.\"
9b3ebe46dSdlg.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10b3ebe46dSdlg.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11b3ebe46dSdlg.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12b3ebe46dSdlg.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13b3ebe46dSdlg.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14b3ebe46dSdlg.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15b3ebe46dSdlg.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16b3ebe46dSdlg.\"
17*139410fbSjsg.Dd $Mdocdate: September 10 2022 $
18b3ebe46dSdlg.Dt KSTAT_CREATE 9
19b3ebe46dSdlg.Os
20b3ebe46dSdlg.Sh NAME
21b3ebe46dSdlg.Nm kstat_create ,
22606807d4Sdlg.Nm kstat_read_nop ,
23606807d4Sdlg.Nm kstat_set_wlock ,
24606807d4Sdlg.Nm kstat_set_rlock ,
25606807d4Sdlg.Nm kstat_set_mutex ,
26b3ebe46dSdlg.Nm kstat_install ,
279aba907fSjmc.Nm kstat_remove ,
28b3ebe46dSdlg.Nm kstat_destroy
29b3ebe46dSdlg.Nd kernel statistics provider API
30b3ebe46dSdlg.Sh SYNOPSIS
31b3ebe46dSdlg.In sys/kstat.h
32b3ebe46dSdlg.Ft struct kstat *
337592d7efSschwarze.Fo kstat_create
34b3ebe46dSdlg.Fa "const char *provider"
35b3ebe46dSdlg.Fa "unsigned int instance"
36b3ebe46dSdlg.Fa "const char *name"
37b3ebe46dSdlg.Fa "unsigned int unit"
38b3ebe46dSdlg.Fa "unsigned int type"
39b3ebe46dSdlg.Fa "unsigned int flags"
40b3ebe46dSdlg.Fc
41606807d4Sdlg.Ft int
42606807d4Sdlg.Fn kstat_read_nop "struct kstat *ks"
43606807d4Sdlg.Ft void
44606807d4Sdlg.Fn kstat_set_wlock "struct kstat *ks" "struct rwlock *rwl"
45606807d4Sdlg.Ft void
46606807d4Sdlg.Fn kstat_set_rlock "struct kstat *ks" "struct rwlock *rwl"
47606807d4Sdlg.Ft void
48606807d4Sdlg.Fn kstat_set_mutex "struct kstat *ks" "struct mutex *mtx"
49b3ebe46dSdlg.Ft void
50b3ebe46dSdlg.Fn kstat_install "struct kstat *ks"
51b3ebe46dSdlg.Ft void
52a2ae35ffSdlg.Fn kstat_remove "struct kstat *ks"
53a2ae35ffSdlg.Ft void
54b3ebe46dSdlg.Fn kstat_destroy "struct kstat *ks"
55b3ebe46dSdlg.Sh DESCRIPTION
56606807d4SdlgKernel subsystems can provide statistics to userland using the kernel
57b3ebe46dSdlgstatistics (kstat) API.
58b3ebe46dSdlg.Pp
59b3ebe46dSdlgA kstat is uniquely identified by a tuple made up of the
60b3ebe46dSdlg.Fa provider ,
61b3ebe46dSdlg.Fa instances ,
62b3ebe46dSdlg.Fa name ,
63b3ebe46dSdlgand
64b3ebe46dSdlg.Fa unit
65b3ebe46dSdlgarguments.
66606807d4Sdlg.\" Once created, the kstat API allocates a unique 64bit identifier for
67606807d4Sdlg.\" the kstat.
68606807d4Sdlg.Pp
69606807d4SdlgThe information exported by a kstat is typed.
70b3ebe46dSdlgThe supported kstat types are
71b3ebe46dSdlg.Bl -tag -width xxx -offset indent
72b3ebe46dSdlg.It Dv KSTAT_T_RAW
73b3ebe46dSdlgThe kstat provides raw bytes.
74b3ebe46dSdlg.It Dv KSTAT_T_KV
75b3ebe46dSdlgThe kstat provides a series of
76b3ebe46dSdlg.Vt struct kstat_kv
77b3ebe46dSdlgstructures that represent key/value information.
78b3ebe46dSdlgSee
79b3ebe46dSdlg.Xr kstat_kv_init 9
80b3ebe46dSdlgfor more detail.
81b3ebe46dSdlg.El
82b3ebe46dSdlg.Pp
83606807d4SdlgBelow is a simplified version of the
84606807d4Sdlg.Vt kstat
85606807d4Sdlgstructure that shows the fields that a subsystem operates on:
86606807d4Sdlg.Bd -literal
87606807d4Sdlgstruct kstat {
88606807d4Sdlg	void			 *ks_softc;
89606807d4Sdlg	void			 *ks_ptr;
90606807d4Sdlg
91606807d4Sdlg	void			 *ks_data;
92606807d4Sdlg	size_t			  ks_datalen;
93606807d4Sdlg	struct timespec		  ks_updated;
94606807d4Sdlg
95606807d4Sdlg	int			(*ks_read)(struct kstat *ks);
96606807d4Sdlg	int			(*ks_copy)(struct kstat *ks, void *dst);
97606807d4Sdlg
98606807d4Sdlg	const struct kstat_lock_ops *
99606807d4Sdlg				  ks_lock_ops;
100606807d4Sdlg	void			 *ks_lock;
101606807d4Sdlg};
102606807d4Sdlg.Ed
103606807d4Sdlg.Pp
104606807d4SdlgThe
105606807d4Sdlg.Fa ks_softc
106606807d4Sdlgand
107606807d4Sdlg.Fa ks_ptr
108606807d4Sdlgfields are available for the subsystem providing the kstat to use.
109606807d4SdlgFor example, if a hardware device driver is providing a kstat then
110606807d4Sdlg.Fa ks_softc
111606807d4Sdlgcan be initialised with a reference to the softc structure allocated
112606807d4Sdlgfor that device driver.
113606807d4Sdlg.Fa ks_ptr
114606807d4Sdlgis intended for use by a subsystem to refer to data or state that
115606807d4Sdlgis only needed when providing the kstat which would not otherwise
116606807d4Sdlgbe referenced by the provider.
117606807d4Sdlg.Pp
118606807d4SdlgThe
119606807d4Sdlg.Fa ks_datalen
120606807d4Sdlgfield specifies how much data is exported by the kstat to userland.
121606807d4Sdlg.Pp
122606807d4Sdlg.Fa ks_updated
123606807d4Sdlgis set by the provider to the system uptime when the kstat data was
124606807d4Sdlgupdated.
125606807d4Sdlg.Pp
126606807d4Sdlg.Fa ks_data
127606807d4Sdlgmay be set to a data buffer used to store the kstat data payload.
128606807d4Sdlg.Pp
129606807d4SdlgThe
130606807d4Sdlg.Fa ks_read
131606807d4Sdlghandler is called by the kstat API when userland requests the current
132606807d4Sdlgkstat data.
133606807d4SdlgA kstat provider may ignore the request via and update the data by
134606807d4Sdlganother process.
135606807d4SdlgFor example, a device may periodically update a set of statistics
136606807d4Sdlgand notify the kernel when the new statistics are available with
137606807d4Sdlgan interrupt.
138606807d4SdlgSuch a driver would update the kstat data and
139606807d4Sdlg.Fa ks_updated
140606807d4Sdlgwhen the interrupt is processed, and ignore the request to update
141606807d4Sdlgfrom userland.
142606807d4SdlgThe default
143606807d4Sdlg.Fa ks_read
144606807d4Sdlghandler sets
145606807d4Sdlg.Fa ks_updated
146606807d4Sdlgusing
147606807d4Sdlg.Xr getnanouptime 9 .
148606807d4Sdlg.Pp
149606807d4SdlgThe
150606807d4Sdlg.Fa ks_copy
151606807d4Sdlghandler is used by the kstat API to copy the current kstat data into the
152606807d4Sdlg.Fa dst
153606807d4Sdlgbuffer.
154606807d4SdlgThe default
155606807d4Sdlg.Fa ks_copy
156606807d4Sdlghandler uses
157606807d4Sdlg.Xr memcpy 3
158606807d4Sdlgto copy
159606807d4Sdlg.Fa ks_datalen
160606807d4Sdlgbytes from
161606807d4Sdlg.Fa ks_data
162606807d4Sdlgto
163606807d4Sdlg.Fa dst .
164606807d4Sdlg.Pp
165606807d4SdlgAccesses to the above
166606807d4Sdlg.Vt kstat
167606807d4Sdlgstructure fields and calls to the
168606807d4Sdlg.Fa ks_read
169606807d4Sdlgand
170606807d4Sdlg.Fa ks_copy
171*139410fbSjsghandlers by the kstat subsystem are serialised by the locking primitive
172606807d4Sdlgreferenced by
173606807d4Sdlg.Fa ks_lock .
174606807d4SdlgBy default
175606807d4Sdlg.Fa ks_lock
176606807d4Sdlgreferences a global write lock provided by the kstat API,
177606807d4Sdlgbut should be set to a provider specific lock with the
178606807d4Sdlg.Fa kstat_set_rlock ,
179606807d4Sdlg.Fa kstat_set_wlock ,
180606807d4Sdlgor
181606807d4Sdlg.Fa kstat_set_mutex
182606807d4Sdlgfunctions.
183606807d4Sdlg.Pp
184606807d4SdlgThe
185606807d4Sdlg.Fn kstat_create
186606807d4Sdlgfunction allocates a
187606807d4Sdlg.Vt kstat
188606807d4Sdlgstructure and adds it to the list of statistics that userland can
189606807d4Sdlgquery.
1907592d7efSschwarzeOnce a
1917592d7efSschwarze.Vt kstat
1927592d7efSschwarzestructure has been created,
1937592d7efSschwarzethe caller is responsible for initialising the structure.
194b3ebe46dSdlg.Pp
195606807d4Sdlg.Fn kstat_read_nop
196606807d4Sdlgcan be used as a
197606807d4Sdlg.Fa ks_read
198606807d4Sdlghandler to ignore the request to update the kstat data and
199606807d4Sdlg.Fa ks_updated
200606807d4Sdlgtimestamp.
201606807d4Sdlg.Pp
202606807d4SdlgThe
203606807d4Sdlg.Fn kstat_set_wlock
204606807d4Sdlgand
205606807d4Sdlg.Fn kstat_set_rlock
206606807d4Sdlgfunctions specifies that the
207606807d4Sdlg.Fa rwl
208606807d4Sdlgread/write lock should be used as an exclusive or shared lock
209606807d4Sdlgrespectively by the kstat API when interacting with the provider.
210606807d4Sdlg.Pp
211606807d4SdlgThe
212606807d4Sdlg.Fn kstat_set_mutex
213606807d4Sdlgfunction specifies that the
214606807d4Sdlg.Fa mtx
215606807d4Sdlgmutex should be acquired by the kstat API when interacting with the
216606807d4Sdlgprovider.
217606807d4Sdlg.Pp
218b3ebe46dSdlgAfter the structure has been initialised,
219b3ebe46dSdlg.Fn kstat_install
2207592d7efSschwarzenotifies the kstat subsystem that
221b3ebe46dSdlg.Fa ks
222b3ebe46dSdlgcan be used to export information to userland.
223b3ebe46dSdlg.Pp
224a2ae35ffSdlg.Fn kstat_remove
225a2ae35ffSdlgdisables the kstat, preventing it from being used to export information
226a2ae35ffSdlgto userland.
227a2ae35ffSdlgThis allows allocations referenced by the kstat struct to be released
228a2ae35ffSdlgand configuration torn down before the kstat itself is freed with
229a2ae35ffSdlg.Fn kstat_destroy .
230a2ae35ffSdlg.Pp
231b3ebe46dSdlg.Fn kstat_destroy
2327592d7efSschwarzeremoves
233b3ebe46dSdlg.Fa ks
2347592d7efSschwarzefrom the list of exported statistics and frees it.
235b3ebe46dSdlg.Sh CONTEXT
236b3ebe46dSdlg.Fn kstat_create ,
237b3ebe46dSdlg.Fn kstat_install ,
2389aba907fSjmc.Fn kstat_remove ,
239606807d4Sdlg.Fn kstat_set_wlock ,
240606807d4Sdlg.Fn kstat_set_rlock ,
241606807d4Sdlg.Fn kstat_set_mutex ,
242b3ebe46dSdlgand
243b3ebe46dSdlg.Fn kstat_destroy
244b3ebe46dSdlgcan be called during autoconf, or from process context.
245606807d4SdlgThey cannot be called by a
246606807d4Sdlg.Fa ks_read
247606807d4Sdlgor
248606807d4Sdlg.Fa ks_copy
249606807d4Sdlghandler.
250b3ebe46dSdlg.Sh RETURN VALUES
251b3ebe46dSdlg.Fn kstat_create
2527592d7efSschwarzereturns a pointer to a
2537592d7efSschwarze.Vt kstat
2547592d7efSschwarzestructure on success, or
255b3ebe46dSdlg.Dv NULL
256b3ebe46dSdlgon failure.
257b3ebe46dSdlg.Sh SEE ALSO
258606807d4Sdlg.Xr kstat 1 ,
259606807d4Sdlg.Xr memcpy 3 ,
260606807d4Sdlg.Xr kstat 4 ,
261606807d4Sdlg.Xr kstat_kv_init 9 ,
262606807d4Sdlg.Xr mtx_enter 9 ,
263606807d4Sdlg.Xr rw_enter 9
2647592d7efSschwarze.Sh HISTORY
2657592d7efSschwarzeThese functions first appeared in
2667592d7efSschwarze.Ox 6.8 .
2677592d7efSschwarze.Sh AUTHORS
2687592d7efSschwarze.An David Gwynne Aq Mt dlg@openbsd.org
269