2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Copyright (c) 2002 Networks Associates Technologies, Inc.
13 * All rights reserved.
15 * Portions of this software were developed for the FreeBSD Project by
16 * ThinkSec AS and NAI Labs, the Security Research Division of Network
17 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
18 * ("CBOSS"), as part of the DARPA CHATS research program.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include <sys/cdefs.h>
46 #include "opt_stack.h"
48 #include <sys/param.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/resourcevar.h>
57 #include <sys/sched.h>
58 #include <sys/stack.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
65 #include <vm/vm_map.h>
68 * Returns 1 if p2 is "better" than p1
70 * The algorithm for picking the "interesting" process is thus:
72 * 1) Only foreground processes are eligible - implied.
73 * 2) Runnable processes are favored over anything else. The runner
74 * with the highest cpu utilization is picked (p_estcpu). Ties are
75 * broken by picking the highest pid.
76 * 3) The sleeper with the shortest sleep time is next. With ties,
77 * we pick out just "short-term" sleepers (P_SINTR == 0).
78 * 4) Further ties are broken by picking the highest pid.
81 #define TESTAB(a, b) ((a)<<1 | (b))
87 proc_sum(struct proc *p, fixpt_t *estcpup)
95 FOREACH_THREAD_IN_PROC(p, td) {
100 estcpu += sched_pctcpu(td);
109 thread_compare(struct thread *td, struct thread *td2)
119 * Fetch running stats, pctcpu usage, and interruptable flag.
122 runa = TD_IS_RUNNING(td) || TD_ON_RUNQ(td);
123 slpa = td->td_flags & TDF_SINTR;
124 esta = sched_pctcpu(td);
127 runb = TD_IS_RUNNING(td2) || TD_ON_RUNQ(td2);
128 estb = sched_pctcpu(td2);
129 slpb = td2->td_flags & TDF_SINTR;
132 * see if at least one of them is runnable
134 switch (TESTAB(runa, runb)) {
143 * favor one with highest recent cpu utilization
150 * favor one sleeping in a non-interruptible sleep
152 switch (TESTAB(slpa, slpb)) {
165 proc_compare(struct proc *p1, struct proc *p2)
175 * Fetch various stats about these processes. After we drop the
176 * lock the information could be stale but the race is unimportant.
179 runa = proc_sum(p1, &esta);
182 runb = proc_sum(p2, &estb);
186 * see if at least one of them is runnable
188 switch (TESTAB(runa, runb)) {
197 * favor one with highest recent cpu utilization
206 switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
215 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
219 sbuf_tty_drain(void *a, const char *d, int len)
230 if (tp != NULL && !KERNEL_PANICKED()) {
231 rc = tty_putstrn(tp, d, len);
241 static int tty_info_kstacks = STACK_SBUF_FMT_COMPACT;
243 static int tty_info_kstacks = STACK_SBUF_FMT_NONE;
247 sysctl_tty_info_kstacks(SYSCTL_HANDLER_ARGS)
249 enum stack_sbuf_fmt val;
252 val = tty_info_kstacks;
253 error = sysctl_handle_int(oidp, &val, 0, req);
254 if (error != 0 || req->newptr == NULL)
258 case STACK_SBUF_FMT_NONE:
259 case STACK_SBUF_FMT_LONG:
260 case STACK_SBUF_FMT_COMPACT:
261 tty_info_kstacks = val;
269 SYSCTL_PROC(_kern, OID_AUTO, tty_info_kstacks,
270 CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_INT, NULL, 0,
271 sysctl_tty_info_kstacks, "I",
272 "Adjust format of kernel stack(9) traces on ^T (tty info): "
273 "0 - disabled; 1 - long; 2 - compact");
277 * Report on state of foreground process group.
280 tty_info(struct tty *tp)
282 struct timeval rtime, utime, stime;
285 int sterr, kstacks_val;
288 struct proc *p, *ppick;
289 struct thread *td, *tdpick;
290 const char *stateprefix, *state;
295 char comm[MAXCOMLEN + 1];
298 tty_assert_locked(tp);
300 if (tty_checkoutq(tp) == 0)
303 (void)sbuf_new(&sb, tp->t_prbuf, tp->t_prbufsz, SBUF_FIXEDLEN);
304 sbuf_set_drain(&sb, sbuf_tty_drain, tp);
306 /* Print load average. */
307 load = ((int64_t)averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
308 sbuf_printf(&sb, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n",
309 load / 100, load % 100);
311 if (tp->t_session == NULL) {
312 sbuf_cat(&sb, "not a controlling terminal\n");
315 if (tp->t_pgrp == NULL) {
316 sbuf_cat(&sb, "no foreground process group\n");
319 PGRP_LOCK(tp->t_pgrp);
320 if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
321 PGRP_UNLOCK(tp->t_pgrp);
322 sbuf_cat(&sb, "empty foreground process group\n");
327 * Pick the most interesting process and copy some of its
328 * state for printing later. This operation could rely on stale
329 * data as we can't hold the proc slock or thread locks over the
330 * whole list. However, we're guaranteed not to reference an exited
331 * thread or proc since we hold the tty locked.
334 LIST_FOREACH(ppick, &tp->t_pgrp->pg_members, p_pglist)
335 if (proc_compare(p, ppick))
339 PGRP_UNLOCK(tp->t_pgrp);
341 FOREACH_THREAD_IN_PROC(p, tdpick)
342 if (thread_compare(td, tdpick))
346 if (TD_IS_RUNNING(td))
348 else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
350 else if (TD_IS_SLEEPING(td)) {
351 /* XXX: If we're sleeping, are we ever not in a queue? */
352 if (TD_ON_SLEEPQ(td))
353 state = td->td_wmesg;
355 state = "sleeping without queue";
356 } else if (TD_ON_LOCK(td)) {
357 state = td->td_lockname;
359 } else if (TD_IS_SUSPENDED(td))
361 else if (TD_AWAITING_INTR(td))
363 else if (p->p_state == PRS_ZOMBIE)
367 pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
369 kstacks_val = atomic_load_int(&tty_info_kstacks);
370 print_kstacks = (kstacks_val != STACK_SBUF_FMT_NONE);
373 if (TD_IS_SWAPPED(td))
376 sterr = stack_save_td(&stack, td);
380 if (p->p_state == PRS_NEW || p->p_state == PRS_ZOMBIE)
383 rss = pgtok(vmspace_resident_count(p->p_vmspace));
385 timevalsub(&rtime, &p->p_stats->p_start);
386 rufetchcalc(p, &ru, &utime, &stime);
388 strlcpy(comm, p->p_comm, sizeof comm);
391 /* Print command, pid, state, rtime, utime, stime, %cpu, and rss. */
393 " cmd: %s %d [%s%s] %ld.%02ldr %ld.%02ldu %ld.%02lds %d%% %ldk\n",
394 comm, pid, stateprefix, state,
395 (long)rtime.tv_sec, rtime.tv_usec / 10000,
396 (long)utime.tv_sec, utime.tv_usec / 10000,
397 (long)stime.tv_sec, stime.tv_usec / 10000,
401 if (print_kstacks && sterr == 0)
402 stack_sbuf_print_flags(&sb, &stack, M_NOWAIT, kstacks_val);