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

📄 m_svr5.c

📁 unix系统下top命令的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
}/* return the owner of the specified process, for use in commands.c as we're   running setuid root */uid_tproc_owner (pid_t pid){  register struct prpsinfo *p;  int i;  for (i = 0, p = pbase; i < nproc; i++, p++)    if (p->pr_pid == pid)      return (p->pr_uid);  return (-1);}intsetpriority (int dummy, int who, int niceval){  int scale;  int prio;  pcinfo_t pcinfo;  pcparms_t pcparms;  tsparms_t *tsparms;  strcpy (pcinfo.pc_clname, "TS");  if (priocntl (0, 0, PC_GETCID, (caddr_t) & pcinfo) == -1)    return (-1);  prio = niceval;  if (prio > PRIO_MAX)    prio = PRIO_MAX;  else if (prio < PRIO_MIN)    prio = PRIO_MIN;  tsparms = (tsparms_t *) pcparms.pc_clparms;  scale = ((tsinfo_t *) pcinfo.pc_clinfo)->ts_maxupri;  tsparms->ts_uprilim = tsparms->ts_upri = -(scale * prio) / 20;  pcparms.pc_cid = pcinfo.pc_cid;  if (priocntl (P_PID, who, PC_SETPARMS, (caddr_t) & pcparms) == -1)    return (-1);  return (0);}get_swapinfo(long *total, long *fr){    register int cnt, i;    register long t, f;    struct swaptable *swt;    struct swapent *ste;    static char path[256];    /* get total number of swap entries */    cnt = swapctl(SC_GETNSWP, 0);    /* allocate enough space to hold count + n swapents */    swt = (struct swaptable *)malloc(sizeof(int) +				     cnt * sizeof(struct swapent));    if (swt == NULL)    {	*total = 0;	*fr = 0;	return;    }    swt->swt_n = cnt;    /* fill in ste_path pointers: we don't care about the paths, so we point       them all to the same buffer */    ste = &(swt->swt_ent[0]);    i = cnt;    while (--i >= 0)    {	ste++->ste_path = path;    }    /* grab all swap info */    swapctl(SC_LIST, swt);    /* walk thru the structs and sum up the fields */    t = f = 0;    ste = &(swt->swt_ent[0]);    i = cnt;    while (--i >= 0)    {	/* dont count slots being deleted */	if (!(ste->ste_flags & ST_INDEL) )	{	    t += ste->ste_pages;	    f += ste->ste_free;	}	ste++;    }    /* fill in the results */    *total = t;    *fr = f;    free(swt);}/* * When we reach a proc limit, we need to realloc the stuff. */static void reallocproc(int n){    int bytes;    struct oldproc *op, *endbase;    if (n < maxprocs)	return;    maxprocs = n;    /* allocate space for proc structure array and array of pointers */    bytes = maxprocs * sizeof(psinfo_t) ;    pbase = (struct prpsinfo *) realloc(pbase, bytes);    pref = (struct prpsinfo **) realloc(pref,			maxprocs * sizeof(struct prpsinfo *));    /* Just in case ... */    if (pbase == (struct prpsinfo *) NULL || pref == (struct prpsinfo **) NULL)    {	fprintf (stderr, "%s: can't allocate sufficient memory\n", myname);	quit(1);    }}/* ---------------------------------------------------------------- *//* Access kernel Metrics  * SVR5 uses metreg inteface to Kernel statistics (metrics) *  see /usr/include/mas.h, /usr/include/metreg.h */#include <sys/mman.h>#include <sys/dl.h>#include <mas.h>#include <metreg.h> static int md;         /* metric descriptor handle */   static  uint32 ncpu;   /* number of processors in system *//* fwd dcls */static uint32 kmet_get_cpu( int type, char *desc);static void kmet_verify(     uint32 md,    metid_t id,  units_t units, type_t mettype,     uint32 metsz, uint32 nobj, uint32 nlocs,  resource_t res_id,     uint32 ressz ) ;static int get_cpustates(int *new){    new[0] = (int)kmet_get_cpu( MPC_CPU_IDLE, "idle");    new[1] = (int)kmet_get_cpu( MPC_CPU_USR,  "usr");    new[2] = (int)kmet_get_cpu( MPC_CPU_SYS,  "sys");    new[3] = (int)kmet_get_cpu( MPC_CPU_WIO,  "wio");}/* initialises kernel metrics access and gets #cpus */static int kmet_init(){    uint32 *ncpu_p;    /*  open (and map in) the metric access file and assoc data structures */    if( ( md = mas_open( MAS_FILE, MAS_MMAP_ACCESS ) ) < 0 )     {        (void)fprintf(stderr,"mas_open failed\n");        mas_perror();        quit(10);    }    /* verify the NCPU metric is everything we expect */    kmet_verify(md, NCPU, CPUS, CONFIGURABLE, sizeof(short),                   1, 1, MAS_SYSTEM, sizeof(uint32) );    /* get the number of cpu's on the system */    if( (ncpu_p = (uint32 *)mas_get_met( md, NCPU, 0 )) == NULL )     {        (void)fprintf(stderr,"mas_get_met of ncpu failed\n");          mas_perror();        quit(12);    }    ncpu = (uint32)(*(short *)ncpu_p);    /* check that MPC_CPU_IDLE is of the form we expect     *      ( paranoically we should check the rest as well but ... )     */    kmet_verify( md, MPC_CPU_IDLE, TIX, PROFILE, sizeof(uint32),                    1,  ncpu, NCPU, sizeof(short) );    kmet_verify( md, PROCUSE, PROCESSES, COUNT, sizeof(uint32),                    1,  1, MAS_SYSTEM, sizeof(uint32) );    nproc = kmet_get_nproc();    return 0;}/* done with kernel metrics access */static intkmet_done(){    if ( mas_close( md ) < 0 )    {        (void)fprintf(stderr,"mas_close failed\n");        mas_perror();        quit(14);    }}static uint32kmet_get_cpu( int type, char *desc){    int i;    uint32 r=0, rtot=0 ;    for (i=0; i <ncpu; i++)    {        r=*(uint32 *)mas_get_met( md, (metid_t)type, 0 );        if ( !r)        {            (void)fprintf(stderr,"mas_get_met of %s failed\n", desc);            mas_perror();            quit(12);        }        rtot += r;      /* sum them for multi cpus */    }    return rtot /* /ncpu */ ;}static intkmet_get_freemem(){    dl_t            *fm_p, fm, fmc, denom;    time_t          td1;    static time_t   td0;    static dl_t     fm_old;        td1 = time(NULL);    if ((fm_p = (dl_t *)mas_get_met( md, FREEMEM, 0 )) == NULL )    {        (void)fprintf(stderr,"mas_get_met of freemem failed\n");        mas_perror();        quit(12);    }    fm = *fm_p;         denom.dl_hop = 0;    denom.dl_lop = (long) (td1 - td0);    td0 = td1;    /* calculate the freemem difference divided by the time diff     * giving the freemem in that time sample     *  (new - old) / (time_between_samples)     */    fmc = lsub(fm, fm_old);    fm_old = fm;     fmc = ldivide(fmc, denom);    return  fmc.dl_lop;}/* * return # of processes currently executing on system */static intkmet_get_nproc(){    uint32 *p;    if ((p = (uint32 *)mas_get_met( md, PROCUSE, 0 )) == NULL )    {        (void)fprintf(stderr,"mas_get_met of procuse failed\n");        mas_perror();        quit(11);    }    nproc = (int)*p;}/* * Function: 	kmet_verify * renamed from mas_usrtime example verify_met() fm Doug Souders * * Description:	Verify the registration data associated with this metric  *		match what are expected.  Cautious consumer applications  *		should do this sort of verification before using metrics. */static voidkmet_verify(      uint32     md,         /* metric descriptor                */     metid_t    id,         /* metric id number                 */     units_t    units,      /* expected units of metric         */     type_t     mettype,    /* expected type of metric          */     uint32     metsz,      /* expected object size of metric   */     uint32     nobj,       /* expected number of array elements */     uint32     nlocs,      /* expected number of instances     */     resource_t res_id,     /* expected resource id number      */     uint32     ressz       /* expected resource object size    */     ){    char		*name;		/* the name of the metric 	*/    units_t		*units_p;	/* the units of the metric	*/    type_t		*mettype_p;	/* type field of the metric	*/    uint32 		*objsz_p;	/* size of each element in met 	*/    uint32 		*nobj_p;	/* num of elements >1 then array*/    uint32 		*nlocs_p;	/* total number of instances	*/    uint32 		*status_p;	/* status word (update|avail)	*/    resource_t	        *resource_p;	/* the resource list of the met	*/    uint32		*resval_p;	/* pointer to resource		*/    uint32 		*ressz_p;	/* size of the resource met	*/    if (!(name = mas_get_met_name( md, id )))     {            (void)fprintf(stderr,"mas_get_met_name failed\n");            mas_perror();            quit(11);    }        if (!(status_p = mas_get_met_status( md, id )))     {            (void)fprintf(stderr,"mas_get_met_status of %s failed\n",                name );            mas_perror();            quit(11);    }    if ( *status_p != MAS_AVAILABLE )     {        (void)fprintf(stderr,"unexpected status word for %s\n"                                "- expected %u got %u\n",                name, MAS_AVAILABLE, *status_p );        quit(11);    }    if (!(units_p = mas_get_met_units( md, id )))     {            (void)fprintf(stderr,"mas_get_met_units of %s failed\n",                name );            mas_perror();            quit(11);    }    if (units != *units_p )     {            (void)fprintf(stderr,"unexpected units for %s\n"                                    "- expected %u got %u\n",                name, units, *units_p );            quit(11);    }    if (!(mettype_p = mas_get_met_type( md, id )))     {            (void)fprintf(stderr,"mas_get_met_type of %s failed\n",                name );            mas_perror();            quit(11);    }    if (mettype != *mettype_p )     {            (void)fprintf(stderr,"unexpected metric type for %s\n"                                    "- expected %u got %u\n",                name, mettype , *mettype_p );            quit(11);    }    if (!(objsz_p = mas_get_met_objsz( md, id )))     {            (void)fprintf(stderr,"mas_get_met_objsz of %s failed\n", name );            mas_perror();            quit(11);    }    if (*objsz_p != metsz )     {            (void)fprintf(stderr,"unexpected object size for %s\n"                                    "- expected %u got %u\n",                name, metsz, *objsz_p );            quit(11);    }    if (!(nobj_p = mas_get_met_nobj( md, id )))     {            (void)fprintf(stderr,"mas_get_met_nobj of %s failed\n", name );            mas_perror();            quit(11);    }    if (nobj != *nobj_p )     {        (void)fprintf(stderr,"unexpected number of objects for %s\n"                                    "- expected %u got %u\n",                name, nobj, *nobj_p );         quit(11);    }    /* get the number of instances that libmas thinks it knows about  */    if (!(nlocs_p = mas_get_met_nlocs( md, id )))     {        (void)fprintf(stderr,"mas_get_met_nlocs of %s failed\n",  name );        mas_perror();        quit(11);    }    if (nlocs != *nlocs_p )    {        (void)fprintf(stderr,"unexpected number of instances for %s"                        " - expected %u got %u\n",                name, nlocs, *nlocs_p );        quit(11);    }    /*	get the resource list for the metric */    if (!(resource_p = mas_get_met_resources( md, id )))    {        (void)fprintf(stderr,"mas_get_met_resources of %s failed\n", name );        mas_perror();        quit(11);    }    if (*resource_p != res_id )    {        (void)fprintf(stderr,"unexpected resource id for %s\n"                                    "- expected %u got %u\n",                name, res_id, *resource_p);        quit(11);    }    /*	get the size of the resource  */    if (!(ressz_p = mas_get_met_objsz( md, (metid_t)(*resource_p) )))    {        (void)fprintf(stderr,"mas_get_met_objsz of resource failed\n");        mas_perror();        quit(11);    }    if (*ressz_p != ressz )    {        (void)fprintf(stderr,"unexpected resource size for %s\n"                                    "- expected %u got %u\n",                name, ressz, *ressz_p );        quit(11);    }/* *	get the address of the resource */    if (!(resval_p = (uint32 *)mas_get_met( md, *resource_p, 0 )))    {            (void)fprintf(stderr,"mas_get_met of resource failed\n");            mas_perror();            quit(11);    }    if (ressz == sizeof( short ) )    {        if( (uint32)(*(short *)resval_p) != nlocs )        {            (void)fprintf(stderr,"unexpected resource value for %s\n"                                    "- expected %u got %u\n",                        name, nlocs, (uint32)(*(short *)resval_p) );            quit(11);        }    }    else    { /* assume size of uint32 */        if (*resval_p != nlocs )        {            (void)fprintf(stderr,"unexpected resource value for %s\n"                                    "- expected %u got %u\n",                        name, nlocs, *resval_p );            quit(11);        }    }    return;}

⌨️ 快捷键说明

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