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