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
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
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
48 #include <sys/mutex.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sched.h>
52 #include <sys/systm.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))
79 proc_sum(struct proc
*p
, int *estcpup
)
87 FOREACH_THREAD_IN_PROC(p
, td
) {
92 estcpu
+= sched_pctcpu(td
);
101 thread_compare(struct thread
*td
, struct thread
*td2
)
111 * Fetch running stats, pctcpu usage, and interruptable flag.
114 runa
= TD_IS_RUNNING(td
) | TD_ON_RUNQ(td
);
115 slpa
= td
->td_flags
& TDF_SINTR
;
116 esta
= sched_pctcpu(td
);
119 runb
= TD_IS_RUNNING(td2
) | TD_ON_RUNQ(td2
);
120 estb
= sched_pctcpu(td2
);
121 slpb
= td2
->td_flags
& TDF_SINTR
;
124 * see if at least one of them is runnable
126 switch (TESTAB(runa
, runb
)) {
135 * favor one with highest recent cpu utilization
142 * favor one sleeping in a non-interruptible sleep
144 switch (TESTAB(slpa
, slpb
)) {
157 proc_compare(struct proc
*p1
, struct proc
*p2
)
167 * Fetch various stats about these processes. After we drop the
168 * lock the information could be stale but the race is unimportant.
171 runa
= proc_sum(p1
, &esta
);
174 runb
= proc_sum(p2
, &estb
);
178 * see if at least one of them is runnable
180 switch (TESTAB(runa
, runb
)) {
189 * favor one with highest recent cpu utilization
198 switch (TESTAB(p1
->p_state
== PRS_ZOMBIE
, p2
->p_state
== PRS_ZOMBIE
)) {
207 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
211 * Report on state of foreground process group.
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
;
223 char comm
[MAXCOMLEN
+ 1];
226 tty_lock_assert(tp
, MA_OWNED
);
228 if (tty_checkoutq(tp
) == 0)
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");
239 if (tp
->t_pgrp
== NULL
) {
240 ttyprintf(tp
, "no foreground process group\n");
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");
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.
258 LIST_FOREACH(p
, &tp
->t_pgrp
->pg_members
, p_pglist
)
259 if (proc_compare(pick
, p
))
264 td
= FIRST_THREAD_IN_PROC(pick
);
265 FOREACH_THREAD_IN_PROC(pick
, td
)
266 if (thread_compare(picktd
, td
))
271 if (TD_IS_RUNNING(td
))
273 else if (TD_ON_RUNQ(td
) || TD_CAN_RUN(td
))
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
;
280 state
= "sleeping without queue";
281 } else if (TD_ON_LOCK(td
)) {
282 state
= td
->td_lockname
;
284 } else if (TD_IS_SUSPENDED(td
))
286 else if (TD_AWAITING_INTR(td
))
290 pctcpu
= (sched_pctcpu(td
) * 10000 + FSCALE
/ 2) >> FSHIFT
;
292 if (pick
->p_state
== PRS_NEW
|| pick
->p_state
== PRS_ZOMBIE
)
295 rss
= pgtok(vmspace_resident_count(pick
->p_vmspace
));
298 PGRP_UNLOCK(tp
->t_pgrp
);
299 rufetchcalc(pick
, &ru
, &utime
, &stime
);
301 bcopy(pick
->p_comm
, comm
, sizeof(comm
));
304 /* Print command, pid, state, utime, stime, %cpu, and rss. */
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,