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.6 2003/07/19 22:21:21 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';
205 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
207 sprintf(cp, "%d", KI_THREAD(k)->td_cpu);
221 if (!(flag & P_INMEM))
223 if (p->p_nice < NZERO)
225 else if (p->p_nice > NZERO)
229 if (flag & P_WEXIT && p->p_stat != SZOMB)
233 if ((flag & P_SYSTEM) || p->p_lock > 0)
235 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
237 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
242 (void)printf("%-*s", v->width, buf);
253 (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
265 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
272 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
284 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
291 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
304 dev = KI_EPROC(k)->e_tdev;
306 (void)printf("%*s", v->width, "??");
308 (void)snprintf(buff, sizeof(buff),
309 "%d/%d", major(dev), minor(dev));
310 (void)printf("%*s", v->width, buff);
324 dev = KI_EPROC(k)->e_tdev;
325 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
326 (void)printf("%*s ", v->width-1, "??");
328 if (strncmp(ttname, "tty", 3) == 0 ||
329 strncmp(ttname, "cua", 3) == 0)
331 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
332 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
346 dev = KI_EPROC(k)->e_tdev;
347 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
348 (void)printf("%-*s", v->width, "??");
350 (void)printf("%-*s", v->width, ttname);
363 static int use_ampm = -1;
366 if (!k->ki_u.u_valid) {
367 (void)printf("%-*s", v->width, "-");
372 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
374 then = k->ki_u.u_start.tv_sec;
375 tp = localtime(&then);
378 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
379 (void)strftime(buf, sizeof(buf) - 1,
380 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
381 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
382 (void)strftime(buf, sizeof(buf) - 1,
383 use_ampm ? "%a%I%p" : "%a%H ", tp);
385 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
386 (void)printf("%-*s", v->width, buf);
399 if (!k->ki_u.u_valid) {
400 (void)printf("%-*s", v->width, "-");
403 then = k->ki_u.u_start.tv_sec;
404 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
405 (void)printf("%-*s", v->width, buf);
416 if (KI_THREAD(k)->td_wchan) {
417 if (KI_THREAD(k)->td_wmesg)
418 (void)printf("%-*.*s", v->width, v->width,
419 KI_EPROC(k)->e_wmesg);
421 (void)printf("%-*lx", v->width,
422 (long)KI_THREAD(k)->td_wchan);
424 (void)printf("%-*s", v->width, "-");
428 #define pgtok(a) (((a)*getpagesize())/1024)
439 (void)printf("%*d", v->width,
440 (KI_EPROC(k)->e_vm.vm_map.size/1024));
451 /* XXX don't have info about shared */
452 (void)printf("%*lu", v->width,
453 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
457 p_rssize(k, ve) /* doesn't account for text */
464 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
474 long psecs; /* "parts" of a second. first micro, then centi */
476 static char decimal_point = 0;
479 decimal_point = localeconv()->decimal_point[0];
481 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
488 * This counts time spent handling interrupts. We could
489 * fix this, but it is not 100% trivial (and interrupt
490 * time fractions only work on the sparc anyway). XXX
492 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
493 secs = timeus / 1000000;
494 psecs = timeus % 1000000;
496 secs += k->ki_u.u_cru.ru_utime.tv_sec +
497 k->ki_u.u_cru.ru_stime.tv_sec;
498 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
499 k->ki_u.u_cru.ru_stime.tv_usec;
502 * round and scale to 100's
504 psecs = (psecs + 5000) / 10000;
508 (void)snprintf(obuff, sizeof(obuff),
509 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
510 (void)printf("%*s", v->width, obuff);
526 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
528 /* XXX - I don't like this */
529 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
532 return (100.0 * fxtofl(p->p_pctcpu));
533 return (100.0 * fxtofl(p->p_pctcpu) /
534 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
545 (void)printf("%*.1f", v->width, getpcpu(k));
556 switch (KI_PROC(k)->p_rtprio.type) {
557 case RTP_PRIO_REALTIME:
558 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
561 nice = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
563 case RTP_PRIO_THREAD:
564 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
567 nice = KI_PROC(k)->p_nice - NZERO;
571 (void)printf("%*d", v->width, nice);
592 if ((p->p_flag & P_INMEM) == 0)
594 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
596 /* XXX don't have info about shared */
597 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
598 return (100.0 * fracmem);
609 (void)printf("%*.1f", v->width, getpmem(k));
620 (void)printf("%*ld", v->width,
621 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
633 (void)printf("%*s", v->width, "-");
644 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
658 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
662 case RTP_PRIO_REALTIME:
663 snprintf(str, sizeof(str), "real:%u", prio);
665 case RTP_PRIO_NORMAL:
666 strncpy(str, "normal", sizeof(str));
669 snprintf(str, sizeof(str), "idle:%u", prio);
672 snprintf(str, sizeof(str), "%u:%u", type, prio);
675 str[sizeof(str) - 1] = '\0';
676 (void)printf("%*s", v->width, str);
680 * Generic output routines. Print fields from various prototype
688 static char ofmt[32] = "%";
696 while ((*cp++ = *fcp++));
700 (void)printf(ofmt, v->width, *(char *)bp);
703 (void)printf(ofmt, v->width, *(u_char *)bp);
706 (void)printf(ofmt, v->width, *(short *)bp);
709 (void)printf(ofmt, v->width, *(u_short *)bp);
712 (void)printf(ofmt, v->width, *(int *)bp);
715 (void)printf(ofmt, v->width, *(u_int *)bp);
718 (void)printf(ofmt, v->width, *(long *)bp);
721 (void)printf(ofmt, v->width, *(u_long *)bp);
724 (void)printf(ofmt, v->width, *(u_long *)bp);
727 errx(1, "unknown type %d", v->type);
739 printval((char *)((char *)KI_PROC(k) + v->off), v);
750 printval((char *)((char *)KI_THREAD(k) + v->off), v);
761 printval((char *)((char *)KI_EPROC(k) + v->off), v);
773 printval((char *)((char *)&k->ki_u + v->off), v);
775 (void)printf("%*s", v->width, "-");
787 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
789 (void)printf("%*s", v->width, "-");