2 .\" Copyright (c) 1998, 1999 Kenneth D. Merry.
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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. The name of the author may not be used to endorse or promote products
14 .\" derived from this software without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" $FreeBSD: src/lib/libdevstat/devstat.3,v 1.7.2.8 2001/12/17 10:08:29 ru Exp $
29 .\" $DragonFly: src/lib/libdevstat/devstat.3,v 1.6 2008/05/02 02:05:04 swildner Exp $
45 .Nd device statistics utility library
53 .Fn getgeneration "void"
57 .Fn checkversion "void"
59 .Fn getdevs "struct statinfo *stats"
62 .Fa "struct device_selection **dev_select"
63 .Fa "int *num_selected"
64 .Fa "int *num_selections"
65 .Fa "long *select_generation"
66 .Fa "long current_generation"
67 .Fa "struct devstat *devices"
69 .Fa "struct devstat_match *matches"
71 .Fa "char **dev_selections"
72 .Fa "int num_dev_selections"
73 .Fa "devstat_select_mode select_mode"
79 .Fa "const char *match_str"
80 .Fa "struct devstat_match **matches"
81 .Fa "int *num_matches"
85 .Fa "struct devstat *current"
86 .Fa "struct devstat *previous"
87 .Fa "long double etime"
88 .Fa "u_int64_t *total_bytes"
89 .Fa "u_int64_t *total_transfers"
90 .Fa "u_int64_t *total_blocks"
91 .Fa "long double *kb_per_transfer"
92 .Fa "long double *transfers_per_second"
93 .Fa "long double *mb_per_second"
94 .Fa "long double *blocks_per_second"
95 .Fa "long double *ms_per_transaction"
99 .Fa "struct timeval cur_time"
100 .Fa "struct timeval prev_time"
105 library is a library of helper functions for dealing with the kernel
107 interface, which is accessible to users via
111 returns the number of devices registered with the
113 subsystem in the kernel.
116 returns the current generation of the
118 list of devices in the kernel.
121 returns the current kernel
126 checks the userland devstat version against the kernel devstat version.
127 If the two are identical, it returns zero.
128 Otherwise, it prints an appropriate error in
133 fetches the current list of devices and statistics into the supplied
138 structure can be found in
140 .Bd -literal -offset indent
142 struct kinfo_cputime cp_time;
143 struct devinfo *dinfo;
144 struct timeval busy_time;
151 structure to be allocated, and it also expects the
153 subelement to be allocated and zeroed prior to the first invocation of
157 subelement is used to store state between calls, and should not be modified
158 after the first call to
162 subelement contains the following elements:
163 .Bd -literal -offset indent
165 struct devstat *devices;
175 variable contains an array of
177 structures, but at the head of the array is the current
180 The reason the generation is at the head of the buffer is so that userland
181 software accessing the devstat statistics information can atomically get
182 both the statistics information and the corresponding generation number.
183 If client software were forced to get the generation number via a separate
185 variable (which is available for convenience), the list of devices could
186 change between the time the client gets the generation and the time the
187 client gets the device list.
193 structure is a pointer to memory that is allocated, and resized if
196 The devices subelement of the
198 structure is basically a pointer to the beginning of the array of devstat
203 The generation subelement of the
205 structure contains the generation number from the
213 structure contains the current
214 number of devices registered with the kernel
219 selects devices to display based upon a number of criteria:
221 .It specified devices
222 Specified devices are the first selection priority.
223 These are generally devices specified by name by the user e.g. da0, da1, cd0.
225 These are pattern matching expressions generated by
229 If performance mode is enabled, devices will be sorted based on the
233 structure passed in to
237 value currently must be maintained by the user.
238 In the future, this may be done for him in a
241 If no devices have been selected by name or by pattern, the performance
242 tracking code will select every device in the system, and sort them by
244 If devices have been selected by name or pattern, the performance tracking
245 code will honor those selections and will only sort among the selected
247 .It order in the devstat list
248 If the selection mode is set to DS_SELECT_ADD, and if there are still less
253 will automatically select up to
259 performs selections in four different modes:
260 .Bl -tag -width DS_SELECT_ADDONLY
264 will select any unselected devices specified by name or matching pattern.
265 It will also select more devices, in devstat list order, until the number
266 of selected devices is equal to
268 or until all devices are
273 will clear all current selections, and will only select devices specified
274 by name or by matching pattern.
278 will remove devices specified by name or by matching pattern.
279 It will not select any additional devices.
280 .It DS_SELECT_ADDONLY
283 will select any unselected devices specified by name or matching pattern.
284 In this respect it is identical to add mode.
285 It will not, however, select any devices other than those specified.
288 In all selection modes,
290 will not select any more than
293 One exception to this is when you are in
295 mode and no devices have been selected.
298 will select every device in the system.
299 Client programs must pay attention to selection order when deciding whether
300 to pay attention to a particular device.
301 This may be the wrong behavior, and probably requires additional thought.
304 handles allocation and resizing of the
312 .Va current_generation
316 generation and number of devices.
323 .Va select_generation
325 .Va current_generation ,
327 will resize the selection list as necessary, and re-initialize the
331 takes a comma separated match string and compiles it into a
332 \fBdevstat_match\fR structure that is understood by
334 Match strings have the following format:
335 .Bd -literal -offset indent
340 takes care of allocating and reallocating the match list as necessary.
341 Currently known match types include:
343 .Bl -tag -width indent -compact
345 .Bl -tag -width 9n -compact
347 Direct Access devices
349 Sequential Access devices
355 Write Once Read Multiple devices
361 Optical Memory devices
363 Medium Changer devices
365 Communication devices
367 Storage Array devices
369 Enclosure Services devices
375 .Bl -tag -width 9n -compact
377 Integrated Drive Electronics devices
379 Small Computer System Interface devices
381 Any other device interface
385 .Bl -tag -width 9n -compact
392 provides an easy way to obtain various device statistics.
393 Only two arguments are mandatory:
397 Every other argument is optional.
398 For most applications, the user will want to supply both
402 devstat structures so that statistics may be calculated over a given period
404 In some instances, for instance when calculating statistics since system boot,
405 the user may pass in a NULL pointer for the
410 will use the total stats in the
412 structure to calculate statistics over
414 The various statistics that may be calculated by
416 should be mostly explained by the function declaration itself, but for
417 completeness here is a list of variable names and the statistics that will
419 .Bl -tag -width transfers_per_second
421 This is the total number of bytes transferred on the given device, both
422 reads and writes, between the acquisition of
424 and the acquisition of
428 is NULL, the result will be the total reads and writes given in
431 This is the total number of transfers completed between the
434 and the acquisition of
438 is NULL, the result will be the total number of transactions listed in
443 divided by the device blocksize.
444 If the device blocksize is listed as
446 the device blocksize will default to 512 bytes.
448 This is the average number of kilobytes per transfer during the measurement
450 .It transfers_per_second
451 This is the average number of transfers per second.
453 This is average megabytes per second.
454 .It blocks_per_second
455 This is average blocks per second.
456 If the device blocksize is
458 a default blocksize of 512 bytes will be used instead.
459 .It ms_per_transaction
460 The average number of milliseconds per transaction.
464 provides an easy way to find the difference in seconds between two
467 This is most commonly used in conjunction with the time recorded by the
471 each time it fetches the current
479 return the indicated \fBsysctl\fR variable, or -1 if there is an error
480 fetching the variable.
483 returns 0 if the kernel and userland
486 If they do not match, it returns -1.
491 return -1 in case of an error, 0 if there is no error and 1 if the device
492 list or selected devices have changed.
493 A return value of 1 from
495 is usually a hint to re-run
497 because the device list has changed.
500 returns -1 for error, and 0 if there is no error.
503 returns -1 for error, and 0 for success.
506 returns the computed elapsed time.
508 If an error is returned from one of the
510 library functions, the reason for the error is generally printed in
514 .Dv DEVSTAT_ERRBUF_SIZE
525 statistics system first appeared in
528 .An Kenneth Merry Aq ken@FreeBSD.org
530 There should probably be an interface to de-allocate memory allocated by
537 should probably not select more than
541 mode when no devices have been selected previously.
543 There should probably be functions to perform the statistics buffer
544 swapping that goes on in most of the clients of this library.
550 structures should probably be cleaned up and thought out a little more.