Both 'ps' and the loadav calculations got broken by thread sleeps, which
[dragonfly.git] / bin / ps / print.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
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.11 2004/06/10 22:11:39 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/stat.h>
42
43 #include <sys/ucred.h>
44 #include <sys/user.h>
45 #include <sys/sysctl.h>
46 #include <sys/rtprio.h>
47 #include <vm/vm.h>
48
49 #include <err.h>
50 #include <langinfo.h>
51 #include <locale.h>
52 #include <math.h>
53 #include <nlist.h>
54 #include <stddef.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <vis.h>
60
61 #include "ps.h"
62
63 void
64 printheader(void)
65 {
66         const VAR *v;
67         struct varent *vent;
68         int allempty;
69
70         allempty = 1;
71         for (vent = vhead; vent; vent = vent->next)
72                 if (*vent->var->header != '\0') {
73                         allempty = 0;
74                         break;
75                 }
76         if (allempty)
77                 return;
78         for (vent = vhead; vent; vent = vent->next) {
79                 v = vent->var;
80                 if (v->flag & LJUST) {
81                         if (vent->next == NULL) /* last one */
82                                 (void)printf("%s", v->header);
83                         else
84                                 (void)printf("%-*s", v->width, v->header);
85                 } else
86                         (void)printf("%*s", v->width, v->header);
87                 if (vent->next != NULL)
88                         (void)putchar(' ');
89         }
90         (void)putchar('\n');
91 }
92
93 void
94 command(const KINFO *k, const VARENT *ve)
95 {
96         const VAR *v;
97         int left;
98         char *cp, *vis_env, *vis_args;
99
100         v = ve->var;
101
102         if (cflag) {
103                 if (ve->next == NULL)   /* last field, don't pad */
104                         (void)printf("%s", KI_THREAD(k)->td_comm);
105                 else
106                         (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
107                 return;
108         }
109
110         if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
111                 err(1, NULL);
112         strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
113         if (k->ki_env) {
114                 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
115                         err(1, NULL);
116                 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
117         } else
118                 vis_env = NULL;
119
120         if (ve->next == NULL) {
121                 /* last field */
122                 if (termwidth == UNLIMITED) {
123                         if (vis_env)
124                                 (void)printf("%s ", vis_env);
125                         (void)printf("%s", vis_args);
126                 } else {
127                         left = termwidth - (totwidth - v->width);
128                         if (left < 1) /* already wrapped, just use std width */
129                                 left = v->width;
130                         if ((cp = vis_env) != NULL) {
131                                 while (--left >= 0 && *cp)
132                                         (void)putchar(*cp++);
133                                 if (--left >= 0)
134                                         putchar(' ');
135                         }
136                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
137                                 (void)putchar(*cp++);
138                 }
139         } else
140                 /* XXX env? */
141                 (void)printf("%-*.*s", v->width, v->width, vis_args);
142         free(vis_args);
143         if (vis_env != NULL)
144                 free(vis_env);
145 }
146
147 void
148 ucomm(const KINFO *k, const VARENT *ve)
149 {
150         const VAR *v;
151
152         v = ve->var;
153         (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
154 }
155
156 void
157 logname(const KINFO *k, const VARENT *ve)
158 {
159         const VAR *v;
160         char *s;
161
162         v = ve->var;
163         (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
164 }
165
166 void
167 state(const KINFO *k, const VARENT *ve)
168 {
169         struct proc *p;
170         int flag;
171         char *cp;
172         const VAR *v;
173         char buf[16];
174
175         v = ve->var;
176         p = KI_PROC(k);
177         flag = p->p_flag;
178         cp = buf;
179
180         switch (p->p_stat) {
181
182         case SSTOP:
183                 *cp = 'T';
184                 break;
185
186         case SSLEEP:
187                 if (flag & P_SINTR)     /* interruptable (long) */
188                         *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
189                 else if (KI_THREAD(k)->td_flags & TDF_SINTR)
190                         *cp = 'S';
191                 else
192                         *cp = 'D';
193                 break;
194
195         case SRUN:
196         case SIDL:
197                 *cp = 'R';
198                 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
199                     ++cp;
200                     sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
201                     while (cp[1])
202                         ++cp;
203                 }
204                 break;
205
206         case SZOMB:
207                 *cp = 'Z';
208                 break;
209
210         default:
211                 *cp = '?';
212         }
213         cp++;
214         if (!(flag & P_INMEM))
215                 *cp++ = 'W';
216         if (p->p_nice < NZERO)
217                 *cp++ = '<';
218         else if (p->p_nice > NZERO)
219                 *cp++ = 'N';
220         if (flag & P_TRACED)
221                 *cp++ = 'X';
222         if (flag & P_WEXIT && p->p_stat != SZOMB)
223                 *cp++ = 'E';
224         if (flag & P_PPWAIT)
225                 *cp++ = 'V';
226         if ((flag & P_SYSTEM) || p->p_lock > 0)
227                 *cp++ = 'L';
228         if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
229                 *cp++ = 's';
230         if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
231                 *cp++ = '+';
232         if (flag & P_JAILED)
233                 *cp++ = 'J';
234         *cp = '\0';
235         (void)printf("%-*s", v->width, buf);
236 }
237
238 void
239 pri(const KINFO *k, const VARENT *ve)
240 {
241         const VAR *v;
242
243         v = ve->var;
244         (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
245 }
246
247 void
248 uname(const KINFO *k, const VARENT *ve)
249 {
250         const VAR *v;
251
252         v = ve->var;
253         (void)printf("%-*s",
254             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
255 }
256
257 int
258 s_uname(const KINFO *k)
259 {
260             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
261 }
262
263 void
264 runame(const KINFO *k, const VARENT *ve)
265 {
266         const VAR *v;
267
268         v = ve->var;
269         (void)printf("%-*s",
270             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
271 }
272
273 int
274 s_runame(const KINFO *k)
275 {
276             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
277 }
278
279 void
280 tdev(const KINFO *k, const VARENT *ve)
281 {
282         const VAR *v;
283         dev_t dev;
284         char buff[16];
285
286         v = ve->var;
287         dev = KI_EPROC(k)->e_tdev;
288         if (dev == NODEV)
289                 (void)printf("%*s", v->width, "??");
290         else {
291                 (void)snprintf(buff, sizeof(buff),
292                     "%d/%d", major(dev), minor(dev));
293                 (void)printf("%*s", v->width, buff);
294         }
295 }
296
297 void
298 tname(const KINFO *k, const VARENT *ve)
299 {
300         const VAR *v;
301         dev_t dev;
302         char *ttname;
303
304         v = ve->var;
305         dev = KI_EPROC(k)->e_tdev;
306         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
307                 (void)printf("%*s ", v->width-1, "??");
308         else {
309                 if (strncmp(ttname, "tty", 3) == 0 ||
310                     strncmp(ttname, "cua", 3) == 0)
311                         ttname += 3;
312                 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
313                         KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
314         }
315 }
316
317 void
318 longtname(const KINFO *k, const VARENT *ve)
319 {
320         const VAR *v;
321         dev_t dev;
322         char *ttname;
323
324         v = ve->var;
325         dev = KI_EPROC(k)->e_tdev;
326         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
327                 (void)printf("%-*s", v->width, "??");
328         else
329                 (void)printf("%-*s", v->width, ttname);
330 }
331
332 void
333 started(const KINFO *k, const VARENT *ve)
334 {
335         const VAR *v;
336         static time_t now;
337         time_t then;
338         struct tm *tp;
339         char buf[100];
340         static int  use_ampm = -1;
341
342         v = ve->var;
343         if (!k->ki_u.u_valid) {
344                 (void)printf("%-*s", v->width, "-");
345                 return;
346         }
347
348         if (use_ampm < 0)
349                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
350
351         then = k->ki_u.u_start.tv_sec;
352         tp = localtime(&then);
353         if (!now)
354                 (void)time(&now);
355         if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
356                 (void)strftime(buf, sizeof(buf) - 1,
357                 use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
358         } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
359                 (void)strftime(buf, sizeof(buf) - 1,
360                 use_ampm ? "%a%I%p" : "%a%H  ", tp);
361         } else
362                 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
363         (void)printf("%-*s", v->width, buf);
364 }
365
366 void
367 lstarted(const KINFO *k, const VARENT *ve)
368 {
369         const VAR *v;
370         time_t then;
371         char buf[100];
372
373         v = ve->var;
374         if (!k->ki_u.u_valid) {
375                 (void)printf("%-*s", v->width, "-");
376                 return;
377         }
378         then = k->ki_u.u_start.tv_sec;
379         (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
380         (void)printf("%-*s", v->width, buf);
381 }
382
383 void
384 wchan(const KINFO *k, const VARENT *ve)
385 {
386         const VAR *v;
387
388         v = ve->var;
389         if (KI_THREAD(k)->td_wchan) {
390                 if (KI_THREAD(k)->td_wmesg)
391                         (void)printf("%-*.*s", v->width, v->width,
392                                       KI_EPROC(k)->e_wmesg);
393                 else
394                         (void)printf("%-*lx", v->width,
395                             (long)KI_THREAD(k)->td_wchan);
396         } else
397                 (void)printf("%-*s", v->width, "-");
398 }
399
400 #ifndef pgtok
401 #define pgtok(a)        (((a)*getpagesize())/1024)
402 #endif
403
404 void
405 vsize(const KINFO *k, const VARENT *ve)
406 {
407         const VAR *v;
408
409         v = ve->var;
410         (void)printf("%*d", v->width,
411             (KI_EPROC(k)->e_vm.vm_map.size/1024));
412 }
413
414 void
415 rssize(const KINFO *k, const VARENT *ve)
416 {
417         const VAR *v;
418
419         v = ve->var;
420         /* XXX don't have info about shared */
421         (void)printf("%*lu", v->width,
422             (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
423 }
424
425 void
426 p_rssize(const KINFO *k, const VARENT *ve)      /* doesn't account for text */
427 {
428         const VAR *v;
429
430         v = ve->var;
431         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
432 }
433
434 void
435 cputime(const KINFO *k, const VARENT *ve)
436 {
437         const VAR *v;
438         long secs;
439         long psecs;     /* "parts" of a second. first micro, then centi */
440         char obuff[128];
441         static char decimal_point = 0;
442
443         if (!decimal_point)
444                 decimal_point = localeconv()->decimal_point[0];
445         v = ve->var;
446         if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
447                 secs = 0;
448                 psecs = 0;
449         } else {
450                 u_int64_t timeus;
451
452                 /*
453                  * This counts time spent handling interrupts.  We could
454                  * fix this, but it is not 100% trivial (and interrupt
455                  * time fractions only work on the sparc anyway).       XXX
456                  */
457                 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
458                 secs = timeus / 1000000;
459                 psecs = timeus % 1000000;
460                 if (sumrusage) {
461                         secs += k->ki_u.u_cru.ru_utime.tv_sec +
462                                 k->ki_u.u_cru.ru_stime.tv_sec;
463                         psecs += k->ki_u.u_cru.ru_utime.tv_usec +
464                                 k->ki_u.u_cru.ru_stime.tv_usec;
465                 }
466                 /*
467                  * round and scale to 100's
468                  */
469                 psecs = (psecs + 5000) / 10000;
470                 secs += psecs / 100;
471                 psecs = psecs % 100;
472         }
473         (void)snprintf(obuff, sizeof(obuff),
474             "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
475         (void)printf("%*s", v->width, obuff);
476 }
477
478 double
479 getpcpu(const KINFO *k)
480 {
481         const struct proc *p;
482         static int failure;
483
484         if (!nlistread)
485                 failure = donlist();
486         if (failure)
487                 return (0.0);
488
489         p = KI_PROC(k);
490 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
491
492         /* XXX - I don't like this */
493         if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
494                 return (0.0);
495         if (rawcpu)
496                 return (100.0 * fxtofl(p->p_pctcpu));
497         return (100.0 * fxtofl(p->p_pctcpu) /
498                 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
499 }
500
501 void
502 pcpu(const KINFO *k, const VARENT *ve)
503 {
504         const VAR *v;
505
506         v = ve->var;
507         (void)printf("%*.1f", v->width, getpcpu(k));
508 }
509
510 void
511 pnice(const KINFO *k, const VARENT *ve)
512 {
513         const VAR *v;
514         int niceval;
515
516         switch (KI_PROC(k)->p_rtprio.type) {
517         case RTP_PRIO_REALTIME:
518                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
519                 break;
520         case RTP_PRIO_IDLE:
521                 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
522                 break;
523         case RTP_PRIO_THREAD:
524                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
525                 break;
526         default:
527                 niceval = KI_PROC(k)->p_nice - NZERO;
528                 break;
529         }
530         v = ve->var;
531         (void)printf("%*d", v->width, niceval);
532 }
533
534
535 double
536 getpmem(const KINFO *k)
537 {
538         static int failure;
539         struct proc *p;
540         struct eproc *e;
541         double fracmem;
542         int szptudot;
543
544         if (!nlistread)
545                 failure = donlist();
546         if (failure)
547                 return (0.0);
548
549         p = KI_PROC(k);
550         e = KI_EPROC(k);
551         if ((p->p_flag & P_INMEM) == 0)
552                 return (0.0);
553         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
554         szptudot = UPAGES;
555         /* XXX don't have info about shared */
556         fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
557         return (100.0 * fracmem);
558 }
559
560 void
561 pmem(const KINFO *k, const VARENT *ve)
562 {
563         const VAR *v;
564
565         v = ve->var;
566         (void)printf("%*.1f", v->width, getpmem(k));
567 }
568
569 void
570 pagein(const KINFO *k, const VARENT *ve)
571 {
572         const VAR *v;
573
574         v = ve->var;
575         (void)printf("%*ld", v->width,
576             k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
577 }
578
579 /* ARGSUSED */
580 void
581 maxrss(const KINFO *k, const VARENT *ve)
582 {
583         const VAR *v;
584
585         v = ve->var;
586         /* XXX not yet */
587         (void)printf("%*s", v->width, "-");
588 }
589
590 void
591 tsize(const KINFO *k, const VARENT *ve)
592 {
593         const VAR *v;
594
595         v = ve->var;
596         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
597 }
598
599 void
600 rtprior(const KINFO *k, const VARENT *ve)
601 {
602         const VAR *v;
603         struct rtprio *prtp;
604         char str[8];
605         unsigned prio, type;
606  
607         v = ve->var;
608         prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
609         prio = prtp->prio;
610         type = prtp->type;
611         switch (type) {
612         case RTP_PRIO_REALTIME:
613                 snprintf(str, sizeof(str), "real:%u", prio);
614                 break;
615         case RTP_PRIO_NORMAL:
616                 strncpy(str, "normal", sizeof(str));
617                 break;
618         case RTP_PRIO_IDLE:
619                 snprintf(str, sizeof(str), "idle:%u", prio);
620                 break;
621         default:
622                 snprintf(str, sizeof(str), "%u:%u", type, prio);
623                 break;
624         }
625         str[sizeof(str) - 1] = '\0';
626         (void)printf("%*s", v->width, str);
627 }
628
629 /*
630  * Generic output routines.  Print fields from various prototype
631  * structures.
632  */
633 static void
634 printval(const char *bp, const VAR *v)
635 {
636         static char ofmt[32] = "%";
637         const char *fcp;
638         char *cp;
639
640         cp = ofmt + 1;
641         fcp = v->fmt;
642         if (v->flag & LJUST)
643                 *cp++ = '-';
644         *cp++ = '*';
645         while ((*cp++ = *fcp++));
646
647         switch (v->type) {
648         case CHAR:
649                 (void)printf(ofmt, v->width, *(const char *)bp);
650                 break;
651         case UCHAR:
652                 (void)printf(ofmt, v->width, *(const u_char *)bp);
653                 break;
654         case SHORT:
655                 (void)printf(ofmt, v->width, *(const short *)bp);
656                 break;
657         case USHORT:
658                 (void)printf(ofmt, v->width, *(const u_short *)bp);
659                 break;
660         case INT:
661                 (void)printf(ofmt, v->width, *(const int *)bp);
662                 break;
663         case UINT:
664                 (void)printf(ofmt, v->width, *(const u_int *)bp);
665                 break;
666         case LONG:
667                 (void)printf(ofmt, v->width, *(const long *)bp);
668                 break;
669         case ULONG:
670                 (void)printf(ofmt, v->width, *(const u_long *)bp);
671                 break;
672         case KPTR:
673                 (void)printf(ofmt, v->width, *(const u_long *)bp);
674                 break;
675         default:
676                 errx(1, "unknown type %d", v->type);
677         }
678 }
679
680 void
681 pvar(const KINFO *k, const VARENT *ve)
682 {
683         const VAR *v;
684
685         v = ve->var;
686         printval((char *)((char *)KI_PROC(k) + v->off), v);
687 }
688
689 void
690 tvar(const KINFO *k, const VARENT *ve)
691 {
692         const VAR *v;
693
694         v = ve->var;
695         printval((char *)((char *)KI_THREAD(k) + v->off), v);
696 }
697
698 void
699 evar(const KINFO *k, const VARENT *ve)
700 {
701         const VAR *v;
702
703         v = ve->var;
704         printval((char *)((char *)KI_EPROC(k) + v->off), v);
705 }
706
707 void
708 uvar(const KINFO *k, const VARENT *ve)
709 {
710         const VAR *v;
711
712         v = ve->var;
713         if (k->ki_u.u_valid)
714                 printval(((const char *)&k->ki_u + v->off), v);
715         else
716                 (void)printf("%*s", v->width, "-");
717 }
718
719 void
720 rvar(const KINFO *k, const VARENT *ve)
721 {
722         const VAR *v;
723
724         v = ve->var;
725         if (k->ki_u.u_valid)
726                 printval(((const char *)&k->ki_u.u_ru + v->off), v);
727         else
728                 (void)printf("%*s", v->width, "-");
729 }