]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/tty_info.c
Import tzdata 2018f
[FreeBSD/FreeBSD.git] / sys / kern / tty_info.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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.
11  *
12  * Copyright (c) 2002 Networks Associates Technologies, Inc.
13  * All rights reserved.
14  *
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.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
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.
31  *
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
42  * SUCH DAMAGE.
43  */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include <sys/param.h>
49 #include <sys/cons.h>
50 #include <sys/kdb.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sbuf.h>
57 #include <sys/sched.h>
58 #include <sys/stack.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 #include <sys/tty.h>
62
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66
67 /*
68  * Returns 1 if p2 is "better" than p1
69  *
70  * The algorithm for picking the "interesting" process is thus:
71  *
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.
79  */
80
81 #define TESTAB(a, b)    ((a)<<1 | (b))
82 #define ONLYA   2
83 #define ONLYB   1
84 #define BOTH    3
85
86 static int
87 proc_sum(struct proc *p, fixpt_t *estcpup)
88 {
89         struct thread *td;
90         int estcpu;
91         int val;
92
93         val = 0;
94         estcpu = 0;
95         FOREACH_THREAD_IN_PROC(p, td) {
96                 thread_lock(td);
97                 if (TD_ON_RUNQ(td) ||
98                     TD_IS_RUNNING(td))
99                         val = 1;
100                 estcpu += sched_pctcpu(td);
101                 thread_unlock(td);
102         }
103         *estcpup = estcpu;
104
105         return (val);
106 }
107
108 static int
109 thread_compare(struct thread *td, struct thread *td2)
110 {
111         int runa, runb;
112         int slpa, slpb;
113         fixpt_t esta, estb;
114
115         if (td == NULL)
116                 return (1);
117
118         /*
119          * Fetch running stats, pctcpu usage, and interruptable flag.
120          */
121         thread_lock(td);
122         runa = TD_IS_RUNNING(td) | TD_ON_RUNQ(td);
123         slpa = td->td_flags & TDF_SINTR;
124         esta = sched_pctcpu(td);
125         thread_unlock(td);
126         thread_lock(td2);
127         runb = TD_IS_RUNNING(td2) | TD_ON_RUNQ(td2);
128         estb = sched_pctcpu(td2);
129         slpb = td2->td_flags & TDF_SINTR;
130         thread_unlock(td2);
131         /*
132          * see if at least one of them is runnable
133          */
134         switch (TESTAB(runa, runb)) {
135         case ONLYA:
136                 return (0);
137         case ONLYB:
138                 return (1);
139         case BOTH:
140                 break;
141         }
142         /*
143          *  favor one with highest recent cpu utilization
144          */
145         if (estb > esta)
146                 return (1);
147         if (esta > estb)
148                 return (0);
149         /*
150          * favor one sleeping in a non-interruptible sleep
151          */
152         switch (TESTAB(slpa, slpb)) {
153         case ONLYA:
154                 return (0);
155         case ONLYB:
156                 return (1);
157         case BOTH:
158                 break;
159         }
160
161         return (td < td2);
162 }
163
164 static int
165 proc_compare(struct proc *p1, struct proc *p2)
166 {
167
168         int runa, runb;
169         fixpt_t esta, estb;
170
171         if (p1 == NULL)
172                 return (1);
173
174         /*
175          * Fetch various stats about these processes.  After we drop the
176          * lock the information could be stale but the race is unimportant.
177          */
178         PROC_LOCK(p1);
179         runa = proc_sum(p1, &esta);
180         PROC_UNLOCK(p1);
181         PROC_LOCK(p2);
182         runb = proc_sum(p2, &estb);
183         PROC_UNLOCK(p2);
184
185         /*
186          * see if at least one of them is runnable
187          */
188         switch (TESTAB(runa, runb)) {
189         case ONLYA:
190                 return (0);
191         case ONLYB:
192                 return (1);
193         case BOTH:
194                 break;
195         }
196         /*
197          *  favor one with highest recent cpu utilization
198          */
199         if (estb > esta)
200                 return (1);
201         if (esta > estb)
202                 return (0);
203         /*
204          * weed out zombies
205          */
206         switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
207         case ONLYA:
208                 return (1);
209         case ONLYB:
210                 return (0);
211         case BOTH:
212                 break;
213         }
214
215         return (p2->p_pid > p1->p_pid);         /* tie - return highest pid */
216 }
217
218 static int
219 sbuf_tty_drain(void *a, const char *d, int len)
220 {
221         struct tty *tp;
222         int rc;
223
224         tp = a;
225
226         if (kdb_active) {
227                 cnputsn(d, len);
228                 return (len);
229         }
230         if (tp != NULL && panicstr == NULL) {
231                 rc = tty_putstrn(tp, d, len);
232                 if (rc != 0)
233                         return (-ENXIO);
234                 return (len);
235         }
236         return (-ENXIO);
237 }
238
239 static bool tty_info_kstacks = false;
240 SYSCTL_BOOL(_kern, OID_AUTO, tty_info_kstacks, CTLFLAG_RWTUN,
241     &tty_info_kstacks, 0,
242     "Enable printing kernel stack(9) traces on ^T (tty info)");
243
244 /*
245  * Report on state of foreground process group.
246  */
247 void
248 tty_info(struct tty *tp)
249 {
250         struct timeval rtime, utime, stime;
251         struct stack stack;
252         struct proc *p, *ppick;
253         struct thread *td, *tdpick;
254         const char *stateprefix, *state;
255         struct sbuf sb;
256         long rss;
257         int load, pctcpu, sterr;
258         pid_t pid;
259         char comm[MAXCOMLEN + 1];
260         struct rusage ru;
261
262         tty_lock_assert(tp, MA_OWNED);
263
264         if (tty_checkoutq(tp) == 0)
265                 return;
266
267         (void)sbuf_new(&sb, tp->t_prbuf, sizeof(tp->t_prbuf), SBUF_FIXEDLEN);
268         sbuf_set_drain(&sb, sbuf_tty_drain, tp);
269
270         /* Print load average. */
271         load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
272         sbuf_printf(&sb, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n",
273             load / 100, load % 100);
274
275         if (tp->t_session == NULL) {
276                 sbuf_printf(&sb, "not a controlling terminal\n");
277                 goto out;
278         }
279         if (tp->t_pgrp == NULL) {
280                 sbuf_printf(&sb, "no foreground process group\n");
281                 goto out;
282         }
283         PGRP_LOCK(tp->t_pgrp);
284         if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
285                 PGRP_UNLOCK(tp->t_pgrp);
286                 sbuf_printf(&sb, "empty foreground process group\n");
287                 goto out;
288         }
289
290         /*
291          * Pick the most interesting process and copy some of its
292          * state for printing later.  This operation could rely on stale
293          * data as we can't hold the proc slock or thread locks over the
294          * whole list. However, we're guaranteed not to reference an exited
295          * thread or proc since we hold the tty locked.
296          */
297         p = NULL;
298         LIST_FOREACH(ppick, &tp->t_pgrp->pg_members, p_pglist)
299                 if (proc_compare(p, ppick))
300                         p = ppick;
301
302         PROC_LOCK(p);
303         PGRP_UNLOCK(tp->t_pgrp);
304         td = NULL;
305         FOREACH_THREAD_IN_PROC(p, tdpick)
306                 if (thread_compare(td, tdpick))
307                         td = tdpick;
308         stateprefix = "";
309         thread_lock(td);
310         if (TD_IS_RUNNING(td))
311                 state = "running";
312         else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
313                 state = "runnable";
314         else if (TD_IS_SLEEPING(td)) {
315                 /* XXX: If we're sleeping, are we ever not in a queue? */
316                 if (TD_ON_SLEEPQ(td))
317                         state = td->td_wmesg;
318                 else
319                         state = "sleeping without queue";
320         } else if (TD_ON_LOCK(td)) {
321                 state = td->td_lockname;
322                 stateprefix = "*";
323         } else if (TD_IS_SUSPENDED(td))
324                 state = "suspended";
325         else if (TD_AWAITING_INTR(td))
326                 state = "intrwait";
327         else if (p->p_state == PRS_ZOMBIE)
328                 state = "zombie";
329         else
330                 state = "unknown";
331         pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
332         if (tty_info_kstacks) {
333                 stack_zero(&stack);
334                 if (TD_IS_SWAPPED(td) || TD_IS_RUNNING(td))
335                         sterr = stack_save_td_running(&stack, td);
336                 else {
337                         stack_save_td(&stack, td);
338                         sterr = 0;
339                 }
340         }
341         thread_unlock(td);
342         if (p->p_state == PRS_NEW || p->p_state == PRS_ZOMBIE)
343                 rss = 0;
344         else
345                 rss = pgtok(vmspace_resident_count(p->p_vmspace));
346         microuptime(&rtime);
347         timevalsub(&rtime, &p->p_stats->p_start);
348         rufetchcalc(p, &ru, &utime, &stime);
349         pid = p->p_pid;
350         strlcpy(comm, p->p_comm, sizeof comm);
351         PROC_UNLOCK(p);
352
353         /* Print command, pid, state, rtime, utime, stime, %cpu, and rss. */
354         sbuf_printf(&sb,
355             " cmd: %s %d [%s%s] %ld.%02ldr %ld.%02ldu %ld.%02lds %d%% %ldk\n",
356             comm, pid, stateprefix, state,
357             (long)rtime.tv_sec, rtime.tv_usec / 10000,
358             (long)utime.tv_sec, utime.tv_usec / 10000,
359             (long)stime.tv_sec, stime.tv_usec / 10000,
360             pctcpu / 100, rss);
361
362         if (tty_info_kstacks && sterr == 0)
363                 stack_sbuf_print_flags(&sb, &stack, M_NOWAIT);
364
365 out:
366         sbuf_finish(&sb);
367         sbuf_delete(&sb);
368 }