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>
74 for (vent = vhead; vent; vent = vent->next) {
76 if (v->flag & LJUST) {
77 if (vent->next == NULL) /* last one */
78 (void)printf("%s", v->header);
80 (void)printf("%-*s", v->width, v->header);
82 (void)printf("%*s", v->width, v->header);
83 if (vent->next != NULL)
96 char *cp, *vis_env, *vis_args;
101 if (ve->next == NULL) /* last field, don't pad */
102 (void)printf("%s", k->ki_p->ki_comm);
104 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
108 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
110 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
112 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
114 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
118 if (ve->next == NULL) {
120 if (termwidth == UNLIMITED) {
122 (void)printf("%s ", vis_env);
123 (void)printf("%s", vis_args);
125 left = termwidth - (totwidth - v->width);
126 if (left < 1) /* already wrapped, just use std width */
128 if ((cp = vis_env) != NULL) {
129 while (--left >= 0 && *cp)
130 (void)putchar(*cp++);
134 for (cp = vis_args; --left >= 0 && *cp != '\0';)
135 (void)putchar(*cp++);
139 (void)printf("%-*.*s", v->width, v->width, vis_args);
153 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
165 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
173 int flag, sflag, tdflags;
179 flag = k->ki_p->ki_flag;
180 sflag = k->ki_p->ki_sflag;
181 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
184 switch (k->ki_p->ki_stat) {
191 if (tdflags & TDF_SINTR) /* interruptable (long) */
192 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
218 if (!(sflag & PS_INMEM))
220 if (k->ki_p->ki_nice < NZERO)
222 else if (k->ki_p->ki_nice > NZERO)
226 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
230 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
232 if (k->ki_p->ki_kiflag & KI_SLEADER)
234 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
239 (void)printf("%-*s", v->width, buf);
250 (void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
262 (int)v->width, user_from_uid(k->ki_p->ki_uid, 0));
269 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
281 (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0));
288 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
301 dev = k->ki_p->ki_tdev;
303 (void)printf("%*s", v->width, "??");
305 (void)snprintf(buff, sizeof(buff),
306 "%d/%d", major(dev), minor(dev));
307 (void)printf("%*s", v->width, buff);
321 dev = k->ki_p->ki_tdev;
322 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
323 (void)printf("%*s ", v->width-1, "??");
325 if (strncmp(ttname, "tty", 3) == 0 ||
326 strncmp(ttname, "cua", 3) == 0)
328 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
329 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
343 dev = k->ki_p->ki_tdev;
344 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
345 (void)printf("%-*s", v->width, "??");
347 (void)printf("%-*s", v->width, ttname);
360 static int use_ampm = -1;
364 (void)printf("%-*s", v->width, "-");
369 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
371 then = k->ki_p->ki_start.tv_sec;
372 tp = localtime(&then);
375 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
376 (void)strftime(buf, sizeof(buf) - 1,
377 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
378 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
379 (void)strftime(buf, sizeof(buf) - 1,
380 use_ampm ? "%a%I%p" : "%a%H ", tp);
382 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
383 (void)printf("%-*s", v->width, buf);
397 (void)printf("%-*s", v->width, "-");
400 then = k->ki_p->ki_start.tv_sec;
401 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
402 (void)printf("%-*s", v->width, buf);
413 if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
414 if (k->ki_p->ki_mtxname[0] != 0)
415 (void)printf("%-*.*s", v->width, v->width,
416 k->ki_p->ki_mtxname);
418 (void)printf("%-*s", v->width, "???");
420 (void)printf("%-*s", v->width, "-");
431 if (k->ki_p->ki_wchan) {
432 if (k->ki_p->ki_wmesg[0] != 0)
433 (void)printf("%-*.*s", v->width, v->width,
436 (void)printf("%-*lx", v->width,
437 (long)k->ki_p->ki_wchan);
439 (void)printf("%-*s", v->width, "-");
443 #define pgtok(a) (((a)*getpagesize())/1024)
454 (void)printf("%*d", v->width,
455 (k->ki_p->ki_size/1024));
466 /* XXX don't have info about shared */
467 (void)printf("%*lu", v->width,
468 (u_long)pgtok(k->ki_p->ki_rssize));
472 p_rssize(k, ve) /* doesn't account for text */
479 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_rssize));
489 long psecs; /* "parts" of a second. first micro, then centi */
491 static char decimal_point = 0;
494 decimal_point = localeconv()->decimal_point[0];
496 if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
501 * This counts time spent handling interrupts. We could
502 * fix this, but it is not 100% trivial (and interrupt
503 * time fractions only work on the sparc anyway). XXX
505 secs = k->ki_p->ki_runtime / 1000000;
506 psecs = k->ki_p->ki_runtime % 1000000;
508 secs += k->ki_p->ki_childtime.tv_sec;
509 psecs += k->ki_p->ki_childtime.tv_usec;
512 * round and scale to 100's
514 psecs = (psecs + 5000) / 10000;
518 (void)snprintf(obuff, sizeof(obuff),
519 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
520 (void)printf("%*s", v->width, obuff);
534 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
536 /* XXX - I don't like this */
537 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
540 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
541 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
542 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
553 (void)printf("%*.1f", v->width, getpcpu(k));
568 if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
570 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
571 /* XXX don't have info about shared */
572 fracmem = ((float)k->ki_p->ki_rssize)/mempages;
573 return (100.0 * fracmem);
584 (void)printf("%*.1f", v->width, getpmem(k));
595 (void)printf("%*ld", v->width,
596 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
608 (void)printf("%*s", v->width, "-");
619 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize));
628 struct priority *pri;
630 unsigned class, level;
633 pri = (struct priority *) ((char *)k + v->off);
634 class = pri->pri_class;
635 level = pri->pri_level;
638 snprintf(str, sizeof(str), "real:%u", level);
641 strncpy(str, "normal", sizeof(str));
644 snprintf(str, sizeof(str), "idle:%u", level);
647 snprintf(str, sizeof(str), "%u:%u", class, level);
650 str[sizeof(str) - 1] = '\0';
651 (void)printf("%*s", v->width, str);
655 * Generic output routines. Print fields from various prototype
663 static char ofmt[32] = "%";
671 while ((*cp++ = *fcp++));
675 (void)printf(ofmt, v->width, *(char *)bp);
678 (void)printf(ofmt, v->width, *(u_char *)bp);
681 (void)printf(ofmt, v->width, *(short *)bp);
684 (void)printf(ofmt, v->width, *(u_short *)bp);
687 (void)printf(ofmt, v->width, *(int *)bp);
690 (void)printf(ofmt, v->width, *(u_int *)bp);
693 (void)printf(ofmt, v->width, *(long *)bp);
696 (void)printf(ofmt, v->width, *(u_long *)bp);
699 (void)printf(ofmt, v->width, *(u_long *)bp);
702 errx(1, "unknown type %d", v->type);
714 printval((char *)((char *)k->ki_p + v->off), v);
726 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
728 (void)printf("%*s", v->width, "-");
739 (void)printf("%-*d", (int)v->width, get_lattr(k->ki_p->ki_pid));