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