2 * Copyright (c) 1997, 1998 Kenneth D. Merry.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/errno.h>
35 #include <sys/resource.h>
36 #include <sys/queue.h>
52 compute_stats(struct devstat *current, struct devstat *previous,
53 long double etime, u_int64_t *total_bytes,
54 u_int64_t *total_transfers, u_int64_t *total_blocks,
55 long double *kb_per_transfer, long double *transfers_per_second,
56 long double *mb_per_second, long double *blocks_per_second,
57 long double *ms_per_transaction);
66 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
69 * Table to match descriptive strings with device types. These are in
70 * order from most common to least common to speed search time.
72 struct devstat_match_table match_table[] = {
73 {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE},
74 {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE},
75 {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF},
76 {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF},
77 {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF},
78 {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE},
79 {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE},
80 {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS},
81 {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE},
82 {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE},
83 {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE},
84 {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE},
85 {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE},
86 {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE},
87 {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE},
88 {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE},
89 {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE},
94 devstat_metric metric;
95 devstat_arg_type argtype;
96 } devstat_arg_list[] = {
97 { DSM_NONE, DEVSTAT_ARG_NOTYPE },
98 { DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 },
99 { DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 },
100 { DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 },
101 { DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 },
102 { DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 },
103 { DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 },
104 { DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 },
105 { DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 },
106 { DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 },
107 { DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 },
108 { DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD },
109 { DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD },
110 { DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD },
111 { DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD },
112 { DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD },
113 { DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
114 { DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD },
115 { DSM_MB_PER_SECOND, DEVSTAT_ARG_LD },
116 { DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD },
117 { DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
118 { DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD },
119 { DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD },
120 { DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
121 { DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD },
122 { DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD },
123 { DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD },
124 { DSM_SKIP, DEVSTAT_ARG_SKIP },
125 { DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 },
126 { DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 },
127 { DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 },
128 { DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD },
129 { DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD },
130 { DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
131 { DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
132 { DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD },
133 { DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD },
134 { DSM_BUSY_PCT, DEVSTAT_ARG_LD },
135 { DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 },
138 static const char *namelist[] = {
141 #define X_GENERATION 1
142 "_devstat_generation",
145 #define X_DEVICE_STATQ 3
151 * Local function declarations.
153 static int compare_select(const void *arg1, const void *arg2);
154 static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes);
155 static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes);
156 static char *get_devstat_kvm(kvm_t *kd);
158 #define KREADNL(kd, var, val) \
159 readkmem_nl(kd, namelist[var], &val, sizeof(val))
162 devstat_getnumdevs(kvm_t *kd)
167 numdevsize = sizeof(int);
170 * Find out how many devices we have in the system.
173 if (sysctlbyname("kern.devstat.numdevs", &numdevs,
174 &numdevsize, NULL, 0) == -1) {
175 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
176 "%s: error getting number of devices\n"
177 "%s: %s", __func__, __func__,
184 if (KREADNL(kd, X_NUMDEVS, numdevs) == -1)
192 * This is an easy way to get the generation number, but the generation is
193 * supplied in a more atmoic manner by the kern.devstat.all sysctl.
194 * Because this generation sysctl is separate from the statistics sysctl,
195 * the device list and the generation could change between the time that
196 * this function is called and the device list is retreived.
199 devstat_getgeneration(kvm_t *kd)
204 gensize = sizeof(long);
207 * Get the current generation number.
210 if (sysctlbyname("kern.devstat.generation", &generation,
211 &gensize, NULL, 0) == -1) {
212 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
213 "%s: error getting devstat generation\n%s: %s",
214 __func__, __func__, strerror(errno));
219 if (KREADNL(kd, X_GENERATION, generation) == -1)
227 * Get the current devstat version. The return value of this function
228 * should be compared with DEVSTAT_VERSION, which is defined in
229 * sys/devicestat.h. This will enable userland programs to determine
230 * whether they are out of sync with the kernel.
233 devstat_getversion(kvm_t *kd)
238 versize = sizeof(int);
241 * Get the current devstat version.
244 if (sysctlbyname("kern.devstat.version", &version, &versize,
246 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
247 "%s: error getting devstat version\n%s: %s",
248 __func__, __func__, strerror(errno));
253 if (KREADNL(kd, X_VERSION, version) == -1)
261 * Check the devstat version we know about against the devstat version the
262 * kernel knows about. If they don't match, print an error into the
263 * devstat error buffer, and return -1. If they match, return 0.
266 devstat_checkversion(kvm_t *kd)
268 int buflen, res, retval = 0, version;
270 version = devstat_getversion(kd);
272 if (version != DEVSTAT_VERSION) {
274 * If getversion() returns an error (i.e. -1), then it
275 * has printed an error message in the buffer. Therefore,
276 * we need to add a \n to the end of that message before we
277 * print our own message in the buffer.
280 buflen = strlen(devstat_errbuf);
284 res = snprintf(devstat_errbuf + buflen,
285 DEVSTAT_ERRBUF_SIZE - buflen,
286 "%s%s: userland devstat version %d is not "
287 "the same as the kernel\n%s: devstat "
288 "version %d\n", version == -1 ? "\n" : "",
289 __func__, DEVSTAT_VERSION, __func__, version);
292 devstat_errbuf[buflen] = '\0';
294 buflen = strlen(devstat_errbuf);
295 if (version < DEVSTAT_VERSION)
296 res = snprintf(devstat_errbuf + buflen,
297 DEVSTAT_ERRBUF_SIZE - buflen,
298 "%s: libdevstat newer than kernel\n",
301 res = snprintf(devstat_errbuf + buflen,
302 DEVSTAT_ERRBUF_SIZE - buflen,
303 "%s: kernel newer than libdevstat\n",
307 devstat_errbuf[buflen] = '\0';
316 * Get the current list of devices and statistics, and the current
321 * 0 -- device list is unchanged
322 * 1 -- device list has changed
325 devstat_getdevs(kvm_t *kd, struct statinfo *stats)
332 struct devinfo *dinfo;
335 dinfo = stats->dinfo;
338 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
339 "%s: stats->dinfo was NULL", __func__);
343 oldnumdevs = dinfo->numdevs;
344 oldgeneration = dinfo->generation;
346 clock_gettime(CLOCK_MONOTONIC, &ts);
347 stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9;
350 /* If this is our first time through, mem_ptr will be null. */
351 if (dinfo->mem_ptr == NULL) {
353 * Get the number of devices. If it's negative, it's an
354 * error. Don't bother setting the error string, since
355 * getnumdevs() has already done that for us.
357 if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
361 * The kern.devstat.all sysctl returns the current
362 * generation number, as well as all the devices.
363 * So we need four bytes more.
365 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
367 dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
369 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
373 * Request all of the devices. We only really allow for one
374 * ENOMEM failure. It would, of course, be possible to just go
375 * in a loop and keep reallocing the device structure until we
376 * don't get ENOMEM back. I'm not sure it's worth it, though.
377 * If devices are being added to the system that quickly, maybe
378 * the user can just wait until all devices are added.
381 error = sysctlbyname("kern.devstat.all",
384 if (error != -1 || errno != EBUSY)
389 * If we get ENOMEM back, that means that there are
390 * more devices now, so we need to allocate more
391 * space for the device array.
393 if (errno == ENOMEM) {
395 * No need to set the error string here,
396 * devstat_getnumdevs() will do that if it fails.
398 if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
401 dssize = (dinfo->numdevs *
402 sizeof(struct devstat)) + sizeof(long);
403 dinfo->mem_ptr = (u_int8_t *)
404 realloc(dinfo->mem_ptr, dssize);
405 if ((error = sysctlbyname("kern.devstat.all",
406 dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
407 snprintf(devstat_errbuf,
408 sizeof(devstat_errbuf),
409 "%s: error getting device "
410 "stats\n%s: %s", __func__,
411 __func__, strerror(errno));
415 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
416 "%s: error getting device stats\n"
417 "%s: %s", __func__, __func__,
425 * This is of course non-atomic, but since we are working
426 * on a core dump, the generation is unlikely to change
428 if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1)
430 if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL)
434 * The sysctl spits out the generation as the first four bytes,
435 * then all of the device statistics structures.
437 dinfo->generation = *(long *)dinfo->mem_ptr;
440 * If the generation has changed, and if the current number of
441 * devices is not the same as the number of devices recorded in the
442 * devinfo structure, it is likely that the device list has shrunk.
443 * The reason that it is likely that the device list has shrunk in
444 * this case is that if the device list has grown, the sysctl above
445 * will return an ENOMEM error, and we will reset the number of
446 * devices and reallocate the device array. If the second sysctl
447 * fails, we will return an error and therefore never get to this
448 * point. If the device list has shrunk, the sysctl will not
449 * return an error since we have more space allocated than is
450 * necessary. So, in the shrinkage case, we catch it here and
451 * reallocate the array so that we don't use any more space than is
454 if (oldgeneration != dinfo->generation) {
455 if (devstat_getnumdevs(kd) != dinfo->numdevs) {
456 if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
458 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
460 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
466 dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
474 * Devices are selected/deselected based upon the following criteria:
475 * - devices specified by the user on the command line
476 * - devices matching any device type expressions given on the command line
477 * - devices with the highest I/O, if 'top' mode is enabled
478 * - the first n unselected devices in the device list, if maxshowdevs
479 * devices haven't already been selected and if the user has not
480 * specified any devices on the command line and if we're in "add" mode.
483 * - device selection list (dev_select)
484 * - current number of devices selected (num_selected)
485 * - total number of devices in the selection list (num_selections)
486 * - devstat generation as of the last time selectdevs() was called
487 * (select_generation)
488 * - current devstat generation (current_generation)
489 * - current list of devices and statistics (devices)
490 * - number of devices in the current device list (numdevs)
491 * - compiled version of the command line device type arguments (matches)
492 * - This is optional. If the number of devices is 0, this will be ignored.
493 * - The matching code pays attention to the current selection mode. So
494 * if you pass in a matching expression, it will be evaluated based
495 * upon the selection mode that is passed in. See below for details.
496 * - number of device type matching expressions (num_matches)
497 * - Set to 0 to disable the matching code.
498 * - list of devices specified on the command line by the user (dev_selections)
499 * - number of devices selected on the command line by the user
500 * (num_dev_selections)
501 * - Our selection mode. There are four different selection modes:
502 * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly
503 * selected by the user or devices matching a pattern given by the
504 * user will be selected in addition to devices that are already
505 * selected. Additional devices will be selected, up to maxshowdevs
507 * - only mode. (DS_SELECT_ONLY) Only devices matching devices
508 * explicitly given by the user or devices matching a pattern
509 * given by the user will be selected. No other devices will be
511 * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and
512 * only. Basically, this will not de-select any devices that are
513 * current selected, as only mode would, but it will also not
514 * gratuitously select up to maxshowdevs devices as add mode would.
515 * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices
516 * explicitly selected by the user or devices matching a pattern
517 * given by the user will be de-selected.
518 * - maximum number of devices we can select (maxshowdevs)
519 * - flag indicating whether or not we're in 'top' mode (perf_select)
522 * - the device selection list may be modified and passed back out
523 * - the number of devices selected and the total number of items in the
524 * device selection list may be changed
525 * - the selection generation may be changed to match the current generation
529 * 0 -- selected devices are unchanged
530 * 1 -- selected devices changed
533 devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
534 int *num_selections, long *select_generation,
535 long current_generation, struct devstat *devices,
536 int numdevs, struct devstat_match *matches, int num_matches,
537 char **dev_selections, int num_dev_selections,
538 devstat_select_mode select_mode, int maxshowdevs,
542 int init_selections = 0, init_selected_var = 0;
543 struct device_selection *old_dev_select = NULL;
544 int old_num_selections = 0, old_num_selected;
545 int selection_number = 0;
546 int changed = 0, found = 0;
548 if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0))
552 * We always want to make sure that we have as many dev_select
553 * entries as there are devices.
556 * In this case, we haven't selected devices before.
558 if (*dev_select == NULL) {
559 *dev_select = (struct device_selection *)malloc(numdevs *
560 sizeof(struct device_selection));
561 *select_generation = current_generation;
565 * In this case, we have selected devices before, but the device
566 * list has changed since we last selected devices, so we need to
567 * either enlarge or reduce the size of the device selection list.
569 } else if (*num_selections != numdevs) {
570 *dev_select = (struct device_selection *)realloc(*dev_select,
571 numdevs * sizeof(struct device_selection));
572 *select_generation = current_generation;
575 * In this case, we've selected devices before, and the selection
576 * list is the same size as it was the last time, but the device
579 } else if (*select_generation < current_generation) {
580 *select_generation = current_generation;
585 * If we're in "only" mode, we want to clear out the selected
586 * variable since we're going to select exactly what the user wants
589 if (select_mode == DS_SELECT_ONLY)
590 init_selected_var = 1;
593 * In all cases, we want to back up the number of selected devices.
594 * It is a quick and accurate way to determine whether the selected
595 * devices have changed.
597 old_num_selected = *num_selected;
600 * We want to make a backup of the current selection list if
601 * the list of devices has changed, or if we're in performance
602 * selection mode. In both cases, we don't want to make a backup
603 * if we already know for sure that the list will be different.
604 * This is certainly the case if this is our first time through the
607 if (((init_selected_var != 0) || (init_selections != 0)
608 || (perf_select != 0)) && (changed == 0)){
609 old_dev_select = (struct device_selection *)malloc(
610 *num_selections * sizeof(struct device_selection));
611 old_num_selections = *num_selections;
612 bcopy(*dev_select, old_dev_select,
613 sizeof(struct device_selection) * *num_selections);
616 if (init_selections != 0) {
617 bzero(*dev_select, sizeof(struct device_selection) * numdevs);
619 for (i = 0; i < numdevs; i++) {
620 (*dev_select)[i].device_number =
621 devices[i].device_number;
622 strncpy((*dev_select)[i].device_name,
623 devices[i].device_name,
625 (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
626 (*dev_select)[i].unit_number = devices[i].unit_number;
627 (*dev_select)[i].position = i;
629 *num_selections = numdevs;
630 } else if (init_selected_var != 0) {
631 for (i = 0; i < numdevs; i++)
632 (*dev_select)[i].selected = 0;
635 /* we haven't gotten around to selecting anything yet.. */
636 if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
637 || (init_selected_var != 0))
641 * Look through any devices the user specified on the command line
642 * and see if they match known devices. If so, select them.
644 for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
647 snprintf(tmpstr, sizeof(tmpstr), "%s%d",
648 (*dev_select)[i].device_name,
649 (*dev_select)[i].unit_number);
650 for (j = 0; j < num_dev_selections; j++) {
651 if (strcmp(tmpstr, dev_selections[j]) == 0) {
653 * Here we do different things based on the
654 * mode we're in. If we're in add or
655 * addonly mode, we only select this device
656 * if it hasn't already been selected.
657 * Otherwise, we would be unnecessarily
658 * changing the selection order and
659 * incrementing the selection count. If
660 * we're in only mode, we unconditionally
661 * select this device, since in only mode
662 * any previous selections are erased and
663 * manually specified devices are the first
664 * ones to be selected. If we're in remove
665 * mode, we de-select the specified device and
666 * decrement the selection count.
668 switch(select_mode) {
670 case DS_SELECT_ADDONLY:
671 if ((*dev_select)[i].selected)
675 (*dev_select)[i].selected =
679 case DS_SELECT_REMOVE:
680 (*dev_select)[i].selected = 0;
683 * This isn't passed back out, we
684 * just use it to keep track of
685 * how many devices we've removed.
687 num_dev_selections--;
696 * Go through the user's device type expressions and select devices
697 * accordingly. We only do this if the number of devices already
698 * selected is less than the maximum number we can show.
700 for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
701 /* We should probably indicate some error here */
702 if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
703 || (matches[i].num_match_categories <= 0))
706 for (j = 0; j < numdevs; j++) {
707 int num_match_categories;
709 num_match_categories = matches[i].num_match_categories;
712 * Determine whether or not the current device
713 * matches the given matching expression. This if
714 * statement consists of three components:
715 * - the device type check
716 * - the device interface check
717 * - the passthrough check
718 * If a the matching test is successful, it
719 * decrements the number of matching categories,
720 * and if we've reached the last element that
721 * needed to be matched, the if statement succeeds.
724 if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
725 && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
726 (matches[i].device_type & DEVSTAT_TYPE_MASK))
727 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
728 || (((matches[i].match_fields &
729 DEVSTAT_MATCH_PASS) == 0)
730 && ((devices[j].device_type &
731 DEVSTAT_TYPE_PASS) == 0)))
732 && (--num_match_categories == 0))
733 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
734 && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
735 (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
736 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
737 || (((matches[i].match_fields &
738 DEVSTAT_MATCH_PASS) == 0)
739 && ((devices[j].device_type &
740 DEVSTAT_TYPE_PASS) == 0)))
741 && (--num_match_categories == 0))
742 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
743 && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
744 && (--num_match_categories == 0))) {
747 * This is probably a non-optimal solution
748 * to the problem that the devices in the
749 * device list will not be in the same
750 * order as the devices in the selection
753 for (k = 0; k < numdevs; k++) {
754 if ((*dev_select)[k].position == j) {
761 * There shouldn't be a case where a device
762 * in the device list is not in the
763 * selection list...but it could happen.
766 fprintf(stderr, "selectdevs: couldn't"
767 " find %s%d in selection "
769 devices[j].device_name,
770 devices[j].unit_number);
775 * We do different things based upon the
776 * mode we're in. If we're in add or only
777 * mode, we go ahead and select this device
778 * if it hasn't already been selected. If
779 * it has already been selected, we leave
780 * it alone so we don't mess up the
781 * selection ordering. Manually specified
782 * devices have already been selected, and
783 * they have higher priority than pattern
784 * matched devices. If we're in remove
785 * mode, we de-select the given device and
786 * decrement the selected count.
788 switch(select_mode) {
790 case DS_SELECT_ADDONLY:
792 if ((*dev_select)[k].selected != 0)
794 (*dev_select)[k].selected =
798 case DS_SELECT_REMOVE:
799 (*dev_select)[k].selected = 0;
808 * Here we implement "top" mode. Devices are sorted in the
809 * selection array based on two criteria: whether or not they are
810 * selected (not selection number, just the fact that they are
811 * selected!) and the number of bytes in the "bytes" field of the
812 * selection structure. The bytes field generally must be kept up
813 * by the user. In the future, it may be maintained by library
814 * functions, but for now the user has to do the work.
816 * At first glance, it may seem wrong that we don't go through and
817 * select every device in the case where the user hasn't specified
818 * any devices or patterns. In fact, though, it won't make any
819 * difference in the device sorting. In that particular case (i.e.
820 * when we're in "add" or "only" mode, and the user hasn't
821 * specified anything) the first time through no devices will be
822 * selected, so the only criterion used to sort them will be their
823 * performance. The second time through, and every time thereafter,
824 * all devices will be selected, so again selection won't matter.
826 if (perf_select != 0) {
828 /* Sort the device array by throughput */
829 qsort(*dev_select, *num_selections,
830 sizeof(struct device_selection),
833 if (*num_selected == 0) {
835 * Here we select every device in the array, if it
836 * isn't already selected. Because the 'selected'
837 * variable in the selection array entries contains
838 * the selection order, the devstats routine can show
839 * the devices that were selected first.
841 for (i = 0; i < *num_selections; i++) {
842 if ((*dev_select)[i].selected == 0) {
843 (*dev_select)[i].selected =
849 selection_number = 0;
850 for (i = 0; i < *num_selections; i++) {
851 if ((*dev_select)[i].selected != 0) {
852 (*dev_select)[i].selected =
860 * If we're in the "add" selection mode and if we haven't already
861 * selected maxshowdevs number of devices, go through the array and
862 * select any unselected devices. If we're in "only" mode, we
863 * obviously don't want to select anything other than what the user
864 * specifies. If we're in "remove" mode, it probably isn't a good
865 * idea to go through and select any more devices, since we might
866 * end up selecting something that the user wants removed. Through
867 * more complicated logic, we could actually figure this out, but
868 * that would probably require combining this loop with the various
869 * selections loops above.
871 if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
872 for (i = 0; i < *num_selections; i++)
873 if ((*dev_select)[i].selected == 0) {
874 (*dev_select)[i].selected = ++selection_number;
880 * Look at the number of devices that have been selected. If it
881 * has changed, set the changed variable. Otherwise, if we've
882 * made a backup of the selection list, compare it to the current
883 * selection list to see if the selected devices have changed.
885 if ((changed == 0) && (old_num_selected != *num_selected))
887 else if ((changed == 0) && (old_dev_select != NULL)) {
889 * Now we go through the selection list and we look at
890 * it three different ways.
892 for (i = 0; (i < *num_selections) && (changed == 0) &&
893 (i < old_num_selections); i++) {
895 * If the device at index i in both the new and old
896 * selection arrays has the same device number and
897 * selection status, it hasn't changed. We
898 * continue on to the next index.
900 if (((*dev_select)[i].device_number ==
901 old_dev_select[i].device_number)
902 && ((*dev_select)[i].selected ==
903 old_dev_select[i].selected))
907 * Now, if we're still going through the if
908 * statement, the above test wasn't true. So we
909 * check here to see if the device at index i in
910 * the current array is the same as the device at
911 * index i in the old array. If it is, that means
912 * that its selection number has changed. Set
913 * changed to 1 and exit the loop.
915 else if ((*dev_select)[i].device_number ==
916 old_dev_select[i].device_number) {
921 * If we get here, then the device at index i in
922 * the current array isn't the same device as the
923 * device at index i in the old array.
929 * Search through the old selection array
930 * looking for a device with the same
931 * device number as the device at index i
932 * in the current array. If the selection
933 * status is the same, then we mark it as
934 * found. If the selection status isn't
935 * the same, we break out of the loop.
936 * Since found isn't set, changed will be
939 for (j = 0; j < old_num_selections; j++) {
940 if (((*dev_select)[i].device_number ==
941 old_dev_select[j].device_number)
942 && ((*dev_select)[i].selected ==
943 old_dev_select[j].selected)){
947 else if ((*dev_select)[i].device_number
948 == old_dev_select[j].device_number)
956 if (old_dev_select != NULL)
957 free(old_dev_select);
963 * Comparison routine for qsort() above. Note that the comparison here is
964 * backwards -- generally, it should return a value to indicate whether
965 * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason
966 * it returns the opposite is so that the selection array will be sorted in
967 * order of decreasing performance. We sort on two parameters. The first
968 * sort key is whether or not one or the other of the devices in question
969 * has been selected. If one of them has, and the other one has not, the
970 * selected device is automatically more important than the unselected
971 * device. If neither device is selected, we judge the devices based upon
975 compare_select(const void *arg1, const void *arg2)
977 if ((((const struct device_selection *)arg1)->selected)
978 && (((const struct device_selection *)arg2)->selected == 0))
980 else if ((((const struct device_selection *)arg1)->selected == 0)
981 && (((const struct device_selection *)arg2)->selected))
983 else if (((const struct device_selection *)arg2)->bytes <
984 ((const struct device_selection *)arg1)->bytes)
986 else if (((const struct device_selection *)arg2)->bytes >
987 ((const struct device_selection *)arg1)->bytes)
994 * Take a string with the general format "arg1,arg2,arg3", and build a
995 * device matching expression from it.
998 devstat_buildmatch(char *match_str, struct devstat_match **matches,
1006 /* We can't do much without a string to parse */
1007 if (match_str == NULL) {
1008 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1009 "%s: no match expression", __func__);
1014 * Break the (comma delimited) input string out into separate strings.
1016 for (tempstr = tstr, num_args = 0;
1017 (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);
1019 if (**tempstr != '\0')
1020 if (++tempstr >= &tstr[5])
1023 /* The user gave us too many type arguments */
1025 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1026 "%s: too many type arguments", __func__);
1031 * Since you can't realloc a pointer that hasn't been malloced
1032 * first, we malloc first and then realloc.
1034 if (*num_matches == 0)
1035 *matches = (struct devstat_match *)malloc(
1036 sizeof(struct devstat_match));
1038 *matches = (struct devstat_match *)realloc(*matches,
1039 sizeof(struct devstat_match) * (*num_matches + 1));
1041 /* Make sure the current entry is clear */
1042 bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
1045 * Step through the arguments the user gave us and build a device
1046 * matching expression from them.
1048 for (i = 0; i < num_args; i++) {
1049 char *tempstr2, *tempstr3;
1052 * Get rid of leading white space.
1055 while (isspace(*tempstr2) && (*tempstr2 != '\0'))
1059 * Get rid of trailing white space.
1061 tempstr3 = &tempstr2[strlen(tempstr2) - 1];
1063 while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
1064 && (isspace(*tempstr3))) {
1070 * Go through the match table comparing the user's
1071 * arguments to known device types, interfaces, etc.
1073 for (j = 0; match_table[j].match_str != NULL; j++) {
1075 * We do case-insensitive matching, in case someone
1076 * wants to enter "SCSI" instead of "scsi" or
1077 * something like that. Only compare as many
1078 * characters as are in the string in the match
1079 * table. This should help if someone tries to use
1080 * a super-long match expression.
1082 if (strncasecmp(tempstr2, match_table[j].match_str,
1083 strlen(match_table[j].match_str)) == 0) {
1085 * Make sure the user hasn't specified two
1086 * items of the same type, like "da" and
1087 * "cd". One device cannot be both.
1089 if (((*matches)[*num_matches].match_fields &
1090 match_table[j].match_field) != 0) {
1091 snprintf(devstat_errbuf,
1092 sizeof(devstat_errbuf),
1093 "%s: cannot have more than "
1094 "one match item in a single "
1095 "category", __func__);
1099 * If we've gotten this far, we have a
1100 * winner. Set the appropriate fields in
1103 (*matches)[*num_matches].match_fields |=
1104 match_table[j].match_field;
1105 (*matches)[*num_matches].device_type |=
1106 match_table[j].type;
1107 (*matches)[*num_matches].num_match_categories++;
1112 * We should have found a match in the above for loop. If
1113 * not, that means the user entered an invalid device type
1116 if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
1117 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1118 "%s: unknown match item \"%s\"", __func__,
1130 * Compute a number of device statistics. Only one field is mandatory, and
1131 * that is "current". Everything else is optional. The caller passes in
1132 * pointers to variables to hold the various statistics he desires. If he
1133 * doesn't want a particular staistic, he should pass in a NULL pointer.
1139 compute_stats(struct devstat *current, struct devstat *previous,
1140 long double etime, u_int64_t *total_bytes,
1141 u_int64_t *total_transfers, u_int64_t *total_blocks,
1142 long double *kb_per_transfer, long double *transfers_per_second,
1143 long double *mb_per_second, long double *blocks_per_second,
1144 long double *ms_per_transaction)
1146 return(devstat_compute_statistics(current, previous, etime,
1147 total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP,
1149 total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP,
1151 total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP,
1153 kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP,
1155 transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP,
1156 transfers_per_second,
1157 mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP,
1159 blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP,
1161 ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP,
1167 /* This is 1/2^64 */
1168 #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20
1171 devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time)
1175 etime = cur_time->sec;
1176 etime += cur_time->frac * BINTIME_SCALE;
1177 if (prev_time != NULL) {
1178 etime -= prev_time->sec;
1179 etime -= prev_time->frac * BINTIME_SCALE;
1184 #define DELTA(field, index) \
1185 (current->field[(index)] - (previous ? previous->field[(index)] : 0))
1187 #define DELTA_T(field) \
1188 devstat_compute_etime(¤t->field, \
1189 (previous ? &previous->field : NULL))
1192 devstat_compute_statistics(struct devstat *current, struct devstat *previous,
1193 long double etime, ...)
1195 u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree;
1196 u_int64_t totaltransfers, totaltransfersread, totaltransferswrite;
1197 u_int64_t totaltransfersother, totalblocks, totalblocksread;
1198 u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree;
1200 devstat_metric metric;
1202 long double *destld;
1208 * current is the only mandatory field.
1210 if (current == NULL) {
1211 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1212 "%s: current stats structure was NULL", __func__);
1216 totalbytesread = DELTA(bytes, DEVSTAT_READ);
1217 totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE);
1218 totalbytesfree = DELTA(bytes, DEVSTAT_FREE);
1219 totalbytes = totalbytesread + totalbyteswrite + totalbytesfree;
1221 totaltransfersread = DELTA(operations, DEVSTAT_READ);
1222 totaltransferswrite = DELTA(operations, DEVSTAT_WRITE);
1223 totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA);
1224 totaltransfersfree = DELTA(operations, DEVSTAT_FREE);
1225 totaltransfers = totaltransfersread + totaltransferswrite +
1226 totaltransfersother + totaltransfersfree;
1228 totalblocks = totalbytes;
1229 totalblocksread = totalbytesread;
1230 totalblockswrite = totalbyteswrite;
1231 totalblocksfree = totalbytesfree;
1233 if (current->block_size > 0) {
1234 totalblocks /= current->block_size;
1235 totalblocksread /= current->block_size;
1236 totalblockswrite /= current->block_size;
1237 totalblocksfree /= current->block_size;
1240 totalblocksread /= 512;
1241 totalblockswrite /= 512;
1242 totalblocksfree /= 512;
1245 va_start(ap, etime);
1247 while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) {
1249 if (metric == DSM_NONE)
1252 if (metric >= DSM_MAX) {
1253 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1254 "%s: metric %d is out of range", __func__,
1260 switch (devstat_arg_list[metric].argtype) {
1261 case DEVSTAT_ARG_UINT64:
1262 destu64 = (u_int64_t *)va_arg(ap, u_int64_t *);
1264 case DEVSTAT_ARG_LD:
1265 destld = (long double *)va_arg(ap, long double *);
1267 case DEVSTAT_ARG_SKIP:
1268 destld = (long double *)va_arg(ap, long double *);
1273 break; /* NOTREACHED */
1276 if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP)
1280 case DSM_TOTAL_BYTES:
1281 *destu64 = totalbytes;
1283 case DSM_TOTAL_BYTES_READ:
1284 *destu64 = totalbytesread;
1286 case DSM_TOTAL_BYTES_WRITE:
1287 *destu64 = totalbyteswrite;
1289 case DSM_TOTAL_BYTES_FREE:
1290 *destu64 = totalbytesfree;
1292 case DSM_TOTAL_TRANSFERS:
1293 *destu64 = totaltransfers;
1295 case DSM_TOTAL_TRANSFERS_READ:
1296 *destu64 = totaltransfersread;
1298 case DSM_TOTAL_TRANSFERS_WRITE:
1299 *destu64 = totaltransferswrite;
1301 case DSM_TOTAL_TRANSFERS_FREE:
1302 *destu64 = totaltransfersfree;
1304 case DSM_TOTAL_TRANSFERS_OTHER:
1305 *destu64 = totaltransfersother;
1307 case DSM_TOTAL_BLOCKS:
1308 *destu64 = totalblocks;
1310 case DSM_TOTAL_BLOCKS_READ:
1311 *destu64 = totalblocksread;
1313 case DSM_TOTAL_BLOCKS_WRITE:
1314 *destu64 = totalblockswrite;
1316 case DSM_TOTAL_BLOCKS_FREE:
1317 *destu64 = totalblocksfree;
1319 case DSM_KB_PER_TRANSFER:
1320 *destld = totalbytes;
1322 if (totaltransfers > 0)
1323 *destld /= totaltransfers;
1327 case DSM_KB_PER_TRANSFER_READ:
1328 *destld = totalbytesread;
1330 if (totaltransfersread > 0)
1331 *destld /= totaltransfersread;
1335 case DSM_KB_PER_TRANSFER_WRITE:
1336 *destld = totalbyteswrite;
1338 if (totaltransferswrite > 0)
1339 *destld /= totaltransferswrite;
1343 case DSM_KB_PER_TRANSFER_FREE:
1344 *destld = totalbytesfree;
1346 if (totaltransfersfree > 0)
1347 *destld /= totaltransfersfree;
1351 case DSM_TRANSFERS_PER_SECOND:
1353 *destld = totaltransfers;
1358 case DSM_TRANSFERS_PER_SECOND_READ:
1360 *destld = totaltransfersread;
1365 case DSM_TRANSFERS_PER_SECOND_WRITE:
1367 *destld = totaltransferswrite;
1372 case DSM_TRANSFERS_PER_SECOND_FREE:
1374 *destld = totaltransfersfree;
1379 case DSM_TRANSFERS_PER_SECOND_OTHER:
1381 *destld = totaltransfersother;
1386 case DSM_MB_PER_SECOND:
1387 *destld = totalbytes;
1388 *destld /= 1024 * 1024;
1394 case DSM_MB_PER_SECOND_READ:
1395 *destld = totalbytesread;
1396 *destld /= 1024 * 1024;
1402 case DSM_MB_PER_SECOND_WRITE:
1403 *destld = totalbyteswrite;
1404 *destld /= 1024 * 1024;
1410 case DSM_MB_PER_SECOND_FREE:
1411 *destld = totalbytesfree;
1412 *destld /= 1024 * 1024;
1418 case DSM_BLOCKS_PER_SECOND:
1419 *destld = totalblocks;
1425 case DSM_BLOCKS_PER_SECOND_READ:
1426 *destld = totalblocksread;
1432 case DSM_BLOCKS_PER_SECOND_WRITE:
1433 *destld = totalblockswrite;
1439 case DSM_BLOCKS_PER_SECOND_FREE:
1440 *destld = totalblocksfree;
1447 * This calculation is somewhat bogus. It simply divides
1448 * the elapsed time by the total number of transactions
1449 * completed. While that does give the caller a good
1450 * picture of the average rate of transaction completion,
1451 * it doesn't necessarily give the caller a good view of
1452 * how long transactions took to complete on average.
1453 * Those two numbers will be different for a device that
1454 * can handle more than one transaction at a time. e.g.
1455 * SCSI disks doing tagged queueing.
1457 * The only way to accurately determine the real average
1458 * time per transaction would be to compute and store the
1459 * time on a per-transaction basis. That currently isn't
1460 * done in the kernel, and would only be desireable if it
1461 * could be implemented in a somewhat non-intrusive and high
1464 case DSM_MS_PER_TRANSACTION:
1465 if (totaltransfers > 0) {
1467 for (i = 0; i < DEVSTAT_N_TRANS_FLAGS; i++)
1468 *destld += DELTA_T(duration[i]);
1469 *destld /= totaltransfers;
1475 * As above, these next two really only give the average
1476 * rate of completion for read and write transactions, not
1477 * the average time the transaction took to complete.
1479 case DSM_MS_PER_TRANSACTION_READ:
1480 if (totaltransfersread > 0) {
1481 *destld = DELTA_T(duration[DEVSTAT_READ]);
1482 *destld /= totaltransfersread;
1487 case DSM_MS_PER_TRANSACTION_WRITE:
1488 if (totaltransferswrite > 0) {
1489 *destld = DELTA_T(duration[DEVSTAT_WRITE]);
1490 *destld /= totaltransferswrite;
1495 case DSM_MS_PER_TRANSACTION_FREE:
1496 if (totaltransfersfree > 0) {
1497 *destld = DELTA_T(duration[DEVSTAT_FREE]);
1498 *destld /= totaltransfersfree;
1503 case DSM_MS_PER_TRANSACTION_OTHER:
1504 if (totaltransfersother > 0) {
1505 *destld = DELTA_T(duration[DEVSTAT_NO_DATA]);
1506 *destld /= totaltransfersother;
1512 *destld = DELTA_T(busy_time);
1520 case DSM_QUEUE_LENGTH:
1521 *destu64 = current->start_count - current->end_count;
1524 * XXX: comment out the default block to see if any case's are missing.
1529 * This shouldn't happen, since we should have
1530 * caught any out of range metrics at the top of
1533 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1534 "%s: unknown metric %d", __func__, metric);
1537 break; /* NOTREACHED */
1549 readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
1552 if (kvm_read(kd, addr, buf, nbytes) == -1) {
1553 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1554 "%s: error reading value (kvm_read): %s", __func__,
1562 readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes)
1566 nl[0].n_name = (char *)name;
1567 nl[1].n_name = NULL;
1569 if (kvm_nlist(kd, nl) == -1) {
1570 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1571 "%s: error getting name list (kvm_nlist): %s",
1572 __func__, kvm_geterr(kd));
1575 return(readkmem(kd, nl[0].n_value, buf, nbytes));
1579 * This duplicates the functionality of the kernel sysctl handler for poking
1580 * through crash dumps.
1583 get_devstat_kvm(kvm_t *kd)
1587 struct devstat *nds;
1589 struct devstatlist dhead;
1593 if ((num_devs = devstat_getnumdevs(kd)) <= 0)
1595 if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1)
1598 nds = STAILQ_FIRST(&dhead);
1600 if ((rv = malloc(sizeof(gen))) == NULL) {
1601 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1602 "%s: out of memory (initial malloc failed)",
1606 gen = devstat_getgeneration(kd);
1607 memcpy(rv, &gen, sizeof(gen));
1610 * Now push out all the devices.
1612 for (i = 0; (nds != NULL) && (i < num_devs);
1613 nds = STAILQ_NEXT(nds, dev_links), i++) {
1614 if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) {
1619 rv = (char *)reallocf(rv, sizeof(gen) +
1620 sizeof(ds) * (i + 1));
1622 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1623 "%s: out of memory (malloc failed)",
1627 memcpy(rv + wp, &ds, sizeof(ds));