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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
44 #include <sys/param.h>
46 #include <sys/resource.h>
51 #include <sys/sysctl.h>
70 static void printval(char *, VAR *);
78 for (vent = vhead; vent; vent = vent->next) {
80 if (v->flag & LJUST) {
81 if (vent->next == NULL) /* last one */
82 (void)printf("%s", v->header);
84 (void)printf("%-*s", v->width, v->header);
86 (void)printf("%*s", v->width, v->header);
87 if (vent->next != NULL)
94 command(KINFO *k, VARENT *ve)
98 char *cp, *vis_env, *vis_args;
103 if (ve->next == NULL) /* last field, don't pad */
104 (void)printf("%s", k->ki_p->ki_comm);
106 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
110 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
112 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
114 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
116 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
120 if (ve->next == NULL) {
122 if (termwidth == UNLIMITED) {
124 (void)printf("%s ", vis_env);
125 (void)printf("%s", vis_args);
127 left = termwidth - (totwidth - v->width);
128 if (left < 1) /* already wrapped, just use std width */
130 if ((cp = vis_env) != NULL) {
131 while (--left >= 0 && *cp)
132 (void)putchar(*cp++);
136 for (cp = vis_args; --left >= 0 && *cp != '\0';)
137 (void)putchar(*cp++);
141 (void)printf("%-*.*s", v->width, v->width, vis_args);
148 ucomm(KINFO *k, VARENT *ve)
153 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
157 logname(KINFO *k, VARENT *ve)
163 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
167 state(KINFO *k, VARENT *ve)
169 int flag, sflag, tdflags;
175 flag = k->ki_p->ki_flag;
176 sflag = k->ki_p->ki_sflag;
177 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
180 switch (k->ki_p->ki_stat) {
187 if (tdflags & TDF_SINTR) /* interruptable (long) */
188 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
214 if (!(sflag & PS_INMEM))
216 if (k->ki_p->ki_nice < NZERO)
218 else if (k->ki_p->ki_nice > NZERO)
222 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
226 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
228 if (k->ki_p->ki_kiflag & KI_SLEADER)
230 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
235 (void)printf("%-*s", v->width, buf);
239 pri(KINFO *k, VARENT *ve)
244 (void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
248 uname(KINFO *k, VARENT *ve)
254 (int)v->width, user_from_uid(k->ki_p->ki_uid, 0));
260 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
264 runame(KINFO *k, VARENT *ve)
270 (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0));
276 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
280 tdev(KINFO *k, VARENT *ve)
287 dev = k->ki_p->ki_tdev;
289 (void)printf("%*s", v->width, "??");
291 (void)snprintf(buff, sizeof(buff),
292 "%d/%d", major(dev), minor(dev));
293 (void)printf("%*s", v->width, buff);
298 tname(KINFO *k, VARENT *ve)
305 dev = k->ki_p->ki_tdev;
306 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
307 (void)printf("%*s ", v->width-1, "??");
309 if (strncmp(ttname, "tty", 3) == 0 ||
310 strncmp(ttname, "cua", 3) == 0)
312 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
313 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
318 longtname(KINFO *k, VARENT *ve)
325 dev = k->ki_p->ki_tdev;
326 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
327 (void)printf("%-*s", v->width, "??");
329 (void)printf("%-*s", v->width, ttname);
333 started(KINFO *k, VARENT *ve)
340 static int use_ampm = -1;
344 (void)printf("%-*s", v->width, "-");
349 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
351 then = k->ki_p->ki_start.tv_sec;
352 tp = localtime(&then);
355 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
356 (void)strftime(buf, sizeof(buf) - 1,
357 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
358 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
359 (void)strftime(buf, sizeof(buf) - 1,
360 use_ampm ? "%a%I%p" : "%a%H ", tp);
362 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
363 (void)printf("%-*s", v->width, buf);
367 lstarted(KINFO *k, VARENT *ve)
375 (void)printf("%-*s", v->width, "-");
378 then = k->ki_p->ki_start.tv_sec;
379 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
380 (void)printf("%-*s", v->width, buf);
384 mtxname(KINFO *k, VARENT *ve)
389 if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
390 if (k->ki_p->ki_mtxname[0] != 0)
391 (void)printf("%-*.*s", v->width, v->width,
392 k->ki_p->ki_mtxname);
394 (void)printf("%-*s", v->width, "???");
396 (void)printf("%-*s", v->width, "-");
400 wchan(KINFO *k, VARENT *ve)
405 if (k->ki_p->ki_wchan) {
406 if (k->ki_p->ki_wmesg[0] != 0)
407 (void)printf("%-*.*s", v->width, v->width,
410 (void)printf("%-*lx", v->width,
411 (long)k->ki_p->ki_wchan);
413 (void)printf("%-*s", v->width, "-");
418 mwchan(KINFO *k, VARENT *ve)
423 if (k->ki_p->ki_wchan) {
424 if (k->ki_p->ki_wmesg[0] != 0)
425 (void)printf("%-*.*s", v->width, v->width,
428 (void)printf("%-*lx", v->width,
429 (long)k->ki_p->ki_wchan);
430 } else if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
431 if (k->ki_p->ki_mtxname[0]) {
432 (void)printf("%-*.*s", v->width, v->width,
433 k->ki_p->ki_mtxname);
435 (void)printf("%-*s", v->width, "???");
438 (void)printf("%-*s", v->width, "-");
443 #define pgtok(a) (((a)*getpagesize())/1024)
447 vsize(KINFO *k, VARENT *ve)
452 (void)printf("%*d", v->width,
453 (k->ki_p->ki_size/1024));
457 cputime(KINFO *k, VARENT *ve)
461 long psecs; /* "parts" of a second. first micro, then centi */
463 static char decimal_point = 0;
466 decimal_point = localeconv()->decimal_point[0];
468 if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
473 * This counts time spent handling interrupts. We could
474 * fix this, but it is not 100% trivial (and interrupt
475 * time fractions only work on the sparc anyway). XXX
477 secs = k->ki_p->ki_runtime / 1000000;
478 psecs = k->ki_p->ki_runtime % 1000000;
480 secs += k->ki_p->ki_childtime.tv_sec;
481 psecs += k->ki_p->ki_childtime.tv_usec;
484 * round and scale to 100's
486 psecs = (psecs + 5000) / 10000;
490 (void)snprintf(obuff, sizeof(obuff),
491 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
492 (void)printf("%*s", v->width, obuff);
496 getpcpu(const KINFO *k)
505 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
507 /* XXX - I don't like this */
508 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
511 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
512 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
513 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
517 pcpu(KINFO *k, VARENT *ve)
522 (void)printf("%*.1f", v->width, getpcpu(k));
536 if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
538 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
539 /* XXX don't have info about shared */
540 fracmem = ((float)k->ki_p->ki_rssize)/mempages;
541 return (100.0 * fracmem);
545 pmem(KINFO *k, VARENT *ve)
550 (void)printf("%*.1f", v->width, getpmem(k));
554 pagein(KINFO *k, VARENT *ve)
559 (void)printf("%*ld", v->width,
560 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
565 maxrss(KINFO *k __unused, VARENT *ve)
571 (void)printf("%*s", v->width, "-");
575 tsize(KINFO *k, VARENT *ve)
580 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize));
584 priorityr(KINFO *k, VARENT *ve)
587 struct priority *lpri;
589 unsigned class, level;
592 lpri = (struct priority *) ((char *)k + v->off);
593 class = lpri->pri_class;
594 level = lpri->pri_level;
597 snprintf(str, sizeof(str), "real:%u", level);
600 strncpy(str, "normal", sizeof(str));
603 snprintf(str, sizeof(str), "idle:%u", level);
606 snprintf(str, sizeof(str), "%u:%u", class, level);
609 str[sizeof(str) - 1] = '\0';
610 (void)printf("%*s", v->width, str);
614 * Generic output routines. Print fields from various prototype
618 printval(char *bp, VAR *v)
620 static char ofmt[32] = "%";
628 while ((*cp++ = *fcp++));
632 (void)printf(ofmt, v->width, *(char *)bp);
635 (void)printf(ofmt, v->width, *(u_char *)bp);
638 (void)printf(ofmt, v->width, *(short *)bp);
641 (void)printf(ofmt, v->width, *(u_short *)bp);
644 (void)printf(ofmt, v->width, *(int *)bp);
647 (void)printf(ofmt, v->width, *(u_int *)bp);
650 (void)printf(ofmt, v->width, *(long *)bp);
653 (void)printf(ofmt, v->width, *(u_long *)bp);
656 (void)printf(ofmt, v->width, *(u_long *)bp);
659 errx(1, "unknown type %d", v->type);
664 kvar(KINFO *k, VARENT *ve)
669 printval((char *)((char *)k->ki_p + v->off), v);
673 rvar(KINFO *k, VARENT *ve)
679 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
681 (void)printf("%*s", v->width, "-");
685 lattr(KINFO *k, VARENT *ve)
690 (void)printf("%-*d", (int)v->width, get_lattr(k->ki_p->ki_pid));