Index: head/share/man/man9/devstat.9 =================================================================== --- head/share/man/man9/devstat.9 (revision 338219) +++ head/share/man/man9/devstat.9 (revision 338220) @@ -1,455 +1,544 @@ .\" .\" Copyright (c) 1998, 1999 Kenneth D. Merry. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. The name of the author may not be used to endorse or promote products .\" derived from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd May 22, 1998 +.Dd August 22, 2018 .Dt DEVSTAT 9 .Os .Sh NAME .Nm devstat , .Nm devstat_add_entry , .Nm devstat_end_transaction , .Nm devstat_end_transaction_bio , +.Nm devstat_end_transaction_bio_bt , .Nm devstat_remove_entry , -.Nm devstat_start_transaction +.Nm devstat_start_transaction , +.Nm devstat_start_transaction_bio .Nd kernel interface for keeping device statistics .Sh SYNOPSIS .In sys/devicestat.h .Ft void .Fo devstat_add_entry .Fa "struct devstat *ds" .Fa "const char *dev_name" .Fa "int unit_number" .Fa "uint32_t block_size" .Fa "devstat_support_flags flags" .Fa "devstat_type_flags device_type" .Fa "devstat_priority priority" .Fc .Ft void .Fn devstat_remove_entry "struct devstat *ds" .Ft void -.Fn devstat_start_transaction "struct devstat *ds" +.Fo devstat_start_transaction +.Fa "struct devstat *ds" +.Fa "const struct bintime *now" +.Fc .Ft void +.Fo devstat_start_transaction_bio +.Fa "struct devstat *ds" +.Fa "struct bio *bp" +.Fc +.Ft void .Fo devstat_end_transaction .Fa "struct devstat *ds" .Fa "uint32_t bytes" .Fa "devstat_tag_type tag_type" .Fa "devstat_trans_flags flags" +.Fa "const struct bintime *now" +.Fa "const struct bintime *then" .Fc .Ft void .Fo devstat_end_transaction_bio .Fa "struct devstat *ds" -.Fa "struct bio *bp" +.Fa "const struct bio *bp" .Fc +.Fc +.Ft void +.Fo devstat_end_transaction_bio_bt +.Fa "struct devstat *ds" +.Fa "const struct bio *bp" +.Fa "const struct bintime *now" +.Fc .Sh DESCRIPTION The devstat subsystem is an interface for recording device statistics, as its name implies. The idea is to keep reasonably detailed statistics while utilizing a minimum amount of CPU time to record them. Thus, no statistical calculations are actually performed in the kernel portion of the .Nm code. Instead, that is left for user programs to handle. .Pp +The historical and antiquated +.Nm +model assumed a single active IO operation per device, which is not accurate +for most disk-like drivers in the 2000s and beyond. +New consumers of the interface should almost certainly use only the "bio" +variants of the start and end transacation routines. +.Pp .Fn devstat_add_entry registers a device with the .Nm subsystem. The caller is expected to have already allocated \fBand zeroed\fR the devstat structure before calling this function. .Fn devstat_add_entry takes several arguments: .Bl -tag -width device_type .It ds The .Va devstat structure, allocated and zeroed by the client. .It dev_name -The device name, e.g.\& da, cd, sa. +The device name, e.g., da, cd, sa. .It unit_number Device unit number. .It block_size Block size of the device, if supported. If the device does not support a block size, or if the blocksize is unknown at the time the device is added to the .Nm list, it should be set to 0. .It flags Flags indicating operations supported or not supported by the device. See below for details. .It device_type The device type. This is broken into three sections: base device type -(e.g.\& direct access, CDROM, sequential access), interface type (IDE, SCSI +(e.g., direct access, CDROM, sequential access), interface type (IDE, SCSI or other) and a pass-through flag to indicate pas-through devices. See below for a complete list of types. .It priority The device priority. The priority is used to determine how devices are sorted within .Nm devstat Ns 's list of devices. Devices are sorted first by priority (highest to lowest), and then by attach order. See below for a complete list of available priorities. .El .Pp .Fn devstat_remove_entry removes a device from the .Nm subsystem. It takes the devstat structure for the device in question as an argument. The .Nm generation number is incremented and the number of devices is decremented. .Pp .Fn devstat_start_transaction registers the start of a transaction with the .Nm subsystem. +Optionally, if the caller already has a +.Fn binuptime +value available, it may be passed in +.Fa *now . +Usually the caller can just pass +.Dv NULL +for +.Fa now , +and the routine will gather the current +.Fn binuptime +itself. The busy count is incremented with each transaction start. When a device goes from idle to busy, the system uptime is recorded in the -.Va start_time +.Va busy_from field of the .Va devstat structure. .Pp +.Fn devstat_start_transaction_bio +records the +.Fn binuptime +in the provided bio's +.Fa bio_t0 +and then invokes +.Fn devstat_start_transaction . +.Pp .Fn devstat_end_transaction registers the end of a transaction with the .Nm subsystem. -It takes four arguments: +It takes six arguments: .Bl -tag -width tag_type .It ds The .Va devstat structure for the device in question. .It bytes The number of bytes transferred in this transaction. .It tag_type Transaction tag type. See below for tag types. .It flags Transaction flags indicating whether the transaction was a read, write, or whether no data was transferred. +.It now +The +.Fn binuptime +at the end of the transaction, or +.Dv NULL . +.It then +The +.Fn binuptime +at the beginning of the transaction, or +.Dv NULL . .El .Pp +If +.Fa now +is +.Dv NULL , +it collects the current time from +.Fn binuptime . +If +.Fa then +is +.Dv NULL , +the operation is not tracked in the +.Va devstat +.Fa duration +table. +.Pp .Fn devstat_end_transaction_bio +is a thin wrapper for +.Fn devstat_end_transaction_bio_bt +with a +.Dv NULL +.Fa now +parameter. +.Pp +.Fn devstat_end_transaction_bio_bt is a wrapper for .Fn devstat_end_transaction -which pulls all the information from a +which pulls all needed information from a .Va "struct bio" -which is ready for biodone(). +prepared by +.Fn devstat_start_transaction_bio . +The bio must be ready for +.Fn biodone +(i.e., +.Fa bio_bcount +and +.Fa bio_resid +must be correctly initialized). .Pp The .Va devstat structure is composed of the following fields: .Bl -tag -width dev_creation_time +.It sequence0 , +.It sequence1 +An implementation detail used to gather consistent snapshots of device +statistics. +.It start_count +Number of operations started. +.It end_count +Number of operations completed. +The +.Dq busy_count +can be calculated by subtracting +.Fa end_count +from +.Fa start_count . +.Fa ( sequence0 +and +.Fa sequence1 +are used to get a consistent snapshot.) +This is the current number of outstanding transactions for the device. +This should never go below zero, and on an idle device it should be zero. +If either one of these conditions is not true, it indicates a problem. +.Pp +There should be one and only one +transaction start event and one transaction end event for each transaction. .It dev_links Each .Va devstat structure is placed in a linked list when it is registered. The .Va dev_links field contains a pointer to the next entry in the list of .Va devstat structures. .It device_number The device number is a unique identifier for each device. The device number is incremented for each new device that is registered. The device number is currently only a 32-bit integer, but it could be enlarged if someone has a system with more than four billion device arrival events. .It device_name The device name is a text string given by the registering driver to identify itself. -(e.g.\& +(e.g., .Dq da , .Dq cd , .Dq sa , etc.) .It unit_number The unit number identifies the particular instance of the peripheral driver in question. -.It bytes_written -This is the number of bytes that have been written to the device. -This number is currently an unsigned 64 bit integer. -This will hopefully -eliminate the counter wrap that would come very quickly on some systems if -32 bit integers were used. -.It bytes_read -This is the number of bytes that have been read from the device. -.It bytes_freed -This is the number of bytes that have been freed/erased on the device. -.It num_reads -This is the number of reads from the device. -.It num_writes -This is the number of writes to the device. -.It num_frees -This is the number of free/erase operations on the device. -.It num_other -This is the number of transactions to the device which are neither reads or -writes. +.It bytes[4] +This array contains the number of bytes that have been read (index +.Dv DEVSTAT_READ ) , +written (index +.Dv DEVSTAT_WRITE ) , +freed or erased (index +.Dv DEVSTAT_FREE ) , +or other (index +.Dv DEVSTAT_NO_DATA ) . +All values are unsigned 64-bit integers. +.It operations[4] +This array contains the number of operations of a given type that have been +performed. +The indices are identical to those for +.Fa bytes +above. +.Dv DEVSTAT_NO_DATA +or "other" represents the number of transactions to the device which are +neither reads, writes, nor frees. For instance, .Tn SCSI drivers often send a test unit ready command to .Tn SCSI devices. The test unit ready command does not read or write any data. It merely causes the device to return its status. -.It busy_count -This is the current number of outstanding transactions for the device. -This should never go below zero, and on an idle device it should be zero. -If either one of these conditions is not true, it indicates a problem in -the way -.Fn devstat_start_transaction -and +.It duration[4] +This array contains the total bintime corresponding to completed operations of +a given type. +The indices are identical to those for +.Fa bytes +above. +(Operations that complete using the historical .Fn devstat_end_transaction -are being called in client code. -There should be one and only one -transaction start event and one transaction end event for each transaction. +API and do not provide a non-NULL +.Fa then +are not accounted for.) +.It busy_time +This is the amount of time that the device busy count has been greater than +zero. +This is only updated when the busy count returns to zero. +.It creation_time +This is the time, as reported by +.Fn getmicrotime +that the device was registered. .It block_size This is the block size of the device, if the device has a block size. .It tag_types This is an array of counters to record the number of various tag types that are sent to a device. See below for a list of tag types. -.It dev_creation_time -This is the time, as reported by -.Fn getmicrotime -that the device was registered. -.It busy_time -This is the amount of time that the device busy count has been greater than -zero. -This is only updated when the busy count returns to zero. -.It start_time -This is the time, as reported by -.Fn getmicrouptime -that the device busy count went from zero to one. -.It last_comp_time -This is the time as reported by -.Fn getmicrouptime -that a transaction last completed. -It is used along with -.Va start_time -to calculate the device busy time. +.It busy_from +If the device is not busy, this was the time that a transaction last completed. +If the device is busy, this the most recent of either the time that the device +became busy, or the time that the last transaction completed. .It flags These flags indicate which statistics measurements are supported by a particular device. These flags are primarily intended to serve as an aid to userland programs that decipher the statistics. .It device_type This is the device type. It consists of three parts: the device type -(e.g.\& direct access, CDROM, sequential access, etc.), the interface (IDE, +(e.g., direct access, CDROM, sequential access, etc.), the interface (IDE, SCSI or other) and whether or not the device in question is a pass-through driver. See below for a complete list of device types. .It priority This is the priority. This is the first parameter used to determine where to insert a device in the .Nm list. The second parameter is attach order. -See below for a list of -available priorities. +See below for a list of available priorities. .El .Pp Each device is given a device type. -Pass-through devices have the same -underlying device type and interface as the device they provide an -interface for, but they also have the pass-through flag set. -The base -device types are identical to the +Pass-through devices have the same underlying device type and interface as the +device they provide an interface for, but they also have the pass-through flag +set. +The base device types are identical to the .Tn SCSI device type numbers, so with .Tn SCSI -peripherals, the device type returned from an inquiry is usually ORed with -the +peripherals, the device type returned from an inquiry is usually ORed with the .Tn SCSI interface type and the pass-through flag if appropriate. The device type flags are as follows: .Bd -literal -offset indent typedef enum { DEVSTAT_TYPE_DIRECT = 0x000, DEVSTAT_TYPE_SEQUENTIAL = 0x001, DEVSTAT_TYPE_PRINTER = 0x002, DEVSTAT_TYPE_PROCESSOR = 0x003, DEVSTAT_TYPE_WORM = 0x004, DEVSTAT_TYPE_CDROM = 0x005, DEVSTAT_TYPE_SCANNER = 0x006, DEVSTAT_TYPE_OPTICAL = 0x007, DEVSTAT_TYPE_CHANGER = 0x008, DEVSTAT_TYPE_COMM = 0x009, DEVSTAT_TYPE_ASC0 = 0x00a, DEVSTAT_TYPE_ASC1 = 0x00b, DEVSTAT_TYPE_STORARRAY = 0x00c, DEVSTAT_TYPE_ENCLOSURE = 0x00d, DEVSTAT_TYPE_FLOPPY = 0x00e, DEVSTAT_TYPE_MASK = 0x00f, DEVSTAT_TYPE_IF_SCSI = 0x010, DEVSTAT_TYPE_IF_IDE = 0x020, DEVSTAT_TYPE_IF_OTHER = 0x030, DEVSTAT_TYPE_IF_MASK = 0x0f0, DEVSTAT_TYPE_PASS = 0x100 } devstat_type_flags; .Ed .Pp Devices have a priority associated with them, which controls roughly where they are placed in the .Nm list. The priorities are as follows: .Bd -literal -offset indent typedef enum { DEVSTAT_PRIORITY_MIN = 0x000, DEVSTAT_PRIORITY_OTHER = 0x020, DEVSTAT_PRIORITY_PASS = 0x030, DEVSTAT_PRIORITY_FD = 0x040, DEVSTAT_PRIORITY_WFD = 0x050, DEVSTAT_PRIORITY_TAPE = 0x060, DEVSTAT_PRIORITY_CD = 0x090, DEVSTAT_PRIORITY_DISK = 0x110, DEVSTAT_PRIORITY_ARRAY = 0x120, DEVSTAT_PRIORITY_MAX = 0xfff } devstat_priority; .Ed .Pp Each device has associated with it flags to indicate what operations are supported or not supported. The .Va devstat_support_flags values are as follows: .Bl -tag -width DEVSTAT_NO_ORDERED_TAGS .It DEVSTAT_ALL_SUPPORTED Every statistic type is supported by the device. .It DEVSTAT_NO_BLOCKSIZE This device does not have a blocksize. .It DEVSTAT_NO_ORDERED_TAGS This device does not support ordered tags. .It DEVSTAT_BS_UNAVAILABLE This device supports a blocksize, but it is currently unavailable. This flag is most often used with removable media drives. .El .Pp Transactions to a device fall into one of three categories, which are represented in the .Va flags passed into .Fn devstat_end_transaction . The transaction types are as follows: .Bd -literal -offset indent typedef enum { DEVSTAT_NO_DATA = 0x00, DEVSTAT_READ = 0x01, DEVSTAT_WRITE = 0x02, DEVSTAT_FREE = 0x03 } devstat_trans_flags; .Ed .Pp There are four possible values for the .Va tag_type argument to .Fn devstat_end_transaction : .Bl -tag -width DEVSTAT_TAG_ORDERED .It DEVSTAT_TAG_SIMPLE The transaction had a simple tag. .It DEVSTAT_TAG_HEAD The transaction had a head of queue tag. .It DEVSTAT_TAG_ORDERED The transaction had an ordered tag. .It DEVSTAT_TAG_NONE The device does not support tags. .El .Pp The tag type values correspond to the lower four bits of the .Tn SCSI tag definitions. In CAM, for instance, the .Va tag_action from the CCB is ORed with 0xf to determine the tag type to pass in to .Fn devstat_end_transaction . .Pp There is a macro, .Dv DEVSTAT_VERSION that is defined in .In sys/devicestat.h . This is the current version of the .Nm subsystem, and it should be incremented each time a change is made that would require recompilation of userland programs that access .Nm statistics. Userland programs use this version, via the .Va kern.devstat.version .Nm sysctl variable to determine whether they are in sync with the kernel .Nm structures. .Sh SEE ALSO .Xr systat 1 , .Xr devstat 3 , .Xr iostat 8 , .Xr rpc.rstatd 8 , .Xr vmstat 8 .Sh HISTORY The .Nm statistics system appeared in .Fx 3.0 . .Sh AUTHORS .An Kenneth Merry Aq Mt ken@FreeBSD.org .Sh BUGS There may be a need for .Fn spl protection around some of the .Nm list manipulation code to ensure, for example, that the list of devices is not changed while someone is fetching the .Va kern.devstat.all .Nm sysctl variable. -.Pp -It is impossible with the current -.Nm -architecture to accurately measure time per transaction. -The only feasible -way to accurately measure time per transaction would be to record a -timestamp for every transaction. -This measurement is probably not -worthwhile for most people as it would adversely affect the performance of -the system and cost space to store the timestamps for individual -transactions. Index: head/sys/kern/subr_devstat.c =================================================================== --- head/sys/kern/subr_devstat.c (revision 338219) +++ head/sys/kern/subr_devstat.c (revision 338220) @@ -1,582 +1,582 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include SDT_PROVIDER_DEFINE(io); SDT_PROBE_DEFINE2(io, , , start, "struct bio *", "struct devstat *"); SDT_PROBE_DEFINE2(io, , , done, "struct bio *", "struct devstat *"); SDT_PROBE_DEFINE2(io, , , wait__start, "struct bio *", "struct devstat *"); SDT_PROBE_DEFINE2(io, , , wait__done, "struct bio *", "struct devstat *"); #define DTRACE_DEVSTAT_START() SDT_PROBE2(io, , , start, NULL, ds) #define DTRACE_DEVSTAT_BIO_START() SDT_PROBE2(io, , , start, bp, ds) #define DTRACE_DEVSTAT_DONE() SDT_PROBE2(io, , , done, NULL, ds) #define DTRACE_DEVSTAT_BIO_DONE() SDT_PROBE2(io, , , done, bp, ds) #define DTRACE_DEVSTAT_WAIT_START() SDT_PROBE2(io, , , wait__start, NULL, ds) #define DTRACE_DEVSTAT_WAIT_DONE() SDT_PROBE2(io, , , wait__done, NULL, ds) static int devstat_num_devs; static long devstat_generation = 1; static int devstat_version = DEVSTAT_VERSION; static int devstat_current_devnumber; static struct mtx devstat_mutex; MTX_SYSINIT(devstat_mutex, &devstat_mutex, "devstat", MTX_DEF); static struct devstatlist device_statq = STAILQ_HEAD_INITIALIZER(device_statq); static struct devstat *devstat_alloc(void); static void devstat_free(struct devstat *); static void devstat_add_entry(struct devstat *ds, const void *dev_name, int unit_number, uint32_t block_size, devstat_support_flags flags, devstat_type_flags device_type, devstat_priority priority); /* * Allocate a devstat and initialize it */ struct devstat * devstat_new_entry(const void *dev_name, int unit_number, uint32_t block_size, devstat_support_flags flags, devstat_type_flags device_type, devstat_priority priority) { struct devstat *ds; mtx_assert(&devstat_mutex, MA_NOTOWNED); ds = devstat_alloc(); mtx_lock(&devstat_mutex); if (unit_number == -1) { ds->unit_number = unit_number; ds->id = dev_name; binuptime(&ds->creation_time); devstat_generation++; } else { devstat_add_entry(ds, dev_name, unit_number, block_size, flags, device_type, priority); } mtx_unlock(&devstat_mutex); return (ds); } /* * Take a malloced and zeroed devstat structure given to us, fill it in * and add it to the queue of devices. */ static void devstat_add_entry(struct devstat *ds, const void *dev_name, int unit_number, uint32_t block_size, devstat_support_flags flags, devstat_type_flags device_type, devstat_priority priority) { struct devstatlist *devstat_head; struct devstat *ds_tmp; mtx_assert(&devstat_mutex, MA_OWNED); devstat_num_devs++; devstat_head = &device_statq; /* * Priority sort. Each driver passes in its priority when it adds * its devstat entry. Drivers are sorted first by priority, and * then by probe order. * * For the first device, we just insert it, since the priority * doesn't really matter yet. Subsequent devices are inserted into * the list using the order outlined above. */ if (devstat_num_devs == 1) STAILQ_INSERT_TAIL(devstat_head, ds, dev_links); else { STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) { struct devstat *ds_next; ds_next = STAILQ_NEXT(ds_tmp, dev_links); /* * If we find a break between higher and lower * priority items, and if this item fits in the * break, insert it. This also applies if the * "lower priority item" is the end of the list. */ if ((priority <= ds_tmp->priority) && ((ds_next == NULL) || (priority > ds_next->priority))) { STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds, dev_links); break; } else if (priority > ds_tmp->priority) { /* * If this is the case, we should be able * to insert ourselves at the head of the * list. If we can't, something is wrong. */ if (ds_tmp == STAILQ_FIRST(devstat_head)) { STAILQ_INSERT_HEAD(devstat_head, ds, dev_links); break; } else { STAILQ_INSERT_TAIL(devstat_head, ds, dev_links); printf("devstat_add_entry: HELP! " "sorting problem detected " "for name %p unit %d\n", dev_name, unit_number); break; } } } } ds->device_number = devstat_current_devnumber++; ds->unit_number = unit_number; strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN); ds->block_size = block_size; ds->flags = flags; ds->device_type = device_type; ds->priority = priority; binuptime(&ds->creation_time); devstat_generation++; } /* * Remove a devstat structure from the list of devices. */ void devstat_remove_entry(struct devstat *ds) { struct devstatlist *devstat_head; mtx_assert(&devstat_mutex, MA_NOTOWNED); if (ds == NULL) return; mtx_lock(&devstat_mutex); devstat_head = &device_statq; /* Remove this entry from the devstat queue */ atomic_add_acq_int(&ds->sequence1, 1); if (ds->unit_number != -1) { devstat_num_devs--; STAILQ_REMOVE(devstat_head, ds, devstat, dev_links); } devstat_free(ds); devstat_generation++; mtx_unlock(&devstat_mutex); } /* * Record a transaction start. * * See comments for devstat_end_transaction(). Ordering is very important * here. */ void -devstat_start_transaction(struct devstat *ds, struct bintime *now) +devstat_start_transaction(struct devstat *ds, const struct bintime *now) { mtx_assert(&devstat_mutex, MA_NOTOWNED); /* sanity check */ if (ds == NULL) return; atomic_add_acq_int(&ds->sequence1, 1); /* * We only want to set the start time when we are going from idle * to busy. The start time is really the start of the latest busy * period. */ if (ds->start_count == ds->end_count) { if (now != NULL) ds->busy_from = *now; else binuptime(&ds->busy_from); } ds->start_count++; atomic_add_rel_int(&ds->sequence0, 1); DTRACE_DEVSTAT_START(); } void devstat_start_transaction_bio(struct devstat *ds, struct bio *bp) { mtx_assert(&devstat_mutex, MA_NOTOWNED); /* sanity check */ if (ds == NULL) return; binuptime(&bp->bio_t0); devstat_start_transaction(ds, &bp->bio_t0); DTRACE_DEVSTAT_BIO_START(); } /* * Record the ending of a transaction, and incrment the various counters. * * Ordering in this function, and in devstat_start_transaction() is VERY * important. The idea here is to run without locks, so we are very * careful to only modify some fields on the way "down" (i.e. at * transaction start) and some fields on the way "up" (i.e. at transaction * completion). One exception is busy_from, which we only modify in * devstat_start_transaction() when there are no outstanding transactions, * and thus it can't be modified in devstat_end_transaction() * simultaneously. * * The sequence0 and sequence1 fields are provided to enable an application * spying on the structures with mmap(2) to tell when a structure is in a * consistent state or not. * * For this to work 100% reliably, it is important that the two fields * are at opposite ends of the structure and that they are incremented * in the opposite order of how a memcpy(3) in userland would copy them. * We assume that the copying happens front to back, but there is actually * no way short of writing your own memcpy(3) replacement to guarantee * this will be the case. * * In addition to this, being a kind of locks, they must be updated with * atomic instructions using appropriate memory barriers. */ void devstat_end_transaction(struct devstat *ds, uint32_t bytes, devstat_tag_type tag_type, devstat_trans_flags flags, - struct bintime *now, struct bintime *then) + const struct bintime *now, const struct bintime *then) { struct bintime dt, lnow; /* sanity check */ if (ds == NULL) return; if (now == NULL) { + binuptime(&lnow); now = &lnow; - binuptime(now); } atomic_add_acq_int(&ds->sequence1, 1); /* Update byte and operations counts */ ds->bytes[flags] += bytes; ds->operations[flags]++; /* * Keep a count of the various tag types sent. */ if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 && tag_type != DEVSTAT_TAG_NONE) ds->tag_types[tag_type]++; if (then != NULL) { /* Update duration of operations */ dt = *now; bintime_sub(&dt, then); bintime_add(&ds->duration[flags], &dt); } /* Accumulate busy time */ dt = *now; bintime_sub(&dt, &ds->busy_from); bintime_add(&ds->busy_time, &dt); ds->busy_from = *now; ds->end_count++; atomic_add_rel_int(&ds->sequence0, 1); DTRACE_DEVSTAT_DONE(); } void -devstat_end_transaction_bio(struct devstat *ds, struct bio *bp) +devstat_end_transaction_bio(struct devstat *ds, const struct bio *bp) { devstat_end_transaction_bio_bt(ds, bp, NULL); } void -devstat_end_transaction_bio_bt(struct devstat *ds, struct bio *bp, - struct bintime *now) +devstat_end_transaction_bio_bt(struct devstat *ds, const struct bio *bp, + const struct bintime *now) { devstat_trans_flags flg; /* sanity check */ if (ds == NULL) return; if (bp->bio_cmd == BIO_DELETE) flg = DEVSTAT_FREE; else if ((bp->bio_cmd == BIO_READ) || ((bp->bio_cmd == BIO_ZONE) && (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES))) flg = DEVSTAT_READ; else if (bp->bio_cmd == BIO_WRITE) flg = DEVSTAT_WRITE; else flg = DEVSTAT_NO_DATA; devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid, DEVSTAT_TAG_SIMPLE, flg, now, &bp->bio_t0); DTRACE_DEVSTAT_BIO_DONE(); } /* * This is the sysctl handler for the devstat package. The data pushed out * on the kern.devstat.all sysctl variable consists of the current devstat * generation number, and then an array of devstat structures, one for each * device in the system. * * This is more cryptic that obvious, but basically we neither can nor * want to hold the devstat_mutex for any amount of time, so we grab it * only when we need to and keep an eye on devstat_generation all the time. */ static int sysctl_devstat(SYSCTL_HANDLER_ARGS) { int error; long mygen; struct devstat *nds; mtx_assert(&devstat_mutex, MA_NOTOWNED); /* * XXX devstat_generation should really be "volatile" but that * XXX freaks out the sysctl macro below. The places where we * XXX change it and inspect it are bracketed in the mutex which * XXX guarantees us proper write barriers. I don't believe the * XXX compiler is allowed to optimize mygen away across calls * XXX to other functions, so the following is belived to be safe. */ mygen = devstat_generation; error = SYSCTL_OUT(req, &mygen, sizeof(mygen)); if (devstat_num_devs == 0) return(0); if (error != 0) return (error); mtx_lock(&devstat_mutex); nds = STAILQ_FIRST(&device_statq); if (mygen != devstat_generation) error = EBUSY; mtx_unlock(&devstat_mutex); if (error != 0) return (error); for (;nds != NULL;) { error = SYSCTL_OUT(req, nds, sizeof(struct devstat)); if (error != 0) return (error); mtx_lock(&devstat_mutex); if (mygen != devstat_generation) error = EBUSY; else nds = STAILQ_NEXT(nds, dev_links); mtx_unlock(&devstat_mutex); if (error != 0) return (error); } return(error); } /* * Sysctl entries for devstat. The first one is a node that all the rest * hang off of. */ static SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, NULL, "Device Statistics"); SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE, NULL, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list"); /* * Export the number of devices in the system so that userland utilities * can determine how much memory to allocate to hold all the devices. */ SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD, &devstat_num_devs, 0, "Number of devices in the devstat list"); SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD, &devstat_generation, 0, "Devstat list generation"); SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD, &devstat_version, 0, "Devstat list version number"); /* * Allocator for struct devstat structures. We sub-allocate these from pages * which we get from malloc. These pages are exported for mmap(2)'ing through * a miniature device driver */ #define statsperpage (PAGE_SIZE / sizeof(struct devstat)) static d_mmap_t devstat_mmap; static struct cdevsw devstat_cdevsw = { .d_version = D_VERSION, .d_mmap = devstat_mmap, .d_name = "devstat", }; struct statspage { TAILQ_ENTRY(statspage) list; struct devstat *stat; u_int nfree; }; static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist); static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics"); static int devstat_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr) { struct statspage *spp; if (nprot != VM_PROT_READ) return (-1); mtx_lock(&devstat_mutex); TAILQ_FOREACH(spp, &pagelist, list) { if (offset == 0) { *paddr = vtophys(spp->stat); mtx_unlock(&devstat_mutex); return (0); } offset -= PAGE_SIZE; } mtx_unlock(&devstat_mutex); return (-1); } static struct devstat * devstat_alloc(void) { struct devstat *dsp; struct statspage *spp, *spp2; u_int u; static int once; mtx_assert(&devstat_mutex, MA_NOTOWNED); if (!once) { make_dev_credf(MAKEDEV_ETERNAL | MAKEDEV_CHECKNAME, &devstat_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0444, DEVSTAT_DEVICE_NAME); once = 1; } spp2 = NULL; mtx_lock(&devstat_mutex); for (;;) { TAILQ_FOREACH(spp, &pagelist, list) { if (spp->nfree > 0) break; } if (spp != NULL) break; mtx_unlock(&devstat_mutex); spp2 = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK); spp2->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK); spp2->nfree = statsperpage; /* * If free statspages were added while the lock was released * just reuse them. */ mtx_lock(&devstat_mutex); TAILQ_FOREACH(spp, &pagelist, list) if (spp->nfree > 0) break; if (spp == NULL) { spp = spp2; /* * It would make more sense to add the new page at the * head but the order on the list determine the * sequence of the mapping so we can't do that. */ TAILQ_INSERT_TAIL(&pagelist, spp, list); } else break; } dsp = spp->stat; for (u = 0; u < statsperpage; u++) { if (dsp->allocated == 0) break; dsp++; } spp->nfree--; dsp->allocated = 1; mtx_unlock(&devstat_mutex); if (spp2 != NULL && spp2 != spp) { free(spp2->stat, M_DEVSTAT); free(spp2, M_DEVSTAT); } return (dsp); } static void devstat_free(struct devstat *dsp) { struct statspage *spp; mtx_assert(&devstat_mutex, MA_OWNED); bzero(dsp, sizeof *dsp); TAILQ_FOREACH(spp, &pagelist, list) { if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) { spp->nfree++; return; } } } SYSCTL_INT(_debug_sizeof, OID_AUTO, devstat, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, sizeof(struct devstat), "sizeof(struct devstat)"); Index: head/sys/sys/devicestat.h =================================================================== --- head/sys/sys/devicestat.h (revision 338219) +++ head/sys/sys/devicestat.h (revision 338220) @@ -1,208 +1,209 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _DEVICESTAT_H #define _DEVICESTAT_H #include #include /* * XXX: Should really be SPECNAMELEN */ #define DEVSTAT_NAME_LEN 16 /* * device name for the mmap device */ #define DEVSTAT_DEVICE_NAME "devstat" /* * ATTENTION: The devstat version below should be incremented any time a * change is made in struct devstat, or any time a change is made in the * enumerated types that struct devstat uses. (Only if those changes * would require a recompile -- i.e. re-arranging the order of an * enumerated type or something like that.) This version number is used by * userland utilities to determine whether or not they are in sync with the * kernel. */ #define DEVSTAT_VERSION 6 /* * These flags specify which statistics features are supported or not * supported by a particular device. The default is all statistics are * supported. */ typedef enum { DEVSTAT_ALL_SUPPORTED = 0x00, DEVSTAT_NO_BLOCKSIZE = 0x01, DEVSTAT_NO_ORDERED_TAGS = 0x02, DEVSTAT_BS_UNAVAILABLE = 0x04 } devstat_support_flags; typedef enum { DEVSTAT_NO_DATA = 0x00, DEVSTAT_READ = 0x01, DEVSTAT_WRITE = 0x02, DEVSTAT_FREE = 0x03 } devstat_trans_flags; #define DEVSTAT_N_TRANS_FLAGS 4 typedef enum { DEVSTAT_TAG_SIMPLE = 0x00, DEVSTAT_TAG_HEAD = 0x01, DEVSTAT_TAG_ORDERED = 0x02, DEVSTAT_TAG_NONE = 0x03 } devstat_tag_type; typedef enum { DEVSTAT_PRIORITY_MIN = 0x000, DEVSTAT_PRIORITY_OTHER = 0x020, DEVSTAT_PRIORITY_PASS = 0x030, DEVSTAT_PRIORITY_FD = 0x040, DEVSTAT_PRIORITY_WFD = 0x050, DEVSTAT_PRIORITY_TAPE = 0x060, DEVSTAT_PRIORITY_CD = 0x090, DEVSTAT_PRIORITY_DISK = 0x110, DEVSTAT_PRIORITY_ARRAY = 0x120, DEVSTAT_PRIORITY_MAX = 0xfff } devstat_priority; /* * These types are intended to aid statistics gathering/display programs. * The first 13 types (up to the 'target' flag) are identical numerically * to the SCSI device type numbers. The next 3 types designate the device * interface. Currently the choices are IDE, SCSI, and 'other'. The last * flag specifies whether or not the given device is a passthrough device * or not. If it is a passthrough device, the lower 4 bits specify which * type of physical device lies under the passthrough device, and the next * 4 bits specify the interface. */ typedef enum { DEVSTAT_TYPE_DIRECT = 0x000, DEVSTAT_TYPE_SEQUENTIAL = 0x001, DEVSTAT_TYPE_PRINTER = 0x002, DEVSTAT_TYPE_PROCESSOR = 0x003, DEVSTAT_TYPE_WORM = 0x004, DEVSTAT_TYPE_CDROM = 0x005, DEVSTAT_TYPE_SCANNER = 0x006, DEVSTAT_TYPE_OPTICAL = 0x007, DEVSTAT_TYPE_CHANGER = 0x008, DEVSTAT_TYPE_COMM = 0x009, DEVSTAT_TYPE_ASC0 = 0x00a, DEVSTAT_TYPE_ASC1 = 0x00b, DEVSTAT_TYPE_STORARRAY = 0x00c, DEVSTAT_TYPE_ENCLOSURE = 0x00d, DEVSTAT_TYPE_FLOPPY = 0x00e, DEVSTAT_TYPE_MASK = 0x00f, DEVSTAT_TYPE_IF_SCSI = 0x010, DEVSTAT_TYPE_IF_IDE = 0x020, DEVSTAT_TYPE_IF_OTHER = 0x030, DEVSTAT_TYPE_IF_MASK = 0x0f0, DEVSTAT_TYPE_PASS = 0x100 } devstat_type_flags; /* * XXX: Next revision should add * off_t offset[DEVSTAT_N_TRANS_FLAGS]; * XXX: which should contain the offset of the last completed transfer. */ struct devstat { /* Internal house-keeping fields */ u_int sequence0; /* Update sequence# */ int allocated; /* Allocated entry */ u_int start_count; /* started ops */ u_int end_count; /* completed ops */ struct bintime busy_from; /* * busy time unaccounted * for since this time */ STAILQ_ENTRY(devstat) dev_links; u_int32_t device_number; /* * Devstat device * number. */ char device_name[DEVSTAT_NAME_LEN]; int unit_number; u_int64_t bytes[DEVSTAT_N_TRANS_FLAGS]; u_int64_t operations[DEVSTAT_N_TRANS_FLAGS]; struct bintime duration[DEVSTAT_N_TRANS_FLAGS]; struct bintime busy_time; struct bintime creation_time; /* * Time the device was * created. */ u_int32_t block_size; /* Block size, bytes */ u_int64_t tag_types[3]; /* * The number of * simple, ordered, * and head of queue * tags sent. */ devstat_support_flags flags; /* * Which statistics * are supported by a * given device. */ devstat_type_flags device_type; /* Device type */ devstat_priority priority; /* Controls list pos. */ const void *id; /* * Identification for * GEOM nodes */ u_int sequence1; /* Update sequence# */ }; STAILQ_HEAD(devstatlist, devstat); #ifdef _KERNEL struct bio; struct devstat *devstat_new_entry(const void *dev_name, int unit_number, u_int32_t block_size, devstat_support_flags flags, devstat_type_flags device_type, devstat_priority priority); void devstat_remove_entry(struct devstat *ds); -void devstat_start_transaction(struct devstat *ds, struct bintime *now); +void devstat_start_transaction(struct devstat *ds, const struct bintime *now); void devstat_start_transaction_bio(struct devstat *ds, struct bio *bp); void devstat_end_transaction(struct devstat *ds, u_int32_t bytes, devstat_tag_type tag_type, devstat_trans_flags flags, - struct bintime *now, struct bintime *then); -void devstat_end_transaction_bio(struct devstat *ds, struct bio *bp); -void devstat_end_transaction_bio_bt(struct devstat *ds, struct bio *bp, - struct bintime *now); + const struct bintime *now, + const struct bintime *then); +void devstat_end_transaction_bio(struct devstat *ds, const struct bio *bp); +void devstat_end_transaction_bio_bt(struct devstat *ds, const struct bio *bp, + const struct bintime *now); #endif #endif /* _DEVICESTAT_H */