xref: /freebsd/sys/kern/sched_4bsd.c (revision 3d7f41175dddea95e605d034f272b86b3cc35654)
1b43179fbSJeff Roberson /*-
2b43179fbSJeff Roberson  * Copyright (c) 1982, 1986, 1990, 1991, 1993
3b43179fbSJeff Roberson  *	The Regents of the University of California.  All rights reserved.
4b43179fbSJeff Roberson  * (c) UNIX System Laboratories, Inc.
5b43179fbSJeff Roberson  * All or some portions of this file are derived from material licensed
6b43179fbSJeff Roberson  * to the University of California by American Telephone and Telegraph
7b43179fbSJeff Roberson  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8b43179fbSJeff Roberson  * the permission of UNIX System Laboratories, Inc.
9b43179fbSJeff Roberson  *
10b43179fbSJeff Roberson  * Redistribution and use in source and binary forms, with or without
11b43179fbSJeff Roberson  * modification, are permitted provided that the following conditions
12b43179fbSJeff Roberson  * are met:
13b43179fbSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
14b43179fbSJeff Roberson  *    notice, this list of conditions and the following disclaimer.
15b43179fbSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
16b43179fbSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
17b43179fbSJeff Roberson  *    documentation and/or other materials provided with the distribution.
18b43179fbSJeff Roberson  * 4. Neither the name of the University nor the names of its contributors
19b43179fbSJeff Roberson  *    may be used to endorse or promote products derived from this software
20b43179fbSJeff Roberson  *    without specific prior written permission.
21b43179fbSJeff Roberson  *
22b43179fbSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23b43179fbSJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b43179fbSJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b43179fbSJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26b43179fbSJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b43179fbSJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b43179fbSJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b43179fbSJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b43179fbSJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b43179fbSJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b43179fbSJeff Roberson  * SUCH DAMAGE.
33b43179fbSJeff Roberson  */
34b43179fbSJeff Roberson 
35677b542eSDavid E. O'Brien #include <sys/cdefs.h>
36677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
37677b542eSDavid E. O'Brien 
384da0d332SPeter Wemm #include "opt_hwpmc_hooks.h"
39a564bfc7SJeff Roberson #include "opt_sched.h"
406f5f25e5SJohn Birrell #include "opt_kdtrace.h"
414da0d332SPeter Wemm 
42b43179fbSJeff Roberson #include <sys/param.h>
43b43179fbSJeff Roberson #include <sys/systm.h>
44f5a3ef99SMarcel Moolenaar #include <sys/cpuset.h>
45b43179fbSJeff Roberson #include <sys/kernel.h>
46b43179fbSJeff Roberson #include <sys/ktr.h>
47b43179fbSJeff Roberson #include <sys/lock.h>
48c55bbb6cSJohn Baldwin #include <sys/kthread.h>
49b43179fbSJeff Roberson #include <sys/mutex.h>
50b43179fbSJeff Roberson #include <sys/proc.h>
51b43179fbSJeff Roberson #include <sys/resourcevar.h>
52b43179fbSJeff Roberson #include <sys/sched.h>
53b3e9e682SRyan Stone #include <sys/sdt.h>
54b43179fbSJeff Roberson #include <sys/smp.h>
55b43179fbSJeff Roberson #include <sys/sysctl.h>
56b43179fbSJeff Roberson #include <sys/sx.h>
57f5c157d9SJohn Baldwin #include <sys/turnstile.h>
583db720fdSDavid Xu #include <sys/umtx.h>
592e4db89cSDavid E. O'Brien #include <machine/pcb.h>
60293968d8SJulian Elischer #include <machine/smp.h>
61b43179fbSJeff Roberson 
62ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS
63ebccf1e3SJoseph Koshy #include <sys/pmckern.h>
64ebccf1e3SJoseph Koshy #endif
65ebccf1e3SJoseph Koshy 
666f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS
676f5f25e5SJohn Birrell #include <sys/dtrace_bsd.h>
686f5f25e5SJohn Birrell int				dtrace_vtime_active;
696f5f25e5SJohn Birrell dtrace_vtime_switch_func_t	dtrace_vtime_switch_func;
706f5f25e5SJohn Birrell #endif
716f5f25e5SJohn Birrell 
7206439a04SJeff Roberson /*
7306439a04SJeff Roberson  * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
7406439a04SJeff Roberson  * the range 100-256 Hz (approximately).
7506439a04SJeff Roberson  */
7606439a04SJeff Roberson #define	ESTCPULIM(e) \
7706439a04SJeff Roberson     min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
7806439a04SJeff Roberson     RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
79b698380fSBruce Evans #ifdef SMP
80b698380fSBruce Evans #define	INVERSE_ESTCPU_WEIGHT	(8 * smp_cpus)
81b698380fSBruce Evans #else
8206439a04SJeff Roberson #define	INVERSE_ESTCPU_WEIGHT	8	/* 1 / (priorities per estcpu level). */
83b698380fSBruce Evans #endif
8406439a04SJeff Roberson #define	NICE_WEIGHT		1	/* Priorities per nice level. */
8506439a04SJeff Roberson 
860d2cf837SJeff Roberson #define	TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX)))
878f51ad55SJeff Roberson 
888460a577SJohn Birrell /*
898460a577SJohn Birrell  * The schedulable entity that runs a context.
90ad1e7d28SJulian Elischer  * This is  an extension to the thread structure and is tailored to
91ad1e7d28SJulian Elischer  * the requirements of this scheduler
928460a577SJohn Birrell  */
93ad1e7d28SJulian Elischer struct td_sched {
94ad1e7d28SJulian Elischer 	fixpt_t		ts_pctcpu;	/* (j) %cpu during p_swtime. */
95ad1e7d28SJulian Elischer 	int		ts_cpticks;	/* (j) Ticks of cpu time. */
9654b0e65fSJeff Roberson 	int		ts_slptime;	/* (j) Seconds !RUNNING. */
9748317e9eSAlexander Motin 	int		ts_slice;	/* Remaining part of time slice. */
98f200843bSJohn Baldwin 	int		ts_flags;
99ad1e7d28SJulian Elischer 	struct runq	*ts_runq;	/* runq the thread is currently on */
1008f51ad55SJeff Roberson #ifdef KTR
1018f51ad55SJeff Roberson 	char		ts_name[TS_NAME_LEN];
1028f51ad55SJeff Roberson #endif
103bcb06d59SJeff Roberson };
104ed062c8dSJulian Elischer 
105ed062c8dSJulian Elischer /* flags kept in td_flags */
106ad1e7d28SJulian Elischer #define TDF_DIDRUN	TDF_SCHED0	/* thread actually ran. */
1079727e637SJeff Roberson #define TDF_BOUND	TDF_SCHED1	/* Bound to one CPU. */
108*3d7f4117SAlexander Motin #define	TDF_SLICEEND	TDF_SCHED2	/* Thread time slice is over. */
109bcb06d59SJeff Roberson 
110f200843bSJohn Baldwin /* flags kept in ts_flags */
111f200843bSJohn Baldwin #define	TSF_AFFINITY	0x0001		/* Has a non-"full" CPU set. */
112f200843bSJohn Baldwin 
113ad1e7d28SJulian Elischer #define SKE_RUNQ_PCPU(ts)						\
114ad1e7d28SJulian Elischer     ((ts)->ts_runq != 0 && (ts)->ts_runq != &runq)
115e17c57b1SJeff Roberson 
116f200843bSJohn Baldwin #define	THREAD_CAN_SCHED(td, cpu)	\
117f200843bSJohn Baldwin     CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
118f200843bSJohn Baldwin 
119ad1e7d28SJulian Elischer static struct td_sched td_sched0;
1206ea38de8SJeff Roberson struct mtx sched_lock;
121b43179fbSJeff Roberson 
12248317e9eSAlexander Motin static int	realstathz;	/* stathz is sometimes 0 and run off of hz. */
123ca59f152SJeff Roberson static int	sched_tdcnt;	/* Total runnable threads in the system. */
12448317e9eSAlexander Motin static int	sched_slice = 1; /* Thread run time before rescheduling. */
125b43179fbSJeff Roberson 
126e17c57b1SJeff Roberson static void	setup_runqs(void);
127c55bbb6cSJohn Baldwin static void	schedcpu(void);
128e17c57b1SJeff Roberson static void	schedcpu_thread(void);
129f5c157d9SJohn Baldwin static void	sched_priority(struct thread *td, u_char prio);
130b43179fbSJeff Roberson static void	sched_setup(void *dummy);
131b43179fbSJeff Roberson static void	maybe_resched(struct thread *td);
1328460a577SJohn Birrell static void	updatepri(struct thread *td);
1338460a577SJohn Birrell static void	resetpriority(struct thread *td);
1348460a577SJohn Birrell static void	resetpriority_thread(struct thread *td);
13500b0483dSJulian Elischer #ifdef SMP
136f200843bSJohn Baldwin static int	sched_pickcpu(struct thread *td);
13782a1dfc1SJulian Elischer static int	forward_wakeup(int cpunum);
1388aa3d7ffSJohn Baldwin static void	kick_other_cpu(int pri, int cpuid);
13900b0483dSJulian Elischer #endif
140b43179fbSJeff Roberson 
141e17c57b1SJeff Roberson static struct kproc_desc sched_kp = {
142e17c57b1SJeff Roberson         "schedcpu",
143e17c57b1SJeff Roberson         schedcpu_thread,
144e17c57b1SJeff Roberson         NULL
145e17c57b1SJeff Roberson };
146237fdd78SRobert Watson SYSINIT(schedcpu, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, kproc_start,
147237fdd78SRobert Watson     &sched_kp);
148237fdd78SRobert Watson SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
149b43179fbSJeff Roberson 
15048317e9eSAlexander Motin static void sched_initticks(void *dummy);
15148317e9eSAlexander Motin SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks,
15248317e9eSAlexander Motin     NULL);
15348317e9eSAlexander Motin 
154b43179fbSJeff Roberson /*
155b43179fbSJeff Roberson  * Global run queue.
156b43179fbSJeff Roberson  */
157b43179fbSJeff Roberson static struct runq runq;
158e17c57b1SJeff Roberson 
159e17c57b1SJeff Roberson #ifdef SMP
160e17c57b1SJeff Roberson /*
161e17c57b1SJeff Roberson  * Per-CPU run queues
162e17c57b1SJeff Roberson  */
163e17c57b1SJeff Roberson static struct runq runq_pcpu[MAXCPU];
164f200843bSJohn Baldwin long runq_length[MAXCPU];
1653121f534SAttilio Rao 
16671a19bdcSAttilio Rao static cpuset_t idle_cpus_mask;
167e17c57b1SJeff Roberson #endif
168e17c57b1SJeff Roberson 
169b722ad00SAlexander Motin struct pcpuidlestat {
170b722ad00SAlexander Motin 	u_int idlecalls;
171b722ad00SAlexander Motin 	u_int oldidlecalls;
172b722ad00SAlexander Motin };
1733e288e62SDimitry Andric static DPCPU_DEFINE(struct pcpuidlestat, idlestat);
174b722ad00SAlexander Motin 
175e17c57b1SJeff Roberson static void
176e17c57b1SJeff Roberson setup_runqs(void)
177e17c57b1SJeff Roberson {
178e17c57b1SJeff Roberson #ifdef SMP
179e17c57b1SJeff Roberson 	int i;
180e17c57b1SJeff Roberson 
181e17c57b1SJeff Roberson 	for (i = 0; i < MAXCPU; ++i)
182e17c57b1SJeff Roberson 		runq_init(&runq_pcpu[i]);
183e17c57b1SJeff Roberson #endif
184e17c57b1SJeff Roberson 
185e17c57b1SJeff Roberson 	runq_init(&runq);
186e17c57b1SJeff Roberson }
187b43179fbSJeff Roberson 
188e038d354SScott Long SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RD, 0, "Scheduler");
189dc095794SScott Long 
190e038d354SScott Long SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "4BSD", 0,
191e038d354SScott Long     "Scheduler name");
19248317e9eSAlexander Motin SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
19348317e9eSAlexander Motin     "Slice size for timeshare threads");
19437c28a02SJulian Elischer #ifdef SMP
19582a1dfc1SJulian Elischer /* Enable forwarding of wakeups to all other cpus */
1966472ac3dSEd Schouten static SYSCTL_NODE(_kern_sched, OID_AUTO, ipiwakeup, CTLFLAG_RD, NULL,
1976472ac3dSEd Schouten     "Kernel SMP");
19882a1dfc1SJulian Elischer 
199a90f3f25SJeff Roberson static int runq_fuzz = 1;
200a90f3f25SJeff Roberson SYSCTL_INT(_kern_sched, OID_AUTO, runq_fuzz, CTLFLAG_RW, &runq_fuzz, 0, "");
201a90f3f25SJeff Roberson 
202bce73aedSJulian Elischer static int forward_wakeup_enabled = 1;
20382a1dfc1SJulian Elischer SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, enabled, CTLFLAG_RW,
20482a1dfc1SJulian Elischer 	   &forward_wakeup_enabled, 0,
20582a1dfc1SJulian Elischer 	   "Forwarding of wakeup to idle CPUs");
20682a1dfc1SJulian Elischer 
20782a1dfc1SJulian Elischer static int forward_wakeups_requested = 0;
20882a1dfc1SJulian Elischer SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, requested, CTLFLAG_RD,
20982a1dfc1SJulian Elischer 	   &forward_wakeups_requested, 0,
21082a1dfc1SJulian Elischer 	   "Requests for Forwarding of wakeup to idle CPUs");
21182a1dfc1SJulian Elischer 
21282a1dfc1SJulian Elischer static int forward_wakeups_delivered = 0;
21382a1dfc1SJulian Elischer SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, delivered, CTLFLAG_RD,
21482a1dfc1SJulian Elischer 	   &forward_wakeups_delivered, 0,
21582a1dfc1SJulian Elischer 	   "Completed Forwarding of wakeup to idle CPUs");
21682a1dfc1SJulian Elischer 
217bce73aedSJulian Elischer static int forward_wakeup_use_mask = 1;
21882a1dfc1SJulian Elischer SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, usemask, CTLFLAG_RW,
21982a1dfc1SJulian Elischer 	   &forward_wakeup_use_mask, 0,
22082a1dfc1SJulian Elischer 	   "Use the mask of idle cpus");
22182a1dfc1SJulian Elischer 
22282a1dfc1SJulian Elischer static int forward_wakeup_use_loop = 0;
22382a1dfc1SJulian Elischer SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, useloop, CTLFLAG_RW,
22482a1dfc1SJulian Elischer 	   &forward_wakeup_use_loop, 0,
22582a1dfc1SJulian Elischer 	   "Use a loop to find idle cpus");
22682a1dfc1SJulian Elischer 
22737c28a02SJulian Elischer #endif
228ad1e7d28SJulian Elischer #if 0
2293389af30SJulian Elischer static int sched_followon = 0;
2303389af30SJulian Elischer SYSCTL_INT(_kern_sched, OID_AUTO, followon, CTLFLAG_RW,
2313389af30SJulian Elischer 	   &sched_followon, 0,
2323389af30SJulian Elischer 	   "allow threads to share a quantum");
2338460a577SJohn Birrell #endif
23482a1dfc1SJulian Elischer 
235b3e9e682SRyan Stone SDT_PROVIDER_DEFINE(sched);
236b3e9e682SRyan Stone 
237b3e9e682SRyan Stone SDT_PROBE_DEFINE3(sched, , , change_pri, change-pri, "struct thread *",
238b3e9e682SRyan Stone     "struct proc *", "uint8_t");
239b3e9e682SRyan Stone SDT_PROBE_DEFINE3(sched, , , dequeue, dequeue, "struct thread *",
240b3e9e682SRyan Stone     "struct proc *", "void *");
241b3e9e682SRyan Stone SDT_PROBE_DEFINE4(sched, , , enqueue, enqueue, "struct thread *",
242b3e9e682SRyan Stone     "struct proc *", "void *", "int");
243b3e9e682SRyan Stone SDT_PROBE_DEFINE4(sched, , , lend_pri, lend-pri, "struct thread *",
244b3e9e682SRyan Stone     "struct proc *", "uint8_t", "struct thread *");
245b3e9e682SRyan Stone SDT_PROBE_DEFINE2(sched, , , load_change, load-change, "int", "int");
246b3e9e682SRyan Stone SDT_PROBE_DEFINE2(sched, , , off_cpu, off-cpu, "struct thread *",
247b3e9e682SRyan Stone     "struct proc *");
248b3e9e682SRyan Stone SDT_PROBE_DEFINE(sched, , , on_cpu, on-cpu);
249b3e9e682SRyan Stone SDT_PROBE_DEFINE(sched, , , remain_cpu, remain-cpu);
250b3e9e682SRyan Stone SDT_PROBE_DEFINE2(sched, , , surrender, surrender, "struct thread *",
251b3e9e682SRyan Stone     "struct proc *");
252b3e9e682SRyan Stone 
253907bdbc2SJeff Roberson static __inline void
254907bdbc2SJeff Roberson sched_load_add(void)
255907bdbc2SJeff Roberson {
2568f51ad55SJeff Roberson 
257907bdbc2SJeff Roberson 	sched_tdcnt++;
2588f51ad55SJeff Roberson 	KTR_COUNTER0(KTR_SCHED, "load", "global load", sched_tdcnt);
259b3e9e682SRyan Stone 	SDT_PROBE2(sched, , , load_change, NOCPU, sched_tdcnt);
260907bdbc2SJeff Roberson }
261907bdbc2SJeff Roberson 
262907bdbc2SJeff Roberson static __inline void
263907bdbc2SJeff Roberson sched_load_rem(void)
264907bdbc2SJeff Roberson {
2658f51ad55SJeff Roberson 
266907bdbc2SJeff Roberson 	sched_tdcnt--;
2678f51ad55SJeff Roberson 	KTR_COUNTER0(KTR_SCHED, "load", "global load", sched_tdcnt);
268b3e9e682SRyan Stone 	SDT_PROBE2(sched, , , load_change, NOCPU, sched_tdcnt);
269907bdbc2SJeff Roberson }
270b43179fbSJeff Roberson /*
271b43179fbSJeff Roberson  * Arrange to reschedule if necessary, taking the priorities and
272b43179fbSJeff Roberson  * schedulers into account.
273b43179fbSJeff Roberson  */
274b43179fbSJeff Roberson static void
275b43179fbSJeff Roberson maybe_resched(struct thread *td)
276b43179fbSJeff Roberson {
277b43179fbSJeff Roberson 
2787b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
279ed062c8dSJulian Elischer 	if (td->td_priority < curthread->td_priority)
2804a338afdSJulian Elischer 		curthread->td_flags |= TDF_NEEDRESCHED;
281b43179fbSJeff Roberson }
282b43179fbSJeff Roberson 
283b43179fbSJeff Roberson /*
284a90f3f25SJeff Roberson  * This function is called when a thread is about to be put on run queue
285a90f3f25SJeff Roberson  * because it has been made runnable or its priority has been adjusted.  It
286a90f3f25SJeff Roberson  * determines if the new thread should be immediately preempted to.  If so,
287a90f3f25SJeff Roberson  * it switches to it and eventually returns true.  If not, it returns false
288a90f3f25SJeff Roberson  * so that the caller may place the thread on an appropriate run queue.
289a90f3f25SJeff Roberson  */
290a90f3f25SJeff Roberson int
291a90f3f25SJeff Roberson maybe_preempt(struct thread *td)
292a90f3f25SJeff Roberson {
293a90f3f25SJeff Roberson #ifdef PREEMPTION
294a90f3f25SJeff Roberson 	struct thread *ctd;
295a90f3f25SJeff Roberson 	int cpri, pri;
296a90f3f25SJeff Roberson 
297a90f3f25SJeff Roberson 	/*
298a90f3f25SJeff Roberson 	 * The new thread should not preempt the current thread if any of the
299a90f3f25SJeff Roberson 	 * following conditions are true:
300a90f3f25SJeff Roberson 	 *
301a90f3f25SJeff Roberson 	 *  - The kernel is in the throes of crashing (panicstr).
302a90f3f25SJeff Roberson 	 *  - The current thread has a higher (numerically lower) or
303a90f3f25SJeff Roberson 	 *    equivalent priority.  Note that this prevents curthread from
304a90f3f25SJeff Roberson 	 *    trying to preempt to itself.
305a90f3f25SJeff Roberson 	 *  - It is too early in the boot for context switches (cold is set).
306a90f3f25SJeff Roberson 	 *  - The current thread has an inhibitor set or is in the process of
307a90f3f25SJeff Roberson 	 *    exiting.  In this case, the current thread is about to switch
308a90f3f25SJeff Roberson 	 *    out anyways, so there's no point in preempting.  If we did,
309a90f3f25SJeff Roberson 	 *    the current thread would not be properly resumed as well, so
310a90f3f25SJeff Roberson 	 *    just avoid that whole landmine.
311a90f3f25SJeff Roberson 	 *  - If the new thread's priority is not a realtime priority and
312a90f3f25SJeff Roberson 	 *    the current thread's priority is not an idle priority and
313a90f3f25SJeff Roberson 	 *    FULL_PREEMPTION is disabled.
314a90f3f25SJeff Roberson 	 *
315a90f3f25SJeff Roberson 	 * If all of these conditions are false, but the current thread is in
316a90f3f25SJeff Roberson 	 * a nested critical section, then we have to defer the preemption
317a90f3f25SJeff Roberson 	 * until we exit the critical section.  Otherwise, switch immediately
318a90f3f25SJeff Roberson 	 * to the new thread.
319a90f3f25SJeff Roberson 	 */
320a90f3f25SJeff Roberson 	ctd = curthread;
321a90f3f25SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
322a90f3f25SJeff Roberson 	KASSERT((td->td_inhibitors == 0),
323a90f3f25SJeff Roberson 			("maybe_preempt: trying to run inhibited thread"));
324a90f3f25SJeff Roberson 	pri = td->td_priority;
325a90f3f25SJeff Roberson 	cpri = ctd->td_priority;
326a90f3f25SJeff Roberson 	if (panicstr != NULL || pri >= cpri || cold /* || dumping */ ||
327a90f3f25SJeff Roberson 	    TD_IS_INHIBITED(ctd))
328a90f3f25SJeff Roberson 		return (0);
329a90f3f25SJeff Roberson #ifndef FULL_PREEMPTION
330a90f3f25SJeff Roberson 	if (pri > PRI_MAX_ITHD && cpri < PRI_MIN_IDLE)
331a90f3f25SJeff Roberson 		return (0);
332a90f3f25SJeff Roberson #endif
333a90f3f25SJeff Roberson 
334a90f3f25SJeff Roberson 	if (ctd->td_critnest > 1) {
335a90f3f25SJeff Roberson 		CTR1(KTR_PROC, "maybe_preempt: in critical section %d",
336a90f3f25SJeff Roberson 		    ctd->td_critnest);
337a90f3f25SJeff Roberson 		ctd->td_owepreempt = 1;
338a90f3f25SJeff Roberson 		return (0);
339a90f3f25SJeff Roberson 	}
340a90f3f25SJeff Roberson 	/*
341a90f3f25SJeff Roberson 	 * Thread is runnable but not yet put on system run queue.
342a90f3f25SJeff Roberson 	 */
343a90f3f25SJeff Roberson 	MPASS(ctd->td_lock == td->td_lock);
344a90f3f25SJeff Roberson 	MPASS(TD_ON_RUNQ(td));
345a90f3f25SJeff Roberson 	TD_SET_RUNNING(td);
346a90f3f25SJeff Roberson 	CTR3(KTR_PROC, "preempting to thread %p (pid %d, %s)\n", td,
347a90f3f25SJeff Roberson 	    td->td_proc->p_pid, td->td_name);
3488df78c41SJeff Roberson 	mi_switch(SW_INVOL | SW_PREEMPT | SWT_PREEMPT, td);
349a90f3f25SJeff Roberson 	/*
350a90f3f25SJeff Roberson 	 * td's lock pointer may have changed.  We have to return with it
351a90f3f25SJeff Roberson 	 * locked.
352a90f3f25SJeff Roberson 	 */
353a90f3f25SJeff Roberson 	spinlock_enter();
354a90f3f25SJeff Roberson 	thread_unlock(ctd);
355a90f3f25SJeff Roberson 	thread_lock(td);
356a90f3f25SJeff Roberson 	spinlock_exit();
357a90f3f25SJeff Roberson 	return (1);
358a90f3f25SJeff Roberson #else
359a90f3f25SJeff Roberson 	return (0);
360a90f3f25SJeff Roberson #endif
361a90f3f25SJeff Roberson }
362a90f3f25SJeff Roberson 
363a90f3f25SJeff Roberson /*
364b43179fbSJeff Roberson  * Constants for digital decay and forget:
3658460a577SJohn Birrell  *	90% of (td_estcpu) usage in 5 * loadav time
366ad1e7d28SJulian Elischer  *	95% of (ts_pctcpu) usage in 60 seconds (load insensitive)
367b43179fbSJeff Roberson  *          Note that, as ps(1) mentions, this can let percentages
368b43179fbSJeff Roberson  *          total over 100% (I've seen 137.9% for 3 processes).
369b43179fbSJeff Roberson  *
3708460a577SJohn Birrell  * Note that schedclock() updates td_estcpu and p_cpticks asynchronously.
371b43179fbSJeff Roberson  *
3728460a577SJohn Birrell  * We wish to decay away 90% of td_estcpu in (5 * loadavg) seconds.
373b43179fbSJeff Roberson  * That is, the system wants to compute a value of decay such
374b43179fbSJeff Roberson  * that the following for loop:
375b43179fbSJeff Roberson  * 	for (i = 0; i < (5 * loadavg); i++)
3768460a577SJohn Birrell  * 		td_estcpu *= decay;
377b43179fbSJeff Roberson  * will compute
3788460a577SJohn Birrell  * 	td_estcpu *= 0.1;
379b43179fbSJeff Roberson  * for all values of loadavg:
380b43179fbSJeff Roberson  *
381b43179fbSJeff Roberson  * Mathematically this loop can be expressed by saying:
382b43179fbSJeff Roberson  * 	decay ** (5 * loadavg) ~= .1
383b43179fbSJeff Roberson  *
384b43179fbSJeff Roberson  * The system computes decay as:
385b43179fbSJeff Roberson  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
386b43179fbSJeff Roberson  *
387b43179fbSJeff Roberson  * We wish to prove that the system's computation of decay
388b43179fbSJeff Roberson  * will always fulfill the equation:
389b43179fbSJeff Roberson  * 	decay ** (5 * loadavg) ~= .1
390b43179fbSJeff Roberson  *
391b43179fbSJeff Roberson  * If we compute b as:
392b43179fbSJeff Roberson  * 	b = 2 * loadavg
393b43179fbSJeff Roberson  * then
394b43179fbSJeff Roberson  * 	decay = b / (b + 1)
395b43179fbSJeff Roberson  *
396b43179fbSJeff Roberson  * We now need to prove two things:
397b43179fbSJeff Roberson  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
398b43179fbSJeff Roberson  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
399b43179fbSJeff Roberson  *
400b43179fbSJeff Roberson  * Facts:
401b43179fbSJeff Roberson  *         For x close to zero, exp(x) =~ 1 + x, since
402b43179fbSJeff Roberson  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
403b43179fbSJeff Roberson  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
404b43179fbSJeff Roberson  *         For x close to zero, ln(1+x) =~ x, since
405b43179fbSJeff Roberson  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
406b43179fbSJeff Roberson  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
407b43179fbSJeff Roberson  *         ln(.1) =~ -2.30
408b43179fbSJeff Roberson  *
409b43179fbSJeff Roberson  * Proof of (1):
410b43179fbSJeff Roberson  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
411b43179fbSJeff Roberson  *	solving for factor,
412b43179fbSJeff Roberson  *      ln(factor) =~ (-2.30/5*loadav), or
413b43179fbSJeff Roberson  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
414b43179fbSJeff Roberson  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
415b43179fbSJeff Roberson  *
416b43179fbSJeff Roberson  * Proof of (2):
417b43179fbSJeff Roberson  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
418b43179fbSJeff Roberson  *	solving for power,
419b43179fbSJeff Roberson  *      power*ln(b/(b+1)) =~ -2.30, or
420b43179fbSJeff Roberson  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
421b43179fbSJeff Roberson  *
422b43179fbSJeff Roberson  * Actual power values for the implemented algorithm are as follows:
423b43179fbSJeff Roberson  *      loadav: 1       2       3       4
424b43179fbSJeff Roberson  *      power:  5.68    10.32   14.94   19.55
425b43179fbSJeff Roberson  */
426b43179fbSJeff Roberson 
427b43179fbSJeff Roberson /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
428b43179fbSJeff Roberson #define	loadfactor(loadav)	(2 * (loadav))
429b43179fbSJeff Roberson #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
430b43179fbSJeff Roberson 
431ad1e7d28SJulian Elischer /* decay 95% of `ts_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
432b43179fbSJeff Roberson static fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;	/* exp(-1/20) */
43352c0b557SMatthew D Fleming SYSCTL_UINT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
434b43179fbSJeff Roberson 
435b43179fbSJeff Roberson /*
436b43179fbSJeff Roberson  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
437b43179fbSJeff Roberson  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
438b43179fbSJeff Roberson  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
439b43179fbSJeff Roberson  *
440b43179fbSJeff Roberson  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
441b43179fbSJeff Roberson  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
442b43179fbSJeff Roberson  *
443b43179fbSJeff Roberson  * If you don't want to bother with the faster/more-accurate formula, you
444b43179fbSJeff Roberson  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
445b43179fbSJeff Roberson  * (more general) method of calculating the %age of CPU used by a process.
446b43179fbSJeff Roberson  */
447b43179fbSJeff Roberson #define	CCPU_SHIFT	11
448b43179fbSJeff Roberson 
449b43179fbSJeff Roberson /*
450b43179fbSJeff Roberson  * Recompute process priorities, every hz ticks.
451b43179fbSJeff Roberson  * MP-safe, called without the Giant mutex.
452b43179fbSJeff Roberson  */
453b43179fbSJeff Roberson /* ARGSUSED */
454b43179fbSJeff Roberson static void
455c55bbb6cSJohn Baldwin schedcpu(void)
456b43179fbSJeff Roberson {
457b43179fbSJeff Roberson 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
458b43179fbSJeff Roberson 	struct thread *td;
459b43179fbSJeff Roberson 	struct proc *p;
460ad1e7d28SJulian Elischer 	struct td_sched *ts;
46148317e9eSAlexander Motin 	int awake;
462b43179fbSJeff Roberson 
463b43179fbSJeff Roberson 	sx_slock(&allproc_lock);
464b43179fbSJeff Roberson 	FOREACH_PROC_IN_SYSTEM(p) {
465374ae2a3SJeff Roberson 		PROC_LOCK(p);
466e806d352SJohn Baldwin 		if (p->p_state == PRS_NEW) {
467e806d352SJohn Baldwin 			PROC_UNLOCK(p);
468e806d352SJohn Baldwin 			continue;
469e806d352SJohn Baldwin 		}
4708460a577SJohn Birrell 		FOREACH_THREAD_IN_PROC(p, td) {
471b43179fbSJeff Roberson 			awake = 0;
4727b20fb19SJeff Roberson 			thread_lock(td);
473ad1e7d28SJulian Elischer 			ts = td->td_sched;
474b43179fbSJeff Roberson 			/*
47570fca427SJohn Baldwin 			 * Increment sleep time (if sleeping).  We
47670fca427SJohn Baldwin 			 * ignore overflow, as above.
477b43179fbSJeff Roberson 			 */
478b43179fbSJeff Roberson 			/*
479ad1e7d28SJulian Elischer 			 * The td_sched slptimes are not touched in wakeup
480ad1e7d28SJulian Elischer 			 * because the thread may not HAVE everything in
481ad1e7d28SJulian Elischer 			 * memory? XXX I think this is out of date.
482b43179fbSJeff Roberson 			 */
483f0393f06SJeff Roberson 			if (TD_ON_RUNQ(td)) {
484b43179fbSJeff Roberson 				awake = 1;
4859727e637SJeff Roberson 				td->td_flags &= ~TDF_DIDRUN;
486f0393f06SJeff Roberson 			} else if (TD_IS_RUNNING(td)) {
487b43179fbSJeff Roberson 				awake = 1;
4889727e637SJeff Roberson 				/* Do not clear TDF_DIDRUN */
4899727e637SJeff Roberson 			} else if (td->td_flags & TDF_DIDRUN) {
490b43179fbSJeff Roberson 				awake = 1;
4919727e637SJeff Roberson 				td->td_flags &= ~TDF_DIDRUN;
492b43179fbSJeff Roberson 			}
493b43179fbSJeff Roberson 
494b43179fbSJeff Roberson 			/*
495ad1e7d28SJulian Elischer 			 * ts_pctcpu is only for ps and ttyinfo().
496b43179fbSJeff Roberson 			 */
497ad1e7d28SJulian Elischer 			ts->ts_pctcpu = (ts->ts_pctcpu * ccpu) >> FSHIFT;
498b43179fbSJeff Roberson 			/*
499ad1e7d28SJulian Elischer 			 * If the td_sched has been idle the entire second,
500b43179fbSJeff Roberson 			 * stop recalculating its priority until
501b43179fbSJeff Roberson 			 * it wakes up.
502b43179fbSJeff Roberson 			 */
503ad1e7d28SJulian Elischer 			if (ts->ts_cpticks != 0) {
504b43179fbSJeff Roberson #if	(FSHIFT >= CCPU_SHIFT)
505ad1e7d28SJulian Elischer 				ts->ts_pctcpu += (realstathz == 100)
506ad1e7d28SJulian Elischer 				    ? ((fixpt_t) ts->ts_cpticks) <<
507b43179fbSJeff Roberson 				    (FSHIFT - CCPU_SHIFT) :
508ad1e7d28SJulian Elischer 				    100 * (((fixpt_t) ts->ts_cpticks)
509bcb06d59SJeff Roberson 				    << (FSHIFT - CCPU_SHIFT)) / realstathz;
510b43179fbSJeff Roberson #else
511ad1e7d28SJulian Elischer 				ts->ts_pctcpu += ((FSCALE - ccpu) *
512ad1e7d28SJulian Elischer 				    (ts->ts_cpticks *
513bcb06d59SJeff Roberson 				    FSCALE / realstathz)) >> FSHIFT;
514b43179fbSJeff Roberson #endif
515ad1e7d28SJulian Elischer 				ts->ts_cpticks = 0;
5168460a577SJohn Birrell 			}
5178460a577SJohn Birrell 			/*
5188460a577SJohn Birrell 			 * If there are ANY running threads in this process,
519b43179fbSJeff Roberson 			 * then don't count it as sleeping.
5208aa3d7ffSJohn Baldwin 			 * XXX: this is broken.
521b43179fbSJeff Roberson 			 */
522b43179fbSJeff Roberson 			if (awake) {
52354b0e65fSJeff Roberson 				if (ts->ts_slptime > 1) {
524b43179fbSJeff Roberson 					/*
525b43179fbSJeff Roberson 					 * In an ideal world, this should not
526b43179fbSJeff Roberson 					 * happen, because whoever woke us
527b43179fbSJeff Roberson 					 * up from the long sleep should have
528b43179fbSJeff Roberson 					 * unwound the slptime and reset our
529b43179fbSJeff Roberson 					 * priority before we run at the stale
530b43179fbSJeff Roberson 					 * priority.  Should KASSERT at some
531b43179fbSJeff Roberson 					 * point when all the cases are fixed.
532b43179fbSJeff Roberson 					 */
5338460a577SJohn Birrell 					updatepri(td);
5348460a577SJohn Birrell 				}
53554b0e65fSJeff Roberson 				ts->ts_slptime = 0;
5368460a577SJohn Birrell 			} else
53754b0e65fSJeff Roberson 				ts->ts_slptime++;
53854b0e65fSJeff Roberson 			if (ts->ts_slptime > 1) {
5397b20fb19SJeff Roberson 				thread_unlock(td);
5408460a577SJohn Birrell 				continue;
5417b20fb19SJeff Roberson 			}
5428460a577SJohn Birrell 			td->td_estcpu = decay_cpu(loadfac, td->td_estcpu);
5438460a577SJohn Birrell 		      	resetpriority(td);
5448460a577SJohn Birrell 			resetpriority_thread(td);
5457b20fb19SJeff Roberson 			thread_unlock(td);
5468aa3d7ffSJohn Baldwin 		}
547374ae2a3SJeff Roberson 		PROC_UNLOCK(p);
5488aa3d7ffSJohn Baldwin 	}
549b43179fbSJeff Roberson 	sx_sunlock(&allproc_lock);
550c55bbb6cSJohn Baldwin }
551c55bbb6cSJohn Baldwin 
552c55bbb6cSJohn Baldwin /*
553c55bbb6cSJohn Baldwin  * Main loop for a kthread that executes schedcpu once a second.
554c55bbb6cSJohn Baldwin  */
555c55bbb6cSJohn Baldwin static void
556e17c57b1SJeff Roberson schedcpu_thread(void)
557c55bbb6cSJohn Baldwin {
558c55bbb6cSJohn Baldwin 
559c55bbb6cSJohn Baldwin 	for (;;) {
560c55bbb6cSJohn Baldwin 		schedcpu();
5614d70511aSJohn Baldwin 		pause("-", hz);
562c55bbb6cSJohn Baldwin 	}
563b43179fbSJeff Roberson }
564b43179fbSJeff Roberson 
565b43179fbSJeff Roberson /*
566b43179fbSJeff Roberson  * Recalculate the priority of a process after it has slept for a while.
5678460a577SJohn Birrell  * For all load averages >= 1 and max td_estcpu of 255, sleeping for at
5688460a577SJohn Birrell  * least six times the loadfactor will decay td_estcpu to zero.
569b43179fbSJeff Roberson  */
570b43179fbSJeff Roberson static void
5718460a577SJohn Birrell updatepri(struct thread *td)
572b43179fbSJeff Roberson {
57354b0e65fSJeff Roberson 	struct td_sched *ts;
57454b0e65fSJeff Roberson 	fixpt_t loadfac;
57554b0e65fSJeff Roberson 	unsigned int newcpu;
576b43179fbSJeff Roberson 
57754b0e65fSJeff Roberson 	ts = td->td_sched;
57870fca427SJohn Baldwin 	loadfac = loadfactor(averunnable.ldavg[0]);
57954b0e65fSJeff Roberson 	if (ts->ts_slptime > 5 * loadfac)
5808460a577SJohn Birrell 		td->td_estcpu = 0;
581b43179fbSJeff Roberson 	else {
5828460a577SJohn Birrell 		newcpu = td->td_estcpu;
58354b0e65fSJeff Roberson 		ts->ts_slptime--;	/* was incremented in schedcpu() */
58454b0e65fSJeff Roberson 		while (newcpu && --ts->ts_slptime)
585b43179fbSJeff Roberson 			newcpu = decay_cpu(loadfac, newcpu);
5868460a577SJohn Birrell 		td->td_estcpu = newcpu;
587b43179fbSJeff Roberson 	}
588b43179fbSJeff Roberson }
589b43179fbSJeff Roberson 
590b43179fbSJeff Roberson /*
591b43179fbSJeff Roberson  * Compute the priority of a process when running in user mode.
592b43179fbSJeff Roberson  * Arrange to reschedule if the resulting priority is better
593b43179fbSJeff Roberson  * than that of the current process.
594b43179fbSJeff Roberson  */
595b43179fbSJeff Roberson static void
5968460a577SJohn Birrell resetpriority(struct thread *td)
597b43179fbSJeff Roberson {
598b43179fbSJeff Roberson 	register unsigned int newpriority;
599b43179fbSJeff Roberson 
6008460a577SJohn Birrell 	if (td->td_pri_class == PRI_TIMESHARE) {
6018460a577SJohn Birrell 		newpriority = PUSER + td->td_estcpu / INVERSE_ESTCPU_WEIGHT +
6028460a577SJohn Birrell 		    NICE_WEIGHT * (td->td_proc->p_nice - PRIO_MIN);
603b43179fbSJeff Roberson 		newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
604b43179fbSJeff Roberson 		    PRI_MAX_TIMESHARE);
6058460a577SJohn Birrell 		sched_user_prio(td, newpriority);
606b43179fbSJeff Roberson 	}
607b43179fbSJeff Roberson }
608f5c157d9SJohn Baldwin 
609f5c157d9SJohn Baldwin /*
610ad1e7d28SJulian Elischer  * Update the thread's priority when the associated process's user
611f5c157d9SJohn Baldwin  * priority changes.
612f5c157d9SJohn Baldwin  */
613f5c157d9SJohn Baldwin static void
6148460a577SJohn Birrell resetpriority_thread(struct thread *td)
615f5c157d9SJohn Baldwin {
616f5c157d9SJohn Baldwin 
617f5c157d9SJohn Baldwin 	/* Only change threads with a time sharing user priority. */
618f5c157d9SJohn Baldwin 	if (td->td_priority < PRI_MIN_TIMESHARE ||
619f5c157d9SJohn Baldwin 	    td->td_priority > PRI_MAX_TIMESHARE)
620f5c157d9SJohn Baldwin 		return;
621f5c157d9SJohn Baldwin 
622f5c157d9SJohn Baldwin 	/* XXX the whole needresched thing is broken, but not silly. */
623f5c157d9SJohn Baldwin 	maybe_resched(td);
624f5c157d9SJohn Baldwin 
6258460a577SJohn Birrell 	sched_prio(td, td->td_user_pri);
626b43179fbSJeff Roberson }
627b43179fbSJeff Roberson 
628b43179fbSJeff Roberson /* ARGSUSED */
629b43179fbSJeff Roberson static void
630b43179fbSJeff Roberson sched_setup(void *dummy)
631b43179fbSJeff Roberson {
632e17c57b1SJeff Roberson 	setup_runqs();
63370fca427SJohn Baldwin 
63448317e9eSAlexander Motin 	/*
63548317e9eSAlexander Motin 	 * To avoid divide-by-zero, we set realstathz a dummy value
63648317e9eSAlexander Motin 	 * in case which sched_clock() called before sched_initticks().
63748317e9eSAlexander Motin 	 */
63848317e9eSAlexander Motin 	realstathz = hz;
63948317e9eSAlexander Motin 	sched_slice = realstathz / 10;	/* ~100ms */
640b43179fbSJeff Roberson 
641ca59f152SJeff Roberson 	/* Account for thread0. */
642907bdbc2SJeff Roberson 	sched_load_add();
643b43179fbSJeff Roberson }
644b43179fbSJeff Roberson 
64548317e9eSAlexander Motin /*
64648317e9eSAlexander Motin  * This routine determines the sched_slice after stathz and hz are setup.
64748317e9eSAlexander Motin  */
64848317e9eSAlexander Motin static void
64948317e9eSAlexander Motin sched_initticks(void *dummy)
65048317e9eSAlexander Motin {
65148317e9eSAlexander Motin 
65248317e9eSAlexander Motin 	realstathz = stathz ? stathz : hz;
65348317e9eSAlexander Motin 	sched_slice = realstathz / 10;	/* ~100ms */
65448317e9eSAlexander Motin }
65548317e9eSAlexander Motin 
656b43179fbSJeff Roberson /* External interfaces start here */
6578aa3d7ffSJohn Baldwin 
658ed062c8dSJulian Elischer /*
659ed062c8dSJulian Elischer  * Very early in the boot some setup of scheduler-specific
660f3050486SMaxim Konovalov  * parts of proc0 and of some scheduler resources needs to be done.
661ed062c8dSJulian Elischer  * Called from:
662ed062c8dSJulian Elischer  *  proc0_init()
663ed062c8dSJulian Elischer  */
664ed062c8dSJulian Elischer void
665ed062c8dSJulian Elischer schedinit(void)
666ed062c8dSJulian Elischer {
667ed062c8dSJulian Elischer 	/*
668ed062c8dSJulian Elischer 	 * Set up the scheduler specific parts of proc0.
669ed062c8dSJulian Elischer 	 */
670ed062c8dSJulian Elischer 	proc0.p_sched = NULL; /* XXX */
671ad1e7d28SJulian Elischer 	thread0.td_sched = &td_sched0;
6727b20fb19SJeff Roberson 	thread0.td_lock = &sched_lock;
67348317e9eSAlexander Motin 	td_sched0.ts_slice = sched_slice;
6746ea38de8SJeff Roberson 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
675ed062c8dSJulian Elischer }
676ed062c8dSJulian Elischer 
677b43179fbSJeff Roberson int
678b43179fbSJeff Roberson sched_runnable(void)
679b43179fbSJeff Roberson {
680e17c57b1SJeff Roberson #ifdef SMP
681e17c57b1SJeff Roberson 	return runq_check(&runq) + runq_check(&runq_pcpu[PCPU_GET(cpuid)]);
682e17c57b1SJeff Roberson #else
683b43179fbSJeff Roberson 	return runq_check(&runq);
684e17c57b1SJeff Roberson #endif
685b43179fbSJeff Roberson }
686b43179fbSJeff Roberson 
687b43179fbSJeff Roberson int
688b43179fbSJeff Roberson sched_rr_interval(void)
689b43179fbSJeff Roberson {
69048317e9eSAlexander Motin 
69148317e9eSAlexander Motin 	/* Convert sched_slice from stathz to hz. */
69248317e9eSAlexander Motin 	return (hz / (realstathz / sched_slice));
693b43179fbSJeff Roberson }
694b43179fbSJeff Roberson 
695b43179fbSJeff Roberson /*
696b43179fbSJeff Roberson  * We adjust the priority of the current process.  The priority of
697b43179fbSJeff Roberson  * a process gets worse as it accumulates CPU time.  The cpu usage
6988460a577SJohn Birrell  * estimator (td_estcpu) is increased here.  resetpriority() will
6998460a577SJohn Birrell  * compute a different priority each time td_estcpu increases by
700b43179fbSJeff Roberson  * INVERSE_ESTCPU_WEIGHT
701b43179fbSJeff Roberson  * (until MAXPRI is reached).  The cpu usage estimator ramps up
702b43179fbSJeff Roberson  * quite quickly when the process is running (linearly), and decays
703b43179fbSJeff Roberson  * away exponentially, at a rate which is proportionally slower when
704b43179fbSJeff Roberson  * the system is busy.  The basic principle is that the system will
705b43179fbSJeff Roberson  * 90% forget that the process used a lot of CPU time in 5 * loadav
706b43179fbSJeff Roberson  * seconds.  This causes the system to favor processes which haven't
707b43179fbSJeff Roberson  * run much recently, and to round-robin among other processes.
708b43179fbSJeff Roberson  */
709b43179fbSJeff Roberson void
7107cf90fb3SJeff Roberson sched_clock(struct thread *td)
711b43179fbSJeff Roberson {
712b722ad00SAlexander Motin 	struct pcpuidlestat *stat;
713ad1e7d28SJulian Elischer 	struct td_sched *ts;
714b43179fbSJeff Roberson 
7157b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
716ad1e7d28SJulian Elischer 	ts = td->td_sched;
717f7f9e7f3SJeff Roberson 
718ad1e7d28SJulian Elischer 	ts->ts_cpticks++;
7198460a577SJohn Birrell 	td->td_estcpu = ESTCPULIM(td->td_estcpu + 1);
7208460a577SJohn Birrell 	if ((td->td_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
7218460a577SJohn Birrell 		resetpriority(td);
7228460a577SJohn Birrell 		resetpriority_thread(td);
723b43179fbSJeff Roberson 	}
7249dddab6fSJohn Baldwin 
7259dddab6fSJohn Baldwin 	/*
7269dddab6fSJohn Baldwin 	 * Force a context switch if the current thread has used up a full
7279dddab6fSJohn Baldwin 	 * quantum (default quantum is 100ms).
7289dddab6fSJohn Baldwin 	 */
72948317e9eSAlexander Motin 	if (!TD_IS_IDLETHREAD(td) && (--ts->ts_slice <= 0)) {
73048317e9eSAlexander Motin 		ts->ts_slice = sched_slice;
731*3d7f4117SAlexander Motin 		td->td_flags |= TDF_NEEDRESCHED | TDF_SLICEEND;
73248317e9eSAlexander Motin 	}
733b722ad00SAlexander Motin 
734b722ad00SAlexander Motin 	stat = DPCPU_PTR(idlestat);
735b722ad00SAlexander Motin 	stat->oldidlecalls = stat->idlecalls;
736b722ad00SAlexander Motin 	stat->idlecalls = 0;
737b43179fbSJeff Roberson }
73870fca427SJohn Baldwin 
7398460a577SJohn Birrell /*
7408aa3d7ffSJohn Baldwin  * Charge child's scheduling CPU usage to parent.
7418460a577SJohn Birrell  */
742b43179fbSJeff Roberson void
74355d44f79SJulian Elischer sched_exit(struct proc *p, struct thread *td)
744f7f9e7f3SJeff Roberson {
7458460a577SJohn Birrell 
7468f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "proc exit",
747cd39bb09SXin LI 	    "prio:%d", td->td_priority);
7488f51ad55SJeff Roberson 
749374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
750ad1e7d28SJulian Elischer 	sched_exit_thread(FIRST_THREAD_IN_PROC(p), td);
751b43179fbSJeff Roberson }
752b43179fbSJeff Roberson 
753b43179fbSJeff Roberson void
754f7f9e7f3SJeff Roberson sched_exit_thread(struct thread *td, struct thread *child)
755b43179fbSJeff Roberson {
756ad1e7d28SJulian Elischer 
7578f51ad55SJeff Roberson 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "exit",
758cd39bb09SXin LI 	    "prio:%d", child->td_priority);
7597b20fb19SJeff Roberson 	thread_lock(td);
760ad1e7d28SJulian Elischer 	td->td_estcpu = ESTCPULIM(td->td_estcpu + child->td_estcpu);
7617b20fb19SJeff Roberson 	thread_unlock(td);
7621b9d701fSAttilio Rao 	thread_lock(child);
7631b9d701fSAttilio Rao 	if ((child->td_flags & TDF_NOLOAD) == 0)
764907bdbc2SJeff Roberson 		sched_load_rem();
7651b9d701fSAttilio Rao 	thread_unlock(child);
766f7f9e7f3SJeff Roberson }
767bcb06d59SJeff Roberson 
768f7f9e7f3SJeff Roberson void
769ed062c8dSJulian Elischer sched_fork(struct thread *td, struct thread *childtd)
770f7f9e7f3SJeff Roberson {
771ed062c8dSJulian Elischer 	sched_fork_thread(td, childtd);
772f7f9e7f3SJeff Roberson }
773bcb06d59SJeff Roberson 
774f7f9e7f3SJeff Roberson void
775ed062c8dSJulian Elischer sched_fork_thread(struct thread *td, struct thread *childtd)
776f7f9e7f3SJeff Roberson {
7778b16c208SJeff Roberson 	struct td_sched *ts;
7788b16c208SJeff Roberson 
779ad1e7d28SJulian Elischer 	childtd->td_estcpu = td->td_estcpu;
7807b20fb19SJeff Roberson 	childtd->td_lock = &sched_lock;
781f5a3ef99SMarcel Moolenaar 	childtd->td_cpuset = cpuset_ref(td->td_cpuset);
78222d19207SJohn Baldwin 	childtd->td_priority = childtd->td_base_pri;
7838b16c208SJeff Roberson 	ts = childtd->td_sched;
7848b16c208SJeff Roberson 	bzero(ts, sizeof(*ts));
785f200843bSJohn Baldwin 	ts->ts_flags |= (td->td_sched->ts_flags & TSF_AFFINITY);
78648317e9eSAlexander Motin 	ts->ts_slice = 1;
787b43179fbSJeff Roberson }
788b43179fbSJeff Roberson 
789b43179fbSJeff Roberson void
790fa885116SJulian Elischer sched_nice(struct proc *p, int nice)
791b43179fbSJeff Roberson {
792f5c157d9SJohn Baldwin 	struct thread *td;
7930b5318c8SJohn Baldwin 
794fa885116SJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
795fa885116SJulian Elischer 	p->p_nice = nice;
7968460a577SJohn Birrell 	FOREACH_THREAD_IN_PROC(p, td) {
7977b20fb19SJeff Roberson 		thread_lock(td);
7988460a577SJohn Birrell 		resetpriority(td);
7998460a577SJohn Birrell 		resetpriority_thread(td);
8007b20fb19SJeff Roberson 		thread_unlock(td);
8018460a577SJohn Birrell 	}
802fa885116SJulian Elischer }
803b43179fbSJeff Roberson 
804f7f9e7f3SJeff Roberson void
8058460a577SJohn Birrell sched_class(struct thread *td, int class)
806f7f9e7f3SJeff Roberson {
8077b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
8088460a577SJohn Birrell 	td->td_pri_class = class;
809f7f9e7f3SJeff Roberson }
810f7f9e7f3SJeff Roberson 
8118460a577SJohn Birrell /*
8128460a577SJohn Birrell  * Adjust the priority of a thread.
8138460a577SJohn Birrell  */
814f5c157d9SJohn Baldwin static void
815f5c157d9SJohn Baldwin sched_priority(struct thread *td, u_char prio)
816b43179fbSJeff Roberson {
817b43179fbSJeff Roberson 
8188f51ad55SJeff Roberson 
8198f51ad55SJeff Roberson 	KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "priority change",
8208f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, "new prio:%d", prio, KTR_ATTR_LINKED,
8218f51ad55SJeff Roberson 	    sched_tdname(curthread));
822b3e9e682SRyan Stone 	SDT_PROBE3(sched, , , change_pri, td, td->td_proc, prio);
8238f51ad55SJeff Roberson 	if (td != curthread && prio > td->td_priority) {
8248f51ad55SJeff Roberson 		KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread),
8258f51ad55SJeff Roberson 		    "lend prio", "prio:%d", td->td_priority, "new prio:%d",
8268f51ad55SJeff Roberson 		    prio, KTR_ATTR_LINKED, sched_tdname(td));
827b3e9e682SRyan Stone 		SDT_PROBE4(sched, , , lend_pri, td, td->td_proc, prio,
828b3e9e682SRyan Stone 		    curthread);
8298f51ad55SJeff Roberson 	}
8307b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
831f5c157d9SJohn Baldwin 	if (td->td_priority == prio)
832f5c157d9SJohn Baldwin 		return;
8331f955e2dSJulian Elischer 	td->td_priority = prio;
8349727e637SJeff Roberson 	if (TD_ON_RUNQ(td) && td->td_rqindex != (prio / RQ_PPQ)) {
835f0393f06SJeff Roberson 		sched_rem(td);
836f0393f06SJeff Roberson 		sched_add(td, SRQ_BORING);
837b43179fbSJeff Roberson 	}
838b43179fbSJeff Roberson }
839b43179fbSJeff Roberson 
840f5c157d9SJohn Baldwin /*
841f5c157d9SJohn Baldwin  * Update a thread's priority when it is lent another thread's
842f5c157d9SJohn Baldwin  * priority.
843f5c157d9SJohn Baldwin  */
844f5c157d9SJohn Baldwin void
845f5c157d9SJohn Baldwin sched_lend_prio(struct thread *td, u_char prio)
846f5c157d9SJohn Baldwin {
847f5c157d9SJohn Baldwin 
848f5c157d9SJohn Baldwin 	td->td_flags |= TDF_BORROWING;
849f5c157d9SJohn Baldwin 	sched_priority(td, prio);
850f5c157d9SJohn Baldwin }
851f5c157d9SJohn Baldwin 
852f5c157d9SJohn Baldwin /*
853f5c157d9SJohn Baldwin  * Restore a thread's priority when priority propagation is
854f5c157d9SJohn Baldwin  * over.  The prio argument is the minimum priority the thread
855f5c157d9SJohn Baldwin  * needs to have to satisfy other possible priority lending
856f5c157d9SJohn Baldwin  * requests.  If the thread's regulary priority is less
857f5c157d9SJohn Baldwin  * important than prio the thread will keep a priority boost
858f5c157d9SJohn Baldwin  * of prio.
859f5c157d9SJohn Baldwin  */
860f5c157d9SJohn Baldwin void
861f5c157d9SJohn Baldwin sched_unlend_prio(struct thread *td, u_char prio)
862f5c157d9SJohn Baldwin {
863f5c157d9SJohn Baldwin 	u_char base_pri;
864f5c157d9SJohn Baldwin 
865f5c157d9SJohn Baldwin 	if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
866f5c157d9SJohn Baldwin 	    td->td_base_pri <= PRI_MAX_TIMESHARE)
8678460a577SJohn Birrell 		base_pri = td->td_user_pri;
868f5c157d9SJohn Baldwin 	else
869f5c157d9SJohn Baldwin 		base_pri = td->td_base_pri;
870f5c157d9SJohn Baldwin 	if (prio >= base_pri) {
871f5c157d9SJohn Baldwin 		td->td_flags &= ~TDF_BORROWING;
872f5c157d9SJohn Baldwin 		sched_prio(td, base_pri);
873f5c157d9SJohn Baldwin 	} else
874f5c157d9SJohn Baldwin 		sched_lend_prio(td, prio);
875f5c157d9SJohn Baldwin }
876f5c157d9SJohn Baldwin 
877f5c157d9SJohn Baldwin void
878f5c157d9SJohn Baldwin sched_prio(struct thread *td, u_char prio)
879f5c157d9SJohn Baldwin {
880f5c157d9SJohn Baldwin 	u_char oldprio;
881f5c157d9SJohn Baldwin 
882f5c157d9SJohn Baldwin 	/* First, update the base priority. */
883f5c157d9SJohn Baldwin 	td->td_base_pri = prio;
884f5c157d9SJohn Baldwin 
885f5c157d9SJohn Baldwin 	/*
886f5c157d9SJohn Baldwin 	 * If the thread is borrowing another thread's priority, don't ever
887f5c157d9SJohn Baldwin 	 * lower the priority.
888f5c157d9SJohn Baldwin 	 */
889f5c157d9SJohn Baldwin 	if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
890f5c157d9SJohn Baldwin 		return;
891f5c157d9SJohn Baldwin 
892f5c157d9SJohn Baldwin 	/* Change the real priority. */
893f5c157d9SJohn Baldwin 	oldprio = td->td_priority;
894f5c157d9SJohn Baldwin 	sched_priority(td, prio);
895f5c157d9SJohn Baldwin 
896f5c157d9SJohn Baldwin 	/*
897f5c157d9SJohn Baldwin 	 * If the thread is on a turnstile, then let the turnstile update
898f5c157d9SJohn Baldwin 	 * its state.
899f5c157d9SJohn Baldwin 	 */
900f5c157d9SJohn Baldwin 	if (TD_ON_LOCK(td) && oldprio != prio)
901f5c157d9SJohn Baldwin 		turnstile_adjust(td, oldprio);
902f5c157d9SJohn Baldwin }
903f5c157d9SJohn Baldwin 
904b43179fbSJeff Roberson void
9058460a577SJohn Birrell sched_user_prio(struct thread *td, u_char prio)
9063db720fdSDavid Xu {
9073db720fdSDavid Xu 
908435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
9098460a577SJohn Birrell 	td->td_base_user_pri = prio;
910acbe332aSDavid Xu 	if (td->td_lend_user_pri <= prio)
9115a215147SDavid Xu 		return;
9128460a577SJohn Birrell 	td->td_user_pri = prio;
9133db720fdSDavid Xu }
9143db720fdSDavid Xu 
9153db720fdSDavid Xu void
9163db720fdSDavid Xu sched_lend_user_prio(struct thread *td, u_char prio)
9173db720fdSDavid Xu {
9183db720fdSDavid Xu 
919435806d3SDavid Xu 	THREAD_LOCK_ASSERT(td, MA_OWNED);
920acbe332aSDavid Xu 	td->td_lend_user_pri = prio;
921c8e368a9SDavid Xu 	td->td_user_pri = min(prio, td->td_base_user_pri);
922c8e368a9SDavid Xu 	if (td->td_priority > td->td_user_pri)
923c8e368a9SDavid Xu 		sched_prio(td, td->td_user_pri);
924c8e368a9SDavid Xu 	else if (td->td_priority != td->td_user_pri)
925c8e368a9SDavid Xu 		td->td_flags |= TDF_NEEDRESCHED;
926435806d3SDavid Xu }
9273db720fdSDavid Xu 
9283db720fdSDavid Xu void
929c5aa6b58SJeff Roberson sched_sleep(struct thread *td, int pri)
930b43179fbSJeff Roberson {
9312056d0a1SJohn Baldwin 
9327b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
93354b0e65fSJeff Roberson 	td->td_slptick = ticks;
93454b0e65fSJeff Roberson 	td->td_sched->ts_slptime = 0;
9352dc29adbSJohn Baldwin 	if (pri != 0 && PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
936c5aa6b58SJeff Roberson 		sched_prio(td, pri);
93717c4c356SKonstantin Belousov 	if (TD_IS_SUSPENDED(td) || pri >= PSOCK)
938c5aa6b58SJeff Roberson 		td->td_flags |= TDF_CANSWAP;
939b43179fbSJeff Roberson }
940b43179fbSJeff Roberson 
941b43179fbSJeff Roberson void
9423389af30SJulian Elischer sched_switch(struct thread *td, struct thread *newtd, int flags)
943b43179fbSJeff Roberson {
944b0b9dee5SAttilio Rao 	struct mtx *tmtx;
945ad1e7d28SJulian Elischer 	struct td_sched *ts;
946b43179fbSJeff Roberson 	struct proc *p;
947*3d7f4117SAlexander Motin 	int preempted;
948b43179fbSJeff Roberson 
949b0b9dee5SAttilio Rao 	tmtx = NULL;
950ad1e7d28SJulian Elischer 	ts = td->td_sched;
951b43179fbSJeff Roberson 	p = td->td_proc;
952b43179fbSJeff Roberson 
9537b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
9548aa3d7ffSJohn Baldwin 
9557b20fb19SJeff Roberson 	/*
9567b20fb19SJeff Roberson 	 * Switch to the sched lock to fix things up and pick
9577b20fb19SJeff Roberson 	 * a new thread.
958b0b9dee5SAttilio Rao 	 * Block the td_lock in order to avoid breaking the critical path.
9597b20fb19SJeff Roberson 	 */
9607b20fb19SJeff Roberson 	if (td->td_lock != &sched_lock) {
9617b20fb19SJeff Roberson 		mtx_lock_spin(&sched_lock);
962b0b9dee5SAttilio Rao 		tmtx = thread_lock_block(td);
9637b20fb19SJeff Roberson 	}
964b43179fbSJeff Roberson 
9651b9d701fSAttilio Rao 	if ((td->td_flags & TDF_NOLOAD) == 0)
966907bdbc2SJeff Roberson 		sched_load_rem();
9673389af30SJulian Elischer 
968060563ecSJulian Elischer 	td->td_lastcpu = td->td_oncpu;
969*3d7f4117SAlexander Motin 	preempted = !(td->td_flags & TDF_SLICEEND);
970*3d7f4117SAlexander Motin 	td->td_flags &= ~(TDF_NEEDRESCHED | TDF_SLICEEND);
97177918643SStephan Uphoff 	td->td_owepreempt = 0;
972ca59f152SJeff Roberson 	td->td_oncpu = NOCPU;
9738aa3d7ffSJohn Baldwin 
974b43179fbSJeff Roberson 	/*
975b43179fbSJeff Roberson 	 * At the last moment, if this thread is still marked RUNNING,
976b43179fbSJeff Roberson 	 * then put it back on the run queue as it has not been suspended
977bf0acc27SJohn Baldwin 	 * or stopped or any thing else similar.  We never put the idle
978bf0acc27SJohn Baldwin 	 * threads on the run queue, however.
979b43179fbSJeff Roberson 	 */
980c6226eeaSJulian Elischer 	if (td->td_flags & TDF_IDLETD) {
981bf0acc27SJohn Baldwin 		TD_SET_CAN_RUN(td);
982c6226eeaSJulian Elischer #ifdef SMP
983a38f1f26SAttilio Rao 		CPU_CLR(PCPU_GET(cpuid), &idle_cpus_mask);
984c6226eeaSJulian Elischer #endif
985c6226eeaSJulian Elischer 	} else {
986ed062c8dSJulian Elischer 		if (TD_IS_RUNNING(td)) {
987ad1e7d28SJulian Elischer 			/* Put us back on the run queue. */
988*3d7f4117SAlexander Motin 			sched_add(td, preempted ?
989c20c691bSJulian Elischer 			    SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
990c20c691bSJulian Elischer 			    SRQ_OURSELF|SRQ_YIELDING);
991ed062c8dSJulian Elischer 		}
992b43179fbSJeff Roberson 	}
993c20c691bSJulian Elischer 	if (newtd) {
994c20c691bSJulian Elischer 		/*
995c20c691bSJulian Elischer 		 * The thread we are about to run needs to be counted
996c20c691bSJulian Elischer 		 * as if it had been added to the run queue and selected.
997c20c691bSJulian Elischer 		 * It came from:
998c20c691bSJulian Elischer 		 * * A preemption
999c20c691bSJulian Elischer 		 * * An upcall
1000c20c691bSJulian Elischer 		 * * A followon
1001c20c691bSJulian Elischer 		 */
1002c20c691bSJulian Elischer 		KASSERT((newtd->td_inhibitors == 0),
10032da78e38SRobert Watson 			("trying to run inhibited thread"));
10049727e637SJeff Roberson 		newtd->td_flags |= TDF_DIDRUN;
1005c20c691bSJulian Elischer         	TD_SET_RUNNING(newtd);
10061b9d701fSAttilio Rao 		if ((newtd->td_flags & TDF_NOLOAD) == 0)
1007907bdbc2SJeff Roberson 			sched_load_add();
1008c20c691bSJulian Elischer 	} else {
1009ae53b483SJeff Roberson 		newtd = choosethread();
10107b20fb19SJeff Roberson 		MPASS(newtd->td_lock == &sched_lock);
101158060789SAttilio Rao 	}
1012c20c691bSJulian Elischer 
1013ebccf1e3SJoseph Koshy 	if (td != newtd) {
1014ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1015ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1016ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1017ebccf1e3SJoseph Koshy #endif
1018b3e9e682SRyan Stone 
1019b3e9e682SRyan Stone 		SDT_PROBE2(sched, , , off_cpu, td, td->td_proc);
1020b3e9e682SRyan Stone 
1021c6226eeaSJulian Elischer                 /* I feel sleepy */
1022eea4f254SJeff Roberson 		lock_profile_release_lock(&sched_lock.lock_object);
10236f5f25e5SJohn Birrell #ifdef KDTRACE_HOOKS
10246f5f25e5SJohn Birrell 		/*
10256f5f25e5SJohn Birrell 		 * If DTrace has set the active vtime enum to anything
10266f5f25e5SJohn Birrell 		 * other than INACTIVE (0), then it should have set the
10276f5f25e5SJohn Birrell 		 * function to call.
10286f5f25e5SJohn Birrell 		 */
10296f5f25e5SJohn Birrell 		if (dtrace_vtime_active)
10306f5f25e5SJohn Birrell 			(*dtrace_vtime_switch_func)(newtd);
10316f5f25e5SJohn Birrell #endif
10326f5f25e5SJohn Birrell 
1033b0b9dee5SAttilio Rao 		cpu_switch(td, newtd, tmtx != NULL ? tmtx : td->td_lock);
1034eea4f254SJeff Roberson 		lock_profile_obtain_lock_success(&sched_lock.lock_object,
1035eea4f254SJeff Roberson 		    0, 0, __FILE__, __LINE__);
1036c6226eeaSJulian Elischer 		/*
1037c6226eeaSJulian Elischer 		 * Where am I?  What year is it?
1038c6226eeaSJulian Elischer 		 * We are in the same thread that went to sleep above,
10398aa3d7ffSJohn Baldwin 		 * but any amount of time may have passed. All our context
1040c6226eeaSJulian Elischer 		 * will still be available as will local variables.
1041c6226eeaSJulian Elischer 		 * PCPU values however may have changed as we may have
1042c6226eeaSJulian Elischer 		 * changed CPU so don't trust cached values of them.
1043c6226eeaSJulian Elischer 		 * New threads will go to fork_exit() instead of here
1044c6226eeaSJulian Elischer 		 * so if you change things here you may need to change
1045c6226eeaSJulian Elischer 		 * things there too.
10468aa3d7ffSJohn Baldwin 		 *
1047c6226eeaSJulian Elischer 		 * If the thread above was exiting it will never wake
1048c6226eeaSJulian Elischer 		 * up again here, so either it has saved everything it
1049c6226eeaSJulian Elischer 		 * needed to, or the thread_wait() or wait() will
1050c6226eeaSJulian Elischer 		 * need to reap it.
1051c6226eeaSJulian Elischer 		 */
1052b3e9e682SRyan Stone 
1053b3e9e682SRyan Stone 		SDT_PROBE0(sched, , , on_cpu);
1054ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
1055ebccf1e3SJoseph Koshy 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1056ebccf1e3SJoseph Koshy 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1057ebccf1e3SJoseph Koshy #endif
1058b3e9e682SRyan Stone 	} else
1059b3e9e682SRyan Stone 		SDT_PROBE0(sched, , , remain_cpu);
1060ebccf1e3SJoseph Koshy 
1061c6226eeaSJulian Elischer #ifdef SMP
1062c6226eeaSJulian Elischer 	if (td->td_flags & TDF_IDLETD)
1063a38f1f26SAttilio Rao 		CPU_SET(PCPU_GET(cpuid), &idle_cpus_mask);
1064c6226eeaSJulian Elischer #endif
1065ae53b483SJeff Roberson 	sched_lock.mtx_lock = (uintptr_t)td;
1066ae53b483SJeff Roberson 	td->td_oncpu = PCPU_GET(cpuid);
10677b20fb19SJeff Roberson 	MPASS(td->td_lock == &sched_lock);
1068b43179fbSJeff Roberson }
1069b43179fbSJeff Roberson 
1070b43179fbSJeff Roberson void
1071b43179fbSJeff Roberson sched_wakeup(struct thread *td)
1072b43179fbSJeff Roberson {
107354b0e65fSJeff Roberson 	struct td_sched *ts;
107454b0e65fSJeff Roberson 
10757b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
107654b0e65fSJeff Roberson 	ts = td->td_sched;
1077c5aa6b58SJeff Roberson 	td->td_flags &= ~TDF_CANSWAP;
107854b0e65fSJeff Roberson 	if (ts->ts_slptime > 1) {
10798460a577SJohn Birrell 		updatepri(td);
10808460a577SJohn Birrell 		resetpriority(td);
10818460a577SJohn Birrell 	}
10826eac7e57SAttilio Rao 	td->td_slptick = 0;
108354b0e65fSJeff Roberson 	ts->ts_slptime = 0;
108448317e9eSAlexander Motin 	ts->ts_slice = sched_slice;
1085f0393f06SJeff Roberson 	sched_add(td, SRQ_BORING);
1086b43179fbSJeff Roberson }
1087b43179fbSJeff Roberson 
108837c28a02SJulian Elischer #ifdef SMP
108982a1dfc1SJulian Elischer static int
109082a1dfc1SJulian Elischer forward_wakeup(int cpunum)
109182a1dfc1SJulian Elischer {
109282a1dfc1SJulian Elischer 	struct pcpu *pc;
1093a38f1f26SAttilio Rao 	cpuset_t dontuse, map, map2;
1094a38f1f26SAttilio Rao 	u_int id, me;
109571a19bdcSAttilio Rao 	int iscpuset;
109682a1dfc1SJulian Elischer 
109782a1dfc1SJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
109882a1dfc1SJulian Elischer 
1099ed062c8dSJulian Elischer 	CTR0(KTR_RUNQ, "forward_wakeup()");
110082a1dfc1SJulian Elischer 
110182a1dfc1SJulian Elischer 	if ((!forward_wakeup_enabled) ||
110282a1dfc1SJulian Elischer 	     (forward_wakeup_use_mask == 0 && forward_wakeup_use_loop == 0))
110382a1dfc1SJulian Elischer 		return (0);
110482a1dfc1SJulian Elischer 	if (!smp_started || cold || panicstr)
110582a1dfc1SJulian Elischer 		return (0);
110682a1dfc1SJulian Elischer 
110782a1dfc1SJulian Elischer 	forward_wakeups_requested++;
110882a1dfc1SJulian Elischer 
110982a1dfc1SJulian Elischer 	/*
11108aa3d7ffSJohn Baldwin 	 * Check the idle mask we received against what we calculated
11118aa3d7ffSJohn Baldwin 	 * before in the old version.
111282a1dfc1SJulian Elischer 	 */
1113a38f1f26SAttilio Rao 	me = PCPU_GET(cpuid);
11148aa3d7ffSJohn Baldwin 
11158aa3d7ffSJohn Baldwin 	/* Don't bother if we should be doing it ourself. */
1116a38f1f26SAttilio Rao 	if (CPU_ISSET(me, &idle_cpus_mask) &&
1117a38f1f26SAttilio Rao 	    (cpunum == NOCPU || me == cpunum))
111882a1dfc1SJulian Elischer 		return (0);
111982a1dfc1SJulian Elischer 
1120a38f1f26SAttilio Rao 	CPU_SETOF(me, &dontuse);
112171a19bdcSAttilio Rao 	CPU_OR(&dontuse, &stopped_cpus);
112271a19bdcSAttilio Rao 	CPU_OR(&dontuse, &hlt_cpus_mask);
112371a19bdcSAttilio Rao 	CPU_ZERO(&map2);
112482a1dfc1SJulian Elischer 	if (forward_wakeup_use_loop) {
1125d098f930SNathan Whitehorn 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
1126a38f1f26SAttilio Rao 			id = pc->pc_cpuid;
1127a38f1f26SAttilio Rao 			if (!CPU_ISSET(id, &dontuse) &&
112882a1dfc1SJulian Elischer 			    pc->pc_curthread == pc->pc_idlethread) {
1129a38f1f26SAttilio Rao 				CPU_SET(id, &map2);
113082a1dfc1SJulian Elischer 			}
113182a1dfc1SJulian Elischer 		}
113282a1dfc1SJulian Elischer 	}
113382a1dfc1SJulian Elischer 
113482a1dfc1SJulian Elischer 	if (forward_wakeup_use_mask) {
113571a19bdcSAttilio Rao 		map = idle_cpus_mask;
113671a19bdcSAttilio Rao 		CPU_NAND(&map, &dontuse);
113782a1dfc1SJulian Elischer 
11388aa3d7ffSJohn Baldwin 		/* If they are both on, compare and use loop if different. */
113982a1dfc1SJulian Elischer 		if (forward_wakeup_use_loop) {
114071a19bdcSAttilio Rao 			if (CPU_CMP(&map, &map2)) {
1141f0283a73SAttilio Rao 				printf("map != map2, loop method preferred\n");
1142f0283a73SAttilio Rao 				map = map2;
114382a1dfc1SJulian Elischer 			}
114482a1dfc1SJulian Elischer 		}
114582a1dfc1SJulian Elischer 	} else {
1146f0283a73SAttilio Rao 		map = map2;
114782a1dfc1SJulian Elischer 	}
11488aa3d7ffSJohn Baldwin 
11498aa3d7ffSJohn Baldwin 	/* If we only allow a specific CPU, then mask off all the others. */
115082a1dfc1SJulian Elischer 	if (cpunum != NOCPU) {
115182a1dfc1SJulian Elischer 		KASSERT((cpunum <= mp_maxcpus),("forward_wakeup: bad cpunum."));
115271a19bdcSAttilio Rao 		iscpuset = CPU_ISSET(cpunum, &map);
115371a19bdcSAttilio Rao 		if (iscpuset == 0)
115471a19bdcSAttilio Rao 			CPU_ZERO(&map);
115571a19bdcSAttilio Rao 		else
115671a19bdcSAttilio Rao 			CPU_SETOF(cpunum, &map);
115782a1dfc1SJulian Elischer 	}
115871a19bdcSAttilio Rao 	if (!CPU_EMPTY(&map)) {
115982a1dfc1SJulian Elischer 		forward_wakeups_delivered++;
1160d098f930SNathan Whitehorn 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
1161a38f1f26SAttilio Rao 			id = pc->pc_cpuid;
1162a38f1f26SAttilio Rao 			if (!CPU_ISSET(id, &map))
1163b722ad00SAlexander Motin 				continue;
1164b722ad00SAlexander Motin 			if (cpu_idle_wakeup(pc->pc_cpuid))
1165a38f1f26SAttilio Rao 				CPU_CLR(id, &map);
1166b722ad00SAlexander Motin 		}
116771a19bdcSAttilio Rao 		if (!CPU_EMPTY(&map))
116882a1dfc1SJulian Elischer 			ipi_selected(map, IPI_AST);
116982a1dfc1SJulian Elischer 		return (1);
117082a1dfc1SJulian Elischer 	}
117182a1dfc1SJulian Elischer 	if (cpunum == NOCPU)
117282a1dfc1SJulian Elischer 		printf("forward_wakeup: Idle processor not found\n");
117382a1dfc1SJulian Elischer 	return (0);
117482a1dfc1SJulian Elischer }
1175f3a0f873SStephan Uphoff 
1176f3a0f873SStephan Uphoff static void
1177f3a0f873SStephan Uphoff kick_other_cpu(int pri, int cpuid)
1178f3a0f873SStephan Uphoff {
11798aa3d7ffSJohn Baldwin 	struct pcpu *pcpu;
11808aa3d7ffSJohn Baldwin 	int cpri;
1181f3a0f873SStephan Uphoff 
11828aa3d7ffSJohn Baldwin 	pcpu = pcpu_find(cpuid);
1183a38f1f26SAttilio Rao 	if (CPU_ISSET(cpuid, &idle_cpus_mask)) {
1184f3a0f873SStephan Uphoff 		forward_wakeups_delivered++;
1185b722ad00SAlexander Motin 		if (!cpu_idle_wakeup(cpuid))
1186d9d8d144SJohn Baldwin 			ipi_cpu(cpuid, IPI_AST);
1187f3a0f873SStephan Uphoff 		return;
1188f3a0f873SStephan Uphoff 	}
1189f3a0f873SStephan Uphoff 
11908aa3d7ffSJohn Baldwin 	cpri = pcpu->pc_curthread->td_priority;
1191f3a0f873SStephan Uphoff 	if (pri >= cpri)
1192f3a0f873SStephan Uphoff 		return;
1193f3a0f873SStephan Uphoff 
1194f3a0f873SStephan Uphoff #if defined(IPI_PREEMPTION) && defined(PREEMPTION)
1195f3a0f873SStephan Uphoff #if !defined(FULL_PREEMPTION)
1196f3a0f873SStephan Uphoff 	if (pri <= PRI_MAX_ITHD)
1197f3a0f873SStephan Uphoff #endif /* ! FULL_PREEMPTION */
1198f3a0f873SStephan Uphoff 	{
1199d9d8d144SJohn Baldwin 		ipi_cpu(cpuid, IPI_PREEMPT);
1200f3a0f873SStephan Uphoff 		return;
1201f3a0f873SStephan Uphoff 	}
1202f3a0f873SStephan Uphoff #endif /* defined(IPI_PREEMPTION) && defined(PREEMPTION) */
1203f3a0f873SStephan Uphoff 
1204f3a0f873SStephan Uphoff 	pcpu->pc_curthread->td_flags |= TDF_NEEDRESCHED;
1205d9d8d144SJohn Baldwin 	ipi_cpu(cpuid, IPI_AST);
1206f3a0f873SStephan Uphoff 	return;
1207f3a0f873SStephan Uphoff }
1208f3a0f873SStephan Uphoff #endif /* SMP */
1209f3a0f873SStephan Uphoff 
1210f200843bSJohn Baldwin #ifdef SMP
1211f200843bSJohn Baldwin static int
1212f200843bSJohn Baldwin sched_pickcpu(struct thread *td)
1213f200843bSJohn Baldwin {
1214f200843bSJohn Baldwin 	int best, cpu;
1215f200843bSJohn Baldwin 
1216f200843bSJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
1217f200843bSJohn Baldwin 
1218c3ea3378SJohn Baldwin 	if (THREAD_CAN_SCHED(td, td->td_lastcpu))
1219c3ea3378SJohn Baldwin 		best = td->td_lastcpu;
1220c3ea3378SJohn Baldwin 	else
1221f200843bSJohn Baldwin 		best = NOCPU;
12223aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
1223f200843bSJohn Baldwin 		if (!THREAD_CAN_SCHED(td, cpu))
1224f200843bSJohn Baldwin 			continue;
1225f200843bSJohn Baldwin 
1226f200843bSJohn Baldwin 		if (best == NOCPU)
1227f200843bSJohn Baldwin 			best = cpu;
1228f200843bSJohn Baldwin 		else if (runq_length[cpu] < runq_length[best])
1229f200843bSJohn Baldwin 			best = cpu;
1230f200843bSJohn Baldwin 	}
1231f200843bSJohn Baldwin 	KASSERT(best != NOCPU, ("no valid CPUs"));
1232f200843bSJohn Baldwin 
1233f200843bSJohn Baldwin 	return (best);
1234f200843bSJohn Baldwin }
1235f200843bSJohn Baldwin #endif
1236f200843bSJohn Baldwin 
1237b43179fbSJeff Roberson void
12382630e4c9SJulian Elischer sched_add(struct thread *td, int flags)
12396804a3abSJulian Elischer #ifdef SMP
1240f3a0f873SStephan Uphoff {
1241a38f1f26SAttilio Rao 	cpuset_t tidlemsk;
1242ad1e7d28SJulian Elischer 	struct td_sched *ts;
1243a38f1f26SAttilio Rao 	u_int cpu, cpuid;
12446804a3abSJulian Elischer 	int forwarded = 0;
1245f3a0f873SStephan Uphoff 	int single_cpu = 0;
12467cf90fb3SJeff Roberson 
1247ad1e7d28SJulian Elischer 	ts = td->td_sched;
12487b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1249f0393f06SJeff Roberson 	KASSERT((td->td_inhibitors == 0),
1250f0393f06SJeff Roberson 	    ("sched_add: trying to run inhibited thread"));
1251f0393f06SJeff Roberson 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1252f0393f06SJeff Roberson 	    ("sched_add: bad thread state"));
1253b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
1254b61ce5b0SJeff Roberson 	    ("sched_add: thread swapped out"));
12558f51ad55SJeff Roberson 
12568f51ad55SJeff Roberson 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add",
12578f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
12588f51ad55SJeff Roberson 	    sched_tdname(curthread));
12598f51ad55SJeff Roberson 	KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup",
12608f51ad55SJeff Roberson 	    KTR_ATTR_LINKED, sched_tdname(td));
1261b3e9e682SRyan Stone 	SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL,
1262b3e9e682SRyan Stone 	    flags & SRQ_PREEMPTED);
12638f51ad55SJeff Roberson 
12648aa3d7ffSJohn Baldwin 
12657b20fb19SJeff Roberson 	/*
12667b20fb19SJeff Roberson 	 * Now that the thread is moving to the run-queue, set the lock
12677b20fb19SJeff Roberson 	 * to the scheduler's lock.
12687b20fb19SJeff Roberson 	 */
12697b20fb19SJeff Roberson 	if (td->td_lock != &sched_lock) {
12707b20fb19SJeff Roberson 		mtx_lock_spin(&sched_lock);
12717b20fb19SJeff Roberson 		thread_lock_set(td, &sched_lock);
12727b20fb19SJeff Roberson 	}
1273f0393f06SJeff Roberson 	TD_SET_RUNQ(td);
1274f3a0f873SStephan Uphoff 
127560dd73b7SRyan Stone 	/*
127660dd73b7SRyan Stone 	 * If SMP is started and the thread is pinned or otherwise limited to
127760dd73b7SRyan Stone 	 * a specific set of CPUs, queue the thread to a per-CPU run queue.
127860dd73b7SRyan Stone 	 * Otherwise, queue the thread to the global run queue.
127960dd73b7SRyan Stone 	 *
128060dd73b7SRyan Stone 	 * If SMP has not yet been started we must use the global run queue
128160dd73b7SRyan Stone 	 * as per-CPU state may not be initialized yet and we may crash if we
128260dd73b7SRyan Stone 	 * try to access the per-CPU run queues.
128360dd73b7SRyan Stone 	 */
128460dd73b7SRyan Stone 	if (smp_started && (td->td_pinned != 0 || td->td_flags & TDF_BOUND ||
128560dd73b7SRyan Stone 	    ts->ts_flags & TSF_AFFINITY)) {
128660dd73b7SRyan Stone 		if (td->td_pinned != 0)
1287f3a0f873SStephan Uphoff 			cpu = td->td_lastcpu;
128860dd73b7SRyan Stone 		else if (td->td_flags & TDF_BOUND) {
12898aa3d7ffSJohn Baldwin 			/* Find CPU from bound runq. */
12908aa3d7ffSJohn Baldwin 			KASSERT(SKE_RUNQ_PCPU(ts),
12918aa3d7ffSJohn Baldwin 			    ("sched_add: bound td_sched not on cpu runq"));
1292ad1e7d28SJulian Elischer 			cpu = ts->ts_runq - &runq_pcpu[0];
129360dd73b7SRyan Stone 		} else
1294f200843bSJohn Baldwin 			/* Find a valid CPU for our cpuset */
1295f200843bSJohn Baldwin 			cpu = sched_pickcpu(td);
1296f200843bSJohn Baldwin 		ts->ts_runq = &runq_pcpu[cpu];
1297f200843bSJohn Baldwin 		single_cpu = 1;
1298f200843bSJohn Baldwin 		CTR3(KTR_RUNQ,
1299f200843bSJohn Baldwin 		    "sched_add: Put td_sched:%p(td:%p) on cpu%d runq", ts, td,
1300f200843bSJohn Baldwin 		    cpu);
1301f3a0f873SStephan Uphoff 	} else {
13026804a3abSJulian Elischer 		CTR2(KTR_RUNQ,
13038aa3d7ffSJohn Baldwin 		    "sched_add: adding td_sched:%p (td:%p) to gbl runq", ts,
13048aa3d7ffSJohn Baldwin 		    td);
13056804a3abSJulian Elischer 		cpu = NOCPU;
1306ad1e7d28SJulian Elischer 		ts->ts_runq = &runq;
1307e17c57b1SJeff Roberson 	}
1308f3a0f873SStephan Uphoff 
1309a38f1f26SAttilio Rao 	cpuid = PCPU_GET(cpuid);
1310a38f1f26SAttilio Rao 	if (single_cpu && cpu != cpuid) {
1311f3a0f873SStephan Uphoff 	        kick_other_cpu(td->td_priority, cpu);
1312f3a0f873SStephan Uphoff 	} else {
1313f3a0f873SStephan Uphoff 		if (!single_cpu) {
1314a38f1f26SAttilio Rao 			tidlemsk = idle_cpus_mask;
1315a38f1f26SAttilio Rao 			CPU_NAND(&tidlemsk, &hlt_cpus_mask);
1316a38f1f26SAttilio Rao 			CPU_CLR(cpuid, &tidlemsk);
1317f3a0f873SStephan Uphoff 
1318a38f1f26SAttilio Rao 			if (!CPU_ISSET(cpuid, &idle_cpus_mask) &&
1319a38f1f26SAttilio Rao 			    ((flags & SRQ_INTR) == 0) &&
132071a19bdcSAttilio Rao 			    !CPU_EMPTY(&tidlemsk))
1321f3a0f873SStephan Uphoff 				forwarded = forward_wakeup(cpu);
1322f3a0f873SStephan Uphoff 		}
1323f3a0f873SStephan Uphoff 
1324f3a0f873SStephan Uphoff 		if (!forwarded) {
1325a3f2d842SStephan Uphoff 			if ((flags & SRQ_YIELDING) == 0 && maybe_preempt(td))
1326f3a0f873SStephan Uphoff 				return;
1327f3a0f873SStephan Uphoff 			else
1328f3a0f873SStephan Uphoff 				maybe_resched(td);
1329f3a0f873SStephan Uphoff 		}
1330f3a0f873SStephan Uphoff 	}
1331f3a0f873SStephan Uphoff 
13321b9d701fSAttilio Rao 	if ((td->td_flags & TDF_NOLOAD) == 0)
1333f3a0f873SStephan Uphoff 		sched_load_add();
13349727e637SJeff Roberson 	runq_add(ts->ts_runq, td, flags);
1335f200843bSJohn Baldwin 	if (cpu != NOCPU)
1336f200843bSJohn Baldwin 		runq_length[cpu]++;
1337f3a0f873SStephan Uphoff }
1338f3a0f873SStephan Uphoff #else /* SMP */
1339f3a0f873SStephan Uphoff {
1340ad1e7d28SJulian Elischer 	struct td_sched *ts;
1341f200843bSJohn Baldwin 
1342ad1e7d28SJulian Elischer 	ts = td->td_sched;
13437b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1344f0393f06SJeff Roberson 	KASSERT((td->td_inhibitors == 0),
1345f0393f06SJeff Roberson 	    ("sched_add: trying to run inhibited thread"));
1346f0393f06SJeff Roberson 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1347f0393f06SJeff Roberson 	    ("sched_add: bad thread state"));
1348b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
1349b61ce5b0SJeff Roberson 	    ("sched_add: thread swapped out"));
13508f51ad55SJeff Roberson 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add",
13518f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
13528f51ad55SJeff Roberson 	    sched_tdname(curthread));
13538f51ad55SJeff Roberson 	KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup",
13548f51ad55SJeff Roberson 	    KTR_ATTR_LINKED, sched_tdname(td));
13552aaae99dSSergey Kandaurov 	SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL,
1356b3e9e682SRyan Stone 	    flags & SRQ_PREEMPTED);
13578aa3d7ffSJohn Baldwin 
13587b20fb19SJeff Roberson 	/*
13597b20fb19SJeff Roberson 	 * Now that the thread is moving to the run-queue, set the lock
13607b20fb19SJeff Roberson 	 * to the scheduler's lock.
13617b20fb19SJeff Roberson 	 */
13627b20fb19SJeff Roberson 	if (td->td_lock != &sched_lock) {
13637b20fb19SJeff Roberson 		mtx_lock_spin(&sched_lock);
13647b20fb19SJeff Roberson 		thread_lock_set(td, &sched_lock);
13657b20fb19SJeff Roberson 	}
1366f0393f06SJeff Roberson 	TD_SET_RUNQ(td);
1367ad1e7d28SJulian Elischer 	CTR2(KTR_RUNQ, "sched_add: adding td_sched:%p (td:%p) to runq", ts, td);
1368ad1e7d28SJulian Elischer 	ts->ts_runq = &runq;
13696804a3abSJulian Elischer 
13706804a3abSJulian Elischer 	/*
13718aa3d7ffSJohn Baldwin 	 * If we are yielding (on the way out anyhow) or the thread
13728aa3d7ffSJohn Baldwin 	 * being saved is US, then don't try be smart about preemption
13738aa3d7ffSJohn Baldwin 	 * or kicking off another CPU as it won't help and may hinder.
13748aa3d7ffSJohn Baldwin 	 * In the YIEDLING case, we are about to run whoever is being
13758aa3d7ffSJohn Baldwin 	 * put in the queue anyhow, and in the OURSELF case, we are
13768aa3d7ffSJohn Baldwin 	 * puting ourself on the run queue which also only happens
13778aa3d7ffSJohn Baldwin 	 * when we are about to yield.
13786804a3abSJulian Elischer 	 */
13796804a3abSJulian Elischer 	if ((flags & SRQ_YIELDING) == 0) {
13806804a3abSJulian Elischer 		if (maybe_preempt(td))
13816804a3abSJulian Elischer 			return;
13826804a3abSJulian Elischer 	}
13831b9d701fSAttilio Rao 	if ((td->td_flags & TDF_NOLOAD) == 0)
1384907bdbc2SJeff Roberson 		sched_load_add();
13859727e637SJeff Roberson 	runq_add(ts->ts_runq, td, flags);
13866942d433SJohn Baldwin 	maybe_resched(td);
1387b43179fbSJeff Roberson }
1388f3a0f873SStephan Uphoff #endif /* SMP */
1389f3a0f873SStephan Uphoff 
1390b43179fbSJeff Roberson void
13917cf90fb3SJeff Roberson sched_rem(struct thread *td)
1392b43179fbSJeff Roberson {
1393ad1e7d28SJulian Elischer 	struct td_sched *ts;
13947cf90fb3SJeff Roberson 
1395ad1e7d28SJulian Elischer 	ts = td->td_sched;
1396b61ce5b0SJeff Roberson 	KASSERT(td->td_flags & TDF_INMEM,
1397b61ce5b0SJeff Roberson 	    ("sched_rem: thread swapped out"));
1398f0393f06SJeff Roberson 	KASSERT(TD_ON_RUNQ(td),
1399ad1e7d28SJulian Elischer 	    ("sched_rem: thread not on run queue"));
1400b43179fbSJeff Roberson 	mtx_assert(&sched_lock, MA_OWNED);
14018f51ad55SJeff Roberson 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq rem",
14028f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
14038f51ad55SJeff Roberson 	    sched_tdname(curthread));
1404b3e9e682SRyan Stone 	SDT_PROBE3(sched, , , dequeue, td, td->td_proc, NULL);
1405b43179fbSJeff Roberson 
14061b9d701fSAttilio Rao 	if ((td->td_flags & TDF_NOLOAD) == 0)
1407907bdbc2SJeff Roberson 		sched_load_rem();
1408f200843bSJohn Baldwin #ifdef SMP
1409f200843bSJohn Baldwin 	if (ts->ts_runq != &runq)
1410f200843bSJohn Baldwin 		runq_length[ts->ts_runq - runq_pcpu]--;
1411f200843bSJohn Baldwin #endif
14129727e637SJeff Roberson 	runq_remove(ts->ts_runq, td);
1413f0393f06SJeff Roberson 	TD_SET_CAN_RUN(td);
1414b43179fbSJeff Roberson }
1415b43179fbSJeff Roberson 
141614f0e2e9SJulian Elischer /*
14178aa3d7ffSJohn Baldwin  * Select threads to run.  Note that running threads still consume a
14188aa3d7ffSJohn Baldwin  * slot.
141914f0e2e9SJulian Elischer  */
1420f0393f06SJeff Roberson struct thread *
1421b43179fbSJeff Roberson sched_choose(void)
1422b43179fbSJeff Roberson {
14239727e637SJeff Roberson 	struct thread *td;
1424e17c57b1SJeff Roberson 	struct runq *rq;
1425b43179fbSJeff Roberson 
14267b20fb19SJeff Roberson 	mtx_assert(&sched_lock,  MA_OWNED);
1427e17c57b1SJeff Roberson #ifdef SMP
14289727e637SJeff Roberson 	struct thread *tdcpu;
1429e17c57b1SJeff Roberson 
1430e17c57b1SJeff Roberson 	rq = &runq;
14319727e637SJeff Roberson 	td = runq_choose_fuzz(&runq, runq_fuzz);
14329727e637SJeff Roberson 	tdcpu = runq_choose(&runq_pcpu[PCPU_GET(cpuid)]);
1433e17c57b1SJeff Roberson 
14349727e637SJeff Roberson 	if (td == NULL ||
14359727e637SJeff Roberson 	    (tdcpu != NULL &&
14369727e637SJeff Roberson 	     tdcpu->td_priority < td->td_priority)) {
14379727e637SJeff Roberson 		CTR2(KTR_RUNQ, "choosing td %p from pcpu runq %d", tdcpu,
1438e17c57b1SJeff Roberson 		     PCPU_GET(cpuid));
14399727e637SJeff Roberson 		td = tdcpu;
1440e17c57b1SJeff Roberson 		rq = &runq_pcpu[PCPU_GET(cpuid)];
1441e17c57b1SJeff Roberson 	} else {
14429727e637SJeff Roberson 		CTR1(KTR_RUNQ, "choosing td_sched %p from main runq", td);
1443e17c57b1SJeff Roberson 	}
1444e17c57b1SJeff Roberson 
1445e17c57b1SJeff Roberson #else
1446e17c57b1SJeff Roberson 	rq = &runq;
14479727e637SJeff Roberson 	td = runq_choose(&runq);
1448e17c57b1SJeff Roberson #endif
1449b43179fbSJeff Roberson 
14509727e637SJeff Roberson 	if (td) {
1451f200843bSJohn Baldwin #ifdef SMP
1452f200843bSJohn Baldwin 		if (td == tdcpu)
1453f200843bSJohn Baldwin 			runq_length[PCPU_GET(cpuid)]--;
1454f200843bSJohn Baldwin #endif
14559727e637SJeff Roberson 		runq_remove(rq, td);
14569727e637SJeff Roberson 		td->td_flags |= TDF_DIDRUN;
1457b43179fbSJeff Roberson 
14589727e637SJeff Roberson 		KASSERT(td->td_flags & TDF_INMEM,
1459b61ce5b0SJeff Roberson 		    ("sched_choose: thread swapped out"));
14609727e637SJeff Roberson 		return (td);
1461b43179fbSJeff Roberson 	}
1462f0393f06SJeff Roberson 	return (PCPU_GET(idlethread));
1463b43179fbSJeff Roberson }
1464b43179fbSJeff Roberson 
1465b43179fbSJeff Roberson void
14661e24c28fSJeff Roberson sched_preempt(struct thread *td)
14671e24c28fSJeff Roberson {
1468b3e9e682SRyan Stone 
1469b3e9e682SRyan Stone 	SDT_PROBE2(sched, , , surrender, td, td->td_proc);
14701e24c28fSJeff Roberson 	thread_lock(td);
14711e24c28fSJeff Roberson 	if (td->td_critnest > 1)
14721e24c28fSJeff Roberson 		td->td_owepreempt = 1;
14731e24c28fSJeff Roberson 	else
14748df78c41SJeff Roberson 		mi_switch(SW_INVOL | SW_PREEMPT | SWT_PREEMPT, NULL);
14751e24c28fSJeff Roberson 	thread_unlock(td);
14761e24c28fSJeff Roberson }
14771e24c28fSJeff Roberson 
14781e24c28fSJeff Roberson void
1479b43179fbSJeff Roberson sched_userret(struct thread *td)
1480b43179fbSJeff Roberson {
1481b43179fbSJeff Roberson 	/*
1482b43179fbSJeff Roberson 	 * XXX we cheat slightly on the locking here to avoid locking in
1483b43179fbSJeff Roberson 	 * the usual case.  Setting td_priority here is essentially an
1484b43179fbSJeff Roberson 	 * incomplete workaround for not setting it properly elsewhere.
1485b43179fbSJeff Roberson 	 * Now that some interrupt handlers are threads, not setting it
1486b43179fbSJeff Roberson 	 * properly elsewhere can clobber it in the window between setting
1487b43179fbSJeff Roberson 	 * it here and returning to user mode, so don't waste time setting
1488b43179fbSJeff Roberson 	 * it perfectly here.
1489b43179fbSJeff Roberson 	 */
1490f5c157d9SJohn Baldwin 	KASSERT((td->td_flags & TDF_BORROWING) == 0,
1491f5c157d9SJohn Baldwin 	    ("thread with borrowed priority returning to userland"));
14928460a577SJohn Birrell 	if (td->td_priority != td->td_user_pri) {
14937b20fb19SJeff Roberson 		thread_lock(td);
14948460a577SJohn Birrell 		td->td_priority = td->td_user_pri;
14958460a577SJohn Birrell 		td->td_base_pri = td->td_user_pri;
14967b20fb19SJeff Roberson 		thread_unlock(td);
14978460a577SJohn Birrell 	}
1498b43179fbSJeff Roberson }
1499de028f5aSJeff Roberson 
1500e17c57b1SJeff Roberson void
1501e17c57b1SJeff Roberson sched_bind(struct thread *td, int cpu)
1502e17c57b1SJeff Roberson {
1503ad1e7d28SJulian Elischer 	struct td_sched *ts;
1504e17c57b1SJeff Roberson 
15051d7830edSJohn Baldwin 	THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
15061d7830edSJohn Baldwin 	KASSERT(td == curthread, ("sched_bind: can only bind curthread"));
1507e17c57b1SJeff Roberson 
1508ad1e7d28SJulian Elischer 	ts = td->td_sched;
1509e17c57b1SJeff Roberson 
15109727e637SJeff Roberson 	td->td_flags |= TDF_BOUND;
1511e17c57b1SJeff Roberson #ifdef SMP
1512ad1e7d28SJulian Elischer 	ts->ts_runq = &runq_pcpu[cpu];
1513e17c57b1SJeff Roberson 	if (PCPU_GET(cpuid) == cpu)
1514e17c57b1SJeff Roberson 		return;
1515e17c57b1SJeff Roberson 
1516bf0acc27SJohn Baldwin 	mi_switch(SW_VOL, NULL);
1517e17c57b1SJeff Roberson #endif
1518e17c57b1SJeff Roberson }
1519e17c57b1SJeff Roberson 
1520e17c57b1SJeff Roberson void
1521e17c57b1SJeff Roberson sched_unbind(struct thread* td)
1522e17c57b1SJeff Roberson {
15237b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
15241d7830edSJohn Baldwin 	KASSERT(td == curthread, ("sched_unbind: can only bind curthread"));
15259727e637SJeff Roberson 	td->td_flags &= ~TDF_BOUND;
1526e17c57b1SJeff Roberson }
1527e17c57b1SJeff Roberson 
1528de028f5aSJeff Roberson int
1529ebccf1e3SJoseph Koshy sched_is_bound(struct thread *td)
1530ebccf1e3SJoseph Koshy {
15317b20fb19SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
15329727e637SJeff Roberson 	return (td->td_flags & TDF_BOUND);
1533ebccf1e3SJoseph Koshy }
1534ebccf1e3SJoseph Koshy 
153536ec198bSDavid Xu void
153636ec198bSDavid Xu sched_relinquish(struct thread *td)
153736ec198bSDavid Xu {
15387b20fb19SJeff Roberson 	thread_lock(td);
15398df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
15407b20fb19SJeff Roberson 	thread_unlock(td);
154136ec198bSDavid Xu }
154236ec198bSDavid Xu 
1543ebccf1e3SJoseph Koshy int
1544ca59f152SJeff Roberson sched_load(void)
1545ca59f152SJeff Roberson {
1546ca59f152SJeff Roberson 	return (sched_tdcnt);
1547ca59f152SJeff Roberson }
1548ca59f152SJeff Roberson 
1549de028f5aSJeff Roberson int
1550de028f5aSJeff Roberson sched_sizeof_proc(void)
1551de028f5aSJeff Roberson {
1552de028f5aSJeff Roberson 	return (sizeof(struct proc));
1553de028f5aSJeff Roberson }
155436ec198bSDavid Xu 
1555de028f5aSJeff Roberson int
1556de028f5aSJeff Roberson sched_sizeof_thread(void)
1557de028f5aSJeff Roberson {
1558ad1e7d28SJulian Elischer 	return (sizeof(struct thread) + sizeof(struct td_sched));
1559de028f5aSJeff Roberson }
156079acfc49SJeff Roberson 
156179acfc49SJeff Roberson fixpt_t
15627cf90fb3SJeff Roberson sched_pctcpu(struct thread *td)
156379acfc49SJeff Roberson {
1564ad1e7d28SJulian Elischer 	struct td_sched *ts;
156555f2099aSJeff Roberson 
15663da35a0aSJohn Baldwin 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1567ad1e7d28SJulian Elischer 	ts = td->td_sched;
1568ad1e7d28SJulian Elischer 	return (ts->ts_pctcpu);
156979acfc49SJeff Roberson }
1570b41f1452SDavid Xu 
1571b41f1452SDavid Xu void
1572a157e425SAlexander Motin sched_tick(int cnt)
1573b41f1452SDavid Xu {
1574b41f1452SDavid Xu }
1575f0393f06SJeff Roberson 
1576f0393f06SJeff Roberson /*
1577f0393f06SJeff Roberson  * The actual idle process.
1578f0393f06SJeff Roberson  */
1579f0393f06SJeff Roberson void
1580f0393f06SJeff Roberson sched_idletd(void *dummy)
1581f0393f06SJeff Roberson {
1582b722ad00SAlexander Motin 	struct pcpuidlestat *stat;
1583f0393f06SJeff Roberson 
1584b722ad00SAlexander Motin 	stat = DPCPU_PTR(idlestat);
1585f0393f06SJeff Roberson 	for (;;) {
1586f0393f06SJeff Roberson 		mtx_assert(&Giant, MA_NOTOWNED);
1587f0393f06SJeff Roberson 
1588b722ad00SAlexander Motin 		while (sched_runnable() == 0) {
1589b722ad00SAlexander Motin 			cpu_idle(stat->idlecalls + stat->oldidlecalls > 64);
1590b722ad00SAlexander Motin 			stat->idlecalls++;
1591b722ad00SAlexander Motin 		}
1592f0393f06SJeff Roberson 
1593f0393f06SJeff Roberson 		mtx_lock_spin(&sched_lock);
15948df78c41SJeff Roberson 		mi_switch(SW_VOL | SWT_IDLE, NULL);
1595f0393f06SJeff Roberson 		mtx_unlock_spin(&sched_lock);
1596f0393f06SJeff Roberson 	}
1597f0393f06SJeff Roberson }
1598f0393f06SJeff Roberson 
15997b20fb19SJeff Roberson /*
16007b20fb19SJeff Roberson  * A CPU is entering for the first time or a thread is exiting.
16017b20fb19SJeff Roberson  */
16027b20fb19SJeff Roberson void
16037b20fb19SJeff Roberson sched_throw(struct thread *td)
16047b20fb19SJeff Roberson {
16057b20fb19SJeff Roberson 	/*
16067b20fb19SJeff Roberson 	 * Correct spinlock nesting.  The idle thread context that we are
16077b20fb19SJeff Roberson 	 * borrowing was created so that it would start out with a single
16087b20fb19SJeff Roberson 	 * spin lock (sched_lock) held in fork_trampoline().  Since we've
16097b20fb19SJeff Roberson 	 * explicitly acquired locks in this function, the nesting count
16107b20fb19SJeff Roberson 	 * is now 2 rather than 1.  Since we are nested, calling
16117b20fb19SJeff Roberson 	 * spinlock_exit() will simply adjust the counts without allowing
16127b20fb19SJeff Roberson 	 * spin lock using code to interrupt us.
16137b20fb19SJeff Roberson 	 */
16147b20fb19SJeff Roberson 	if (td == NULL) {
16157b20fb19SJeff Roberson 		mtx_lock_spin(&sched_lock);
16167b20fb19SJeff Roberson 		spinlock_exit();
16177e3a96eaSJohn Baldwin 		PCPU_SET(switchtime, cpu_ticks());
16187e3a96eaSJohn Baldwin 		PCPU_SET(switchticks, ticks);
16197b20fb19SJeff Roberson 	} else {
1620eea4f254SJeff Roberson 		lock_profile_release_lock(&sched_lock.lock_object);
16217b20fb19SJeff Roberson 		MPASS(td->td_lock == &sched_lock);
16227b20fb19SJeff Roberson 	}
16237b20fb19SJeff Roberson 	mtx_assert(&sched_lock, MA_OWNED);
16247b20fb19SJeff Roberson 	KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
16257b20fb19SJeff Roberson 	cpu_throw(td, choosethread());	/* doesn't return */
16267b20fb19SJeff Roberson }
16277b20fb19SJeff Roberson 
16287b20fb19SJeff Roberson void
1629fe54587fSJeff Roberson sched_fork_exit(struct thread *td)
16307b20fb19SJeff Roberson {
16317b20fb19SJeff Roberson 
16327b20fb19SJeff Roberson 	/*
16337b20fb19SJeff Roberson 	 * Finish setting up thread glue so that it begins execution in a
16347b20fb19SJeff Roberson 	 * non-nested critical section with sched_lock held but not recursed.
16357b20fb19SJeff Roberson 	 */
1636fe54587fSJeff Roberson 	td->td_oncpu = PCPU_GET(cpuid);
1637fe54587fSJeff Roberson 	sched_lock.mtx_lock = (uintptr_t)td;
1638eea4f254SJeff Roberson 	lock_profile_obtain_lock_success(&sched_lock.lock_object,
1639eea4f254SJeff Roberson 	    0, 0, __FILE__, __LINE__);
1640fe54587fSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED);
16417b20fb19SJeff Roberson }
16427b20fb19SJeff Roberson 
16438f51ad55SJeff Roberson char *
16448f51ad55SJeff Roberson sched_tdname(struct thread *td)
16458f51ad55SJeff Roberson {
16468f51ad55SJeff Roberson #ifdef KTR
16478f51ad55SJeff Roberson 	struct td_sched *ts;
16488f51ad55SJeff Roberson 
16498f51ad55SJeff Roberson 	ts = td->td_sched;
16508f51ad55SJeff Roberson 	if (ts->ts_name[0] == '\0')
16518f51ad55SJeff Roberson 		snprintf(ts->ts_name, sizeof(ts->ts_name),
16528f51ad55SJeff Roberson 		    "%s tid %d", td->td_name, td->td_tid);
16538f51ad55SJeff Roberson 	return (ts->ts_name);
16548f51ad55SJeff Roberson #else
16558f51ad55SJeff Roberson 	return (td->td_name);
16568f51ad55SJeff Roberson #endif
16578f51ad55SJeff Roberson }
16588f51ad55SJeff Roberson 
165944ad5475SJohn Baldwin #ifdef KTR
166044ad5475SJohn Baldwin void
166144ad5475SJohn Baldwin sched_clear_tdname(struct thread *td)
166244ad5475SJohn Baldwin {
166344ad5475SJohn Baldwin 	struct td_sched *ts;
166444ad5475SJohn Baldwin 
166544ad5475SJohn Baldwin 	ts = td->td_sched;
166644ad5475SJohn Baldwin 	ts->ts_name[0] = '\0';
166744ad5475SJohn Baldwin }
166844ad5475SJohn Baldwin #endif
166944ad5475SJohn Baldwin 
1670885d51a3SJeff Roberson void
1671885d51a3SJeff Roberson sched_affinity(struct thread *td)
1672885d51a3SJeff Roberson {
1673f200843bSJohn Baldwin #ifdef SMP
1674f200843bSJohn Baldwin 	struct td_sched *ts;
1675f200843bSJohn Baldwin 	int cpu;
1676f200843bSJohn Baldwin 
1677f200843bSJohn Baldwin 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1678f200843bSJohn Baldwin 
1679f200843bSJohn Baldwin 	/*
1680f200843bSJohn Baldwin 	 * Set the TSF_AFFINITY flag if there is at least one CPU this
1681f200843bSJohn Baldwin 	 * thread can't run on.
1682f200843bSJohn Baldwin 	 */
1683f200843bSJohn Baldwin 	ts = td->td_sched;
1684f200843bSJohn Baldwin 	ts->ts_flags &= ~TSF_AFFINITY;
16853aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
1686f200843bSJohn Baldwin 		if (!THREAD_CAN_SCHED(td, cpu)) {
1687f200843bSJohn Baldwin 			ts->ts_flags |= TSF_AFFINITY;
1688f200843bSJohn Baldwin 			break;
1689f200843bSJohn Baldwin 		}
1690f200843bSJohn Baldwin 	}
1691f200843bSJohn Baldwin 
1692f200843bSJohn Baldwin 	/*
1693f200843bSJohn Baldwin 	 * If this thread can run on all CPUs, nothing else to do.
1694f200843bSJohn Baldwin 	 */
1695f200843bSJohn Baldwin 	if (!(ts->ts_flags & TSF_AFFINITY))
1696f200843bSJohn Baldwin 		return;
1697f200843bSJohn Baldwin 
1698f200843bSJohn Baldwin 	/* Pinned threads and bound threads should be left alone. */
1699f200843bSJohn Baldwin 	if (td->td_pinned != 0 || td->td_flags & TDF_BOUND)
1700f200843bSJohn Baldwin 		return;
1701f200843bSJohn Baldwin 
1702f200843bSJohn Baldwin 	switch (td->td_state) {
1703f200843bSJohn Baldwin 	case TDS_RUNQ:
1704f200843bSJohn Baldwin 		/*
1705f200843bSJohn Baldwin 		 * If we are on a per-CPU runqueue that is in the set,
1706f200843bSJohn Baldwin 		 * then nothing needs to be done.
1707f200843bSJohn Baldwin 		 */
1708f200843bSJohn Baldwin 		if (ts->ts_runq != &runq &&
1709f200843bSJohn Baldwin 		    THREAD_CAN_SCHED(td, ts->ts_runq - runq_pcpu))
1710f200843bSJohn Baldwin 			return;
1711f200843bSJohn Baldwin 
1712f200843bSJohn Baldwin 		/* Put this thread on a valid per-CPU runqueue. */
1713f200843bSJohn Baldwin 		sched_rem(td);
1714f200843bSJohn Baldwin 		sched_add(td, SRQ_BORING);
1715f200843bSJohn Baldwin 		break;
1716f200843bSJohn Baldwin 	case TDS_RUNNING:
1717f200843bSJohn Baldwin 		/*
1718f200843bSJohn Baldwin 		 * See if our current CPU is in the set.  If not, force a
1719f200843bSJohn Baldwin 		 * context switch.
1720f200843bSJohn Baldwin 		 */
1721f200843bSJohn Baldwin 		if (THREAD_CAN_SCHED(td, td->td_oncpu))
1722f200843bSJohn Baldwin 			return;
1723f200843bSJohn Baldwin 
1724f200843bSJohn Baldwin 		td->td_flags |= TDF_NEEDRESCHED;
1725f200843bSJohn Baldwin 		if (td != curthread)
1726d9d8d144SJohn Baldwin 			ipi_cpu(cpu, IPI_AST);
1727f200843bSJohn Baldwin 		break;
1728f200843bSJohn Baldwin 	default:
1729f200843bSJohn Baldwin 		break;
1730f200843bSJohn Baldwin 	}
1731f200843bSJohn Baldwin #endif
1732885d51a3SJeff Roberson }
1733