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