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