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";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
44 #include <sys/resource.h>
49 #include <sys/sysctl.h>
68 #define ps_pgtok(a) (((a) * getpagesize()) / 1024)
76 for (vent = vhead; vent; vent = vent->next) {
78 if (v->flag & LJUST) {
79 if (vent->next == NULL) /* last one */
80 (void)printf("%s", v->header);
82 (void)printf("%-*s", v->width, v->header);
84 (void)printf("%*s", v->width, v->header);
85 if (vent->next != NULL)
92 arguments(KINFO *k, VARENT *ve)
100 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
101 errx(1, "malloc failed");
102 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
104 if (ve->next == NULL) {
106 if (termwidth == UNLIMITED) {
107 (void)printf("%s", vis_args);
109 left = termwidth - (totwidth - v->width);
110 if (left < 1) /* already wrapped, just use std width */
112 for (cp = vis_args; --left >= 0 && *cp != '\0';)
113 (void)putchar(*cp++);
116 (void)printf("%-*.*s", v->width, v->width, vis_args);
122 command(KINFO *k, VARENT *ve)
126 char *cp, *vis_env, *vis_args;
131 if (ve->next == NULL) /* last field, don't pad */
132 (void)printf("%s", k->ki_p->ki_comm);
134 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
138 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
139 errx(1, "malloc failed");
140 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
142 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
143 errx(1, "malloc failed");
144 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
148 if (ve->next == NULL) {
150 if (termwidth == UNLIMITED) {
152 (void)printf("%s ", vis_env);
153 (void)printf("%s", vis_args);
155 left = termwidth - (totwidth - v->width);
156 if (left < 1) /* already wrapped, just use std width */
158 if ((cp = vis_env) != NULL) {
159 while (--left >= 0 && *cp)
160 (void)putchar(*cp++);
164 for (cp = vis_args; --left >= 0 && *cp != '\0';)
165 (void)putchar(*cp++);
169 (void)printf("%-*.*s", v->width, v->width, vis_args);
176 ucomm(KINFO *k, VARENT *ve)
181 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
185 logname(KINFO *k, VARENT *ve)
191 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
195 state(KINFO *k, VARENT *ve)
197 int flag, sflag, tdflags;
203 flag = k->ki_p->ki_flag;
204 sflag = k->ki_p->ki_sflag;
205 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
208 switch (k->ki_p->ki_stat) {
215 if (tdflags & TDF_SINTR) /* interruptable (long) */
216 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
242 if (!(sflag & PS_INMEM))
244 if (k->ki_p->ki_nice < NZERO)
246 else if (k->ki_p->ki_nice > NZERO)
250 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
254 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
256 if (k->ki_p->ki_kiflag & KI_SLEADER)
258 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
263 (void)printf("%-*s", v->width, buf);
267 pri(KINFO *k, VARENT *ve)
272 (void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
276 uname(KINFO *k, VARENT *ve)
281 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
287 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
291 rgroupname(KINFO *k, VARENT *ve)
296 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
300 s_rgroupname(KINFO *k)
302 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
306 runame(KINFO *k, VARENT *ve)
311 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
317 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
322 tdev(KINFO *k, VARENT *ve)
329 dev = k->ki_p->ki_tdev;
331 (void)printf("%*s", v->width, "??");
333 (void)snprintf(buff, sizeof(buff),
334 "%d/%d", major(dev), minor(dev));
335 (void)printf("%*s", v->width, buff);
340 tname(KINFO *k, VARENT *ve)
347 dev = k->ki_p->ki_tdev;
348 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
349 (void)printf("%*s ", v->width-1, "??");
351 if (strncmp(ttname, "tty", 3) == 0 ||
352 strncmp(ttname, "cua", 3) == 0)
354 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
355 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
360 longtname(KINFO *k, VARENT *ve)
367 dev = k->ki_p->ki_tdev;
368 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
369 (void)printf("%-*s", v->width, "??");
371 (void)printf("%-*s", v->width, ttname);
375 started(KINFO *k, VARENT *ve)
381 static int use_ampm = -1;
385 (void)printf("%-*s", v->width, "-");
390 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
392 then = k->ki_p->ki_start.tv_sec;
393 tp = localtime(&then);
394 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
395 (void)strftime(buf, sizeof(buf) - 1,
396 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
397 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
398 (void)strftime(buf, sizeof(buf) - 1,
399 use_ampm ? "%a%I%p" : "%a%H ", tp);
401 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
402 (void)printf("%-*s", v->width, buf);
406 lstarted(KINFO *k, VARENT *ve)
414 (void)printf("%-*s", v->width, "-");
417 then = k->ki_p->ki_start.tv_sec;
418 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
419 (void)printf("%-*s", v->width, buf);
423 lockname(KINFO *k, VARENT *ve)
428 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
429 if (k->ki_p->ki_lockname[0] != 0)
430 (void)printf("%-*.*s", v->width, v->width,
431 k->ki_p->ki_lockname);
433 (void)printf("%-*s", v->width, "???");
435 (void)printf("%-*s", v->width, "-");
439 wchan(KINFO *k, VARENT *ve)
444 if (k->ki_p->ki_wchan) {
445 if (k->ki_p->ki_wmesg[0] != 0)
446 (void)printf("%-*.*s", v->width, v->width,
449 (void)printf("%-*lx", v->width,
450 (long)k->ki_p->ki_wchan);
452 (void)printf("%-*s", v->width, "-");
457 mwchan(KINFO *k, VARENT *ve)
462 if (k->ki_p->ki_wchan) {
463 if (k->ki_p->ki_wmesg[0] != 0)
464 (void)printf("%-*.*s", v->width, v->width,
467 (void)printf("%-*lx", v->width,
468 (long)k->ki_p->ki_wchan);
469 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
470 if (k->ki_p->ki_lockname[0]) {
471 (void)printf("%-*.*s", v->width, v->width,
472 k->ki_p->ki_lockname);
474 (void)printf("%-*s", v->width, "???");
477 (void)printf("%-*s", v->width, "-");
482 vsize(KINFO *k, VARENT *ve)
487 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
491 cputime(KINFO *k, VARENT *ve)
495 long psecs; /* "parts" of a second. first micro, then centi */
497 static char decimal_point = 0;
500 decimal_point = localeconv()->decimal_point[0];
502 if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
507 * This counts time spent handling interrupts. We could
508 * fix this, but it is not 100% trivial (and interrupt
509 * time fractions only work on the sparc anyway). XXX
511 secs = k->ki_p->ki_runtime / 1000000;
512 psecs = k->ki_p->ki_runtime % 1000000;
514 secs += k->ki_p->ki_childtime.tv_sec;
515 psecs += k->ki_p->ki_childtime.tv_usec;
518 * round and scale to 100's
520 psecs = (psecs + 5000) / 10000;
524 (void)snprintf(obuff, sizeof(obuff),
525 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
526 (void)printf("%*s", v->width, obuff);
530 elapsed(KINFO *k, VARENT *ve)
538 secs = now - k->ki_p->ki_start.tv_sec;
539 (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld", (long)secs/60,
541 (void)printf("%*s", v->width, obuff);
545 getpcpu(const KINFO *k)
554 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
556 /* XXX - I don't like this */
557 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
560 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
561 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
562 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
566 pcpu(KINFO *k, VARENT *ve)
571 (void)printf("%*.1f", v->width, getpcpu(k));
585 if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
587 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
588 /* XXX don't have info about shared */
589 fracmem = ((float)k->ki_p->ki_rssize)/mempages;
590 return (100.0 * fracmem);
594 pmem(KINFO *k, VARENT *ve)
599 (void)printf("%*.1f", v->width, getpmem(k));
603 pagein(KINFO *k, VARENT *ve)
608 (void)printf("%*ld", v->width,
609 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
614 maxrss(KINFO *k __unused, VARENT *ve)
620 (void)printf("%*s", v->width, "-");
624 priorityr(KINFO *k, VARENT *ve)
627 struct priority *lpri;
629 unsigned class, level;
632 lpri = (struct priority *) ((char *)k + v->off);
633 class = lpri->pri_class;
634 level = lpri->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
658 printval(void *bp, VAR *v)
660 static char ofmt[32] = "%";
669 while ((*cp++ = *fcp++));
673 (void)printf(ofmt, v->width, *(char *)bp);
676 (void)printf(ofmt, v->width, *(u_char *)bp);
679 (void)printf(ofmt, v->width, *(short *)bp);
682 (void)printf(ofmt, v->width, *(u_short *)bp);
685 (void)printf(ofmt, v->width, *(int *)bp);
688 (void)printf(ofmt, v->width, *(u_int *)bp);
691 (void)printf(ofmt, v->width, *(long *)bp);
694 (void)printf(ofmt, v->width, *(u_long *)bp);
697 (void)printf(ofmt, v->width, *(u_long *)bp);
700 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
703 errx(1, "unknown type %d", v->type);
708 kvar(KINFO *k, VARENT *ve)
713 printval((char *)((char *)k->ki_p + v->off), v);
717 rvar(KINFO *k, VARENT *ve)
723 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
725 (void)printf("%*s", v->width, "-");
729 lattr(KINFO *k, VARENT *ve)
734 (void)printf("%-*d", v->width, get_lattr(k->ki_p->ki_pid));