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.18 2004/11/22 06:50:12 dillon 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 * Normalized priority (lower is better). For pure threads
237 * output a negated LWKT priority (so lower still means better).
240 pri(const KINFO *k, const struct varent *vent)
242 if (KI_THREAD(k)->td_proc)
243 printf("%*d", vent->width, KI_PROC(k)->p_priority);
245 printf("%*d", vent->width, -(KI_THREAD(k)->td_pri & TDPRI_MASK));
249 tdpri(const KINFO *k, const struct varent *vent)
252 int val = KI_THREAD(k)->td_pri;
254 snprintf(buf, sizeof(buf), "%02d/%d", val & TDPRI_MASK, val / TDPRI_CRIT);
255 printf("%*s", vent->width, buf);
259 uname(const KINFO *k, const struct varent *vent)
261 printf("%-*s", vent->width,
262 user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
266 s_uname(const KINFO *k)
268 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
272 runame(const KINFO *k, const struct varent *vent)
274 printf("%-*s", vent->width,
275 user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
279 s_runame(const KINFO *k)
281 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
285 tdev(const KINFO *k, const struct varent *vent)
290 dev = KI_EPROC(k)->e_tdev;
292 printf("%*s", vent->width, "??");
294 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
295 printf("%*s", vent->width, buff);
300 tname(const KINFO *k, const struct varent *vent)
305 dev = KI_EPROC(k)->e_tdev;
306 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
307 printf("%*s ", vent->width-1, "??");
309 if (strncmp(ttname, "tty", 3) == 0 ||
310 strncmp(ttname, "cua", 3) == 0)
312 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
313 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
318 longtname(const KINFO *k, const struct varent *vent)
323 dev = KI_EPROC(k)->e_tdev;
324 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
325 printf("%-*s", vent->width, "??");
327 printf("%-*s", vent->width, ttname);
331 started(const KINFO *k, const struct varent *vent)
337 static int use_ampm = -1;
339 if (!k->ki_u.u_valid) {
340 printf("%-*s", vent->width, "-");
345 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
347 if (KI_THREAD(k)->td_proc != NULL) {
348 then = k->ki_u.u_start.tv_sec;
350 then = KI_THREAD(k)->td_start.tv_sec;
351 if (then < btime.tv_sec) {
356 tp = localtime(&then);
359 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
360 strftime(buf, sizeof(buf) - 1,
361 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
362 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
363 strftime(buf, sizeof(buf) - 1,
364 use_ampm ? "%a%I%p" : "%a%H ", tp);
366 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
367 printf("%-*s", vent->width, buf);
371 lstarted(const KINFO *k, const struct varent *vent)
376 if (!k->ki_u.u_valid) {
377 printf("%-*s", vent->width, "-");
380 then = k->ki_u.u_start.tv_sec;
381 strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
382 printf("%-*s", vent->width, buf);
386 wchan(const KINFO *k, const struct varent *vent)
388 if (KI_THREAD(k)->td_wchan) {
389 if (KI_THREAD(k)->td_wmesg)
390 printf("%-*.*s", vent->width, vent->width,
391 KI_EPROC(k)->e_wmesg);
393 printf("%-*lx", vent->width,
394 (long)KI_THREAD(k)->td_wchan);
396 printf("%-*s", vent->width, "-");
400 #define pgtok(a) (((a)*getpagesize())/1024)
404 vsize(const KINFO *k, const struct varent *vent)
406 printf("%*d", vent->width, (KI_EPROC(k)->e_vm.vm_map.size/1024));
410 rssize(const KINFO *k, const struct varent *vent)
412 /* XXX don't have info about shared */
413 printf("%*lu", vent->width, (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
417 p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */
419 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
423 cputime(const KINFO *k, const struct varent *vent)
426 long psecs; /* "parts" of a second. first micro, then centi */
428 static char decimal_point = '\0';
430 if (decimal_point == '\0')
431 decimal_point = localeconv()->decimal_point[0];
433 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
440 * This counts time spent handling interrupts. We could
441 * fix this, but it is not 100% trivial (and interrupt
442 * time fractions only work on the sparc anyway). XXX
444 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
445 secs = timeus / 1000000;
446 psecs = timeus % 1000000;
448 secs += k->ki_u.u_cru.ru_utime.tv_sec +
449 k->ki_u.u_cru.ru_stime.tv_sec;
450 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
451 k->ki_u.u_cru.ru_stime.tv_usec;
454 * round and scale to 100's
456 psecs = (psecs + 5000) / 10000;
460 snprintf(obuff, sizeof(obuff),
461 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
462 printf("%*s", vent->width, obuff);
466 getpcpu(const KINFO *k)
468 const struct proc *p;
477 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
479 /* XXX - I don't like this */
480 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
483 return (100.0 * fxtofl(p->p_pctcpu));
484 return (100.0 * fxtofl(p->p_pctcpu) /
485 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
489 pcpu(const KINFO *k, const struct varent *vent)
491 printf("%*.1f", vent->width, getpcpu(k));
495 pnice(const KINFO *k, const struct varent *vent)
499 switch (KI_PROC(k)->p_rtprio.type) {
500 case RTP_PRIO_REALTIME:
501 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
504 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
506 case RTP_PRIO_THREAD:
507 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
510 niceval = KI_PROC(k)->p_nice - NZERO;
513 printf("%*d", vent->width, niceval);
518 getpmem(const KINFO *k)
533 if ((p->p_flag & P_INMEM) == 0)
535 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
537 /* XXX don't have info about shared */
538 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
539 return (100.0 * fracmem);
543 pmem(const KINFO *k, const struct varent *vent)
545 printf("%*.1f", vent->width, getpmem(k));
549 pagein(const KINFO *k, const struct varent *vent)
551 printf("%*ld", vent->width,
552 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
557 maxrss(const KINFO *k __unused, const struct varent *vent)
560 printf("%*s", vent->width, "-");
564 tsize(const KINFO *k, const struct varent *vent)
566 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
570 rtprior(const KINFO *k, const struct varent *vent)
576 prtp = (struct rtprio *) ((char *)KI_PROC(k) + vent->var->off);
580 case RTP_PRIO_REALTIME:
581 snprintf(str, sizeof(str), "real:%u", prio);
583 case RTP_PRIO_NORMAL:
584 strncpy(str, "normal", sizeof(str));
587 snprintf(str, sizeof(str), "idle:%u", prio);
590 snprintf(str, sizeof(str), "%u:%u", type, prio);
593 str[sizeof(str) - 1] = '\0';
594 printf("%*s", vent->width, str);
598 * Generic output routines. Print fields from various prototype
602 printval(const char *bp, const struct varent *vent)
604 static char ofmt[32] = "%";
609 fcp = vent->var->fmt;
610 if (vent->var->flag & LJUST)
613 while ((*cp++ = *fcp++));
615 switch (vent->var->type) {
617 printf(ofmt, vent->width, *(const char *)bp);
620 printf(ofmt, vent->width, *(const u_char *)bp);
623 printf(ofmt, vent->width, *(const short *)bp);
626 printf(ofmt, vent->width, *(const u_short *)bp);
629 printf(ofmt, vent->width, *(const int *)bp);
632 printf(ofmt, vent->width, *(const u_int *)bp);
635 printf(ofmt, vent->width, *(const long *)bp);
638 printf(ofmt, vent->width, *(const u_long *)bp);
641 printf(ofmt, vent->width, *(const u_long *)bp);
644 errx(1, "unknown type %d", vent->var->type);
649 pvar(const KINFO *k, const struct varent *vent)
651 printval((char *)((char *)KI_PROC(k) + vent->var->off), vent);
655 tvar(const KINFO *k, const struct varent *vent)
657 printval((char *)((char *)KI_THREAD(k) + vent->var->off), vent);
661 evar(const KINFO *k, const struct varent *vent)
663 printval((char *)((char *)KI_EPROC(k) + vent->var->off), vent);
667 uvar(const KINFO *k, const struct varent *vent)
670 printval(((const char *)&k->ki_u + vent->var->off), vent);
672 printf("%*s", vent->width, "-");
676 rvar(const KINFO *k, const struct varent *vent)
679 printval(((const char *)&k->ki_u.u_ru + vent->var->off), vent);
681 printf("%*s", vent->width, "-");