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