xref: /dragonfly/usr.bin/systat/pigs.c (revision e0b1d537)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)pigs.c	8.2 (Berkeley) 9/23/93
30  */
31 
32 /*
33  * Pigs display from Bill Reeves at Lucasfilm
34  */
35 
36 #include <sys/user.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/sysctl.h>
40 
41 #include <curses.h>
42 #include <err.h>
43 #include <kinfo.h>
44 #include <math.h>
45 #include <nlist.h>
46 #include <pwd.h>
47 #include <stdlib.h>
48 
49 #include "systat.h"
50 #include "extern.h"
51 
52 int compar(const void *, const void *);
53 
54 static int nproc;
55 static struct p_times {
56 	float pt_pctcpu;
57 	struct kinfo_proc *pt_kp;
58 } *pt;
59 
60 struct kinfo_cputime old_cp_time;
61 static int fscale;
62 
63 WINDOW *
64 openpigs(void)
65 {
66 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
67 }
68 
69 void
70 closepigs(WINDOW *w)
71 {
72 	if (w == NULL)
73 		return;
74 	wclear(w);
75 	wrefresh(w);
76 	delwin(w);
77 }
78 
79 
80 void
81 showpigs(void)
82 {
83 	int i, j, y, k;
84 	float total;
85 	int factor;
86 	const char *uname, *pname;
87 	char pidname[30];
88 
89 	if (pt == NULL)
90 		return;
91 	/* Accumulate the percent of cpu per user. */
92 	total = 0.0;
93 	for (i = 0; i <= nproc; i++) {
94 		/* Accumulate the percentage. */
95 		total += pt[i].pt_pctcpu;
96 	}
97 
98 	if (total < 1.0)
99  		total = 1.0;
100 	factor = 50.0/total;
101 
102         qsort(pt, nproc + 1, sizeof (struct p_times), compar);
103 	y = 1;
104 	i = nproc + 1;
105 	if (i > wnd->_maxy-1)
106 		i = wnd->_maxy-1;
107 	for (k = 0; i > 0; i--, y++, k++) {
108 		if (pt[k].pt_pctcpu <= 0.01 &&
109 		    (pt[k].pt_kp == NULL ||
110 		    pt[k].pt_kp->kp_lwp.kl_slptime > 1)
111 		) {
112 			--y;
113 			continue;
114 		}
115 		if (pt[k].pt_kp == NULL) {
116 			uname = "";
117 			pname = "<idle>";
118 		} else {
119 			uname = user_from_uid(pt[k].pt_kp->kp_uid, 0);
120 			pname = pt[k].pt_kp->kp_comm;
121 		}
122 		wmove(wnd, y, 0);
123 		wclrtoeol(wnd);
124 		mvwaddstr(wnd, y, 0, uname);
125 		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
126 		mvwaddstr(wnd, y, 9, pidname);
127 		wmove(wnd, y, 20);
128 		for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
129 			waddch(wnd, 'X');
130 	}
131 	wmove(wnd, y, 0); wclrtobot(wnd);
132 }
133 
134 int
135 initpigs(void)
136 {
137 	size_t oldlen;
138 
139 	if (kinfo_get_sched_cputime(&old_cp_time))
140 		err(1, "kinfo_get_sched_cputime");
141 
142 	oldlen = sizeof(fscale);
143 	if (sysctlbyname("kern.fscale", &fscale, &oldlen, NULL, 0) < 0)
144 		err(1, "sysctlbyname");
145 
146 	return(1);
147 }
148 
149 void
150 fetchpigs(void)
151 {
152 	int i;
153 	float ftime;
154 	float *pctp;
155 	struct kinfo_proc *kpp, *pp;
156 	struct kinfo_cputime cp_time, diff_cp_time;
157 	double t;
158 	static int lastnproc = 0;
159 
160 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
161 		error("%s", kvm_geterr(kd));
162 		if (pt)
163 			free(pt);
164 		return;
165 	}
166 	if (nproc > lastnproc) {
167 		free(pt);
168 		if ((pt =
169 		    malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
170 			error("Out of memory");
171 			die(0);
172 		}
173 	}
174 	lastnproc = nproc;
175 	/*
176 	 * calculate %cpu for each proc
177 	 */
178 	for (i = 0; i < nproc; i++) {
179 		pt[i].pt_kp = &kpp[i];
180 		pp = &kpp[i];
181 		pctp = &pt[i].pt_pctcpu;
182 		ftime = pp->kp_swtime;
183 		if (ftime == 0 || (pp->kp_flags & P_SWAPPEDOUT))
184 			*pctp = 0;
185 		else
186 			*pctp = ((double) pp->kp_lwp.kl_pctcpu / fscale);
187 	}
188 	/*
189 	 * and for the imaginary "idle" process
190 	 */
191 	if (kinfo_get_sched_cputime(&cp_time))
192 		err(1, "kinfo_get_sched_cputime");
193 	diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
194 	diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
195 	diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
196 	diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
197 	diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
198 	old_cp_time = cp_time;
199 	t = diff_cp_time.cp_user + diff_cp_time.cp_nice +
200 	    diff_cp_time.cp_sys + diff_cp_time.cp_intr +
201 	    diff_cp_time.cp_idle;
202 	if (t == 0.0)
203 		t = 1.0;
204 	pt[nproc].pt_kp = NULL;
205 	pt[nproc].pt_pctcpu = diff_cp_time.cp_idle / t;
206 }
207 
208 void
209 labelpigs(void)
210 {
211 	wmove(wnd, 0, 0);
212 	wclrtoeol(wnd);
213 	mvwaddstr(wnd, 0, 20,
214 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
215 }
216 
217 int
218 compar(const void *a, const void *b)
219 {
220 	const struct p_times *pta = (const struct p_times *)a;
221 	const struct p_times *ptb = (const struct p_times *)b;
222 
223 	/*
224 	 * Check overall cpu percentage first.
225 	 */
226 	if (pta->pt_pctcpu > ptb->pt_pctcpu)
227 		return (-1);	/* a is better */
228 	else if (pta->pt_pctcpu < ptb->pt_pctcpu)
229 		return (1);	/* b is better */
230 	else
231 		return 0;
232 
233 	if (pta->pt_kp == NULL && ptb->pt_kp == NULL)
234 		return(0);
235 	if (ptb->pt_kp == NULL)
236 		return(-1);	/* a is better */
237 	if (pta->pt_kp == NULL)
238 		return(1);	/* b is better */
239 	/*
240 	 * Then check sleep times and run status.
241 	 */
242 	if (pta->pt_kp->kp_lwp.kl_slptime < ptb->pt_kp->kp_lwp.kl_slptime)
243 		return(-1);
244 	if (pta->pt_kp->kp_lwp.kl_slptime > ptb->pt_kp->kp_lwp.kl_slptime)
245 		return(1);
246 
247 	/*
248 	 * Runnability
249 	 */
250 	if (pta->pt_kp->kp_lwp.kl_stat != ptb->pt_kp->kp_lwp.kl_stat) {
251 		if (pta->pt_kp->kp_lwp.kl_stat == LSRUN)
252 			return(-1);
253 		if (ptb->pt_kp->kp_lwp.kl_stat == LSRUN)
254 			return(1);
255 	}
256 	return(0);
257 }
258