2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
38 static const char rcsid[] =
42 #include <sys/param.h>
44 #include <sys/resource.h>
48 #include <sys/ucred.h>
50 #include <sys/sysctl.h>
73 for (vent = vhead; vent; vent = vent->next) {
75 if (v->flag & LJUST) {
76 if (vent->next == NULL) /* last one */
77 (void)printf("%s", v->header);
79 (void)printf("%-*s", v->width, v->header);
81 (void)printf("%*s", v->width, v->header);
82 if (vent->next != NULL)
95 char *cp, *vis_env, *vis_args;
100 if (ve->next == NULL) /* last field, don't pad */
101 (void)printf("%s", k->ki_p->ki_comm);
103 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
107 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
109 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
111 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
113 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
117 if (ve->next == NULL) {
119 if (termwidth == UNLIMITED) {
121 (void)printf("%s ", vis_env);
122 (void)printf("%s", vis_args);
124 left = termwidth - (totwidth - v->width);
125 if (left < 1) /* already wrapped, just use std width */
127 if ((cp = vis_env) != NULL) {
128 while (--left >= 0 && *cp)
129 (void)putchar(*cp++);
133 for (cp = vis_args; --left >= 0 && *cp != '\0';)
134 (void)putchar(*cp++);
138 (void)printf("%-*.*s", v->width, v->width, vis_args);
152 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
164 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
172 int flag, sflag, tdflags;
178 flag = k->ki_p->ki_flag;
179 sflag = k->ki_p->ki_sflag;
180 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
183 switch (k->ki_p->ki_stat) {
190 if (tdflags & TDF_SINTR) /* interruptable (long) */
191 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
217 if (!(sflag & PS_INMEM))
219 if (k->ki_p->ki_nice < NZERO)
221 else if (k->ki_p->ki_nice > NZERO)
225 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
229 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
231 if (k->ki_p->ki_kiflag & KI_SLEADER)
233 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
238 (void)printf("%-*s", v->width, buf);
249 (void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
261 (int)v->width, user_from_uid(k->ki_p->ki_uid, 0));
268 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
280 (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0));
287 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
300 dev = k->ki_p->ki_tdev;
302 (void)printf("%*s", v->width, "??");
304 (void)snprintf(buff, sizeof(buff),
305 "%d/%d", major(dev), minor(dev));
306 (void)printf("%*s", v->width, buff);
320 dev = k->ki_p->ki_tdev;
321 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
322 (void)printf("%*s ", v->width-1, "??");
324 if (strncmp(ttname, "tty", 3) == 0 ||
325 strncmp(ttname, "cua", 3) == 0)
327 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
328 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
342 dev = k->ki_p->ki_tdev;
343 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
344 (void)printf("%-*s", v->width, "??");
346 (void)printf("%-*s", v->width, ttname);
359 static int use_ampm = -1;
363 (void)printf("%-*s", v->width, "-");
368 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
370 then = k->ki_p->ki_start.tv_sec;
371 tp = localtime(&then);
374 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
375 (void)strftime(buf, sizeof(buf) - 1,
376 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
377 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
378 (void)strftime(buf, sizeof(buf) - 1,
379 use_ampm ? "%a%I%p" : "%a%H ", tp);
381 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
382 (void)printf("%-*s", v->width, buf);
396 (void)printf("%-*s", v->width, "-");
399 then = k->ki_p->ki_start.tv_sec;
400 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
401 (void)printf("%-*s", v->width, buf);
412 if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
413 if (k->ki_p->ki_mtxname[0] != 0)
414 (void)printf("%-*.*s", v->width, v->width,
415 k->ki_p->ki_mtxname);
417 (void)printf("%-*s", v->width, "???");
419 (void)printf("%-*s", v->width, "-");
430 if (k->ki_p->ki_wchan) {
431 if (k->ki_p->ki_wmesg[0] != 0)
432 (void)printf("%-*.*s", v->width, v->width,
435 (void)printf("%-*lx", v->width,
436 (long)k->ki_p->ki_wchan);
438 (void)printf("%-*s", v->width, "-");
442 #define pgtok(a) (((a)*getpagesize())/1024)
453 (void)printf("%*d", v->width,
454 (k->ki_p->ki_size/1024));
465 /* XXX don't have info about shared */
466 (void)printf("%*lu", v->width,
467 (u_long)pgtok(k->ki_p->ki_rssize));
471 p_rssize(k, ve) /* doesn't account for text */
478 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_rssize));
488 long psecs; /* "parts" of a second. first micro, then centi */
490 static char decimal_point = 0;
493 decimal_point = localeconv()->decimal_point[0];
495 if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
500 * This counts time spent handling interrupts. We could
501 * fix this, but it is not 100% trivial (and interrupt
502 * time fractions only work on the sparc anyway). XXX
504 secs = k->ki_p->ki_runtime / 1000000;
505 psecs = k->ki_p->ki_runtime % 1000000;
507 secs += k->ki_p->ki_childtime.tv_sec;
508 psecs += k->ki_p->ki_childtime.tv_usec;
511 * round and scale to 100's
513 psecs = (psecs + 5000) / 10000;
517 (void)snprintf(obuff, sizeof(obuff),
518 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
519 (void)printf("%*s", v->width, obuff);
533 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
535 /* XXX - I don't like this */
536 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
539 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
540 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
541 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
552 (void)printf("%*.1f", v->width, getpcpu(k));
567 if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
569 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
570 /* XXX don't have info about shared */
571 fracmem = ((float)k->ki_p->ki_rssize)/mempages;
572 return (100.0 * fracmem);
583 (void)printf("%*.1f", v->width, getpmem(k));
594 (void)printf("%*ld", v->width,
595 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
607 (void)printf("%*s", v->width, "-");
618 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize));
627 struct priority *pri;
629 unsigned class, level;
632 pri = (struct priority *) ((char *)k + v->off);
633 class = pri->pri_class;
634 level = pri->pri_level;
637 snprintf(str, sizeof(str), "real:%u", level);
640 strncpy(str, "normal", sizeof(str));
643 snprintf(str, sizeof(str), "idle:%u", level);
646 snprintf(str, sizeof(str), "%u:%u", class, level);
649 str[sizeof(str) - 1] = '\0';
650 (void)printf("%*s", v->width, str);
654 * Generic output routines. Print fields from various prototype
662 static char ofmt[32] = "%";
670 while ((*cp++ = *fcp++));
674 (void)printf(ofmt, v->width, *(char *)bp);
677 (void)printf(ofmt, v->width, *(u_char *)bp);
680 (void)printf(ofmt, v->width, *(short *)bp);
683 (void)printf(ofmt, v->width, *(u_short *)bp);
686 (void)printf(ofmt, v->width, *(int *)bp);
689 (void)printf(ofmt, v->width, *(u_int *)bp);
692 (void)printf(ofmt, v->width, *(long *)bp);
695 (void)printf(ofmt, v->width, *(u_long *)bp);
698 (void)printf(ofmt, v->width, *(u_long *)bp);
701 errx(1, "unknown type %d", v->type);
713 printval((char *)((char *)k->ki_p + v->off), v);
725 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
727 (void)printf("%*s", v->width, "-");
738 (void)printf("%-*d", (int)v->width, get_lattr(k->ki_p->ki_pid));