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.Dd July 15, 2020 29.Dt DEVSTAT 9 30.Os 31.Sh NAME 32.Nm devstat , 33.Nm devstat_end_transaction , 34.Nm devstat_end_transaction_bio , 35.Nm devstat_end_transaction_bio_bt , 36.Nm devstat_new_entry , 37.Nm devstat_remove_entry , 38.Nm devstat_start_transaction , 39.Nm devstat_start_transaction_bio 40.Nd kernel interface for keeping device statistics 41.Sh SYNOPSIS 42.In sys/devicestat.h 43.Ft struct devstat * 44.Fo devstat_new_entry 45.Fa "const void *dev_name" 46.Fa "int unit_number" 47.Fa "uint32_t block_size" 48.Fa "devstat_support_flags flags" 49.Fa "devstat_type_flags device_type" 50.Fa "devstat_priority priority" 51.Fc 52.Ft void 53.Fn devstat_remove_entry "struct devstat *ds" 54.Ft void 55.Fo devstat_start_transaction 56.Fa "struct devstat *ds" 57.Fa "const struct bintime *now" 58.Fc 59.Ft void 60.Fo devstat_start_transaction_bio 61.Fa "struct devstat *ds" 62.Fa "struct bio *bp" 63.Fc 64.Ft void 65.Fo devstat_end_transaction 66.Fa "struct devstat *ds" 67.Fa "uint32_t bytes" 68.Fa "devstat_tag_type tag_type" 69.Fa "devstat_trans_flags flags" 70.Fa "const struct bintime *now" 71.Fa "const struct bintime *then" 72.Fc 73.Ft void 74.Fo devstat_end_transaction_bio 75.Fa "struct devstat *ds" 76.Fa "const struct bio *bp" 77.Fc 78.Ft void 79.Fo devstat_end_transaction_bio_bt 80.Fa "struct devstat *ds" 81.Fa "const struct bio *bp" 82.Fa "const struct bintime *now" 83.Fc 84.Sh DESCRIPTION 85The devstat subsystem is an interface for recording device 86statistics, as its name implies. 87The idea is to keep reasonably detailed 88statistics while utilizing a minimum amount of CPU time to record them. 89Thus, no statistical calculations are actually performed in the kernel 90portion of the 91.Nm 92code. 93Instead, that is left for user programs to handle. 94.Pp 95The historical and antiquated 96.Nm 97model assumed a single active IO operation per device, which is not accurate 98for most disk-like drivers in the 2000s and beyond. 99New consumers of the interface should almost certainly use only the "bio" 100variants of the start and end transacation routines. 101.Pp 102.Fn devstat_new_entry 103allocates and initializes 104.Va devstat 105structure and returns a pointer to it. 106.Fn devstat_new_entry 107takes several arguments: 108.Bl -tag -width device_type 109.It dev_name 110The device name, e.g., da, cd, sa. 111.It unit_number 112Device unit number. 113.It block_size 114Block size of the device, if supported. 115If the device does not support a 116block size, or if the blocksize is unknown at the time the device is added 117to the 118.Nm 119list, it should be set to 0. 120.It flags 121Flags indicating operations supported or not supported by the device. 122See below for details. 123.It device_type 124The device type. 125This is broken into three sections: base device type 126(e.g., direct access, CDROM, sequential access), interface type (IDE, SCSI 127or other) and a pass-through flag to indicate pas-through devices. 128See below for a complete list of types. 129.It priority 130The device priority. 131The priority is used to determine how devices are 132sorted within 133.Nm devstat Ns 's 134list of devices. 135Devices are sorted first by priority (highest to lowest), 136and then by attach order. 137See below for a complete list of available 138priorities. 139.El 140.Pp 141.Fn devstat_remove_entry 142removes a device from the 143.Nm 144subsystem. 145It takes the devstat structure for the device in question as 146an argument. 147The 148.Nm 149generation number is incremented and the number of devices is decremented. 150.Pp 151.Fn devstat_start_transaction 152registers the start of a transaction with the 153.Nm 154subsystem. 155Optionally, if the caller already has a 156.Fn binuptime 157value available, it may be passed in 158.Fa *now . 159Usually the caller can just pass 160.Dv NULL 161for 162.Fa now , 163and the routine will gather the current 164.Fn binuptime 165itself. 166The busy count is incremented with each transaction start. 167When a device goes from idle to busy, the system uptime is recorded in the 168.Va busy_from 169field of the 170.Va devstat 171structure. 172.Pp 173.Fn devstat_start_transaction_bio 174records the 175.Fn binuptime 176in the provided bio's 177.Fa bio_t0 178and then invokes 179.Fn devstat_start_transaction . 180.Pp 181.Fn devstat_end_transaction 182registers the end of a transaction with the 183.Nm 184subsystem. 185It takes six arguments: 186.Bl -tag -width tag_type 187.It ds 188The 189.Va devstat 190structure for the device in question. 191.It bytes 192The number of bytes transferred in this transaction. 193.It tag_type 194Transaction tag type. 195See below for tag types. 196.It flags 197Transaction flags indicating whether the transaction was a read, write, or 198whether no data was transferred. 199.It now 200The 201.Fn binuptime 202at the end of the transaction, or 203.Dv NULL . 204.It then 205The 206.Fn binuptime 207at the beginning of the transaction, or 208.Dv NULL . 209.El 210.Pp 211If 212.Fa now 213is 214.Dv NULL , 215it collects the current time from 216.Fn binuptime . 217If 218.Fa then 219is 220.Dv NULL , 221the operation is not tracked in the 222.Va devstat 223.Fa duration 224table. 225.Pp 226.Fn devstat_end_transaction_bio 227is a thin wrapper for 228.Fn devstat_end_transaction_bio_bt 229with a 230.Dv NULL 231.Fa now 232parameter. 233.Pp 234.Fn devstat_end_transaction_bio_bt 235is a wrapper for 236.Fn devstat_end_transaction 237which pulls all needed information from a 238.Va "struct bio" 239prepared by 240.Fn devstat_start_transaction_bio . 241The bio must be ready for 242.Fn biodone 243(i.e., 244.Fa bio_bcount 245and 246.Fa bio_resid 247must be correctly initialized). 248.Pp 249The 250.Va devstat 251structure is composed of the following fields: 252.Bl -tag -width dev_creation_time 253.It sequence0 , 254.It sequence1 255An implementation detail used to gather consistent snapshots of device 256statistics. 257.It start_count 258Number of operations started. 259.It end_count 260Number of operations completed. 261The 262.Dq busy_count 263can be calculated by subtracting 264.Fa end_count 265from 266.Fa start_count . 267.Fa ( sequence0 268and 269.Fa sequence1 270are used to get a consistent snapshot.) 271This is the current number of outstanding transactions for the device. 272This should never go below zero, and on an idle device it should be zero. 273If either one of these conditions is not true, it indicates a problem. 274.Pp 275There should be one and only one 276transaction start event and one transaction end event for each transaction. 277.It dev_links 278Each 279.Va devstat 280structure is placed in a linked list when it is registered. 281The 282.Va dev_links 283field contains a pointer to the next entry in the list of 284.Va devstat 285structures. 286.It device_number 287The device number is a unique identifier for each device. 288The device 289number is incremented for each new device that is registered. 290The device 291number is currently only a 32-bit integer, but it could be enlarged if 292someone has a system with more than four billion device arrival events. 293.It device_name 294The device name is a text string given by the registering driver to 295identify itself. 296(e.g., 297.Dq da , 298.Dq cd , 299.Dq sa , 300etc.) 301.It unit_number 302The unit number identifies the particular instance of the peripheral driver 303in question. 304.It bytes[4] 305This array contains the number of bytes that have been read (index 306.Dv DEVSTAT_READ ) , 307written (index 308.Dv DEVSTAT_WRITE ) , 309freed or erased (index 310.Dv DEVSTAT_FREE ) , 311or other (index 312.Dv DEVSTAT_NO_DATA ) . 313All values are unsigned 64-bit integers. 314.It operations[4] 315This array contains the number of operations of a given type that have been 316performed. 317The indices are identical to those for 318.Fa bytes 319above. 320.Dv DEVSTAT_NO_DATA 321or "other" represents the number of transactions to the device which are 322neither reads, writes, nor frees. 323For instance, 324.Tn SCSI 325drivers often send a test unit ready command to 326.Tn SCSI 327devices. 328The test unit ready command does not read or write any data. 329It merely causes the device to return its status. 330.It duration[4] 331This array contains the total bintime corresponding to completed operations of 332a given type. 333The indices are identical to those for 334.Fa bytes 335above. 336(Operations that complete using the historical 337.Fn devstat_end_transaction 338API and do not provide a non-NULL 339.Fa then 340are not accounted for.) 341.It busy_time 342This is the amount of time that the device busy count has been greater than 343zero. 344This is only updated when the busy count returns to zero. 345.It creation_time 346This is the time, as reported by 347.Fn getmicrotime 348that the device was registered. 349.It block_size 350This is the block size of the device, if the device has a block size. 351.It tag_types 352This is an array of counters to record the number of various tag types that 353are sent to a device. 354See below for a list of tag types. 355.It busy_from 356If the device is not busy, this was the time that a transaction last completed. 357If the device is busy, this the most recent of either the time that the device 358became busy, or the time that the last transaction completed. 359.It flags 360These flags indicate which statistics measurements are supported by a 361particular device. 362These flags are primarily intended to serve as an aid 363to userland programs that decipher the statistics. 364.It device_type 365This is the device type. 366It consists of three parts: the device type 367(e.g., direct access, CDROM, sequential access, etc.), the interface (IDE, 368SCSI or other) and whether or not the device in question is a pass-through 369driver. 370See below for a complete list of device types. 371.It priority 372This is the priority. 373This is the first parameter used to determine where 374to insert a device in the 375.Nm 376list. 377The second parameter is attach order. 378See below for a list of available priorities. 379.It id 380Identification for GEOM nodes. 381.El 382.Pp 383Each device is given a device type. 384Pass-through devices have the same underlying device type and interface as the 385device they provide an interface for, but they also have the pass-through flag 386set. 387The base device types are identical to the 388.Tn SCSI 389device type numbers, so with 390.Tn SCSI 391peripherals, the device type returned from an inquiry is usually ORed with the 392.Tn SCSI 393interface type and the pass-through flag if appropriate. 394The device type 395flags are as follows: 396.Bd -literal -offset indent 397typedef enum { 398 DEVSTAT_TYPE_DIRECT = 0x000, 399 DEVSTAT_TYPE_SEQUENTIAL = 0x001, 400 DEVSTAT_TYPE_PRINTER = 0x002, 401 DEVSTAT_TYPE_PROCESSOR = 0x003, 402 DEVSTAT_TYPE_WORM = 0x004, 403 DEVSTAT_TYPE_CDROM = 0x005, 404 DEVSTAT_TYPE_SCANNER = 0x006, 405 DEVSTAT_TYPE_OPTICAL = 0x007, 406 DEVSTAT_TYPE_CHANGER = 0x008, 407 DEVSTAT_TYPE_COMM = 0x009, 408 DEVSTAT_TYPE_ASC0 = 0x00a, 409 DEVSTAT_TYPE_ASC1 = 0x00b, 410 DEVSTAT_TYPE_STORARRAY = 0x00c, 411 DEVSTAT_TYPE_ENCLOSURE = 0x00d, 412 DEVSTAT_TYPE_FLOPPY = 0x00e, 413 DEVSTAT_TYPE_MASK = 0x00f, 414 DEVSTAT_TYPE_IF_SCSI = 0x010, 415 DEVSTAT_TYPE_IF_IDE = 0x020, 416 DEVSTAT_TYPE_IF_OTHER = 0x030, 417 DEVSTAT_TYPE_IF_MASK = 0x0f0, 418 DEVSTAT_TYPE_PASS = 0x100 419} devstat_type_flags; 420.Ed 421.Pp 422Devices have a priority associated with them, which controls roughly where 423they are placed in the 424.Nm 425list. 426The priorities are as follows: 427.Bd -literal -offset indent 428typedef enum { 429 DEVSTAT_PRIORITY_MIN = 0x000, 430 DEVSTAT_PRIORITY_OTHER = 0x020, 431 DEVSTAT_PRIORITY_PASS = 0x030, 432 DEVSTAT_PRIORITY_FD = 0x040, 433 DEVSTAT_PRIORITY_WFD = 0x050, 434 DEVSTAT_PRIORITY_TAPE = 0x060, 435 DEVSTAT_PRIORITY_CD = 0x090, 436 DEVSTAT_PRIORITY_DISK = 0x110, 437 DEVSTAT_PRIORITY_ARRAY = 0x120, 438 DEVSTAT_PRIORITY_MAX = 0xfff 439} devstat_priority; 440.Ed 441.Pp 442Each device has associated with it flags to indicate what operations are 443supported or not supported. 444The 445.Va devstat_support_flags 446values are as follows: 447.Bl -tag -width DEVSTAT_NO_ORDERED_TAGS 448.It DEVSTAT_ALL_SUPPORTED 449Every statistic type is supported by the device. 450.It DEVSTAT_NO_BLOCKSIZE 451This device does not have a blocksize. 452.It DEVSTAT_NO_ORDERED_TAGS 453This device does not support ordered tags. 454.It DEVSTAT_BS_UNAVAILABLE 455This device supports a blocksize, but it is currently unavailable. 456This 457flag is most often used with removable media drives. 458.El 459.Pp 460Transactions to a device fall into one of three categories, which are 461represented in the 462.Va flags 463passed into 464.Fn devstat_end_transaction . 465The transaction types are as follows: 466.Bd -literal -offset indent 467typedef enum { 468 DEVSTAT_NO_DATA = 0x00, 469 DEVSTAT_READ = 0x01, 470 DEVSTAT_WRITE = 0x02, 471 DEVSTAT_FREE = 0x03 472} devstat_trans_flags; 473#define DEVSTAT_N_TRANS_FLAGS 4 474.Ed 475.Pp 476DEVSTAT_NO_DATA is a type of transactions to the device which are neither 477reads or writes. 478For instance, 479.Tn SCSI 480drivers often send a test unit ready command to 481.Tn SCSI 482devices. 483The test unit ready command does not read or write any data. 484It merely causes the device to return its status. 485.Pp 486There are four possible values for the 487.Va tag_type 488argument to 489.Fn devstat_end_transaction : 490.Bl -tag -width DEVSTAT_TAG_ORDERED 491.It DEVSTAT_TAG_SIMPLE 492The transaction had a simple tag. 493.It DEVSTAT_TAG_HEAD 494The transaction had a head of queue tag. 495.It DEVSTAT_TAG_ORDERED 496The transaction had an ordered tag. 497.It DEVSTAT_TAG_NONE 498The device does not support tags. 499.El 500.Pp 501The tag type values correspond to the lower four bits of the 502.Tn SCSI 503tag definitions. 504In CAM, for instance, the 505.Va tag_action 506from the CCB is ORed with 0xf to determine the tag type to pass in to 507.Fn devstat_end_transaction . 508.Pp 509There is a macro, 510.Dv DEVSTAT_VERSION 511that is defined in 512.In sys/devicestat.h . 513This is the current version of the 514.Nm 515subsystem, and it should be incremented each time a change is made that 516would require recompilation of userland programs that access 517.Nm 518statistics. 519Userland programs use this version, via the 520.Va kern.devstat.version 521.Nm sysctl 522variable to determine whether they are in sync with the kernel 523.Nm 524structures. 525.Sh SEE ALSO 526.Xr systat 1 , 527.Xr devstat 3 , 528.Xr iostat 8 , 529.Xr rpc.rstatd 8 , 530.Xr vmstat 8 531.Sh HISTORY 532The 533.Nm 534statistics system appeared in 535.Fx 3.0 . 536.Sh AUTHORS 537.An Kenneth Merry Aq Mt ken@FreeBSD.org 538.Sh BUGS 539There may be a need for 540.Fn spl 541protection around some of the 542.Nm 543list manipulation code to ensure, for example, that the list of devices 544is not changed while someone is fetching the 545.Va kern.devstat.all 546.Nm sysctl 547variable. 548