xref: /freebsd/usr.bin/systat/pigs.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)pigs.c	8.2 (Berkeley) 9/23/93";
35 #endif /* not lint */
36 #endif
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * Pigs display from Bill Reeves at Lucasfilm
43  */
44 
45 #include <sys/param.h>
46 #include <sys/proc.h>
47 #include <sys/sysctl.h>
48 #include <sys/time.h>
49 #include <sys/user.h>
50 
51 #include <curses.h>
52 #include <math.h>
53 #include <pwd.h>
54 #include <stdlib.h>
55 
56 #include "systat.h"
57 #include "extern.h"
58 
59 int compar(const void *, const void *);
60 
61 static int nproc;
62 static struct p_times {
63 	float pt_pctcpu;
64 	struct kinfo_proc *pt_kp;
65 } *pt;
66 
67 static int    fscale;
68 static double  lccpu;
69 
70 WINDOW *
71 openpigs(void)
72 {
73 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
74 }
75 
76 void
77 closepigs(WINDOW *w)
78 {
79 	if (w == NULL)
80 		return;
81 	wclear(w);
82 	wrefresh(w);
83 	delwin(w);
84 }
85 
86 void
87 showpigs(void)
88 {
89 	int i, j, y, k;
90 	const char *uname, *pname;
91 	char pidname[30];
92 
93 	if (pt == NULL)
94 		return;
95 
96 	qsort(pt, nproc, sizeof (struct p_times), compar);
97 	y = 1;
98 	i = nproc;
99 	if (i > getmaxy(wnd)-2)
100 		i = getmaxy(wnd)-2;
101 	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
102 		uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
103 		pname = pt[k].pt_kp->ki_comm;
104 		wmove(wnd, y, 0);
105 		wclrtoeol(wnd);
106 		mvwaddstr(wnd, y, 0, uname);
107 		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
108 		mvwaddstr(wnd, y, 9, pidname);
109 		wmove(wnd, y, 20);
110 		for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
111 			waddch(wnd, 'X');
112 	}
113 	wmove(wnd, y, 0); wclrtobot(wnd);
114 }
115 
116 int
117 initpigs(void)
118 {
119 	fixpt_t ccpu;
120 	size_t len;
121 	int err;
122 
123 	len = sizeof(ccpu);
124 	err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
125 	if (err || len != sizeof(ccpu)) {
126 		perror("kern.ccpu");
127 		return (0);
128 	}
129 
130 	len = sizeof(fscale);
131 	err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
132 	if (err || len != sizeof(fscale)) {
133 		perror("kern.fscale");
134 		return (0);
135 	}
136 
137 	lccpu = log((double) ccpu / fscale);
138 
139 	return(1);
140 }
141 
142 void
143 fetchpigs(void)
144 {
145 	int i;
146 	float ftime;
147 	float *pctp;
148 	struct kinfo_proc *kpp;
149 	static int lastnproc = 0;
150 
151 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
152 		error("%s", kvm_geterr(kd));
153 		if (pt)
154 			free(pt);
155 		return;
156 	}
157 	if (nproc > lastnproc) {
158 		free(pt);
159 		if ((pt =
160 		    malloc(nproc * sizeof(struct p_times))) == NULL) {
161 			error("Out of memory");
162 			die(0);
163 		}
164 	}
165 	lastnproc = nproc;
166 	/*
167 	 * calculate %cpu for each proc
168 	 */
169 	for (i = 0; i < nproc; i++) {
170 		pt[i].pt_kp = &kpp[i];
171 		pctp = &pt[i].pt_pctcpu;
172 		ftime = kpp[i].ki_swtime;
173 		if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
174 			*pctp = 0;
175 		else
176 			*pctp = ((double) kpp[i].ki_pctcpu /
177 					fscale) / (1.0 - exp(ftime * lccpu));
178 	}
179 }
180 
181 void
182 labelpigs(void)
183 {
184 	wmove(wnd, 0, 0);
185 	wclrtoeol(wnd);
186 	mvwaddstr(wnd, 0, 20,
187 	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
188 }
189 
190 int
191 compar(const void *a, const void *b)
192 {
193 	return (((const struct p_times *) a)->pt_pctcpu >
194 		((const struct p_times *) b)->pt_pctcpu)? -1: 1;
195 }
196