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.8 2003/09/28 14:39:15 hmp Exp $
38 #include <sys/param.h>
40 #include <sys/resource.h>
44 #include <sys/ucred.h>
46 #include <sys/sysctl.h>
47 #include <sys/rtprio.h>
72 for (vent = vhead; vent; vent = vent->next)
73 if (*vent->var->header != '\0') {
79 for (vent = vhead; vent; vent = vent->next) {
81 if (v->flag & LJUST) {
82 if (vent->next == NULL) /* last one */
83 (void)printf("%s", v->header);
85 (void)printf("%-*s", v->width, v->header);
87 (void)printf("%*s", v->width, v->header);
88 if (vent->next != NULL)
95 command(KINFO *k, VARENT *ve)
99 char *cp, *vis_env, *vis_args;
104 if (ve->next == NULL) /* last field, don't pad */
105 (void)printf("%s", KI_THREAD(k)->td_comm);
107 (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
111 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
113 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
115 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
117 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
121 if (ve->next == NULL) {
123 if (termwidth == UNLIMITED) {
125 (void)printf("%s ", vis_env);
126 (void)printf("%s", vis_args);
128 left = termwidth - (totwidth - v->width);
129 if (left < 1) /* already wrapped, just use std width */
131 if ((cp = vis_env) != NULL) {
132 while (--left >= 0 && *cp)
133 (void)putchar(*cp++);
137 for (cp = vis_args; --left >= 0 && *cp != '\0';)
138 (void)putchar(*cp++);
142 (void)printf("%-*.*s", v->width, v->width, vis_args);
149 ucomm(KINFO *k, VARENT *ve)
154 (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
158 logname(KINFO *k, VARENT *ve)
164 (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
168 state(KINFO *k, VARENT *ve)
188 if (flag & P_SINTR) /* interruptable (long) */
189 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
197 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
199 sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
213 if (!(flag & P_INMEM))
215 if (p->p_nice < NZERO)
217 else if (p->p_nice > NZERO)
221 if (flag & P_WEXIT && p->p_stat != SZOMB)
225 if ((flag & P_SYSTEM) || p->p_lock > 0)
227 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
229 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
234 (void)printf("%-*s", v->width, buf);
238 pri(KINFO *k, VARENT *ve)
243 (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
247 uname(KINFO *k, VARENT *ve)
253 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
259 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
263 runame(KINFO *k, VARENT *ve)
269 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
275 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
279 tdev(KINFO *k, VARENT *ve)
286 dev = KI_EPROC(k)->e_tdev;
288 (void)printf("%*s", v->width, "??");
290 (void)snprintf(buff, sizeof(buff),
291 "%d/%d", major(dev), minor(dev));
292 (void)printf("%*s", v->width, buff);
297 tname(KINFO *k, VARENT *ve)
304 dev = KI_EPROC(k)->e_tdev;
305 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
306 (void)printf("%*s ", v->width-1, "??");
308 if (strncmp(ttname, "tty", 3) == 0 ||
309 strncmp(ttname, "cua", 3) == 0)
311 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
312 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
317 longtname(KINFO *k, VARENT *ve)
324 dev = KI_EPROC(k)->e_tdev;
325 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
326 (void)printf("%-*s", v->width, "??");
328 (void)printf("%-*s", v->width, ttname);
332 started(KINFO *k, VARENT *ve)
339 static int use_ampm = -1;
342 if (!k->ki_u.u_valid) {
343 (void)printf("%-*s", v->width, "-");
348 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
350 then = k->ki_u.u_start.tv_sec;
351 tp = localtime(&then);
354 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
355 (void)strftime(buf, sizeof(buf) - 1,
356 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
357 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
358 (void)strftime(buf, sizeof(buf) - 1,
359 use_ampm ? "%a%I%p" : "%a%H ", tp);
361 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
362 (void)printf("%-*s", v->width, buf);
366 lstarted(KINFO *k, VARENT *ve)
373 if (!k->ki_u.u_valid) {
374 (void)printf("%-*s", v->width, "-");
377 then = k->ki_u.u_start.tv_sec;
378 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
379 (void)printf("%-*s", v->width, buf);
383 wchan(KINFO *k, VARENT *ve)
388 if (KI_THREAD(k)->td_wchan) {
389 if (KI_THREAD(k)->td_wmesg)
390 (void)printf("%-*.*s", v->width, v->width,
391 KI_EPROC(k)->e_wmesg);
393 (void)printf("%-*lx", v->width,
394 (long)KI_THREAD(k)->td_wchan);
396 (void)printf("%-*s", v->width, "-");
400 #define pgtok(a) (((a)*getpagesize())/1024)
404 vsize(KINFO *k, VARENT *ve)
409 (void)printf("%*d", v->width,
410 (KI_EPROC(k)->e_vm.vm_map.size/1024));
414 rssize(KINFO *k, VARENT *ve)
419 /* XXX don't have info about shared */
420 (void)printf("%*lu", v->width,
421 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
425 p_rssize(KINFO *k, VARENT *ve) /* doesn't account for text */
430 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
434 cputime(KINFO *k, VARENT *ve)
438 long psecs; /* "parts" of a second. first micro, then centi */
440 static char decimal_point = 0;
443 decimal_point = localeconv()->decimal_point[0];
445 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
452 * This counts time spent handling interrupts. We could
453 * fix this, but it is not 100% trivial (and interrupt
454 * time fractions only work on the sparc anyway). XXX
456 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
457 secs = timeus / 1000000;
458 psecs = timeus % 1000000;
460 secs += k->ki_u.u_cru.ru_utime.tv_sec +
461 k->ki_u.u_cru.ru_stime.tv_sec;
462 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
463 k->ki_u.u_cru.ru_stime.tv_usec;
466 * round and scale to 100's
468 psecs = (psecs + 5000) / 10000;
472 (void)snprintf(obuff, sizeof(obuff),
473 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
474 (void)printf("%*s", v->width, obuff);
489 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
491 /* XXX - I don't like this */
492 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
495 return (100.0 * fxtofl(p->p_pctcpu));
496 return (100.0 * fxtofl(p->p_pctcpu) /
497 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
501 pcpu(KINFO *k, VARENT *ve)
506 (void)printf("%*.1f", v->width, getpcpu(k));
510 pnice(KINFO *k, VARENT *ve)
515 switch (KI_PROC(k)->p_rtprio.type) {
516 case RTP_PRIO_REALTIME:
517 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
520 nice = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
522 case RTP_PRIO_THREAD:
523 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
526 nice = KI_PROC(k)->p_nice - NZERO;
530 (void)printf("%*d", v->width, nice);
550 if ((p->p_flag & P_INMEM) == 0)
552 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
554 /* XXX don't have info about shared */
555 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
556 return (100.0 * fracmem);
560 pmem(KINFO *k, VARENT *ve)
565 (void)printf("%*.1f", v->width, getpmem(k));
569 pagein(KINFO *k, VARENT *ve)
574 (void)printf("%*ld", v->width,
575 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
579 maxrss(KINFO *k, VARENT *ve)
585 (void)printf("%*s", v->width, "-");
589 tsize(KINFO *k, VARENT *ve)
594 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
598 rtprior(KINFO *k, 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(char *bp, VAR *v)
634 static char ofmt[32] = "%";
642 while ((*cp++ = *fcp++));
646 (void)printf(ofmt, v->width, *(char *)bp);
649 (void)printf(ofmt, v->width, *(u_char *)bp);
652 (void)printf(ofmt, v->width, *(short *)bp);
655 (void)printf(ofmt, v->width, *(u_short *)bp);
658 (void)printf(ofmt, v->width, *(int *)bp);
661 (void)printf(ofmt, v->width, *(u_int *)bp);
664 (void)printf(ofmt, v->width, *(long *)bp);
667 (void)printf(ofmt, v->width, *(u_long *)bp);
670 (void)printf(ofmt, v->width, *(u_long *)bp);
673 errx(1, "unknown type %d", v->type);
678 pvar(KINFO *k, VARENT *ve)
683 printval((char *)((char *)KI_PROC(k) + v->off), v);
687 tvar(KINFO *k, VARENT *ve)
692 printval((char *)((char *)KI_THREAD(k) + v->off), v);
696 evar(KINFO *k, VARENT *ve)
701 printval((char *)((char *)KI_EPROC(k) + v->off), v);
705 uvar(KINFO *k, VARENT *ve)
711 printval((char *)((char *)&k->ki_u + v->off), v);
713 (void)printf("%*s", v->width, "-");
717 rvar(KINFO *k, VARENT *ve)
723 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
725 (void)printf("%*s", v->width, "-");