Home | History | Annotate | Line # | Download | only in vmstat
drvstats.c revision 1.10
      1  1.10   mlelstv /*	$NetBSD: drvstats.c,v 1.10 2017/03/05 23:07:12 mlelstv Exp $	*/
      2   1.1     blymn 
      3   1.1     blymn /*
      4   1.1     blymn  * Copyright (c) 1996 John M. Vinopal
      5   1.1     blymn  * All rights reserved.
      6   1.1     blymn  *
      7   1.1     blymn  * Redistribution and use in source and binary forms, with or without
      8   1.1     blymn  * modification, are permitted provided that the following conditions
      9   1.1     blymn  * are met:
     10   1.1     blymn  * 1. Redistributions of source code must retain the above copyright
     11   1.1     blymn  *    notice, this list of conditions and the following disclaimer.
     12   1.1     blymn  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     blymn  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     blymn  *    documentation and/or other materials provided with the distribution.
     15   1.1     blymn  * 3. All advertising materials mentioning features or use of this software
     16   1.1     blymn  *    must display the following acknowledgement:
     17   1.1     blymn  *      This product includes software developed for the NetBSD Project
     18   1.1     blymn  *      by John M. Vinopal.
     19   1.1     blymn  * 4. The name of the author may not be used to endorse or promote products
     20   1.1     blymn  *    derived from this software without specific prior written permission.
     21   1.1     blymn  *
     22   1.1     blymn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1     blymn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1     blymn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1     blymn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1     blymn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27   1.1     blymn  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28   1.1     blymn  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29   1.1     blymn  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30   1.1     blymn  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1     blymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1     blymn  * SUCH DAMAGE.
     33   1.1     blymn  */
     34   1.1     blymn 
     35   1.1     blymn #include <sys/param.h>
     36   1.1     blymn #include <sys/sched.h>
     37   1.1     blymn #include <sys/sysctl.h>
     38   1.1     blymn #include <sys/time.h>
     39   1.1     blymn #include <sys/iostat.h>
     40   1.1     blymn 
     41   1.1     blymn #include <err.h>
     42   1.1     blymn #include <fcntl.h>
     43   1.1     blymn #include <limits.h>
     44   1.1     blymn #include <stdio.h>
     45   1.1     blymn #include <stdlib.h>
     46   1.1     blymn #include <string.h>
     47   1.1     blymn #include <unistd.h>
     48   1.1     blymn #include "drvstats.h"
     49   1.1     blymn 
     50   1.1     blymn /* Structures to hold the statistics. */
     51   1.1     blymn struct _drive	cur, last;
     52   1.1     blymn 
     53   1.1     blymn extern int	hz;
     54   1.1     blymn 
     55   1.1     blymn /* sysctl hw.drivestats buffer. */
     56   1.1     blymn static struct io_sysctl	*drives = NULL;
     57   1.1     blymn 
     58   1.1     blymn /* Backward compatibility references. */
     59   1.4  christos size_t		ndrive = 0;
     60   1.1     blymn int		*drv_select;
     61   1.1     blymn char		**dr_name;
     62   1.1     blymn 
     63   1.1     blymn /* Missing from <sys/time.h> */
     64   1.1     blymn #define	timerset(tvp, uvp) do {						\
     65   1.1     blymn 	((uvp)->tv_sec = (tvp)->tv_sec);				\
     66   1.1     blymn 	((uvp)->tv_usec = (tvp)->tv_usec);				\
     67   1.1     blymn } while (/* CONSTCOND */0)
     68   1.1     blymn 
     69   1.1     blymn /*
     70   1.1     blymn  * Take the delta between the present values and the last recorded
     71   1.1     blymn  * values, storing the present values in the 'last' structure, and
     72   1.1     blymn  * the delta values in the 'cur' structure.
     73   1.1     blymn  */
     74   1.1     blymn void
     75   1.1     blymn drvswap(void)
     76   1.1     blymn {
     77   1.1     blymn 	u_int64_t tmp;
     78   1.5     lukem 	size_t	i;
     79   1.1     blymn 
     80   1.1     blymn #define	SWAP(fld) do {							\
     81   1.1     blymn 	tmp = cur.fld;							\
     82   1.1     blymn 	cur.fld -= last.fld;						\
     83   1.1     blymn 	last.fld = tmp;							\
     84   1.1     blymn } while (/* CONSTCOND */0)
     85   1.1     blymn 
     86  1.10   mlelstv #define DELTA(x) do {							\
     87  1.10   mlelstv 		timerclear(&tmp_timer);					\
     88  1.10   mlelstv 		timerset(&(cur.x), &tmp_timer);				\
     89  1.10   mlelstv 		timersub(&tmp_timer, &(last.x), &(cur.x));		\
     90  1.10   mlelstv 		timerclear(&(last.x));					\
     91  1.10   mlelstv 		timerset(&tmp_timer, &(last.x));			\
     92  1.10   mlelstv } while (/* CONSTCOND */0)
     93  1.10   mlelstv 
     94   1.1     blymn 	for (i = 0; i < ndrive; i++) {
     95   1.1     blymn 		struct timeval	tmp_timer;
     96   1.1     blymn 
     97   1.1     blymn 		if (!cur.select[i])
     98   1.1     blymn 			continue;
     99   1.1     blymn 
    100   1.1     blymn 		/* Delta Values. */
    101   1.1     blymn 		SWAP(rxfer[i]);
    102   1.1     blymn 		SWAP(wxfer[i]);
    103   1.1     blymn 		SWAP(seek[i]);
    104   1.1     blymn 		SWAP(rbytes[i]);
    105   1.1     blymn 		SWAP(wbytes[i]);
    106   1.1     blymn 
    107  1.10   mlelstv 		DELTA(wait[i]);
    108  1.10   mlelstv 		DELTA(time[i]);
    109  1.10   mlelstv 		DELTA(waitsum[i]);
    110  1.10   mlelstv 		DELTA(busysum[i]);
    111   1.1     blymn 	}
    112   1.1     blymn }
    113   1.1     blymn 
    114   1.1     blymn void
    115   1.1     blymn tkswap(void)
    116   1.1     blymn {
    117   1.1     blymn 	u_int64_t tmp;
    118   1.1     blymn 
    119   1.1     blymn 	SWAP(tk_nin);
    120   1.1     blymn 	SWAP(tk_nout);
    121   1.1     blymn }
    122   1.1     blymn 
    123   1.1     blymn void
    124   1.1     blymn cpuswap(void)
    125   1.1     blymn {
    126   1.1     blymn 	double etime;
    127   1.1     blymn 	u_int64_t tmp;
    128   1.1     blymn 	int	i, state;
    129   1.1     blymn 
    130   1.1     blymn 	for (i = 0; i < CPUSTATES; i++)
    131   1.1     blymn 		SWAP(cp_time[i]);
    132   1.1     blymn 
    133   1.1     blymn 	etime = 0;
    134   1.1     blymn 	for (state = 0; state < CPUSTATES; ++state) {
    135   1.1     blymn 		etime += cur.cp_time[state];
    136   1.1     blymn 	}
    137   1.1     blymn 	if (etime == 0)
    138   1.1     blymn 		etime = 1;
    139   1.1     blymn 	etime /= hz;
    140   1.1     blymn 	etime /= cur.cp_ncpu;
    141   1.1     blymn 
    142   1.1     blymn 	cur.cp_etime = etime;
    143   1.1     blymn }
    144  1.10   mlelstv #undef DELTA
    145   1.1     blymn #undef SWAP
    146   1.1     blymn 
    147   1.1     blymn /*
    148   1.1     blymn  * Read the drive statistics for each drive in the drive list.
    149   1.1     blymn  * Also collect statistics for tty i/o and CPU ticks.
    150   1.1     blymn  */
    151   1.1     blymn void
    152   1.1     blymn drvreadstats(void)
    153   1.1     blymn {
    154   1.5     lukem 	size_t		size, i;
    155   1.1     blymn 	int		mib[3];
    156   1.1     blymn 
    157   1.7     joerg 	mib[0] = CTL_HW;
    158   1.7     joerg 	mib[1] = HW_IOSTATS;
    159   1.7     joerg 	mib[2] = sizeof(struct io_sysctl);
    160   1.7     joerg 
    161   1.7     joerg 	size = ndrive * sizeof(struct io_sysctl);
    162   1.7     joerg 	if (sysctl(mib, 3, drives, &size, NULL, 0) < 0)
    163   1.7     joerg 		err(1, "sysctl hw.iostats failed");
    164  1.10   mlelstv 
    165  1.10   mlelstv #define COPYF(x,k) cur.x[k] = drives[k].x
    166  1.10   mlelstv #define COPYT(x,k) do {							\
    167  1.10   mlelstv 		cur.x[k].tv_sec = drives[k].x##_sec;			\
    168  1.10   mlelstv 		cur.x[k].tv_usec = drives[k].x##_usec;			\
    169  1.10   mlelstv } while (/* CONSTCOND */0)
    170  1.10   mlelstv 
    171   1.7     joerg 	for (i = 0; i < ndrive; i++) {
    172  1.10   mlelstv 
    173  1.10   mlelstv 		COPYF(rxfer, i);
    174  1.10   mlelstv 		COPYF(wxfer, i);
    175  1.10   mlelstv 		COPYF(seek, i);
    176  1.10   mlelstv 		COPYF(rbytes, i);
    177  1.10   mlelstv 		COPYF(wbytes, i);
    178  1.10   mlelstv 
    179  1.10   mlelstv 		COPYT(wait, i);
    180  1.10   mlelstv 		COPYT(time, i);
    181  1.10   mlelstv 		COPYT(waitsum, i);
    182  1.10   mlelstv 		COPYT(busysum, i);
    183   1.7     joerg 	}
    184   1.1     blymn 
    185  1.10   mlelstv 	mib[0] = CTL_KERN;
    186   1.7     joerg 	mib[1] = KERN_TKSTAT;
    187   1.7     joerg 	mib[2] = KERN_TKSTAT_NIN;
    188   1.7     joerg 	size = sizeof(cur.tk_nin);
    189   1.7     joerg 	if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
    190   1.7     joerg 		cur.tk_nin = 0;
    191   1.7     joerg 
    192   1.7     joerg 	mib[2] = KERN_TKSTAT_NOUT;
    193   1.7     joerg 	size = sizeof(cur.tk_nout);
    194   1.7     joerg 	if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
    195   1.7     joerg 		cur.tk_nout = 0;
    196   1.1     blymn 
    197   1.1     blymn 	size = sizeof(cur.cp_time);
    198   1.4  christos 	(void)memset(cur.cp_time, 0, size);
    199   1.7     joerg 	mib[0] = CTL_KERN;
    200   1.7     joerg 	mib[1] = KERN_CP_TIME;
    201   1.7     joerg 	if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
    202   1.7     joerg 		(void)memset(cur.cp_time, 0, sizeof(cur.cp_time));
    203   1.1     blymn }
    204  1.10   mlelstv #undef COPYT
    205  1.10   mlelstv #undef COPYF
    206   1.1     blymn 
    207   1.1     blymn /*
    208   1.1     blymn  * Read collect statistics for tty i/o.
    209   1.1     blymn  */
    210   1.1     blymn 
    211   1.1     blymn void
    212   1.1     blymn tkreadstats(void)
    213   1.1     blymn {
    214   1.1     blymn 	size_t		size;
    215   1.1     blymn 	int		mib[3];
    216   1.1     blymn 
    217   1.7     joerg 	mib[0] = CTL_KERN;
    218   1.7     joerg 	mib[1] = KERN_TKSTAT;
    219   1.7     joerg 	mib[2] = KERN_TKSTAT_NIN;
    220   1.7     joerg 	size = sizeof(cur.tk_nin);
    221   1.7     joerg 	if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
    222   1.7     joerg 		cur.tk_nin = 0;
    223   1.7     joerg 
    224   1.7     joerg 	mib[2] = KERN_TKSTAT_NOUT;
    225   1.7     joerg 	size = sizeof(cur.tk_nout);
    226   1.7     joerg 	if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
    227   1.7     joerg 		cur.tk_nout = 0;
    228   1.1     blymn }
    229   1.1     blymn 
    230   1.1     blymn /*
    231   1.1     blymn  * Read collect statistics for CPU ticks.
    232   1.1     blymn  */
    233   1.1     blymn 
    234   1.1     blymn void
    235   1.1     blymn cpureadstats(void)
    236   1.1     blymn {
    237   1.1     blymn 	size_t		size;
    238   1.1     blymn 	int		mib[2];
    239   1.1     blymn 
    240   1.1     blymn 	size = sizeof(cur.cp_time);
    241   1.4  christos 	(void)memset(cur.cp_time, 0, size);
    242   1.7     joerg 	mib[0] = CTL_KERN;
    243   1.7     joerg 	mib[1] = KERN_CP_TIME;
    244   1.7     joerg 	if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
    245   1.7     joerg 		(void)memset(cur.cp_time, 0, sizeof(cur.cp_time));
    246   1.1     blymn }
    247   1.1     blymn 
    248   1.1     blymn /*
    249   1.1     blymn  * Perform all of the initialization and memory allocation needed to
    250   1.1     blymn  * track drive statistics.
    251   1.1     blymn  */
    252   1.1     blymn int
    253   1.1     blymn drvinit(int selected)
    254   1.1     blymn {
    255   1.1     blymn 	struct clockinfo clockinfo;
    256   1.5     lukem 	size_t		size, i;
    257   1.1     blymn 	static int	once = 0;
    258   1.5     lukem 	int		mib[3];
    259   1.1     blymn 
    260   1.1     blymn 	if (once)
    261   1.1     blymn 		return (1);
    262   1.1     blymn 
    263   1.7     joerg 	mib[0] = CTL_HW;
    264   1.7     joerg 	mib[1] = HW_NCPU;
    265   1.7     joerg 	size = sizeof(cur.cp_ncpu);
    266   1.7     joerg 	if (sysctl(mib, 2, &cur.cp_ncpu, &size, NULL, 0) == -1)
    267   1.7     joerg 		err(1, "sysctl hw.ncpu failed");
    268   1.7     joerg 
    269   1.7     joerg 	mib[0] = CTL_KERN;
    270   1.7     joerg 	mib[1] = KERN_CLOCKRATE;
    271   1.7     joerg 	size = sizeof(clockinfo);
    272   1.7     joerg 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) == -1)
    273   1.7     joerg 		err(1, "sysctl kern.clockrate failed");
    274   1.7     joerg 	hz = clockinfo.stathz;
    275   1.7     joerg 	if (!hz)
    276   1.7     joerg 		hz = clockinfo.hz;
    277   1.7     joerg 
    278   1.7     joerg 	mib[0] = CTL_HW;
    279   1.7     joerg 	mib[1] = HW_IOSTATS;
    280   1.7     joerg 	mib[2] = sizeof(struct io_sysctl);
    281   1.7     joerg 	if (sysctl(mib, 3, NULL, &size, NULL, 0) == -1)
    282   1.7     joerg 		err(1, "sysctl hw.drivestats failed");
    283   1.7     joerg 	ndrive = size / sizeof(struct io_sysctl);
    284   1.1     blymn 
    285   1.7     joerg 	if (size == 0) {
    286   1.7     joerg 		warnx("No drives attached.");
    287   1.1     blymn 	} else {
    288   1.7     joerg 		drives = (struct io_sysctl *)malloc(size);
    289   1.7     joerg 		if (drives == NULL)
    290   1.7     joerg 			errx(1, "Memory allocation failure.");
    291   1.1     blymn 	}
    292   1.1     blymn 
    293   1.1     blymn 	/* Allocate space for the statistics. */
    294   1.1     blymn 	cur.time = calloc(ndrive, sizeof(struct timeval));
    295  1.10   mlelstv 	cur.wait = calloc(ndrive, sizeof(struct timeval));
    296  1.10   mlelstv 	cur.waitsum = calloc(ndrive, sizeof(struct timeval));
    297  1.10   mlelstv 	cur.busysum = calloc(ndrive, sizeof(struct timeval));
    298   1.1     blymn 	cur.rxfer = calloc(ndrive, sizeof(u_int64_t));
    299   1.1     blymn 	cur.wxfer = calloc(ndrive, sizeof(u_int64_t));
    300   1.1     blymn 	cur.seek = calloc(ndrive, sizeof(u_int64_t));
    301   1.1     blymn 	cur.rbytes = calloc(ndrive, sizeof(u_int64_t));
    302   1.1     blymn 	cur.wbytes = calloc(ndrive, sizeof(u_int64_t));
    303   1.1     blymn 	last.time = calloc(ndrive, sizeof(struct timeval));
    304  1.10   mlelstv 	last.wait = calloc(ndrive, sizeof(struct timeval));
    305  1.10   mlelstv 	last.waitsum = calloc(ndrive, sizeof(struct timeval));
    306  1.10   mlelstv 	last.busysum = calloc(ndrive, sizeof(struct timeval));
    307   1.1     blymn 	last.rxfer = calloc(ndrive, sizeof(u_int64_t));
    308   1.1     blymn 	last.wxfer = calloc(ndrive, sizeof(u_int64_t));
    309   1.1     blymn 	last.seek = calloc(ndrive, sizeof(u_int64_t));
    310   1.1     blymn 	last.rbytes = calloc(ndrive, sizeof(u_int64_t));
    311   1.1     blymn 	last.wbytes = calloc(ndrive, sizeof(u_int64_t));
    312   1.1     blymn 	cur.select = calloc(ndrive, sizeof(int));
    313   1.1     blymn 	cur.name = calloc(ndrive, sizeof(char *));
    314   1.1     blymn 
    315  1.10   mlelstv 	if (cur.time == NULL || cur.wait == NULL ||
    316  1.10   mlelstv 	    cur.waitsum == NULL || cur.busysum == NULL ||
    317  1.10   mlelstv 	    cur.rxfer == NULL || cur.wxfer == NULL ||
    318  1.10   mlelstv 	    cur.seek == NULL || cur.rbytes == NULL ||
    319  1.10   mlelstv 	    cur.wbytes == NULL ||
    320  1.10   mlelstv 	    last.time == NULL || last.wait == NULL ||
    321  1.10   mlelstv 	    last.waitsum == NULL || last.busysum == NULL ||
    322  1.10   mlelstv 	    last.rxfer == NULL || last.wxfer == NULL ||
    323  1.10   mlelstv 	    last.seek == NULL || last.rbytes == NULL ||
    324  1.10   mlelstv 	    last.wbytes == NULL ||
    325   1.1     blymn 	    cur.select == NULL || cur.name == NULL)
    326   1.1     blymn 		errx(1, "Memory allocation failure.");
    327   1.1     blymn 
    328   1.1     blymn 	/* Set up the compatibility interfaces. */
    329   1.1     blymn 	drv_select = cur.select;
    330   1.1     blymn 	dr_name = cur.name;
    331   1.1     blymn 
    332   1.1     blymn 	/* Read the drive names and set intial selection. */
    333   1.7     joerg 	mib[0] = CTL_HW;		/* Should be still set from */
    334   1.7     joerg 	mib[1] = HW_IOSTATS;		/* ... above, but be safe... */
    335   1.7     joerg 	mib[2] = sizeof(struct io_sysctl);
    336   1.7     joerg 	if (sysctl(mib, 3, drives, &size, NULL, 0) == -1)
    337   1.7     joerg 		err(1, "sysctl hw.iostats failed");
    338   1.7     joerg 	for (i = 0; i < ndrive; i++) {
    339   1.7     joerg 		cur.name[i] = drives[i].name;
    340   1.7     joerg 		cur.select[i] = selected;
    341   1.1     blymn 	}
    342   1.1     blymn 
    343   1.1     blymn 	/* Never do this initialization again. */
    344   1.1     blymn 	once = 1;
    345   1.1     blymn 	return (1);
    346   1.1     blymn }
    347