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