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 */
216 if (KI_LWP(k, stat) == LSRUN) {
218 if (!(KI_LWP(k, tdflags) &
219 (TDF_RUNNING | TDF_RUNQ)))
222 /*if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ))*/ {
224 sprintf(cp, "%d", KI_LWP(k, cpuid));
231 /* shouldn't happen anyways */
246 if (flag & P_SWAPPEDOUT)
248 if (KI_PROC(k, nice) < NZERO)
250 else if (KI_PROC(k, nice) > NZERO)
254 if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
258 if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
260 if (numcpus > 1 && KI_LWP(k, mpcount) == 0)
264 if (KI_PROC(k, auxflags) & KI_SLEADER)
266 if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
269 printf("%-*s", vent->width, buf);
273 * Normalized priority (lower is better). For pure threads
274 * output a negated LWKT priority (so lower still means better).
276 * XXX bsd4 scheduler specific.
279 pri(const KINFO *k, const struct varent *vent)
281 if (KI_LWP(k, pid) != -1)
282 printf("%*d", vent->width, KI_LWP(k, prio));
284 printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
288 tdpri(const KINFO *k, const struct varent *vent)
291 int val = KI_LWP(k, tdprio);
293 snprintf(buf, sizeof(buf), "%2d", val);
294 printf("%*s", vent->width, buf);
298 uname(const KINFO *k, const struct varent *vent)
300 printf("%-*s", vent->width,
301 user_from_uid(KI_PROC(k, uid), 0));
305 s_uname(const KINFO *k)
307 return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
311 runame(const KINFO *k, const struct varent *vent)
313 printf("%-*s", vent->width,
314 user_from_uid(KI_PROC(k, ruid), 0));
318 s_runame(const KINFO *k)
320 return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
324 tdev(const KINFO *k, const struct varent *vent)
329 dev = KI_PROC(k, tdev);
331 printf("%*s", vent->width, "??");
333 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
334 printf("%*s", vent->width, buff);
339 tname(const KINFO *k, const struct varent *vent)
344 dev = KI_PROC(k, tdev);
345 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
346 printf("%*s ", vent->width-1, "??");
348 if (strncmp(ttname, "tty", 3) == 0 ||
349 strncmp(ttname, "cua", 3) == 0)
351 if (strncmp(ttname, "pts/", 4) == 0)
353 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
354 KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
359 longtname(const KINFO *k, const struct varent *vent)
364 dev = KI_PROC(k, tdev);
365 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
366 printf("%-*s", vent->width, "??");
368 printf("%-*s", vent->width, ttname);
372 started(const KINFO *k, const struct varent *vent)
378 static int use_ampm = -1;
381 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
383 then = KI_PROC(k, start).tv_sec;
384 if (then < btime.tv_sec) {
388 tp = localtime(&then);
391 if (now - then < 24 * 3600) {
392 strftime(buf, sizeof(buf) - 1,
393 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
394 } else if (now - then < 7 * 86400) {
395 strftime(buf, sizeof(buf) - 1,
396 use_ampm ? "%a%I%p" : "%a%H ", tp);
398 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
399 printf("%-*s", vent->width, buf);
403 lstarted(const KINFO *k, const struct varent *vent)
408 then = KI_PROC(k, start).tv_sec;
409 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
410 printf("%-*s", vent->width, buf);
414 wchan(const KINFO *k, const struct varent *vent)
416 if (*KI_LWP(k, wmesg)) {
417 printf("%-*.*s", vent->width, vent->width,
420 printf("%-*s", vent->width, "-");
427 static size_t pgsize;
430 pgsize = getpagesize();
435 vsize(const KINFO *k, const struct varent *vent)
437 put64((uintmax_t)KI_PROC(k, vm_map_size)/1024, vent->width, 'k');
441 rssize(const KINFO *k, const struct varent *vent)
443 /* XXX don't have info about shared */
444 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
447 /* doesn't account for text */
449 p_rssize(const KINFO *k, const struct varent *vent)
451 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
455 cputime(const KINFO *k, const struct varent *vent)
458 long psecs; /* "parts" of a second. first micro, then centi */
461 static char decimal_point = '\0';
463 if (decimal_point == '\0')
464 decimal_point = localeconv()->decimal_point[0];
467 * This counts time spent handling interrupts. We could
468 * fix this, but it is not 100% trivial (and interrupt
469 * time fractions only work on the sparc anyway). XXX
471 timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
473 secs = timeus / 1000000;
474 psecs = timeus % 1000000;
476 secs += KI_PROC(k, cru).ru_utime.tv_sec +
477 KI_PROC(k, cru).ru_stime.tv_sec;
478 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
479 KI_PROC(k, cru).ru_stime.tv_usec;
482 * round and scale to 100's
484 psecs = (psecs + 5000) / 10000;
489 snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
490 secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
491 } else if (secs >= 100 * 60) {
492 snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
493 secs / 60 / 60, secs / 60 % 60, secs % 60);
497 snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
498 secs / 60, secs % 60,
499 decimal_point, psecs);
501 printf("%*s", vent->width, obuff);
505 getpcpu(const KINFO *k)
514 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
516 /* XXX - I don't like this */
517 if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
519 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
523 pcpu(const KINFO *k, const struct varent *vent)
525 printf("%*.1f", vent->width, getpcpu(k));
529 pnice(const KINFO *k, const struct varent *vent)
533 switch (KI_LWP(k, rtprio).type) {
534 case RTP_PRIO_REALTIME:
535 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
538 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
540 case RTP_PRIO_THREAD:
541 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
544 niceval = KI_PROC(k, nice) - NZERO;
547 printf("%*d", vent->width, niceval);
552 getpmem(const KINFO *k)
563 if (KI_PROC(k, flags) & P_SWAPPEDOUT)
565 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
567 /* XXX don't have info about shared */
568 fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
569 return (100.0 * fracmem);
573 pmem(const KINFO *k, const struct varent *vent)
575 printf("%*.1f", vent->width, getpmem(k));
579 pagein(const KINFO *k, const struct varent *vent)
581 printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
586 maxrss(const KINFO *k __unused, const struct varent *vent)
588 printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
592 tsize(const KINFO *k, const struct varent *vent)
594 put64(pgtob(KI_PROC(k, vm_tsize))/1024, vent->width, 'k');
598 rtprior(const KINFO *k, const struct varent *vent)
604 prtp = &KI_LWP(k, rtprio);
608 case RTP_PRIO_REALTIME:
609 snprintf(str, sizeof(str), "real:%u", prio);
611 case RTP_PRIO_NORMAL:
612 strncpy(str, "normal", sizeof(str));
615 snprintf(str, sizeof(str), "idle:%u", prio);
618 snprintf(str, sizeof(str), "%u:%u", type, prio);
621 str[sizeof(str) - 1] = '\0';
622 printf("%*s", vent->width, str);
626 * Generic output routines. Print fields from various prototype
630 printval(const char *bp, const struct varent *vent)
632 static char ofmt[32] = "%";
637 fcp = vent->var->fmt;
638 if (vent->var->flag & LJUST)
641 while ((*cp++ = *fcp++));
643 switch (vent->var->type) {
645 printf(ofmt, vent->width, *(const char *)bp);
648 printf(ofmt, vent->width, *(const u_char *)bp);
651 printf(ofmt, vent->width, *(const short *)bp);
654 printf(ofmt, vent->width, *(const u_short *)bp);
657 printf(ofmt, vent->width, *(const int *)bp);
660 printf(ofmt, vent->width, *(const u_int *)bp);
663 printf(ofmt, vent->width, *(const long *)bp);
666 printf(ofmt, vent->width, *(const u_long *)bp);
669 printf(ofmt, vent->width, *(const u_long *)bp);
672 errx(1, "unknown type %d", vent->var->type);
677 pvar(const KINFO *k, const struct varent *vent)
679 printval((char *)((char *)k->ki_proc + vent->var->off), vent);
683 lpest(const KINFO *k, const struct varent *vent)
687 val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
689 printval((char *)&val, vent);
694 lpvar(const KINFO *k, const struct varent *vent)
696 printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
700 rvar(const KINFO *k, const struct varent *vent)
702 printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
706 make_printable(const char *str)
714 if ((cpy = malloc(len * 4 + 1)) == NULL)
716 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
721 * Output a number, divide down as needed to fit within the
722 * field. This function differs from the code used by systat
723 * in that it tries to differentiate the display by always
724 * using a decimal point for excessively large numbers so
725 * the human eye naturally notices the difference.
728 put64(u_int64_t n, int w, int type)
736 snprintf(b, sizeof(b), "%*jd", w, (uintmax_t)n);
738 if (len <= (size_t)w) {
739 fwrite(b, len, 1, stdout);
749 for (d = 1; n / d >= 100000; d *= u) {
777 if (w > 4 && n / d >= u) {
778 snprintf(b, sizeof(b), "%*ju.%02u%c",
780 (uintmax_t)(n / (d * u)),
781 (u_int)(n / (d * u / 100) % 100),
784 snprintf(b, sizeof(b), "%*jd%c",
785 w - 1, (uintmax_t)n / d, type);
788 fwrite(b, len, 1, stdout);