DEVSTAT(3)
๐ DEVSTAT(3) โ Library Functions Manual ๐
okay so bestie, this is the glow-up of the device statistics utility library page, and we are keeping every single detail because facts are the main character too. ๐
โจ NAME โจ
devstat, devstat_getnumdevs, devstat_getgeneration, devstat_getversion, devstat_checkversion, devstat_getdevs, devstat_selectdevs, devstat_buildmatch, devstat_compute_statistics, devstat_compute_etime โ device statistics utility library. Thatโs the whole squad. ๐ซถ
๐ LIBRARY ๐
Device Statistics Library (libdevstat, -ldevstat)
๐ ๏ธ SYNOPSIS ๐ ๏ธ
#include <devstat.h>
int
devstat_getnumdevs(kvm_t *kd);
long
devstat_getgeneration(kvm_t *kd);
int
devstat_getversion(kvm_t *kd);
int
devstat_checkversion(kvm_t *kd);
int
devstat_getdevs(kvm_t *kd, struct statinfo *stats);
int
devstat_selectdevs(struct device_selection **dev_select,
int *num_selected, int *num_selections, long *select_generation,
long current_generation, struct devstat *devices, int numdevs,
struct devstat_match *matches, int num_matches,
char **dev_selections, int num_dev_selections,
devstat_select_mode select_mode, int maxshowdevs, int perf_select);
int
devstat_buildmatch(char *match_str, struct devstat_match **matches,
int *num_matches);
int
devstat_compute_statistics(struct devstat *current,
struct devstat *previous, long double etime, ...);
long double
devstat_compute_etime(struct bintime *cur_time,
struct bintime *prev_time);
๐ DESCRIPTION ๐
The devstat library is a little collection of helper functions for dealing with the kernel devstat(9) interface, which is accessible to users via sysctl(3) and kvm(3). Literally the bridge between your program and the kernelโs device stats, no cap. ๐
All functions that take a kvm_t * as first argument can be passed NULL instead of a kvm handle as this argument, and that causes the data to be read via sysctl(3). Otherwise, it is read via kvm(3) using the supplied handle. The devstat_checkversion() function should be called with each kvm handle that is going to be used (or with NULL if sysctl(3) is going to be used). Bestie, do not skip the vibe check on that version. ๐
๐ธ the functions, one by one ๐ธ
The devstat_getnumdevs() function returns the number of devices registered with the devstat subsystem in the kernel. Itโs giving โhow many devices are in the group chat.โ ๐
The devstat_getgeneration() function returns the current generation of the devstat list of devices in the kernel. Yes, generation like a timeline, not like your grandmaโs iPod. โจ
The devstat_getversion() function returns the current kernel devstat version. Version check energy, fr fr. ๐
The devstat_checkversion() function checks the userland devstat version against the kernel devstat version. If the two are identical, it returns zero. Otherwise, it prints an appropriate error in devstat_errbuf and returns -1. Not matching? Itโs giving red flag ๐ฉ and the error message is in devstat_errbuf.
The devstat_getdevs() function fetches the current list of devices and statistics into the supplied statinfo structure. The statinfo structure can be found in <devstat.h>:
struct statinfo {
long cp_time[CPUSTATES];
long tk_nin;
long tk_nout;
struct devinfo *dinfo;
long double snap_time;
};
The devstat_getdevs() function expects the statinfo structure to be allocated, and it also expects the dinfo subelement to be allocated and zeroed prior to the first invocation of devstat_getdevs(). The dinfo subelement is used to store state between calls, and should not be modified after the first call to devstat_getdevs(). Do not touch it after first call, okay? Thatโs its main character moment, leave it alone. ๐ซถ
The dinfo subelement contains the following elements:
struct devinfo {
struct devstat *devices;
uint8_t *mem_ptr;
long generation;
int numdevs;
};
The kern.devstat.all sysctl(8) variable contains an array of devstat structures, but at the head of the array is the current devstat generation. The reason the generation is at the head of the buffer is so that userland software accessing the devstat statistics information can atomically get both the statistics information and the corresponding generation number. If client software were forced to get the generation number via a separate sysctl(8) variable (which is available for convenience), the list of devices could change between the time the client gets the generation and the time the client gets the device list. Atomicity slay. ๐ฆ
The mem_ptr subelement of the devinfo structure is a pointer to memory that is allocated, and resized if necessary, by devstat_getdevs(). The devices subelement of the devinfo structure is basically a pointer to the beginning of the array of devstat structures from the kern.devstat.all sysctl(8) variable (or the corresponding values read via kvm(3)). The generation subelement of the devinfo structure contains the corresponding generation number. The numdevs subelement of the devinfo structure contains the current number of devices registered with the kernel devstat subsystem. All the little fields doing their little jobs. ๐
๐ devstat_selectdevs() selection criteria ๐
The devstat_selectdevs() function selects devices to display based upon a number of criteria:
- specified devices โ Specified devices are the first selection priority. These are generally devices specified by name by the user e.g.
da0,da1,cd0. Name-dropped devices get VIP treatment first. ๐ - match patterns โ These are pattern matching expressions generated by
devstat_buildmatch()from user input. - performance โ If performance mode is enabled, devices will be sorted based on the
bytesfield in thedevice_selectionstructure passed in todevstat_selectdevs(). Thebytesvalue currently must be maintained by the user. In the future, this may be done for him in adevstatlibrary routine. If no devices have been selected by name or by pattern, the performance tracking code will select every device in the system, and sort them by performance. If devices have been selected by name or pattern, the performance tracking code will honor those selections and will only sort among the selected devices. Performance mode is lowkey obsessed with thebytesfield. ๐ฅ - order in the devstat list โ If the selection mode is set to
DS_SELECT_ADD, and if there are still less thanmaxshowdevsdevices selected,devstat_selectdevs()will automatically select up tomaxshowdevsdevices.
๐ฎ the four selection modes ๐ฎ
The devstat_selectdevs() function performs selections in four different modes:
DS_SELECT_ADDโ In โaddโ mode,devstat_selectdevs()will select any unselected devices specified by name or matching pattern. It will also select more devices, in devstat list order, until the number of selected devices is equal tomaxshowdevsor until all devices are selected. Itโs giving โadd them to the group chat.โ ๐ฌDS_SELECT_ONLYโ In โonlyโ mode,devstat_selectdevs()will clear all current selections, and will only select devices specified by name or by matching pattern. Clean slate, only the chosen ones. โจDS_SELECT_REMOVEโ In โremoveโ mode,devstat_selectdevs()will remove devices specified by name or by matching pattern. It will not select any additional devices. Bye bestie, blocked. ๐ซDS_SELECT_ADDONLYโ In โadd onlyโ mode,devstat_selectdevs()will select any unselected devices specified by name or matching pattern. In this respect it is identical to โaddโ mode. It will not, however, select any devices other than those specified. Add only, no extras, we stay focused. ๐ฏ
In all selection modes, devstat_selectdevs() will not select any more than maxshowdevs devices. One exception to this is when you are in โtopโ mode and no devices have been selected. In this case, devstat_selectdevs() will select every device in the system. Client programs must pay attention to selection order when deciding whether to pay attention to a particular device. This may be the wrong behavior, and probably requires additional thought. Yeah, thatโs a little โhmmโ moment. ๐
The devstat_selectdevs() function handles allocation and resizing of the dev_select structure passed in by the client. The devstat_selectdevs() function uses the numdevs and current_generation fields to track the current devstat generation and number of devices. If num_selections is not the same as numdevs or if select_generation is not the same as current_generation, devstat_selectdevs() will resize the selection list as necessary, and re-initialize the selection array. Itโs giving adaptive, resize-aware, self-care. ๐ธ
๐งธ devstat_buildmatch() ๐งธ
The devstat_buildmatch() function takes a comma separated match string and compiles it into a devstat_match structure that is understood by devstat_selectdevs(). Match strings have the following format:
device,type,if
The devstat_buildmatch() function takes care of allocating and reallocating the match list as necessary. Currently known match types include:
๐ง device type:
daโ Direct Access devicessaโ Sequential Access devicesprinterโ Printersprocโ Processor deviceswormโ Write Once Read Multiple devicescdโ CD devicesscannerโ Scanner devicesopticalโ Optical Memory deviceschangerโ Medium Changer devicescommโ Communication devicesarrayโ Storage Array devicesenclosureโ Enclosure Services devicesfloppyโ Floppy devices
๐ interface:
IDEโ Integrated Drive Electronics devicesSCSIโ Small Computer System Interface devicesNVMEโ NVM Express Interface devicesotherโ Any other device interface
๐ passthrough:
passโ Passthrough devices
๐งฎ devstat_compute_statistics() ๐งฎ
The devstat_compute_statistics() function provides complete statistics calculation. There are four arguments for which values must be supplied: current, previous, etime, and the terminating argument for the varargs list, DSM_NONE. For most applications, the user will want to supply valid devstat structures for both current and previous. In some instances, for instance when calculating statistics since system boot, the user may pass in a NULL pointer for the previous argument. In that case, devstat_compute_statistics() will use the total stats in the current structure to calculate statistics over etime. For each statistics to be calculated, the user should supply the proper enumerated type (listed below), and a variable of the indicated type. All statistics are either integer values, for which a uint64_t is used, or floating point, for which a long double is used. The statistics that may be calculated are:
DSM_NONEโ type: N/A
This must be the last argument passed to devstat_compute_statistics(). It is an argument list terminator.
DSM_TOTAL_BYTESโ type:uint64_t *
โฆand the last one in that byte group finishes the sentence off: the total number of bytes transferred between the acquisition of previous and current. slay ๐ซถ
DSM_TOTAL_BYTES_READDSM_TOTAL_BYTES_WRITEDSM_TOTAL_BYTES_FREEโ type:uint64_t *
and for that trio it's literally just: the total number of bytes in transactions of the specified type between the acquisition of previous and current โจ
DSM_TOTAL_TRANSFERSโ type:uint64_t *โ the total number of transfers between the acquisition of previous and current. counting them fr ๐DSM_TOTAL_TRANSFERS_OTHERDSM_TOTAL_TRANSFERS_READDSM_TOTAL_TRANSFERS_WRITEDSM_TOTAL_TRANSFERS_FREEโ type:uint64_t *
those four, together: the total number of transactions of the specified type between the acquisition of previous and current ๐
DSM_TOTAL_DURATIONโ type:long double *โ the total duration of transactions, in seconds, between the acquisition of previous and current โฑ๏ธDSM_TOTAL_DURATION_OTHERDSM_TOTAL_DURATION_READDSM_TOTAL_DURATION_WRITEDSM_TOTAL_DURATION_FREEโ type:long double *
same deal, but scoped: the total duration of transactions of the specified type between the acquisition of previous and current ๐
DSM_TOTAL_BUSY_TIMEโ type:long double *โ total time the device had one or more transactions outstanding between the acquisition of previous and current. it's giving "never a free moment" ๐ญDSM_TOTAL_BLOCKSโ type:uint64_t *โ the total number of blocks transferred between the acquisition of previous and current.
heads up bestie: this number is in terms of the blocksize reported by the device. if no blocksize has been reported (i.e., the block size is0), a default blocksize of512bytes will be used in the calculation ๐ฅ
DSM_TOTAL_BLOCKS_READDSM_TOTAL_BLOCKS_WRITEDSM_TOTAL_BLOCKS_FREEโ type:uint64_t *
those three: the total number of blocks of the specified type between the acquisition of previous and current. same blocksize situation โ in terms of the blocksize reported by the device, and if no blocksize has been reported (i.e., the block size is 0), a default blocksize of 512 bytes will be used in the calculation ๐ง
DSM_KB_PER_TRANSFERโ type:long double *โ the average number of kilobytes per transfer between the acquisition of previous and current ๐ธDSM_KB_PER_TRANSFER_READDSM_KB_PER_TRANSFER_WRITEDSM_KB_PER_TRANSFER_FREEโ type:long double *
those: the average number of kilobytes in the specified type transaction between the acquisition of previous and current ๐ซ
DSM_TRANSFERS_PER_SECONDโ type:long double *โ the average number of transfers per second between the acquisition of previous and current ๐ฆDSM_TRANSFERS_PER_SECOND_OTHERDSM_TRANSFERS_PER_SECOND_READDSM_TRANSFERS_PER_SECOND_WRITEDSM_TRANSFERS_PER_SECOND_FREEโ type:long double *
and those four: the average number of transactions of the specified type per second between the acquisition of previous and current โจ
DSM_MB_PER_SECONDโ type:long double *โ the average number of megabytes transferred per second between the acquisition of previous and current ๐ฅDSM_MB_PER_SECOND_READDSM_MB_PER_SECOND_WRITEDSM_MB_PER_SECOND_FREEโ type:long double *
those: the average number of megabytes per second in the specified type of transaction between the acquisition of previous and current ๐
DSM_BLOCKS_PER_SECONDโ type:long double *โ the average number of blocks transferred per second between the acquisition of previous and current.
again with the blocksize lore: this number is in terms of the blocksize reported by the device. if no blocksize has been reported (i.e., the block size is0), a default blocksize of512bytes will be used in the calculation ๐
DSM_BLOCKS_PER_SECOND_READDSM_BLOCKS_PER_SECOND_WRITEDSM_BLOCKS_PER_SECOND_FREEโ type:long double *
those three: the average number of blocks per second in the specified type of transaction between the acquisition of previous and current. yep, still in terms of the blocksize reported by the device โ and if no blocksize has been reported (i.e., the block size is 0), a default blocksize of 512 bytes will be used in the calculation ๐
DSM_MS_PER_TRANSACTIONโ type:long double *โ the average duration of transactions between the acquisition of previous and current โณDSM_MS_PER_TRANSACTION_OTHERDSM_MS_PER_TRANSACTION_READDSM_MS_PER_TRANSACTION_WRITEDSM_MS_PER_TRANSACTION_FREEโ type:long double *
those: the average duration of transactions of the specified type between the acquisition of previous and current ๐ซถ
DSM_BUSY_PCTโ type:long double *โ the percentage of time the device had one or more transactions outstanding between the acquisition of previous and current. main character never catches a break ๐ญDSM_QUEUE_LENGTHโ type:uint64_t *โ the number of not yet completed transactions at the time when current was acquired ๐DSM_SKIPโ type: N/A โ if you do not need a result fromdevstat_compute_statistics(), just putDSM_SKIPas the first (type) parameter andNULLas the second parameter. this is lowkey so useful in scenarios where the statistics to be calculated are determined at run time ๐
and ok one more vibe, bestie: the devstat_compute_etime() function provides an easy way to find the difference in seconds between two bintime structures ๐ธ this is most commonly used in conjunction with the time recorded by the devstat_getdevs() function (in struct statinfo) each time it fetches the current devstat list โจ
๐ RETURN VALUES ๐
devstat_getnumdevs(),devstat_getgeneration(), anddevstat_getversion()โ return the indicated sysctl variable, or-1if there is an error fetching the variable ๐ญdevstat_checkversion()โ returns0if the kernel and userland devstat versions match. if they do not match, it returns-1๐devstat_getdevs()anddevstat_selectdevs()โ return-1in case of an error,0if there is no error, and1if the device list or selected devices have changed.
a return value of1fromdevstat_getdevs()is usually a hint to re-rundevstat_selectdevs()because the device list has changed ๐
devstat_buildmatch()โ returns-1for error, and0if there is no error ๐ฆdevstat_compute_etime()โ returns the computed elapsed time โฑ๏ธdevstat_compute_statistics()โ returns-1for error, and0for success โจ
and if an error is returned from one of the devstat library functions, the reason for the error is generally printed in the global string devstat_errbuf which is DEVSTAT_ERRBUF_SIZE characters long ๐ข
๐ SEE ALSO ๐
systat(1), kvm(3), sysctl(3), iostat(8), rpc.rstatd(8), sysctl(8), vmstat(8), devstat(9) โ go read those too, iykyk ๐
โญ HISTORY โญ
the devstat statistics system first appeared in FreeBSD 3.0 ๐ and the new interface (the functions prefixed with devstat_) first appeared in FreeBSD 5.0 ๐ฆ
๐ AUTHORS ๐
Kenneth Merry <ken@FreeBSD.org> โ thank you king ๐ซถ
๐ BUGS ๐
- there should probably be an interface to de-allocate memory allocated by
devstat_getdevs(),devstat_selectdevs(), anddevstat_buildmatch()๐ - the
devstat_selectdevs()function should probably not select more thanmaxshowdevsdevices in "top" mode when no devices have been selected previously ๐ฌ - there should probably be functions to perform the statistics buffer swapping that goes on in most of the clients of this library ๐
- the
statinfoanddevinfostructures should probably be cleaned up and thought out a little more ๐ญ
ok that's the whole page bestie, you ate ๐ this one's from the Debian build of the page, dated December 15, 2012 โ no cap, that's the version info, mwah ๐โจ
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1998, 1999, 2001 Kenneth D. Merry
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms