xref: /freebsd/sys/kern/tty_info.c (revision cacdd70cc751fb68dec4b86c5e5b8c969b6e26ef)
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, int *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 ttyinfo(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 	if (ttycheckoutq(tp,0) == 0)
227 		return;
228 
229 	/* Print load average. */
230 	load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
231 	ttyprintf(tp, "load: %d.%02d ", load / 100, load % 100);
232 
233 	/*
234 	 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
235 	 * that pending input will be retyped on BS.
236 	 */
237 	if (tp->t_session == NULL) {
238 		ttyprintf(tp, "not a controlling terminal\n");
239 		tp->t_rocount = 0;
240 		return;
241 	}
242 	if (tp->t_pgrp == NULL) {
243 		ttyprintf(tp, "no foreground process group\n");
244 		tp->t_rocount = 0;
245 		return;
246 	}
247 	PGRP_LOCK(tp->t_pgrp);
248 	if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
249 		PGRP_UNLOCK(tp->t_pgrp);
250 		ttyprintf(tp, "empty foreground process group\n");
251 		tp->t_rocount = 0;
252 		return;
253 	}
254 
255 	/*
256 	 * Pick the most interesting process and copy some of its
257 	 * state for printing later.  This operation could rely on stale
258 	 * data as we can't hold the proc slock or thread locks over the
259 	 * whole list. However, we're guaranteed not to reference an exited
260 	 * thread or proc since we hold the tty locked.
261 	 */
262 	pick = NULL;
263 	LIST_FOREACH(p, &tp->t_pgrp->pg_members, p_pglist)
264 		if (proc_compare(pick, p))
265 			pick = p;
266 
267 	PROC_LOCK(pick);
268 	picktd = NULL;
269 	td = FIRST_THREAD_IN_PROC(pick);
270 	FOREACH_THREAD_IN_PROC(pick, td)
271 		if (thread_compare(picktd, td))
272 			picktd = td;
273 	td = picktd;
274 	stateprefix = "";
275 	thread_lock(td);
276 	if (TD_IS_RUNNING(td))
277 		state = "running";
278 	else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
279 		state = "runnable";
280 	else if (TD_IS_SLEEPING(td)) {
281 		/* XXX: If we're sleeping, are we ever not in a queue? */
282 		if (TD_ON_SLEEPQ(td))
283 			state = td->td_wmesg;
284 		else
285 			state = "sleeping without queue";
286 	} else if (TD_ON_LOCK(td)) {
287 		state = td->td_lockname;
288 		stateprefix = "*";
289 	} else if (TD_IS_SUSPENDED(td))
290 		state = "suspended";
291 	else if (TD_AWAITING_INTR(td))
292 		state = "intrwait";
293 	else
294 		state = "unknown";
295 	pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
296 	thread_unlock(td);
297 	if (pick->p_state == PRS_NEW || pick->p_state == PRS_ZOMBIE)
298 		rss = 0;
299 	else
300 		rss = pgtok(vmspace_resident_count(pick->p_vmspace));
301 	PROC_UNLOCK(pick);
302 	PROC_LOCK(pick);
303 	PGRP_UNLOCK(tp->t_pgrp);
304 	rufetchcalc(pick, &ru, &utime, &stime);
305 	pid = pick->p_pid;
306 	bcopy(pick->p_comm, comm, sizeof(comm));
307 	PROC_UNLOCK(pick);
308 
309 	/* Print command, pid, state, utime, stime, %cpu, and rss. */
310 	ttyprintf(tp,
311 	    " cmd: %s %d [%s%s] %ld.%02ldu %ld.%02lds %d%% %ldk\n",
312 	    comm, pid, stateprefix, state,
313 	    (long)utime.tv_sec, utime.tv_usec / 10000,
314 	    (long)stime.tv_sec, stime.tv_usec / 10000,
315 	    pctcpu / 100, rss);
316 	tp->t_rocount = 0;
317 }
318