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)
102 char *cp, *vis_env, *vis_args;
105 /* Don't pad the last field. */
106 if (STAILQ_NEXT(vent, link) == NULL)
107 printf("%s", make_printable(KI_PROC(k, comm)));
109 printf("%-*s", vent->width,
110 make_printable(KI_PROC(k, comm)));
114 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
116 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
118 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
120 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
125 indent = k->ki_indent;
129 if (STAILQ_NEXT(vent, link) == NULL) {
131 if (termwidth == UNLIMITED) {
133 printf("%s ", vis_env);
138 printf("%s", vis_args);
140 left = termwidth - (totwidth - vent->width);
141 if (left < 1) /* already wrapped, just use std width */
143 while (indent && left > 1) {
148 if ((cp = vis_env) != NULL) {
149 while (--left >= 0 && *cp)
154 for (cp = vis_args; --left >= 0 && *cp != '\0';)
159 printf("%-*.*s", vent->width, vent->width, vis_args);
166 ucomm(const KINFO *k, const struct varent *vent)
168 /* Do not pad the last field */
169 if (STAILQ_NEXT(vent, link) == NULL)
170 printf("%s", make_printable(KI_PROC(k, comm)));
172 printf("%-*s", vent->width, make_printable(KI_PROC(k, comm)));
176 logname(const KINFO *k, const struct varent *vent)
178 const char *s = KI_PROC(k, login);
180 printf("%-*s", vent->width, *s != '\0' ? s : "-");
184 state(const KINFO *k, const struct varent *vent)
190 flag = KI_PROC(k, flags);
193 switch (KI_PROC(k, stat)) {
200 switch (KI_LWP(k, stat)) {
202 if (KI_LWP(k, flags) & LWP_SINTR) {
203 /* interruptable wait short/long */
204 *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
206 else if (KI_LWP(k, tdflags) & TDF_SINTR)
207 *cp = 'S'; /* interruptable lwkt wait */
208 else if (KI_PROC(k, paddr))
209 *cp = 'D'; /* uninterruptable wait */
211 *cp = 'B'; /* uninterruptable lwkt wait */
216 if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ)) {
218 sprintf(cp, "%d", KI_LWP(k, cpuid));
225 /* shouldn't happen anyways */
240 if (flag & P_SWAPPEDOUT)
242 if (KI_PROC(k, nice) < NZERO)
244 else if (KI_PROC(k, nice) > NZERO)
248 if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
252 if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
254 if (numcpus > 1 && KI_LWP(k, mpcount) == 0)
258 if (KI_PROC(k, auxflags) & KI_SLEADER)
260 if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
263 printf("%-*s", vent->width, buf);
267 * Normalized priority (lower is better). For pure threads
268 * output a negated LWKT priority (so lower still means better).
270 * XXX bsd4 scheduler specific.
273 pri(const KINFO *k, const struct varent *vent)
275 if (KI_LWP(k, pid) != -1)
276 printf("%*d", vent->width, KI_LWP(k, prio));
278 printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
282 tdpri(const KINFO *k, const struct varent *vent)
285 int val = KI_LWP(k, tdprio);
287 snprintf(buf, sizeof(buf), "%2d", val);
288 printf("%*s", vent->width, buf);
292 uname(const KINFO *k, const struct varent *vent)
294 printf("%-*s", vent->width,
295 user_from_uid(KI_PROC(k, uid), 0));
299 s_uname(const KINFO *k)
301 return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
305 runame(const KINFO *k, const struct varent *vent)
307 printf("%-*s", vent->width,
308 user_from_uid(KI_PROC(k, ruid), 0));
312 s_runame(const KINFO *k)
314 return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
318 tdev(const KINFO *k, const struct varent *vent)
323 dev = KI_PROC(k, tdev);
325 printf("%*s", vent->width, "??");
327 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
328 printf("%*s", vent->width, buff);
333 tname(const KINFO *k, const struct varent *vent)
338 dev = KI_PROC(k, tdev);
339 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
340 printf("%*s ", vent->width-1, "??");
342 if (strncmp(ttname, "tty", 3) == 0 ||
343 strncmp(ttname, "cua", 3) == 0)
345 if (strncmp(ttname, "pts/", 4) == 0)
347 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
348 KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
353 longtname(const KINFO *k, const struct varent *vent)
358 dev = KI_PROC(k, tdev);
359 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
360 printf("%-*s", vent->width, "??");
362 printf("%-*s", vent->width, ttname);
366 started(const KINFO *k, const struct varent *vent)
372 static int use_ampm = -1;
375 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
377 then = KI_PROC(k, start).tv_sec;
378 if (then < btime.tv_sec) {
382 tp = localtime(&then);
385 if (now - then < 24 * 3600) {
386 strftime(buf, sizeof(buf) - 1,
387 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
388 } else if (now - then < 7 * 86400) {
389 strftime(buf, sizeof(buf) - 1,
390 use_ampm ? "%a%I%p" : "%a%H ", tp);
392 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
393 printf("%-*s", vent->width, buf);
397 lstarted(const KINFO *k, const struct varent *vent)
402 then = KI_PROC(k, start).tv_sec;
403 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
404 printf("%-*s", vent->width, buf);
408 wchan(const KINFO *k, const struct varent *vent)
410 if (*KI_LWP(k, wmesg)) {
411 printf("%-*.*s", vent->width, vent->width,
414 printf("%-*s", vent->width, "-");
419 #define pgtok(a) (((a)*getpagesize())/1024)
423 vsize(const KINFO *k, const struct varent *vent)
425 printf("%*ju", vent->width, (uintmax_t)(KI_PROC(k, vm_map_size)/1024));
429 rssize(const KINFO *k, const struct varent *vent)
431 /* XXX don't have info about shared */
432 printf("%*lu", vent->width, (u_long)pgtok(KI_PROC(k, vm_rssize)));
436 p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */
438 printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_rssize)));
442 cputime(const KINFO *k, const struct varent *vent)
445 long psecs; /* "parts" of a second. first micro, then centi */
448 static char decimal_point = '\0';
450 if (decimal_point == '\0')
451 decimal_point = localeconv()->decimal_point[0];
454 * This counts time spent handling interrupts. We could
455 * fix this, but it is not 100% trivial (and interrupt
456 * time fractions only work on the sparc anyway). XXX
458 timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
460 secs = timeus / 1000000;
461 psecs = timeus % 1000000;
463 secs += KI_PROC(k, cru).ru_utime.tv_sec +
464 KI_PROC(k, cru).ru_stime.tv_sec;
465 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
466 KI_PROC(k, cru).ru_stime.tv_usec;
469 * round and scale to 100's
471 psecs = (psecs + 5000) / 10000;
476 snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
477 secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
478 } else if (secs >= 100 * 60) {
479 snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
480 secs / 60 / 60, secs / 60 % 60, secs % 60);
484 snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
485 secs / 60, secs % 60,
486 decimal_point, psecs);
488 printf("%*s", vent->width, obuff);
492 getpcpu(const KINFO *k)
501 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
503 /* XXX - I don't like this */
504 if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
507 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
508 return (100.0 * fxtofl(KI_LWP(k, pctcpu)) /
509 (1.0 - exp(KI_PROC(k, swtime) * log(fxtofl(ccpu)))));
513 pcpu(const KINFO *k, const struct varent *vent)
515 printf("%*.1f", vent->width, getpcpu(k));
519 pnice(const KINFO *k, const struct varent *vent)
523 switch (KI_LWP(k, rtprio).type) {
524 case RTP_PRIO_REALTIME:
525 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
528 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
530 case RTP_PRIO_THREAD:
531 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
534 niceval = KI_PROC(k, nice) - NZERO;
537 printf("%*d", vent->width, niceval);
542 getpmem(const KINFO *k)
553 if (KI_PROC(k, flags) & P_SWAPPEDOUT)
555 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
557 /* XXX don't have info about shared */
558 fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
559 return (100.0 * fracmem);
563 pmem(const KINFO *k, const struct varent *vent)
565 printf("%*.1f", vent->width, getpmem(k));
569 pagein(const KINFO *k, const struct varent *vent)
571 printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
576 maxrss(const KINFO *k __unused, const struct varent *vent)
578 printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
582 tsize(const KINFO *k, const struct varent *vent)
584 printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_tsize)));
588 rtprior(const KINFO *k, const struct varent *vent)
594 prtp = &KI_LWP(k, rtprio);
598 case RTP_PRIO_REALTIME:
599 snprintf(str, sizeof(str), "real:%u", prio);
601 case RTP_PRIO_NORMAL:
602 strncpy(str, "normal", sizeof(str));
605 snprintf(str, sizeof(str), "idle:%u", prio);
608 snprintf(str, sizeof(str), "%u:%u", type, prio);
611 str[sizeof(str) - 1] = '\0';
612 printf("%*s", vent->width, str);
616 * Generic output routines. Print fields from various prototype
620 printval(const char *bp, const struct varent *vent)
622 static char ofmt[32] = "%";
627 fcp = vent->var->fmt;
628 if (vent->var->flag & LJUST)
631 while ((*cp++ = *fcp++));
633 switch (vent->var->type) {
635 printf(ofmt, vent->width, *(const char *)bp);
638 printf(ofmt, vent->width, *(const u_char *)bp);
641 printf(ofmt, vent->width, *(const short *)bp);
644 printf(ofmt, vent->width, *(const u_short *)bp);
647 printf(ofmt, vent->width, *(const int *)bp);
650 printf(ofmt, vent->width, *(const u_int *)bp);
653 printf(ofmt, vent->width, *(const long *)bp);
656 printf(ofmt, vent->width, *(const u_long *)bp);
659 printf(ofmt, vent->width, *(const u_long *)bp);
662 errx(1, "unknown type %d", vent->var->type);
667 pvar(const KINFO *k, const struct varent *vent)
669 printval((char *)((char *)k->ki_proc + vent->var->off), vent);
673 lpest(const KINFO *k, const struct varent *vent)
677 val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
679 printval((char *)&val, vent);
684 lpvar(const KINFO *k, const struct varent *vent)
686 printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
690 rvar(const KINFO *k, const struct varent *vent)
692 printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
696 make_printable(const char *str)
704 if ((cpy = malloc(len * 4 + 1)) == NULL)
706 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);