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

📄 m_freebsd20.c

📁 查看系统硬件和内存资源使用情况;以及各个进程的使用情况
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * top - a top users display for Unix * * SYNOPSIS:  For a FreeBSD-2.0 (4.4BSD) system *	      Note process resident sizes could be wrong, but ps shows *	      zero for them too.. * * DESCRIPTION: * Originally written for BSD4.4 system by Christos Zoulas. * Ported to FreeBSD 2.0 by Steven Wallace && Wolfram Schneider * * This is the machine-dependent module for FreeBSD 2.x * Works for: *	FreeBSD 2.0 *	FreeBSD 2.1.5 *	FreeBSD 2.2 * * LIBS: -lkvm * * CFLAGS: -DHAVE_GETOPT * * AUTHOR:  Christos Zoulas <christos@ee.cornell.edu> *          Steven Wallace  <swallace@freebsd.org> *          Wolfram Schneider <wosch@cs.tu-berlin.de> * * $Id: machine.c,v 1.5 1995/01/06 02:04:39 swallace Exp $ */#define LASTPID      /**/  /* use last pid, compiler depended *//* #define LASTPID_FIXED /**/ #define VM_REAL      /**/  /* use the same values as vmstat -s */#define USE_SWAP     /**/  /* use swap usage (pstat -s),                               need to much cpu time *//* #define DEBUG 1      /**/#include <sys/types.h>#include <sys/signal.h>#include <sys/param.h>#include "os.h"#include <stdio.h>#include <nlist.h>#include <math.h>#include <kvm.h>#include <sys/errno.h>#include <sys/sysctl.h>#include <sys/dir.h>#include <sys/dkstat.h>#include <sys/file.h>#include <sys/time.h>#include <sys/vmmeter.h>#include <sys/user.h>#ifdef USE_SWAP#include <stdlib.h>#include <sys/rlist.h>#include <sys/conf.h>#endifstatic int check_nlist __P((struct nlist *));static int getkval __P((unsigned long, int *, int, char *));extern char* printable __P((char *));#include "top.h"#include "machine.h"/* get_process_info passes back a handle.  This is what it looks like: */struct handle{    struct kinfo_proc **next_proc;	/* points to next valid proc pointer */    int remaining;		/* number of pointers remaining */};/* declarations for load_avg */#include "loadavg.h"#define PP(pp, field) ((pp)->kp_proc . field)#define EP(pp, field) ((pp)->kp_eproc . field)#define VP(pp, field) ((pp)->kp_eproc.e_vm . field)/* define what weighted cpu is.  */#define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \			 ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu))))/* what we consider to be process size: */#define PROCSIZE(pp) (VP((pp), vm_tsize) + VP((pp), vm_dsize) + VP((pp), vm_ssize))/* definitions for indices in the nlist array */static struct nlist nlst[] = {#define X_CCPU		0    { "_ccpu" },		/* 0 */#define X_CP_TIME	1    { "_cp_time" },		/* 1 */#define X_HZ		2    { "_hz" },		        /* 2 */#define X_STATHZ	3    { "_stathz" },		/* 3 */#define X_AVENRUN	4    { "_averunnable" },		/* 4 */#ifdef USE_SWAP#define VM_SWAPLIST	5	{ "_swaplist" },/* list of free swap areas */#define VM_SWDEVT	6	{ "_swdevt" },	/* list of swap devices and sizes */#define VM_NSWAP	7	{ "_nswap" },	/* size of largest swap device */#define VM_NSWDEV	8	{ "_nswdev" },	/* number of swap devices */#define VM_DMMAX	9	{ "_dmmax" },	/* maximum size of a swap block */#endif#ifdef VM_REAL#ifdef USE_SWAP#define X_CNT           10#else#define X_CNT           5#endif    { "_cnt" },		        /* struct vmmeter cnt */#endif#ifdef LASTPID#if (defined USE_SWAP && defined VM_REAL)#define X_LASTPID	11#elif (defined VM_REAL)#define X_LASTPID	6#else#define X_LASTPID       5#endif#ifdef LASTPID_FIXED    { "_nextpid" },		#else    { "_nextpid.178" },		/* lastpid, compiler depended 				 * should be changed 				 * in /sys/kern/kern_fork.c */#endif#endif    { 0 }};/* *  These definitions control the format of the per-process area */static char header[] =  "  PID X        PRI NICE   SIZE   RES STATE   TIME   WCPU    CPU COMMAND";/* 0123456   -- field to fill in starts at header+6 */#define UNAME_START 6#define Proc_format \	"%5d %-8.8s %3d %4d%7s %5s %-5s%7s %5.2f%% %5.2f%% %.14s"/* process state names for the "STATE" column of the display *//* the extra nulls in the string "run" are for adding a slash and   the processor number when needed */char *state_abbrev[] ={    "", "start", "run\0\0\0", "sleep", "stop", "zomb", "WAIT"};static kvm_t *kd;/* values that we stash away in _init and use in later routines */static double logcpu;/* these are retrieved from the kernel in _init */static          long hz;static load_avg  ccpu;/* these are offsets obtained via nlist and used in the get_ functions */static unsigned long cp_time_offset;static unsigned long avenrun_offset;#ifdef LASTPIDstatic unsigned long lastpid_offset;static long lastpid;#endif#ifdef VM_REALstatic unsigned long cnt_offset;static long cnt;#endif/* these are for calculating cpu state percentages */static long cp_time[CPUSTATES];static long cp_old[CPUSTATES];static long cp_diff[CPUSTATES];/* these are for detailing the process states */int process_states[7];char *procstatenames[] = {    "", " starting, ", " running, ", " sleeping, ", " stopped, ",    " zombie, ", " ABANDONED, ",    NULL};/* these are for detailing the cpu states */int cpu_states[CPUSTATES];char *cpustatenames[] = {    "user", "nice", "system", "interrupt", "idle", NULL};/* these are for detailing the memory statistics */int memory_stats[8];char *memorynames[] = {#ifndef VM_REAL    "Real: ", "K/", "K ", "Virt: ", "K/",    "K ", "Free: ", "K", NULL#else#if 0    "K Act ", "K Inact ", "K Wired ", "K Free ", "% Swap, ",    "K/", "K SWIO", #else    "K Act ", "K Inact ", "K Wired ", "K Free ", "% Swap, ",    "Kin ", "Kout", #endif    NULL#endif};/* these are for keeping track of the proc array */static int nproc;static int onproc = -1;static int pref_len;static struct kinfo_proc *pbase;static struct kinfo_proc **pref;/* these are for getting the memory statistics */static int pageshift;		/* log base 2 of the pagesize *//* define pagetok in terms of pageshift */#define pagetok(size) ((size) << pageshift)/* useful externals */long percentages();intmachine_init(statics)struct statics *statics;{    register int i = 0;    register int pagesize;    if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)	return -1;    /* get the list of symbols we want to access in the kernel */    (void) kvm_nlist(kd, nlst);    if (nlst[0].n_type == 0)    {	fprintf(stderr, "top: nlist failed\n");	return(-1);    }    /* make sure they were all found */    if (i > 0 && check_nlist(nlst) > 0)    {	return(-1);    }    /* get the symbol values out of kmem */    (void) getkval(nlst[X_STATHZ].n_value, (int *)(&hz), sizeof(hz), "!");    if (!hz) {	(void) getkval(nlst[X_HZ].n_value, (int *)(&hz), sizeof(hz),		       nlst[X_HZ].n_name);    }#if (defined DEBUG)    fprintf(stderr, "Hertz: %d\n", hz); #endif    (void) getkval(nlst[X_CCPU].n_value,   (int *)(&ccpu),	sizeof(ccpu),	    nlst[X_CCPU].n_name);    /* stash away certain offsets for later use */    cp_time_offset = nlst[X_CP_TIME].n_value;    avenrun_offset = nlst[X_AVENRUN].n_value;#ifdef LASTPID    lastpid_offset =  nlst[X_LASTPID].n_value;#endif#ifdef VM_REAL    cnt_offset = nlst[X_CNT].n_value;#endif    /* this is used in calculating WCPU -- calculate it ahead of time */    logcpu = log(loaddouble(ccpu));    pbase = NULL;    pref = NULL;    nproc = 0;    onproc = -1;    /* get the page size with "getpagesize" and calculate pageshift from it */    pagesize = getpagesize();    pageshift = 0;    while (pagesize > 1)    {	pageshift++;	pagesize >>= 1;    }    /* we only need the amount of log(2)1024 for our conversion */    pageshift -= LOG1024;    /* fill in the statics information */    statics->procstate_names = procstatenames;    statics->cpustate_names = cpustatenames;    statics->memory_names = memorynames;    /* all done! */    return(0);}char *format_header(uname_field)register char *uname_field;{    register char *ptr;    ptr = header + UNAME_START;    while (*uname_field != '\0')    {	*ptr++ = *uname_field++;    }    return(header);}static int swappgsin = -1;static int swappgsout = -1;extern struct timeval timeout;voidget_system_info(si)struct system_info *si;{    long total;    load_avg avenrun[3];    /* get the cp_time array */    (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),		   nlst[X_CP_TIME].n_name);    (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),		   nlst[X_AVENRUN].n_name);#ifdef LASTPID    (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid),		   "!");#endif    /* convert load averages to doubles */    {	register int i;	register double *infoloadp;	load_avg *avenrunp;#ifdef notyet	struct loadavg sysload;	int size;	getkerninfo(KINFO_LOADAVG, &sysload, &size, 0);#endif	infoloadp = si->load_avg;	avenrunp = avenrun;	for (i = 0; i < 3; i++)	{#ifdef notyet	    *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;#endif	    *infoloadp++ = loaddouble(*avenrunp++);	}    }    /* convert cp_time counts to percentages */    total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);    /* sum memory statistics */    {#ifndef VM_REAL	struct vmtotal total;	int size = sizeof(total);	static int mib[] = { CTL_VM, VM_METER };	/* get total -- systemwide main memory usage structure */	if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {	    (void) fprintf(stderr, "top: sysctl failed: %s\n", strerror(errno));	    bzero(&total, sizeof(total));	}	/* convert memory stats to Kbytes */	memory_stats[0] = -1;	memory_stats[1] = pagetok(total.t_arm);	memory_stats[2] = pagetok(total.t_rm);	memory_stats[3] = -1;	memory_stats[4] = pagetok(total.t_avm);	memory_stats[5] = pagetok(total.t_vm);	memory_stats[6] = -1;	memory_stats[7] = pagetok(total.t_free);    }#else	struct vmmeter sum;	static unsigned int swap_delay = 0;        (void) getkval(cnt_offset, (int *)(&sum), sizeof(sum),		   "_cnt");	/* convert memory stats to Kbytes */	memory_stats[0] = pagetok(sum.v_active_count);	memory_stats[1] = pagetok(sum.v_inactive_count);	memory_stats[2] = pagetok(sum.v_wire_count);	memory_stats[3] = pagetok(sum.v_free_count);        if (swappgsin < 0) {	    memory_stats[5] = 0;	    memory_stats[6] = 0;	} else {	    memory_stats[5] = pagetok(((sum.v_swappgsin - swappgsin)));	    memory_stats[6] = pagetok(((sum.v_swappgsout - swappgsout)));	}        swappgsin = sum.v_swappgsin;	swappgsout = sum.v_swappgsout;#ifdef USE_SWAP        if ((memory_stats[5] > 0 || memory_stats[6]) > 0 || swap_delay == 0) {	    memory_stats[4] = swapmode();	}        swap_delay++;#else        memory_stats[4] = 0;

⌨️ 快捷键说明

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