Replace hybrid libm with OpenBSD libm on vendor branch
[dragonfly.git] / lib / libdevstat / devstat.c
1 /*
2  * Copyright (c) 1997, 1998 Kenneth D. Merry.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
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 $
30  */
31
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/errno.h>
35
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "devstat.h"
43
44 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
45
46 /*
47  * Table to match descriptive strings with device types.  These are in
48  * order from most common to least common to speed search time.
49  */
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},
69         {NULL,          0,                      0}
70 };
71
72 /*
73  * Local function declarations.
74  */
75 static int compare_select(const void *arg1, const void *arg2);
76
77 int
78 getnumdevs(void)
79 {
80         size_t numdevsize;
81         int numdevs;
82         const char *func_name = "getnumdevs";
83
84         numdevsize = sizeof(int);
85
86         /*
87          * Find out how many devices we have in the system.
88          */
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));
93                 return(-1);
94         } else
95                 return(numdevs);
96 }
97
98 /*
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.
104  */
105 long
106 getgeneration(void)
107 {
108         size_t gensize;
109         long generation;
110         const char *func_name = "getgeneration";
111
112         gensize = sizeof(long);
113
114         /*
115          * Get the current generation number.
116          */
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));
121                 return(-1);
122         } else
123                 return(generation);
124 }
125
126 /*
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.
131  */
132 int
133 getversion(void)
134 {
135         size_t versize;
136         int version;
137         const char *func_name = "getversion";
138
139         versize = sizeof(int);
140
141         /*
142          * Get the current devstat version.
143          */
144         if (sysctlbyname("kern.devstat.version", &version, &versize,
145                          NULL, 0) == -1) {
146                 sprintf(devstat_errbuf, "%s: error getting devstat version\n"
147                         "%s: %s", func_name, func_name, strerror(errno));
148                 return(-1);
149         } else
150                 return(version);
151 }
152
153 /*
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.
157  */
158 int
159 checkversion(void)
160 {
161         int retval = 0;
162         int errlen = 0;
163         const char *func_name = "checkversion";
164         int version;
165
166         version = getversion();
167
168         if (version != DEVSTAT_VERSION) {
169                 int buflen = 0;
170                 char tmpstr[256];
171
172                 /*
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.
178                  */
179                 if (version == -1) {
180                         buflen = strlen(devstat_errbuf);
181                         errlen = snprintf(tmpstr, sizeof(tmpstr), "\n");
182                         strncat(devstat_errbuf, tmpstr,
183                                 DEVSTAT_ERRBUF_SIZE - buflen - 1);
184                         buflen += errlen;
185                 }
186
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,
191                                   func_name, version);
192
193                 if (version == -1) {
194                         strncat(devstat_errbuf, tmpstr,
195                                 DEVSTAT_ERRBUF_SIZE - buflen - 1);
196                         buflen += errlen;
197                 } else {
198                         strncpy(devstat_errbuf, tmpstr, DEVSTAT_ERRBUF_SIZE);
199                         devstat_errbuf[DEVSTAT_ERRBUF_SIZE - 1] = '\0';
200                 }
201
202                 if (version < DEVSTAT_VERSION)
203                         snprintf(tmpstr, sizeof(tmpstr),
204                                  "%s: libdevstat newer than kernel\n",
205                                  func_name);
206                 else
207                         snprintf(tmpstr, sizeof(tmpstr),
208                                  "%s: kernel newer than libdevstat\n",
209                                  func_name);
210
211                 strncat(devstat_errbuf, tmpstr,
212                         DEVSTAT_ERRBUF_SIZE - buflen - 1);
213
214                 retval = -1;
215         }
216
217         return(retval);
218 }
219
220 /*
221  * Get the current list of devices and statistics, and the current
222  * generation number.
223  * 
224  * Return values:
225  * -1  -- error
226  *  0  -- device list is unchanged
227  *  1  -- device list has changed
228  */
229 int
230 getdevs(struct statinfo *stats)
231 {
232         int error;
233         size_t dssize;
234         long oldgeneration;
235         int retval = 0;
236         struct devinfo *dinfo;
237         const char *func_name = "getdevs";
238
239         dinfo = stats->dinfo;
240
241         if (dinfo == NULL) {
242                 sprintf(devstat_errbuf, "%s: stats->dinfo was NULL", func_name);
243                 return(-1);
244         }
245
246         oldgeneration = dinfo->generation;
247
248         /*
249          * If this is our first time through, mem_ptr will be null.  
250          */
251         if (dinfo->mem_ptr == NULL) {
252                 /*
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.
256                  */
257                 if ((dinfo->numdevs = getnumdevs()) < 0)
258                         return(-1);
259
260                 /*
261                  * The kern.devstat.all sysctl returns the current generation
262                  * number, as well as all the devices.  So we need four
263                  * bytes more.
264                  */
265                 dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long);
266                 dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
267         } else
268                 dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long);
269
270         /* Get the current time when we get the stats */
271         gettimeofday(&stats->busy_time, NULL);
272
273         /*
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.
280          */
281         if ((error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr, 
282              &dssize, NULL, 0)) == -1) {
283                 /*
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.
287                  */
288                 if (errno == ENOMEM) {
289                         /*
290                          * No need to set the error string here, getnumdevs()
291                          * will do that if it fails.
292                          */
293                         if ((dinfo->numdevs = getnumdevs()) < 0)
294                                 return(-1);
295
296                         dssize = (dinfo->numdevs * sizeof(struct devstat)) +
297                                 sizeof(long);
298                         dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
299                                                              dssize);
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,
305                                         strerror(errno));
306                                 return(-1);
307                         }
308                 } else {
309                         sprintf(devstat_errbuf,
310                                 "%s: error getting device stats\n"
311                                 "%s: %s", func_name, func_name,
312                                 strerror(errno));
313                         return(-1);
314                 }
315         } 
316
317         /*
318          * The sysctl spits out the generation as the first four bytes,
319          * then all of the device statistics structures.
320          */
321         dinfo->generation = *(long *)dinfo->mem_ptr;
322
323         /*
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
336          * necessary.
337          */
338         if (oldgeneration != dinfo->generation) {
339                 if (getnumdevs() != dinfo->numdevs) {
340                         if ((dinfo->numdevs = getnumdevs()) < 0)
341                                 return(-1);
342                         dssize = (dinfo->numdevs * sizeof(struct devstat)) +
343                                 sizeof(long);
344                         dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
345                                                              dssize);
346                 }
347                 retval = 1;
348         }
349
350         dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
351
352         return(retval);
353 }
354
355 /*
356  * selectdevs():
357  *
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.
365  *
366  * Input parameters:
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
390  *        number of devices. 
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
394  *        selected.
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)
404  *
405  * Output data:
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
410  * 
411  * Return values:
412  * -1  -- error
413  *  0  -- selected devices are unchanged
414  *  1  -- selected devices changed
415  */
416 int
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)
423 {
424         int i, j, k;
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;
430
431         if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0))
432                 return(-1);
433
434         /*
435          * We always want to make sure that we have as many dev_select
436          * entries as there are devices. 
437          */
438         /*
439          * In this case, we haven't selected devices before.
440          */
441         if (*dev_select == NULL) {
442                 *dev_select = (struct device_selection *)malloc(numdevs *
443                         sizeof(struct device_selection));
444                 *select_generation = current_generation;
445                 init_selections = 1;
446                 changed = 1;
447         /*
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.
451          */
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;
456                 init_selections = 1;
457         /*
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
460          * list has changed.
461          */
462         } else if (*select_generation < current_generation) {
463                 *select_generation = current_generation;
464                 init_selections = 1;
465         }
466
467         /*
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
470          * this time through.
471          */
472         if (select_mode == DS_SELECT_ONLY)
473                 init_selected_var = 1;
474
475         /*
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.
479          */
480         old_num_selected = *num_selected;
481
482         /*
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
488          * selection code.
489          */
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);
497         }
498
499         if (init_selections != 0) {
500                 bzero(*dev_select, sizeof(struct device_selection) * numdevs);
501
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,
507                                 DEVSTAT_NAME_LEN);
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;
511                 }
512                 *num_selections = numdevs;
513         } else if (init_selected_var != 0) {
514                 for (i = 0; i < numdevs; i++) 
515                         (*dev_select)[i].selected = 0;
516         }
517
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))
521                 *num_selected = 0;
522
523         /*
524          * Look through any devices the user specified on the command line
525          * and see if they match known devices.  If so, select them.
526          */
527         for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
528                 char tmpstr[80];
529
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) {
535                                 /*
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.
550                                  */
551                                 switch(select_mode) {
552                                 case DS_SELECT_ADD:
553                                 case DS_SELECT_ADDONLY:
554                                         if ((*dev_select)[i].selected)
555                                                 break;
556                                         /* FALLTHROUGH */
557                                 case DS_SELECT_ONLY:
558                                         (*dev_select)[i].selected =
559                                                 ++selection_number;
560                                         (*num_selected)++;
561                                         break;
562                                 case DS_SELECT_REMOVE:
563                                         (*dev_select)[i].selected = 0;
564                                         (*num_selected)--;
565                                         /*
566                                          * This isn't passed back out, we
567                                          * just use it to keep track of
568                                          * how many devices we've removed.
569                                          */
570                                         num_dev_selections--;
571                                         break;
572                                 }
573                                 break;
574                         }
575                 }
576         }
577
578         /*
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.
582          */
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))
587                         continue;
588
589                 for (j = 0; j < numdevs; j++) {
590                         int num_match_categories;
591
592                         num_match_categories = matches[i].num_match_categories;
593
594                         /*
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.
605                          * 
606                          */
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))) {
628
629                                 /*
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
634                                  * array.
635                                  */
636                                 for (k = 0; k < numdevs; k++) {
637                                         if ((*dev_select)[k].position == j) {
638                                                 found = 1;
639                                                 break;
640                                         }
641                                 }
642
643                                 /*
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.
647                                  */
648                                 if (found != 1) {
649                                         fprintf(stderr, "selectdevs: couldn't"
650                                                 " find %s%d in selection "
651                                                 "list\n",
652                                                 devices[j].device_name,
653                                                 devices[j].unit_number);
654                                         break;
655                                 }
656
657                                 /*
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.
670                                  */
671                                 switch(select_mode) {
672                                 case DS_SELECT_ADD:
673                                 case DS_SELECT_ADDONLY:
674                                 case DS_SELECT_ONLY:
675                                         if ((*dev_select)[k].selected != 0)
676                                                 break;
677                                         (*dev_select)[k].selected =
678                                                 ++selection_number;
679                                         (*num_selected)++;
680                                         break;
681                                 case DS_SELECT_REMOVE:
682                                         (*dev_select)[k].selected = 0;
683                                         (*num_selected)--;
684                                         break;
685                                 }
686                         }
687                 }
688         }
689
690         /*
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.
698          *
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.
708          */
709         if (perf_select != 0) {
710
711                 /* Sort the device array by throughput  */
712                 qsort(*dev_select, *num_selections,
713                       sizeof(struct device_selection),
714                       compare_select);
715
716                 if (*num_selected == 0) {
717                         /*
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.
723                          */
724                         for (i = 0; i < *num_selections; i++) {
725                                 if ((*dev_select)[i].selected == 0) {
726                                         (*dev_select)[i].selected =
727                                                 ++selection_number;
728                                         (*num_selected)++;
729                                 }
730                         }
731                 } else {
732                         selection_number = 0;
733                         for (i = 0; i < *num_selections; i++) {
734                                 if ((*dev_select)[i].selected != 0) {
735                                         (*dev_select)[i].selected =
736                                                 ++selection_number;
737                                 }
738                         }
739                 }
740         }
741
742         /*
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.
753          */
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;
758                                 (*num_selected)++;
759                         }
760         }
761
762         /*
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.
767          */
768         if ((changed == 0) && (old_num_selected != *num_selected))
769                 changed = 1;
770         else if ((changed == 0) && (old_dev_select != NULL)) {
771                 /*
772                  * Now we go through the selection list and we look at
773                  * it three different ways.
774                  */
775                 for (i = 0; (i < *num_selections) && (changed == 0) && 
776                      (i < old_num_selections); i++) {
777                         /*
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.
782                          */
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))
787                                 continue;
788
789                         /*
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.
797                          */
798                         else if ((*dev_select)[i].device_number ==
799                                   old_dev_select[i].device_number) {
800                                 changed = 1;
801                                 break;
802                         }
803                         /*
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.
807                          */
808                         else {
809                                 found = 0;
810
811                                 /*
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
820                                  * set to 1 below.
821                                  */
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)){
827                                                 found = 1;
828                                                 break;
829                                         }
830                                         else if ((*dev_select)[i].device_number
831                                             == old_dev_select[j].device_number)
832                                                 break;
833                                 }
834                                 if (found == 0)
835                                         changed = 1;
836                         }
837                 }
838         }
839         if (old_dev_select != NULL)
840                 free(old_dev_select);
841
842         return(changed);
843 }
844
845 /*
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
855  * performance.
856  */
857 static int
858 compare_select(const void *arg1, const void *arg2)
859 {
860         if ((((const struct device_selection *)arg1)->selected)
861          && (((const struct device_selection *)arg2)->selected == 0))
862                 return(-1);
863         else if ((((const struct device_selection *)arg1)->selected == 0)
864               && (((const struct device_selection *)arg2)->selected))
865                 return(1);
866         else if (((const struct device_selection *)arg2)->bytes <
867                  ((const struct device_selection *)arg1)->bytes)
868                 return(-1);
869         else if (((const struct device_selection *)arg2)->bytes >
870                  ((const struct device_selection *)arg1)->bytes)
871                 return(1);
872         else
873                 return(0);
874 }
875
876 /*
877  * Take a string with the general format "arg1,arg2,arg3", and build a
878  * device matching expression from it.
879  */
880 int
881 buildmatch(const char *match_str, struct devstat_match **matches,
882            int *num_matches)
883 {
884         char *tstr[5];
885         char **tempstr;
886         char *matchbuf_orig;    /* strdup of match_str */
887         char *matchbuf;         /* allow strsep to clobber */
888         int num_args;
889         int i, j;
890         int retval = -1;
891
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__);
895                 return(-1);
896         }
897
898         /*
899          * Break the (comma delimited) input string out into separate strings.
900          * strsep is destructive, so copy the string first.
901          */
902         matchbuf = matchbuf_orig = strdup(match_str);
903         if (matchbuf == NULL) {
904                 sprintf(devstat_errbuf, "%s: out of memory", __func__);
905                 return(-1);
906         }
907         for (tempstr = tstr, num_args  = 0; 
908              (*tempstr = strsep(&matchbuf, ",")) != NULL && (num_args < 5); 
909              num_args++)
910                 if (**tempstr != '\0')
911                         if (++tempstr >= &tstr[5])
912                                 break;
913
914         /* The user gave us too many type arguments */
915         if (num_args > 3) {
916                 sprintf(devstat_errbuf, "%s: too many type arguments",
917                         __func__);
918                 goto cleanup;
919         }
920
921         /*
922          * Since you can't realloc a pointer that hasn't been malloced
923          * first, we malloc first and then realloc.
924          */
925         if (*num_matches == 0)
926                 *matches = (struct devstat_match *)malloc(
927                            sizeof(struct devstat_match));
928         else
929                 *matches = (struct devstat_match *)realloc(*matches,
930                           sizeof(struct devstat_match) * (*num_matches + 1));
931                           
932         /* Make sure the current entry is clear */
933         bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
934
935         /*
936          * Step through the arguments the user gave us and build a device
937          * matching expression from them.
938          */
939         for (i = 0; i < num_args; i++) {
940                 char *tempstr2, *tempstr3;
941
942                 /*
943                  * Get rid of leading white space.
944                  */
945                 tempstr2 = tstr[i];
946                 while (isspace(*tempstr2) && (*tempstr2 != '\0'))
947                         tempstr2++;
948
949                 /*
950                  * Get rid of trailing white space.
951                  */
952                 tempstr3 = &tempstr2[strlen(tempstr2) - 1];
953
954                 while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
955                     && (isspace(*tempstr3))) {
956                         *tempstr3 = '\0';
957                         tempstr3--;
958                 }
959
960                 /*
961                  * Go through the match table comparing the user's
962                  * arguments to known device types, interfaces, etc.  
963                  */
964                 for (j = 0; match_table[j].match_str != NULL; j++) {
965                         /*
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.  
972                          */
973                         if (strncasecmp(tempstr2, match_table[j].match_str,
974                             strlen(match_table[j].match_str)) == 0) {
975                                 /*
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.
979                                  */
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__);
986                                         goto cleanup;
987                                 }
988                                 /*
989                                  * If we've gotten this far, we have a
990                                  * winner.  Set the appropriate fields in
991                                  * the match entry.
992                                  */
993                                 (*matches)[*num_matches].match_fields |=
994                                         match_table[j].match_field;
995                                 (*matches)[*num_matches].device_type |=
996                                         match_table[j].type;
997                                 (*matches)[*num_matches].num_match_categories++;
998                                 break;
999                         }
1000                 }
1001                 /*
1002                  * We should have found a match in the above for loop.  If
1003                  * not, that means the user entered an invalid device type
1004                  * or interface.
1005                  */
1006                 if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
1007                         snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1008                                 "%s: unknown match item \"%s\"", __func__,
1009                                 tstr[i]);
1010                         goto cleanup;
1011                 }
1012         }
1013
1014         (*num_matches)++;
1015         retval = 0;
1016 cleanup:
1017         free(matchbuf_orig);
1018         return(retval);
1019 }
1020
1021 /*
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.
1026  * Return values:
1027  * 0   -- success
1028  * -1  -- failure
1029  */
1030 int
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)
1037 {
1038         u_int64_t totalbytes, totaltransfers, totalblocks;
1039
1040         /*
1041          * current is the only mandatory field.
1042          */
1043         if (current == NULL) {
1044                 sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1045                         __func__);
1046                 return(-1);
1047         }
1048
1049         totalbytes = (current->bytes_written + current->bytes_read) -
1050                      ((previous) ? (previous->bytes_written +
1051                                     previous->bytes_read) : 0);
1052
1053         if (total_bytes)
1054                 *total_bytes = totalbytes;
1055
1056         totaltransfers = (current->num_reads +
1057                           current->num_writes +
1058                           current->num_other) -
1059                          ((previous) ?
1060                           (previous->num_reads +
1061                            previous->num_writes +
1062                            previous->num_other) : 0);
1063         if (total_transfers)
1064                 *total_transfers = totaltransfers;
1065
1066         if (transfers_per_second) {
1067                 if (etime > 0.0) {
1068                         *transfers_per_second = totaltransfers;
1069                         *transfers_per_second /= etime;
1070                 } else
1071                         *transfers_per_second = 0.0;
1072         }
1073
1074         if (kb_per_transfer) {
1075                 *kb_per_transfer = totalbytes;
1076                 *kb_per_transfer /= 1024;
1077                 if (totaltransfers > 0)
1078                         *kb_per_transfer /= totaltransfers;
1079                 else
1080                         *kb_per_transfer = 0.0;
1081         }
1082
1083         if (mb_per_second) {
1084                 *mb_per_second = totalbytes;
1085                 *mb_per_second /= 1024 * 1024;
1086                 if (etime > 0.0)
1087                         *mb_per_second /= etime;
1088                 else
1089                         *mb_per_second = 0.0;
1090         }
1091
1092         totalblocks = totalbytes;
1093         if (current->block_size > 0)
1094                 totalblocks /= current->block_size;
1095         else
1096                 totalblocks /= 512;
1097
1098         if (total_blocks)
1099                 *total_blocks = totalblocks;
1100
1101         if (blocks_per_second) {
1102                 *blocks_per_second = totalblocks;
1103                 if (etime > 0.0)
1104                         *blocks_per_second /= etime;
1105                 else
1106                         *blocks_per_second = 0.0;
1107         }
1108
1109         if (ms_per_transaction) {
1110                 if (totaltransfers > 0) {
1111                         *ms_per_transaction = etime;
1112                         *ms_per_transaction /= totaltransfers;
1113                         *ms_per_transaction *= 1000;
1114                 } else
1115                         *ms_per_transaction = 0.0;
1116         }
1117
1118         return(0);
1119 }
1120
1121 int
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)
1128 {
1129         u_int64_t totalbytes, totaltransfers, totalblocks;
1130
1131         /*
1132          * current is the only mandatory field.
1133          */
1134         if (current == NULL) {
1135                 sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1136                         __func__);
1137                 return(-1);
1138         }
1139
1140         totalbytes = current->bytes_read -
1141                      (previous ? previous->bytes_read : 0);
1142
1143         if (total_bytes)
1144                 *total_bytes = totalbytes;
1145
1146         totaltransfers = current->num_reads -
1147                          (previous ? previous->num_reads : 0);
1148         if (total_transfers)
1149                 *total_transfers = totaltransfers;
1150
1151         if (transfers_per_second) {
1152                 if (etime > 0.0) {
1153                         *transfers_per_second = totaltransfers;
1154                         *transfers_per_second /= etime;
1155                 } else
1156                         *transfers_per_second = 0.0;
1157         }
1158
1159         if (kb_per_transfer) {
1160                 *kb_per_transfer = totalbytes;
1161                 *kb_per_transfer /= 1024;
1162                 if (totaltransfers > 0)
1163                         *kb_per_transfer /= totaltransfers;
1164                 else
1165                         *kb_per_transfer = 0.0;
1166         }
1167
1168         if (mb_per_second) {
1169                 *mb_per_second = totalbytes;
1170                 *mb_per_second /= 1024 * 1024;
1171                 if (etime > 0.0)
1172                         *mb_per_second /= etime;
1173                 else
1174                         *mb_per_second = 0.0;
1175         }
1176
1177         totalblocks = totalbytes;
1178         if (current->block_size > 0)
1179                 totalblocks /= current->block_size;
1180         else
1181                 totalblocks /= 512;
1182
1183         if (total_blocks)
1184                 *total_blocks = totalblocks;
1185
1186         if (blocks_per_second) {
1187                 *blocks_per_second = totalblocks;
1188                 if (etime > 0.0)
1189                         *blocks_per_second /= etime;
1190                 else
1191                         *blocks_per_second = 0.0;
1192         }
1193
1194         if (ms_per_transaction) {
1195                 if (totaltransfers > 0) {
1196                         *ms_per_transaction = etime;
1197                         *ms_per_transaction /= totaltransfers;
1198                         *ms_per_transaction *= 1000;
1199                 } else
1200                         *ms_per_transaction = 0.0;
1201         }
1202
1203         return(0);
1204 }
1205
1206 int
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)
1213 {
1214         u_int64_t totalbytes, totaltransfers, totalblocks;
1215
1216         /*
1217          * current is the only mandatory field.
1218          */
1219         if (current == NULL) {
1220                 sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1221                         __func__);
1222                 return(-1);
1223         }
1224
1225         totalbytes = current->bytes_written -
1226                      (previous ? previous->bytes_written : 0);
1227
1228         if (total_bytes)
1229                 *total_bytes = totalbytes;
1230
1231         totaltransfers = current->num_writes -
1232                          (previous ? previous->num_writes : 0);
1233         if (total_transfers)
1234                 *total_transfers = totaltransfers;
1235
1236         if (transfers_per_second) {
1237                 if (etime > 0.0) {
1238                         *transfers_per_second = totaltransfers;
1239                         *transfers_per_second /= etime;
1240                 } else
1241                         *transfers_per_second = 0.0;
1242         }
1243
1244         if (kb_per_transfer) {
1245                 *kb_per_transfer = totalbytes;
1246                 *kb_per_transfer /= 1024;
1247                 if (totaltransfers > 0)
1248                         *kb_per_transfer /= totaltransfers;
1249                 else
1250                         *kb_per_transfer = 0.0;
1251         }
1252
1253         if (mb_per_second) {
1254                 *mb_per_second = totalbytes;
1255                 *mb_per_second /= 1024 * 1024;
1256                 if (etime > 0.0)
1257                         *mb_per_second /= etime;
1258                 else
1259                         *mb_per_second = 0.0;
1260         }
1261
1262         totalblocks = totalbytes;
1263         if (current->block_size > 0)
1264                 totalblocks /= current->block_size;
1265         else
1266                 totalblocks /= 512;
1267
1268         if (total_blocks)
1269                 *total_blocks = totalblocks;
1270
1271         if (blocks_per_second) {
1272                 *blocks_per_second = totalblocks;
1273                 if (etime > 0.0)
1274                         *blocks_per_second /= etime;
1275                 else
1276                         *blocks_per_second = 0.0;
1277         }
1278
1279         if (ms_per_transaction) {
1280                 if (totaltransfers > 0) {
1281                         *ms_per_transaction = etime;
1282                         *ms_per_transaction /= totaltransfers;
1283                         *ms_per_transaction *= 1000;
1284                 } else
1285                         *ms_per_transaction = 0.0;
1286         }
1287
1288         return(0);
1289 }
1290
1291 long double
1292 compute_etime(struct timeval cur_time, struct timeval prev_time)
1293 {
1294         struct timeval busy_time;
1295         u_int64_t busy_usec;
1296         long double etime;
1297
1298         timersub(&cur_time, &prev_time, &busy_time);
1299
1300         busy_usec = busy_time.tv_sec;  
1301         busy_usec *= 1000000;          
1302         busy_usec += busy_time.tv_usec;
1303         etime = busy_usec;
1304         etime /= 1000000;
1305
1306         return(etime);
1307 }