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.28 2007/02/08 10:18:21 corecode 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>
63 static const char *make_printable(const char *str);
73 STAILQ_FOREACH(vent, &var_head, link) {
74 if (*vent->header != '\0') {
81 STAILQ_FOREACH(vent, &var_head, link) {
83 if (v->flag & LJUST) {
84 if (STAILQ_NEXT(vent, link) == NULL) /* last one */
85 printf("%s", vent->header);
87 printf("%-*s", vent->width, vent->header);
89 printf("%*s", vent->width, vent->header);
90 if (STAILQ_NEXT(vent, link) != NULL)
97 command(const KINFO *k, const struct varent *vent)
101 char *cp, *vis_env, *vis_args;
106 /* Don't pad the last field. */
107 if (STAILQ_NEXT(vent, link) == NULL)
108 printf("%s", make_printable(KI_PROC(k, comm)));
110 printf("%-*s", vent->width,
111 make_printable(KI_PROC(k, comm)));
115 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
117 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
119 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
121 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
125 if (STAILQ_NEXT(vent, link) == NULL) {
127 if (termwidth == UNLIMITED) {
129 printf("%s ", vis_env);
130 printf("%s", vis_args);
132 left = termwidth - (totwidth - vent->width);
133 if (left < 1) /* already wrapped, just use std width */
135 if ((cp = vis_env) != NULL) {
136 while (--left >= 0 && *cp)
141 for (cp = vis_args; --left >= 0 && *cp != '\0';)
146 printf("%-*.*s", vent->width, vent->width, vis_args);
153 ucomm(const KINFO *k, const struct varent *vent)
155 printf("%-*s", vent->width, make_printable(KI_PROC(k, comm)));
159 logname(const KINFO *k, const struct varent *vent)
161 const char *s = KI_PROC(k, login);
163 printf("%-*s", vent->width, *s != '\0' ? s : "-");
167 state(const KINFO *k, const struct varent *vent)
173 flag = KI_PROC(k, flags);
176 switch (KI_PROC(k, stat)) {
183 if (flag & P_SINTR) /* interruptable (long) */
184 *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
185 else if (KI_LWP(k, tdflags) & TDF_SINTR)
194 if (KI_LWP(k, tdflags) & TDF_RUNNING) {
196 sprintf(cp, "%d", KI_LWP(k, cpuid));
210 if (flag & P_SWAPPEDOUT)
212 if (KI_PROC(k, nice) < NZERO)
214 else if (KI_PROC(k, nice) > NZERO)
218 if (flag & P_WEXIT && KI_LWP(k, stat) != SZOMB)
222 if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
224 if (numcpus > 1 && KI_LWP(k, mpcount) == 0)
228 if (KI_PROC(k, auxflags) & KI_SLEADER)
230 if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
233 printf("%-*s", vent->width, buf);
237 * Normalized priority (lower is better). For pure threads
238 * output a negated LWKT priority (so lower still means better).
240 * XXX bsd4 scheduler specific.
243 pri(const KINFO *k, const struct varent *vent)
245 if (KI_LWP(k, pid) != -1)
246 printf("%*d", vent->width, KI_LWP(k, prio));
248 printf("%*d", vent->width, -(KI_LWP(k, tdprio) & TDPRI_MASK));
252 tdpri(const KINFO *k, const struct varent *vent)
255 int val = KI_LWP(k, tdprio);
257 snprintf(buf, sizeof(buf), "%02d/%d", val & TDPRI_MASK, val / TDPRI_CRIT);
258 printf("%*s", vent->width, buf);
262 uname(const KINFO *k, const struct varent *vent)
264 printf("%-*s", vent->width,
265 user_from_uid(KI_PROC(k, uid), 0));
269 s_uname(const KINFO *k)
271 return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
275 runame(const KINFO *k, const struct varent *vent)
277 printf("%-*s", vent->width,
278 user_from_uid(KI_PROC(k, ruid), 0));
282 s_runame(const KINFO *k)
284 return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
288 tdev(const KINFO *k, const struct varent *vent)
293 dev = KI_PROC(k, tdev);
295 printf("%*s", vent->width, "??");
297 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
298 printf("%*s", vent->width, buff);
303 tname(const KINFO *k, const struct varent *vent)
308 dev = KI_PROC(k, tdev);
309 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
310 printf("%*s ", vent->width-1, "??");
312 if (strncmp(ttname, "tty", 3) == 0 ||
313 strncmp(ttname, "cua", 3) == 0)
315 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
316 KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
321 longtname(const KINFO *k, const struct varent *vent)
326 dev = KI_PROC(k, tdev);
327 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
328 printf("%-*s", vent->width, "??");
330 printf("%-*s", vent->width, ttname);
334 started(const KINFO *k, const struct varent *vent)
340 static int use_ampm = -1;
343 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
345 then = KI_PROC(k, start).tv_sec;
346 if (then < btime.tv_sec) {
350 tp = localtime(&then);
353 if (now - then < 24 * 3600) {
354 strftime(buf, sizeof(buf) - 1,
355 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
356 } else if (now - then < 7 * 86400) {
357 strftime(buf, sizeof(buf) - 1,
358 use_ampm ? "%a%I%p" : "%a%H ", tp);
360 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
361 printf("%-*s", vent->width, buf);
365 lstarted(const KINFO *k, const struct varent *vent)
370 then = KI_PROC(k, start).tv_sec;
371 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
372 printf("%-*s", vent->width, buf);
376 wchan(const KINFO *k, const struct varent *vent)
378 if (KI_LWP(k, wchan)) {
379 if (*KI_LWP(k, wmesg) != '\0')
380 printf("%-*.*s", vent->width, vent->width,
383 printf("%-*lx", vent->width,
384 (long)KI_LWP(k, wchan));
386 printf("%-*s", vent->width, "-");
390 #define pgtok(a) (((a)*getpagesize())/1024)
394 vsize(const KINFO *k, const struct varent *vent)
396 printf("%*d", vent->width, (KI_PROC(k, vm_map_size)/1024));
400 rssize(const KINFO *k, const struct varent *vent)
402 /* XXX don't have info about shared */
403 printf("%*lu", vent->width, (u_long)pgtok(KI_PROC(k, vm_rssize)));
407 p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */
409 printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_rssize)));
413 cputime(const KINFO *k, const struct varent *vent)
416 long psecs; /* "parts" of a second. first micro, then centi */
418 static char decimal_point = '\0';
420 if (decimal_point == '\0')
421 decimal_point = localeconv()->decimal_point[0];
423 if (KI_LWP(k, stat) == SZOMB) {
430 * This counts time spent handling interrupts. We could
431 * fix this, but it is not 100% trivial (and interrupt
432 * time fractions only work on the sparc anyway). XXX
434 timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
436 secs = timeus / 1000000;
437 psecs = timeus % 1000000;
439 secs += KI_PROC(k, cru).ru_utime.tv_sec +
440 KI_PROC(k, cru).ru_stime.tv_sec;
441 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
442 KI_PROC(k, cru).ru_stime.tv_usec;
445 * round and scale to 100's
447 psecs = (psecs + 5000) / 10000;
451 snprintf(obuff, sizeof(obuff),
452 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
453 printf("%*s", vent->width, obuff);
457 getpcpu(const KINFO *k)
466 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
468 /* XXX - I don't like this */
469 if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
472 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
473 return (100.0 * fxtofl(KI_LWP(k, pctcpu)) /
474 (1.0 - exp(KI_PROC(k, swtime) * log(fxtofl(ccpu)))));
478 pcpu(const KINFO *k, const struct varent *vent)
480 printf("%*.1f", vent->width, getpcpu(k));
484 pnice(const KINFO *k, const struct varent *vent)
488 switch (KI_LWP(k, rtprio).type) {
489 case RTP_PRIO_REALTIME:
490 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
493 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
495 case RTP_PRIO_THREAD:
496 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
499 niceval = KI_PROC(k, nice) - NZERO;
502 printf("%*d", vent->width, niceval);
507 getpmem(const KINFO *k)
518 if (KI_PROC(k, flags) & P_SWAPPEDOUT)
520 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
522 /* XXX don't have info about shared */
523 fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
524 return (100.0 * fracmem);
528 pmem(const KINFO *k, const struct varent *vent)
530 printf("%*.1f", vent->width, getpmem(k));
534 pagein(const KINFO *k, const struct varent *vent)
536 printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
541 maxrss(const KINFO *k __unused, const struct varent *vent)
543 printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
547 tsize(const KINFO *k, const struct varent *vent)
549 printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_tsize)));
553 rtprior(const KINFO *k, const struct varent *vent)
559 prtp = &KI_LWP(k, rtprio);
563 case RTP_PRIO_REALTIME:
564 snprintf(str, sizeof(str), "real:%u", prio);
566 case RTP_PRIO_NORMAL:
567 strncpy(str, "normal", sizeof(str));
570 snprintf(str, sizeof(str), "idle:%u", prio);
573 snprintf(str, sizeof(str), "%u:%u", type, prio);
576 str[sizeof(str) - 1] = '\0';
577 printf("%*s", vent->width, str);
581 * Generic output routines. Print fields from various prototype
585 printval(const char *bp, const struct varent *vent)
587 static char ofmt[32] = "%";
592 fcp = vent->var->fmt;
593 if (vent->var->flag & LJUST)
596 while ((*cp++ = *fcp++));
598 switch (vent->var->type) {
600 printf(ofmt, vent->width, *(const char *)bp);
603 printf(ofmt, vent->width, *(const u_char *)bp);
606 printf(ofmt, vent->width, *(const short *)bp);
609 printf(ofmt, vent->width, *(const u_short *)bp);
612 printf(ofmt, vent->width, *(const int *)bp);
615 printf(ofmt, vent->width, *(const u_int *)bp);
618 printf(ofmt, vent->width, *(const long *)bp);
621 printf(ofmt, vent->width, *(const u_long *)bp);
624 printf(ofmt, vent->width, *(const u_long *)bp);
627 errx(1, "unknown type %d", vent->var->type);
632 pvar(const KINFO *k, const struct varent *vent)
634 printval((char *)((char *)k->ki_proc + vent->var->off), vent);
638 lpest(const KINFO *k, const struct varent *vent)
642 val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
644 printval((char *)&val, vent);
649 lpvar(const KINFO *k, const struct varent *vent)
651 printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
655 rvar(const KINFO *k, const struct varent *vent)
657 printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
661 make_printable(const char *str)
669 if ((cpy = malloc(len * 4 + 1)) == NULL)
671 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);