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
33 * @(#)print.c 8.6 (Berkeley) 4/16/94
34 * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $
35 * $DragonFly: src/bin/ps/print.c,v 1.34 2008/11/10 14:56:33 swildner Exp $
39 #include <sys/param.h>
41 #include <sys/resource.h>
44 #include <sys/ucred.h>
45 #include <sys/sysctl.h>
46 #include <sys/rtprio.h>
64 static const char *make_printable(const char *str);
65 static void put64(u_int64_t n, int w, int type);
75 STAILQ_FOREACH(vent, &var_head, link) {
76 if (*vent->header != '\0') {
83 STAILQ_FOREACH(vent, &var_head, link) {
85 if (v->flag & LJUST) {
86 if (STAILQ_NEXT(vent, link) == NULL) /* last one */
87 printf("%s", vent->header);
89 printf("%-*s", vent->width, vent->header);
91 printf("%*s", vent->width, vent->header);
92 if (STAILQ_NEXT(vent, link) != NULL)
99 command(const KINFO *k, const struct varent *vent)
103 char *cp, *vis_env, *vis_args;
106 /* Don't pad the last field. */
107 if (STAILQ_NEXT(vent, link) == NULL)
108 printf("%s", make_printable(KI_PROC(k, comm)));
110 printf("%-*s", vent->width,
111 make_printable(KI_PROC(k, comm)));
115 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
117 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
119 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
121 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
126 indent = k->ki_indent;
130 if (STAILQ_NEXT(vent, link) == NULL) {
132 if (termwidth == UNLIMITED) {
134 printf("%s ", vis_env);
139 printf("%s", vis_args);
141 left = termwidth - (totwidth - vent->width);
142 if (left < 1) /* already wrapped, just use std width */
144 while (indent && left > 1) {
149 if ((cp = vis_env) != NULL) {
150 while (--left >= 0 && *cp)
155 for (cp = vis_args; --left >= 0 && *cp != '\0';)
160 printf("%-*.*s", vent->width, vent->width, vis_args);
167 ucomm(const KINFO *k, const struct varent *vent)
169 /* Do not pad the last field */
170 if (STAILQ_NEXT(vent, link) == NULL)
171 printf("%s", make_printable(KI_PROC(k, comm)));
173 printf("%-*s", vent->width, make_printable(KI_PROC(k, comm)));
177 logname(const KINFO *k, const struct varent *vent)
179 const char *s = KI_PROC(k, login);
181 printf("%-*s", vent->width, *s != '\0' ? s : "-");
185 state(const KINFO *k, const struct varent *vent)
191 flag = KI_PROC(k, flags);
194 switch (KI_PROC(k, stat)) {
201 switch (KI_LWP(k, stat)) {
203 if (KI_LWP(k, flags) & LWP_SINTR) {
204 /* interruptable wait short/long */
205 *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
207 else if (KI_LWP(k, tdflags) & TDF_SINTR)
208 *cp = 'S'; /* interruptable lwkt wait */
209 else if (KI_PROC(k, paddr))
210 *cp = 'D'; /* uninterruptable wait */
212 *cp = 'B'; /* uninterruptable lwkt wait */
217 if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ)) {
219 sprintf(cp, "%d", KI_LWP(k, cpuid));
226 /* shouldn't happen anyways */
241 if (flag & P_SWAPPEDOUT)
243 if (KI_PROC(k, nice) < NZERO)
245 else if (KI_PROC(k, nice) > NZERO)
249 if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
253 if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
255 if (numcpus > 1 && KI_LWP(k, mpcount) == 0)
259 if (KI_PROC(k, auxflags) & KI_SLEADER)
261 if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
264 printf("%-*s", vent->width, buf);
268 * Normalized priority (lower is better). For pure threads
269 * output a negated LWKT priority (so lower still means better).
271 * XXX bsd4 scheduler specific.
274 pri(const KINFO *k, const struct varent *vent)
276 if (KI_LWP(k, pid) != -1)
277 printf("%*d", vent->width, KI_LWP(k, prio));
279 printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
283 tdpri(const KINFO *k, const struct varent *vent)
286 int val = KI_LWP(k, tdprio);
288 snprintf(buf, sizeof(buf), "%2d", val);
289 printf("%*s", vent->width, buf);
293 uname(const KINFO *k, const struct varent *vent)
295 printf("%-*s", vent->width,
296 user_from_uid(KI_PROC(k, uid), 0));
300 s_uname(const KINFO *k)
302 return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
306 runame(const KINFO *k, const struct varent *vent)
308 printf("%-*s", vent->width,
309 user_from_uid(KI_PROC(k, ruid), 0));
313 s_runame(const KINFO *k)
315 return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
319 tdev(const KINFO *k, const struct varent *vent)
324 dev = KI_PROC(k, tdev);
326 printf("%*s", vent->width, "??");
328 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
329 printf("%*s", vent->width, buff);
334 tname(const KINFO *k, const struct varent *vent)
339 dev = KI_PROC(k, tdev);
340 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
341 printf("%*s ", vent->width-1, "??");
343 if (strncmp(ttname, "tty", 3) == 0 ||
344 strncmp(ttname, "cua", 3) == 0)
346 if (strncmp(ttname, "pts/", 4) == 0)
348 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
349 KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
354 longtname(const KINFO *k, const struct varent *vent)
359 dev = KI_PROC(k, tdev);
360 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
361 printf("%-*s", vent->width, "??");
363 printf("%-*s", vent->width, ttname);
367 started(const KINFO *k, const struct varent *vent)
373 static int use_ampm = -1;
376 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
378 then = KI_PROC(k, start).tv_sec;
379 if (then < btime.tv_sec) {
383 tp = localtime(&then);
386 if (now - then < 24 * 3600) {
387 strftime(buf, sizeof(buf) - 1,
388 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
389 } else if (now - then < 7 * 86400) {
390 strftime(buf, sizeof(buf) - 1,
391 use_ampm ? "%a%I%p" : "%a%H ", tp);
393 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
394 printf("%-*s", vent->width, buf);
398 lstarted(const KINFO *k, const struct varent *vent)
403 then = KI_PROC(k, start).tv_sec;
404 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
405 printf("%-*s", vent->width, buf);
409 wchan(const KINFO *k, const struct varent *vent)
411 if (*KI_LWP(k, wmesg)) {
412 printf("%-*.*s", vent->width, vent->width,
415 printf("%-*s", vent->width, "-");
422 static size_t pgsize;
425 pgsize = getpagesize();
430 vsize(const KINFO *k, const struct varent *vent)
432 put64((uintmax_t)KI_PROC(k, vm_map_size)/1024, vent->width, 'k');
436 rssize(const KINFO *k, const struct varent *vent)
438 /* XXX don't have info about shared */
439 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
442 /* doesn't account for text */
444 p_rssize(const KINFO *k, const struct varent *vent)
446 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
450 cputime(const KINFO *k, const struct varent *vent)
453 long psecs; /* "parts" of a second. first micro, then centi */
456 static char decimal_point = '\0';
458 if (decimal_point == '\0')
459 decimal_point = localeconv()->decimal_point[0];
462 * This counts time spent handling interrupts. We could
463 * fix this, but it is not 100% trivial (and interrupt
464 * time fractions only work on the sparc anyway). XXX
466 timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
468 secs = timeus / 1000000;
469 psecs = timeus % 1000000;
471 secs += KI_PROC(k, cru).ru_utime.tv_sec +
472 KI_PROC(k, cru).ru_stime.tv_sec;
473 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
474 KI_PROC(k, cru).ru_stime.tv_usec;
477 * round and scale to 100's
479 psecs = (psecs + 5000) / 10000;
484 snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
485 secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
486 } else if (secs >= 100 * 60) {
487 snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
488 secs / 60 / 60, secs / 60 % 60, secs % 60);
492 snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
493 secs / 60, secs % 60,
494 decimal_point, psecs);
496 printf("%*s", vent->width, obuff);
500 getpcpu(const KINFO *k)
509 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
511 /* XXX - I don't like this */
512 if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
515 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
516 return (100.0 * fxtofl(KI_LWP(k, pctcpu)) /
517 (1.0 - exp(KI_PROC(k, swtime) * log(fxtofl(ccpu)))));
521 pcpu(const KINFO *k, const struct varent *vent)
523 printf("%*.1f", vent->width, getpcpu(k));
527 pnice(const KINFO *k, const struct varent *vent)
531 switch (KI_LWP(k, rtprio).type) {
532 case RTP_PRIO_REALTIME:
533 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
536 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
538 case RTP_PRIO_THREAD:
539 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
542 niceval = KI_PROC(k, nice) - NZERO;
545 printf("%*d", vent->width, niceval);
550 getpmem(const KINFO *k)
561 if (KI_PROC(k, flags) & P_SWAPPEDOUT)
563 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
565 /* XXX don't have info about shared */
566 fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
567 return (100.0 * fracmem);
571 pmem(const KINFO *k, const struct varent *vent)
573 printf("%*.1f", vent->width, getpmem(k));
577 pagein(const KINFO *k, const struct varent *vent)
579 printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
584 maxrss(const KINFO *k __unused, const struct varent *vent)
586 printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
590 tsize(const KINFO *k, const struct varent *vent)
592 put64(pgtob(KI_PROC(k, vm_tsize))/1024, vent->width, 'k');
596 rtprior(const KINFO *k, const struct varent *vent)
602 prtp = &KI_LWP(k, rtprio);
606 case RTP_PRIO_REALTIME:
607 snprintf(str, sizeof(str), "real:%u", prio);
609 case RTP_PRIO_NORMAL:
610 strncpy(str, "normal", sizeof(str));
613 snprintf(str, sizeof(str), "idle:%u", prio);
616 snprintf(str, sizeof(str), "%u:%u", type, prio);
619 str[sizeof(str) - 1] = '\0';
620 printf("%*s", vent->width, str);
624 * Generic output routines. Print fields from various prototype
628 printval(const char *bp, const struct varent *vent)
630 static char ofmt[32] = "%";
635 fcp = vent->var->fmt;
636 if (vent->var->flag & LJUST)
639 while ((*cp++ = *fcp++));
641 switch (vent->var->type) {
643 printf(ofmt, vent->width, *(const char *)bp);
646 printf(ofmt, vent->width, *(const u_char *)bp);
649 printf(ofmt, vent->width, *(const short *)bp);
652 printf(ofmt, vent->width, *(const u_short *)bp);
655 printf(ofmt, vent->width, *(const int *)bp);
658 printf(ofmt, vent->width, *(const u_int *)bp);
661 printf(ofmt, vent->width, *(const long *)bp);
664 printf(ofmt, vent->width, *(const u_long *)bp);
667 printf(ofmt, vent->width, *(const u_long *)bp);
670 errx(1, "unknown type %d", vent->var->type);
675 pvar(const KINFO *k, const struct varent *vent)
677 printval((char *)((char *)k->ki_proc + vent->var->off), vent);
681 lpest(const KINFO *k, const struct varent *vent)
685 val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
687 printval((char *)&val, vent);
692 lpvar(const KINFO *k, const struct varent *vent)
694 printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
698 rvar(const KINFO *k, const struct varent *vent)
700 printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
704 make_printable(const char *str)
712 if ((cpy = malloc(len * 4 + 1)) == NULL)
714 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
719 * Output a number, divide down as needed to fit within the
720 * field. This function differs from the code used by systat
721 * in that it tries to differentiate the display by always
722 * using a decimal point for excessively large numbers so
723 * the human eye naturally notices the difference.
726 put64(u_int64_t n, int w, int type)
734 snprintf(b, sizeof(b), "%*jd", w, (uintmax_t)n);
736 if (len <= (size_t)w) {
737 fwrite(b, len, 1, stdout);
747 for (d = 1; n / d >= 100000; d *= u) {
775 if (w > 4 && n / d >= u) {
776 snprintf(b, sizeof(b), "%*ju.%02u%c",
778 (uintmax_t)(n / (d * u)),
779 (u_int)(n / (d * u / 100) % 100),
782 snprintf(b, sizeof(b), "%*jd%c",
783 w - 1, (uintmax_t)n / d, type);
786 fwrite(b, len, 1, stdout);