Home | History | Annotate | Line # | Download | only in systat
ps.c revision 1.5
      1 /*      $NetBSD: ps.c,v 1.5 1999/07/23 08:56:13 veego 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.5 1999/07/23 08:56:13 veego 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 			ttyname += 3;
    230 		sprintf(ttystr, "%s%c", ttyname, e->e_flag & EPROC_CTTY ? ' ' : '-');
    231 	}
    232 
    233 	return ttystr;
    234 }
    235 
    236 #define pgtok(a)	(((a)*getpagesize())/1024)
    237 
    238 int
    239 vsz2int(kp)
    240 	struct kinfo_proc *kp;
    241 {
    242 	struct eproc *e;
    243 	int     i;
    244 
    245 	e = &(kp->kp_eproc);
    246 	i = pgtok(e->e_vm.vm_dsize + e->e_vm.vm_ssize + e->e_vm.vm_tsize);
    247 
    248 	return ((i < 0) ? 0 : i);
    249 }
    250 
    251 int
    252 rss2int(kp)
    253 	struct kinfo_proc *kp;
    254 {
    255 	struct eproc *e;
    256 	int	i;
    257 
    258 	e = &(kp->kp_eproc);
    259 	i = pgtok(e->e_vm.vm_rssize);
    260 
    261 	/* XXX don't have info about shared */
    262 	return ((i < 0) ? 0 : i);
    263 }
    264 
    265 char *
    266 comm2str(kp)
    267 	struct kinfo_proc *kp;
    268 {
    269 	char **argv, **pt;
    270 	static char commstr[41];
    271 	struct proc *p;
    272 
    273 	p = &(kp->kp_proc);
    274 	commstr[0]='\0';
    275 
    276 	argv = kvm_getargv(kd, kp, 40);
    277 	if ((pt = argv) != NULL) {
    278 		while (*pt) {
    279 			strcat(commstr, *pt);
    280 			pt++;
    281 			strcat(commstr, " ");
    282 		}
    283 	} else {
    284 		commstr[0] = '(';
    285 		commstr[1] = '\0';
    286 		strncat(commstr, p->p_comm, sizeof(commstr) - 1);
    287 		strcat(commstr, ")");
    288 	}
    289 
    290 	return commstr;
    291 }
    292 
    293 double
    294 pmem2float(kp)
    295 	struct kinfo_proc *kp;
    296 {
    297 	struct proc *p;
    298 	struct eproc *e;
    299 	double fracmem;
    300 	int szptudot;
    301 
    302 	p = &(kp->kp_proc);
    303 	e = &(kp->kp_eproc);
    304 
    305 	if ((p->p_flag & P_INMEM) == 0)
    306 	        return (0.0);
    307 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
    308 	szptudot = USPACE/getpagesize();
    309 	/* XXX don't have info about shared */
    310 	fracmem = ((double)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
    311 	return (fracmem >= 0) ? 100.0 * fracmem : 0;
    312 }
    313 
    314 char *
    315 start2str(kp)
    316 	struct kinfo_proc *kp;
    317 {
    318 	struct proc *p;
    319 	struct pstats pstats;
    320 	struct timeval u_start;
    321 	struct tm *tp;
    322 	time_t startt;
    323 	static char startstr[10];
    324 
    325 	p = &(kp->kp_proc);
    326 
    327 	kvm_read(kd, (u_long)&(p->p_addr->u_stats), (char *)&pstats, sizeof(pstats));
    328 	u_start = pstats.p_start;
    329 
    330 	startt = u_start.tv_sec;
    331 	tp = localtime(&startt);
    332 	if (now == 0)
    333 	        time(&now);
    334 	if (now - u_start.tv_sec < 24 * SECSPERHOUR) {
    335 		/* I *hate* SCCS... */
    336 	        static char fmt[] = __CONCAT("%l:%", "M%p");
    337 	        strftime(startstr, sizeof(startstr) - 1, fmt, tp);
    338 	} else if (now - u_start.tv_sec < 7 * SECSPERDAY) {
    339 	        /* I *hate* SCCS... */
    340 	        static char fmt[] = __CONCAT("%a%", "I%p");
    341 	        strftime(startstr, sizeof(startstr) - 1, fmt, tp);
    342 	} else
    343 	        strftime(startstr, sizeof(startstr) - 1, "%e%b%y", tp);
    344 
    345 	return startstr;
    346 }
    347 
    348 char *
    349 time2str(kp)
    350 	struct kinfo_proc *kp;
    351 {
    352 	long secs;
    353 	long psecs;     /* "parts" of a second. first micro, then centi */
    354 	static char timestr[10];
    355 	struct proc *p;
    356 
    357 	p = &(kp->kp_proc);
    358 
    359 	if (P_ZOMBIE(p)) {
    360 	        secs = 0;
    361 	        psecs = 0;
    362 	} else {
    363 	        /*
    364 	         * This counts time spent handling interrupts.  We could
    365 	         * fix this, but it is not 100% trivial (and interrupt
    366 	         * time fractions only work on the sparc anyway).       XXX
    367 	         */
    368 	        secs = p->p_rtime.tv_sec;
    369 	        psecs = p->p_rtime.tv_usec;
    370 	        /* if (sumrusage) {
    371 	                secs += k->ki_u.u_cru.ru_utime.tv_sec +
    372 	                        k->ki_u.u_cru.ru_stime.tv_sec;
    373 	                psecs += k->ki_u.u_cru.ru_utime.tv_usec +
    374 	                        k->ki_u.u_cru.ru_stime.tv_usec;
    375 	        } */
    376 	        /*
    377 	         * round and scale to 100's
    378 	         */
    379 	        psecs = (psecs + 5000) / 10000;
    380 	        secs += psecs / 100;
    381 	        psecs = psecs % 100;
    382 	}
    383 	snprintf(timestr, sizeof(timestr), "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
    384 
    385 	return timestr;
    386 }
    387