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 * 4. 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 void put64(u_int64_t n, int w, int type);
71 STAILQ_FOREACH(vent, &var_head, link) {
72 if (*vent->header != '\0') {
79 STAILQ_FOREACH(vent, &var_head, link) {
81 if (v->flag & LJUST) {
82 if (STAILQ_NEXT(vent, link) == NULL) /* last one */
83 printf("%s", vent->header);
85 printf("%-*s", vent->width, vent->header);
87 printf("%*s", vent->width, vent->header);
88 if (STAILQ_NEXT(vent, link) != NULL)
95 command(const KINFO *k, const struct varent *vent)
99 char *cp, *vis_env, *vis_args;
102 /* Don't pad the last field. */
103 if (STAILQ_NEXT(vent, link) == NULL)
104 printf("%s", make_printable(KI_PROC(k, comm)));
106 printf("%-*s", vent->width,
107 make_printable(KI_PROC(k, comm)));
111 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
113 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
115 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
117 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
122 indent = k->ki_indent;
126 if (STAILQ_NEXT(vent, link) == NULL) {
128 if (termwidth == UNLIMITED) {
130 printf("%s ", vis_env);
135 printf("%s", vis_args);
137 left = termwidth - (totwidth - vent->width);
138 if (left < 1) /* already wrapped, just use std width */
140 while (indent && left > 1) {
145 if ((cp = vis_env) != NULL) {
146 while (--left >= 0 && *cp)
151 for (cp = vis_args; --left >= 0 && *cp != '\0';)
156 printf("%-*.*s", vent->width, vent->width, vis_args);
163 ucomm(const KINFO *k, const struct varent *vent)
165 /* Do not pad the last field */
166 if (STAILQ_NEXT(vent, link) == NULL)
167 printf("%s", make_printable(KI_PROC(k, comm)));
169 printf("%-*s", vent->width, make_printable(KI_PROC(k, comm)));
173 logname(const KINFO *k, const struct varent *vent)
175 const char *s = KI_PROC(k, login);
177 printf("%-*s", vent->width, *s != '\0' ? s : "-");
181 state(const KINFO *k, const struct varent *vent)
187 flag = KI_PROC(k, flags);
190 switch (KI_PROC(k, stat)) {
197 switch (KI_LWP(k, stat)) {
199 if (KI_LWP(k, flags) & LWP_SINTR) {
200 /* interruptable wait short/long */
201 *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
203 else if (KI_LWP(k, tdflags) & TDF_SINTR)
204 *cp = 'S'; /* interruptable lwkt wait */
205 else if (KI_PROC(k, paddr))
206 *cp = 'D'; /* uninterruptable wait */
208 *cp = 'B'; /* uninterruptable lwkt wait */
212 if (KI_LWP(k, stat) == LSRUN) {
214 if (!(KI_LWP(k, tdflags) &
215 (TDF_RUNNING | TDF_RUNQ)))
218 /*if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ))*/ {
220 sprintf(cp, "%d", KI_LWP(k, cpuid));
227 /* shouldn't happen anyways */
242 if (flag & P_SWAPPEDOUT)
244 if (KI_PROC(k, nice) < NZERO)
246 else if (KI_PROC(k, nice) > NZERO)
250 if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
254 if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
256 if (numcpus > 1 && KI_LWP(k, mpcount) == 0)
260 if (KI_PROC(k, auxflags) & KI_SLEADER)
262 if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
265 printf("%-*s", vent->width, buf);
269 * Normalized priority (lower is better). For pure threads
270 * output a negated LWKT priority (so lower still means better).
272 * XXX bsd4 scheduler specific.
275 pri(const KINFO *k, const struct varent *vent)
277 if (KI_LWP(k, pid) != -1)
278 printf("%*d", vent->width, KI_LWP(k, prio));
280 printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
284 tdpri(const KINFO *k, const struct varent *vent)
287 int val = KI_LWP(k, tdprio);
289 snprintf(buf, sizeof(buf), "%2d", val);
290 printf("%*s", vent->width, buf);
294 uname(const KINFO *k, const struct varent *vent)
296 printf("%-*s", vent->width,
297 user_from_uid(KI_PROC(k, uid), 0));
301 s_uname(const KINFO *k)
303 return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
307 runame(const KINFO *k, const struct varent *vent)
309 printf("%-*s", vent->width,
310 user_from_uid(KI_PROC(k, ruid), 0));
314 s_runame(const KINFO *k)
316 return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
320 tdev(const KINFO *k, const struct varent *vent)
325 dev = KI_PROC(k, tdev);
327 printf("%*s", vent->width, "??");
329 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
330 printf("%*s", vent->width, buff);
335 tname(const KINFO *k, const struct varent *vent)
340 dev = KI_PROC(k, tdev);
341 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
342 printf("%*s ", vent->width-1, "??");
344 if (strncmp(ttname, "tty", 3) == 0 ||
345 strncmp(ttname, "cua", 3) == 0)
347 if (strncmp(ttname, "pts/", 4) == 0)
349 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
350 KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
355 longtname(const KINFO *k, const struct varent *vent)
360 dev = KI_PROC(k, tdev);
361 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
362 printf("%-*s", vent->width, "??");
364 printf("%-*s", vent->width, ttname);
368 started(const KINFO *k, const struct varent *vent)
374 static int use_ampm = -1;
377 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
379 then = KI_PROC(k, start).tv_sec;
380 if (then < btime.tv_sec) {
384 tp = localtime(&then);
387 if (now - then < 24 * 3600) {
388 strftime(buf, sizeof(buf) - 1,
389 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
390 } else if (now - then < 7 * 86400) {
391 strftime(buf, sizeof(buf) - 1,
392 use_ampm ? "%a%I%p" : "%a%H ", tp);
394 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
395 printf("%-*s", vent->width, buf);
399 lstarted(const KINFO *k, const struct varent *vent)
404 then = KI_PROC(k, start).tv_sec;
405 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
406 printf("%-*s", vent->width, buf);
410 wchan(const KINFO *k, const struct varent *vent)
412 if (*KI_LWP(k, wmesg)) {
413 printf("%-*.*s", vent->width, vent->width,
416 printf("%-*s", vent->width, "-");
423 static size_t pgsize;
426 pgsize = getpagesize();
431 vsize(const KINFO *k, const struct varent *vent)
433 put64((uintmax_t)KI_PROC(k, vm_map_size)/1024, vent->width, 'k');
437 rssize(const KINFO *k, const struct varent *vent)
439 /* XXX don't have info about shared */
440 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
443 /* doesn't account for text */
445 p_rssize(const KINFO *k, const struct varent *vent)
447 put64(pgtob(KI_PROC(k, vm_rssize))/1024, vent->width, 'k');
451 cputime(const KINFO *k, const struct varent *vent)
454 long psecs; /* "parts" of a second. first micro, then centi */
457 static char decimal_point = '\0';
459 if (decimal_point == '\0')
460 decimal_point = localeconv()->decimal_point[0];
463 * This counts time spent handling interrupts. We could
464 * fix this, but it is not 100% trivial (and interrupt
465 * time fractions only work on the sparc anyway). XXX
467 timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
469 secs = timeus / 1000000;
470 psecs = timeus % 1000000;
472 secs += KI_PROC(k, cru).ru_utime.tv_sec +
473 KI_PROC(k, cru).ru_stime.tv_sec;
474 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
475 KI_PROC(k, cru).ru_stime.tv_usec;
478 * round and scale to 100's
480 psecs = (psecs + 5000) / 10000;
485 snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
486 secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
487 } else if (secs >= 100 * 60) {
488 snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
489 secs / 60 / 60, secs / 60 % 60, secs % 60);
493 snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
494 secs / 60, secs % 60,
495 decimal_point, psecs);
497 printf("%*s", vent->width, obuff);
501 getpcpu(const KINFO *k)
510 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
512 /* XXX - I don't like this */
513 if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
515 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
519 pcpu(const KINFO *k, const struct varent *vent)
521 printf("%*.1f", vent->width, getpcpu(k));
525 pnice(const KINFO *k, const struct varent *vent)
529 switch (KI_LWP(k, rtprio).type) {
530 case RTP_PRIO_REALTIME:
531 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
534 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
536 case RTP_PRIO_THREAD:
537 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
540 niceval = KI_PROC(k, nice) - NZERO;
543 printf("%*d", vent->width, niceval);
548 getpmem(const KINFO *k)
559 if (KI_PROC(k, flags) & P_SWAPPEDOUT)
561 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
563 /* XXX don't have info about shared */
564 fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
565 return (100.0 * fracmem);
569 pmem(const KINFO *k, const struct varent *vent)
571 printf("%*.1f", vent->width, getpmem(k));
575 pagein(const KINFO *k, const struct varent *vent)
577 printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
582 maxrss(const KINFO *k __unused, const struct varent *vent)
584 printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
588 tsize(const KINFO *k, const struct varent *vent)
590 put64(pgtob(KI_PROC(k, vm_tsize))/1024, vent->width, 'k');
594 rtprior(const KINFO *k, const struct varent *vent)
600 prtp = &KI_LWP(k, rtprio);
604 case RTP_PRIO_REALTIME:
605 snprintf(str, sizeof(str), "real:%u", prio);
607 case RTP_PRIO_NORMAL:
608 strncpy(str, "normal", sizeof(str));
611 snprintf(str, sizeof(str), "idle:%u", prio);
614 snprintf(str, sizeof(str), "%u:%u", type, prio);
617 str[sizeof(str) - 1] = '\0';
618 printf("%*s", vent->width, str);
622 * Generic output routines. Print fields from various prototype
626 printval(const char *bp, const struct varent *vent)
628 static char ofmt[32] = "%";
633 fcp = vent->var->fmt;
634 if (vent->var->flag & LJUST)
637 while ((*cp++ = *fcp++));
639 switch (vent->var->type) {
641 printf(ofmt, vent->width, *(const char *)bp);
644 printf(ofmt, vent->width, *(const u_char *)bp);
647 printf(ofmt, vent->width, *(const short *)bp);
650 printf(ofmt, vent->width, *(const u_short *)bp);
653 printf(ofmt, vent->width, *(const int *)bp);
656 printf(ofmt, vent->width, *(const u_int *)bp);
659 printf(ofmt, vent->width, *(const long *)bp);
662 printf(ofmt, vent->width, *(const u_long *)bp);
665 printf(ofmt, vent->width, *(const u_long *)bp);
668 errx(1, "unknown type %d", vent->var->type);
673 pvar(const KINFO *k, const struct varent *vent)
675 printval((char *)((char *)k->ki_proc + vent->var->off), vent);
679 lpest(const KINFO *k, const struct varent *vent)
683 val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
685 printval((char *)&val, vent);
690 lpvar(const KINFO *k, const struct varent *vent)
692 printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
696 rvar(const KINFO *k, const struct varent *vent)
698 printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
702 make_printable(const char *str)
710 if ((cpy = malloc(len * 4 + 1)) == NULL)
712 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
717 * Output a number, divide down as needed to fit within the
718 * field. This function differs from the code used by systat
719 * in that it tries to differentiate the display by always
720 * using a decimal point for excessively large numbers so
721 * the human eye naturally notices the difference.
724 put64(u_int64_t n, int w, int type)
732 snprintf(b, sizeof(b), "%*jd", w, (uintmax_t)n);
734 if (len <= (size_t)w) {
735 fwrite(b, len, 1, stdout);
745 for (d = 1; n / d >= 100000; d *= u) {
773 if (w > 4 && n / d >= u) {
774 snprintf(b, sizeof(b), "%*ju.%02u%c",
776 (uintmax_t)(n / (d * u)),
777 (u_int)(n / (d * u / 100) % 100),
780 snprintf(b, sizeof(b), "%*jd%c",
781 w - 1, (uintmax_t)n / d, type);
784 fwrite(b, len, 1, stdout);