Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / tty_info.c
blobde06775ab9e406f1f752ea6f9b444bb628d9a7ec
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.
10 * Copyright (c) 2002 Networks Associates Technologies, Inc.
11 * All rights reserved.
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.
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.
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.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
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>
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
60 * Returns 1 if p2 is "better" than p1
62 * The algorithm for picking the "interesting" process is thus:
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.
73 #define TESTAB(a, b) ((a)<<1 | (b))
74 #define ONLYA 2
75 #define ONLYB 1
76 #define BOTH 3
78 static int
79 proc_sum(struct proc *p, int *estcpup)
81 struct thread *td;
82 int estcpu;
83 int val;
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);
95 *estcpup = estcpu;
97 return (val);
100 static int
101 thread_compare(struct thread *td, struct thread *td2)
103 int runa, runb;
104 int slpa, slpb;
105 fixpt_t esta, estb;
107 if (td == NULL)
108 return (1);
111 * Fetch running stats, pctcpu usage, and interruptable flag.
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);
124 * see if at least one of them is runnable
126 switch (TESTAB(runa, runb)) {
127 case ONLYA:
128 return (0);
129 case ONLYB:
130 return (1);
131 case BOTH:
132 break;
135 * favor one with highest recent cpu utilization
137 if (estb > esta)
138 return (1);
139 if (esta > estb)
140 return (0);
142 * favor one sleeping in a non-interruptible sleep
144 switch (TESTAB(slpa, slpb)) {
145 case ONLYA:
146 return (0);
147 case ONLYB:
148 return (1);
149 case BOTH:
150 break;
153 return (td < td2);
156 static int
157 proc_compare(struct proc *p1, struct proc *p2)
160 int runa, runb;
161 fixpt_t esta, estb;
163 if (p1 == NULL)
164 return (1);
167 * Fetch various stats about these processes. After we drop the
168 * lock the information could be stale but the race is unimportant.
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);
178 * see if at least one of them is runnable
180 switch (TESTAB(runa, runb)) {
181 case ONLYA:
182 return (0);
183 case ONLYB:
184 return (1);
185 case BOTH:
186 break;
189 * favor one with highest recent cpu utilization
191 if (estb > esta)
192 return (1);
193 if (esta > estb)
194 return (0);
196 * weed out zombies
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;
207 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
211 * Report on state of foreground process group.
213 void
214 tty_info(struct tty *tp)
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;
226 tty_lock_assert(tp, MA_OWNED);
228 if (tty_checkoutq(tp) == 0)
229 return;
231 /* Print load average. */
232 load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
233 ttyprintf(tp, "load: %d.%02d ", load / 100, load % 100);
235 if (tp->t_session == NULL) {
236 ttyprintf(tp, "not a controlling terminal\n");
237 return;
239 if (tp->t_pgrp == NULL) {
240 ttyprintf(tp, "no foreground process group\n");
241 return;
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;
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.
257 pick = NULL;
258 LIST_FOREACH(p, &tp->t_pgrp->pg_members, p_pglist)
259 if (proc_compare(pick, p))
260 pick = p;
262 PROC_LOCK(pick);
263 picktd = NULL;
264 td = FIRST_THREAD_IN_PROC(pick);
265 FOREACH_THREAD_IN_PROC(pick, td)
266 if (thread_compare(picktd, td))
267 picktd = td;
268 td = picktd;
269 stateprefix = "";
270 thread_lock(td);
271 if (TD_IS_RUNNING(td))
272 state = "running";
273 else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
274 state = "runnable";
275 else if (TD_IS_SLEEPING(td)) {
276 /* XXX: If we're sleeping, are we ever not in a queue? */
277 if (TD_ON_SLEEPQ(td))
278 state = td->td_wmesg;
279 else
280 state = "sleeping without queue";
281 } else if (TD_ON_LOCK(td)) {
282 state = td->td_lockname;
283 stateprefix = "*";
284 } else if (TD_IS_SUSPENDED(td))
285 state = "suspended";
286 else if (TD_AWAITING_INTR(td))
287 state = "intrwait";
288 else
289 state = "unknown";
290 pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
291 thread_unlock(td);
292 if (pick->p_state == PRS_NEW || pick->p_state == PRS_ZOMBIE)
293 rss = 0;
294 else
295 rss = pgtok(vmspace_resident_count(pick->p_vmspace));
296 PROC_UNLOCK(pick);
297 PROC_LOCK(pick);
298 PGRP_UNLOCK(tp->t_pgrp);
299 rufetchcalc(pick, &ru, &utime, &stime);
300 pid = pick->p_pid;
301 bcopy(pick->p_comm, comm, sizeof(comm));
302 PROC_UNLOCK(pick);
304 /* Print command, pid, state, utime, stime, %cpu, and rss. */
305 ttyprintf(tp,
306 " cmd: %s %d [%s%s] %ld.%02ldu %ld.%02lds %d%% %ldk\n",
307 comm, pid, stateprefix, state,
308 (long)utime.tv_sec, utime.tv_usec / 10000,
309 (long)stime.tv_sec, stime.tv_usec / 10000,
310 pctcpu / 100, rss);