2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1997, 1998 Kenneth D. Merry.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
33 #include <sys/errno.h>
34 #include <sys/resource.h>
35 #include <sys/queue.h>
51 compute_stats(struct devstat *current, struct devstat *previous,
52 long double etime, u_int64_t *total_bytes,
53 u_int64_t *total_transfers, u_int64_t *total_blocks,
54 long double *kb_per_transfer, long double *transfers_per_second,
55 long double *mb_per_second, long double *blocks_per_second,
56 long double *ms_per_transaction);
65 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
68 * Table to match descriptive strings with device types. These are in
69 * order from most common to least common to speed search time.
71 struct devstat_match_table match_table[] = {
72 {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE},
73 {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE},
74 {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF},
75 {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF},
76 {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF},
77 {"nvme", DEVSTAT_TYPE_IF_NVME, 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 },
136 { DSM_TOTAL_DURATION, DEVSTAT_ARG_LD },
137 { DSM_TOTAL_DURATION_READ, DEVSTAT_ARG_LD },
138 { DSM_TOTAL_DURATION_WRITE, DEVSTAT_ARG_LD },
139 { DSM_TOTAL_DURATION_FREE, DEVSTAT_ARG_LD },
140 { DSM_TOTAL_DURATION_OTHER, DEVSTAT_ARG_LD },
141 { DSM_TOTAL_BUSY_TIME, DEVSTAT_ARG_LD },
144 static const char *namelist[] = {
147 #define X_GENERATION 1
148 "_devstat_generation",
151 #define X_DEVICE_STATQ 3
153 #define X_TIME_UPTIME 4
159 * Local function declarations.
161 static int compare_select(const void *arg1, const void *arg2);
162 static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes);
163 static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes);
164 static char *get_devstat_kvm(kvm_t *kd);
166 #define KREADNL(kd, var, val) \
167 readkmem_nl(kd, namelist[var], &val, sizeof(val))
170 devstat_getnumdevs(kvm_t *kd)
175 numdevsize = sizeof(int);
178 * Find out how many devices we have in the system.
181 if (sysctlbyname("kern.devstat.numdevs", &numdevs,
182 &numdevsize, NULL, 0) == -1) {
183 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
184 "%s: error getting number of devices\n"
185 "%s: %s", __func__, __func__,
192 if (KREADNL(kd, X_NUMDEVS, numdevs) == -1)
200 * This is an easy way to get the generation number, but the generation is
201 * supplied in a more atmoic manner by the kern.devstat.all sysctl.
202 * Because this generation sysctl is separate from the statistics sysctl,
203 * the device list and the generation could change between the time that
204 * this function is called and the device list is retrieved.
207 devstat_getgeneration(kvm_t *kd)
212 gensize = sizeof(long);
215 * Get the current generation number.
218 if (sysctlbyname("kern.devstat.generation", &generation,
219 &gensize, NULL, 0) == -1) {
220 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
221 "%s: error getting devstat generation\n%s: %s",
222 __func__, __func__, strerror(errno));
227 if (KREADNL(kd, X_GENERATION, generation) == -1)
235 * Get the current devstat version. The return value of this function
236 * should be compared with DEVSTAT_VERSION, which is defined in
237 * sys/devicestat.h. This will enable userland programs to determine
238 * whether they are out of sync with the kernel.
241 devstat_getversion(kvm_t *kd)
246 versize = sizeof(int);
249 * Get the current devstat version.
252 if (sysctlbyname("kern.devstat.version", &version, &versize,
254 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
255 "%s: error getting devstat version\n%s: %s",
256 __func__, __func__, strerror(errno));
261 if (KREADNL(kd, X_VERSION, version) == -1)
269 * Check the devstat version we know about against the devstat version the
270 * kernel knows about. If they don't match, print an error into the
271 * devstat error buffer, and return -1. If they match, return 0.
274 devstat_checkversion(kvm_t *kd)
276 int buflen, res, retval = 0, version;
278 version = devstat_getversion(kd);
280 if (version != DEVSTAT_VERSION) {
282 * If getversion() returns an error (i.e. -1), then it
283 * has printed an error message in the buffer. Therefore,
284 * we need to add a \n to the end of that message before we
285 * print our own message in the buffer.
288 buflen = strlen(devstat_errbuf);
292 res = snprintf(devstat_errbuf + buflen,
293 DEVSTAT_ERRBUF_SIZE - buflen,
294 "%s%s: userland devstat version %d is not "
295 "the same as the kernel\n%s: devstat "
296 "version %d\n", version == -1 ? "\n" : "",
297 __func__, DEVSTAT_VERSION, __func__, version);
300 devstat_errbuf[buflen] = '\0';
302 buflen = strlen(devstat_errbuf);
303 if (version < DEVSTAT_VERSION)
304 res = snprintf(devstat_errbuf + buflen,
305 DEVSTAT_ERRBUF_SIZE - buflen,
306 "%s: libdevstat newer than kernel\n",
309 res = snprintf(devstat_errbuf + buflen,
310 DEVSTAT_ERRBUF_SIZE - buflen,
311 "%s: kernel newer than libdevstat\n",
315 devstat_errbuf[buflen] = '\0';
324 * Get the current list of devices and statistics, and the current
329 * 0 -- device list is unchanged
330 * 1 -- device list has changed
333 devstat_getdevs(kvm_t *kd, struct statinfo *stats)
339 struct devinfo *dinfo;
342 dinfo = stats->dinfo;
345 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
346 "%s: stats->dinfo was NULL", __func__);
350 oldgeneration = dinfo->generation;
353 clock_gettime(CLOCK_MONOTONIC, &ts);
354 stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9;
356 /* If this is our first time through, mem_ptr will be null. */
357 if (dinfo->mem_ptr == NULL) {
359 * Get the number of devices. If it's negative, it's an
360 * error. Don't bother setting the error string, since
361 * getnumdevs() has already done that for us.
363 if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
367 * The kern.devstat.all sysctl returns the current
368 * generation number, as well as all the devices.
369 * So we need four bytes more.
371 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
373 dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
374 if (dinfo->mem_ptr == NULL) {
375 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
376 "%s: Cannot allocate memory for mem_ptr element",
381 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
385 * Request all of the devices. We only really allow for one
386 * ENOMEM failure. It would, of course, be possible to just go
387 * in a loop and keep reallocing the device structure until we
388 * don't get ENOMEM back. I'm not sure it's worth it, though.
389 * If devices are being added to the system that quickly, maybe
390 * the user can just wait until all devices are added.
393 error = sysctlbyname("kern.devstat.all",
396 if (error != -1 || errno != EBUSY)
401 * If we get ENOMEM back, that means that there are
402 * more devices now, so we need to allocate more
403 * space for the device array.
405 if (errno == ENOMEM) {
407 * No need to set the error string here,
408 * devstat_getnumdevs() will do that if it fails.
410 if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
413 dssize = (dinfo->numdevs *
414 sizeof(struct devstat)) + sizeof(long);
415 dinfo->mem_ptr = (u_int8_t *)
416 realloc(dinfo->mem_ptr, dssize);
417 if ((error = sysctlbyname("kern.devstat.all",
418 dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
419 snprintf(devstat_errbuf,
420 sizeof(devstat_errbuf),
421 "%s: error getting device "
422 "stats\n%s: %s", __func__,
423 __func__, strerror(errno));
427 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
428 "%s: error getting device stats\n"
429 "%s: %s", __func__, __func__,
436 if (KREADNL(kd, X_TIME_UPTIME, ts.tv_sec) == -1)
439 stats->snap_time = ts.tv_sec;
442 * This is of course non-atomic, but since we are working
443 * on a core dump, the generation is unlikely to change
445 if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1)
447 if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL)
451 * The sysctl spits out the generation as the first four bytes,
452 * then all of the device statistics structures.
454 dinfo->generation = *(long *)dinfo->mem_ptr;
457 * If the generation has changed, and if the current number of
458 * devices is not the same as the number of devices recorded in the
459 * devinfo structure, it is likely that the device list has shrunk.
460 * The reason that it is likely that the device list has shrunk in
461 * this case is that if the device list has grown, the sysctl above
462 * will return an ENOMEM error, and we will reset the number of
463 * devices and reallocate the device array. If the second sysctl
464 * fails, we will return an error and therefore never get to this
465 * point. If the device list has shrunk, the sysctl will not
466 * return an error since we have more space allocated than is
467 * necessary. So, in the shrinkage case, we catch it here and
468 * reallocate the array so that we don't use any more space than is
471 if (oldgeneration != dinfo->generation) {
472 if (devstat_getnumdevs(kd) != dinfo->numdevs) {
473 if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
475 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
477 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
483 dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
491 * Devices are selected/deselected based upon the following criteria:
492 * - devices specified by the user on the command line
493 * - devices matching any device type expressions given on the command line
494 * - devices with the highest I/O, if 'top' mode is enabled
495 * - the first n unselected devices in the device list, if maxshowdevs
496 * devices haven't already been selected and if the user has not
497 * specified any devices on the command line and if we're in "add" mode.
500 * - device selection list (dev_select)
501 * - current number of devices selected (num_selected)
502 * - total number of devices in the selection list (num_selections)
503 * - devstat generation as of the last time selectdevs() was called
504 * (select_generation)
505 * - current devstat generation (current_generation)
506 * - current list of devices and statistics (devices)
507 * - number of devices in the current device list (numdevs)
508 * - compiled version of the command line device type arguments (matches)
509 * - This is optional. If the number of devices is 0, this will be ignored.
510 * - The matching code pays attention to the current selection mode. So
511 * if you pass in a matching expression, it will be evaluated based
512 * upon the selection mode that is passed in. See below for details.
513 * - number of device type matching expressions (num_matches)
514 * - Set to 0 to disable the matching code.
515 * - list of devices specified on the command line by the user (dev_selections)
516 * - number of devices selected on the command line by the user
517 * (num_dev_selections)
518 * - Our selection mode. There are four different selection modes:
519 * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly
520 * selected by the user or devices matching a pattern given by the
521 * user will be selected in addition to devices that are already
522 * selected. Additional devices will be selected, up to maxshowdevs
524 * - only mode. (DS_SELECT_ONLY) Only devices matching devices
525 * explicitly given by the user or devices matching a pattern
526 * given by the user will be selected. No other devices will be
528 * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and
529 * only. Basically, this will not de-select any devices that are
530 * current selected, as only mode would, but it will also not
531 * gratuitously select up to maxshowdevs devices as add mode would.
532 * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices
533 * explicitly selected by the user or devices matching a pattern
534 * given by the user will be de-selected.
535 * - maximum number of devices we can select (maxshowdevs)
536 * - flag indicating whether or not we're in 'top' mode (perf_select)
539 * - the device selection list may be modified and passed back out
540 * - the number of devices selected and the total number of items in the
541 * device selection list may be changed
542 * - the selection generation may be changed to match the current generation
546 * 0 -- selected devices are unchanged
547 * 1 -- selected devices changed
550 devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
551 int *num_selections, long *select_generation,
552 long current_generation, struct devstat *devices,
553 int numdevs, struct devstat_match *matches, int num_matches,
554 char **dev_selections, int num_dev_selections,
555 devstat_select_mode select_mode, int maxshowdevs,
559 int init_selections = 0, init_selected_var = 0;
560 struct device_selection *old_dev_select = NULL;
561 int old_num_selections = 0, old_num_selected;
562 int selection_number = 0;
563 int changed = 0, found = 0;
565 if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0))
569 * We always want to make sure that we have as many dev_select
570 * entries as there are devices.
573 * In this case, we haven't selected devices before.
575 if (*dev_select == NULL) {
576 *dev_select = (struct device_selection *)malloc(numdevs *
577 sizeof(struct device_selection));
578 *select_generation = current_generation;
582 * In this case, we have selected devices before, but the device
583 * list has changed since we last selected devices, so we need to
584 * either enlarge or reduce the size of the device selection list.
585 * But delay the resizing until after copying the data to old_dev_select
586 * as to not lose any data in the case of reducing the size.
588 } else if (*num_selections != numdevs) {
589 *select_generation = current_generation;
592 * In this case, we've selected devices before, and the selection
593 * list is the same size as it was the last time, but the device
596 } else if (*select_generation < current_generation) {
597 *select_generation = current_generation;
601 if (*dev_select == NULL) {
602 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
603 "%s: Cannot (re)allocate memory for dev_select argument",
609 * If we're in "only" mode, we want to clear out the selected
610 * variable since we're going to select exactly what the user wants
613 if (select_mode == DS_SELECT_ONLY)
614 init_selected_var = 1;
617 * In all cases, we want to back up the number of selected devices.
618 * It is a quick and accurate way to determine whether the selected
619 * devices have changed.
621 old_num_selected = *num_selected;
624 * We want to make a backup of the current selection list if
625 * the list of devices has changed, or if we're in performance
626 * selection mode. In both cases, we don't want to make a backup
627 * if we already know for sure that the list will be different.
628 * This is certainly the case if this is our first time through the
631 if (((init_selected_var != 0) || (init_selections != 0)
632 || (perf_select != 0)) && (changed == 0)){
633 old_dev_select = (struct device_selection *)malloc(
634 *num_selections * sizeof(struct device_selection));
635 if (old_dev_select == NULL) {
636 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
637 "%s: Cannot allocate memory for selection list backup",
641 old_num_selections = *num_selections;
642 bcopy(*dev_select, old_dev_select,
643 sizeof(struct device_selection) * *num_selections);
646 if (!changed && *num_selections != numdevs) {
647 *dev_select = (struct device_selection *)reallocf(*dev_select,
648 numdevs * sizeof(struct device_selection));
651 if (init_selections != 0) {
652 bzero(*dev_select, sizeof(struct device_selection) * numdevs);
654 for (i = 0; i < numdevs; i++) {
655 (*dev_select)[i].device_number =
656 devices[i].device_number;
657 strncpy((*dev_select)[i].device_name,
658 devices[i].device_name,
660 (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
661 (*dev_select)[i].unit_number = devices[i].unit_number;
662 (*dev_select)[i].position = i;
664 *num_selections = numdevs;
665 } else if (init_selected_var != 0) {
666 for (i = 0; i < numdevs; i++)
667 (*dev_select)[i].selected = 0;
670 /* we haven't gotten around to selecting anything yet.. */
671 if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
672 || (init_selected_var != 0))
676 * Look through any devices the user specified on the command line
677 * and see if they match known devices. If so, select them.
679 for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
682 snprintf(tmpstr, sizeof(tmpstr), "%s%d",
683 (*dev_select)[i].device_name,
684 (*dev_select)[i].unit_number);
685 for (j = 0; j < num_dev_selections; j++) {
686 if (strcmp(tmpstr, dev_selections[j]) == 0) {
688 * Here we do different things based on the
689 * mode we're in. If we're in add or
690 * addonly mode, we only select this device
691 * if it hasn't already been selected.
692 * Otherwise, we would be unnecessarily
693 * changing the selection order and
694 * incrementing the selection count. If
695 * we're in only mode, we unconditionally
696 * select this device, since in only mode
697 * any previous selections are erased and
698 * manually specified devices are the first
699 * ones to be selected. If we're in remove
700 * mode, we de-select the specified device and
701 * decrement the selection count.
703 switch(select_mode) {
705 case DS_SELECT_ADDONLY:
706 if ((*dev_select)[i].selected)
710 (*dev_select)[i].selected =
714 case DS_SELECT_REMOVE:
715 (*dev_select)[i].selected = 0;
718 * This isn't passed back out, we
719 * just use it to keep track of
720 * how many devices we've removed.
722 num_dev_selections--;
731 * Go through the user's device type expressions and select devices
732 * accordingly. We only do this if the number of devices already
733 * selected is less than the maximum number we can show.
735 for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
736 /* We should probably indicate some error here */
737 if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
738 || (matches[i].num_match_categories <= 0))
741 for (j = 0; j < numdevs; j++) {
742 int num_match_categories;
744 num_match_categories = matches[i].num_match_categories;
747 * Determine whether or not the current device
748 * matches the given matching expression. This if
749 * statement consists of three components:
750 * - the device type check
751 * - the device interface check
752 * - the passthrough check
753 * If a the matching test is successful, it
754 * decrements the number of matching categories,
755 * and if we've reached the last element that
756 * needed to be matched, the if statement succeeds.
759 if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
760 && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
761 (matches[i].device_type & DEVSTAT_TYPE_MASK))
762 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
763 || (((matches[i].match_fields &
764 DEVSTAT_MATCH_PASS) == 0)
765 && ((devices[j].device_type &
766 DEVSTAT_TYPE_PASS) == 0)))
767 && (--num_match_categories == 0))
768 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
769 && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
770 (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
771 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
772 || (((matches[i].match_fields &
773 DEVSTAT_MATCH_PASS) == 0)
774 && ((devices[j].device_type &
775 DEVSTAT_TYPE_PASS) == 0)))
776 && (--num_match_categories == 0))
777 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
778 && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
779 && (--num_match_categories == 0))) {
782 * This is probably a non-optimal solution
783 * to the problem that the devices in the
784 * device list will not be in the same
785 * order as the devices in the selection
788 for (k = 0; k < numdevs; k++) {
789 if ((*dev_select)[k].position == j) {
796 * There shouldn't be a case where a device
797 * in the device list is not in the
798 * selection list...but it could happen.
801 fprintf(stderr, "selectdevs: couldn't"
802 " find %s%d in selection "
804 devices[j].device_name,
805 devices[j].unit_number);
810 * We do different things based upon the
811 * mode we're in. If we're in add or only
812 * mode, we go ahead and select this device
813 * if it hasn't already been selected. If
814 * it has already been selected, we leave
815 * it alone so we don't mess up the
816 * selection ordering. Manually specified
817 * devices have already been selected, and
818 * they have higher priority than pattern
819 * matched devices. If we're in remove
820 * mode, we de-select the given device and
821 * decrement the selected count.
823 switch(select_mode) {
825 case DS_SELECT_ADDONLY:
827 if ((*dev_select)[k].selected != 0)
829 (*dev_select)[k].selected =
833 case DS_SELECT_REMOVE:
834 (*dev_select)[k].selected = 0;
843 * Here we implement "top" mode. Devices are sorted in the
844 * selection array based on two criteria: whether or not they are
845 * selected (not selection number, just the fact that they are
846 * selected!) and the number of bytes in the "bytes" field of the
847 * selection structure. The bytes field generally must be kept up
848 * by the user. In the future, it may be maintained by library
849 * functions, but for now the user has to do the work.
851 * At first glance, it may seem wrong that we don't go through and
852 * select every device in the case where the user hasn't specified
853 * any devices or patterns. In fact, though, it won't make any
854 * difference in the device sorting. In that particular case (i.e.
855 * when we're in "add" or "only" mode, and the user hasn't
856 * specified anything) the first time through no devices will be
857 * selected, so the only criterion used to sort them will be their
858 * performance. The second time through, and every time thereafter,
859 * all devices will be selected, so again selection won't matter.
861 if (perf_select != 0) {
863 /* Sort the device array by throughput */
864 qsort(*dev_select, *num_selections,
865 sizeof(struct device_selection),
868 if (*num_selected == 0) {
870 * Here we select every device in the array, if it
871 * isn't already selected. Because the 'selected'
872 * variable in the selection array entries contains
873 * the selection order, the devstats routine can show
874 * the devices that were selected first.
876 for (i = 0; i < *num_selections; i++) {
877 if ((*dev_select)[i].selected == 0) {
878 (*dev_select)[i].selected =
884 selection_number = 0;
885 for (i = 0; i < *num_selections; i++) {
886 if ((*dev_select)[i].selected != 0) {
887 (*dev_select)[i].selected =
895 * If we're in the "add" selection mode and if we haven't already
896 * selected maxshowdevs number of devices, go through the array and
897 * select any unselected devices. If we're in "only" mode, we
898 * obviously don't want to select anything other than what the user
899 * specifies. If we're in "remove" mode, it probably isn't a good
900 * idea to go through and select any more devices, since we might
901 * end up selecting something that the user wants removed. Through
902 * more complicated logic, we could actually figure this out, but
903 * that would probably require combining this loop with the various
904 * selections loops above.
906 if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
907 for (i = 0; i < *num_selections; i++)
908 if ((*dev_select)[i].selected == 0) {
909 (*dev_select)[i].selected = ++selection_number;
915 * Look at the number of devices that have been selected. If it
916 * has changed, set the changed variable. Otherwise, if we've
917 * made a backup of the selection list, compare it to the current
918 * selection list to see if the selected devices have changed.
920 if ((changed == 0) && (old_num_selected != *num_selected))
922 else if ((changed == 0) && (old_dev_select != NULL)) {
924 * Now we go through the selection list and we look at
925 * it three different ways.
927 for (i = 0; (i < *num_selections) && (changed == 0) &&
928 (i < old_num_selections); i++) {
930 * If the device at index i in both the new and old
931 * selection arrays has the same device number and
932 * selection status, it hasn't changed. We
933 * continue on to the next index.
935 if (((*dev_select)[i].device_number ==
936 old_dev_select[i].device_number)
937 && ((*dev_select)[i].selected ==
938 old_dev_select[i].selected))
942 * Now, if we're still going through the if
943 * statement, the above test wasn't true. So we
944 * check here to see if the device at index i in
945 * the current array is the same as the device at
946 * index i in the old array. If it is, that means
947 * that its selection number has changed. Set
948 * changed to 1 and exit the loop.
950 else if ((*dev_select)[i].device_number ==
951 old_dev_select[i].device_number) {
956 * If we get here, then the device at index i in
957 * the current array isn't the same device as the
958 * device at index i in the old array.
964 * Search through the old selection array
965 * looking for a device with the same
966 * device number as the device at index i
967 * in the current array. If the selection
968 * status is the same, then we mark it as
969 * found. If the selection status isn't
970 * the same, we break out of the loop.
971 * Since found isn't set, changed will be
974 for (j = 0; j < old_num_selections; j++) {
975 if (((*dev_select)[i].device_number ==
976 old_dev_select[j].device_number)
977 && ((*dev_select)[i].selected ==
978 old_dev_select[j].selected)){
982 else if ((*dev_select)[i].device_number
983 == old_dev_select[j].device_number)
991 if (old_dev_select != NULL)
992 free(old_dev_select);
998 * Comparison routine for qsort() above. Note that the comparison here is
999 * backwards -- generally, it should return a value to indicate whether
1000 * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason
1001 * it returns the opposite is so that the selection array will be sorted in
1002 * order of decreasing performance. We sort on two parameters. The first
1003 * sort key is whether or not one or the other of the devices in question
1004 * has been selected. If one of them has, and the other one has not, the
1005 * selected device is automatically more important than the unselected
1006 * device. If neither device is selected, we judge the devices based upon
1010 compare_select(const void *arg1, const void *arg2)
1012 if ((((const struct device_selection *)arg1)->selected)
1013 && (((const struct device_selection *)arg2)->selected == 0))
1015 else if ((((const struct device_selection *)arg1)->selected == 0)
1016 && (((const struct device_selection *)arg2)->selected))
1018 else if (((const struct device_selection *)arg2)->bytes <
1019 ((const struct device_selection *)arg1)->bytes)
1021 else if (((const struct device_selection *)arg2)->bytes >
1022 ((const struct device_selection *)arg1)->bytes)
1029 * Take a string with the general format "arg1,arg2,arg3", and build a
1030 * device matching expression from it.
1033 devstat_buildmatch(char *match_str, struct devstat_match **matches,
1041 /* We can't do much without a string to parse */
1042 if (match_str == NULL) {
1043 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1044 "%s: no match expression", __func__);
1049 * Break the (comma delimited) input string out into separate strings.
1051 for (tempstr = tstr, num_args = 0;
1052 (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);)
1053 if (**tempstr != '\0') {
1055 if (++tempstr >= &tstr[5])
1059 /* The user gave us too many type arguments */
1061 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1062 "%s: too many type arguments", __func__);
1066 if (*num_matches == 0)
1069 *matches = (struct devstat_match *)reallocf(*matches,
1070 sizeof(struct devstat_match) * (*num_matches + 1));
1072 if (*matches == NULL) {
1073 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1074 "%s: Cannot allocate memory for matches list", __func__);
1078 /* Make sure the current entry is clear */
1079 bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
1082 * Step through the arguments the user gave us and build a device
1083 * matching expression from them.
1085 for (i = 0; i < num_args; i++) {
1086 char *tempstr2, *tempstr3;
1089 * Get rid of leading white space.
1092 while (isspace(*tempstr2) && (*tempstr2 != '\0'))
1096 * Get rid of trailing white space.
1098 tempstr3 = &tempstr2[strlen(tempstr2) - 1];
1100 while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
1101 && (isspace(*tempstr3))) {
1107 * Go through the match table comparing the user's
1108 * arguments to known device types, interfaces, etc.
1110 for (j = 0; match_table[j].match_str != NULL; j++) {
1112 * We do case-insensitive matching, in case someone
1113 * wants to enter "SCSI" instead of "scsi" or
1114 * something like that. Only compare as many
1115 * characters as are in the string in the match
1116 * table. This should help if someone tries to use
1117 * a super-long match expression.
1119 if (strncasecmp(tempstr2, match_table[j].match_str,
1120 strlen(match_table[j].match_str)) == 0) {
1122 * Make sure the user hasn't specified two
1123 * items of the same type, like "da" and
1124 * "cd". One device cannot be both.
1126 if (((*matches)[*num_matches].match_fields &
1127 match_table[j].match_field) != 0) {
1128 snprintf(devstat_errbuf,
1129 sizeof(devstat_errbuf),
1130 "%s: cannot have more than "
1131 "one match item in a single "
1132 "category", __func__);
1136 * If we've gotten this far, we have a
1137 * winner. Set the appropriate fields in
1140 (*matches)[*num_matches].match_fields |=
1141 match_table[j].match_field;
1142 (*matches)[*num_matches].device_type |=
1143 match_table[j].type;
1144 (*matches)[*num_matches].num_match_categories++;
1149 * We should have found a match in the above for loop. If
1150 * not, that means the user entered an invalid device type
1153 if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
1154 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1155 "%s: unknown match item \"%s\"", __func__,
1167 * Compute a number of device statistics. Only one field is mandatory, and
1168 * that is "current". Everything else is optional. The caller passes in
1169 * pointers to variables to hold the various statistics he desires. If he
1170 * doesn't want a particular staistic, he should pass in a NULL pointer.
1176 compute_stats(struct devstat *current, struct devstat *previous,
1177 long double etime, u_int64_t *total_bytes,
1178 u_int64_t *total_transfers, u_int64_t *total_blocks,
1179 long double *kb_per_transfer, long double *transfers_per_second,
1180 long double *mb_per_second, long double *blocks_per_second,
1181 long double *ms_per_transaction)
1183 return(devstat_compute_statistics(current, previous, etime,
1184 total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP,
1186 total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP,
1188 total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP,
1190 kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP,
1192 transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP,
1193 transfers_per_second,
1194 mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP,
1196 blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP,
1198 ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP,
1204 /* This is 1/2^64 */
1205 #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20
1208 devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time)
1212 etime = cur_time->sec;
1213 etime += cur_time->frac * BINTIME_SCALE;
1214 if (prev_time != NULL) {
1215 etime -= prev_time->sec;
1216 etime -= prev_time->frac * BINTIME_SCALE;
1221 #define DELTA(field, index) \
1222 (current->field[(index)] - (previous ? previous->field[(index)] : 0))
1224 #define DELTA_T(field) \
1225 devstat_compute_etime(¤t->field, \
1226 (previous ? &previous->field : NULL))
1229 devstat_compute_statistics(struct devstat *current, struct devstat *previous,
1230 long double etime, ...)
1232 u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree;
1233 u_int64_t totaltransfers, totaltransfersread, totaltransferswrite;
1234 u_int64_t totaltransfersother, totalblocks, totalblocksread;
1235 u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree;
1236 long double totalduration, totaldurationread, totaldurationwrite;
1237 long double totaldurationfree, totaldurationother;
1239 devstat_metric metric;
1241 long double *destld;
1247 * current is the only mandatory field.
1249 if (current == NULL) {
1250 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1251 "%s: current stats structure was NULL", __func__);
1255 totalbytesread = DELTA(bytes, DEVSTAT_READ);
1256 totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE);
1257 totalbytesfree = DELTA(bytes, DEVSTAT_FREE);
1258 totalbytes = totalbytesread + totalbyteswrite + totalbytesfree;
1260 totaltransfersread = DELTA(operations, DEVSTAT_READ);
1261 totaltransferswrite = DELTA(operations, DEVSTAT_WRITE);
1262 totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA);
1263 totaltransfersfree = DELTA(operations, DEVSTAT_FREE);
1264 totaltransfers = totaltransfersread + totaltransferswrite +
1265 totaltransfersother + totaltransfersfree;
1267 totalblocks = totalbytes;
1268 totalblocksread = totalbytesread;
1269 totalblockswrite = totalbyteswrite;
1270 totalblocksfree = totalbytesfree;
1272 if (current->block_size > 0) {
1273 totalblocks /= current->block_size;
1274 totalblocksread /= current->block_size;
1275 totalblockswrite /= current->block_size;
1276 totalblocksfree /= current->block_size;
1279 totalblocksread /= 512;
1280 totalblockswrite /= 512;
1281 totalblocksfree /= 512;
1284 totaldurationread = DELTA_T(duration[DEVSTAT_READ]);
1285 totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]);
1286 totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]);
1287 totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]);
1288 totalduration = totaldurationread + totaldurationwrite +
1289 totaldurationfree + totaldurationother;
1291 va_start(ap, etime);
1293 while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) {
1295 if (metric == DSM_NONE)
1298 if (metric >= DSM_MAX) {
1299 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1300 "%s: metric %d is out of range", __func__,
1306 switch (devstat_arg_list[metric].argtype) {
1307 case DEVSTAT_ARG_UINT64:
1308 destu64 = (u_int64_t *)va_arg(ap, u_int64_t *);
1310 case DEVSTAT_ARG_LD:
1311 destld = (long double *)va_arg(ap, long double *);
1313 case DEVSTAT_ARG_SKIP:
1314 destld = (long double *)va_arg(ap, long double *);
1319 break; /* NOTREACHED */
1322 if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP)
1326 case DSM_TOTAL_BYTES:
1327 *destu64 = totalbytes;
1329 case DSM_TOTAL_BYTES_READ:
1330 *destu64 = totalbytesread;
1332 case DSM_TOTAL_BYTES_WRITE:
1333 *destu64 = totalbyteswrite;
1335 case DSM_TOTAL_BYTES_FREE:
1336 *destu64 = totalbytesfree;
1338 case DSM_TOTAL_TRANSFERS:
1339 *destu64 = totaltransfers;
1341 case DSM_TOTAL_TRANSFERS_READ:
1342 *destu64 = totaltransfersread;
1344 case DSM_TOTAL_TRANSFERS_WRITE:
1345 *destu64 = totaltransferswrite;
1347 case DSM_TOTAL_TRANSFERS_FREE:
1348 *destu64 = totaltransfersfree;
1350 case DSM_TOTAL_TRANSFERS_OTHER:
1351 *destu64 = totaltransfersother;
1353 case DSM_TOTAL_BLOCKS:
1354 *destu64 = totalblocks;
1356 case DSM_TOTAL_BLOCKS_READ:
1357 *destu64 = totalblocksread;
1359 case DSM_TOTAL_BLOCKS_WRITE:
1360 *destu64 = totalblockswrite;
1362 case DSM_TOTAL_BLOCKS_FREE:
1363 *destu64 = totalblocksfree;
1365 case DSM_KB_PER_TRANSFER:
1366 *destld = totalbytes;
1368 if (totaltransfers > 0)
1369 *destld /= totaltransfers;
1373 case DSM_KB_PER_TRANSFER_READ:
1374 *destld = totalbytesread;
1376 if (totaltransfersread > 0)
1377 *destld /= totaltransfersread;
1381 case DSM_KB_PER_TRANSFER_WRITE:
1382 *destld = totalbyteswrite;
1384 if (totaltransferswrite > 0)
1385 *destld /= totaltransferswrite;
1389 case DSM_KB_PER_TRANSFER_FREE:
1390 *destld = totalbytesfree;
1392 if (totaltransfersfree > 0)
1393 *destld /= totaltransfersfree;
1397 case DSM_TRANSFERS_PER_SECOND:
1399 *destld = totaltransfers;
1404 case DSM_TRANSFERS_PER_SECOND_READ:
1406 *destld = totaltransfersread;
1411 case DSM_TRANSFERS_PER_SECOND_WRITE:
1413 *destld = totaltransferswrite;
1418 case DSM_TRANSFERS_PER_SECOND_FREE:
1420 *destld = totaltransfersfree;
1425 case DSM_TRANSFERS_PER_SECOND_OTHER:
1427 *destld = totaltransfersother;
1432 case DSM_MB_PER_SECOND:
1433 *destld = totalbytes;
1434 *destld /= 1024 * 1024;
1440 case DSM_MB_PER_SECOND_READ:
1441 *destld = totalbytesread;
1442 *destld /= 1024 * 1024;
1448 case DSM_MB_PER_SECOND_WRITE:
1449 *destld = totalbyteswrite;
1450 *destld /= 1024 * 1024;
1456 case DSM_MB_PER_SECOND_FREE:
1457 *destld = totalbytesfree;
1458 *destld /= 1024 * 1024;
1464 case DSM_BLOCKS_PER_SECOND:
1465 *destld = totalblocks;
1471 case DSM_BLOCKS_PER_SECOND_READ:
1472 *destld = totalblocksread;
1478 case DSM_BLOCKS_PER_SECOND_WRITE:
1479 *destld = totalblockswrite;
1485 case DSM_BLOCKS_PER_SECOND_FREE:
1486 *destld = totalblocksfree;
1493 * Some devstat callers update the duration and some don't.
1494 * So this will only be accurate if they provide the
1497 case DSM_MS_PER_TRANSACTION:
1498 if (totaltransfers > 0) {
1499 *destld = totalduration;
1500 *destld /= totaltransfers;
1505 case DSM_MS_PER_TRANSACTION_READ:
1506 if (totaltransfersread > 0) {
1507 *destld = totaldurationread;
1508 *destld /= totaltransfersread;
1513 case DSM_MS_PER_TRANSACTION_WRITE:
1514 if (totaltransferswrite > 0) {
1515 *destld = totaldurationwrite;
1516 *destld /= totaltransferswrite;
1521 case DSM_MS_PER_TRANSACTION_FREE:
1522 if (totaltransfersfree > 0) {
1523 *destld = totaldurationfree;
1524 *destld /= totaltransfersfree;
1529 case DSM_MS_PER_TRANSACTION_OTHER:
1530 if (totaltransfersother > 0) {
1531 *destld = totaldurationother;
1532 *destld /= totaltransfersother;
1538 *destld = DELTA_T(busy_time);
1546 case DSM_QUEUE_LENGTH:
1547 *destu64 = current->start_count - current->end_count;
1549 case DSM_TOTAL_DURATION:
1550 *destld = totalduration;
1552 case DSM_TOTAL_DURATION_READ:
1553 *destld = totaldurationread;
1555 case DSM_TOTAL_DURATION_WRITE:
1556 *destld = totaldurationwrite;
1558 case DSM_TOTAL_DURATION_FREE:
1559 *destld = totaldurationfree;
1561 case DSM_TOTAL_DURATION_OTHER:
1562 *destld = totaldurationother;
1564 case DSM_TOTAL_BUSY_TIME:
1565 *destld = DELTA_T(busy_time);
1568 * XXX: comment out the default block to see if any case's are missing.
1573 * This shouldn't happen, since we should have
1574 * caught any out of range metrics at the top of
1577 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1578 "%s: unknown metric %d", __func__, metric);
1581 break; /* NOTREACHED */
1593 readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
1596 if (kvm_read(kd, addr, buf, nbytes) == -1) {
1597 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1598 "%s: error reading value (kvm_read): %s", __func__,
1606 readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes)
1610 nl[0].n_name = (char *)name;
1611 nl[1].n_name = NULL;
1613 if (kvm_nlist(kd, nl) == -1) {
1614 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1615 "%s: error getting name list (kvm_nlist): %s",
1616 __func__, kvm_geterr(kd));
1619 return(readkmem(kd, nl[0].n_value, buf, nbytes));
1623 * This duplicates the functionality of the kernel sysctl handler for poking
1624 * through crash dumps.
1627 get_devstat_kvm(kvm_t *kd)
1631 struct devstat *nds;
1633 struct devstatlist dhead;
1637 if ((num_devs = devstat_getnumdevs(kd)) <= 0)
1639 if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1)
1642 nds = STAILQ_FIRST(&dhead);
1644 if ((rv = malloc(sizeof(gen))) == NULL) {
1645 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1646 "%s: out of memory (initial malloc failed)",
1650 gen = devstat_getgeneration(kd);
1651 memcpy(rv, &gen, sizeof(gen));
1654 * Now push out all the devices.
1656 for (i = 0; (nds != NULL) && (i < num_devs);
1657 nds = STAILQ_NEXT(nds, dev_links), i++) {
1658 if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) {
1663 rv = (char *)reallocf(rv, sizeof(gen) +
1664 sizeof(ds) * (i + 1));
1666 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1667 "%s: out of memory (malloc failed)",
1671 memcpy(rv + wp, &ds, sizeof(ds));