⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmstat.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/*- * Copyright (c) 1983, 1989, 1992, 1993 *	The Regents of the University of California.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";#endif /* not lint *//* * Cursed vmstat -- from Robert Elz. */#include <sys/param.h>#include <sys/dkstat.h>#include <sys/buf.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/user.h>#include <sys/proc.h>#include <sys/namei.h>#include <sys/sysctl.h>#include <vm/vm.h>#include <signal.h>#include <nlist.h>#include <ctype.h>#include <utmp.h>#include <paths.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include "systat.h"#include "extern.h"static struct Info {	long	time[CPUSTATES];	struct	vmmeter Cnt;	struct	vmtotal Total;	long	*dk_time;	long	*dk_wds;	long	*dk_seek;	long	*dk_xfer;	int	dk_busy;	struct	nchstats nchstats;	long	nchcount;	long	*intrcnt;} s, s1, s2, z;#define	cnt s.Cnt#define oldcnt s1.Cnt#define	total s.Total#define	nchtotal s.nchstats#define	oldnchtotal s1.nchstatsstatic	enum state { BOOT, TIME, RUN } state = TIME;static void allocinfo __P((struct Info *));static void copyinfo __P((struct Info *, struct Info *));static float cputime __P((int));static void dinfo __P((int, int));static void getinfo __P((struct Info *, enum state));static void putint __P((int, int, int, int));static void putfloat __P((double, int, int, int, int, int));static int ucount __P((void));static	int ut;static	char buf[26];static	time_t t;static	double etime;static	float hertz;static	int nintr;static	long *intrloc;static	char **intrname;static	int nextintsrow;struct	utmp utmp;WINDOW *openkre(){	ut = open(_PATH_UTMP, O_RDONLY);	if (ut < 0)		error("No utmp");	return (stdscr);}voidclosekre(w)	WINDOW *w;{	(void) close(ut);	if (w == NULL)		return;	wclear(w);	wrefresh(w);}static struct nlist namelist[] = {#define X_CPTIME	0	{ "_cp_time" },#define X_CNT		1	{ "_cnt" },#define X_TOTAL		2	{ "_total" },#define	X_DK_BUSY	3	{ "_dk_busy" },#define	X_DK_TIME	4	{ "_dk_time" },#define	X_DK_XFER	5	{ "_dk_xfer" },#define	X_DK_WDS	6	{ "_dk_wds" },#define	X_DK_SEEK	7	{ "_dk_seek" },#define	X_NCHSTATS	8	{ "_nchstats" },#define	X_INTRNAMES	9	{ "_intrnames" },#define	X_EINTRNAMES	10	{ "_eintrnames" },#define	X_INTRCNT	11	{ "_intrcnt" },#define	X_EINTRCNT	12	{ "_eintrcnt" },	{ "" },};/* * These constants define where the major pieces are laid out */#define STATROW		 0	/* uses 1 row and 68 cols */#define STATCOL		 2#define MEMROW		 2	/* uses 4 rows and 31 cols */#define MEMCOL		 0#define PAGEROW		 2	/* uses 4 rows and 26 cols */#define PAGECOL		36#define INTSROW		 2	/* uses all rows to bottom and 17 cols */#define INTSCOL		63#define PROCSROW	 7	/* uses 2 rows and 20 cols */#define PROCSCOL	 0#define GENSTATROW	 7	/* uses 2 rows and 30 cols */#define GENSTATCOL	20#define VMSTATROW	 7	/* uses 17 rows and 12 cols */#define VMSTATCOL	48#define GRAPHROW	10	/* uses 3 rows and 51 cols */#define GRAPHCOL	 0#define NAMEIROW	14	/* uses 3 rows and 38 cols */#define NAMEICOL	 0#define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */#define DISKCOL		 0#define	DRIVESPACE	 9	/* max # for space */#if DK_NDRIVE > DRIVESPACE#define	MAXDRIVES	DRIVESPACE	 /* max # to display */#else#define	MAXDRIVES	DK_NDRIVE	 /* max # to display */#endifintinitkre(){	char *intrnamebuf, *cp;	int i;	static int once = 0;	if (namelist[0].n_type == 0) {		if (kvm_nlist(kd, namelist)) {			nlisterr(namelist);			return(0);		}		if (namelist[0].n_type == 0) {			error("No namelist");			return(0);		}	}	hertz = stathz ? stathz : hz;	if (! dkinit())		return(0);	if (dk_ndrive && !once) {#define	allocate(e, t) \    s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \    s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \    s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \    z./**/e = (t *)calloc(dk_ndrive, sizeof (t));		allocate(dk_time, long);		allocate(dk_wds, long);		allocate(dk_seek, long);		allocate(dk_xfer, long);		once = 1;#undef allocate	}	if (nintr == 0) {		nintr = (namelist[X_EINTRCNT].n_value -			namelist[X_INTRCNT].n_value) / sizeof (long);		intrloc = calloc(nintr, sizeof (long));		intrname = calloc(nintr, sizeof (long));		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -			namelist[X_INTRNAMES].n_value);		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {			error("Out of memory\n");			if (intrnamebuf)				free(intrnamebuf);			if (intrname)				free(intrname);			if (intrloc)				free(intrloc);			nintr = 0;			return(0);		}		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -			NVAL(X_INTRNAMES));		for (cp = intrnamebuf, i = 0; i < nintr; i++) {			intrname[i] = cp;			cp += strlen(cp) + 1;		}		nextintsrow = INTSROW + 2;		allocinfo(&s);		allocinfo(&s1);		allocinfo(&s2);		allocinfo(&z);	}	getinfo(&s2, RUN);	copyinfo(&s2, &s1);	return(1);}voidfetchkre(){	time_t now;	time(&now);	strcpy(buf, ctime(&now));	buf[16] = '\0';	getinfo(&s, state);}voidlabelkre(){	register int i, j;	clear();	mvprintw(STATROW, STATCOL + 4, "users    Load");	mvprintw(MEMROW, MEMCOL, "Mem:KB  REAL        VIRTUAL");	mvprintw(MEMROW + 1, MEMCOL, "      Tot Share    Tot  Share");	mvprintw(MEMROW + 2, MEMCOL, "Act");	mvprintw(MEMROW + 3, MEMCOL, "All");	mvprintw(MEMROW + 1, MEMCOL + 31, "Free");	mvprintw(PAGEROW, PAGECOL,     "        PAGING   SWAPPING ");	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");	mvprintw(PAGEROW + 2, PAGECOL, "count");	mvprintw(PAGEROW + 3, PAGECOL, "pages");	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");	mvprintw(INTSROW + 1, INTSCOL + 9, "total");	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");	if (LINES - 1 > VMSTATROW + 16)		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");	mvprintw(GRAPHROW, GRAPHCOL,		"    . %% Sys    . %% User    . %% Nice    . %% Idle");	mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");	mvprintw(GRAPHROW + 1, GRAPHCOL,		"|    |    |    |    |    |    |    |    |    |    |");	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");	mvprintw(NAMEIROW + 1, NAMEICOL,		"    Calls     hits    %%     hits     %%");	mvprintw(DISKROW, DISKCOL, "Discs");	mvprintw(DISKROW + 1, DISKCOL, "seeks");	mvprintw(DISKROW + 2, DISKCOL, "xfers");	mvprintw(DISKROW + 3, DISKCOL, " blks");	mvprintw(DISKROW + 4, DISKCOL, " msps");	j = 0;	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)		if (dk_select[i]) {			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,				"  %3.3s", dr_name[j]);			j++;		}	for (i = 0; i < nintr; i++) {		if (intrloc[i] == 0)			continue;		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);	}}#define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}#define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -