Home | History | Annotate | Line # | Download | only in systat
ps.c revision 1.12
      1 /*      $NetBSD: ps.c,v 1.12 1999/12/22 14:46:15 kleink 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.12 1999/12/22 14:46:15 kleink 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 void
     87 labelps ()
     88 {
     89 	mvwaddstr(wnd, 0, 0, "USER      PID %CPU %MEM    VSZ   RSS TT  STAT STARTED       TIME COMMAND");
     90 }
     91 
     92 void
     93 showps ()
     94 {
     95 	int i, k, y, vsz, rss;
     96 	const char *user, *comm, *state, *tty, *start, *time;
     97 	pid_t pid;
     98 	double pctcpu, pctmem;
     99 	struct  eproc *ep;
    100 
    101 	now = 0;	/* force start2str to reget current time */
    102 
    103 	qsort(pt, nproc + 1, sizeof (struct p_times), compare_pctcpu_noidle);
    104 
    105 	y = 1;
    106 	i = nproc + 1;
    107 	if (i > getmaxy(wnd)-2)
    108 		i = getmaxy(wnd)-1;
    109 	for (k = 0; i > 0 ; k++) {
    110 		if (pt[k].pt_kp == NULL) /* We're all the way down to the imaginary idle proc */
    111 			break;
    112 
    113 		ep = &pt[k].pt_kp->kp_eproc;
    114 		if (showuser != SHOWUSER_ANY && ep->e_ucred.cr_uid != showuser)
    115 			continue;
    116 		user = user_from_uid(ep->e_ucred.cr_uid, 0);
    117 		pid = pt[k].pt_kp->kp_proc.p_pid;
    118 		pctcpu = 100.0 * pt[k].pt_pctcpu;
    119 		pctmem = pmem2float(pt[k].pt_kp);
    120 		vsz = vsz2int(pt[k].pt_kp);
    121 		rss = rss2int(pt[k].pt_kp);
    122 		tty = tty2str(pt[k].pt_kp);
    123 		state = state2str(pt[k].pt_kp);
    124 		start = start2str(pt[k].pt_kp);
    125 		time = time2str(pt[k].pt_kp);
    126 		comm = comm2str(pt[k].pt_kp);
    127 		/*comm = pt[k].pt_kp->kp_proc.p_comm; */
    128 
    129 		wmove(wnd, y, 0);
    130 		wclrtoeol(wnd);
    131 		mvwprintw(wnd, y++, 0,
    132 		    "%-8.8s%5d %4.1f %4.1f %6d %5d %-3s %-4s %7s %10.10s %s",
    133 		    user, pid, pctcpu, pctmem, vsz, rss, tty, state, start, time, comm);
    134 		i--;
    135 	}
    136 	wmove(wnd, y, 0);
    137 	wclrtobot(wnd);
    138 }
    139 
    140 int
    141 compare_pctcpu_noidle (a, b)
    142 	const void *a, *b;
    143 {
    144 	if (((struct p_times *) a)->pt_kp == NULL)
    145 		return 1;
    146 
    147 	if (((struct p_times *) b)->pt_kp == NULL)
    148 	 	return -1;
    149 
    150 	return (((struct p_times *) a)->pt_pctcpu >
    151 		((struct p_times *) b)->pt_pctcpu)? -1: 1;
    152 }
    153 
    154 /* from here down adapted from .../src/usr.bin/ps/print.c .  Any mistakes are my own, however. */
    155 char *
    156 state2str(kp)
    157 	struct kinfo_proc *kp;
    158 {
    159 	struct proc *p;
    160 	struct eproc *e;
    161 	int flag;
    162 	char *cp;
    163 	char buf[5];
    164 	static char statestr[4];
    165 
    166 	p = &(kp->kp_proc);
    167 	e = &(kp->kp_eproc);
    168 
    169 	flag = p->p_flag;
    170 	cp = buf;
    171 
    172 	switch (p->p_stat) {
    173 	case SSTOP:
    174 		*cp = 'T';
    175 		break;
    176 
    177 	case SSLEEP:
    178 		if (flag & P_SINTR)     /* interuptable (long) */
    179 			*cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
    180 		else
    181 			*cp = 'D';
    182 		break;
    183 
    184 	case SRUN:
    185 	case SIDL:
    186 		*cp = 'R';
    187 		break;
    188 
    189 	case SZOMB:
    190 	case SDEAD:
    191 		*cp = 'Z';
    192 		break;
    193 
    194 	default:
    195 		*cp = '?';
    196 	}
    197 	cp++;
    198 	if (flag & P_INMEM) {
    199 	} else
    200 		*cp++ = 'W';
    201 	if (p->p_nice < NZERO)
    202 		*cp++ = '<';
    203 	else if (p->p_nice > NZERO)
    204 		*cp++ = 'N';
    205 	if (flag & P_TRACED)
    206 		*cp++ = 'X';
    207 	if (flag & P_WEXIT && P_ZOMBIE(p) == 0)
    208 		*cp++ = 'E';
    209 	if (flag & P_PPWAIT)
    210 		*cp++ = 'V';
    211 	if ((flag & P_SYSTEM) || p->p_holdcnt)
    212 		*cp++ = 'L';
    213 	if (e->e_flag & EPROC_SLEADER)
    214 		*cp++ = 's';
    215 	if ((flag & P_CONTROLT) && e->e_pgid == e->e_tpgid)
    216 		*cp++ = '+';
    217 	*cp = '\0';
    218 	snprintf(statestr, sizeof(statestr), "%-s",  buf);
    219 
    220 	return statestr;
    221 }
    222 
    223 char *
    224 tty2str(kp)
    225 	struct kinfo_proc *kp;
    226 {
    227 	static char ttystr[4];
    228 	char *ttyname;
    229 	struct eproc *e;
    230 
    231 	e = &(kp->kp_eproc);
    232 
    233 	if (e->e_tdev == NODEV || (ttyname = devname(e->e_tdev, S_IFCHR)) == NULL)
    234 		strcpy(ttystr, "??");
    235 	else {
    236 		if (strncmp(ttyname, "tty", 3) == 0 ||
    237 		    strncmp(ttyname, "dty", 3) == 0)
    238 			ttyname += 3;
    239 		snprintf(ttystr, sizeof(ttystr), "%s%c", ttyname, e->e_flag & EPROC_CTTY ? ' ' : '-');
    240 	}
    241 
    242 	return ttystr;
    243 }
    244 
    245 #define pgtok(a)	(((a)*getpagesize())/1024)
    246 
    247 int
    248 vsz2int(kp)
    249 	struct kinfo_proc *kp;
    250 {
    251 	struct eproc *e;
    252 	int     i;
    253 
    254 	e = &(kp->kp_eproc);
    255 	i = pgtok(e->e_vm.vm_dsize + e->e_vm.vm_ssize + e->e_vm.vm_tsize);
    256 
    257 	return ((i < 0) ? 0 : i);
    258 }
    259 
    260 int
    261 rss2int(kp)
    262 	struct kinfo_proc *kp;
    263 {
    264 	struct eproc *e;
    265 	int	i;
    266 
    267 	e = &(kp->kp_eproc);
    268 	i = pgtok(e->e_vm.vm_rssize);
    269 
    270 	/* XXX don't have info about shared */
    271 	return ((i < 0) ? 0 : i);
    272 }
    273 
    274 char *
    275 comm2str(kp)
    276 	struct kinfo_proc *kp;
    277 {
    278 	char **argv, **pt;
    279 	static char commstr[41];
    280 	struct proc *p;
    281 
    282 	p = &(kp->kp_proc);
    283 	commstr[0]='\0';
    284 
    285 	argv = kvm_getargv(kd, kp, 40);
    286 	if ((pt = argv) != NULL) {
    287 		while (*pt) {
    288 			strcat(commstr, *pt);
    289 			pt++;
    290 			strcat(commstr, " ");
    291 		}
    292 	} else {
    293 		commstr[0] = '(';
    294 		commstr[1] = '\0';
    295 		strncat(commstr, p->p_comm, sizeof(commstr) - 1);
    296 		strcat(commstr, ")");
    297 	}
    298 
    299 	return commstr;
    300 }
    301 
    302 double
    303 pmem2float(kp)
    304 	struct kinfo_proc *kp;
    305 {
    306 	struct proc *p;
    307 	struct eproc *e;
    308 	double fracmem;
    309 	int szptudot;
    310 
    311 	p = &(kp->kp_proc);
    312 	e = &(kp->kp_eproc);
    313 
    314 	if ((p->p_flag & P_INMEM) == 0)
    315 	        return (0.0);
    316 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
    317 	szptudot = USPACE/getpagesize();
    318 	/* XXX don't have info about shared */
    319 	fracmem = ((double)e->e_vm.vm_rssize + szptudot)/mempages;
    320 	return (fracmem >= 0) ? 100.0 * fracmem : 0;
    321 }
    322 
    323 char *
    324 start2str(kp)
    325 	struct kinfo_proc *kp;
    326 {
    327 	struct proc *p;
    328 	struct pstats pstats;
    329 	struct timeval u_start;
    330 	struct tm *tp;
    331 	time_t startt;
    332 	static char startstr[10];
    333 
    334 	p = &(kp->kp_proc);
    335 
    336 	kvm_read(kd, (u_long)&(p->p_addr->u_stats), (char *)&pstats, sizeof(pstats));
    337 	u_start = pstats.p_start;
    338 
    339 	startt = u_start.tv_sec;
    340 	tp = localtime(&startt);
    341 	if (now == 0)
    342 	        time(&now);
    343 	if (now - u_start.tv_sec < 24 * SECSPERHOUR) {
    344 		/* I *hate* SCCS... */
    345 	        static char fmt[] = __CONCAT("%l:%", "M%p");
    346 	        strftime(startstr, sizeof(startstr) - 1, fmt, tp);
    347 	} else if (now - u_start.tv_sec < 7 * SECSPERDAY) {
    348 	        /* I *hate* SCCS... */
    349 	        static char fmt[] = __CONCAT("%a%", "I%p");
    350 	        strftime(startstr, sizeof(startstr) - 1, fmt, tp);
    351 	} else
    352 	        strftime(startstr, sizeof(startstr) - 1, "%e%b%y", tp);
    353 
    354 	return startstr;
    355 }
    356 
    357 char *
    358 time2str(kp)
    359 	struct kinfo_proc *kp;
    360 {
    361 	long secs;
    362 	long psecs;     /* "parts" of a second. first micro, then centi */
    363 	static char timestr[10];
    364 	struct proc *p;
    365 
    366 	p = &(kp->kp_proc);
    367 
    368 	if (P_ZOMBIE(p)) {
    369 	        secs = 0;
    370 	        psecs = 0;
    371 	} else {
    372 	        /*
    373 	         * This counts time spent handling interrupts.  We could
    374 	         * fix this, but it is not 100% trivial (and interrupt
    375 	         * time fractions only work on the sparc anyway).       XXX
    376 	         */
    377 	        secs = p->p_rtime.tv_sec;
    378 	        psecs = p->p_rtime.tv_usec;
    379 	        /* if (sumrusage) {
    380 	                secs += k->ki_u.u_cru.ru_utime.tv_sec +
    381 	                        k->ki_u.u_cru.ru_stime.tv_sec;
    382 	                psecs += k->ki_u.u_cru.ru_utime.tv_usec +
    383 	                        k->ki_u.u_cru.ru_stime.tv_usec;
    384 	        } */
    385 	        /*
    386 	         * round and scale to 100's
    387 	         */
    388 	        psecs = (psecs + 5000) / 10000;
    389 	        secs += psecs / 100;
    390 	        psecs = psecs % 100;
    391 	}
    392 	snprintf(timestr, sizeof(timestr), "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
    393 
    394 	return timestr;
    395 }
    396 
    397 void
    398 ps_user(args)
    399 	char *args;
    400 {
    401 	uid_t uid;
    402 
    403 	if (args == NULL)
    404 		args = "";
    405 	if (strcmp(args, "+") == 0) {
    406 		uid = SHOWUSER_ANY;
    407 	} else if (uid_from_user(args, &uid) != 0) {
    408 		error("%s: unknown user", args);
    409 		return;
    410 	}
    411 
    412 	showuser = uid;
    413 	display(0);
    414 }
    415