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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)print.c 8.6 (Berkeley) 4/16/94
30 * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $
31 * $DragonFly: src/bin/ps/print.c,v 1.34 2008/11/10 14:56:33 swildner Exp $
35 #include <sys/param.h>
37 #include <sys/resource.h>
40 #include <sys/ucred.h>
41 #include <sys/sysctl.h>
42 #include <sys/rtprio.h>
60 static const char *make_printable(const char *str);
61 static const char *make_printable2(const char *str);
62 static void put64(u_int64_t n, int w, int type);
64 #define SHOW_THRNAME(k) \
65 (showtid && KI_PROC(k, pid) != -1 && KI_PROC(k, nthreads) > 1)
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 if (SHOW_THRNAME(k)) {
110 make_printable(KI_PROC(k, comm)),
111 make_printable2(KI_LWP(k, comm)));
113 printf("%s", make_printable(KI_PROC(k, comm)));
116 if (SHOW_THRNAME(k)) {
117 printf("%-*s/%s", vent->width,
118 make_printable(KI_PROC(k, comm)),
119 make_printable2(KI_LWP(k, comm)));
121 printf("%-*s", vent->width,
122 make_printable(KI_PROC(k, comm)));
128 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
130 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
132 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
134 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
139 indent = k->ki_indent;
143 if (STAILQ_NEXT(vent, link) == NULL) {
145 if (termwidth == UNLIMITED) {
147 printf("%s ", vis_env);
152 printf("%s", vis_args);
154 left = termwidth - (totwidth - vent->width);
155 if (left < 1) /* already wrapped, just use std width */
157 while (indent && left > 1) {
162 if ((cp = vis_env) != NULL) {
163 while (--left >= 0 && *cp)
168 for (cp = vis_args; --left >= 0 && *cp != '\0';)
173 printf("%-*.*s", vent->width, vent->width, vis_args);
180 ucomm(const KINFO *k, const struct varent *vent)
182 /* Do not pad the last field */
183 if (STAILQ_NEXT(vent, link) == NULL) {
184 if (SHOW_THRNAME(k)) {
185 printf("%s/%s", make_printable(KI_PROC(k, comm)),
186 make_printable2(KI_LWP(k, comm)));
188 printf("%s", make_printable(KI_PROC(k, comm)));
191 if (SHOW_THRNAME(k)) {
192 printf("%-*s/%s", vent->width,
193 make_printable(KI_PROC(k, comm)),
194 make_printable2(KI_LWP(k, comm)));
196 printf("%-*s", vent->width,
197 make_printable(KI_PROC(k, comm)));
203 logname(const KINFO *k, const struct varent *vent)
205 const char *s = KI_PROC(k, login);
207 printf("%-*s", vent->width, *s != '\0' ? s : "-");
211 state(const KINFO *k, const struct varent *vent)
217 flag = KI_PROC(k, flags);
220 switch (KI_PROC(k, stat)) {
227 switch (KI_LWP(k, stat)) {
229 if (KI_LWP(k, flags) & LWP_SINTR) {
230 /* interruptable wait short/long */
231 *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
233 else if (KI_LWP(k, tdflags) & TDF_SINTR)
234 *cp = 'S'; /* interruptable lwkt wait */
235 else if (KI_PROC(k, paddr))
236 *cp = 'D'; /* uninterruptable wait */
238 *cp = 'B'; /* uninterruptable lwkt wait */
242 if (KI_LWP(k, stat) == LSRUN) {
244 if (!(KI_LWP(k, tdflags) &
245 (TDF_RUNNING | TDF_RUNQ)))
248 /*if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ))*/ {
250 sprintf(cp, "%d", KI_LWP(k, cpuid));
257 /* shouldn't happen anyways */
272 if (flag & P_SWAPPEDOUT)
274 if (KI_PROC(k, nice) < NZERO)
276 else if (KI_PROC(k, nice) > NZERO)
280 if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
284 if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
288 if (KI_PROC(k, auxflags) & KI_SLEADER)
290 if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
293 printf("%-*s", vent->width, buf);
297 * Normalized priority (lower is better). For pure threads
298 * output a negated LWKT priority (so lower still means better).
300 * XXX bsd4 scheduler specific.
303 pri(const KINFO *k, const struct varent *vent)
305 if (KI_LWP(k, pid) != -1)
306 printf("%*d", vent->width, KI_LWP(k, prio));
308 printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
312 tdpri(const KINFO *k, const struct varent *vent)
315 int val = KI_LWP(k, tdprio);
317 snprintf(buf, sizeof(buf), "%2d", val);
318 printf("%*s", vent->width, buf);
322 uname(const KINFO *k, const struct varent *vent)
324 printf("%-*s", vent->width,
325 user_from_uid(KI_PROC(k, uid), 0));
329 s_uname(const KINFO *k)
331 return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
335 runame(const KINFO *k, const struct varent *vent)
337 printf("%-*s", vent->width,
338 user_from_uid(KI_PROC(k, ruid), 0));
342 s_runame(const KINFO *k)
344 return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
348 tdev(const KINFO *k, const struct varent *vent)
353 dev = KI_PROC(k, tdev);
355 printf("%*s", vent->width, "??");
357 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
358 printf("%*s", vent->width, buff);
363 tname(const KINFO *k, const struct varent *vent)
368 dev = KI_PROC(k, tdev);
369 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
370 printf("%*s ", vent->width-1, "??");
372 if (strncmp(ttname, "tty", 3) == 0 ||
373 strncmp(ttname, "cua", 3) == 0)
375 if (strncmp(ttname, "pts/", 4) == 0)
377 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
378 KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
383 longtname(const KINFO *k, const struct varent *vent)
388 dev = KI_PROC(k, tdev);
389 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
390 printf("%-*s", vent->width, "??");
392 printf("%-*s", vent->width, ttname);
396 started(const KINFO *k, const struct varent *vent)
402 static int use_ampm = -1;
405 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
407 then = KI_PROC(k, start).tv_sec;
408 if (then < btime.tv_sec) {
412 tp = localtime(&then);
415 if (now - then < 24 * 3600) {
416 strftime(buf, sizeof(buf) - 1,
417 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
418 } else if (now - then < 7 * 86400) {
419 strftime(buf, sizeof(buf) - 1,
420 use_ampm ? "%a%I%p" : "%a%H ", tp);
422 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
423 printf("%-*s", vent->width, buf);
427 lstarted(const KINFO *k, const struct varent *vent)
432 then = KI_PROC(k, start).tv_sec;
433 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
434 printf("%-*s", vent->width, buf);
438 wchan(const KINFO *k, const struct varent *vent)
440 if (*KI_LWP(k, wmesg)) {
441 printf("%-*.*s", vent->width, vent->width,
444 printf("%-*s", vent->width, "-");
451 static size_t pgsize;
454 pgsize = getpagesize();
459 vsize(const KINFO *k, const struct varent *vent)
461 put64((uintmax_t)KI_PROC(k, vm_map_size)/1024, vent->width, 'k');
465 rssize(const KINFO *k, const struct varent *vent)
467 /* XXX don't have info about shared */
468 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
471 /* doesn't account for text */
473 p_rssize(const KINFO *k, const struct varent *vent)
475 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
479 cputime(const KINFO *k, const struct varent *vent)
482 long psecs; /* "parts" of a second. first micro, then centi */
485 static char decimal_point = '\0';
487 if (decimal_point == '\0')
488 decimal_point = localeconv()->decimal_point[0];
491 * This counts time spent handling interrupts. We could
492 * fix this, but it is not 100% trivial (and interrupt
493 * time fractions only work on the sparc anyway). XXX
495 timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
497 secs = timeus / 1000000;
498 psecs = timeus % 1000000;
500 secs += KI_PROC(k, cru).ru_utime.tv_sec +
501 KI_PROC(k, cru).ru_stime.tv_sec;
502 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
503 KI_PROC(k, cru).ru_stime.tv_usec;
506 * round and scale to 100's
508 psecs = (psecs + 5000) / 10000;
513 snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
514 secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
515 } else if (secs >= 100 * 60) {
516 snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
517 secs / 60 / 60, secs / 60 % 60, secs % 60);
521 snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
522 secs / 60, secs % 60,
523 decimal_point, psecs);
525 printf("%*s", vent->width, obuff);
529 getpcpu(const KINFO *k)
538 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
540 /* XXX - I don't like this */
541 if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
543 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
547 pcpu(const KINFO *k, const struct varent *vent)
549 printf("%*.1f", vent->width, getpcpu(k));
553 pnice(const KINFO *k, const struct varent *vent)
557 switch (KI_LWP(k, rtprio).type) {
558 case RTP_PRIO_REALTIME:
559 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
562 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
564 case RTP_PRIO_THREAD:
565 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
568 niceval = KI_PROC(k, nice) - NZERO;
571 printf("%*d", vent->width, niceval);
576 getpmem(const KINFO *k)
587 if (KI_PROC(k, flags) & P_SWAPPEDOUT)
589 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
591 /* XXX don't have info about shared */
592 fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
593 return (100.0 * fracmem);
597 pmem(const KINFO *k, const struct varent *vent)
599 printf("%*.1f", vent->width, getpmem(k));
603 pagein(const KINFO *k, const struct varent *vent)
605 printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
610 maxrss(const KINFO *k __unused, const struct varent *vent)
612 printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
616 tsize(const KINFO *k, const struct varent *vent)
618 put64(pgtob(KI_PROC(k, vm_tsize))/1024, vent->width, 'k');
622 rtprior(const KINFO *k, const struct varent *vent)
628 prtp = &KI_LWP(k, rtprio);
632 case RTP_PRIO_REALTIME:
633 snprintf(str, sizeof(str), "real:%u", prio);
635 case RTP_PRIO_NORMAL:
636 strncpy(str, "normal", sizeof(str));
639 snprintf(str, sizeof(str), "idle:%u", prio);
642 snprintf(str, sizeof(str), "%u:%u", type, prio);
645 str[sizeof(str) - 1] = '\0';
646 printf("%*s", vent->width, str);
650 * Generic output routines. Print fields from various prototype
654 printval(const char *bp, const struct varent *vent)
656 static char ofmt[32] = "%";
661 fcp = vent->var->fmt;
662 if (vent->var->flag & LJUST)
665 while ((*cp++ = *fcp++));
667 switch (vent->var->type) {
669 printf(ofmt, vent->width, *(const char *)bp);
672 printf(ofmt, vent->width, *(const u_char *)bp);
675 printf(ofmt, vent->width, *(const short *)bp);
678 printf(ofmt, vent->width, *(const u_short *)bp);
681 printf(ofmt, vent->width, *(const int *)bp);
684 printf(ofmt, vent->width, *(const u_int *)bp);
687 printf(ofmt, vent->width, *(const long *)bp);
690 printf(ofmt, vent->width, *(const u_long *)bp);
693 printf(ofmt, vent->width, *(const u_long *)bp);
696 errx(1, "unknown type %d", vent->var->type);
701 pvar(const KINFO *k, const struct varent *vent)
703 printval((char *)((char *)k->ki_proc + vent->var->off), vent);
707 lpest(const KINFO *k, const struct varent *vent)
711 val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
713 printval((char *)&val, vent);
718 lpvar(const KINFO *k, const struct varent *vent)
720 printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
724 rvar(const KINFO *k, const struct varent *vent)
726 printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
730 make_printable(const char *str)
738 if ((cpy = malloc(len * 4 + 1)) == NULL)
740 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
745 make_printable2(const char *str)
753 if ((cpy2 = malloc(len * 4 + 1)) == NULL)
755 strvis(cpy2, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
760 * Output a number, divide down as needed to fit within the
761 * field. This function differs from the code used by systat
762 * in that it tries to differentiate the display by always
763 * using a decimal point for excessively large numbers so
764 * the human eye naturally notices the difference.
767 put64(u_int64_t n, int w, int type)
775 snprintf(b, sizeof(b), "%*jd", w, (uintmax_t)n);
777 if (len <= (size_t)w) {
778 fwrite(b, len, 1, stdout);
788 for (d = 1; n / d >= 100000; d *= u) {
816 if (w > 4 && n / d >= u) {
817 snprintf(b, sizeof(b), "%*ju.%02u%c",
819 (uintmax_t)(n / (d * u)),
820 (u_int)(n / (d * u / 100) % 100),
823 snprintf(b, sizeof(b), "%*jd%c",
824 w - 1, (uintmax_t)n / d, type);
827 fwrite(b, len, 1, stdout);