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.25 2007/01/01 22:51:17 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_THREAD(k)->td_comm));
110 printf("%-*s", vent->width,
111 make_printable(KI_THREAD(k)->td_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_THREAD(k)->td_comm));
159 logname(const KINFO *k, const struct varent *vent)
161 const char *s = KI_EPROC(k)->e_login;
163 printf("%-*s", vent->width, *s != '\0' ? s : "-");
167 state(const KINFO *k, const struct varent *vent)
185 if (flag & P_SINTR) /* interruptable (long) */
186 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
187 else if (KI_THREAD(k)->td_flags & TDF_SINTR)
196 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
198 sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
212 if (flag & P_SWAPPEDOUT)
214 if (p->p_nice < NZERO)
216 else if (p->p_nice > NZERO)
220 if (flag & P_WEXIT && p->p_stat != SZOMB)
224 if ((flag & P_SYSTEM) || p->p_lock > 0)
226 if (numcpus > 1 && KI_THREAD(k)->td_mpcount_unused == 0)
230 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
232 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
235 printf("%-*s", vent->width, buf);
239 * Normalized priority (lower is better). For pure threads
240 * output a negated LWKT priority (so lower still means better).
242 * XXX bsd4 scheduler specific.
245 pri(const KINFO *k, const struct varent *vent)
247 if (KI_THREAD(k)->td_proc)
248 printf("%*d", vent->width, KI_PROC(k)->p_usdata.bsd4.priority);
250 printf("%*d", vent->width, -(KI_THREAD(k)->td_pri & TDPRI_MASK));
254 tdpri(const KINFO *k, const struct varent *vent)
257 int val = KI_THREAD(k)->td_pri;
259 snprintf(buf, sizeof(buf), "%02d/%d", val & TDPRI_MASK, val / TDPRI_CRIT);
260 printf("%*s", vent->width, buf);
264 uname(const KINFO *k, const struct varent *vent)
266 printf("%-*s", vent->width,
267 user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
271 s_uname(const KINFO *k)
273 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
277 runame(const KINFO *k, const struct varent *vent)
279 printf("%-*s", vent->width,
280 user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
284 s_runame(const KINFO *k)
286 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
290 tdev(const KINFO *k, const struct varent *vent)
295 dev = KI_EPROC(k)->e_tdev;
297 printf("%*s", vent->width, "??");
299 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
300 printf("%*s", vent->width, buff);
305 tname(const KINFO *k, const struct varent *vent)
310 dev = KI_EPROC(k)->e_tdev;
311 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
312 printf("%*s ", vent->width-1, "??");
314 if (strncmp(ttname, "tty", 3) == 0 ||
315 strncmp(ttname, "cua", 3) == 0)
317 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
318 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
323 longtname(const KINFO *k, const struct varent *vent)
328 dev = KI_EPROC(k)->e_tdev;
329 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
330 printf("%-*s", vent->width, "??");
332 printf("%-*s", vent->width, ttname);
336 started(const KINFO *k, const struct varent *vent)
342 static int use_ampm = -1;
345 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
347 then = k->ki_u.u_start.tv_sec;
348 if (then < btime.tv_sec) {
352 tp = localtime(&then);
355 if (now - then < 24 * 3600) {
356 strftime(buf, sizeof(buf) - 1,
357 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
358 } else if (now - then < 7 * 86400) {
359 strftime(buf, sizeof(buf) - 1,
360 use_ampm ? "%a%I%p" : "%a%H ", tp);
362 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
363 printf("%-*s", vent->width, buf);
367 lstarted(const KINFO *k, const struct varent *vent)
372 then = k->ki_u.u_start.tv_sec;
373 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
374 printf("%-*s", vent->width, buf);
378 wchan(const KINFO *k, const struct varent *vent)
380 if (KI_THREAD(k)->td_wchan) {
381 if (KI_THREAD(k)->td_wmesg)
382 printf("%-*.*s", vent->width, vent->width,
383 KI_EPROC(k)->e_wmesg);
385 printf("%-*lx", vent->width,
386 (long)KI_THREAD(k)->td_wchan);
388 printf("%-*s", vent->width, "-");
392 #define pgtok(a) (((a)*getpagesize())/1024)
396 vsize(const KINFO *k, const struct varent *vent)
398 printf("%*d", vent->width, (KI_EPROC(k)->e_vm.vm_map.size/1024));
402 rssize(const KINFO *k, const struct varent *vent)
404 /* XXX don't have info about shared */
405 printf("%*lu", vent->width, (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
409 p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */
411 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
415 cputime(const KINFO *k, const struct varent *vent)
418 long psecs; /* "parts" of a second. first micro, then centi */
420 static char decimal_point = '\0';
422 if (decimal_point == '\0')
423 decimal_point = localeconv()->decimal_point[0];
425 if (KI_PROC(k)->p_stat == SZOMB) {
432 * This counts time spent handling interrupts. We could
433 * fix this, but it is not 100% trivial (and interrupt
434 * time fractions only work on the sparc anyway). XXX
436 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks +
437 KI_EPROC(k)->e_iticks;
438 secs = timeus / 1000000;
439 psecs = timeus % 1000000;
441 secs += k->ki_u.u_cru.ru_utime.tv_sec +
442 k->ki_u.u_cru.ru_stime.tv_sec;
443 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
444 k->ki_u.u_cru.ru_stime.tv_usec;
447 * round and scale to 100's
449 psecs = (psecs + 5000) / 10000;
453 snprintf(obuff, sizeof(obuff),
454 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
455 printf("%*s", vent->width, obuff);
459 getpcpu(const KINFO *k)
461 const struct proc *p;
470 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
472 /* XXX - I don't like this */
473 if (p->p_swtime == 0 || (p->p_flag & P_SWAPPEDOUT))
476 return (100.0 * fxtofl(p->p_pctcpu));
477 return (100.0 * fxtofl(p->p_pctcpu) /
478 (1.0 - exp(p->p_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_PROC(k)->p_rtprio.type) {
493 case RTP_PRIO_REALTIME:
494 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
497 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
499 case RTP_PRIO_THREAD:
500 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
503 niceval = KI_PROC(k)->p_nice - NZERO;
506 printf("%*d", vent->width, niceval);
511 getpmem(const KINFO *k)
526 if (p->p_flag & P_SWAPPEDOUT)
528 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
530 /* XXX don't have info about shared */
531 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
532 return (100.0 * fracmem);
536 pmem(const KINFO *k, const struct varent *vent)
538 printf("%*.1f", vent->width, getpmem(k));
542 pagein(const KINFO *k, const struct varent *vent)
544 printf("%*ld", vent->width, k->ki_u.u_ru.ru_majflt);
549 maxrss(const KINFO *k __unused, const struct varent *vent)
552 printf("%*s", vent->width, "-");
556 tsize(const KINFO *k, const struct varent *vent)
558 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
562 rtprior(const KINFO *k, const struct varent *vent)
568 prtp = (struct rtprio *) ((char *)KI_PROC(k) + vent->var->off);
572 case RTP_PRIO_REALTIME:
573 snprintf(str, sizeof(str), "real:%u", prio);
575 case RTP_PRIO_NORMAL:
576 strncpy(str, "normal", sizeof(str));
579 snprintf(str, sizeof(str), "idle:%u", prio);
582 snprintf(str, sizeof(str), "%u:%u", type, prio);
585 str[sizeof(str) - 1] = '\0';
586 printf("%*s", vent->width, str);
590 * Generic output routines. Print fields from various prototype
594 printval(const char *bp, const struct varent *vent)
596 static char ofmt[32] = "%";
601 fcp = vent->var->fmt;
602 if (vent->var->flag & LJUST)
605 while ((*cp++ = *fcp++));
607 switch (vent->var->type) {
609 printf(ofmt, vent->width, *(const char *)bp);
612 printf(ofmt, vent->width, *(const u_char *)bp);
615 printf(ofmt, vent->width, *(const short *)bp);
618 printf(ofmt, vent->width, *(const u_short *)bp);
621 printf(ofmt, vent->width, *(const int *)bp);
624 printf(ofmt, vent->width, *(const u_int *)bp);
627 printf(ofmt, vent->width, *(const long *)bp);
630 printf(ofmt, vent->width, *(const u_long *)bp);
633 printf(ofmt, vent->width, *(const u_long *)bp);
636 errx(1, "unknown type %d", vent->var->type);
641 pvar(const KINFO *k, const struct varent *vent)
643 printval((char *)((char *)KI_PROC(k) + vent->var->off), vent);
647 pest(const KINFO *k, const struct varent *vent)
651 val = *(int *)((char *)KI_PROC(k) + vent->var->off);
653 printval((char *)&val, vent);
658 tvar(const KINFO *k, const struct varent *vent)
660 printval((char *)((char *)KI_THREAD(k) + vent->var->off), vent);
664 evar(const KINFO *k, const struct varent *vent)
666 printval((char *)((char *)KI_EPROC(k) + vent->var->off), vent);
670 uvar(const KINFO *k, const struct varent *vent)
672 printval(((const char *)&k->ki_u + vent->var->off), vent);
676 rvar(const KINFO *k, const struct varent *vent)
678 printval(((const char *)&k->ki_u.u_ru + vent->var->off), vent);
682 make_printable(const char *str)
690 if ((cpy = malloc(len * 4 + 1)) == NULL)
692 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);