1 /*- 2 * Copyright (c) 1982, 1986, 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Copyright (c) 2002 Networks Associates Technologies, Inc. 11 * All rights reserved. 12 * 13 * Portions of this software were developed for the FreeBSD Project by 14 * ThinkSec AS and NAI Labs, the Security Research Division of Network 15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 16 * ("CBOSS"), as part of the DARPA CHATS research program. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/proc.h> 50 #include <sys/resourcevar.h> 51 #include <sys/sched.h> 52 #include <sys/systm.h> 53 #include <sys/tty.h> 54 55 #include <vm/vm.h> 56 #include <vm/pmap.h> 57 #include <vm/vm_map.h> 58 59 /* 60 * Returns 1 if p2 is "better" than p1 61 * 62 * The algorithm for picking the "interesting" process is thus: 63 * 64 * 1) Only foreground processes are eligible - implied. 65 * 2) Runnable processes are favored over anything else. The runner 66 * with the highest cpu utilization is picked (p_estcpu). Ties are 67 * broken by picking the highest pid. 68 * 3) The sleeper with the shortest sleep time is next. With ties, 69 * we pick out just "short-term" sleepers (P_SINTR == 0). 70 * 4) Further ties are broken by picking the highest pid. 71 */ 72 73 #define TESTAB(a, b) ((a)<<1 | (b)) 74 #define ONLYA 2 75 #define ONLYB 1 76 #define BOTH 3 77 78 static int 79 proc_sum(struct proc *p, fixpt_t *estcpup) 80 { 81 struct thread *td; 82 int estcpu; 83 int val; 84 85 val = 0; 86 estcpu = 0; 87 FOREACH_THREAD_IN_PROC(p, td) { 88 thread_lock(td); 89 if (TD_ON_RUNQ(td) || 90 TD_IS_RUNNING(td)) 91 val = 1; 92 estcpu += sched_pctcpu(td); 93 thread_unlock(td); 94 } 95 *estcpup = estcpu; 96 97 return (val); 98 } 99 100 static int 101 thread_compare(struct thread *td, struct thread *td2) 102 { 103 int runa, runb; 104 int slpa, slpb; 105 fixpt_t esta, estb; 106 107 if (td == NULL) 108 return (1); 109 110 /* 111 * Fetch running stats, pctcpu usage, and interruptable flag. 112 */ 113 thread_lock(td); 114 runa = TD_IS_RUNNING(td) | TD_ON_RUNQ(td); 115 slpa = td->td_flags & TDF_SINTR; 116 esta = sched_pctcpu(td); 117 thread_unlock(td); 118 thread_lock(td2); 119 runb = TD_IS_RUNNING(td2) | TD_ON_RUNQ(td2); 120 estb = sched_pctcpu(td2); 121 slpb = td2->td_flags & TDF_SINTR; 122 thread_unlock(td2); 123 /* 124 * see if at least one of them is runnable 125 */ 126 switch (TESTAB(runa, runb)) { 127 case ONLYA: 128 return (0); 129 case ONLYB: 130 return (1); 131 case BOTH: 132 break; 133 } 134 /* 135 * favor one with highest recent cpu utilization 136 */ 137 if (estb > esta) 138 return (1); 139 if (esta > estb) 140 return (0); 141 /* 142 * favor one sleeping in a non-interruptible sleep 143 */ 144 switch (TESTAB(slpa, slpb)) { 145 case ONLYA: 146 return (0); 147 case ONLYB: 148 return (1); 149 case BOTH: 150 break; 151 } 152 153 return (td < td2); 154 } 155 156 static int 157 proc_compare(struct proc *p1, struct proc *p2) 158 { 159 160 int runa, runb; 161 fixpt_t esta, estb; 162 163 if (p1 == NULL) 164 return (1); 165 166 /* 167 * Fetch various stats about these processes. After we drop the 168 * lock the information could be stale but the race is unimportant. 169 */ 170 PROC_LOCK(p1); 171 runa = proc_sum(p1, &esta); 172 PROC_UNLOCK(p1); 173 PROC_LOCK(p2); 174 runb = proc_sum(p2, &estb); 175 PROC_UNLOCK(p2); 176 177 /* 178 * see if at least one of them is runnable 179 */ 180 switch (TESTAB(runa, runb)) { 181 case ONLYA: 182 return (0); 183 case ONLYB: 184 return (1); 185 case BOTH: 186 break; 187 } 188 /* 189 * favor one with highest recent cpu utilization 190 */ 191 if (estb > esta) 192 return (1); 193 if (esta > estb) 194 return (0); 195 /* 196 * weed out zombies 197 */ 198 switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) { 199 case ONLYA: 200 return (1); 201 case ONLYB: 202 return (0); 203 case BOTH: 204 break; 205 } 206 207 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 208 } 209 210 /* 211 * Report on state of foreground process group. 212 */ 213 void 214 tty_info(struct tty *tp) 215 { 216 struct timeval utime, stime; 217 struct proc *p, *pick; 218 struct thread *td, *picktd; 219 const char *stateprefix, *state; 220 long rss; 221 int load, pctcpu; 222 pid_t pid; 223 char comm[MAXCOMLEN + 1]; 224 struct rusage ru; 225 226 tty_lock_assert(tp, MA_OWNED); 227 228 if (tty_checkoutq(tp) == 0) 229 return; 230 231 /* Print load average. */ 232 load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 233 ttyprintf(tp, "load: %d.%02d ", load / 100, load % 100); 234 235 if (tp->t_session == NULL) { 236 ttyprintf(tp, "not a controlling terminal\n"); 237 return; 238 } 239 if (tp->t_pgrp == NULL) { 240 ttyprintf(tp, "no foreground process group\n"); 241 return; 242 } 243 PGRP_LOCK(tp->t_pgrp); 244 if (LIST_EMPTY(&tp->t_pgrp->pg_members)) { 245 PGRP_UNLOCK(tp->t_pgrp); 246 ttyprintf(tp, "empty foreground process group\n"); 247 return; 248 } 249 250 /* 251 * Pick the most interesting process and copy some of its 252 * state for printing later. This operation could rely on stale 253 * data as we can't hold the proc slock or thread locks over the 254 * whole list. However, we're guaranteed not to reference an exited 255 * thread or proc since we hold the tty locked. 256 */ 257 pick = NULL; 258 LIST_FOREACH(p, &tp->t_pgrp->pg_members, p_pglist) 259 if (proc_compare(pick, p)) 260 pick = p; 261 262 PROC_LOCK(pick); 263 picktd = NULL; 264 FOREACH_THREAD_IN_PROC(pick, td) 265 if (thread_compare(picktd, td)) 266 picktd = td; 267 td = picktd; 268 stateprefix = ""; 269 thread_lock(td); 270 if (TD_IS_RUNNING(td)) 271 state = "running"; 272 else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td)) 273 state = "runnable"; 274 else if (TD_IS_SLEEPING(td)) { 275 /* XXX: If we're sleeping, are we ever not in a queue? */ 276 if (TD_ON_SLEEPQ(td)) 277 state = td->td_wmesg; 278 else 279 state = "sleeping without queue"; 280 } else if (TD_ON_LOCK(td)) { 281 state = td->td_lockname; 282 stateprefix = "*"; 283 } else if (TD_IS_SUSPENDED(td)) 284 state = "suspended"; 285 else if (TD_AWAITING_INTR(td)) 286 state = "intrwait"; 287 else if (pick->p_state == PRS_ZOMBIE) 288 state = "zombie"; 289 else 290 state = "unknown"; 291 pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT; 292 thread_unlock(td); 293 if (pick->p_state == PRS_NEW || pick->p_state == PRS_ZOMBIE) 294 rss = 0; 295 else 296 rss = pgtok(vmspace_resident_count(pick->p_vmspace)); 297 PROC_UNLOCK(pick); 298 PROC_LOCK(pick); 299 PGRP_UNLOCK(tp->t_pgrp); 300 rufetchcalc(pick, &ru, &utime, &stime); 301 pid = pick->p_pid; 302 strlcpy(comm, pick->p_comm, sizeof comm); 303 PROC_UNLOCK(pick); 304 305 /* Print command, pid, state, utime, stime, %cpu, and rss. */ 306 ttyprintf(tp, 307 " cmd: %s %d [%s%s] %ld.%02ldu %ld.%02lds %d%% %ldk\n", 308 comm, pid, stateprefix, state, 309 (long)utime.tv_sec, utime.tv_usec / 10000, 310 (long)stime.tv_sec, stime.tv_usec / 10000, 311 pctcpu / 100, rss); 312 } 313