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