Really fix systat(1).
[dragonfly.git] / usr.bin / systat / pigs.c
1 /*-
2  * Copyright (c) 1980, 1992, 1993
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  * @(#)pigs.c   8.2 (Berkeley) 9/23/93
34  */
35
36 /*
37  * Pigs display from Bill Reeves at Lucasfilm
38  */
39
40 #include <sys/user.h>
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/sysctl.h>
44
45 #include <curses.h>
46 #include <err.h>
47 #include <kinfo.h>
48 #include <math.h>
49 #include <nlist.h>
50 #include <pwd.h>
51 #include <stdlib.h>
52
53 #include "systat.h"
54 #include "extern.h"
55
56 int compar(const void *, const void *);
57
58 static int nproc;
59 static struct p_times {
60         float pt_pctcpu;
61         struct kinfo_proc *pt_kp;
62 } *pt;
63
64 struct kinfo_cputime old_cp_time;
65 static int fscale;
66
67 WINDOW *
68 openpigs(void)
69 {
70         return (subwin(stdscr, LINES-5-1, 0, 5, 0));
71 }
72
73 void
74 closepigs(WINDOW *w)
75 {
76         if (w == NULL)
77                 return;
78         wclear(w);
79         wrefresh(w);
80         delwin(w);
81 }
82
83
84 void
85 showpigs(void)
86 {
87         int i, j, y, k;
88         float total;
89         int factor;
90         const char *uname, *pname;
91         char pidname[30];
92
93         if (pt == NULL)
94                 return;
95         /* Accumulate the percent of cpu per user. */
96         total = 0.0;
97         for (i = 0; i <= nproc; i++) {
98                 /* Accumulate the percentage. */
99                 total += pt[i].pt_pctcpu;
100         }
101
102         if (total < 1.0)
103                 total = 1.0;
104         factor = 50.0/total;
105
106         qsort(pt, nproc + 1, sizeof (struct p_times), compar);
107         y = 1;
108         i = nproc + 1;
109         if (i > wnd->_maxy-1)
110                 i = wnd->_maxy-1;
111         for (k = 0; i > 0; i--, y++, k++) {
112                 if (pt[k].pt_pctcpu <= 0.01 &&
113                     (pt[k].pt_kp == NULL ||
114                     pt[k].pt_kp->kp_lwp.kl_slptime > 1)
115                 ) {
116                         --y;
117                         continue;
118                 }
119                 if (pt[k].pt_kp == NULL) {
120                         uname = "";
121                         pname = "<idle>";
122                 } else {
123                         uname = user_from_uid(pt[k].pt_kp->kp_uid, 0);
124                         pname = pt[k].pt_kp->kp_comm;
125                 }
126                 wmove(wnd, y, 0);
127                 wclrtoeol(wnd);
128                 mvwaddstr(wnd, y, 0, uname);
129                 snprintf(pidname, sizeof(pidname), "%10.10s", pname);
130                 mvwaddstr(wnd, y, 9, pidname);
131                 wmove(wnd, y, 20);
132                 for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
133                         waddch(wnd, 'X');
134         }
135         wmove(wnd, y, 0); wclrtobot(wnd);
136 }
137
138 int
139 initpigs(void)
140 {
141         size_t oldlen;
142
143         if (kinfo_get_sched_cputime(&old_cp_time))
144                 err(1, "kinfo_get_sched_cputime");
145
146         oldlen = sizeof(fscale);
147         if (sysctlbyname("kern.fscale", &fscale, &oldlen, NULL, 0) < 0)
148                 err(1, "sysctlbyname");
149
150         return(1);
151 }
152
153 void
154 fetchpigs(void)
155 {
156         int i;
157         float ftime;
158         float *pctp;
159         struct kinfo_proc *kpp, *pp;
160         struct kinfo_cputime cp_time, diff_cp_time;
161         double t;
162         static int lastnproc = 0;
163
164         if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
165                 error("%s", kvm_geterr(kd));
166                 if (pt)
167                         free(pt);
168                 return;
169         }
170         if (nproc > lastnproc) {
171                 free(pt);
172                 if ((pt =
173                     malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
174                         error("Out of memory");
175                         die(0);
176                 }
177         }
178         lastnproc = nproc;
179         /*
180          * calculate %cpu for each proc
181          */
182         for (i = 0; i < nproc; i++) {
183                 pt[i].pt_kp = &kpp[i];
184                 pp = &kpp[i];
185                 pctp = &pt[i].pt_pctcpu;
186                 ftime = pp->kp_swtime;
187                 if (ftime == 0 || (pp->kp_flags & P_SWAPPEDOUT))
188                         *pctp = 0;
189                 else
190                         *pctp = ((double) pp->kp_lwp.kl_pctcpu / fscale);
191         }
192         /*
193          * and for the imaginary "idle" process
194          */
195         if (kinfo_get_sched_cputime(&cp_time))
196                 err(1, "kinfo_get_sched_cputime");
197         diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
198         diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
199         diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
200         diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
201         diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
202         old_cp_time = cp_time;
203         t = diff_cp_time.cp_user + diff_cp_time.cp_nice +
204             diff_cp_time.cp_sys + diff_cp_time.cp_intr +
205             diff_cp_time.cp_idle;
206         if (t == 0.0)
207                 t = 1.0;
208         pt[nproc].pt_kp = NULL;
209         pt[nproc].pt_pctcpu = diff_cp_time.cp_idle / t;
210 }
211
212 void
213 labelpigs(void)
214 {
215         wmove(wnd, 0, 0);
216         wclrtoeol(wnd);
217         mvwaddstr(wnd, 0, 20,
218             "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
219 }
220
221 int
222 compar(const void *a, const void *b)
223 {
224         const struct p_times *pta = (const struct p_times *)a;
225         const struct p_times *ptb = (const struct p_times *)b;
226
227         /*
228          * Check overall cpu percentage first.
229          */
230         if (pta->pt_pctcpu > ptb->pt_pctcpu)
231                 return (-1);    /* a is better */
232         else if (pta->pt_pctcpu < ptb->pt_pctcpu)
233                 return (1);     /* b is better */
234         else
235                 return 0;
236
237         if (pta->pt_kp == NULL && ptb->pt_kp == NULL)
238                 return(0);
239         if (ptb->pt_kp == NULL)
240                 return(-1);     /* a is better */
241         if (pta->pt_kp == NULL)
242                 return(1);      /* b is better */
243         /*
244          * Then check sleep times and run status.
245          */
246         if (pta->pt_kp->kp_lwp.kl_slptime < ptb->pt_kp->kp_lwp.kl_slptime)
247                 return(-1);
248         if (pta->pt_kp->kp_lwp.kl_slptime > ptb->pt_kp->kp_lwp.kl_slptime)
249                 return(1);
250
251         /*
252          * Runnability
253          */
254         if (pta->pt_kp->kp_lwp.kl_stat != ptb->pt_kp->kp_lwp.kl_stat) {
255                 if (pta->pt_kp->kp_lwp.kl_stat == LSRUN)
256                         return(-1);
257                 if (ptb->pt_kp->kp_lwp.kl_stat == LSRUN)
258                         return(1);
259         }
260         return(0);
261 }