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.24 2005/11/14 18:49:48 dillon 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;
344 if (!k->ki_u.u_valid) {
345 printf("%-*s", vent->width, "-");
350 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
352 then = k->ki_u.u_start.tv_sec;
353 if (then < btime.tv_sec) {
357 tp = localtime(&then);
360 if (now - then < 24 * 3600) {
361 strftime(buf, sizeof(buf) - 1,
362 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
363 } else if (now - then < 7 * 86400) {
364 strftime(buf, sizeof(buf) - 1,
365 use_ampm ? "%a%I%p" : "%a%H ", tp);
367 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
368 printf("%-*s", vent->width, buf);
372 lstarted(const KINFO *k, const struct varent *vent)
377 if (!k->ki_u.u_valid) {
378 printf("%-*s", vent->width, "-");
381 then = k->ki_u.u_start.tv_sec;
382 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
383 printf("%-*s", vent->width, buf);
387 wchan(const KINFO *k, const struct varent *vent)
389 if (KI_THREAD(k)->td_wchan) {
390 if (KI_THREAD(k)->td_wmesg)
391 printf("%-*.*s", vent->width, vent->width,
392 KI_EPROC(k)->e_wmesg);
394 printf("%-*lx", vent->width,
395 (long)KI_THREAD(k)->td_wchan);
397 printf("%-*s", vent->width, "-");
401 #define pgtok(a) (((a)*getpagesize())/1024)
405 vsize(const KINFO *k, const struct varent *vent)
407 printf("%*d", vent->width, (KI_EPROC(k)->e_vm.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_EPROC(k)->e_vm.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_EPROC(k)->e_vm.vm_rssize));
424 cputime(const KINFO *k, const struct varent *vent)
427 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];
434 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
441 * This counts time spent handling interrupts. We could
442 * fix this, but it is not 100% trivial (and interrupt
443 * time fractions only work on the sparc anyway). XXX
445 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks +
446 KI_EPROC(k)->e_iticks;
447 secs = timeus / 1000000;
448 psecs = timeus % 1000000;
450 secs += k->ki_u.u_cru.ru_utime.tv_sec +
451 k->ki_u.u_cru.ru_stime.tv_sec;
452 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
453 k->ki_u.u_cru.ru_stime.tv_usec;
456 * round and scale to 100's
458 psecs = (psecs + 5000) / 10000;
462 snprintf(obuff, sizeof(obuff),
463 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
464 printf("%*s", vent->width, obuff);
468 getpcpu(const KINFO *k)
470 const struct proc *p;
479 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
481 /* XXX - I don't like this */
482 if (p->p_swtime == 0 || (p->p_flag & P_SWAPPEDOUT))
485 return (100.0 * fxtofl(p->p_pctcpu));
486 return (100.0 * fxtofl(p->p_pctcpu) /
487 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
491 pcpu(const KINFO *k, const struct varent *vent)
493 printf("%*.1f", vent->width, getpcpu(k));
497 pnice(const KINFO *k, const struct varent *vent)
501 switch (KI_PROC(k)->p_rtprio.type) {
502 case RTP_PRIO_REALTIME:
503 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
506 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
508 case RTP_PRIO_THREAD:
509 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
512 niceval = KI_PROC(k)->p_nice - NZERO;
515 printf("%*d", vent->width, niceval);
520 getpmem(const KINFO *k)
535 if (p->p_flag & P_SWAPPEDOUT)
537 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
539 /* XXX don't have info about shared */
540 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
541 return (100.0 * fracmem);
545 pmem(const KINFO *k, const struct varent *vent)
547 printf("%*.1f", vent->width, getpmem(k));
551 pagein(const KINFO *k, const struct varent *vent)
553 printf("%*ld", vent->width,
554 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
559 maxrss(const KINFO *k __unused, const struct varent *vent)
562 printf("%*s", vent->width, "-");
566 tsize(const KINFO *k, const struct varent *vent)
568 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
572 rtprior(const KINFO *k, const struct varent *vent)
578 prtp = (struct rtprio *) ((char *)KI_PROC(k) + vent->var->off);
582 case RTP_PRIO_REALTIME:
583 snprintf(str, sizeof(str), "real:%u", prio);
585 case RTP_PRIO_NORMAL:
586 strncpy(str, "normal", sizeof(str));
589 snprintf(str, sizeof(str), "idle:%u", prio);
592 snprintf(str, sizeof(str), "%u:%u", type, prio);
595 str[sizeof(str) - 1] = '\0';
596 printf("%*s", vent->width, str);
600 * Generic output routines. Print fields from various prototype
604 printval(const char *bp, const struct varent *vent)
606 static char ofmt[32] = "%";
611 fcp = vent->var->fmt;
612 if (vent->var->flag & LJUST)
615 while ((*cp++ = *fcp++));
617 switch (vent->var->type) {
619 printf(ofmt, vent->width, *(const char *)bp);
622 printf(ofmt, vent->width, *(const u_char *)bp);
625 printf(ofmt, vent->width, *(const short *)bp);
628 printf(ofmt, vent->width, *(const u_short *)bp);
631 printf(ofmt, vent->width, *(const int *)bp);
634 printf(ofmt, vent->width, *(const u_int *)bp);
637 printf(ofmt, vent->width, *(const long *)bp);
640 printf(ofmt, vent->width, *(const u_long *)bp);
643 printf(ofmt, vent->width, *(const u_long *)bp);
646 errx(1, "unknown type %d", vent->var->type);
651 pvar(const KINFO *k, const struct varent *vent)
653 printval((char *)((char *)KI_PROC(k) + vent->var->off), vent);
657 pest(const KINFO *k, const struct varent *vent)
661 val = *(int *)((char *)KI_PROC(k) + vent->var->off);
663 printval((char *)&val, vent);
668 tvar(const KINFO *k, const struct varent *vent)
670 printval((char *)((char *)KI_THREAD(k) + vent->var->off), vent);
674 evar(const KINFO *k, const struct varent *vent)
676 printval((char *)((char *)KI_EPROC(k) + vent->var->off), vent);
680 uvar(const KINFO *k, const struct varent *vent)
683 printval(((const char *)&k->ki_u + vent->var->off), vent);
685 printf("%*s", vent->width, "-");
689 rvar(const KINFO *k, const struct varent *vent)
692 printval(((const char *)&k->ki_u.u_ru + vent->var->off), vent);
694 printf("%*s", vent->width, "-");
698 make_printable(const char *str)
706 if ((cpy = malloc(len * 4 + 1)) == NULL)
708 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);