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
28 * $FreeBSD: src/lib/libdevstat/devstat.c,v 1.6 1999/08/28 00:04:26 peter Exp $
29 * $DragonFly: src/lib/libdevstat/devstat.c,v 1.5 2005/01/08 19:19:26 joerg Exp $
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/errno.h>
44 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
47 * Table to match descriptive strings with device types. These are in
48 * order from most common to least common to speed search time.
50 struct devstat_match_table match_table[] = {
51 {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE},
52 {"xa", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE},
53 {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE},
54 {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF},
55 {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF},
56 {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF},
57 {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE},
58 {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE},
59 {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS},
60 {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE},
61 {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE},
62 {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE},
63 {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE},
64 {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE},
65 {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE},
66 {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE},
67 {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE},
68 {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE},
73 * Local function declarations.
75 static int compare_select(const void *arg1, const void *arg2);
82 const char *func_name = "getnumdevs";
84 numdevsize = sizeof(int);
87 * Find out how many devices we have in the system.
89 if (sysctlbyname("kern.devstat.numdevs", &numdevs,
90 &numdevsize, NULL, 0) == -1) {
91 sprintf(devstat_errbuf, "%s: error getting number of devices\n"
92 "%s: %s", func_name, func_name, strerror(errno));
99 * This is an easy way to get the generation number, but the generation is
100 * supplied in a more atmoic manner by the kern.devstat.all sysctl.
101 * Because this generation sysctl is separate from the statistics sysctl,
102 * the device list and the generation could change between the time that
103 * this function is called and the device list is retreived.
110 const char *func_name = "getgeneration";
112 gensize = sizeof(long);
115 * Get the current generation number.
117 if (sysctlbyname("kern.devstat.generation", &generation,
118 &gensize, NULL, 0) == -1) {
119 sprintf(devstat_errbuf,"%s: error getting devstat generation\n"
120 "%s: %s", func_name, func_name, strerror(errno));
127 * Get the current devstat version. The return value of this function
128 * should be compared with DEVSTAT_VERSION, which is defined in
129 * sys/devicestat.h. This will enable userland programs to determine
130 * whether they are out of sync with the kernel.
137 const char *func_name = "getversion";
139 versize = sizeof(int);
142 * Get the current devstat version.
144 if (sysctlbyname("kern.devstat.version", &version, &versize,
146 sprintf(devstat_errbuf, "%s: error getting devstat version\n"
147 "%s: %s", func_name, func_name, strerror(errno));
154 * Check the devstat version we know about against the devstat version the
155 * kernel knows about. If they don't match, print an error into the
156 * devstat error buffer, and return -1. If they match, return 0.
163 const char *func_name = "checkversion";
166 version = getversion();
168 if (version != DEVSTAT_VERSION) {
173 * This is really pretty silly, but basically the idea is
174 * that if getversion() returns an error (i.e. -1), then it
175 * has printed an error message in the buffer. Therefore,
176 * we need to add a \n to the end of that message before we
177 * print our own message in the buffer.
180 buflen = strlen(devstat_errbuf);
181 errlen = snprintf(tmpstr, sizeof(tmpstr), "\n");
182 strncat(devstat_errbuf, tmpstr,
183 DEVSTAT_ERRBUF_SIZE - buflen - 1);
187 errlen = snprintf(tmpstr, sizeof(tmpstr),
188 "%s: userland devstat version %d is not "
189 "the same as the kernel\n%s: devstat "
190 "version %d\n", func_name, DEVSTAT_VERSION,
194 strncat(devstat_errbuf, tmpstr,
195 DEVSTAT_ERRBUF_SIZE - buflen - 1);
198 strncpy(devstat_errbuf, tmpstr, DEVSTAT_ERRBUF_SIZE);
199 devstat_errbuf[DEVSTAT_ERRBUF_SIZE - 1] = '\0';
202 if (version < DEVSTAT_VERSION)
203 snprintf(tmpstr, sizeof(tmpstr),
204 "%s: libdevstat newer than kernel\n",
207 snprintf(tmpstr, sizeof(tmpstr),
208 "%s: kernel newer than libdevstat\n",
211 strncat(devstat_errbuf, tmpstr,
212 DEVSTAT_ERRBUF_SIZE - buflen - 1);
221 * Get the current list of devices and statistics, and the current
226 * 0 -- device list is unchanged
227 * 1 -- device list has changed
230 getdevs(struct statinfo *stats)
236 struct devinfo *dinfo;
237 const char *func_name = "getdevs";
239 dinfo = stats->dinfo;
242 sprintf(devstat_errbuf, "%s: stats->dinfo was NULL", func_name);
246 oldgeneration = dinfo->generation;
249 * If this is our first time through, mem_ptr will be null.
251 if (dinfo->mem_ptr == NULL) {
253 * Get the number of devices. If it's negative, it's an
254 * error. Don't bother setting the error string, since
255 * getnumdevs() has already done that for us.
257 if ((dinfo->numdevs = getnumdevs()) < 0)
261 * The kern.devstat.all sysctl returns the current generation
262 * number, as well as all the devices. So we need four
265 dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long);
266 dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
268 dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long);
270 /* Get the current time when we get the stats */
271 gettimeofday(&stats->busy_time, NULL);
274 * Request all of the devices. We only really allow for one
275 * ENOMEM failure. It would, of course, be possible to just go in
276 * a loop and keep reallocing the device structure until we don't
277 * get ENOMEM back. I'm not sure it's worth it, though. If
278 * devices are being added to the system that quickly, maybe the
279 * user can just wait until all devices are added.
281 if ((error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr,
282 &dssize, NULL, 0)) == -1) {
284 * If we get ENOMEM back, that means that there are
285 * more devices now, so we need to allocate more
286 * space for the device array.
288 if (errno == ENOMEM) {
290 * No need to set the error string here, getnumdevs()
291 * will do that if it fails.
293 if ((dinfo->numdevs = getnumdevs()) < 0)
296 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
298 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
300 if ((error = sysctlbyname("kern.devstat.all",
301 dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
302 sprintf(devstat_errbuf,
303 "%s: error getting device stats\n"
304 "%s: %s", func_name, func_name,
309 sprintf(devstat_errbuf,
310 "%s: error getting device stats\n"
311 "%s: %s", func_name, func_name,
318 * The sysctl spits out the generation as the first four bytes,
319 * then all of the device statistics structures.
321 dinfo->generation = *(long *)dinfo->mem_ptr;
324 * If the generation has changed, and if the current number of
325 * devices is not the same as the number of devices recorded in the
326 * devinfo structure, it is likely that the device list has shrunk.
327 * The reason that it is likely that the device list has shrunk in
328 * this case is that if the device list has grown, the sysctl above
329 * will return an ENOMEM error, and we will reset the number of
330 * devices and reallocate the device array. If the second sysctl
331 * fails, we will return an error and therefore never get to this
332 * point. If the device list has shrunk, the sysctl will not
333 * return an error since we have more space allocated than is
334 * necessary. So, in the shrinkage case, we catch it here and
335 * reallocate the array so that we don't use any more space than is
338 if (oldgeneration != dinfo->generation) {
339 if (getnumdevs() != dinfo->numdevs) {
340 if ((dinfo->numdevs = getnumdevs()) < 0)
342 dssize = (dinfo->numdevs * sizeof(struct devstat)) +
344 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
350 dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
358 * Devices are selected/deselected based upon the following criteria:
359 * - devices specified by the user on the command line
360 * - devices matching any device type expressions given on the command line
361 * - devices with the highest I/O, if 'top' mode is enabled
362 * - the first n unselected devices in the device list, if maxshowdevs
363 * devices haven't already been selected and if the user has not
364 * specified any devices on the command line and if we're in "add" mode.
367 * - device selection list (dev_select)
368 * - current number of devices selected (num_selected)
369 * - total number of devices in the selection list (num_selections)
370 * - devstat generation as of the last time selectdevs() was called
371 * (select_generation)
372 * - current devstat generation (current_generation)
373 * - current list of devices and statistics (devices)
374 * - number of devices in the current device list (numdevs)
375 * - compiled version of the command line device type arguments (matches)
376 * - This is optional. If the number of devices is 0, this will be ignored.
377 * - The matching code pays attention to the current selection mode. So
378 * if you pass in a matching expression, it will be evaluated based
379 * upon the selection mode that is passed in. See below for details.
380 * - number of device type matching expressions (num_matches)
381 * - Set to 0 to disable the matching code.
382 * - list of devices specified on the command line by the user (dev_selections)
383 * - number of devices selected on the command line by the user
384 * (num_dev_selections)
385 * - Our selection mode. There are four different selection modes:
386 * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly
387 * selected by the user or devices matching a pattern given by the
388 * user will be selected in addition to devices that are already
389 * selected. Additional devices will be selected, up to maxshowdevs
391 * - only mode. (DS_SELECT_ONLY) Only devices matching devices
392 * explicitly given by the user or devices matching a pattern
393 * given by the user will be selected. No other devices will be
395 * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and
396 * only. Basically, this will not de-select any devices that are
397 * current selected, as only mode would, but it will also not
398 * gratuitously select up to maxshowdevs devices as add mode would.
399 * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices
400 * explicitly selected by the user or devices matching a pattern
401 * given by the user will be de-selected.
402 * - maximum number of devices we can select (maxshowdevs)
403 * - flag indicating whether or not we're in 'top' mode (perf_select)
406 * - the device selection list may be modified and passed back out
407 * - the number of devices selected and the total number of items in the
408 * device selection list may be changed
409 * - the selection generation may be changed to match the current generation
413 * 0 -- selected devices are unchanged
414 * 1 -- selected devices changed
417 selectdevs(struct device_selection **dev_select, int *num_selected,
418 int *num_selections, long *select_generation,
419 long current_generation, struct devstat *devices, int numdevs,
420 struct devstat_match *matches, int num_matches,
421 char **dev_selections, int num_dev_selections,
422 devstat_select_mode select_mode, int maxshowdevs, int perf_select)
425 int init_selections = 0, init_selected_var = 0;
426 struct device_selection *old_dev_select = NULL;
427 int old_num_selections = 0, old_num_selected;
428 int selection_number = 0;
429 int changed = 0, found = 0;
431 if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0))
435 * We always want to make sure that we have as many dev_select
436 * entries as there are devices.
439 * In this case, we haven't selected devices before.
441 if (*dev_select == NULL) {
442 *dev_select = (struct device_selection *)malloc(numdevs *
443 sizeof(struct device_selection));
444 *select_generation = current_generation;
448 * In this case, we have selected devices before, but the device
449 * list has changed since we last selected devices, so we need to
450 * either enlarge or reduce the size of the device selection list.
452 } else if (*num_selections != numdevs) {
453 *dev_select = (struct device_selection *)realloc(*dev_select,
454 numdevs * sizeof(struct device_selection));
455 *select_generation = current_generation;
458 * In this case, we've selected devices before, and the selection
459 * list is the same size as it was the last time, but the device
462 } else if (*select_generation < current_generation) {
463 *select_generation = current_generation;
468 * If we're in "only" mode, we want to clear out the selected
469 * variable since we're going to select exactly what the user wants
472 if (select_mode == DS_SELECT_ONLY)
473 init_selected_var = 1;
476 * In all cases, we want to back up the number of selected devices.
477 * It is a quick and accurate way to determine whether the selected
478 * devices have changed.
480 old_num_selected = *num_selected;
483 * We want to make a backup of the current selection list if
484 * the list of devices has changed, or if we're in performance
485 * selection mode. In both cases, we don't want to make a backup
486 * if we already know for sure that the list will be different.
487 * This is certainly the case if this is our first time through the
490 if (((init_selected_var != 0) || (init_selections != 0)
491 || (perf_select != 0)) && (changed == 0)){
492 old_dev_select = (struct device_selection *)malloc(
493 *num_selections * sizeof(struct device_selection));
494 old_num_selections = *num_selections;
495 bcopy(*dev_select, old_dev_select,
496 sizeof(struct device_selection) * *num_selections);
499 if (init_selections != 0) {
500 bzero(*dev_select, sizeof(struct device_selection) * numdevs);
502 for (i = 0; i < numdevs; i++) {
503 (*dev_select)[i].device_number =
504 devices[i].device_number;
505 strncpy((*dev_select)[i].device_name,
506 devices[i].device_name,
508 (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
509 (*dev_select)[i].unit_number = devices[i].unit_number;
510 (*dev_select)[i].position = i;
512 *num_selections = numdevs;
513 } else if (init_selected_var != 0) {
514 for (i = 0; i < numdevs; i++)
515 (*dev_select)[i].selected = 0;
518 /* we haven't gotten around to selecting anything yet.. */
519 if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
520 || (init_selected_var != 0))
524 * Look through any devices the user specified on the command line
525 * and see if they match known devices. If so, select them.
527 for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
530 snprintf(tmpstr, sizeof(tmpstr), "%s%d",
531 (*dev_select)[i].device_name,
532 (*dev_select)[i].unit_number);
533 for (j = 0; j < num_dev_selections; j++) {
534 if (strcmp(tmpstr, dev_selections[j]) == 0) {
536 * Here we do different things based on the
537 * mode we're in. If we're in add or
538 * addonly mode, we only select this device
539 * if it hasn't already been selected.
540 * Otherwise, we would be unnecessarily
541 * changing the selection order and
542 * incrementing the selection count. If
543 * we're in only mode, we unconditionally
544 * select this device, since in only mode
545 * any previous selections are erased and
546 * manually specified devices are the first
547 * ones to be selected. If we're in remove
548 * mode, we de-select the specified device and
549 * decrement the selection count.
551 switch(select_mode) {
553 case DS_SELECT_ADDONLY:
554 if ((*dev_select)[i].selected)
558 (*dev_select)[i].selected =
562 case DS_SELECT_REMOVE:
563 (*dev_select)[i].selected = 0;
566 * This isn't passed back out, we
567 * just use it to keep track of
568 * how many devices we've removed.
570 num_dev_selections--;
579 * Go through the user's device type expressions and select devices
580 * accordingly. We only do this if the number of devices already
581 * selected is less than the maximum number we can show.
583 for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
584 /* We should probably indicate some error here */
585 if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
586 || (matches[i].num_match_categories <= 0))
589 for (j = 0; j < numdevs; j++) {
590 int num_match_categories;
592 num_match_categories = matches[i].num_match_categories;
595 * Determine whether or not the current device
596 * matches the given matching expression. This if
597 * statement consists of three components:
598 * - the device type check
599 * - the device interface check
600 * - the passthrough check
601 * If a the matching test is successful, it
602 * decrements the number of matching categories,
603 * and if we've reached the last element that
604 * needed to be matched, the if statement succeeds.
607 if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
608 && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
609 (matches[i].device_type & DEVSTAT_TYPE_MASK))
610 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
611 || (((matches[i].match_fields &
612 DEVSTAT_MATCH_PASS) == 0)
613 && ((devices[j].device_type &
614 DEVSTAT_TYPE_PASS) == 0)))
615 && (--num_match_categories == 0))
616 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
617 && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
618 (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
619 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
620 || (((matches[i].match_fields &
621 DEVSTAT_MATCH_PASS) == 0)
622 && ((devices[j].device_type &
623 DEVSTAT_TYPE_PASS) == 0)))
624 && (--num_match_categories == 0))
625 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
626 && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
627 && (--num_match_categories == 0))) {
630 * This is probably a non-optimal solution
631 * to the problem that the devices in the
632 * device list will not be in the same
633 * order as the devices in the selection
636 for (k = 0; k < numdevs; k++) {
637 if ((*dev_select)[k].position == j) {
644 * There shouldn't be a case where a device
645 * in the device list is not in the
646 * selection list...but it could happen.
649 fprintf(stderr, "selectdevs: couldn't"
650 " find %s%d in selection "
652 devices[j].device_name,
653 devices[j].unit_number);
658 * We do different things based upon the
659 * mode we're in. If we're in add or only
660 * mode, we go ahead and select this device
661 * if it hasn't already been selected. If
662 * it has already been selected, we leave
663 * it alone so we don't mess up the
664 * selection ordering. Manually specified
665 * devices have already been selected, and
666 * they have higher priority than pattern
667 * matched devices. If we're in remove
668 * mode, we de-select the given device and
669 * decrement the selected count.
671 switch(select_mode) {
673 case DS_SELECT_ADDONLY:
675 if ((*dev_select)[k].selected != 0)
677 (*dev_select)[k].selected =
681 case DS_SELECT_REMOVE:
682 (*dev_select)[k].selected = 0;
691 * Here we implement "top" mode. Devices are sorted in the
692 * selection array based on two criteria: whether or not they are
693 * selected (not selection number, just the fact that they are
694 * selected!) and the number of bytes in the "bytes" field of the
695 * selection structure. The bytes field generally must be kept up
696 * by the user. In the future, it may be maintained by library
697 * functions, but for now the user has to do the work.
699 * At first glance, it may seem wrong that we don't go through and
700 * select every device in the case where the user hasn't specified
701 * any devices or patterns. In fact, though, it won't make any
702 * difference in the device sorting. In that particular case (i.e.
703 * when we're in "add" or "only" mode, and the user hasn't
704 * specified anything) the first time through no devices will be
705 * selected, so the only criterion used to sort them will be their
706 * performance. The second time through, and every time thereafter,
707 * all devices will be selected, so again selection won't matter.
709 if (perf_select != 0) {
711 /* Sort the device array by throughput */
712 qsort(*dev_select, *num_selections,
713 sizeof(struct device_selection),
716 if (*num_selected == 0) {
718 * Here we select every device in the array, if it
719 * isn't already selected. Because the 'selected'
720 * variable in the selection array entries contains
721 * the selection order, the devstats routine can show
722 * the devices that were selected first.
724 for (i = 0; i < *num_selections; i++) {
725 if ((*dev_select)[i].selected == 0) {
726 (*dev_select)[i].selected =
732 selection_number = 0;
733 for (i = 0; i < *num_selections; i++) {
734 if ((*dev_select)[i].selected != 0) {
735 (*dev_select)[i].selected =
743 * If we're in the "add" selection mode and if we haven't already
744 * selected maxshowdevs number of devices, go through the array and
745 * select any unselected devices. If we're in "only" mode, we
746 * obviously don't want to select anything other than what the user
747 * specifies. If we're in "remove" mode, it probably isn't a good
748 * idea to go through and select any more devices, since we might
749 * end up selecting something that the user wants removed. Through
750 * more complicated logic, we could actually figure this out, but
751 * that would probably require combining this loop with the various
752 * selections loops above.
754 if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
755 for (i = 0; i < *num_selections; i++)
756 if ((*dev_select)[i].selected == 0) {
757 (*dev_select)[i].selected = ++selection_number;
763 * Look at the number of devices that have been selected. If it
764 * has changed, set the changed variable. Otherwise, if we've
765 * made a backup of the selection list, compare it to the current
766 * selection list to see if the selected devices have changed.
768 if ((changed == 0) && (old_num_selected != *num_selected))
770 else if ((changed == 0) && (old_dev_select != NULL)) {
772 * Now we go through the selection list and we look at
773 * it three different ways.
775 for (i = 0; (i < *num_selections) && (changed == 0) &&
776 (i < old_num_selections); i++) {
778 * If the device at index i in both the new and old
779 * selection arrays has the same device number and
780 * selection status, it hasn't changed. We
781 * continue on to the next index.
783 if (((*dev_select)[i].device_number ==
784 old_dev_select[i].device_number)
785 && ((*dev_select)[i].selected ==
786 old_dev_select[i].selected))
790 * Now, if we're still going through the if
791 * statement, the above test wasn't true. So we
792 * check here to see if the device at index i in
793 * the current array is the same as the device at
794 * index i in the old array. If it is, that means
795 * that its selection number has changed. Set
796 * changed to 1 and exit the loop.
798 else if ((*dev_select)[i].device_number ==
799 old_dev_select[i].device_number) {
804 * If we get here, then the device at index i in
805 * the current array isn't the same device as the
806 * device at index i in the old array.
812 * Search through the old selection array
813 * looking for a device with the same
814 * device number as the device at index i
815 * in the current array. If the selection
816 * status is the same, then we mark it as
817 * found. If the selection status isn't
818 * the same, we break out of the loop.
819 * Since found isn't set, changed will be
822 for (j = 0; j < old_num_selections; j++) {
823 if (((*dev_select)[i].device_number ==
824 old_dev_select[j].device_number)
825 && ((*dev_select)[i].selected ==
826 old_dev_select[j].selected)){
830 else if ((*dev_select)[i].device_number
831 == old_dev_select[j].device_number)
839 if (old_dev_select != NULL)
840 free(old_dev_select);
846 * Comparison routine for qsort() above. Note that the comparison here is
847 * backwards -- generally, it should return a value to indicate whether
848 * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason
849 * it returns the opposite is so that the selection array will be sorted in
850 * order of decreasing performance. We sort on two parameters. The first
851 * sort key is whether or not one or the other of the devices in question
852 * has been selected. If one of them has, and the other one has not, the
853 * selected device is automatically more important than the unselected
854 * device. If neither device is selected, we judge the devices based upon
858 compare_select(const void *arg1, const void *arg2)
860 if ((((const struct device_selection *)arg1)->selected)
861 && (((const struct device_selection *)arg2)->selected == 0))
863 else if ((((const struct device_selection *)arg1)->selected == 0)
864 && (((const struct device_selection *)arg2)->selected))
866 else if (((const struct device_selection *)arg2)->bytes <
867 ((const struct device_selection *)arg1)->bytes)
869 else if (((const struct device_selection *)arg2)->bytes >
870 ((const struct device_selection *)arg1)->bytes)
877 * Take a string with the general format "arg1,arg2,arg3", and build a
878 * device matching expression from it.
881 buildmatch(const char *match_str, struct devstat_match **matches,
886 char *matchbuf_orig; /* strdup of match_str */
887 char *matchbuf; /* allow strsep to clobber */
892 /* We can't do much without a string to parse */
893 if (match_str == NULL) {
894 sprintf(devstat_errbuf, "%s: no match expression", __func__);
899 * Break the (comma delimited) input string out into separate strings.
900 * strsep is destructive, so copy the string first.
902 matchbuf = matchbuf_orig = strdup(match_str);
903 if (matchbuf == NULL) {
904 sprintf(devstat_errbuf, "%s: out of memory", __func__);
907 for (tempstr = tstr, num_args = 0;
908 (*tempstr = strsep(&matchbuf, ",")) != NULL && (num_args < 5);
910 if (**tempstr != '\0')
911 if (++tempstr >= &tstr[5])
914 /* The user gave us too many type arguments */
916 sprintf(devstat_errbuf, "%s: too many type arguments",
922 * Since you can't realloc a pointer that hasn't been malloced
923 * first, we malloc first and then realloc.
925 if (*num_matches == 0)
926 *matches = (struct devstat_match *)malloc(
927 sizeof(struct devstat_match));
929 *matches = (struct devstat_match *)realloc(*matches,
930 sizeof(struct devstat_match) * (*num_matches + 1));
932 /* Make sure the current entry is clear */
933 bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
936 * Step through the arguments the user gave us and build a device
937 * matching expression from them.
939 for (i = 0; i < num_args; i++) {
940 char *tempstr2, *tempstr3;
943 * Get rid of leading white space.
946 while (isspace(*tempstr2) && (*tempstr2 != '\0'))
950 * Get rid of trailing white space.
952 tempstr3 = &tempstr2[strlen(tempstr2) - 1];
954 while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
955 && (isspace(*tempstr3))) {
961 * Go through the match table comparing the user's
962 * arguments to known device types, interfaces, etc.
964 for (j = 0; match_table[j].match_str != NULL; j++) {
966 * We do case-insensitive matching, in case someone
967 * wants to enter "SCSI" instead of "scsi" or
968 * something like that. Only compare as many
969 * characters as are in the string in the match
970 * table. This should help if someone tries to use
971 * a super-long match expression.
973 if (strncasecmp(tempstr2, match_table[j].match_str,
974 strlen(match_table[j].match_str)) == 0) {
976 * Make sure the user hasn't specified two
977 * items of the same type, like "da" and
978 * "cd". One device cannot be both.
980 if (((*matches)[*num_matches].match_fields &
981 match_table[j].match_field) != 0) {
982 sprintf(devstat_errbuf,
983 "%s: cannot have more than "
984 "one match item in a single "
985 "category", __func__);
989 * If we've gotten this far, we have a
990 * winner. Set the appropriate fields in
993 (*matches)[*num_matches].match_fields |=
994 match_table[j].match_field;
995 (*matches)[*num_matches].device_type |=
997 (*matches)[*num_matches].num_match_categories++;
1002 * We should have found a match in the above for loop. If
1003 * not, that means the user entered an invalid device type
1006 if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
1007 snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1008 "%s: unknown match item \"%s\"", __func__,
1017 free(matchbuf_orig);
1022 * Compute a number of device statistics. Only one field is mandatory, and
1023 * that is "current". Everything else is optional. The caller passes in
1024 * pointers to variables to hold the various statistics he desires. If he
1025 * doesn't want a particular staistic, he should pass in a NULL pointer.
1031 compute_stats(struct devstat *current, struct devstat *previous,
1032 long double etime, u_int64_t *total_bytes,
1033 u_int64_t *total_transfers, u_int64_t *total_blocks,
1034 long double *kb_per_transfer, long double *transfers_per_second,
1035 long double *mb_per_second, long double *blocks_per_second,
1036 long double *ms_per_transaction)
1038 u_int64_t totalbytes, totaltransfers, totalblocks;
1041 * current is the only mandatory field.
1043 if (current == NULL) {
1044 sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1049 totalbytes = (current->bytes_written + current->bytes_read) -
1050 ((previous) ? (previous->bytes_written +
1051 previous->bytes_read) : 0);
1054 *total_bytes = totalbytes;
1056 totaltransfers = (current->num_reads +
1057 current->num_writes +
1058 current->num_other) -
1060 (previous->num_reads +
1061 previous->num_writes +
1062 previous->num_other) : 0);
1063 if (total_transfers)
1064 *total_transfers = totaltransfers;
1066 if (transfers_per_second) {
1068 *transfers_per_second = totaltransfers;
1069 *transfers_per_second /= etime;
1071 *transfers_per_second = 0.0;
1074 if (kb_per_transfer) {
1075 *kb_per_transfer = totalbytes;
1076 *kb_per_transfer /= 1024;
1077 if (totaltransfers > 0)
1078 *kb_per_transfer /= totaltransfers;
1080 *kb_per_transfer = 0.0;
1083 if (mb_per_second) {
1084 *mb_per_second = totalbytes;
1085 *mb_per_second /= 1024 * 1024;
1087 *mb_per_second /= etime;
1089 *mb_per_second = 0.0;
1092 totalblocks = totalbytes;
1093 if (current->block_size > 0)
1094 totalblocks /= current->block_size;
1099 *total_blocks = totalblocks;
1101 if (blocks_per_second) {
1102 *blocks_per_second = totalblocks;
1104 *blocks_per_second /= etime;
1106 *blocks_per_second = 0.0;
1109 if (ms_per_transaction) {
1110 if (totaltransfers > 0) {
1111 *ms_per_transaction = etime;
1112 *ms_per_transaction /= totaltransfers;
1113 *ms_per_transaction *= 1000;
1115 *ms_per_transaction = 0.0;
1122 compute_stats_read(struct devstat *current, struct devstat *previous,
1123 long double etime, u_int64_t *total_bytes,
1124 u_int64_t *total_transfers, u_int64_t *total_blocks,
1125 long double *kb_per_transfer, long double *transfers_per_second,
1126 long double *mb_per_second, long double *blocks_per_second,
1127 long double *ms_per_transaction)
1129 u_int64_t totalbytes, totaltransfers, totalblocks;
1132 * current is the only mandatory field.
1134 if (current == NULL) {
1135 sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1140 totalbytes = current->bytes_read -
1141 (previous ? previous->bytes_read : 0);
1144 *total_bytes = totalbytes;
1146 totaltransfers = current->num_reads -
1147 (previous ? previous->num_reads : 0);
1148 if (total_transfers)
1149 *total_transfers = totaltransfers;
1151 if (transfers_per_second) {
1153 *transfers_per_second = totaltransfers;
1154 *transfers_per_second /= etime;
1156 *transfers_per_second = 0.0;
1159 if (kb_per_transfer) {
1160 *kb_per_transfer = totalbytes;
1161 *kb_per_transfer /= 1024;
1162 if (totaltransfers > 0)
1163 *kb_per_transfer /= totaltransfers;
1165 *kb_per_transfer = 0.0;
1168 if (mb_per_second) {
1169 *mb_per_second = totalbytes;
1170 *mb_per_second /= 1024 * 1024;
1172 *mb_per_second /= etime;
1174 *mb_per_second = 0.0;
1177 totalblocks = totalbytes;
1178 if (current->block_size > 0)
1179 totalblocks /= current->block_size;
1184 *total_blocks = totalblocks;
1186 if (blocks_per_second) {
1187 *blocks_per_second = totalblocks;
1189 *blocks_per_second /= etime;
1191 *blocks_per_second = 0.0;
1194 if (ms_per_transaction) {
1195 if (totaltransfers > 0) {
1196 *ms_per_transaction = etime;
1197 *ms_per_transaction /= totaltransfers;
1198 *ms_per_transaction *= 1000;
1200 *ms_per_transaction = 0.0;
1207 compute_stats_write(struct devstat *current, struct devstat *previous,
1208 long double etime, u_int64_t *total_bytes,
1209 u_int64_t *total_transfers, u_int64_t *total_blocks,
1210 long double *kb_per_transfer, long double *transfers_per_second,
1211 long double *mb_per_second, long double *blocks_per_second,
1212 long double *ms_per_transaction)
1214 u_int64_t totalbytes, totaltransfers, totalblocks;
1217 * current is the only mandatory field.
1219 if (current == NULL) {
1220 sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1225 totalbytes = current->bytes_written -
1226 (previous ? previous->bytes_written : 0);
1229 *total_bytes = totalbytes;
1231 totaltransfers = current->num_writes -
1232 (previous ? previous->num_writes : 0);
1233 if (total_transfers)
1234 *total_transfers = totaltransfers;
1236 if (transfers_per_second) {
1238 *transfers_per_second = totaltransfers;
1239 *transfers_per_second /= etime;
1241 *transfers_per_second = 0.0;
1244 if (kb_per_transfer) {
1245 *kb_per_transfer = totalbytes;
1246 *kb_per_transfer /= 1024;
1247 if (totaltransfers > 0)
1248 *kb_per_transfer /= totaltransfers;
1250 *kb_per_transfer = 0.0;
1253 if (mb_per_second) {
1254 *mb_per_second = totalbytes;
1255 *mb_per_second /= 1024 * 1024;
1257 *mb_per_second /= etime;
1259 *mb_per_second = 0.0;
1262 totalblocks = totalbytes;
1263 if (current->block_size > 0)
1264 totalblocks /= current->block_size;
1269 *total_blocks = totalblocks;
1271 if (blocks_per_second) {
1272 *blocks_per_second = totalblocks;
1274 *blocks_per_second /= etime;
1276 *blocks_per_second = 0.0;
1279 if (ms_per_transaction) {
1280 if (totaltransfers > 0) {
1281 *ms_per_transaction = etime;
1282 *ms_per_transaction /= totaltransfers;
1283 *ms_per_transaction *= 1000;
1285 *ms_per_transaction = 0.0;
1292 compute_etime(struct timeval cur_time, struct timeval prev_time)
1294 struct timeval busy_time;
1295 u_int64_t busy_usec;
1298 timersub(&cur_time, &prev_time, &busy_time);
1300 busy_usec = busy_time.tv_sec;
1301 busy_usec *= 1000000;
1302 busy_usec += busy_time.tv_usec;