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.2 2003/06/17 04:22:50 dillon Exp $
38 #include <sys/param.h>
40 #include <sys/resource.h>
44 #include <sys/ucred.h>
46 #include <sys/sysctl.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)
100 char *cp, *vis_env, *vis_args;
105 if (ve->next == NULL) /* last field, don't pad */
106 (void)printf("%s", KI_PROC(k)->p_comm);
108 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
112 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
114 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
116 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
118 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
122 if (ve->next == NULL) {
124 if (termwidth == UNLIMITED) {
126 (void)printf("%s ", vis_env);
127 (void)printf("%s", vis_args);
129 left = termwidth - (totwidth - v->width);
130 if (left < 1) /* already wrapped, just use std width */
132 if ((cp = vis_env) != NULL) {
133 while (--left >= 0 && *cp)
134 (void)putchar(*cp++);
138 for (cp = vis_args; --left >= 0 && *cp != '\0';)
139 (void)putchar(*cp++);
143 (void)printf("%-*.*s", v->width, v->width, vis_args);
157 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
169 (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
195 if (flag & P_SINTR) /* interruptable (long) */
196 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
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);
246 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
258 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
265 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
277 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
284 return (strlen(user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0)));
297 dev = KI_EPROC(k)->e_tdev;
299 (void)printf("%*s", v->width, "??");
301 (void)snprintf(buff, sizeof(buff),
302 "%d/%d", major(dev), minor(dev));
303 (void)printf("%*s", v->width, buff);
317 dev = KI_EPROC(k)->e_tdev;
318 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
319 (void)printf("%*s ", v->width-1, "??");
321 if (strncmp(ttname, "tty", 3) == 0 ||
322 strncmp(ttname, "cua", 3) == 0)
324 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
325 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
339 dev = KI_EPROC(k)->e_tdev;
340 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
341 (void)printf("%-*s", v->width, "??");
343 (void)printf("%-*s", v->width, ttname);
356 static int use_ampm = -1;
359 if (!k->ki_u.u_valid) {
360 (void)printf("%-*s", v->width, "-");
365 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
367 then = k->ki_u.u_start.tv_sec;
368 tp = localtime(&then);
371 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
372 (void)strftime(buf, sizeof(buf) - 1,
373 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
374 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
375 (void)strftime(buf, sizeof(buf) - 1,
376 use_ampm ? "%a%I%p" : "%a%H ", tp);
378 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
379 (void)printf("%-*s", v->width, buf);
392 if (!k->ki_u.u_valid) {
393 (void)printf("%-*s", v->width, "-");
396 then = k->ki_u.u_start.tv_sec;
397 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
398 (void)printf("%-*s", v->width, buf);
409 if (KI_PROC(k)->p_wchan) {
410 if (KI_PROC(k)->p_wmesg)
411 (void)printf("%-*.*s", v->width, v->width,
412 KI_EPROC(k)->e_wmesg);
414 (void)printf("%-*lx", v->width,
415 (long)KI_PROC(k)->p_wchan);
417 (void)printf("%-*s", v->width, "-");
421 #define pgtok(a) (((a)*getpagesize())/1024)
432 (void)printf("%*d", v->width,
433 (KI_EPROC(k)->e_vm.vm_map.size/1024));
444 /* XXX don't have info about shared */
445 (void)printf("%*lu", v->width,
446 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
450 p_rssize(k, ve) /* doesn't account for text */
457 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
467 long psecs; /* "parts" of a second. first micro, then centi */
469 static char decimal_point = 0;
472 decimal_point = localeconv()->decimal_point[0];
474 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
479 * This counts time spent handling interrupts. We could
480 * fix this, but it is not 100% trivial (and interrupt
481 * time fractions only work on the sparc anyway). XXX
483 secs = KI_PROC(k)->p_runtime / 1000000;
484 psecs = KI_PROC(k)->p_runtime % 1000000;
486 secs += k->ki_u.u_cru.ru_utime.tv_sec +
487 k->ki_u.u_cru.ru_stime.tv_sec;
488 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
489 k->ki_u.u_cru.ru_stime.tv_usec;
492 * round and scale to 100's
494 psecs = (psecs + 5000) / 10000;
498 (void)snprintf(obuff, sizeof(obuff),
499 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
500 (void)printf("%*s", v->width, obuff);
516 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
518 /* XXX - I don't like this */
519 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
522 return (100.0 * fxtofl(p->p_pctcpu));
523 return (100.0 * fxtofl(p->p_pctcpu) /
524 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
535 (void)printf("%*.1f", v->width, getpcpu(k));
555 if ((p->p_flag & P_INMEM) == 0)
557 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
559 /* XXX don't have info about shared */
560 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
561 return (100.0 * fracmem);
572 (void)printf("%*.1f", v->width, getpmem(k));
583 (void)printf("%*ld", v->width,
584 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
596 (void)printf("%*s", v->width, "-");
607 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
621 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
625 case RTP_PRIO_REALTIME:
626 snprintf(str, sizeof(str), "real:%u", prio);
628 case RTP_PRIO_NORMAL:
629 strncpy(str, "normal", sizeof(str));
632 snprintf(str, sizeof(str), "idle:%u", prio);
635 snprintf(str, sizeof(str), "%u:%u", type, prio);
638 str[sizeof(str) - 1] = '\0';
639 (void)printf("%*s", v->width, str);
643 * Generic output routines. Print fields from various prototype
651 static char ofmt[32] = "%";
659 while ((*cp++ = *fcp++));
663 (void)printf(ofmt, v->width, *(char *)bp);
666 (void)printf(ofmt, v->width, *(u_char *)bp);
669 (void)printf(ofmt, v->width, *(short *)bp);
672 (void)printf(ofmt, v->width, *(u_short *)bp);
675 (void)printf(ofmt, v->width, *(int *)bp);
678 (void)printf(ofmt, v->width, *(u_int *)bp);
681 (void)printf(ofmt, v->width, *(long *)bp);
684 (void)printf(ofmt, v->width, *(u_long *)bp);
687 (void)printf(ofmt, v->width, *(u_long *)bp);
690 errx(1, "unknown type %d", v->type);
702 printval((char *)((char *)KI_PROC(k) + v->off), v);
713 printval((char *)((char *)KI_EPROC(k) + v->off), v);
725 printval((char *)((char *)&k->ki_u + v->off), v);
727 (void)printf("%*s", v->width, "-");
739 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
741 (void)printf("%*s", v->width, "-");