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.17 2004/11/16 12:16:36 joerg 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 STAILQ_FOREACH(vent, &var_head, link) {
72 if (*vent->header != '\0') {
79 STAILQ_FOREACH(vent, &var_head, link) {
81 if (v->flag & LJUST) {
82 if (STAILQ_NEXT(vent, link) == NULL) /* last one */
83 printf("%s", vent->header);
85 printf("%-*s", vent->width, vent->header);
87 printf("%*s", vent->width, vent->header);
88 if (STAILQ_NEXT(vent, link) != NULL)
95 command(const KINFO *k, const struct varent *vent)
99 char *cp, *vis_env, *vis_args;
104 /* Don't pad the last field. */
105 if (STAILQ_NEXT(vent, link) == NULL)
106 printf("%s", KI_THREAD(k)->td_comm);
108 printf("%-*s", vent->width, KI_THREAD(k)->td_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 (STAILQ_NEXT(vent, link) == NULL) {
124 if (termwidth == UNLIMITED) {
126 printf("%s ", vis_env);
127 printf("%s", vis_args);
129 left = termwidth - (totwidth - vent->width);
130 if (left < 1) /* already wrapped, just use std width */
132 if ((cp = vis_env) != NULL) {
133 while (--left >= 0 && *cp)
138 for (cp = vis_args; --left >= 0 && *cp != '\0';)
143 printf("%-*.*s", vent->width, vent->width, vis_args);
150 ucomm(const KINFO *k, const struct varent *vent)
152 printf("%-*s", vent->width, KI_THREAD(k)->td_comm);
156 logname(const KINFO *k, const struct varent *vent)
158 const char *s = KI_EPROC(k)->e_login;
160 printf("%-*s", vent->width, *s != '\0' ? s : "-");
164 state(const KINFO *k, const struct varent *vent)
182 if (flag & P_SINTR) /* interruptable (long) */
183 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
184 else if (KI_THREAD(k)->td_flags & TDF_SINTR)
193 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
195 sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
209 if (!(flag & P_INMEM))
211 if (p->p_nice < NZERO)
213 else if (p->p_nice > NZERO)
217 if (flag & P_WEXIT && p->p_stat != SZOMB)
221 if ((flag & P_SYSTEM) || p->p_lock > 0)
223 if (numcpus > 1 && KI_THREAD(k)->td_mpcount_unused == 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)
232 printf("%-*s", vent->width, buf);
236 pri(const KINFO *k, const struct varent *vent)
238 printf("%*d", vent->width, KI_PROC(k)->p_priority);
242 uname(const KINFO *k, const struct varent *vent)
244 printf("%-*s", vent->width,
245 user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
249 s_uname(const KINFO *k)
251 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
255 runame(const KINFO *k, const struct varent *vent)
257 printf("%-*s", vent->width,
258 user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
262 s_runame(const KINFO *k)
264 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
268 tdev(const KINFO *k, const struct varent *vent)
273 dev = KI_EPROC(k)->e_tdev;
275 printf("%*s", vent->width, "??");
277 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
278 printf("%*s", vent->width, buff);
283 tname(const KINFO *k, const struct varent *vent)
288 dev = KI_EPROC(k)->e_tdev;
289 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
290 printf("%*s ", vent->width-1, "??");
292 if (strncmp(ttname, "tty", 3) == 0 ||
293 strncmp(ttname, "cua", 3) == 0)
295 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
296 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
301 longtname(const KINFO *k, const struct varent *vent)
306 dev = KI_EPROC(k)->e_tdev;
307 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
308 printf("%-*s", vent->width, "??");
310 printf("%-*s", vent->width, ttname);
314 started(const KINFO *k, const struct varent *vent)
320 static int use_ampm = -1;
322 if (!k->ki_u.u_valid) {
323 printf("%-*s", vent->width, "-");
328 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
330 if (KI_THREAD(k)->td_proc != NULL) {
331 then = k->ki_u.u_start.tv_sec;
333 then = KI_THREAD(k)->td_start.tv_sec;
334 if (then < btime.tv_sec) {
339 tp = localtime(&then);
342 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
343 strftime(buf, sizeof(buf) - 1,
344 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
345 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
346 strftime(buf, sizeof(buf) - 1,
347 use_ampm ? "%a%I%p" : "%a%H ", tp);
349 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
350 printf("%-*s", vent->width, buf);
354 lstarted(const KINFO *k, const struct varent *vent)
359 if (!k->ki_u.u_valid) {
360 printf("%-*s", vent->width, "-");
363 then = k->ki_u.u_start.tv_sec;
364 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
365 printf("%-*s", vent->width, buf);
369 wchan(const KINFO *k, const struct varent *vent)
371 if (KI_THREAD(k)->td_wchan) {
372 if (KI_THREAD(k)->td_wmesg)
373 printf("%-*.*s", vent->width, vent->width,
374 KI_EPROC(k)->e_wmesg);
376 printf("%-*lx", vent->width,
377 (long)KI_THREAD(k)->td_wchan);
379 printf("%-*s", vent->width, "-");
383 #define pgtok(a) (((a)*getpagesize())/1024)
387 vsize(const KINFO *k, const struct varent *vent)
389 printf("%*d", vent->width, (KI_EPROC(k)->e_vm.vm_map.size/1024));
393 rssize(const KINFO *k, const struct varent *vent)
395 /* XXX don't have info about shared */
396 printf("%*lu", vent->width, (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
400 p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */
402 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
406 cputime(const KINFO *k, const struct varent *vent)
409 long psecs; /* "parts" of a second. first micro, then centi */
411 static char decimal_point = '\0';
413 if (decimal_point == '\0')
414 decimal_point = localeconv()->decimal_point[0];
416 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
423 * This counts time spent handling interrupts. We could
424 * fix this, but it is not 100% trivial (and interrupt
425 * time fractions only work on the sparc anyway). XXX
427 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
428 secs = timeus / 1000000;
429 psecs = timeus % 1000000;
431 secs += k->ki_u.u_cru.ru_utime.tv_sec +
432 k->ki_u.u_cru.ru_stime.tv_sec;
433 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
434 k->ki_u.u_cru.ru_stime.tv_usec;
437 * round and scale to 100's
439 psecs = (psecs + 5000) / 10000;
443 snprintf(obuff, sizeof(obuff),
444 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
445 printf("%*s", vent->width, obuff);
449 getpcpu(const KINFO *k)
451 const struct proc *p;
460 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
462 /* XXX - I don't like this */
463 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
466 return (100.0 * fxtofl(p->p_pctcpu));
467 return (100.0 * fxtofl(p->p_pctcpu) /
468 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
472 pcpu(const KINFO *k, const struct varent *vent)
474 printf("%*.1f", vent->width, getpcpu(k));
478 pnice(const KINFO *k, const struct varent *vent)
482 switch (KI_PROC(k)->p_rtprio.type) {
483 case RTP_PRIO_REALTIME:
484 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
487 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
489 case RTP_PRIO_THREAD:
490 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
493 niceval = KI_PROC(k)->p_nice - NZERO;
496 printf("%*d", vent->width, niceval);
501 getpmem(const KINFO *k)
516 if ((p->p_flag & P_INMEM) == 0)
518 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
520 /* XXX don't have info about shared */
521 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
522 return (100.0 * fracmem);
526 pmem(const KINFO *k, const struct varent *vent)
528 printf("%*.1f", vent->width, getpmem(k));
532 pagein(const KINFO *k, const struct varent *vent)
534 printf("%*ld", vent->width,
535 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
540 maxrss(const KINFO *k __unused, const struct varent *vent)
543 printf("%*s", vent->width, "-");
547 tsize(const KINFO *k, const struct varent *vent)
549 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
553 rtprior(const KINFO *k, const struct varent *vent)
559 prtp = (struct rtprio *) ((char *)KI_PROC(k) + vent->var->off);
563 case RTP_PRIO_REALTIME:
564 snprintf(str, sizeof(str), "real:%u", prio);
566 case RTP_PRIO_NORMAL:
567 strncpy(str, "normal", sizeof(str));
570 snprintf(str, sizeof(str), "idle:%u", prio);
573 snprintf(str, sizeof(str), "%u:%u", type, prio);
576 str[sizeof(str) - 1] = '\0';
577 printf("%*s", vent->width, str);
581 * Generic output routines. Print fields from various prototype
585 printval(const char *bp, const struct varent *vent)
587 static char ofmt[32] = "%";
592 fcp = vent->var->fmt;
593 if (vent->var->flag & LJUST)
596 while ((*cp++ = *fcp++));
598 switch (vent->var->type) {
600 printf(ofmt, vent->width, *(const char *)bp);
603 printf(ofmt, vent->width, *(const u_char *)bp);
606 printf(ofmt, vent->width, *(const short *)bp);
609 printf(ofmt, vent->width, *(const u_short *)bp);
612 printf(ofmt, vent->width, *(const int *)bp);
615 printf(ofmt, vent->width, *(const u_int *)bp);
618 printf(ofmt, vent->width, *(const long *)bp);
621 printf(ofmt, vent->width, *(const u_long *)bp);
624 printf(ofmt, vent->width, *(const u_long *)bp);
627 errx(1, "unknown type %d", vent->var->type);
632 pvar(const KINFO *k, const struct varent *vent)
634 printval((char *)((char *)KI_PROC(k) + vent->var->off), vent);
638 tvar(const KINFO *k, const struct varent *vent)
640 printval((char *)((char *)KI_THREAD(k) + vent->var->off), vent);
644 evar(const KINFO *k, const struct varent *vent)
646 printval((char *)((char *)KI_EPROC(k) + vent->var->off), vent);
650 uvar(const KINFO *k, const struct varent *vent)
653 printval(((const char *)&k->ki_u + vent->var->off), vent);
655 printf("%*s", vent->width, "-");
659 rvar(const KINFO *k, const struct varent *vent)
662 printval(((const char *)&k->ki_u.u_ru + vent->var->off), vent);
664 printf("%*s", vent->width, "-");