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