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
)
285 /* removed, no longer interesting */
286 if ((flag
& P_SYSTEM
) || KI_PROC(k
, lock
) > 0)
291 if (KI_PROC(k
, auxflags
) & KI_SLEADER
)
293 if ((flag
& P_CONTROLT
) && KI_PROC(k
, pgid
) == KI_PROC(k
, tpgid
))
296 printf("%-*s", vent
->width
, buf
);
300 * Normalized priority (lower is better). For pure threads
301 * output a negated LWKT priority (so lower still means better).
303 * XXX bsd4 scheduler specific.
306 pri(const KINFO
*k
, const struct varent
*vent
)
308 if (KI_LWP(k
, pid
) != -1)
309 printf("%*d", vent
->width
, KI_LWP(k
, prio
));
311 printf("%*d", vent
->width
, -(KI_LWP(k
, tdprio
)));
315 tdpri(const KINFO
*k
, const struct varent
*vent
)
318 int val
= KI_LWP(k
, tdprio
);
320 snprintf(buf
, sizeof(buf
), "%2d", val
);
321 printf("%*s", vent
->width
, buf
);
325 uname(const KINFO
*k
, const struct varent
*vent
)
327 printf("%-*s", vent
->width
,
328 user_from_uid(KI_PROC(k
, uid
), 0));
332 s_uname(const KINFO
*k
)
334 return (strlen(user_from_uid(KI_PROC(k
, uid
), 0)));
338 runame(const KINFO
*k
, const struct varent
*vent
)
340 printf("%-*s", vent
->width
,
341 user_from_uid(KI_PROC(k
, ruid
), 0));
345 s_runame(const KINFO
*k
)
347 return (strlen(user_from_uid(KI_PROC(k
, ruid
), 0)));
351 tdev(const KINFO
*k
, const struct varent
*vent
)
356 dev
= KI_PROC(k
, tdev
);
358 printf("%*s", vent
->width
, "??");
360 snprintf(buff
, sizeof(buff
), "%d/%d", major(dev
), minor(dev
));
361 printf("%*s", vent
->width
, buff
);
366 tname(const KINFO
*k
, const struct varent
*vent
)
371 dev
= KI_PROC(k
, tdev
);
372 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
)
373 printf("%*s ", vent
->width
-1, "??");
375 if (strncmp(ttname
, "tty", 3) == 0 ||
376 strncmp(ttname
, "cua", 3) == 0)
378 if (strncmp(ttname
, "pts/", 4) == 0)
380 printf("%*.*s%c", vent
->width
-1, vent
->width
-1, ttname
,
381 KI_PROC(k
, auxflags
) & KI_CTTY
? ' ' : '-');
386 longtname(const KINFO
*k
, const struct varent
*vent
)
391 dev
= KI_PROC(k
, tdev
);
392 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
)
393 printf("%-*s", vent
->width
, "??");
395 printf("%-*s", vent
->width
, ttname
);
399 started(const KINFO
*k
, const struct varent
*vent
)
405 static int use_ampm
= -1;
408 use_ampm
= (*nl_langinfo(T_FMT_AMPM
) != '\0');
410 then
= KI_PROC(k
, start
).tv_sec
;
411 if (then
< btime
.tv_sec
) {
415 tp
= localtime(&then
);
418 if (now
- then
< 24 * 3600) {
419 strftime(buf
, sizeof(buf
) - 1,
420 use_ampm
? "%l:%M%p" : "%k:%M ", tp
);
421 } else if (now
- then
< 7 * 86400) {
422 strftime(buf
, sizeof(buf
) - 1,
423 use_ampm
? "%a%I%p" : "%a%H ", tp
);
425 strftime(buf
, sizeof(buf
) - 1, "%e%b%y", tp
);
426 printf("%-*s", vent
->width
, buf
);
430 lstarted(const KINFO
*k
, const struct varent
*vent
)
435 then
= KI_PROC(k
, start
).tv_sec
;
436 strftime(buf
, sizeof(buf
) -1, "%c", localtime(&then
));
437 printf("%-*s", vent
->width
, buf
);
441 wchan(const KINFO
*k
, const struct varent
*vent
)
443 if (*KI_LWP(k
, wmesg
)) {
444 printf("%-*.*s", vent
->width
, vent
->width
,
447 printf("%-*s", vent
->width
, "-");
454 static size_t pgsize
;
457 pgsize
= getpagesize();
462 vsize(const KINFO
*k
, const struct varent
*vent
)
464 put64((uintmax_t)KI_PROC(k
, vm_map_size
)/1024, vent
->width
, 'k');
468 rssize(const KINFO
*k
, const struct varent
*vent
)
470 /* XXX don't have info about shared */
471 put64(pgtob(KI_PROC(k
, vm_rssize
))/1024, vent
->width
, 'k');
474 /* doesn't account for text */
476 p_rssize(const KINFO
*k
, const struct varent
*vent
)
478 put64(pgtob(KI_PROC(k
, vm_rssize
))/1024, vent
->width
, 'k');
482 cputime(const KINFO
*k
, const struct varent
*vent
)
485 long psecs
; /* "parts" of a second. first micro, then centi */
488 static char decimal_point
= '\0';
490 if (decimal_point
== '\0')
491 decimal_point
= localeconv()->decimal_point
[0];
494 * This counts time spent handling interrupts. We could
495 * fix this, but it is not 100% trivial (and interrupt
496 * time fractions only work on the sparc anyway). XXX
498 timeus
= KI_LWP(k
, uticks
) + KI_LWP(k
, sticks
) +
500 secs
= timeus
/ 1000000;
501 psecs
= timeus
% 1000000;
503 secs
+= KI_PROC(k
, cru
).ru_utime
.tv_sec
+
504 KI_PROC(k
, cru
).ru_stime
.tv_sec
;
505 psecs
+= KI_PROC(k
, cru
).ru_utime
.tv_usec
+
506 KI_PROC(k
, cru
).ru_stime
.tv_usec
;
509 * round and scale to 100's
511 psecs
= (psecs
+ 5000) / 10000;
516 snprintf(obuff
, sizeof(obuff
), "%3ldd%02ld:%02ld",
517 secs
/ 86400, secs
/ (60 * 60) % 24, secs
/ 60 % 60);
518 } else if (secs
>= 100 * 60) {
519 snprintf(obuff
, sizeof(obuff
), "%2ld:%02ld:%02ld",
520 secs
/ 60 / 60, secs
/ 60 % 60, secs
% 60);
524 snprintf(obuff
, sizeof(obuff
), "%3ld:%02ld%c%02ld",
525 secs
/ 60, secs
% 60,
526 decimal_point
, psecs
);
528 printf("%*s", vent
->width
, obuff
);
532 getpcpu(const KINFO
*k
)
541 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
543 /* XXX - I don't like this */
544 if (KI_PROC(k
, swtime
) == 0 || (KI_PROC(k
, flags
) & P_SWAPPEDOUT
))
546 return (100.0 * fxtofl(KI_LWP(k
, pctcpu
)));
550 pcpu(const KINFO
*k
, const struct varent
*vent
)
552 printf("%*.1f", vent
->width
, getpcpu(k
));
556 pnice(const KINFO
*k
, const struct varent
*vent
)
560 switch (KI_LWP(k
, rtprio
).type
) {
561 case RTP_PRIO_REALTIME
:
562 niceval
= PRIO_MIN
- 1 - RTP_PRIO_MAX
+ KI_LWP(k
, rtprio
).prio
;
565 niceval
= PRIO_MAX
+ 1 + KI_LWP(k
, rtprio
).prio
;
567 case RTP_PRIO_THREAD
:
568 niceval
= PRIO_MIN
- 1 - RTP_PRIO_MAX
- KI_LWP(k
, rtprio
).prio
;
571 niceval
= KI_PROC(k
, nice
) - NZERO
;
574 printf("%*d", vent
->width
, niceval
);
579 getpmem(const KINFO
*k
)
590 if (KI_PROC(k
, flags
) & P_SWAPPEDOUT
)
592 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
594 /* XXX don't have info about shared */
595 fracmem
= ((float)KI_PROC(k
, vm_rssize
) + szptudot
)/mempages
;
596 return (100.0 * fracmem
);
600 pmem(const KINFO
*k
, const struct varent
*vent
)
602 printf("%*.1f", vent
->width
, getpmem(k
));
606 pagein(const KINFO
*k
, const struct varent
*vent
)
608 printf("%*ld", vent
->width
, KI_LWP(k
, ru
).ru_majflt
);
613 maxrss(const KINFO
*k __unused
, const struct varent
*vent
)
615 printf("%*ld", vent
->width
, KI_PROC(k
, ru
).ru_maxrss
);
619 tsize(const KINFO
*k
, const struct varent
*vent
)
621 put64(pgtob(KI_PROC(k
, vm_tsize
))/1024, vent
->width
, 'k');
625 rtprior(const KINFO
*k
, const struct varent
*vent
)
631 prtp
= &KI_LWP(k
, rtprio
);
635 case RTP_PRIO_REALTIME
:
636 snprintf(str
, sizeof(str
), "real:%u", prio
);
638 case RTP_PRIO_NORMAL
:
639 strncpy(str
, "normal", sizeof(str
));
642 snprintf(str
, sizeof(str
), "idle:%u", prio
);
645 snprintf(str
, sizeof(str
), "%u:%u", type
, prio
);
648 str
[sizeof(str
) - 1] = '\0';
649 printf("%*s", vent
->width
, str
);
653 * Generic output routines. Print fields from various prototype
657 printval(const char *bp
, const struct varent
*vent
)
659 static char ofmt
[32] = "%";
664 fcp
= vent
->var
->fmt
;
665 if (vent
->var
->flag
& LJUST
)
668 while ((*cp
++ = *fcp
++));
670 switch (vent
->var
->type
) {
672 printf(ofmt
, vent
->width
, *(const char *)bp
);
675 printf(ofmt
, vent
->width
, *(const u_char
*)bp
);
678 printf(ofmt
, vent
->width
, *(const short *)bp
);
681 printf(ofmt
, vent
->width
, *(const u_short
*)bp
);
684 printf(ofmt
, vent
->width
, *(const int *)bp
);
687 printf(ofmt
, vent
->width
, *(const u_int
*)bp
);
690 printf(ofmt
, vent
->width
, *(const long *)bp
);
693 printf(ofmt
, vent
->width
, *(const u_long
*)bp
);
696 printf(ofmt
, vent
->width
, *(const u_long
*)bp
);
699 errx(1, "unknown type %d", vent
->var
->type
);
704 pvar(const KINFO
*k
, const struct varent
*vent
)
706 printval((char *)((char *)k
->ki_proc
+ vent
->var
->off
), vent
);
710 lpest(const KINFO
*k
, const struct varent
*vent
)
714 val
= *(int *)((char *)&k
->ki_proc
->kp_lwp
+ vent
->var
->off
);
716 printval((char *)&val
, vent
);
721 lpvar(const KINFO
*k
, const struct varent
*vent
)
723 printval((char *)((char *)&k
->ki_proc
->kp_lwp
+ vent
->var
->off
), vent
);
727 rvar(const KINFO
*k
, const struct varent
*vent
)
729 printval(((const char *)&KI_LWP(k
, ru
) + vent
->var
->off
), vent
);
733 make_printable(const char *str
)
741 if ((cpy
= malloc(len
* 4 + 1)) == NULL
)
743 strvis(cpy
, str
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);
748 make_printable2(const char *str
)
756 if ((cpy2
= malloc(len
* 4 + 1)) == NULL
)
758 strvis(cpy2
, str
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);
763 * Output a number, divide down as needed to fit within the
764 * field. This function differs from the code used by systat
765 * in that it tries to differentiate the display by always
766 * using a decimal point for excessively large numbers so
767 * the human eye naturally notices the difference.
770 put64(u_int64_t n
, int w
, int type
)
778 snprintf(b
, sizeof(b
), "%*jd", w
, (uintmax_t)n
);
780 if (len
<= (size_t)w
) {
781 fwrite(b
, len
, 1, stdout
);
791 for (d
= 1; n
/ d
>= 100000; d
*= u
) {
819 if (w
> 4 && n
/ d
>= u
) {
820 snprintf(b
, sizeof(b
), "%*ju.%02u%c",
822 (uintmax_t)(n
/ (d
* u
)),
823 (u_int
)(n
/ (d
* u
/ 100) % 100),
826 snprintf(b
, sizeof(b
), "%*jd%c",
827 w
- 1, (uintmax_t)n
/ d
, type
);
830 fwrite(b
, len
, 1, stdout
);