ps.c revision 1.11 1 /* $NetBSD: ps.c,v 1.11 1999/12/20 19:31:47 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 * 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.11 1999/12/20 19:31:47 jwise 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 void
84 labelps ()
85 {
86 mvwaddstr(wnd, 0, 0, "USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND");
87 }
88
89 void
90 showps ()
91 {
92 int i, k, y, vsz, rss;
93 const char *user, *comm, *state, *tty, *start, *time;
94 pid_t pid;
95 double pctcpu, pctmem;
96 struct eproc *ep;
97
98 now = 0; /* force start2str to reget current time */
99
100 qsort(pt, nproc + 1, sizeof (struct p_times), compare_pctcpu_noidle);
101
102 y = 1;
103 i = nproc + 1;
104 if (i > getmaxy(wnd)-2)
105 i = getmaxy(wnd)-1;
106 for (k = 0; i > 0 ; i--, y++, k++) {
107 if (pt[k].pt_kp == NULL) /* We're all the way down to the imaginary idle proc */
108 return;
109
110 ep = &pt[k].pt_kp->kp_eproc;
111 user = user_from_uid(ep->e_ucred.cr_uid, 0);
112 pid = pt[k].pt_kp->kp_proc.p_pid;
113 pctcpu = 100.0 * pt[k].pt_pctcpu;
114 pctmem = pmem2float(pt[k].pt_kp);
115 vsz = vsz2int(pt[k].pt_kp);
116 rss = rss2int(pt[k].pt_kp);
117 tty = tty2str(pt[k].pt_kp);
118 state = state2str(pt[k].pt_kp);
119 start = start2str(pt[k].pt_kp);
120 time = time2str(pt[k].pt_kp);
121 comm = comm2str(pt[k].pt_kp);
122 /*comm = pt[k].pt_kp->kp_proc.p_comm; */
123
124 wmove(wnd, y, 0);
125 wclrtoeol(wnd);
126 mvwprintw(wnd, y, 0, "%-8.8s%5d %4.1f %4.1f %6d %5d %-3s %-4s %7s %10.10s %s",
127 user, pid, pctcpu, pctmem, vsz, rss, tty, state, start, time, comm);
128 }
129 }
130
131 int
132 compare_pctcpu_noidle (a, b)
133 const void *a, *b;
134 {
135 if (((struct p_times *) a)->pt_kp == NULL)
136 return 1;
137
138 if (((struct p_times *) b)->pt_kp == NULL)
139 return -1;
140
141 return (((struct p_times *) a)->pt_pctcpu >
142 ((struct p_times *) b)->pt_pctcpu)? -1: 1;
143 }
144
145 /* from here down adapted from .../src/usr.bin/ps/print.c . Any mistakes are my own, however. */
146 char *
147 state2str(kp)
148 struct kinfo_proc *kp;
149 {
150 struct proc *p;
151 struct eproc *e;
152 int flag;
153 char *cp;
154 char buf[5];
155 static char statestr[4];
156
157 p = &(kp->kp_proc);
158 e = &(kp->kp_eproc);
159
160 flag = p->p_flag;
161 cp = buf;
162
163 switch (p->p_stat) {
164 case SSTOP:
165 *cp = 'T';
166 break;
167
168 case SSLEEP:
169 if (flag & P_SINTR) /* interuptable (long) */
170 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
171 else
172 *cp = 'D';
173 break;
174
175 case SRUN:
176 case SIDL:
177 *cp = 'R';
178 break;
179
180 case SZOMB:
181 case SDEAD:
182 *cp = 'Z';
183 break;
184
185 default:
186 *cp = '?';
187 }
188 cp++;
189 if (flag & P_INMEM) {
190 } else
191 *cp++ = 'W';
192 if (p->p_nice < NZERO)
193 *cp++ = '<';
194 else if (p->p_nice > NZERO)
195 *cp++ = 'N';
196 if (flag & P_TRACED)
197 *cp++ = 'X';
198 if (flag & P_WEXIT && P_ZOMBIE(p) == 0)
199 *cp++ = 'E';
200 if (flag & P_PPWAIT)
201 *cp++ = 'V';
202 if ((flag & P_SYSTEM) || p->p_holdcnt)
203 *cp++ = 'L';
204 if (e->e_flag & EPROC_SLEADER)
205 *cp++ = 's';
206 if ((flag & P_CONTROLT) && e->e_pgid == e->e_tpgid)
207 *cp++ = '+';
208 *cp = '\0';
209 snprintf(statestr, sizeof(statestr), "%-s", buf);
210
211 return statestr;
212 }
213
214 char *
215 tty2str(kp)
216 struct kinfo_proc *kp;
217 {
218 static char ttystr[4];
219 char *ttyname;
220 struct eproc *e;
221
222 e = &(kp->kp_eproc);
223
224 if (e->e_tdev == NODEV || (ttyname = devname(e->e_tdev, S_IFCHR)) == NULL)
225 strcpy(ttystr, "??");
226 else {
227 if (strncmp(ttyname, "tty", 3) == 0 ||
228 strncmp(ttyname, "dty", 3) == 0)
229 ttyname += 3;
230 snprintf(ttystr, sizeof(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)/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