xref: /freebsd/sys/kern/tty_info.c (revision 0a713948)
1bea45cddSEd Schouten /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4bea45cddSEd Schouten  * Copyright (c) 1982, 1986, 1990, 1991, 1993
5bea45cddSEd Schouten  *	The Regents of the University of California.  All rights reserved.
6bea45cddSEd Schouten  * (c) UNIX System Laboratories, Inc.
7bea45cddSEd Schouten  * All or some portions of this file are derived from material licensed
8bea45cddSEd Schouten  * to the University of California by American Telephone and Telegraph
9bea45cddSEd Schouten  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10bea45cddSEd Schouten  * the permission of UNIX System Laboratories, Inc.
11bea45cddSEd Schouten  *
12bea45cddSEd Schouten  * Copyright (c) 2002 Networks Associates Technologies, Inc.
13bea45cddSEd Schouten  * All rights reserved.
14bea45cddSEd Schouten  *
15bea45cddSEd Schouten  * Portions of this software were developed for the FreeBSD Project by
16bea45cddSEd Schouten  * ThinkSec AS and NAI Labs, the Security Research Division of Network
17bea45cddSEd Schouten  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
18bea45cddSEd Schouten  * ("CBOSS"), as part of the DARPA CHATS research program.
19bea45cddSEd Schouten  *
20bea45cddSEd Schouten  * Redistribution and use in source and binary forms, with or without
21bea45cddSEd Schouten  * modification, are permitted provided that the following conditions
22bea45cddSEd Schouten  * are met:
23bea45cddSEd Schouten  * 1. Redistributions of source code must retain the above copyright
24bea45cddSEd Schouten  *    notice, this list of conditions and the following disclaimer.
25bea45cddSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
26bea45cddSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
27bea45cddSEd Schouten  *    documentation and/or other materials provided with the distribution.
2869a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
29bea45cddSEd Schouten  *    may be used to endorse or promote products derived from this software
30bea45cddSEd Schouten  *    without specific prior written permission.
31bea45cddSEd Schouten  *
32bea45cddSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33bea45cddSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34bea45cddSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35bea45cddSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36bea45cddSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37bea45cddSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38bea45cddSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39bea45cddSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40bea45cddSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41bea45cddSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42bea45cddSEd Schouten  * SUCH DAMAGE.
43bea45cddSEd Schouten  */
44bea45cddSEd Schouten 
45bea45cddSEd Schouten #include <sys/cdefs.h>
466741ea08SMark Johnston #include "opt_stack.h"
476741ea08SMark Johnston 
48bea45cddSEd Schouten #include <sys/param.h>
496858c2ccSConrad Meyer #include <sys/cons.h>
506858c2ccSConrad Meyer #include <sys/kdb.h>
51bea45cddSEd Schouten #include <sys/lock.h>
52d7aa89c3SConrad Meyer #include <sys/malloc.h>
53bea45cddSEd Schouten #include <sys/mutex.h>
54bea45cddSEd Schouten #include <sys/proc.h>
55bea45cddSEd Schouten #include <sys/resourcevar.h>
566858c2ccSConrad Meyer #include <sys/sbuf.h>
57bea45cddSEd Schouten #include <sys/sched.h>
58d7aa89c3SConrad Meyer #include <sys/stack.h>
59d7aa89c3SConrad Meyer #include <sys/sysctl.h>
60bea45cddSEd Schouten #include <sys/systm.h>
61bea45cddSEd Schouten #include <sys/tty.h>
62bea45cddSEd Schouten 
63bea45cddSEd Schouten #include <vm/vm.h>
64bea45cddSEd Schouten #include <vm/pmap.h>
65bea45cddSEd Schouten #include <vm/vm_map.h>
66bea45cddSEd Schouten 
67bea45cddSEd Schouten /*
68bea45cddSEd Schouten  * Returns 1 if p2 is "better" than p1
69bea45cddSEd Schouten  *
70bea45cddSEd Schouten  * The algorithm for picking the "interesting" process is thus:
71bea45cddSEd Schouten  *
72bea45cddSEd Schouten  *	1) Only foreground processes are eligible - implied.
73bea45cddSEd Schouten  *	2) Runnable processes are favored over anything else.  The runner
74bea45cddSEd Schouten  *	   with the highest cpu utilization is picked (p_estcpu).  Ties are
75bea45cddSEd Schouten  *	   broken by picking the highest pid.
76bea45cddSEd Schouten  *	3) The sleeper with the shortest sleep time is next.  With ties,
77bea45cddSEd Schouten  *	   we pick out just "short-term" sleepers (P_SINTR == 0).
78bea45cddSEd Schouten  *	4) Further ties are broken by picking the highest pid.
79bea45cddSEd Schouten  */
80bea45cddSEd Schouten 
81bea45cddSEd Schouten #define TESTAB(a, b)    ((a)<<1 | (b))
82bea45cddSEd Schouten #define ONLYA   2
83bea45cddSEd Schouten #define ONLYB   1
84bea45cddSEd Schouten #define BOTH    3
85bea45cddSEd Schouten 
86bea45cddSEd Schouten static int
proc_sum(struct proc * p,fixpt_t * estcpup)879e577585SEd Schouten proc_sum(struct proc *p, fixpt_t *estcpup)
88bea45cddSEd Schouten {
89bea45cddSEd Schouten 	struct thread *td;
90bea45cddSEd Schouten 	int estcpu;
91bea45cddSEd Schouten 	int val;
92bea45cddSEd Schouten 
93bea45cddSEd Schouten 	val = 0;
94bea45cddSEd Schouten 	estcpu = 0;
95bea45cddSEd Schouten 	FOREACH_THREAD_IN_PROC(p, td) {
96bea45cddSEd Schouten 		thread_lock(td);
97bea45cddSEd Schouten 		if (TD_ON_RUNQ(td) ||
98bea45cddSEd Schouten 		    TD_IS_RUNNING(td))
99bea45cddSEd Schouten 			val = 1;
100bea45cddSEd Schouten 		estcpu += sched_pctcpu(td);
101bea45cddSEd Schouten 		thread_unlock(td);
102bea45cddSEd Schouten 	}
103bea45cddSEd Schouten 	*estcpup = estcpu;
104bea45cddSEd Schouten 
105bea45cddSEd Schouten 	return (val);
106bea45cddSEd Schouten }
107bea45cddSEd Schouten 
108bea45cddSEd Schouten static int
thread_compare(struct thread * td,struct thread * td2)109bea45cddSEd Schouten thread_compare(struct thread *td, struct thread *td2)
110bea45cddSEd Schouten {
111bea45cddSEd Schouten 	int runa, runb;
112bea45cddSEd Schouten 	int slpa, slpb;
113bea45cddSEd Schouten 	fixpt_t esta, estb;
114bea45cddSEd Schouten 
115bea45cddSEd Schouten 	if (td == NULL)
116bea45cddSEd Schouten 		return (1);
117bea45cddSEd Schouten 
118bea45cddSEd Schouten 	/*
119bea45cddSEd Schouten 	 * Fetch running stats, pctcpu usage, and interruptable flag.
120bea45cddSEd Schouten 	 */
121bea45cddSEd Schouten 	thread_lock(td);
1227d8a4eb9SDimitry Andric 	runa = TD_IS_RUNNING(td) || TD_ON_RUNQ(td);
123bea45cddSEd Schouten 	slpa = td->td_flags & TDF_SINTR;
124bea45cddSEd Schouten 	esta = sched_pctcpu(td);
125bea45cddSEd Schouten 	thread_unlock(td);
126bea45cddSEd Schouten 	thread_lock(td2);
1277d8a4eb9SDimitry Andric 	runb = TD_IS_RUNNING(td2) || TD_ON_RUNQ(td2);
128bea45cddSEd Schouten 	estb = sched_pctcpu(td2);
129bea45cddSEd Schouten 	slpb = td2->td_flags & TDF_SINTR;
130bea45cddSEd Schouten 	thread_unlock(td2);
131bea45cddSEd Schouten 	/*
132bea45cddSEd Schouten 	 * see if at least one of them is runnable
133bea45cddSEd Schouten 	 */
134bea45cddSEd Schouten 	switch (TESTAB(runa, runb)) {
135bea45cddSEd Schouten 	case ONLYA:
136bea45cddSEd Schouten 		return (0);
137bea45cddSEd Schouten 	case ONLYB:
138bea45cddSEd Schouten 		return (1);
139bea45cddSEd Schouten 	case BOTH:
140bea45cddSEd Schouten 		break;
141bea45cddSEd Schouten 	}
142bea45cddSEd Schouten 	/*
143bea45cddSEd Schouten 	 *  favor one with highest recent cpu utilization
144bea45cddSEd Schouten 	 */
145bea45cddSEd Schouten 	if (estb > esta)
146bea45cddSEd Schouten 		return (1);
147bea45cddSEd Schouten 	if (esta > estb)
148bea45cddSEd Schouten 		return (0);
149bea45cddSEd Schouten 	/*
150bea45cddSEd Schouten 	 * favor one sleeping in a non-interruptible sleep
151bea45cddSEd Schouten 	 */
152bea45cddSEd Schouten 	switch (TESTAB(slpa, slpb)) {
153bea45cddSEd Schouten 	case ONLYA:
154bea45cddSEd Schouten 		return (0);
155bea45cddSEd Schouten 	case ONLYB:
156bea45cddSEd Schouten 		return (1);
157bea45cddSEd Schouten 	case BOTH:
158bea45cddSEd Schouten 		break;
159bea45cddSEd Schouten 	}
160bea45cddSEd Schouten 
161bea45cddSEd Schouten 	return (td < td2);
162bea45cddSEd Schouten }
163bea45cddSEd Schouten 
164bea45cddSEd Schouten static int
proc_compare(struct proc * p1,struct proc * p2)165bea45cddSEd Schouten proc_compare(struct proc *p1, struct proc *p2)
166bea45cddSEd Schouten {
167bea45cddSEd Schouten 
168bea45cddSEd Schouten 	int runa, runb;
169bea45cddSEd Schouten 	fixpt_t esta, estb;
170bea45cddSEd Schouten 
171bea45cddSEd Schouten 	if (p1 == NULL)
172bea45cddSEd Schouten 		return (1);
173bea45cddSEd Schouten 
174bea45cddSEd Schouten 	/*
175bea45cddSEd Schouten 	 * Fetch various stats about these processes.  After we drop the
176bea45cddSEd Schouten 	 * lock the information could be stale but the race is unimportant.
177bea45cddSEd Schouten 	 */
178bea45cddSEd Schouten 	PROC_LOCK(p1);
179bea45cddSEd Schouten 	runa = proc_sum(p1, &esta);
180bea45cddSEd Schouten 	PROC_UNLOCK(p1);
181bea45cddSEd Schouten 	PROC_LOCK(p2);
182bea45cddSEd Schouten 	runb = proc_sum(p2, &estb);
183bea45cddSEd Schouten 	PROC_UNLOCK(p2);
184bea45cddSEd Schouten 
185bea45cddSEd Schouten 	/*
186bea45cddSEd Schouten 	 * see if at least one of them is runnable
187bea45cddSEd Schouten 	 */
188bea45cddSEd Schouten 	switch (TESTAB(runa, runb)) {
189bea45cddSEd Schouten 	case ONLYA:
190bea45cddSEd Schouten 		return (0);
191bea45cddSEd Schouten 	case ONLYB:
192bea45cddSEd Schouten 		return (1);
193bea45cddSEd Schouten 	case BOTH:
194bea45cddSEd Schouten 		break;
195bea45cddSEd Schouten 	}
196bea45cddSEd Schouten 	/*
197bea45cddSEd Schouten 	 *  favor one with highest recent cpu utilization
198bea45cddSEd Schouten 	 */
199bea45cddSEd Schouten 	if (estb > esta)
200bea45cddSEd Schouten 		return (1);
201bea45cddSEd Schouten 	if (esta > estb)
202bea45cddSEd Schouten 		return (0);
203bea45cddSEd Schouten 	/*
204bea45cddSEd Schouten 	 * weed out zombies
205bea45cddSEd Schouten 	 */
206bea45cddSEd Schouten 	switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
207bea45cddSEd Schouten 	case ONLYA:
208bea45cddSEd Schouten 		return (1);
209bea45cddSEd Schouten 	case ONLYB:
210bea45cddSEd Schouten 		return (0);
211bea45cddSEd Schouten 	case BOTH:
212bea45cddSEd Schouten 		break;
213bea45cddSEd Schouten 	}
214bea45cddSEd Schouten 
215bea45cddSEd Schouten 	return (p2->p_pid > p1->p_pid);		/* tie - return highest pid */
216bea45cddSEd Schouten }
217bea45cddSEd Schouten 
2186858c2ccSConrad Meyer static int
sbuf_tty_drain(void * a,const char * d,int len)2196858c2ccSConrad Meyer sbuf_tty_drain(void *a, const char *d, int len)
2206858c2ccSConrad Meyer {
2216858c2ccSConrad Meyer 	struct tty *tp;
2226858c2ccSConrad Meyer 	int rc;
2236858c2ccSConrad Meyer 
2246858c2ccSConrad Meyer 	tp = a;
2256858c2ccSConrad Meyer 
2266858c2ccSConrad Meyer 	if (kdb_active) {
2276858c2ccSConrad Meyer 		cnputsn(d, len);
2286858c2ccSConrad Meyer 		return (len);
2296858c2ccSConrad Meyer 	}
230879e0604SMateusz Guzik 	if (tp != NULL && !KERNEL_PANICKED()) {
2316858c2ccSConrad Meyer 		rc = tty_putstrn(tp, d, len);
2326858c2ccSConrad Meyer 		if (rc != 0)
2336858c2ccSConrad Meyer 			return (-ENXIO);
2346858c2ccSConrad Meyer 		return (len);
2356858c2ccSConrad Meyer 	}
2366858c2ccSConrad Meyer 	return (-ENXIO);
2376858c2ccSConrad Meyer }
2386858c2ccSConrad Meyer 
23993940996SConrad Meyer #ifdef STACK
240b69996d1SWarner Losh #ifdef INVARIANTS
241e94fdc38SPawel Biernacki static int tty_info_kstacks = STACK_SBUF_FMT_COMPACT;
242b69996d1SWarner Losh #else
243b69996d1SWarner Losh static int tty_info_kstacks = STACK_SBUF_FMT_NONE;
244b69996d1SWarner Losh #endif
245cd1c083dSPawel Biernacki 
246cd1c083dSPawel Biernacki static int
sysctl_tty_info_kstacks(SYSCTL_HANDLER_ARGS)247cd1c083dSPawel Biernacki sysctl_tty_info_kstacks(SYSCTL_HANDLER_ARGS)
248cd1c083dSPawel Biernacki {
249cd1c083dSPawel Biernacki 	enum stack_sbuf_fmt val;
250cd1c083dSPawel Biernacki 	int error;
251cd1c083dSPawel Biernacki 
252cd1c083dSPawel Biernacki 	val = tty_info_kstacks;
253cd1c083dSPawel Biernacki 	error = sysctl_handle_int(oidp, &val, 0, req);
254cd1c083dSPawel Biernacki 	if (error != 0 || req->newptr == NULL)
255cd1c083dSPawel Biernacki 		return (error);
256cd1c083dSPawel Biernacki 
257cd1c083dSPawel Biernacki 	switch (val) {
258cd1c083dSPawel Biernacki 	case STACK_SBUF_FMT_NONE:
259cd1c083dSPawel Biernacki 	case STACK_SBUF_FMT_LONG:
260cd1c083dSPawel Biernacki 	case STACK_SBUF_FMT_COMPACT:
261cd1c083dSPawel Biernacki 		tty_info_kstacks = val;
262cd1c083dSPawel Biernacki 		break;
263cd1c083dSPawel Biernacki 	default:
264cd1c083dSPawel Biernacki 		error = EINVAL;
265cd1c083dSPawel Biernacki 	}
266cd1c083dSPawel Biernacki 
267cd1c083dSPawel Biernacki 	return (error);
268cd1c083dSPawel Biernacki }
269cd1c083dSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, tty_info_kstacks,
270cd1c083dSPawel Biernacki     CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_INT, NULL, 0,
271cd1c083dSPawel Biernacki     sysctl_tty_info_kstacks, "I",
272cd1c083dSPawel Biernacki     "Adjust format of kernel stack(9) traces on ^T (tty info): "
273cd1c083dSPawel Biernacki     "0 - disabled; 1 - long; 2 - compact");
27493940996SConrad Meyer #endif
275d7aa89c3SConrad Meyer 
276bea45cddSEd Schouten /*
277bea45cddSEd Schouten  * Report on state of foreground process group.
278bea45cddSEd Schouten  */
279bea45cddSEd Schouten void
tty_info(struct tty * tp)280bc093719SEd Schouten tty_info(struct tty *tp)
281bea45cddSEd Schouten {
282dd970f41SEd Schouten 	struct timeval rtime, utime, stime;
28393940996SConrad Meyer #ifdef STACK
284d7aa89c3SConrad Meyer 	struct stack stack;
285cd1c083dSPawel Biernacki 	int sterr, kstacks_val;
286cd1c083dSPawel Biernacki 	bool print_kstacks;
28793940996SConrad Meyer #endif
288dd970f41SEd Schouten 	struct proc *p, *ppick;
289dd970f41SEd Schouten 	struct thread *td, *tdpick;
290bea45cddSEd Schouten 	const char *stateprefix, *state;
2916858c2ccSConrad Meyer 	struct sbuf sb;
292bea45cddSEd Schouten 	long rss;
29393940996SConrad Meyer 	int load, pctcpu;
294bea45cddSEd Schouten 	pid_t pid;
295bea45cddSEd Schouten 	char comm[MAXCOMLEN + 1];
296bea45cddSEd Schouten 	struct rusage ru;
297bea45cddSEd Schouten 
29823d53268SKyle Evans 	tty_assert_locked(tp);
299bc093719SEd Schouten 
300bc093719SEd Schouten 	if (tty_checkoutq(tp) == 0)
301bea45cddSEd Schouten 		return;
302bea45cddSEd Schouten 
303f8a22201SMark Johnston 	(void)sbuf_new(&sb, tp->t_prbuf, tp->t_prbufsz, SBUF_FIXEDLEN);
3046858c2ccSConrad Meyer 	sbuf_set_drain(&sb, sbuf_tty_drain, tp);
3056858c2ccSConrad Meyer 
306bea45cddSEd Schouten 	/* Print load average. */
3071d2421adSAlan Somers 	load = ((int64_t)averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
3086858c2ccSConrad Meyer 	sbuf_printf(&sb, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n",
309379affd5SEd Schouten 	    load / 100, load % 100);
310bea45cddSEd Schouten 
311bea45cddSEd Schouten 	if (tp->t_session == NULL) {
3120a713948SAlexander Motin 		sbuf_cat(&sb, "not a controlling terminal\n");
3136858c2ccSConrad Meyer 		goto out;
314bea45cddSEd Schouten 	}
315bea45cddSEd Schouten 	if (tp->t_pgrp == NULL) {
3160a713948SAlexander Motin 		sbuf_cat(&sb, "no foreground process group\n");
3176858c2ccSConrad Meyer 		goto out;
318bea45cddSEd Schouten 	}
319bea45cddSEd Schouten 	PGRP_LOCK(tp->t_pgrp);
320bea45cddSEd Schouten 	if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
321bea45cddSEd Schouten 		PGRP_UNLOCK(tp->t_pgrp);
3220a713948SAlexander Motin 		sbuf_cat(&sb, "empty foreground process group\n");
3236858c2ccSConrad Meyer 		goto out;
324bea45cddSEd Schouten 	}
325bea45cddSEd Schouten 
326bea45cddSEd Schouten 	/*
327bea45cddSEd Schouten 	 * Pick the most interesting process and copy some of its
328bea45cddSEd Schouten 	 * state for printing later.  This operation could rely on stale
329bea45cddSEd Schouten 	 * data as we can't hold the proc slock or thread locks over the
330bea45cddSEd Schouten 	 * whole list. However, we're guaranteed not to reference an exited
331bea45cddSEd Schouten 	 * thread or proc since we hold the tty locked.
332bea45cddSEd Schouten 	 */
333dd970f41SEd Schouten 	p = NULL;
334dd970f41SEd Schouten 	LIST_FOREACH(ppick, &tp->t_pgrp->pg_members, p_pglist)
335dd970f41SEd Schouten 		if (proc_compare(p, ppick))
336dd970f41SEd Schouten 			p = ppick;
337bea45cddSEd Schouten 
338dd970f41SEd Schouten 	PROC_LOCK(p);
339dd970f41SEd Schouten 	PGRP_UNLOCK(tp->t_pgrp);
340dd970f41SEd Schouten 	td = NULL;
341dd970f41SEd Schouten 	FOREACH_THREAD_IN_PROC(p, tdpick)
342dd970f41SEd Schouten 		if (thread_compare(td, tdpick))
343dd970f41SEd Schouten 			td = tdpick;
344bea45cddSEd Schouten 	stateprefix = "";
345bea45cddSEd Schouten 	thread_lock(td);
346bea45cddSEd Schouten 	if (TD_IS_RUNNING(td))
347bea45cddSEd Schouten 		state = "running";
348bea45cddSEd Schouten 	else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
349bea45cddSEd Schouten 		state = "runnable";
350bea45cddSEd Schouten 	else if (TD_IS_SLEEPING(td)) {
351bea45cddSEd Schouten 		/* XXX: If we're sleeping, are we ever not in a queue? */
352bea45cddSEd Schouten 		if (TD_ON_SLEEPQ(td))
353bea45cddSEd Schouten 			state = td->td_wmesg;
354bea45cddSEd Schouten 		else
355bea45cddSEd Schouten 			state = "sleeping without queue";
356bea45cddSEd Schouten 	} else if (TD_ON_LOCK(td)) {
357bea45cddSEd Schouten 		state = td->td_lockname;
358bea45cddSEd Schouten 		stateprefix = "*";
359bea45cddSEd Schouten 	} else if (TD_IS_SUSPENDED(td))
360bea45cddSEd Schouten 		state = "suspended";
361bea45cddSEd Schouten 	else if (TD_AWAITING_INTR(td))
362bea45cddSEd Schouten 		state = "intrwait";
363dd970f41SEd Schouten 	else if (p->p_state == PRS_ZOMBIE)
364ad765b09SRobert Watson 		state = "zombie";
365bea45cddSEd Schouten 	else
366bea45cddSEd Schouten 		state = "unknown";
367bea45cddSEd Schouten 	pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
36893940996SConrad Meyer #ifdef STACK
369cd1c083dSPawel Biernacki 	kstacks_val = atomic_load_int(&tty_info_kstacks);
370cd1c083dSPawel Biernacki 	print_kstacks = (kstacks_val != STACK_SBUF_FMT_NONE);
371cd1c083dSPawel Biernacki 
372cd1c083dSPawel Biernacki 	if (print_kstacks) {
3735d3bd9e2SMark Johnston 		if (TD_IS_SWAPPED(td))
3745d3bd9e2SMark Johnston 			sterr = ENOENT;
3751c29da02SMark Johnston 		else
3761c29da02SMark Johnston 			sterr = stack_save_td(&stack, td);
377d7aa89c3SConrad Meyer 	}
37893940996SConrad Meyer #endif
379bea45cddSEd Schouten 	thread_unlock(td);
380dd970f41SEd Schouten 	if (p->p_state == PRS_NEW || p->p_state == PRS_ZOMBIE)
381bea45cddSEd Schouten 		rss = 0;
382bea45cddSEd Schouten 	else
383dd970f41SEd Schouten 		rss = pgtok(vmspace_resident_count(p->p_vmspace));
384dd970f41SEd Schouten 	microuptime(&rtime);
385dd970f41SEd Schouten 	timevalsub(&rtime, &p->p_stats->p_start);
386dd970f41SEd Schouten 	rufetchcalc(p, &ru, &utime, &stime);
387dd970f41SEd Schouten 	pid = p->p_pid;
388dd970f41SEd Schouten 	strlcpy(comm, p->p_comm, sizeof comm);
389dd970f41SEd Schouten 	PROC_UNLOCK(p);
390bea45cddSEd Schouten 
391dd970f41SEd Schouten 	/* Print command, pid, state, rtime, utime, stime, %cpu, and rss. */
3926858c2ccSConrad Meyer 	sbuf_printf(&sb,
393dd970f41SEd Schouten 	    " cmd: %s %d [%s%s] %ld.%02ldr %ld.%02ldu %ld.%02lds %d%% %ldk\n",
394bea45cddSEd Schouten 	    comm, pid, stateprefix, state,
395dd970f41SEd Schouten 	    (long)rtime.tv_sec, rtime.tv_usec / 10000,
396bea45cddSEd Schouten 	    (long)utime.tv_sec, utime.tv_usec / 10000,
397bea45cddSEd Schouten 	    (long)stime.tv_sec, stime.tv_usec / 10000,
398bea45cddSEd Schouten 	    pctcpu / 100, rss);
3996858c2ccSConrad Meyer 
40093940996SConrad Meyer #ifdef STACK
401cd1c083dSPawel Biernacki 	if (print_kstacks && sterr == 0)
402cd1c083dSPawel Biernacki 		stack_sbuf_print_flags(&sb, &stack, M_NOWAIT, kstacks_val);
40393940996SConrad Meyer #endif
404d7aa89c3SConrad Meyer 
4056858c2ccSConrad Meyer out:
4066858c2ccSConrad Meyer 	sbuf_finish(&sb);
4076858c2ccSConrad Meyer 	sbuf_delete(&sb);
408bea45cddSEd Schouten }
409