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 $
38 #include <sys/param.h>
40 #include <sys/resource.h>
43 #include <sys/ucred.h>
45 #include <sys/sysctl.h>
46 #include <sys/rtprio.h>
64 static const char *make_printable(const char *str
);
74 STAILQ_FOREACH(vent
, &var_head
, link
) {
75 if (*vent
->header
!= '\0') {
82 STAILQ_FOREACH(vent
, &var_head
, link
) {
84 if (v
->flag
& LJUST
) {
85 if (STAILQ_NEXT(vent
, link
) == NULL
) /* last one */
86 printf("%s", vent
->header
);
88 printf("%-*s", vent
->width
, vent
->header
);
90 printf("%*s", vent
->width
, vent
->header
);
91 if (STAILQ_NEXT(vent
, link
) != NULL
)
98 command(const KINFO
*k
, const struct varent
*vent
)
102 char *cp
, *vis_env
, *vis_args
;
107 /* Don't pad the last field. */
108 if (STAILQ_NEXT(vent
, link
) == NULL
)
109 printf("%s", make_printable(KI_PROC(k
, comm
)));
111 printf("%-*s", vent
->width
,
112 make_printable(KI_PROC(k
, comm
)));
116 if ((vis_args
= malloc(strlen(k
->ki_args
) * 4 + 1)) == NULL
)
118 strvis(vis_args
, k
->ki_args
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);
120 if ((vis_env
= malloc(strlen(k
->ki_env
) * 4 + 1)) == NULL
)
122 strvis(vis_env
, k
->ki_env
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);
126 if (STAILQ_NEXT(vent
, link
) == NULL
) {
128 if (termwidth
== UNLIMITED
) {
130 printf("%s ", vis_env
);
131 printf("%s", vis_args
);
133 left
= termwidth
- (totwidth
- vent
->width
);
134 if (left
< 1) /* already wrapped, just use std width */
136 if ((cp
= vis_env
) != NULL
) {
137 while (--left
>= 0 && *cp
)
142 for (cp
= vis_args
; --left
>= 0 && *cp
!= '\0';)
147 printf("%-*.*s", vent
->width
, vent
->width
, vis_args
);
154 ucomm(const KINFO
*k
, const struct varent
*vent
)
156 printf("%-*s", vent
->width
, make_printable(KI_PROC(k
, comm
)));
160 logname(const KINFO
*k
, const struct varent
*vent
)
162 const char *s
= KI_PROC(k
, login
);
164 printf("%-*s", vent
->width
, *s
!= '\0' ? s
: "-");
168 state(const KINFO
*k
, const struct varent
*vent
)
174 flag
= KI_PROC(k
, flags
);
177 switch (KI_PROC(k
, stat
)) {
184 switch (KI_LWP(k
, stat
)) {
186 if (KI_LWP(k
, flags
) & LWP_SINTR
) {
187 /* interruptable wait short/long */
188 *cp
= KI_LWP(k
, slptime
) >= MAXSLP
? 'I' : 'S';
190 else if (KI_LWP(k
, tdflags
) & TDF_SINTR
)
191 *cp
= 'S'; /* interruptable lwkt wait */
192 else if (KI_PROC(k
, paddr
))
193 *cp
= 'D'; /* uninterruptable wait */
195 *cp
= 'B'; /* uninterruptable lwkt wait */
200 if (KI_LWP(k
, tdflags
) & TDF_RUNNING
) {
202 sprintf(cp
, "%d", KI_LWP(k
, cpuid
));
209 /* shouldn't happen anyways */
224 if (flag
& P_SWAPPEDOUT
)
226 if (KI_PROC(k
, nice
) < NZERO
)
228 else if (KI_PROC(k
, nice
) > NZERO
)
232 if (flag
& P_WEXIT
&& KI_PROC(k
, stat
) != SZOMB
)
236 if ((flag
& P_SYSTEM
) || KI_PROC(k
, lock
) > 0)
238 if (numcpus
> 1 && KI_LWP(k
, mpcount
) == 0)
242 if (KI_PROC(k
, auxflags
) & KI_SLEADER
)
244 if ((flag
& P_CONTROLT
) && KI_PROC(k
, pgid
) == KI_PROC(k
, tpgid
))
247 printf("%-*s", vent
->width
, buf
);
251 * Normalized priority (lower is better). For pure threads
252 * output a negated LWKT priority (so lower still means better).
254 * XXX bsd4 scheduler specific.
257 pri(const KINFO
*k
, const struct varent
*vent
)
259 if (KI_LWP(k
, pid
) != -1)
260 printf("%*d", vent
->width
, KI_LWP(k
, prio
));
262 printf("%*d", vent
->width
, -(KI_LWP(k
, tdprio
) & TDPRI_MASK
));
266 tdpri(const KINFO
*k
, const struct varent
*vent
)
269 int val
= KI_LWP(k
, tdprio
);
271 snprintf(buf
, sizeof(buf
), "%02d/%d", val
& TDPRI_MASK
, val
/ TDPRI_CRIT
);
272 printf("%*s", vent
->width
, buf
);
276 uname(const KINFO
*k
, const struct varent
*vent
)
278 printf("%-*s", vent
->width
,
279 user_from_uid(KI_PROC(k
, uid
), 0));
283 s_uname(const KINFO
*k
)
285 return (strlen(user_from_uid(KI_PROC(k
, uid
), 0)));
289 runame(const KINFO
*k
, const struct varent
*vent
)
291 printf("%-*s", vent
->width
,
292 user_from_uid(KI_PROC(k
, ruid
), 0));
296 s_runame(const KINFO
*k
)
298 return (strlen(user_from_uid(KI_PROC(k
, ruid
), 0)));
302 tdev(const KINFO
*k
, const struct varent
*vent
)
307 dev
= KI_PROC(k
, tdev
);
309 printf("%*s", vent
->width
, "??");
311 snprintf(buff
, sizeof(buff
), "%d/%d", major(dev
), minor(dev
));
312 printf("%*s", vent
->width
, buff
);
317 tname(const KINFO
*k
, const struct varent
*vent
)
322 dev
= KI_PROC(k
, tdev
);
323 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
)
324 printf("%*s ", vent
->width
-1, "??");
326 if (strncmp(ttname
, "tty", 3) == 0 ||
327 strncmp(ttname
, "cua", 3) == 0)
329 printf("%*.*s%c", vent
->width
-1, vent
->width
-1, ttname
,
330 KI_PROC(k
, auxflags
) & KI_CTTY
? ' ' : '-');
335 longtname(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
, "??");
344 printf("%-*s", vent
->width
, ttname
);
348 started(const KINFO
*k
, const struct varent
*vent
)
354 static int use_ampm
= -1;
357 use_ampm
= (*nl_langinfo(T_FMT_AMPM
) != '\0');
359 then
= KI_PROC(k
, start
).tv_sec
;
360 if (then
< btime
.tv_sec
) {
364 tp
= localtime(&then
);
367 if (now
- then
< 24 * 3600) {
368 strftime(buf
, sizeof(buf
) - 1,
369 use_ampm
? "%l:%M%p" : "%k:%M ", tp
);
370 } else if (now
- then
< 7 * 86400) {
371 strftime(buf
, sizeof(buf
) - 1,
372 use_ampm
? "%a%I%p" : "%a%H ", tp
);
374 strftime(buf
, sizeof(buf
) - 1, "%e%b%y", tp
);
375 printf("%-*s", vent
->width
, buf
);
379 lstarted(const KINFO
*k
, const struct varent
*vent
)
384 then
= KI_PROC(k
, start
).tv_sec
;
385 strftime(buf
, sizeof(buf
) -1, "%c", localtime(&then
));
386 printf("%-*s", vent
->width
, buf
);
390 wchan(const KINFO
*k
, const struct varent
*vent
)
392 if (*KI_LWP(k
, wmesg
)) {
393 printf("%-*.*s", vent
->width
, vent
->width
,
396 printf("%-*s", vent
->width
, "-");
401 #define pgtok(a) (((a)*getpagesize())/1024)
405 vsize(const KINFO
*k
, const struct varent
*vent
)
407 printf("%*ju", vent
->width
, (uintmax_t)(KI_PROC(k
, vm_map_size
)/1024));
411 rssize(const KINFO
*k
, const struct varent
*vent
)
413 /* XXX don't have info about shared */
414 printf("%*lu", vent
->width
, (u_long
)pgtok(KI_PROC(k
, vm_rssize
)));
418 p_rssize(const KINFO
*k
, const struct varent
*vent
) /* doesn't account for text */
420 printf("%*ld", vent
->width
, (long)pgtok(KI_PROC(k
, vm_rssize
)));
424 cputime(const KINFO
*k
, const struct varent
*vent
)
427 long psecs
; /* "parts" of a second. first micro, then centi */
430 static char decimal_point
= '\0';
432 if (decimal_point
== '\0')
433 decimal_point
= localeconv()->decimal_point
[0];
436 * This counts time spent handling interrupts. We could
437 * fix this, but it is not 100% trivial (and interrupt
438 * time fractions only work on the sparc anyway). XXX
440 timeus
= KI_LWP(k
, uticks
) + KI_LWP(k
, sticks
) +
442 secs
= timeus
/ 1000000;
443 psecs
= timeus
% 1000000;
445 secs
+= KI_PROC(k
, cru
).ru_utime
.tv_sec
+
446 KI_PROC(k
, cru
).ru_stime
.tv_sec
;
447 psecs
+= KI_PROC(k
, cru
).ru_utime
.tv_usec
+
448 KI_PROC(k
, cru
).ru_stime
.tv_usec
;
451 * round and scale to 100's
453 psecs
= (psecs
+ 5000) / 10000;
456 snprintf(obuff
, sizeof(obuff
),
457 "%3ld:%02ld%c%02ld", secs
/60, secs
%60, decimal_point
, psecs
);
458 printf("%*s", vent
->width
, obuff
);
462 getpcpu(const KINFO
*k
)
471 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
473 /* XXX - I don't like this */
474 if (KI_PROC(k
, swtime
) == 0 || (KI_PROC(k
, flags
) & P_SWAPPEDOUT
))
477 return (100.0 * fxtofl(KI_LWP(k
, pctcpu
)));
478 return (100.0 * fxtofl(KI_LWP(k
, pctcpu
)) /
479 (1.0 - exp(KI_PROC(k
, swtime
) * log(fxtofl(ccpu
)))));
483 pcpu(const KINFO
*k
, const struct varent
*vent
)
485 printf("%*.1f", vent
->width
, getpcpu(k
));
489 pnice(const KINFO
*k
, const struct varent
*vent
)
493 switch (KI_LWP(k
, rtprio
).type
) {
494 case RTP_PRIO_REALTIME
:
495 niceval
= PRIO_MIN
- 1 - RTP_PRIO_MAX
+ KI_LWP(k
, rtprio
).prio
;
498 niceval
= PRIO_MAX
+ 1 + KI_LWP(k
, rtprio
).prio
;
500 case RTP_PRIO_THREAD
:
501 niceval
= PRIO_MIN
- 1 - RTP_PRIO_MAX
- KI_LWP(k
, rtprio
).prio
;
504 niceval
= KI_PROC(k
, nice
) - NZERO
;
507 printf("%*d", vent
->width
, niceval
);
512 getpmem(const KINFO
*k
)
523 if (KI_PROC(k
, flags
) & P_SWAPPEDOUT
)
525 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
527 /* XXX don't have info about shared */
528 fracmem
= ((float)KI_PROC(k
, vm_rssize
) + szptudot
)/mempages
;
529 return (100.0 * fracmem
);
533 pmem(const KINFO
*k
, const struct varent
*vent
)
535 printf("%*.1f", vent
->width
, getpmem(k
));
539 pagein(const KINFO
*k
, const struct varent
*vent
)
541 printf("%*ld", vent
->width
, KI_LWP(k
, ru
).ru_majflt
);
546 maxrss(const KINFO
*k __unused
, const struct varent
*vent
)
548 printf("%*ld", vent
->width
, KI_PROC(k
, ru
).ru_maxrss
);
552 tsize(const KINFO
*k
, const struct varent
*vent
)
554 printf("%*ld", vent
->width
, (long)pgtok(KI_PROC(k
, vm_tsize
)));
558 rtprior(const KINFO
*k
, const struct varent
*vent
)
564 prtp
= &KI_LWP(k
, rtprio
);
568 case RTP_PRIO_REALTIME
:
569 snprintf(str
, sizeof(str
), "real:%u", prio
);
571 case RTP_PRIO_NORMAL
:
572 strncpy(str
, "normal", sizeof(str
));
575 snprintf(str
, sizeof(str
), "idle:%u", prio
);
578 snprintf(str
, sizeof(str
), "%u:%u", type
, prio
);
581 str
[sizeof(str
) - 1] = '\0';
582 printf("%*s", vent
->width
, str
);
586 * Generic output routines. Print fields from various prototype
590 printval(const char *bp
, const struct varent
*vent
)
592 static char ofmt
[32] = "%";
597 fcp
= vent
->var
->fmt
;
598 if (vent
->var
->flag
& LJUST
)
601 while ((*cp
++ = *fcp
++));
603 switch (vent
->var
->type
) {
605 printf(ofmt
, vent
->width
, *(const char *)bp
);
608 printf(ofmt
, vent
->width
, *(const u_char
*)bp
);
611 printf(ofmt
, vent
->width
, *(const short *)bp
);
614 printf(ofmt
, vent
->width
, *(const u_short
*)bp
);
617 printf(ofmt
, vent
->width
, *(const int *)bp
);
620 printf(ofmt
, vent
->width
, *(const u_int
*)bp
);
623 printf(ofmt
, vent
->width
, *(const long *)bp
);
626 printf(ofmt
, vent
->width
, *(const u_long
*)bp
);
629 printf(ofmt
, vent
->width
, *(const u_long
*)bp
);
632 errx(1, "unknown type %d", vent
->var
->type
);
637 pvar(const KINFO
*k
, const struct varent
*vent
)
639 printval((char *)((char *)k
->ki_proc
+ vent
->var
->off
), vent
);
643 lpest(const KINFO
*k
, const struct varent
*vent
)
647 val
= *(int *)((char *)&k
->ki_proc
->kp_lwp
+ vent
->var
->off
);
649 printval((char *)&val
, vent
);
654 lpvar(const KINFO
*k
, const struct varent
*vent
)
656 printval((char *)((char *)&k
->ki_proc
->kp_lwp
+ vent
->var
->off
), vent
);
660 rvar(const KINFO
*k
, const struct varent
*vent
)
662 printval(((const char *)&KI_LWP(k
, ru
) + vent
->var
->off
), vent
);
666 make_printable(const char *str
)
674 if ((cpy
= malloc(len
* 4 + 1)) == NULL
)
676 strvis(cpy
, str
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);