xref: /freebsd/usr.bin/systat/pigs.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 /*
40  * Pigs display from Bill Reeves at Lucasfilm
41  */
42 
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/sysctl.h>
46 #include <sys/time.h>
47 #include <sys/user.h>
48 
49 #include <curses.h>
50 #include <math.h>
51 #include <pwd.h>
52 #include <stdlib.h>
53 
54 #include "systat.h"
55 #include "extern.h"
56 
57 int compar(const void *, const void *);
58 
59 static int nproc;
60 static struct p_times {
61 	float pt_pctcpu;
62 	struct kinfo_proc *pt_kp;
63 } *pt = NULL;
64 
65 static int    fscale;
66 static double  lccpu;
67 
68 WINDOW *
69 openpigs(void)
70 {
71 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
72 }
73 
74 void
75 closepigs(WINDOW *w)
76 {
77 	if (w == NULL)
78 		return;
79 	wclear(w);
80 	wrefresh(w);
81 	delwin(w);
82 }
83 
84 void
85 showpigs(void)
86 {
87 	int i, j, y, k;
88 	const char *uname, *pname;
89 	char pidname[30];
90 
91 	if (nproc == 0)
92 		return;
93 
94 	qsort(pt, nproc, sizeof (struct p_times), compar);
95 	y = 1;
96 	i = nproc;
97 	if (i > getmaxy(wnd)-2)
98 		i = getmaxy(wnd)-2;
99 	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
100 		uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
101 		pname = pt[k].pt_kp->ki_comm;
102 		wmove(wnd, y, 0);
103 		wclrtoeol(wnd);
104 		mvwaddstr(wnd, y, 0, uname);
105 		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
106 		mvwaddstr(wnd, y, 9, pidname);
107 		wmove(wnd, y, 20);
108 		for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
109 			waddch(wnd, 'X');
110 	}
111 	wmove(wnd, y, 0); wclrtobot(wnd);
112 }
113 
114 int
115 initpigs(void)
116 {
117 	fixpt_t ccpu;
118 	size_t len;
119 	int err;
120 
121 	len = sizeof(ccpu);
122 	err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
123 	if (err || len != sizeof(ccpu)) {
124 		perror("kern.ccpu");
125 		return (0);
126 	}
127 
128 	len = sizeof(fscale);
129 	err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
130 	if (err || len != sizeof(fscale)) {
131 		perror("kern.fscale");
132 		return (0);
133 	}
134 
135 	lccpu = log((double) ccpu / fscale);
136 
137 	return(1);
138 }
139 
140 void
141 fetchpigs(void)
142 {
143 	int i;
144 	float ftime;
145 	float *pctp;
146 	struct kinfo_proc *kpp;
147 	static int maxnproc = 0;
148 
149 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
150 		error("%s", kvm_geterr(kd));
151 		nproc = 0;
152 		return;
153 	}
154 	if (nproc > maxnproc) {
155 		if ((pt = realloc(pt, nproc * sizeof(*pt))) == NULL) {
156 			error("Out of memory");
157 			die(0);
158 		}
159 		maxnproc = nproc;
160 	}
161 	/*
162 	 * calculate %cpu for each proc
163 	 */
164 	for (i = 0; i < nproc; i++) {
165 		pt[i].pt_kp = &kpp[i];
166 		pctp = &pt[i].pt_pctcpu;
167 		ftime = kpp[i].ki_swtime;
168 		if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
169 			*pctp = 0;
170 		else
171 			*pctp = ((double) kpp[i].ki_pctcpu /
172 					fscale) / (1.0 - exp(ftime * lccpu));
173 	}
174 }
175 
176 void
177 labelpigs(void)
178 {
179 	wmove(wnd, 0, 0);
180 	wclrtoeol(wnd);
181 	mvwaddstr(wnd, 0, 20,
182 	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
183 }
184 
185 int
186 compar(const void *a, const void *b)
187 {
188 	return (((const struct p_times *) a)->pt_pctcpu >
189 		((const struct p_times *) b)->pt_pctcpu)? -1: 1;
190 }
191