Home | History | Annotate | Line # | Download | only in systat
ps.c revision 1.9
      1 /*      $NetBSD: ps.c,v 1.9 1999/12/16 04:41:49 jwise Exp $  */
      2 
      3 /*-
      4  * Copyright (c) 1999
      5  *      The NetBSD Foundation, Inc.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by the University of
     18  *      California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * XXX Notes XXX
     38  * showps -- print data needed at each refresh
     39  * fetchps -- get data used by mode (done at each refresh)
     40  * labelps -- print labels (ie info not needing refreshing)
     41  * initps -- prepare once-only data structures for mode
     42  * openps -- prepare per-run information for mode, return window
     43  * closeps -- close mode to prepare to switch modes
     44  * cmdps -- optional, handle commands
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 #ifndef lint
     49 __RCSID("$NetBSD: ps.c,v 1.9 1999/12/16 04:41:49 jwise Exp $");
     50 #endif /* not lint */
     51 
     52 #include <sys/param.h>
     53 #include <sys/dkstat.h>
     54 #include <sys/dir.h>
     55 #include <sys/time.h>
     56 #include <sys/proc.h>
     57 #include <sys/sysctl.h>
     58 #include <sys/user.h>
     59 #include <curses.h>
     60 #include <math.h>
     61 #include <nlist.h>
     62 #include <pwd.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <tzfile.h>
     66 #include <unistd.h>
     67 
     68 #include "extern.h"
     69 #include "systat.h"
     70 #include "ps.h"
     71 
     72 int compare_pctcpu_noidle __P((const void *, const void *));
     73 char *state2str __P((struct kinfo_proc *));
     74 char *tty2str __P((struct kinfo_proc *));
     75 int rss2int __P((struct kinfo_proc *));
     76 int vsz2int __P((struct kinfo_proc *));
     77 char *comm2str __P((struct kinfo_proc *));
     78 double pmem2float __P((struct kinfo_proc *));
     79 char *start2str __P((struct kinfo_proc *));
     80 char *time2str __P((struct kinfo_proc *));
     81 
     82 static time_t now;
     83 
     84 void
     85 labelps ()
     86 {
     87 	mvwaddstr(wnd, 0, 0, "USER      PID %CPU %MEM    VSZ   RSS TT  STAT STARTED       TIME COMMAND");
     88 }
     89 
     90 void
     91 showps ()
     92 {
     93 	int i, k, y, vsz, rss;
     94 	const char *user, *comm, *state, *tty, *start, *time;
     95 	pid_t pid;
     96 	double pctcpu, pctmem;
     97 	struct  eproc *ep;
     98 
     99 	now = 0;	/* force start2str to reget current time */
    100 
    101 	qsort(pt, nproc + 1, sizeof (struct p_times), compare_pctcpu_noidle);
    102 
    103 	y = 1;
    104 	i = nproc + 1;
    105 	if (i > getmaxy(wnd)-2)
    106 		i = getmaxy(wnd)-1;
    107 	for (k = 0; i > 0 ; i--, y++, k++) {
    108 		if (pt[k].pt_kp == NULL) /* We're all the way down to the imaginary idle proc */
    109 			return;
    110 
    111 		ep = &pt[k].pt_kp->kp_eproc;
    112 		user = user_from_uid(ep->e_ucred.cr_uid, 0);
    113 		pid = pt[k].pt_kp->kp_proc.p_pid;
    114 		pctcpu = 100.0 * pt[k].pt_pctcpu;
    115 		pctmem = pmem2float(pt[k].pt_kp);
    116 		vsz = vsz2int(pt[k].pt_kp);
    117 		rss = rss2int(pt[k].pt_kp);
    118 		tty = tty2str(pt[k].pt_kp);
    119 		state = state2str(pt[k].pt_kp);
    120 		start = start2str(pt[k].pt_kp);
    121 		time = time2str(pt[k].pt_kp);
    122 		comm = comm2str(pt[k].pt_kp);
    123 		/*comm = pt[k].pt_kp->kp_proc.p_comm; */
    124 
    125 		wmove(wnd, y, 0);
    126 		wclrtoeol(wnd);
    127 		mvwprintw(wnd, y, 0, "%-8.8s%5d %4.1f %4.1f %6d %5d %-3s %-4s %7s %10.10s %s",
    128 			user, pid, pctcpu, pctmem, vsz, rss, tty, state, start, time, comm);
    129 	}
    130 }
    131 
    132 int
    133 compare_pctcpu_noidle (a, b)
    134 	const void *a, *b;
    135 {
    136 	if (((struct p_times *) a)->pt_kp == NULL)
    137 		return 1;
    138 
    139 	if (((struct p_times *) b)->pt_kp == NULL)
    140 	 	return -1;
    141 
    142 	return (((struct p_times *) a)->pt_pctcpu >
    143 		((struct p_times *) b)->pt_pctcpu)? -1: 1;
    144 }
    145 
    146 /* from here down adapted from .../src/usr.bin/ps/print.c .  Any mistakes are my own, however. */
    147 char *
    148 state2str(kp)
    149 	struct kinfo_proc *kp;
    150 {
    151 	struct proc *p;
    152 	struct eproc *e;
    153 	int flag;
    154 	char *cp;
    155 	char buf[5];
    156 	static char statestr[4];
    157 
    158 	p = &(kp->kp_proc);
    159 	e = &(kp->kp_eproc);
    160 
    161 	flag = p->p_flag;
    162 	cp = buf;
    163 
    164 	switch (p->p_stat) {
    165 	case SSTOP:
    166 		*cp = 'T';
    167 		break;
    168 
    169 	case SSLEEP:
    170 		if (flag & P_SINTR)     /* interuptable (long) */
    171 			*cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
    172 		else
    173 			*cp = 'D';
    174 		break;
    175 
    176 	case SRUN:
    177 	case SIDL:
    178 		*cp = 'R';
    179 		break;
    180 
    181 	case SZOMB:
    182 	case SDEAD:
    183 		*cp = 'Z';
    184 		break;
    185 
    186 	default:
    187 		*cp = '?';
    188 	}
    189 	cp++;
    190 	if (flag & P_INMEM) {
    191 	} else
    192 		*cp++ = 'W';
    193 	if (p->p_nice < NZERO)
    194 		*cp++ = '<';
    195 	else if (p->p_nice > NZERO)
    196 		*cp++ = 'N';
    197 	if (flag & P_TRACED)
    198 		*cp++ = 'X';
    199 	if (flag & P_WEXIT && P_ZOMBIE(p) == 0)
    200 		*cp++ = 'E';
    201 	if (flag & P_PPWAIT)
    202 		*cp++ = 'V';
    203 	if ((flag & P_SYSTEM) || p->p_holdcnt)
    204 		*cp++ = 'L';
    205 	if (e->e_flag & EPROC_SLEADER)
    206 		*cp++ = 's';
    207 	if ((flag & P_CONTROLT) && e->e_pgid == e->e_tpgid)
    208 		*cp++ = '+';
    209 	*cp = '\0';
    210 	sprintf(statestr, "%-s",  buf);
    211 
    212 	return statestr;
    213 }
    214 
    215 char *
    216 tty2str(kp)
    217 	struct kinfo_proc *kp;
    218 {
    219 	static char ttystr[4];
    220 	char *ttyname;
    221 	struct eproc *e;
    222 
    223 	e = &(kp->kp_eproc);
    224 
    225 	if (e->e_tdev == NODEV || (ttyname = devname(e->e_tdev, S_IFCHR)) == NULL)
    226 		strcpy(ttystr, "??");
    227 	else {
    228 		if (strncmp(ttyname, "tty", 3) == 0 ||
    229 		    strncmp(ttyname, "dty", 3) == 0)
    230 			ttyname += 3;
    231 		sprintf(ttystr, "%s%c", ttyname, e->e_flag & EPROC_CTTY ? ' ' : '-');
    232 	}
    233 
    234 	return ttystr;
    235 }
    236 
    237 #define pgtok(a)	(((a)*getpagesize())/1024)
    238 
    239 int
    240 vsz2int(kp)
    241 	struct kinfo_proc *kp;
    242 {
    243 	struct eproc *e;
    244 	int     i;
    245 
    246 	e = &(kp->kp_eproc);
    247 	i = pgtok(e->e_vm.vm_dsize + e->e_vm.vm_ssize + e->e_vm.vm_tsize);
    248 
    249 	return ((i < 0) ? 0 : i);
    250 }
    251 
    252 int
    253 rss2int(kp)
    254 	struct kinfo_proc *kp;
    255 {
    256 	struct eproc *e;
    257 	int	i;
    258 
    259 	e = &(kp->kp_eproc);
    260 	i = pgtok(e->e_vm.vm_rssize);
    261 
    262 	/* XXX don't have info about shared */
    263 	return ((i < 0) ? 0 : i);
    264 }
    265 
    266 char *
    267 comm2str(kp)
    268 	struct kinfo_proc *kp;
    269 {
    270 	char **argv, **pt;
    271 	static char commstr[41];
    272 	struct proc *p;
    273 
    274 	p = &(kp->kp_proc);
    275 	commstr[0]='\0';
    276 
    277 	argv = kvm_getargv(kd, kp, 40);
    278 	if ((pt = argv) != NULL) {
    279 		while (*pt) {
    280 			strcat(commstr, *pt);
    281 			pt++;
    282 			strcat(commstr, " ");
    283 		}
    284 	} else {
    285 		commstr[0] = '(';
    286 		commstr[1] = '\0';
    287 		strncat(commstr, p->p_comm, sizeof(commstr) - 1);
    288 		strcat(commstr, ")");
    289 	}
    290 
    291 	return commstr;
    292 }
    293 
    294 double
    295 pmem2float(kp)
    296 	struct kinfo_proc *kp;
    297 {
    298 	struct proc *p;
    299 	struct eproc *e;
    300 	double fracmem;
    301 	int szptudot;
    302 
    303 	p = &(kp->kp_proc);
    304 	e = &(kp->kp_eproc);
    305 
    306 	if ((p->p_flag & P_INMEM) == 0)
    307 	        return (0.0);
    308 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
    309 	szptudot = USPACE/getpagesize();
    310 	/* XXX don't have info about shared */
    311 	fracmem = ((double)e->e_vm.vm_rssize + szptudot)/mempages;
    312 	return (fracmem >= 0) ? 100.0 * fracmem : 0;
    313 }
    314 
    315 char *
    316 start2str(kp)
    317 	struct kinfo_proc *kp;
    318 {
    319 	struct proc *p;
    320 	struct pstats pstats;
    321 	struct timeval u_start;
    322 	struct tm *tp;
    323 	time_t startt;
    324 	static char startstr[10];
    325 
    326 	p = &(kp->kp_proc);
    327 
    328 	kvm_read(kd, (u_long)&(p->p_addr->u_stats), (char *)&pstats, sizeof(pstats));
    329 	u_start = pstats.p_start;
    330 
    331 	startt = u_start.tv_sec;
    332 	tp = localtime(&startt);
    333 	if (now == 0)
    334 	        time(&now);
    335 	if (now - u_start.tv_sec < 24 * SECSPERHOUR) {
    336 		/* I *hate* SCCS... */
    337 	        static char fmt[] = __CONCAT("%l:%", "M%p");
    338 	        strftime(startstr, sizeof(startstr) - 1, fmt, tp);
    339 	} else if (now - u_start.tv_sec < 7 * SECSPERDAY) {
    340 	        /* I *hate* SCCS... */
    341 	        static char fmt[] = __CONCAT("%a%", "I%p");
    342 	        strftime(startstr, sizeof(startstr) - 1, fmt, tp);
    343 	} else
    344 	        strftime(startstr, sizeof(startstr) - 1, "%e%b%y", tp);
    345 
    346 	return startstr;
    347 }
    348 
    349 char *
    350 time2str(kp)
    351 	struct kinfo_proc *kp;
    352 {
    353 	long secs;
    354 	long psecs;     /* "parts" of a second. first micro, then centi */
    355 	static char timestr[10];
    356 	struct proc *p;
    357 
    358 	p = &(kp->kp_proc);
    359 
    360 	if (P_ZOMBIE(p)) {
    361 	        secs = 0;
    362 	        psecs = 0;
    363 	} else {
    364 	        /*
    365 	         * This counts time spent handling interrupts.  We could
    366 	         * fix this, but it is not 100% trivial (and interrupt
    367 	         * time fractions only work on the sparc anyway).       XXX
    368 	         */
    369 	        secs = p->p_rtime.tv_sec;
    370 	        psecs = p->p_rtime.tv_usec;
    371 	        /* if (sumrusage) {
    372 	                secs += k->ki_u.u_cru.ru_utime.tv_sec +
    373 	                        k->ki_u.u_cru.ru_stime.tv_sec;
    374 	                psecs += k->ki_u.u_cru.ru_utime.tv_usec +
    375 	                        k->ki_u.u_cru.ru_stime.tv_usec;
    376 	        } */
    377 	        /*
    378 	         * round and scale to 100's
    379 	         */
    380 	        psecs = (psecs + 5000) / 10000;
    381 	        secs += psecs / 100;
    382 	        psecs = psecs % 100;
    383 	}
    384 	snprintf(timestr, sizeof(timestr), "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
    385 
    386 	return timestr;
    387 }
    388