1 /* $OpenBSD: pigs.c,v 1.16 2004/01/08 19:28:56 millert Exp $ */
2 /* $NetBSD: pigs.c,v 1.3 1995/04/29 05:54:50 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 1980, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
36 #endif
37 static char rcsid[] = "$OpenBSD: pigs.c,v 1.16 2004/01/08 19:28:56 millert Exp $";
38 #endif /* not lint */
39
40 /*
41 * Pigs display from Bill Reeves at Lucasfilm
42 */
43
44 #include <sys/param.h>
45 #include <sys/dkstat.h>
46 #include <sys/resource.h>
47 #include <sys/dir.h>
48 #include <sys/time.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51
52 #include <curses.h>
53 #include <math.h>
54 #include <pwd.h>
55 #include <err.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include "extern.h"
60 #include "systat.h"
61
62 int compar(const void *, const void *);
63
64 static int nproc;
65 static struct p_times {
66 float pt_pctcpu;
67 struct kinfo_proc2 *pt_kp;
68 } *pt;
69
70 static long stime[CPUSTATES];
71 static double lccpu;
72
73 WINDOW *
openpigs(void)74 openpigs(void)
75 {
76 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
77 }
78
79 void
closepigs(WINDOW * w)80 closepigs(WINDOW *w)
81 {
82 if (w == NULL)
83 return;
84 wclear(w);
85 wrefresh(w);
86 delwin(w);
87 }
88
89
90 void
showpigs(void)91 showpigs(void)
92 {
93 int i, j, y, k;
94 struct kinfo_proc2 *kp;
95 float total;
96 int factor;
97 char *uname, *pname, pidname[30];
98
99 if (pt == NULL)
100 return;
101 /* Accumulate the percent of cpu per user. */
102 total = 0.0;
103 for (i = 0; i <= nproc; i++) {
104 /* Accumulate the percentage. */
105 total += pt[i].pt_pctcpu;
106 }
107
108 if (total < 1.0)
109 total = 1.0;
110 factor = 50.0/total;
111
112 qsort(pt, nproc + 1, sizeof (struct p_times), compar);
113 y = 1;
114 i = nproc + 1;
115 if (i > wnd->_maxy-1)
116 i = wnd->_maxy-1;
117 for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
118 kp = pt[k].pt_kp;
119 if (kp == NULL) {
120 uname = "";
121 pname = "<idle>";
122 } else {
123 uname = user_from_uid(kp->p_uid, 0);
124 pname = kp->p_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 struct loadavg sysload;
139
140 int
initpigs(void)141 initpigs(void)
142 {
143 static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
144 static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
145 static int ccpu_mib[] = { CTL_KERN, KERN_CCPU };
146 size_t size;
147 fixpt_t ccpu;
148
149 size = sizeof(stime);
150 (void) sysctl(cp_time_mib, 2, &stime, &size, NULL, 0);
151
152 size = sizeof(sysload);
153 (void) sysctl(sysload_mib, 2, &sysload, &size, NULL, 0);
154
155 size = sizeof(ccpu);
156 (void) sysctl(ccpu_mib, 2, &ccpu, &size, NULL, 0);
157
158 lccpu = log((double) ccpu / sysload.fscale);
159
160 return(1);
161 }
162
163 void
fetchpigs(void)164 fetchpigs(void)
165 {
166 static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
167 static int lastnproc = 0;
168 struct kinfo_proc2 *kpp;
169 long ctime[CPUSTATES];
170 double t;
171 int i;
172 size_t size;
173 float *pctp;;
174
175 kpp = kvm_getproc2(kd, KERN_PROC_KTHREAD, 0, sizeof(*kpp), &nproc);
176 if (kpp == NULL) {
177 error("%s", kvm_geterr(kd));
178 if (pt)
179 free(pt);
180 return;
181 }
182 if (nproc > lastnproc) {
183 free(pt);
184 if ((pt =
185 malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
186 error("Out of memory");
187 die();
188 }
189 }
190 lastnproc = nproc;
191 /*
192 * calculate %cpu for each proc
193 */
194 for (i = 0; i < nproc; i++) {
195 pt[i].pt_kp = &kpp[i];
196 pctp = &pt[i].pt_pctcpu;
197 if (kpp->p_swtime == 0 || (kpp->p_flag & P_INMEM) == 0)
198 *pctp = 0;
199 else
200 *pctp = ((double) kpp->p_pctcpu / sysload.fscale) /
201 (1.0 - exp(kpp->p_swtime * lccpu));
202 }
203 /*
204 * and for the imaginary "idle" process
205 */
206 size = sizeof(ctime);
207 (void) sysctl(cp_time_mib, 2, &ctime, &size, NULL, 0);
208
209 t = 0;
210 for (i = 0; i < CPUSTATES; i++)
211 t += ctime[i] - stime[i];
212 if (t == 0.0)
213 t = 1.0;
214 pt[nproc].pt_kp = NULL;
215 pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
216 for (i = 0; i < CPUSTATES; i++)
217 stime[i] = ctime[i];
218 }
219
220 void
labelpigs(void)221 labelpigs(void)
222 {
223 wmove(wnd, 0, 0);
224 wclrtoeol(wnd);
225 mvwaddstr(wnd, 0, 20,
226 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
227 }
228
229 int
compar(const void * a,const void * b)230 compar(const void *a, const void *b)
231 {
232 return (((struct p_times *) a)->pt_pctcpu >
233 ((struct p_times *) b)->pt_pctcpu)? -1: 1;
234 }
235