1180801Sed/*-
2180801Sed * Copyright (c) 1982, 1986, 1990, 1991, 1993
3180801Sed *	The Regents of the University of California.  All rights reserved.
4180801Sed * (c) UNIX System Laboratories, Inc.
5180801Sed * All or some portions of this file are derived from material licensed
6180801Sed * to the University of California by American Telephone and Telegraph
7180801Sed * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8180801Sed * the permission of UNIX System Laboratories, Inc.
9180801Sed *
10180801Sed * Copyright (c) 2002 Networks Associates Technologies, Inc.
11180801Sed * All rights reserved.
12180801Sed *
13180801Sed * Portions of this software were developed for the FreeBSD Project by
14180801Sed * ThinkSec AS and NAI Labs, the Security Research Division of Network
15180801Sed * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
16180801Sed * ("CBOSS"), as part of the DARPA CHATS research program.
17180801Sed *
18180801Sed * Redistribution and use in source and binary forms, with or without
19180801Sed * modification, are permitted provided that the following conditions
20180801Sed * are met:
21180801Sed * 1. Redistributions of source code must retain the above copyright
22180801Sed *    notice, this list of conditions and the following disclaimer.
23180801Sed * 2. Redistributions in binary form must reproduce the above copyright
24180801Sed *    notice, this list of conditions and the following disclaimer in the
25180801Sed *    documentation and/or other materials provided with the distribution.
26180801Sed * 4. Neither the name of the University nor the names of its contributors
27180801Sed *    may be used to endorse or promote products derived from this software
28180801Sed *    without specific prior written permission.
29180801Sed *
30180801Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31180801Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32180801Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33180801Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34180801Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35180801Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36180801Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37180801Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38180801Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39180801Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40180801Sed * SUCH DAMAGE.
41180801Sed */
42180801Sed
43180801Sed#include <sys/cdefs.h>
44180801Sed__FBSDID("$FreeBSD$");
45180801Sed
46180801Sed#include <sys/param.h>
47180801Sed#include <sys/lock.h>
48180801Sed#include <sys/mutex.h>
49180801Sed#include <sys/proc.h>
50180801Sed#include <sys/resourcevar.h>
51180801Sed#include <sys/sched.h>
52180801Sed#include <sys/systm.h>
53180801Sed#include <sys/tty.h>
54180801Sed
55180801Sed#include <vm/vm.h>
56180801Sed#include <vm/pmap.h>
57180801Sed#include <vm/vm_map.h>
58180801Sed
59180801Sed/*
60180801Sed * Returns 1 if p2 is "better" than p1
61180801Sed *
62180801Sed * The algorithm for picking the "interesting" process is thus:
63180801Sed *
64180801Sed *	1) Only foreground processes are eligible - implied.
65180801Sed *	2) Runnable processes are favored over anything else.  The runner
66180801Sed *	   with the highest cpu utilization is picked (p_estcpu).  Ties are
67180801Sed *	   broken by picking the highest pid.
68180801Sed *	3) The sleeper with the shortest sleep time is next.  With ties,
69180801Sed *	   we pick out just "short-term" sleepers (P_SINTR == 0).
70180801Sed *	4) Further ties are broken by picking the highest pid.
71180801Sed */
72180801Sed
73180801Sed#define TESTAB(a, b)    ((a)<<1 | (b))
74180801Sed#define ONLYA   2
75180801Sed#define ONLYB   1
76180801Sed#define BOTH    3
77180801Sed
78180801Sedstatic int
79189062Sedproc_sum(struct proc *p, fixpt_t *estcpup)
80180801Sed{
81180801Sed	struct thread *td;
82180801Sed	int estcpu;
83180801Sed	int val;
84180801Sed
85180801Sed	val = 0;
86180801Sed	estcpu = 0;
87180801Sed	FOREACH_THREAD_IN_PROC(p, td) {
88180801Sed		thread_lock(td);
89180801Sed		if (TD_ON_RUNQ(td) ||
90180801Sed		    TD_IS_RUNNING(td))
91180801Sed			val = 1;
92180801Sed		estcpu += sched_pctcpu(td);
93180801Sed		thread_unlock(td);
94180801Sed	}
95180801Sed	*estcpup = estcpu;
96180801Sed
97180801Sed	return (val);
98180801Sed}
99180801Sed
100180801Sedstatic int
101180801Sedthread_compare(struct thread *td, struct thread *td2)
102180801Sed{
103180801Sed	int runa, runb;
104180801Sed	int slpa, slpb;
105180801Sed	fixpt_t esta, estb;
106180801Sed
107180801Sed	if (td == NULL)
108180801Sed		return (1);
109180801Sed
110180801Sed	/*
111180801Sed	 * Fetch running stats, pctcpu usage, and interruptable flag.
112231095Sed	 */
113180801Sed	thread_lock(td);
114180801Sed	runa = TD_IS_RUNNING(td) | TD_ON_RUNQ(td);
115180801Sed	slpa = td->td_flags & TDF_SINTR;
116180801Sed	esta = sched_pctcpu(td);
117180801Sed	thread_unlock(td);
118180801Sed	thread_lock(td2);
119180801Sed	runb = TD_IS_RUNNING(td2) | TD_ON_RUNQ(td2);
120180801Sed	estb = sched_pctcpu(td2);
121180801Sed	slpb = td2->td_flags & TDF_SINTR;
122180801Sed	thread_unlock(td2);
123180801Sed	/*
124180801Sed	 * see if at least one of them is runnable
125180801Sed	 */
126180801Sed	switch (TESTAB(runa, runb)) {
127180801Sed	case ONLYA:
128180801Sed		return (0);
129180801Sed	case ONLYB:
130180801Sed		return (1);
131180801Sed	case BOTH:
132180801Sed		break;
133180801Sed	}
134180801Sed	/*
135180801Sed	 *  favor one with highest recent cpu utilization
136180801Sed	 */
137180801Sed	if (estb > esta)
138180801Sed		return (1);
139180801Sed	if (esta > estb)
140180801Sed		return (0);
141180801Sed	/*
142180801Sed	 * favor one sleeping in a non-interruptible sleep
143180801Sed	 */
144180801Sed	switch (TESTAB(slpa, slpb)) {
145180801Sed	case ONLYA:
146180801Sed		return (0);
147180801Sed	case ONLYB:
148180801Sed		return (1);
149180801Sed	case BOTH:
150180801Sed		break;
151180801Sed	}
152180801Sed
153180801Sed	return (td < td2);
154180801Sed}
155180801Sed
156180801Sedstatic int
157180801Sedproc_compare(struct proc *p1, struct proc *p2)
158180801Sed{
159180801Sed
160180801Sed	int runa, runb;
161180801Sed	fixpt_t esta, estb;
162180801Sed
163180801Sed	if (p1 == NULL)
164180801Sed		return (1);
165180801Sed
166180801Sed	/*
167180801Sed	 * Fetch various stats about these processes.  After we drop the
168180801Sed	 * lock the information could be stale but the race is unimportant.
169180801Sed	 */
170180801Sed	PROC_LOCK(p1);
171180801Sed	runa = proc_sum(p1, &esta);
172180801Sed	PROC_UNLOCK(p1);
173180801Sed	PROC_LOCK(p2);
174180801Sed	runb = proc_sum(p2, &estb);
175180801Sed	PROC_UNLOCK(p2);
176231095Sed
177180801Sed	/*
178180801Sed	 * see if at least one of them is runnable
179180801Sed	 */
180180801Sed	switch (TESTAB(runa, runb)) {
181180801Sed	case ONLYA:
182180801Sed		return (0);
183180801Sed	case ONLYB:
184180801Sed		return (1);
185180801Sed	case BOTH:
186180801Sed		break;
187180801Sed	}
188180801Sed	/*
189180801Sed	 *  favor one with highest recent cpu utilization
190180801Sed	 */
191180801Sed	if (estb > esta)
192180801Sed		return (1);
193180801Sed	if (esta > estb)
194180801Sed		return (0);
195180801Sed	/*
196180801Sed	 * weed out zombies
197180801Sed	 */
198180801Sed	switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
199180801Sed	case ONLYA:
200180801Sed		return (1);
201180801Sed	case ONLYB:
202180801Sed		return (0);
203180801Sed	case BOTH:
204180801Sed		break;
205180801Sed	}
206180801Sed
207180801Sed	return (p2->p_pid > p1->p_pid);		/* tie - return highest pid */
208180801Sed}
209180801Sed
210180801Sed/*
211180801Sed * Report on state of foreground process group.
212180801Sed */
213180801Sedvoid
214181905Sedtty_info(struct tty *tp)
215180801Sed{
216192250Sed	struct timeval rtime, utime, stime;
217192250Sed	struct proc *p, *ppick;
218192250Sed	struct thread *td, *tdpick;
219180801Sed	const char *stateprefix, *state;
220180801Sed	long rss;
221180801Sed	int load, pctcpu;
222180801Sed	pid_t pid;
223180801Sed	char comm[MAXCOMLEN + 1];
224180801Sed	struct rusage ru;
225180801Sed
226181905Sed	tty_lock_assert(tp, MA_OWNED);
227181905Sed
228181905Sed	if (tty_checkoutq(tp) == 0)
229180801Sed		return;
230180801Sed
231180801Sed	/* Print load average. */
232180801Sed	load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
233192252Sed	ttyprintf(tp, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n",
234192252Sed	    load / 100, load % 100);
235180801Sed
236180801Sed	if (tp->t_session == NULL) {
237180801Sed		ttyprintf(tp, "not a controlling terminal\n");
238180801Sed		return;
239180801Sed	}
240180801Sed	if (tp->t_pgrp == NULL) {
241180801Sed		ttyprintf(tp, "no foreground process group\n");
242180801Sed		return;
243180801Sed	}
244180801Sed	PGRP_LOCK(tp->t_pgrp);
245180801Sed	if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
246180801Sed		PGRP_UNLOCK(tp->t_pgrp);
247180801Sed		ttyprintf(tp, "empty foreground process group\n");
248180801Sed		return;
249180801Sed	}
250180801Sed
251180801Sed	/*
252180801Sed	 * Pick the most interesting process and copy some of its
253180801Sed	 * state for printing later.  This operation could rely on stale
254180801Sed	 * data as we can't hold the proc slock or thread locks over the
255180801Sed	 * whole list. However, we're guaranteed not to reference an exited
256180801Sed	 * thread or proc since we hold the tty locked.
257180801Sed	 */
258192250Sed	p = NULL;
259192250Sed	LIST_FOREACH(ppick, &tp->t_pgrp->pg_members, p_pglist)
260192250Sed		if (proc_compare(p, ppick))
261192250Sed			p = ppick;
262180801Sed
263192250Sed	PROC_LOCK(p);
264192250Sed	PGRP_UNLOCK(tp->t_pgrp);
265192250Sed	td = NULL;
266192250Sed	FOREACH_THREAD_IN_PROC(p, tdpick)
267192250Sed		if (thread_compare(td, tdpick))
268192250Sed			td = tdpick;
269180801Sed	stateprefix = "";
270180801Sed	thread_lock(td);
271180801Sed	if (TD_IS_RUNNING(td))
272180801Sed		state = "running";
273180801Sed	else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
274180801Sed		state = "runnable";
275180801Sed	else if (TD_IS_SLEEPING(td)) {
276180801Sed		/* XXX: If we're sleeping, are we ever not in a queue? */
277180801Sed		if (TD_ON_SLEEPQ(td))
278180801Sed			state = td->td_wmesg;
279180801Sed		else
280180801Sed			state = "sleeping without queue";
281180801Sed	} else if (TD_ON_LOCK(td)) {
282180801Sed		state = td->td_lockname;
283180801Sed		stateprefix = "*";
284180801Sed	} else if (TD_IS_SUSPENDED(td))
285180801Sed		state = "suspended";
286180801Sed	else if (TD_AWAITING_INTR(td))
287180801Sed		state = "intrwait";
288192250Sed	else if (p->p_state == PRS_ZOMBIE)
289187881Srwatson		state = "zombie";
290180801Sed	else
291180801Sed		state = "unknown";
292180801Sed	pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
293180801Sed	thread_unlock(td);
294192250Sed	if (p->p_state == PRS_NEW || p->p_state == PRS_ZOMBIE)
295180801Sed		rss = 0;
296180801Sed	else
297192250Sed		rss = pgtok(vmspace_resident_count(p->p_vmspace));
298192250Sed	microuptime(&rtime);
299192250Sed	timevalsub(&rtime, &p->p_stats->p_start);
300192250Sed	rufetchcalc(p, &ru, &utime, &stime);
301192250Sed	pid = p->p_pid;
302192250Sed	strlcpy(comm, p->p_comm, sizeof comm);
303192250Sed	PROC_UNLOCK(p);
304180801Sed
305192250Sed	/* Print command, pid, state, rtime, utime, stime, %cpu, and rss. */
306180801Sed	ttyprintf(tp,
307192250Sed	    " cmd: %s %d [%s%s] %ld.%02ldr %ld.%02ldu %ld.%02lds %d%% %ldk\n",
308180801Sed	    comm, pid, stateprefix, state,
309192250Sed	    (long)rtime.tv_sec, rtime.tv_usec / 10000,
310180801Sed	    (long)utime.tv_sec, utime.tv_usec / 10000,
311180801Sed	    (long)stime.tv_sec, stime.tv_usec / 10000,
312180801Sed	    pctcpu / 100, rss);
313180801Sed}
314