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.12 2004/06/21 00:47:57 hmp 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>
71 for (vent = vhead; vent; vent = vent->next)
72 if (*vent->var->header != '\0') {
78 for (vent = vhead; vent; vent = vent->next) {
80 if (v->flag & LJUST) {
81 if (vent->next == NULL) /* last one */
82 (void)printf("%s", v->header);
84 (void)printf("%-*s", v->width, v->header);
86 (void)printf("%*s", v->width, v->header);
87 if (vent->next != NULL)
94 command(const KINFO *k, const VARENT *ve)
98 char *cp, *vis_env, *vis_args;
103 if (ve->next == NULL) /* last field, don't pad */
104 (void)printf("%s", KI_THREAD(k)->td_comm);
106 (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
110 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
112 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
114 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
116 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
120 if (ve->next == NULL) {
122 if (termwidth == UNLIMITED) {
124 (void)printf("%s ", vis_env);
125 (void)printf("%s", vis_args);
127 left = termwidth - (totwidth - v->width);
128 if (left < 1) /* already wrapped, just use std width */
130 if ((cp = vis_env) != NULL) {
131 while (--left >= 0 && *cp)
132 (void)putchar(*cp++);
136 for (cp = vis_args; --left >= 0 && *cp != '\0';)
137 (void)putchar(*cp++);
141 (void)printf("%-*.*s", v->width, v->width, vis_args);
148 ucomm(const KINFO *k, const VARENT *ve)
153 (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
157 logname(const KINFO *k, const VARENT *ve)
163 (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
167 state(const KINFO *k, const VARENT *ve)
187 if (flag & P_SINTR) /* interruptable (long) */
188 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
189 else if (KI_THREAD(k)->td_flags & TDF_SINTR)
198 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
200 sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
214 if (!(flag & P_INMEM))
216 if (p->p_nice < NZERO)
218 else if (p->p_nice > NZERO)
222 if (flag & P_WEXIT && p->p_stat != SZOMB)
226 if ((flag & P_SYSTEM) || p->p_lock > 0)
228 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
230 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
235 (void)printf("%-*s", v->width, buf);
239 pri(const KINFO *k, const VARENT *ve)
244 (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
248 uname(const KINFO *k, const VARENT *ve)
254 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
258 s_uname(const KINFO *k)
260 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
264 runame(const KINFO *k, const VARENT *ve)
270 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
274 s_runame(const KINFO *k)
276 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
280 tdev(const KINFO *k, const VARENT *ve)
287 dev = KI_EPROC(k)->e_tdev;
289 (void)printf("%*s", v->width, "??");
291 (void)snprintf(buff, sizeof(buff),
292 "%d/%d", major(dev), minor(dev));
293 (void)printf("%*s", v->width, buff);
298 tname(const KINFO *k, const VARENT *ve)
305 dev = KI_EPROC(k)->e_tdev;
306 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
307 (void)printf("%*s ", v->width-1, "??");
309 if (strncmp(ttname, "tty", 3) == 0 ||
310 strncmp(ttname, "cua", 3) == 0)
312 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
313 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
318 longtname(const KINFO *k, const VARENT *ve)
325 dev = KI_EPROC(k)->e_tdev;
326 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
327 (void)printf("%-*s", v->width, "??");
329 (void)printf("%-*s", v->width, ttname);
333 started(const KINFO *k, const VARENT *ve)
340 static int use_ampm = -1;
343 if (!k->ki_u.u_valid) {
344 (void)printf("%-*s", v->width, "-");
349 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
351 if (KI_THREAD(k)->td_proc != NULL) {
352 then = k->ki_u.u_start.tv_sec;
354 then = KI_THREAD(k)->td_start.tv_sec;
355 if (then < btime.tv_sec) {
360 tp = localtime(&then);
363 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
364 (void)strftime(buf, sizeof(buf) - 1,
365 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
366 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
367 (void)strftime(buf, sizeof(buf) - 1,
368 use_ampm ? "%a%I%p" : "%a%H ", tp);
370 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
371 (void)printf("%-*s", v->width, buf);
375 lstarted(const KINFO *k, const VARENT *ve)
382 if (!k->ki_u.u_valid) {
383 (void)printf("%-*s", v->width, "-");
386 then = k->ki_u.u_start.tv_sec;
387 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
388 (void)printf("%-*s", v->width, buf);
392 wchan(const KINFO *k, const VARENT *ve)
397 if (KI_THREAD(k)->td_wchan) {
398 if (KI_THREAD(k)->td_wmesg)
399 (void)printf("%-*.*s", v->width, v->width,
400 KI_EPROC(k)->e_wmesg);
402 (void)printf("%-*lx", v->width,
403 (long)KI_THREAD(k)->td_wchan);
405 (void)printf("%-*s", v->width, "-");
409 #define pgtok(a) (((a)*getpagesize())/1024)
413 vsize(const KINFO *k, const VARENT *ve)
418 (void)printf("%*d", v->width,
419 (KI_EPROC(k)->e_vm.vm_map.size/1024));
423 rssize(const KINFO *k, const VARENT *ve)
428 /* XXX don't have info about shared */
429 (void)printf("%*lu", v->width,
430 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
434 p_rssize(const KINFO *k, const VARENT *ve) /* doesn't account for text */
439 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
443 cputime(const KINFO *k, const VARENT *ve)
447 long psecs; /* "parts" of a second. first micro, then centi */
449 static char decimal_point = 0;
452 decimal_point = localeconv()->decimal_point[0];
454 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
461 * This counts time spent handling interrupts. We could
462 * fix this, but it is not 100% trivial (and interrupt
463 * time fractions only work on the sparc anyway). XXX
465 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
466 secs = timeus / 1000000;
467 psecs = timeus % 1000000;
469 secs += k->ki_u.u_cru.ru_utime.tv_sec +
470 k->ki_u.u_cru.ru_stime.tv_sec;
471 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
472 k->ki_u.u_cru.ru_stime.tv_usec;
475 * round and scale to 100's
477 psecs = (psecs + 5000) / 10000;
481 (void)snprintf(obuff, sizeof(obuff),
482 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
483 (void)printf("%*s", v->width, obuff);
487 getpcpu(const KINFO *k)
489 const struct proc *p;
498 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
500 /* XXX - I don't like this */
501 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
504 return (100.0 * fxtofl(p->p_pctcpu));
505 return (100.0 * fxtofl(p->p_pctcpu) /
506 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
510 pcpu(const KINFO *k, const VARENT *ve)
515 (void)printf("%*.1f", v->width, getpcpu(k));
519 pnice(const KINFO *k, const VARENT *ve)
524 switch (KI_PROC(k)->p_rtprio.type) {
525 case RTP_PRIO_REALTIME:
526 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
529 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
531 case RTP_PRIO_THREAD:
532 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
535 niceval = KI_PROC(k)->p_nice - NZERO;
539 (void)printf("%*d", v->width, niceval);
544 getpmem(const KINFO *k)
559 if ((p->p_flag & P_INMEM) == 0)
561 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
563 /* XXX don't have info about shared */
564 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
565 return (100.0 * fracmem);
569 pmem(const KINFO *k, const VARENT *ve)
574 (void)printf("%*.1f", v->width, getpmem(k));
578 pagein(const KINFO *k, const VARENT *ve)
583 (void)printf("%*ld", v->width,
584 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
589 maxrss(const KINFO *k, const VARENT *ve)
595 (void)printf("%*s", v->width, "-");
599 tsize(const KINFO *k, const VARENT *ve)
604 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
608 rtprior(const KINFO *k, const VARENT *ve)
616 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
620 case RTP_PRIO_REALTIME:
621 snprintf(str, sizeof(str), "real:%u", prio);
623 case RTP_PRIO_NORMAL:
624 strncpy(str, "normal", sizeof(str));
627 snprintf(str, sizeof(str), "idle:%u", prio);
630 snprintf(str, sizeof(str), "%u:%u", type, prio);
633 str[sizeof(str) - 1] = '\0';
634 (void)printf("%*s", v->width, str);
638 * Generic output routines. Print fields from various prototype
642 printval(const char *bp, const VAR *v)
644 static char ofmt[32] = "%";
653 while ((*cp++ = *fcp++));
657 (void)printf(ofmt, v->width, *(const char *)bp);
660 (void)printf(ofmt, v->width, *(const u_char *)bp);
663 (void)printf(ofmt, v->width, *(const short *)bp);
666 (void)printf(ofmt, v->width, *(const u_short *)bp);
669 (void)printf(ofmt, v->width, *(const int *)bp);
672 (void)printf(ofmt, v->width, *(const u_int *)bp);
675 (void)printf(ofmt, v->width, *(const long *)bp);
678 (void)printf(ofmt, v->width, *(const u_long *)bp);
681 (void)printf(ofmt, v->width, *(const u_long *)bp);
684 errx(1, "unknown type %d", v->type);
689 pvar(const KINFO *k, const VARENT *ve)
694 printval((char *)((char *)KI_PROC(k) + v->off), v);
698 tvar(const KINFO *k, const VARENT *ve)
703 printval((char *)((char *)KI_THREAD(k) + v->off), v);
707 evar(const KINFO *k, const VARENT *ve)
712 printval((char *)((char *)KI_EPROC(k) + v->off), v);
716 uvar(const KINFO *k, const VARENT *ve)
722 printval(((const char *)&k->ki_u + v->off), v);
724 (void)printf("%*s", v->width, "-");
728 rvar(const KINFO *k, const VARENT *ve)
734 printval(((const char *)&k->ki_u.u_ru + v->off), v);
736 (void)printf("%*s", v->width, "-");