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
);
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
)
101 char *cp
, *vis_env
, *vis_args
;
104 /* Don't pad the last field. */
105 if (STAILQ_NEXT(vent
, link
) == NULL
)
106 printf("%s", make_printable(KI_PROC(k
, comm
)));
108 printf("%-*s", vent
->width
,
109 make_printable(KI_PROC(k
, comm
)));
113 if ((vis_args
= malloc(strlen(k
->ki_args
) * 4 + 1)) == NULL
)
115 strvis(vis_args
, k
->ki_args
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);
117 if ((vis_env
= malloc(strlen(k
->ki_env
) * 4 + 1)) == NULL
)
119 strvis(vis_env
, k
->ki_env
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);
123 if (STAILQ_NEXT(vent
, link
) == NULL
) {
125 if (termwidth
== UNLIMITED
) {
127 printf("%s ", vis_env
);
128 printf("%s", vis_args
);
130 left
= termwidth
- (totwidth
- vent
->width
);
131 if (left
< 1) /* already wrapped, just use std width */
133 if ((cp
= vis_env
) != NULL
) {
134 while (--left
>= 0 && *cp
)
139 for (cp
= vis_args
; --left
>= 0 && *cp
!= '\0';)
144 printf("%-*.*s", vent
->width
, vent
->width
, vis_args
);
151 ucomm(const KINFO
*k
, const struct varent
*vent
)
153 printf("%-*s", vent
->width
, make_printable(KI_PROC(k
, comm
)));
157 logname(const KINFO
*k
, const struct varent
*vent
)
159 const char *s
= KI_PROC(k
, login
);
161 printf("%-*s", vent
->width
, *s
!= '\0' ? s
: "-");
165 state(const KINFO
*k
, const struct varent
*vent
)
171 flag
= KI_PROC(k
, flags
);
174 switch (KI_PROC(k
, stat
)) {
181 switch (KI_LWP(k
, stat
)) {
183 if (KI_LWP(k
, flags
) & LWP_SINTR
) {
184 /* interruptable wait short/long */
185 *cp
= KI_LWP(k
, slptime
) >= MAXSLP
? 'I' : 'S';
187 else if (KI_LWP(k
, tdflags
) & TDF_SINTR
)
188 *cp
= 'S'; /* interruptable lwkt wait */
189 else if (KI_PROC(k
, paddr
))
190 *cp
= 'D'; /* uninterruptable wait */
192 *cp
= 'B'; /* uninterruptable lwkt wait */
197 if (KI_LWP(k
, tdflags
) & TDF_RUNNING
) {
199 sprintf(cp
, "%d", KI_LWP(k
, cpuid
));
206 /* shouldn't happen anyways */
221 if (flag
& P_SWAPPEDOUT
)
223 if (KI_PROC(k
, nice
) < NZERO
)
225 else if (KI_PROC(k
, nice
) > NZERO
)
229 if (flag
& P_WEXIT
&& KI_PROC(k
, stat
) != SZOMB
)
233 if ((flag
& P_SYSTEM
) || KI_PROC(k
, lock
) > 0)
235 if (numcpus
> 1 && KI_LWP(k
, mpcount
) == 0)
239 if (KI_PROC(k
, auxflags
) & KI_SLEADER
)
241 if ((flag
& P_CONTROLT
) && KI_PROC(k
, pgid
) == KI_PROC(k
, tpgid
))
244 printf("%-*s", vent
->width
, buf
);
248 * Normalized priority (lower is better). For pure threads
249 * output a negated LWKT priority (so lower still means better).
251 * XXX bsd4 scheduler specific.
254 pri(const KINFO
*k
, const struct varent
*vent
)
256 if (KI_LWP(k
, pid
) != -1)
257 printf("%*d", vent
->width
, KI_LWP(k
, prio
));
259 printf("%*d", vent
->width
, -(KI_LWP(k
, tdprio
)));
263 tdpri(const KINFO
*k
, const struct varent
*vent
)
266 int val
= KI_LWP(k
, tdprio
);
268 snprintf(buf
, sizeof(buf
), "%2d", val
);
269 printf("%*s", vent
->width
, buf
);
273 uname(const KINFO
*k
, const struct varent
*vent
)
275 printf("%-*s", vent
->width
,
276 user_from_uid(KI_PROC(k
, uid
), 0));
280 s_uname(const KINFO
*k
)
282 return (strlen(user_from_uid(KI_PROC(k
, uid
), 0)));
286 runame(const KINFO
*k
, const struct varent
*vent
)
288 printf("%-*s", vent
->width
,
289 user_from_uid(KI_PROC(k
, ruid
), 0));
293 s_runame(const KINFO
*k
)
295 return (strlen(user_from_uid(KI_PROC(k
, ruid
), 0)));
299 tdev(const KINFO
*k
, const struct varent
*vent
)
304 dev
= KI_PROC(k
, tdev
);
306 printf("%*s", vent
->width
, "??");
308 snprintf(buff
, sizeof(buff
), "%d/%d", major(dev
), minor(dev
));
309 printf("%*s", vent
->width
, buff
);
314 tname(const KINFO
*k
, const struct varent
*vent
)
319 dev
= KI_PROC(k
, tdev
);
320 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
)
321 printf("%*s ", vent
->width
-1, "??");
323 if (strncmp(ttname
, "tty", 3) == 0 ||
324 strncmp(ttname
, "cua", 3) == 0)
326 if (strncmp(ttname
, "pts/", 4) == 0)
328 printf("%*.*s%c", vent
->width
-1, vent
->width
-1, ttname
,
329 KI_PROC(k
, auxflags
) & KI_CTTY
? ' ' : '-');
334 longtname(const KINFO
*k
, const struct varent
*vent
)
339 dev
= KI_PROC(k
, tdev
);
340 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
)
341 printf("%-*s", vent
->width
, "??");
343 printf("%-*s", vent
->width
, ttname
);
347 started(const KINFO
*k
, const struct varent
*vent
)
353 static int use_ampm
= -1;
356 use_ampm
= (*nl_langinfo(T_FMT_AMPM
) != '\0');
358 then
= KI_PROC(k
, start
).tv_sec
;
359 if (then
< btime
.tv_sec
) {
363 tp
= localtime(&then
);
366 if (now
- then
< 24 * 3600) {
367 strftime(buf
, sizeof(buf
) - 1,
368 use_ampm
? "%l:%M%p" : "%k:%M ", tp
);
369 } else if (now
- then
< 7 * 86400) {
370 strftime(buf
, sizeof(buf
) - 1,
371 use_ampm
? "%a%I%p" : "%a%H ", tp
);
373 strftime(buf
, sizeof(buf
) - 1, "%e%b%y", tp
);
374 printf("%-*s", vent
->width
, buf
);
378 lstarted(const KINFO
*k
, const struct varent
*vent
)
383 then
= KI_PROC(k
, start
).tv_sec
;
384 strftime(buf
, sizeof(buf
) -1, "%c", localtime(&then
));
385 printf("%-*s", vent
->width
, buf
);
389 wchan(const KINFO
*k
, const struct varent
*vent
)
391 if (*KI_LWP(k
, wmesg
)) {
392 printf("%-*.*s", vent
->width
, vent
->width
,
395 printf("%-*s", vent
->width
, "-");
400 #define pgtok(a) (((a)*getpagesize())/1024)
404 vsize(const KINFO
*k
, const struct varent
*vent
)
406 printf("%*ju", vent
->width
, (uintmax_t)(KI_PROC(k
, vm_map_size
)/1024));
410 rssize(const KINFO
*k
, const struct varent
*vent
)
412 /* XXX don't have info about shared */
413 printf("%*lu", vent
->width
, (u_long
)pgtok(KI_PROC(k
, vm_rssize
)));
417 p_rssize(const KINFO
*k
, const struct varent
*vent
) /* doesn't account for text */
419 printf("%*ld", vent
->width
, (long)pgtok(KI_PROC(k
, vm_rssize
)));
423 cputime(const KINFO
*k
, const struct varent
*vent
)
426 long psecs
; /* "parts" of a second. first micro, then centi */
429 static char decimal_point
= '\0';
431 if (decimal_point
== '\0')
432 decimal_point
= localeconv()->decimal_point
[0];
435 * This counts time spent handling interrupts. We could
436 * fix this, but it is not 100% trivial (and interrupt
437 * time fractions only work on the sparc anyway). XXX
439 timeus
= KI_LWP(k
, uticks
) + KI_LWP(k
, sticks
) +
441 secs
= timeus
/ 1000000;
442 psecs
= timeus
% 1000000;
444 secs
+= KI_PROC(k
, cru
).ru_utime
.tv_sec
+
445 KI_PROC(k
, cru
).ru_stime
.tv_sec
;
446 psecs
+= KI_PROC(k
, cru
).ru_utime
.tv_usec
+
447 KI_PROC(k
, cru
).ru_stime
.tv_usec
;
450 * round and scale to 100's
452 psecs
= (psecs
+ 5000) / 10000;
455 snprintf(obuff
, sizeof(obuff
),
456 "%3ld:%02ld%c%02ld", secs
/60, secs
%60, decimal_point
, psecs
);
457 printf("%*s", vent
->width
, obuff
);
461 getpcpu(const KINFO
*k
)
470 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
472 /* XXX - I don't like this */
473 if (KI_PROC(k
, swtime
) == 0 || (KI_PROC(k
, flags
) & P_SWAPPEDOUT
))
476 return (100.0 * fxtofl(KI_LWP(k
, pctcpu
)));
477 return (100.0 * fxtofl(KI_LWP(k
, pctcpu
)) /
478 (1.0 - exp(KI_PROC(k
, swtime
) * log(fxtofl(ccpu
)))));
482 pcpu(const KINFO
*k
, const struct varent
*vent
)
484 printf("%*.1f", vent
->width
, getpcpu(k
));
488 pnice(const KINFO
*k
, const struct varent
*vent
)
492 switch (KI_LWP(k
, rtprio
).type
) {
493 case RTP_PRIO_REALTIME
:
494 niceval
= PRIO_MIN
- 1 - RTP_PRIO_MAX
+ KI_LWP(k
, rtprio
).prio
;
497 niceval
= PRIO_MAX
+ 1 + KI_LWP(k
, rtprio
).prio
;
499 case RTP_PRIO_THREAD
:
500 niceval
= PRIO_MIN
- 1 - RTP_PRIO_MAX
- KI_LWP(k
, rtprio
).prio
;
503 niceval
= KI_PROC(k
, nice
) - NZERO
;
506 printf("%*d", vent
->width
, niceval
);
511 getpmem(const KINFO
*k
)
522 if (KI_PROC(k
, flags
) & P_SWAPPEDOUT
)
524 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
526 /* XXX don't have info about shared */
527 fracmem
= ((float)KI_PROC(k
, vm_rssize
) + szptudot
)/mempages
;
528 return (100.0 * fracmem
);
532 pmem(const KINFO
*k
, const struct varent
*vent
)
534 printf("%*.1f", vent
->width
, getpmem(k
));
538 pagein(const KINFO
*k
, const struct varent
*vent
)
540 printf("%*ld", vent
->width
, KI_LWP(k
, ru
).ru_majflt
);
545 maxrss(const KINFO
*k __unused
, const struct varent
*vent
)
547 printf("%*ld", vent
->width
, KI_PROC(k
, ru
).ru_maxrss
);
551 tsize(const KINFO
*k
, const struct varent
*vent
)
553 printf("%*ld", vent
->width
, (long)pgtok(KI_PROC(k
, vm_tsize
)));
557 rtprior(const KINFO
*k
, const struct varent
*vent
)
563 prtp
= &KI_LWP(k
, rtprio
);
567 case RTP_PRIO_REALTIME
:
568 snprintf(str
, sizeof(str
), "real:%u", prio
);
570 case RTP_PRIO_NORMAL
:
571 strncpy(str
, "normal", sizeof(str
));
574 snprintf(str
, sizeof(str
), "idle:%u", prio
);
577 snprintf(str
, sizeof(str
), "%u:%u", type
, prio
);
580 str
[sizeof(str
) - 1] = '\0';
581 printf("%*s", vent
->width
, str
);
585 * Generic output routines. Print fields from various prototype
589 printval(const char *bp
, const struct varent
*vent
)
591 static char ofmt
[32] = "%";
596 fcp
= vent
->var
->fmt
;
597 if (vent
->var
->flag
& LJUST
)
600 while ((*cp
++ = *fcp
++));
602 switch (vent
->var
->type
) {
604 printf(ofmt
, vent
->width
, *(const char *)bp
);
607 printf(ofmt
, vent
->width
, *(const u_char
*)bp
);
610 printf(ofmt
, vent
->width
, *(const short *)bp
);
613 printf(ofmt
, vent
->width
, *(const u_short
*)bp
);
616 printf(ofmt
, vent
->width
, *(const int *)bp
);
619 printf(ofmt
, vent
->width
, *(const u_int
*)bp
);
622 printf(ofmt
, vent
->width
, *(const long *)bp
);
625 printf(ofmt
, vent
->width
, *(const u_long
*)bp
);
628 printf(ofmt
, vent
->width
, *(const u_long
*)bp
);
631 errx(1, "unknown type %d", vent
->var
->type
);
636 pvar(const KINFO
*k
, const struct varent
*vent
)
638 printval((char *)((char *)k
->ki_proc
+ vent
->var
->off
), vent
);
642 lpest(const KINFO
*k
, const struct varent
*vent
)
646 val
= *(int *)((char *)&k
->ki_proc
->kp_lwp
+ vent
->var
->off
);
648 printval((char *)&val
, vent
);
653 lpvar(const KINFO
*k
, const struct varent
*vent
)
655 printval((char *)((char *)&k
->ki_proc
->kp_lwp
+ vent
->var
->off
), vent
);
659 rvar(const KINFO
*k
, const struct varent
*vent
)
661 printval(((const char *)&KI_LWP(k
, ru
) + vent
->var
->off
), vent
);
665 make_printable(const char *str
)
673 if ((cpy
= malloc(len
* 4 + 1)) == NULL
)
675 strvis(cpy
, str
, VIS_TAB
| VIS_NL
| VIS_NOSLASH
);