Initial import from FreeBSD RELENG_4:
[games.git] / share / man / man9 / devstat.9
1 .\"
2 .\" Copyright (c) 1998, 1999 Kenneth D. Merry.
3 .\" All rights reserved.
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. The name of the author may not be used to endorse or promote products
14 .\"    derived from this software without specific prior written permission.
15 .\"
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
26 .\" SUCH DAMAGE.
27 .\"
28 .\" $FreeBSD: src/share/man/man9/devstat.9,v 1.10.2.5 2001/12/17 11:30:18 ru Exp $
29 .\"
30 .Dd May 22, 1998
31 .Dt DEVSTAT 9
32 .Os
33 .Sh NAME
34 .Nm devstat ,
35 .Nm devstat_add_entry ,
36 .Nm devstat_end_transaction ,
37 .Nm devstat_end_transaction_buf ,
38 .Nm devstat_remove_entry ,
39 .Nm devstat_start_transaction
40 .Nd kernel interface for keeping device statistics
41 .Sh SYNOPSIS
42 .In sys/devicestat.h
43 .Ft void
44 .Fo devstat_add_entry
45 .Fa "struct devstat *ds"
46 .Fa "const char *dev_name"
47 .Fa "int unit_number"
48 .Fa "u_int32_t block_size"
49 .Fa "devstat_support_flags flags"
50 .Fa "devstat_type_flags device_type"
51 .Fa "devstat_priority priority"
52 .Fc
53 .Ft void
54 .Fn devstat_remove_entry "struct devstat *ds"
55 .Ft void
56 .Fn devstat_start_transaction "struct devstat *ds"
57 .Ft void
58 .Fo devstat_end_transaction
59 .Fa "struct devstat *ds"
60 .Fa "u_int32_t bytes"
61 .Fa "devstat_tag_type tag_type"
62 .Fa "devstat_trans_flags flags"
63 .Fc
64 .Ft void
65 .Fo devstat_end_transaction_buf
66 .Fa "struct devstat *ds"
67 .Fa "struct buf *bp"
68 .Fc
69 .Sh DESCRIPTION
70 The devstat subsystem is an interface for recording device
71 statistics, as its name implies.  The idea is to keep reasonably detailed
72 statistics while utilizing a minimum amount of CPU time to record them.
73 Thus, no statistical calculations are actually performed in the kernel
74 portion of the
75 .Nm
76 code. Instead, that is left for user programs to handle.
77 .Pp
78 .Fn devstat_add_entry
79 registers a device with the
80 .Nm
81 subsystem.  The caller is expected to have already allocated \fBand zeroed\fR
82 the devstat structure before calling this function.
83 .Fn devstat_add_entry
84 takes several arguments:
85 .Bl -tag -width device_type
86 .It ds
87 The
88 .Va devstat
89 structure, allocated and zeroed by the client.
90 .It dev_name
91 The device name. e.g. da, cd, sa.
92 .It unit_number
93 Device unit number.
94 .It block_size
95 Block size of the device, if supported.  If the device does not support a
96 block size, or if the blocksize is unknown at the time the device is added
97 to the
98 .Nm
99 list, it should be set to 0.
100 .It flags
101 Flags indicating operations supported or not supported by the device.  See
102 below for details.
103 .It device_type
104 The device type.  This is broken into three sections:  base device type
105 (e.g. direct access, CDROM, sequential access), interface type (IDE, SCSI
106 or other) and a passthrough flag to indicate pasthrough devices.  See below
107 for a complete list of types.
108 .It priority
109 The device priority.  The priority is used to determine how devices are
110 sorted within
111 .Nm devstat Ns 's
112 list of devices.  Devices are sorted first by priority (highest to lowest),
113 and then by attach order.  See below for a complete list of available
114 priorities.
115 .El
116 .Pp
117 .Fn devstat_remove_entry
118 removes a device from the
119 .Nm
120 subsystem.  It takes the devstat structure for the device in question as
121 an argument.  The
122 .Nm
123 generation number is incremented and the number of devices is decremented.
124 .Pp
125 .Fn devstat_start_transaction
126 registers the start of a transaction with the
127 .Nm
128 subsystem.  The busy count is incremented with each transaction start.
129 When a device goes from idle to busy, the system uptime is recorded in the
130 .Va start_time
131 field of the
132 .Va devstat
133 structure.
134 .Pp
135 .Fn devstat_end_transaction
136 registers the end of a transaction with the
137 .Nm
138 subsystem.  It takes four arguments:
139 .Bl -tag -width tag_type
140 .It ds
141 The
142 .Va devstat
143 structure for the device in question.
144 .It bytes
145 The number of bytes transferred in this transaction.
146 .It tag_type
147 Transaction tag type.  See below for tag types.
148 .It flags
149 Transaction flags indicating whether the transaction was a read, write, or
150 whether no data was transferred.
151 .El
152 .Pp
153 .Fn devstat_end_transaction_buf
154 is a wrapper for
155 .Fn devstat_end_transaction
156 which pulls all the information from a
157 .Va "struct buf"
158 which is ready for biodone().
159 .Pp
160 The
161 .Va devstat
162 structure is composed of the following fields:
163 .Bl -tag -width dev_creation_time
164 .It dev_links
165 Each
166 .Va devstat
167 structure is placed in a linked list when it is registered.  The
168 .Va dev_links
169 field contains a pointer to the next entry in the list of
170 .Va devstat
171 structures.
172 .It device_number
173 The device number is a unique identifier for each device.  The device
174 number is incremented for each new device that is registered.  The device
175 number is currently only a 32-bit integer, but it could be enlarged if
176 someone has a system with more than four billion device arrival events.
177 .It device_name
178 The device name is a text string given by the registering driver to
179 identify itself.  (e.g.\&
180 .Dq da ,
181 .Dq cd ,
182 .Dq sa ,
183 etc.)
184 .It unit_number
185 The unit number identifies the particular instance of the peripheral driver
186 in question.
187 .It bytes_written
188 This is the number of bytes that have been written to the device.  This
189 number is currently an unsigned 64 bit integer.  This will hopefully
190 eliminate the counter wrap that would come very quickly on some systems if
191 32 bit integers were used.
192 .It bytes_read
193 This is the number of bytes that have been read from the device.
194 .It bytes_freed
195 This is the number of bytes that have been freed/erased on the device.
196 .It num_reads
197 This is the number of reads from the device.
198 .It num_writes
199 This is the number of writes to the device.
200 .It num_frees
201 This is the number of free/erase operations on the device.
202 .It num_other
203 This is the number of transactions to the device which are neither reads or
204 writes.  For instance,
205 .Tn SCSI
206 drivers often send a test unit ready command to
207 .Tn SCSI
208 devices.  The test unit ready command does not read or write any data.  It
209 merely causes the device to return its status.
210 .It busy_count
211 This is the current number of outstanding transactions for the device.
212 This should never go below zero, and on an idle device it should be zero.
213 If either one of these conditions is not true, it indicates a problem in
214 the way
215 .Fn devstat_start_transaction
216 and
217 .Fn devstat_end_transaction
218 are being called in client code.  There should be one and only one
219 transaction start event and one transaction end event for each transaction.
220 .It block_size
221 This is the block size of the device, if the device has a block size.
222 .It tag_types
223 This is an array of counters to record the number of various tag types that
224 are sent to a device.  See below for a list of tag types.
225 .It dev_creation_time
226 This is the time, as reported by
227 .Fn getmicrotime
228 that the device was registered.
229 .It busy_time
230 This is the amount of time that the device busy count has been greater than
231 zero.  This is only updated when the busy count returns to zero.
232 .It start_time
233 This is the time, as reported by
234 .Fn getmicrouptime
235 that the device busy count went from zero to one.
236 .It last_comp_time
237 This is the time as reported by
238 .Fn getmicrouptime
239 that a transaction last completed.  It is used along with
240 .Va start_time
241 to calculate the device busy time.
242 .It flags
243 These flags indicate which statistics measurements are supported by a
244 particular device.  These flags are primarily intended to serve as an aid
245 to userland programs that decipher the statistics.
246 .It device_type
247 This is the device type.  It consists of three parts:  the device type
248 (e.g. direct access, CDROM, sequential access, etc.), the interface (IDE,
249 SCSI or other) and whether or not the device in question is a passthrough
250 driver.  See below for a complete list of device types.
251 .It priority
252 This is the priority.  This is the first parameter used to determine where
253 to insert a device in the
254 .Nm
255 list.  The second parameter is attach order.  See below for a list of
256 available priorities.
257 .El
258 .Pp
259 Each device is given a device type.  Passthrough devices have the same
260 underlying device type and interface as the device they provide an
261 interface for, but they also have the passthrough flag set.  The base
262 device types are identical to the
263 .Tn SCSI
264 device type numbers, so with
265 .Tn SCSI
266 peripherals, the device type returned from an inquiry is usually ORed with
267 the
268 .Tn SCSI
269 interface type and the passthrough flag if appropriate.  The device type
270 flags are as follows:
271 .Bd -literal -offset indent
272 typedef enum {
273         DEVSTAT_TYPE_DIRECT     = 0x000,
274         DEVSTAT_TYPE_SEQUENTIAL = 0x001,
275         DEVSTAT_TYPE_PRINTER    = 0x002,
276         DEVSTAT_TYPE_PROCESSOR  = 0x003,
277         DEVSTAT_TYPE_WORM       = 0x004,
278         DEVSTAT_TYPE_CDROM      = 0x005,
279         DEVSTAT_TYPE_SCANNER    = 0x006,
280         DEVSTAT_TYPE_OPTICAL    = 0x007,
281         DEVSTAT_TYPE_CHANGER    = 0x008,
282         DEVSTAT_TYPE_COMM       = 0x009,
283         DEVSTAT_TYPE_ASC0       = 0x00a,
284         DEVSTAT_TYPE_ASC1       = 0x00b,
285         DEVSTAT_TYPE_STORARRAY  = 0x00c,
286         DEVSTAT_TYPE_ENCLOSURE  = 0x00d,
287         DEVSTAT_TYPE_FLOPPY     = 0x00e,
288         DEVSTAT_TYPE_MASK       = 0x00f,
289         DEVSTAT_TYPE_IF_SCSI    = 0x010,
290         DEVSTAT_TYPE_IF_IDE     = 0x020,
291         DEVSTAT_TYPE_IF_OTHER   = 0x030,
292         DEVSTAT_TYPE_IF_MASK    = 0x0f0,
293         DEVSTAT_TYPE_PASS       = 0x100
294 } devstat_type_flags;
295 .Ed
296 .Pp
297 Devices have a priority associated with them, which controls roughly where
298 they are placed in the
299 .Nm
300 list.  The priorities are as follows:
301 .Bd -literal -offset indent
302 typedef enum {
303         DEVSTAT_PRIORITY_MIN    = 0x000,
304         DEVSTAT_PRIORITY_OTHER  = 0x020,
305         DEVSTAT_PRIORITY_PASS   = 0x030,
306         DEVSTAT_PRIORITY_FD     = 0x040,
307         DEVSTAT_PRIORITY_WFD    = 0x050,
308         DEVSTAT_PRIORITY_TAPE   = 0x060,
309         DEVSTAT_PRIORITY_CD     = 0x090,
310         DEVSTAT_PRIORITY_DISK   = 0x110,
311         DEVSTAT_PRIORITY_ARRAY  = 0x120,
312         DEVSTAT_PRIORITY_MAX    = 0xfff
313 } devstat_priority;
314 .Ed
315 .Pp
316 Each device has associated with it flags to indicate what operations are
317 supported or not supported.  The
318 .Va devstat_support_flags
319 values are as follows:
320 .Bl -tag -width DEVSTAT_NO_ORDERED_TAGS
321 .It DEVSTAT_ALL_SUPPORTED
322 Every statistic type is supported by the device.
323 .It DEVSTAT_NO_BLOCKSIZE
324 This device does not have a blocksize.
325 .It DEVSTAT_NO_ORDERED_TAGS
326 This device does not support ordered tags.
327 .It DEVSTAT_BS_UNAVAILABLE
328 This device supports a blocksize, but it is currently unavailable.  This
329 flag is most often used with removable media drives.
330 .El
331 .Pp
332 Transactions to a device fall into one of three categories, which are
333 represented in the
334 .Va flags
335 passed into
336 .Fn devstat_end_transaction .
337 The transaction types are as follows:
338 .Bd -literal -offset indent
339 typedef enum {
340         DEVSTAT_NO_DATA = 0x00,
341         DEVSTAT_READ    = 0x01,
342         DEVSTAT_WRITE   = 0x02,
343         DEVSTAT_FREE    = 0x03
344 } devstat_trans_flags;
345 .Ed
346 .Pp
347 There are four possible values for the
348 .Va tag_type
349 argument to
350 .Fn devstat_end_transaction :
351 .Bl -tag -width DEVSTAT_TAG_ORDERED
352 .It DEVSTAT_TAG_SIMPLE
353 The transaction had a simple tag.
354 .It DEVSTAT_TAG_HEAD
355 The transaction had a head of queue tag.
356 .It DEVSTAT_TAG_ORDERED
357 The transaction had an ordered tag.
358 .It DEVSTAT_TAG_NONE
359 The device doesn't support tags.
360 .El
361 .Pp
362 The tag type values correspond to the lower four bits of the
363 .Tn SCSI
364 tag definitions.  In CAM, for instance, the
365 .Va tag_action
366 from the CCB is ORed with 0xf to determine the tag type to pass in to
367 .Fn devstat_end_transaction .
368 .Pp
369 There is a macro,
370 .Dv DEVSTAT_VERSION
371 that is defined in
372 .Aq sys/devicestat.h .
373 This is the current version of the
374 .Nm
375 subsystem, and it should be incremented each time a change is made that
376 would require recompilation of userland programs that access
377 .Nm
378 statistics.  Userland programs use this version, via the
379 .Va kern.devstat.version
380 .Nm sysctl
381 variable to determine whether they are in sync with the kernel
382 .Nm
383 structures.
384 .Sh SEE ALSO
385 .Xr systat 1 ,
386 .Xr devstat 3 ,
387 .Xr iostat 8 ,
388 .Xr rpc.rstatd 8 ,
389 .Xr vmstat 8
390 .Sh HISTORY
391 The
392 .Nm
393 statistics system appeared in
394 .Fx 3.0 .
395 .Sh AUTHORS
396 .An Kenneth Merry Aq ken@FreeBSD.org
397 .Sh BUGS
398 There may be a need for
399 .Fn spl
400 protection around some of the
401 .Nm
402 list manipulation code to insure, for example, that the list of devices
403 is not changed while someone is fetching the
404 .Va kern.devstat.all
405 .Nm sysctl
406 variable.
407 .Pp
408 It is impossible with the current
409 .Nm
410 architecture to accurately measure time per transaction.  The only feasible
411 way to accurately measure time per transaction would be to record a
412 timestamp for every transaction.  This measurement is probably not
413 worthwhile for most people as it would adversely affect the performance of
414 the system and cost space to store the timestamps for individual
415 transactions.