19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
3272c7c2e6SDavid Malone #include <sys/cdefs.h>
339b50d902SRodney W. Grimes /*
349b50d902SRodney W. Grimes * Pigs display from Bill Reeves at Lucasfilm
359b50d902SRodney W. Grimes */
369b50d902SRodney W. Grimes
379b50d902SRodney W. Grimes #include <sys/param.h>
38a1541efaSBruce Evans #include <sys/proc.h>
39a1541efaSBruce Evans #include <sys/sysctl.h>
409b50d902SRodney W. Grimes #include <sys/time.h>
4152a28d70SPoul-Henning Kamp #include <sys/user.h>
429b50d902SRodney W. Grimes
439b50d902SRodney W. Grimes #include <curses.h>
449b50d902SRodney W. Grimes #include <math.h>
459b50d902SRodney W. Grimes #include <pwd.h>
469b50d902SRodney W. Grimes #include <stdlib.h>
479b50d902SRodney W. Grimes
48add62273SXin LI #include "systat.h"
499b50d902SRodney W. Grimes #include "extern.h"
509b50d902SRodney W. Grimes
513f330d7dSWarner Losh int compar(const void *, const void *);
529b50d902SRodney W. Grimes
539b50d902SRodney W. Grimes static int nproc;
549b50d902SRodney W. Grimes static struct p_times {
559b50d902SRodney W. Grimes float pt_pctcpu;
569b50d902SRodney W. Grimes struct kinfo_proc *pt_kp;
57*dcc2fb37SMichael Reifenberger } *pt = NULL;
589b50d902SRodney W. Grimes
5936acf3d9SAndrew Gallatin static int fscale;
609b50d902SRodney W. Grimes static double lccpu;
619b50d902SRodney W. Grimes
629b50d902SRodney W. Grimes WINDOW *
openpigs(void)6393b9f504SXin LI openpigs(void)
649b50d902SRodney W. Grimes {
658aa22952SBruce Evans return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
669b50d902SRodney W. Grimes }
679b50d902SRodney W. Grimes
689b50d902SRodney W. Grimes void
closepigs(WINDOW * w)6993b9f504SXin LI closepigs(WINDOW *w)
709b50d902SRodney W. Grimes {
719b50d902SRodney W. Grimes if (w == NULL)
729b50d902SRodney W. Grimes return;
739b50d902SRodney W. Grimes wclear(w);
749b50d902SRodney W. Grimes wrefresh(w);
759b50d902SRodney W. Grimes delwin(w);
769b50d902SRodney W. Grimes }
779b50d902SRodney W. Grimes
789b50d902SRodney W. Grimes void
showpigs(void)7993b9f504SXin LI showpigs(void)
809b50d902SRodney W. Grimes {
8193b9f504SXin LI int i, j, y, k;
829ff712b0SMark Murray const char *uname, *pname;
839ff712b0SMark Murray char pidname[30];
849b50d902SRodney W. Grimes
85*dcc2fb37SMichael Reifenberger if (nproc == 0)
869b50d902SRodney W. Grimes return;
879b50d902SRodney W. Grimes
888be3f374SRuslan Ermilov qsort(pt, nproc, sizeof (struct p_times), compar);
899b50d902SRodney W. Grimes y = 1;
908be3f374SRuslan Ermilov i = nproc;
911e902b3bSXin LI if (i > getmaxy(wnd)-2)
921e902b3bSXin LI i = getmaxy(wnd)-2;
939b50d902SRodney W. Grimes for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
9472c7c2e6SDavid Malone uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
951f7d2501SKirk McKusick pname = pt[k].pt_kp->ki_comm;
969b50d902SRodney W. Grimes wmove(wnd, y, 0);
979b50d902SRodney W. Grimes wclrtoeol(wnd);
989b50d902SRodney W. Grimes mvwaddstr(wnd, y, 0, uname);
99448b84a0SWarner Losh snprintf(pidname, sizeof(pidname), "%10.10s", pname);
1009b50d902SRodney W. Grimes mvwaddstr(wnd, y, 9, pidname);
1019b50d902SRodney W. Grimes wmove(wnd, y, 20);
1028be3f374SRuslan Ermilov for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
1039b50d902SRodney W. Grimes waddch(wnd, 'X');
1049b50d902SRodney W. Grimes }
1059b50d902SRodney W. Grimes wmove(wnd, y, 0); wclrtobot(wnd);
1069b50d902SRodney W. Grimes }
1079b50d902SRodney W. Grimes
1089b50d902SRodney W. Grimes int
initpigs(void)10993b9f504SXin LI initpigs(void)
1109b50d902SRodney W. Grimes {
1119b50d902SRodney W. Grimes fixpt_t ccpu;
11200df2277SRobert Watson size_t len;
11300df2277SRobert Watson int err;
1149b50d902SRodney W. Grimes
11500df2277SRobert Watson len = sizeof(ccpu);
11600df2277SRobert Watson err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
11700df2277SRobert Watson if (err || len != sizeof(ccpu)) {
11800df2277SRobert Watson perror("kern.ccpu");
1199b50d902SRodney W. Grimes return (0);
1209b50d902SRodney W. Grimes }
12100df2277SRobert Watson
12200df2277SRobert Watson len = sizeof(fscale);
12300df2277SRobert Watson err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
12400df2277SRobert Watson if (err || len != sizeof(fscale)) {
12500df2277SRobert Watson perror("kern.fscale");
12600df2277SRobert Watson return (0);
1279b50d902SRodney W. Grimes }
12800df2277SRobert Watson
1299b50d902SRodney W. Grimes lccpu = log((double) ccpu / fscale);
1309b50d902SRodney W. Grimes
1319b50d902SRodney W. Grimes return(1);
1329b50d902SRodney W. Grimes }
1339b50d902SRodney W. Grimes
1349b50d902SRodney W. Grimes void
fetchpigs(void)13593b9f504SXin LI fetchpigs(void)
1369b50d902SRodney W. Grimes {
1379ff712b0SMark Murray int i;
1389ff712b0SMark Murray float ftime;
1399ff712b0SMark Murray float *pctp;
1409b50d902SRodney W. Grimes struct kinfo_proc *kpp;
141*dcc2fb37SMichael Reifenberger static int maxnproc = 0;
1429b50d902SRodney W. Grimes
1439b50d902SRodney W. Grimes if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
1449b50d902SRodney W. Grimes error("%s", kvm_geterr(kd));
145*dcc2fb37SMichael Reifenberger nproc = 0;
1469b50d902SRodney W. Grimes return;
1479b50d902SRodney W. Grimes }
148*dcc2fb37SMichael Reifenberger if (nproc > maxnproc) {
149*dcc2fb37SMichael Reifenberger if ((pt = realloc(pt, nproc * sizeof(*pt))) == NULL) {
1509b50d902SRodney W. Grimes error("Out of memory");
1519b50d902SRodney W. Grimes die(0);
1529b50d902SRodney W. Grimes }
153*dcc2fb37SMichael Reifenberger maxnproc = nproc;
1549b50d902SRodney W. Grimes }
1559b50d902SRodney W. Grimes /*
1569b50d902SRodney W. Grimes * calculate %cpu for each proc
1579b50d902SRodney W. Grimes */
1589b50d902SRodney W. Grimes for (i = 0; i < nproc; i++) {
1599b50d902SRodney W. Grimes pt[i].pt_kp = &kpp[i];
1609b50d902SRodney W. Grimes pctp = &pt[i].pt_pctcpu;
1619ff712b0SMark Murray ftime = kpp[i].ki_swtime;
162b61ce5b0SJeff Roberson if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
1639b50d902SRodney W. Grimes *pctp = 0;
1649b50d902SRodney W. Grimes else
1651f7d2501SKirk McKusick *pctp = ((double) kpp[i].ki_pctcpu /
1669ff712b0SMark Murray fscale) / (1.0 - exp(ftime * lccpu));
1679b50d902SRodney W. Grimes }
1689b50d902SRodney W. Grimes }
1699b50d902SRodney W. Grimes
1709b50d902SRodney W. Grimes void
labelpigs(void)17193b9f504SXin LI labelpigs(void)
1729b50d902SRodney W. Grimes {
1739b50d902SRodney W. Grimes wmove(wnd, 0, 0);
1749b50d902SRodney W. Grimes wclrtoeol(wnd);
1759b50d902SRodney W. Grimes mvwaddstr(wnd, 0, 20,
17665b02a0fSYaroslav Tykhiy "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
1779b50d902SRodney W. Grimes }
1789b50d902SRodney W. Grimes
1799b50d902SRodney W. Grimes int
compar(const void * a,const void * b)18093b9f504SXin LI compar(const void *a, const void *b)
1819b50d902SRodney W. Grimes {
1829ff712b0SMark Murray return (((const struct p_times *) a)->pt_pctcpu >
1839ff712b0SMark Murray ((const struct p_times *) b)->pt_pctcpu)? -1: 1;
1849b50d902SRodney W. Grimes }
185