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.4 2003/07/01 00:19:29 dillon 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)
101 char *cp, *vis_env, *vis_args;
106 if (ve->next == NULL) /* last field, don't pad */
107 (void)printf("%s", KI_THREAD(k)->td_comm);
109 (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
113 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
115 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
117 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
119 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
123 if (ve->next == NULL) {
125 if (termwidth == UNLIMITED) {
127 (void)printf("%s ", vis_env);
128 (void)printf("%s", vis_args);
130 left = termwidth - (totwidth - v->width);
131 if (left < 1) /* already wrapped, just use std width */
133 if ((cp = vis_env) != NULL) {
134 while (--left >= 0 && *cp)
135 (void)putchar(*cp++);
139 for (cp = vis_args; --left >= 0 && *cp != '\0';)
140 (void)putchar(*cp++);
144 (void)printf("%-*.*s", v->width, v->width, vis_args);
158 (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
170 (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
196 if (flag & P_SINTR) /* interruptable (long) */
197 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
215 if (!(flag & P_INMEM))
217 if (p->p_nice < NZERO)
219 else if (p->p_nice > NZERO)
223 if (flag & P_WEXIT && p->p_stat != SZOMB)
227 if ((flag & P_SYSTEM) || p->p_lock > 0)
229 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
231 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
236 (void)printf("%-*s", v->width, buf);
247 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
259 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
266 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
278 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
285 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
298 dev = KI_EPROC(k)->e_tdev;
300 (void)printf("%*s", v->width, "??");
302 (void)snprintf(buff, sizeof(buff),
303 "%d/%d", major(dev), minor(dev));
304 (void)printf("%*s", v->width, buff);
318 dev = KI_EPROC(k)->e_tdev;
319 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
320 (void)printf("%*s ", v->width-1, "??");
322 if (strncmp(ttname, "tty", 3) == 0 ||
323 strncmp(ttname, "cua", 3) == 0)
325 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
326 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
340 dev = KI_EPROC(k)->e_tdev;
341 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
342 (void)printf("%-*s", v->width, "??");
344 (void)printf("%-*s", v->width, ttname);
357 static int use_ampm = -1;
360 if (!k->ki_u.u_valid) {
361 (void)printf("%-*s", v->width, "-");
366 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
368 then = k->ki_u.u_start.tv_sec;
369 tp = localtime(&then);
372 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
373 (void)strftime(buf, sizeof(buf) - 1,
374 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
375 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
376 (void)strftime(buf, sizeof(buf) - 1,
377 use_ampm ? "%a%I%p" : "%a%H ", tp);
379 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
380 (void)printf("%-*s", v->width, buf);
393 if (!k->ki_u.u_valid) {
394 (void)printf("%-*s", v->width, "-");
397 then = k->ki_u.u_start.tv_sec;
398 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
399 (void)printf("%-*s", v->width, buf);
410 if (KI_THREAD(k)->td_wchan) {
411 if (KI_THREAD(k)->td_wmesg)
412 (void)printf("%-*.*s", v->width, v->width,
413 KI_EPROC(k)->e_wmesg);
415 (void)printf("%-*lx", v->width,
416 (long)KI_THREAD(k)->td_wchan);
418 (void)printf("%-*s", v->width, "-");
422 #define pgtok(a) (((a)*getpagesize())/1024)
433 (void)printf("%*d", v->width,
434 (KI_EPROC(k)->e_vm.vm_map.size/1024));
445 /* XXX don't have info about shared */
446 (void)printf("%*lu", v->width,
447 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
451 p_rssize(k, ve) /* doesn't account for text */
458 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
468 long psecs; /* "parts" of a second. first micro, then centi */
470 static char decimal_point = 0;
473 decimal_point = localeconv()->decimal_point[0];
475 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
482 * This counts time spent handling interrupts. We could
483 * fix this, but it is not 100% trivial (and interrupt
484 * time fractions only work on the sparc anyway). XXX
486 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
487 secs = timeus / 1000000;
488 psecs = timeus % 1000000;
490 secs += k->ki_u.u_cru.ru_utime.tv_sec +
491 k->ki_u.u_cru.ru_stime.tv_sec;
492 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
493 k->ki_u.u_cru.ru_stime.tv_usec;
496 * round and scale to 100's
498 psecs = (psecs + 5000) / 10000;
502 (void)snprintf(obuff, sizeof(obuff),
503 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
504 (void)printf("%*s", v->width, obuff);
520 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
522 /* XXX - I don't like this */
523 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
526 return (100.0 * fxtofl(p->p_pctcpu));
527 return (100.0 * fxtofl(p->p_pctcpu) /
528 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
539 (void)printf("%*.1f", v->width, getpcpu(k));
550 switch (KI_PROC(k)->p_rtprio.type) {
551 case RTP_PRIO_REALTIME:
552 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
555 nice = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
557 case RTP_PRIO_THREAD:
558 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
561 nice = KI_PROC(k)->p_nice - NZERO;
565 (void)printf("%*d", v->width, nice);
586 if ((p->p_flag & P_INMEM) == 0)
588 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
590 /* XXX don't have info about shared */
591 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
592 return (100.0 * fracmem);
603 (void)printf("%*.1f", v->width, getpmem(k));
614 (void)printf("%*ld", v->width,
615 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
627 (void)printf("%*s", v->width, "-");
638 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
652 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
656 case RTP_PRIO_REALTIME:
657 snprintf(str, sizeof(str), "real:%u", prio);
659 case RTP_PRIO_NORMAL:
660 strncpy(str, "normal", sizeof(str));
663 snprintf(str, sizeof(str), "idle:%u", prio);
666 snprintf(str, sizeof(str), "%u:%u", type, prio);
669 str[sizeof(str) - 1] = '\0';
670 (void)printf("%*s", v->width, str);
674 * Generic output routines. Print fields from various prototype
682 static char ofmt[32] = "%";
690 while ((*cp++ = *fcp++));
694 (void)printf(ofmt, v->width, *(char *)bp);
697 (void)printf(ofmt, v->width, *(u_char *)bp);
700 (void)printf(ofmt, v->width, *(short *)bp);
703 (void)printf(ofmt, v->width, *(u_short *)bp);
706 (void)printf(ofmt, v->width, *(int *)bp);
709 (void)printf(ofmt, v->width, *(u_int *)bp);
712 (void)printf(ofmt, v->width, *(long *)bp);
715 (void)printf(ofmt, v->width, *(u_long *)bp);
718 (void)printf(ofmt, v->width, *(u_long *)bp);
721 errx(1, "unknown type %d", v->type);
733 printval((char *)((char *)KI_PROC(k) + v->off), v);
744 printval((char *)((char *)KI_THREAD(k) + v->off), v);
755 printval((char *)((char *)KI_EPROC(k) + v->off), v);
767 printval((char *)((char *)&k->ki_u + v->off), v);
769 (void)printf("%*s", v->width, "-");
781 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
783 (void)printf("%*s", v->width, "-");