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.10 2004/03/19 17:47:49 cpressey 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';
196 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
198 sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
212 if (!(flag & P_INMEM))
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 (KI_EPROC(k)->e_flag & EPROC_SLEADER)
228 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
233 (void)printf("%-*s", v->width, buf);
237 pri(const KINFO *k, const VARENT *ve)
242 (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
246 uname(const KINFO *k, const VARENT *ve)
252 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
256 s_uname(const KINFO *k)
258 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
262 runame(const KINFO *k, const VARENT *ve)
268 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
272 s_runame(const KINFO *k)
274 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
278 tdev(const KINFO *k, const VARENT *ve)
285 dev = KI_EPROC(k)->e_tdev;
287 (void)printf("%*s", v->width, "??");
289 (void)snprintf(buff, sizeof(buff),
290 "%d/%d", major(dev), minor(dev));
291 (void)printf("%*s", v->width, buff);
296 tname(const KINFO *k, const VARENT *ve)
303 dev = KI_EPROC(k)->e_tdev;
304 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
305 (void)printf("%*s ", v->width-1, "??");
307 if (strncmp(ttname, "tty", 3) == 0 ||
308 strncmp(ttname, "cua", 3) == 0)
310 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
311 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
316 longtname(const KINFO *k, const VARENT *ve)
323 dev = KI_EPROC(k)->e_tdev;
324 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
325 (void)printf("%-*s", v->width, "??");
327 (void)printf("%-*s", v->width, ttname);
331 started(const KINFO *k, const VARENT *ve)
338 static int use_ampm = -1;
341 if (!k->ki_u.u_valid) {
342 (void)printf("%-*s", v->width, "-");
347 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
349 then = k->ki_u.u_start.tv_sec;
350 tp = localtime(&then);
353 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
354 (void)strftime(buf, sizeof(buf) - 1,
355 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
356 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
357 (void)strftime(buf, sizeof(buf) - 1,
358 use_ampm ? "%a%I%p" : "%a%H ", tp);
360 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
361 (void)printf("%-*s", v->width, buf);
365 lstarted(const KINFO *k, const VARENT *ve)
372 if (!k->ki_u.u_valid) {
373 (void)printf("%-*s", v->width, "-");
376 then = k->ki_u.u_start.tv_sec;
377 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
378 (void)printf("%-*s", v->width, buf);
382 wchan(const KINFO *k, const VARENT *ve)
387 if (KI_THREAD(k)->td_wchan) {
388 if (KI_THREAD(k)->td_wmesg)
389 (void)printf("%-*.*s", v->width, v->width,
390 KI_EPROC(k)->e_wmesg);
392 (void)printf("%-*lx", v->width,
393 (long)KI_THREAD(k)->td_wchan);
395 (void)printf("%-*s", v->width, "-");
399 #define pgtok(a) (((a)*getpagesize())/1024)
403 vsize(const KINFO *k, const VARENT *ve)
408 (void)printf("%*d", v->width,
409 (KI_EPROC(k)->e_vm.vm_map.size/1024));
413 rssize(const KINFO *k, const VARENT *ve)
418 /* XXX don't have info about shared */
419 (void)printf("%*lu", v->width,
420 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
424 p_rssize(const KINFO *k, const VARENT *ve) /* doesn't account for text */
429 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
433 cputime(const KINFO *k, const VARENT *ve)
437 long psecs; /* "parts" of a second. first micro, then centi */
439 static char decimal_point = 0;
442 decimal_point = localeconv()->decimal_point[0];
444 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
451 * This counts time spent handling interrupts. We could
452 * fix this, but it is not 100% trivial (and interrupt
453 * time fractions only work on the sparc anyway). XXX
455 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
456 secs = timeus / 1000000;
457 psecs = timeus % 1000000;
459 secs += k->ki_u.u_cru.ru_utime.tv_sec +
460 k->ki_u.u_cru.ru_stime.tv_sec;
461 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
462 k->ki_u.u_cru.ru_stime.tv_usec;
465 * round and scale to 100's
467 psecs = (psecs + 5000) / 10000;
471 (void)snprintf(obuff, sizeof(obuff),
472 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
473 (void)printf("%*s", v->width, obuff);
477 getpcpu(const KINFO *k)
479 const struct proc *p;
488 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
490 /* XXX - I don't like this */
491 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
494 return (100.0 * fxtofl(p->p_pctcpu));
495 return (100.0 * fxtofl(p->p_pctcpu) /
496 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
500 pcpu(const KINFO *k, const VARENT *ve)
505 (void)printf("%*.1f", v->width, getpcpu(k));
509 pnice(const KINFO *k, const VARENT *ve)
514 switch (KI_PROC(k)->p_rtprio.type) {
515 case RTP_PRIO_REALTIME:
516 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
519 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
521 case RTP_PRIO_THREAD:
522 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
525 niceval = KI_PROC(k)->p_nice - NZERO;
529 (void)printf("%*d", v->width, niceval);
534 getpmem(const KINFO *k)
549 if ((p->p_flag & P_INMEM) == 0)
551 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
553 /* XXX don't have info about shared */
554 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
555 return (100.0 * fracmem);
559 pmem(const KINFO *k, const VARENT *ve)
564 (void)printf("%*.1f", v->width, getpmem(k));
568 pagein(const KINFO *k, const VARENT *ve)
573 (void)printf("%*ld", v->width,
574 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
579 maxrss(const KINFO *k, const VARENT *ve)
585 (void)printf("%*s", v->width, "-");
589 tsize(const KINFO *k, const VARENT *ve)
594 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
598 rtprior(const KINFO *k, const VARENT *ve)
606 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
610 case RTP_PRIO_REALTIME:
611 snprintf(str, sizeof(str), "real:%u", prio);
613 case RTP_PRIO_NORMAL:
614 strncpy(str, "normal", sizeof(str));
617 snprintf(str, sizeof(str), "idle:%u", prio);
620 snprintf(str, sizeof(str), "%u:%u", type, prio);
623 str[sizeof(str) - 1] = '\0';
624 (void)printf("%*s", v->width, str);
628 * Generic output routines. Print fields from various prototype
632 printval(const char *bp, const VAR *v)
634 static char ofmt[32] = "%";
643 while ((*cp++ = *fcp++));
647 (void)printf(ofmt, v->width, *(const char *)bp);
650 (void)printf(ofmt, v->width, *(const u_char *)bp);
653 (void)printf(ofmt, v->width, *(const short *)bp);
656 (void)printf(ofmt, v->width, *(const u_short *)bp);
659 (void)printf(ofmt, v->width, *(const int *)bp);
662 (void)printf(ofmt, v->width, *(const u_int *)bp);
665 (void)printf(ofmt, v->width, *(const long *)bp);
668 (void)printf(ofmt, v->width, *(const u_long *)bp);
671 (void)printf(ofmt, v->width, *(const u_long *)bp);
674 errx(1, "unknown type %d", v->type);
679 pvar(const KINFO *k, const VARENT *ve)
684 printval((char *)((char *)KI_PROC(k) + v->off), v);
688 tvar(const KINFO *k, const VARENT *ve)
693 printval((char *)((char *)KI_THREAD(k) + v->off), v);
697 evar(const KINFO *k, const VARENT *ve)
702 printval((char *)((char *)KI_EPROC(k) + v->off), v);
706 uvar(const KINFO *k, const VARENT *ve)
712 printval(((const char *)&k->ki_u + v->off), v);
714 (void)printf("%*s", v->width, "-");
718 rvar(const KINFO *k, const VARENT *ve)
724 printval(((const char *)&k->ki_u.u_ru + v->off), v);
726 (void)printf("%*s", v->width, "-");