xref: /freebsd/sys/kern/sched_4bsd.c (revision 145992504973bd16cf3518af9ba5ce185fefa82a)
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_hwpmc_hooks.h"
39 #include "opt_sched.h"
40 #include "opt_kdtrace.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/cpuset.h>
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/lock.h>
48 #include <sys/kthread.h>
49 #include <sys/mutex.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/sched.h>
53 #include <sys/sdt.h>
54 #include <sys/smp.h>
55 #include <sys/sysctl.h>
56 #include <sys/sx.h>
57 #include <sys/turnstile.h>
58 #include <sys/umtx.h>
59 #include <machine/pcb.h>
60 #include <machine/smp.h>
61 
62 #ifdef HWPMC_HOOKS
63 #include <sys/pmckern.h>
64 #endif
65 
66 #ifdef KDTRACE_HOOKS
67 #include <sys/dtrace_bsd.h>
68 int				dtrace_vtime_active;
69 dtrace_vtime_switch_func_t	dtrace_vtime_switch_func;
70 #endif
71 
72 /*
73  * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
74  * the range 100-256 Hz (approximately).
75  */
76 #define	ESTCPULIM(e) \
77     min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
78     RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
79 #ifdef SMP
80 #define	INVERSE_ESTCPU_WEIGHT	(8 * smp_cpus)
81 #else
82 #define	INVERSE_ESTCPU_WEIGHT	8	/* 1 / (priorities per estcpu level). */
83 #endif
84 #define	NICE_WEIGHT		1	/* Priorities per nice level. */
85 
86 #define	TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX)))
87 
88 /*
89  * The schedulable entity that runs a context.
90  * This is  an extension to the thread structure and is tailored to
91  * the requirements of this scheduler
92  */
93 struct td_sched {
94 	fixpt_t		ts_pctcpu;	/* (j) %cpu during p_swtime. */
95 	int		ts_cpticks;	/* (j) Ticks of cpu time. */
96 	int		ts_slptime;	/* (j) Seconds !RUNNING. */
97 	int		ts_slice;	/* Remaining part of time slice. */
98 	int		ts_flags;
99 	struct runq	*ts_runq;	/* runq the thread is currently on */
100 #ifdef KTR
101 	char		ts_name[TS_NAME_LEN];
102 #endif
103 };
104 
105 /* flags kept in td_flags */
106 #define TDF_DIDRUN	TDF_SCHED0	/* thread actually ran. */
107 #define TDF_BOUND	TDF_SCHED1	/* Bound to one CPU. */
108 #define	TDF_SLICEEND	TDF_SCHED2	/* Thread time slice is over. */
109 
110 /* flags kept in ts_flags */
111 #define	TSF_AFFINITY	0x0001		/* Has a non-"full" CPU set. */
112 
113 #define SKE_RUNQ_PCPU(ts)						\
114     ((ts)->ts_runq != 0 && (ts)->ts_runq != &runq)
115 
116 #define	THREAD_CAN_SCHED(td, cpu)	\
117     CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
118 
119 static struct td_sched td_sched0;
120 struct mtx sched_lock;
121 
122 static int	realstathz = 127; /* stathz is sometimes 0 and run off of hz. */
123 static int	sched_tdcnt;	/* Total runnable threads in the system. */
124 static int	sched_slice = 12; /* Thread run time before rescheduling. */
125 
126 static void	setup_runqs(void);
127 static void	schedcpu(void);
128 static void	schedcpu_thread(void);
129 static void	sched_priority(struct thread *td, u_char prio);
130 static void	sched_setup(void *dummy);
131 static void	maybe_resched(struct thread *td);
132 static void	updatepri(struct thread *td);
133 static void	resetpriority(struct thread *td);
134 static void	resetpriority_thread(struct thread *td);
135 #ifdef SMP
136 static int	sched_pickcpu(struct thread *td);
137 static int	forward_wakeup(int cpunum);
138 static void	kick_other_cpu(int pri, int cpuid);
139 #endif
140 
141 static struct kproc_desc sched_kp = {
142         "schedcpu",
143         schedcpu_thread,
144         NULL
145 };
146 SYSINIT(schedcpu, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, kproc_start,
147     &sched_kp);
148 SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
149 
150 static void sched_initticks(void *dummy);
151 SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks,
152     NULL);
153 
154 /*
155  * Global run queue.
156  */
157 static struct runq runq;
158 
159 #ifdef SMP
160 /*
161  * Per-CPU run queues
162  */
163 static struct runq runq_pcpu[MAXCPU];
164 long runq_length[MAXCPU];
165 
166 static cpuset_t idle_cpus_mask;
167 #endif
168 
169 struct pcpuidlestat {
170 	u_int idlecalls;
171 	u_int oldidlecalls;
172 };
173 static DPCPU_DEFINE(struct pcpuidlestat, idlestat);
174 
175 static void
176 setup_runqs(void)
177 {
178 #ifdef SMP
179 	int i;
180 
181 	for (i = 0; i < MAXCPU; ++i)
182 		runq_init(&runq_pcpu[i]);
183 #endif
184 
185 	runq_init(&runq);
186 }
187 
188 static int
189 sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
190 {
191 	int error, new_val, period;
192 
193 	period = 1000000 / realstathz;
194 	new_val = period * sched_slice;
195 	error = sysctl_handle_int(oidp, &new_val, 0, req);
196 	if (error != 0 || req->newptr == NULL)
197 		return (error);
198 	if (new_val <= 0)
199 		return (EINVAL);
200 	sched_slice = imax(1, (new_val + period / 2) / period);
201 	hogticks = imax(1, (2 * hz * sched_slice + realstathz / 2) /
202 	    realstathz);
203 	return (0);
204 }
205 
206 SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RD, 0, "Scheduler");
207 
208 SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "4BSD", 0,
209     "Scheduler name");
210 SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, CTLTYPE_INT | CTLFLAG_RW,
211     NULL, 0, sysctl_kern_quantum, "I",
212     "Quantum for timeshare threads in microseconds");
213 SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
214     "Quantum for timeshare threads in stathz ticks");
215 #ifdef SMP
216 /* Enable forwarding of wakeups to all other cpus */
217 static SYSCTL_NODE(_kern_sched, OID_AUTO, ipiwakeup, CTLFLAG_RD, NULL,
218     "Kernel SMP");
219 
220 static int runq_fuzz = 1;
221 SYSCTL_INT(_kern_sched, OID_AUTO, runq_fuzz, CTLFLAG_RW, &runq_fuzz, 0, "");
222 
223 static int forward_wakeup_enabled = 1;
224 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, enabled, CTLFLAG_RW,
225 	   &forward_wakeup_enabled, 0,
226 	   "Forwarding of wakeup to idle CPUs");
227 
228 static int forward_wakeups_requested = 0;
229 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, requested, CTLFLAG_RD,
230 	   &forward_wakeups_requested, 0,
231 	   "Requests for Forwarding of wakeup to idle CPUs");
232 
233 static int forward_wakeups_delivered = 0;
234 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, delivered, CTLFLAG_RD,
235 	   &forward_wakeups_delivered, 0,
236 	   "Completed Forwarding of wakeup to idle CPUs");
237 
238 static int forward_wakeup_use_mask = 1;
239 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, usemask, CTLFLAG_RW,
240 	   &forward_wakeup_use_mask, 0,
241 	   "Use the mask of idle cpus");
242 
243 static int forward_wakeup_use_loop = 0;
244 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, useloop, CTLFLAG_RW,
245 	   &forward_wakeup_use_loop, 0,
246 	   "Use a loop to find idle cpus");
247 
248 #endif
249 #if 0
250 static int sched_followon = 0;
251 SYSCTL_INT(_kern_sched, OID_AUTO, followon, CTLFLAG_RW,
252 	   &sched_followon, 0,
253 	   "allow threads to share a quantum");
254 #endif
255 
256 SDT_PROVIDER_DEFINE(sched);
257 
258 SDT_PROBE_DEFINE3(sched, , , change_pri, change-pri, "struct thread *",
259     "struct proc *", "uint8_t");
260 SDT_PROBE_DEFINE3(sched, , , dequeue, dequeue, "struct thread *",
261     "struct proc *", "void *");
262 SDT_PROBE_DEFINE4(sched, , , enqueue, enqueue, "struct thread *",
263     "struct proc *", "void *", "int");
264 SDT_PROBE_DEFINE4(sched, , , lend_pri, lend-pri, "struct thread *",
265     "struct proc *", "uint8_t", "struct thread *");
266 SDT_PROBE_DEFINE2(sched, , , load_change, load-change, "int", "int");
267 SDT_PROBE_DEFINE2(sched, , , off_cpu, off-cpu, "struct thread *",
268     "struct proc *");
269 SDT_PROBE_DEFINE(sched, , , on_cpu, on-cpu);
270 SDT_PROBE_DEFINE(sched, , , remain_cpu, remain-cpu);
271 SDT_PROBE_DEFINE2(sched, , , surrender, surrender, "struct thread *",
272     "struct proc *");
273 
274 static __inline void
275 sched_load_add(void)
276 {
277 
278 	sched_tdcnt++;
279 	KTR_COUNTER0(KTR_SCHED, "load", "global load", sched_tdcnt);
280 	SDT_PROBE2(sched, , , load_change, NOCPU, sched_tdcnt);
281 }
282 
283 static __inline void
284 sched_load_rem(void)
285 {
286 
287 	sched_tdcnt--;
288 	KTR_COUNTER0(KTR_SCHED, "load", "global load", sched_tdcnt);
289 	SDT_PROBE2(sched, , , load_change, NOCPU, sched_tdcnt);
290 }
291 /*
292  * Arrange to reschedule if necessary, taking the priorities and
293  * schedulers into account.
294  */
295 static void
296 maybe_resched(struct thread *td)
297 {
298 
299 	THREAD_LOCK_ASSERT(td, MA_OWNED);
300 	if (td->td_priority < curthread->td_priority)
301 		curthread->td_flags |= TDF_NEEDRESCHED;
302 }
303 
304 /*
305  * This function is called when a thread is about to be put on run queue
306  * because it has been made runnable or its priority has been adjusted.  It
307  * determines if the new thread should be immediately preempted to.  If so,
308  * it switches to it and eventually returns true.  If not, it returns false
309  * so that the caller may place the thread on an appropriate run queue.
310  */
311 int
312 maybe_preempt(struct thread *td)
313 {
314 #ifdef PREEMPTION
315 	struct thread *ctd;
316 	int cpri, pri;
317 
318 	/*
319 	 * The new thread should not preempt the current thread if any of the
320 	 * following conditions are true:
321 	 *
322 	 *  - The kernel is in the throes of crashing (panicstr).
323 	 *  - The current thread has a higher (numerically lower) or
324 	 *    equivalent priority.  Note that this prevents curthread from
325 	 *    trying to preempt to itself.
326 	 *  - It is too early in the boot for context switches (cold is set).
327 	 *  - The current thread has an inhibitor set or is in the process of
328 	 *    exiting.  In this case, the current thread is about to switch
329 	 *    out anyways, so there's no point in preempting.  If we did,
330 	 *    the current thread would not be properly resumed as well, so
331 	 *    just avoid that whole landmine.
332 	 *  - If the new thread's priority is not a realtime priority and
333 	 *    the current thread's priority is not an idle priority and
334 	 *    FULL_PREEMPTION is disabled.
335 	 *
336 	 * If all of these conditions are false, but the current thread is in
337 	 * a nested critical section, then we have to defer the preemption
338 	 * until we exit the critical section.  Otherwise, switch immediately
339 	 * to the new thread.
340 	 */
341 	ctd = curthread;
342 	THREAD_LOCK_ASSERT(td, MA_OWNED);
343 	KASSERT((td->td_inhibitors == 0),
344 			("maybe_preempt: trying to run inhibited thread"));
345 	pri = td->td_priority;
346 	cpri = ctd->td_priority;
347 	if (panicstr != NULL || pri >= cpri || cold /* || dumping */ ||
348 	    TD_IS_INHIBITED(ctd))
349 		return (0);
350 #ifndef FULL_PREEMPTION
351 	if (pri > PRI_MAX_ITHD && cpri < PRI_MIN_IDLE)
352 		return (0);
353 #endif
354 
355 	if (ctd->td_critnest > 1) {
356 		CTR1(KTR_PROC, "maybe_preempt: in critical section %d",
357 		    ctd->td_critnest);
358 		ctd->td_owepreempt = 1;
359 		return (0);
360 	}
361 	/*
362 	 * Thread is runnable but not yet put on system run queue.
363 	 */
364 	MPASS(ctd->td_lock == td->td_lock);
365 	MPASS(TD_ON_RUNQ(td));
366 	TD_SET_RUNNING(td);
367 	CTR3(KTR_PROC, "preempting to thread %p (pid %d, %s)\n", td,
368 	    td->td_proc->p_pid, td->td_name);
369 	mi_switch(SW_INVOL | SW_PREEMPT | SWT_PREEMPT, td);
370 	/*
371 	 * td's lock pointer may have changed.  We have to return with it
372 	 * locked.
373 	 */
374 	spinlock_enter();
375 	thread_unlock(ctd);
376 	thread_lock(td);
377 	spinlock_exit();
378 	return (1);
379 #else
380 	return (0);
381 #endif
382 }
383 
384 /*
385  * Constants for digital decay and forget:
386  *	90% of (td_estcpu) usage in 5 * loadav time
387  *	95% of (ts_pctcpu) usage in 60 seconds (load insensitive)
388  *          Note that, as ps(1) mentions, this can let percentages
389  *          total over 100% (I've seen 137.9% for 3 processes).
390  *
391  * Note that schedclock() updates td_estcpu and p_cpticks asynchronously.
392  *
393  * We wish to decay away 90% of td_estcpu in (5 * loadavg) seconds.
394  * That is, the system wants to compute a value of decay such
395  * that the following for loop:
396  * 	for (i = 0; i < (5 * loadavg); i++)
397  * 		td_estcpu *= decay;
398  * will compute
399  * 	td_estcpu *= 0.1;
400  * for all values of loadavg:
401  *
402  * Mathematically this loop can be expressed by saying:
403  * 	decay ** (5 * loadavg) ~= .1
404  *
405  * The system computes decay as:
406  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
407  *
408  * We wish to prove that the system's computation of decay
409  * will always fulfill the equation:
410  * 	decay ** (5 * loadavg) ~= .1
411  *
412  * If we compute b as:
413  * 	b = 2 * loadavg
414  * then
415  * 	decay = b / (b + 1)
416  *
417  * We now need to prove two things:
418  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
419  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
420  *
421  * Facts:
422  *         For x close to zero, exp(x) =~ 1 + x, since
423  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
424  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
425  *         For x close to zero, ln(1+x) =~ x, since
426  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
427  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
428  *         ln(.1) =~ -2.30
429  *
430  * Proof of (1):
431  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
432  *	solving for factor,
433  *      ln(factor) =~ (-2.30/5*loadav), or
434  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
435  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
436  *
437  * Proof of (2):
438  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
439  *	solving for power,
440  *      power*ln(b/(b+1)) =~ -2.30, or
441  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
442  *
443  * Actual power values for the implemented algorithm are as follows:
444  *      loadav: 1       2       3       4
445  *      power:  5.68    10.32   14.94   19.55
446  */
447 
448 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
449 #define	loadfactor(loadav)	(2 * (loadav))
450 #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
451 
452 /* decay 95% of `ts_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
453 static fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;	/* exp(-1/20) */
454 SYSCTL_UINT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
455 
456 /*
457  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
458  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
459  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
460  *
461  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
462  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
463  *
464  * If you don't want to bother with the faster/more-accurate formula, you
465  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
466  * (more general) method of calculating the %age of CPU used by a process.
467  */
468 #define	CCPU_SHIFT	11
469 
470 /*
471  * Recompute process priorities, every hz ticks.
472  * MP-safe, called without the Giant mutex.
473  */
474 /* ARGSUSED */
475 static void
476 schedcpu(void)
477 {
478 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
479 	struct thread *td;
480 	struct proc *p;
481 	struct td_sched *ts;
482 	int awake;
483 
484 	sx_slock(&allproc_lock);
485 	FOREACH_PROC_IN_SYSTEM(p) {
486 		PROC_LOCK(p);
487 		if (p->p_state == PRS_NEW) {
488 			PROC_UNLOCK(p);
489 			continue;
490 		}
491 		FOREACH_THREAD_IN_PROC(p, td) {
492 			awake = 0;
493 			thread_lock(td);
494 			ts = td->td_sched;
495 			/*
496 			 * Increment sleep time (if sleeping).  We
497 			 * ignore overflow, as above.
498 			 */
499 			/*
500 			 * The td_sched slptimes are not touched in wakeup
501 			 * because the thread may not HAVE everything in
502 			 * memory? XXX I think this is out of date.
503 			 */
504 			if (TD_ON_RUNQ(td)) {
505 				awake = 1;
506 				td->td_flags &= ~TDF_DIDRUN;
507 			} else if (TD_IS_RUNNING(td)) {
508 				awake = 1;
509 				/* Do not clear TDF_DIDRUN */
510 			} else if (td->td_flags & TDF_DIDRUN) {
511 				awake = 1;
512 				td->td_flags &= ~TDF_DIDRUN;
513 			}
514 
515 			/*
516 			 * ts_pctcpu is only for ps and ttyinfo().
517 			 */
518 			ts->ts_pctcpu = (ts->ts_pctcpu * ccpu) >> FSHIFT;
519 			/*
520 			 * If the td_sched has been idle the entire second,
521 			 * stop recalculating its priority until
522 			 * it wakes up.
523 			 */
524 			if (ts->ts_cpticks != 0) {
525 #if	(FSHIFT >= CCPU_SHIFT)
526 				ts->ts_pctcpu += (realstathz == 100)
527 				    ? ((fixpt_t) ts->ts_cpticks) <<
528 				    (FSHIFT - CCPU_SHIFT) :
529 				    100 * (((fixpt_t) ts->ts_cpticks)
530 				    << (FSHIFT - CCPU_SHIFT)) / realstathz;
531 #else
532 				ts->ts_pctcpu += ((FSCALE - ccpu) *
533 				    (ts->ts_cpticks *
534 				    FSCALE / realstathz)) >> FSHIFT;
535 #endif
536 				ts->ts_cpticks = 0;
537 			}
538 			/*
539 			 * If there are ANY running threads in this process,
540 			 * then don't count it as sleeping.
541 			 * XXX: this is broken.
542 			 */
543 			if (awake) {
544 				if (ts->ts_slptime > 1) {
545 					/*
546 					 * In an ideal world, this should not
547 					 * happen, because whoever woke us
548 					 * up from the long sleep should have
549 					 * unwound the slptime and reset our
550 					 * priority before we run at the stale
551 					 * priority.  Should KASSERT at some
552 					 * point when all the cases are fixed.
553 					 */
554 					updatepri(td);
555 				}
556 				ts->ts_slptime = 0;
557 			} else
558 				ts->ts_slptime++;
559 			if (ts->ts_slptime > 1) {
560 				thread_unlock(td);
561 				continue;
562 			}
563 			td->td_estcpu = decay_cpu(loadfac, td->td_estcpu);
564 		      	resetpriority(td);
565 			resetpriority_thread(td);
566 			thread_unlock(td);
567 		}
568 		PROC_UNLOCK(p);
569 	}
570 	sx_sunlock(&allproc_lock);
571 }
572 
573 /*
574  * Main loop for a kthread that executes schedcpu once a second.
575  */
576 static void
577 schedcpu_thread(void)
578 {
579 
580 	for (;;) {
581 		schedcpu();
582 		pause("-", hz);
583 	}
584 }
585 
586 /*
587  * Recalculate the priority of a process after it has slept for a while.
588  * For all load averages >= 1 and max td_estcpu of 255, sleeping for at
589  * least six times the loadfactor will decay td_estcpu to zero.
590  */
591 static void
592 updatepri(struct thread *td)
593 {
594 	struct td_sched *ts;
595 	fixpt_t loadfac;
596 	unsigned int newcpu;
597 
598 	ts = td->td_sched;
599 	loadfac = loadfactor(averunnable.ldavg[0]);
600 	if (ts->ts_slptime > 5 * loadfac)
601 		td->td_estcpu = 0;
602 	else {
603 		newcpu = td->td_estcpu;
604 		ts->ts_slptime--;	/* was incremented in schedcpu() */
605 		while (newcpu && --ts->ts_slptime)
606 			newcpu = decay_cpu(loadfac, newcpu);
607 		td->td_estcpu = newcpu;
608 	}
609 }
610 
611 /*
612  * Compute the priority of a process when running in user mode.
613  * Arrange to reschedule if the resulting priority is better
614  * than that of the current process.
615  */
616 static void
617 resetpriority(struct thread *td)
618 {
619 	register unsigned int newpriority;
620 
621 	if (td->td_pri_class == PRI_TIMESHARE) {
622 		newpriority = PUSER + td->td_estcpu / INVERSE_ESTCPU_WEIGHT +
623 		    NICE_WEIGHT * (td->td_proc->p_nice - PRIO_MIN);
624 		newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
625 		    PRI_MAX_TIMESHARE);
626 		sched_user_prio(td, newpriority);
627 	}
628 }
629 
630 /*
631  * Update the thread's priority when the associated process's user
632  * priority changes.
633  */
634 static void
635 resetpriority_thread(struct thread *td)
636 {
637 
638 	/* Only change threads with a time sharing user priority. */
639 	if (td->td_priority < PRI_MIN_TIMESHARE ||
640 	    td->td_priority > PRI_MAX_TIMESHARE)
641 		return;
642 
643 	/* XXX the whole needresched thing is broken, but not silly. */
644 	maybe_resched(td);
645 
646 	sched_prio(td, td->td_user_pri);
647 }
648 
649 /* ARGSUSED */
650 static void
651 sched_setup(void *dummy)
652 {
653 
654 	setup_runqs();
655 
656 	/* Account for thread0. */
657 	sched_load_add();
658 }
659 
660 /*
661  * This routine determines time constants after stathz and hz are setup.
662  */
663 static void
664 sched_initticks(void *dummy)
665 {
666 
667 	realstathz = stathz ? stathz : hz;
668 	sched_slice = realstathz / 10;	/* ~100ms */
669 	hogticks = imax(1, (2 * hz * sched_slice + realstathz / 2) /
670 	    realstathz);
671 }
672 
673 /* External interfaces start here */
674 
675 /*
676  * Very early in the boot some setup of scheduler-specific
677  * parts of proc0 and of some scheduler resources needs to be done.
678  * Called from:
679  *  proc0_init()
680  */
681 void
682 schedinit(void)
683 {
684 	/*
685 	 * Set up the scheduler specific parts of proc0.
686 	 */
687 	proc0.p_sched = NULL; /* XXX */
688 	thread0.td_sched = &td_sched0;
689 	thread0.td_lock = &sched_lock;
690 	td_sched0.ts_slice = sched_slice;
691 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
692 }
693 
694 int
695 sched_runnable(void)
696 {
697 #ifdef SMP
698 	return runq_check(&runq) + runq_check(&runq_pcpu[PCPU_GET(cpuid)]);
699 #else
700 	return runq_check(&runq);
701 #endif
702 }
703 
704 int
705 sched_rr_interval(void)
706 {
707 
708 	/* Convert sched_slice from stathz to hz. */
709 	return (imax(1, (sched_slice * hz + realstathz / 2) / realstathz));
710 }
711 
712 /*
713  * We adjust the priority of the current process.  The priority of
714  * a process gets worse as it accumulates CPU time.  The cpu usage
715  * estimator (td_estcpu) is increased here.  resetpriority() will
716  * compute a different priority each time td_estcpu increases by
717  * INVERSE_ESTCPU_WEIGHT
718  * (until MAXPRI is reached).  The cpu usage estimator ramps up
719  * quite quickly when the process is running (linearly), and decays
720  * away exponentially, at a rate which is proportionally slower when
721  * the system is busy.  The basic principle is that the system will
722  * 90% forget that the process used a lot of CPU time in 5 * loadav
723  * seconds.  This causes the system to favor processes which haven't
724  * run much recently, and to round-robin among other processes.
725  */
726 void
727 sched_clock(struct thread *td)
728 {
729 	struct pcpuidlestat *stat;
730 	struct td_sched *ts;
731 
732 	THREAD_LOCK_ASSERT(td, MA_OWNED);
733 	ts = td->td_sched;
734 
735 	ts->ts_cpticks++;
736 	td->td_estcpu = ESTCPULIM(td->td_estcpu + 1);
737 	if ((td->td_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
738 		resetpriority(td);
739 		resetpriority_thread(td);
740 	}
741 
742 	/*
743 	 * Force a context switch if the current thread has used up a full
744 	 * time slice (default is 100ms).
745 	 */
746 	if (!TD_IS_IDLETHREAD(td) && --ts->ts_slice <= 0) {
747 		ts->ts_slice = sched_slice;
748 		td->td_flags |= TDF_NEEDRESCHED | TDF_SLICEEND;
749 	}
750 
751 	stat = DPCPU_PTR(idlestat);
752 	stat->oldidlecalls = stat->idlecalls;
753 	stat->idlecalls = 0;
754 }
755 
756 /*
757  * Charge child's scheduling CPU usage to parent.
758  */
759 void
760 sched_exit(struct proc *p, struct thread *td)
761 {
762 
763 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "proc exit",
764 	    "prio:%d", td->td_priority);
765 
766 	PROC_LOCK_ASSERT(p, MA_OWNED);
767 	sched_exit_thread(FIRST_THREAD_IN_PROC(p), td);
768 }
769 
770 void
771 sched_exit_thread(struct thread *td, struct thread *child)
772 {
773 
774 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(child), "exit",
775 	    "prio:%d", child->td_priority);
776 	thread_lock(td);
777 	td->td_estcpu = ESTCPULIM(td->td_estcpu + child->td_estcpu);
778 	thread_unlock(td);
779 	thread_lock(child);
780 	if ((child->td_flags & TDF_NOLOAD) == 0)
781 		sched_load_rem();
782 	thread_unlock(child);
783 }
784 
785 void
786 sched_fork(struct thread *td, struct thread *childtd)
787 {
788 	sched_fork_thread(td, childtd);
789 }
790 
791 void
792 sched_fork_thread(struct thread *td, struct thread *childtd)
793 {
794 	struct td_sched *ts;
795 
796 	childtd->td_estcpu = td->td_estcpu;
797 	childtd->td_lock = &sched_lock;
798 	childtd->td_cpuset = cpuset_ref(td->td_cpuset);
799 	childtd->td_priority = childtd->td_base_pri;
800 	ts = childtd->td_sched;
801 	bzero(ts, sizeof(*ts));
802 	ts->ts_flags |= (td->td_sched->ts_flags & TSF_AFFINITY);
803 	ts->ts_slice = 1;
804 }
805 
806 void
807 sched_nice(struct proc *p, int nice)
808 {
809 	struct thread *td;
810 
811 	PROC_LOCK_ASSERT(p, MA_OWNED);
812 	p->p_nice = nice;
813 	FOREACH_THREAD_IN_PROC(p, td) {
814 		thread_lock(td);
815 		resetpriority(td);
816 		resetpriority_thread(td);
817 		thread_unlock(td);
818 	}
819 }
820 
821 void
822 sched_class(struct thread *td, int class)
823 {
824 	THREAD_LOCK_ASSERT(td, MA_OWNED);
825 	td->td_pri_class = class;
826 }
827 
828 /*
829  * Adjust the priority of a thread.
830  */
831 static void
832 sched_priority(struct thread *td, u_char prio)
833 {
834 
835 
836 	KTR_POINT3(KTR_SCHED, "thread", sched_tdname(td), "priority change",
837 	    "prio:%d", td->td_priority, "new prio:%d", prio, KTR_ATTR_LINKED,
838 	    sched_tdname(curthread));
839 	SDT_PROBE3(sched, , , change_pri, td, td->td_proc, prio);
840 	if (td != curthread && prio > td->td_priority) {
841 		KTR_POINT3(KTR_SCHED, "thread", sched_tdname(curthread),
842 		    "lend prio", "prio:%d", td->td_priority, "new prio:%d",
843 		    prio, KTR_ATTR_LINKED, sched_tdname(td));
844 		SDT_PROBE4(sched, , , lend_pri, td, td->td_proc, prio,
845 		    curthread);
846 	}
847 	THREAD_LOCK_ASSERT(td, MA_OWNED);
848 	if (td->td_priority == prio)
849 		return;
850 	td->td_priority = prio;
851 	if (TD_ON_RUNQ(td) && td->td_rqindex != (prio / RQ_PPQ)) {
852 		sched_rem(td);
853 		sched_add(td, SRQ_BORING);
854 	}
855 }
856 
857 /*
858  * Update a thread's priority when it is lent another thread's
859  * priority.
860  */
861 void
862 sched_lend_prio(struct thread *td, u_char prio)
863 {
864 
865 	td->td_flags |= TDF_BORROWING;
866 	sched_priority(td, prio);
867 }
868 
869 /*
870  * Restore a thread's priority when priority propagation is
871  * over.  The prio argument is the minimum priority the thread
872  * needs to have to satisfy other possible priority lending
873  * requests.  If the thread's regulary priority is less
874  * important than prio the thread will keep a priority boost
875  * of prio.
876  */
877 void
878 sched_unlend_prio(struct thread *td, u_char prio)
879 {
880 	u_char base_pri;
881 
882 	if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
883 	    td->td_base_pri <= PRI_MAX_TIMESHARE)
884 		base_pri = td->td_user_pri;
885 	else
886 		base_pri = td->td_base_pri;
887 	if (prio >= base_pri) {
888 		td->td_flags &= ~TDF_BORROWING;
889 		sched_prio(td, base_pri);
890 	} else
891 		sched_lend_prio(td, prio);
892 }
893 
894 void
895 sched_prio(struct thread *td, u_char prio)
896 {
897 	u_char oldprio;
898 
899 	/* First, update the base priority. */
900 	td->td_base_pri = prio;
901 
902 	/*
903 	 * If the thread is borrowing another thread's priority, don't ever
904 	 * lower the priority.
905 	 */
906 	if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
907 		return;
908 
909 	/* Change the real priority. */
910 	oldprio = td->td_priority;
911 	sched_priority(td, prio);
912 
913 	/*
914 	 * If the thread is on a turnstile, then let the turnstile update
915 	 * its state.
916 	 */
917 	if (TD_ON_LOCK(td) && oldprio != prio)
918 		turnstile_adjust(td, oldprio);
919 }
920 
921 void
922 sched_user_prio(struct thread *td, u_char prio)
923 {
924 
925 	THREAD_LOCK_ASSERT(td, MA_OWNED);
926 	td->td_base_user_pri = prio;
927 	if (td->td_lend_user_pri <= prio)
928 		return;
929 	td->td_user_pri = prio;
930 }
931 
932 void
933 sched_lend_user_prio(struct thread *td, u_char prio)
934 {
935 
936 	THREAD_LOCK_ASSERT(td, MA_OWNED);
937 	td->td_lend_user_pri = prio;
938 	td->td_user_pri = min(prio, td->td_base_user_pri);
939 	if (td->td_priority > td->td_user_pri)
940 		sched_prio(td, td->td_user_pri);
941 	else if (td->td_priority != td->td_user_pri)
942 		td->td_flags |= TDF_NEEDRESCHED;
943 }
944 
945 void
946 sched_sleep(struct thread *td, int pri)
947 {
948 
949 	THREAD_LOCK_ASSERT(td, MA_OWNED);
950 	td->td_slptick = ticks;
951 	td->td_sched->ts_slptime = 0;
952 	if (pri != 0 && PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
953 		sched_prio(td, pri);
954 	if (TD_IS_SUSPENDED(td) || pri >= PSOCK)
955 		td->td_flags |= TDF_CANSWAP;
956 }
957 
958 void
959 sched_switch(struct thread *td, struct thread *newtd, int flags)
960 {
961 	struct mtx *tmtx;
962 	struct td_sched *ts;
963 	struct proc *p;
964 	int preempted;
965 
966 	tmtx = NULL;
967 	ts = td->td_sched;
968 	p = td->td_proc;
969 
970 	THREAD_LOCK_ASSERT(td, MA_OWNED);
971 
972 	/*
973 	 * Switch to the sched lock to fix things up and pick
974 	 * a new thread.
975 	 * Block the td_lock in order to avoid breaking the critical path.
976 	 */
977 	if (td->td_lock != &sched_lock) {
978 		mtx_lock_spin(&sched_lock);
979 		tmtx = thread_lock_block(td);
980 	}
981 
982 	if ((td->td_flags & TDF_NOLOAD) == 0)
983 		sched_load_rem();
984 
985 	td->td_lastcpu = td->td_oncpu;
986 	preempted = !(td->td_flags & TDF_SLICEEND);
987 	td->td_flags &= ~(TDF_NEEDRESCHED | TDF_SLICEEND);
988 	td->td_owepreempt = 0;
989 	td->td_oncpu = NOCPU;
990 
991 	/*
992 	 * At the last moment, if this thread is still marked RUNNING,
993 	 * then put it back on the run queue as it has not been suspended
994 	 * or stopped or any thing else similar.  We never put the idle
995 	 * threads on the run queue, however.
996 	 */
997 	if (td->td_flags & TDF_IDLETD) {
998 		TD_SET_CAN_RUN(td);
999 #ifdef SMP
1000 		CPU_CLR(PCPU_GET(cpuid), &idle_cpus_mask);
1001 #endif
1002 	} else {
1003 		if (TD_IS_RUNNING(td)) {
1004 			/* Put us back on the run queue. */
1005 			sched_add(td, preempted ?
1006 			    SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1007 			    SRQ_OURSELF|SRQ_YIELDING);
1008 		}
1009 	}
1010 	if (newtd) {
1011 		/*
1012 		 * The thread we are about to run needs to be counted
1013 		 * as if it had been added to the run queue and selected.
1014 		 * It came from:
1015 		 * * A preemption
1016 		 * * An upcall
1017 		 * * A followon
1018 		 */
1019 		KASSERT((newtd->td_inhibitors == 0),
1020 			("trying to run inhibited thread"));
1021 		newtd->td_flags |= TDF_DIDRUN;
1022         	TD_SET_RUNNING(newtd);
1023 		if ((newtd->td_flags & TDF_NOLOAD) == 0)
1024 			sched_load_add();
1025 	} else {
1026 		newtd = choosethread();
1027 		MPASS(newtd->td_lock == &sched_lock);
1028 	}
1029 
1030 	if (td != newtd) {
1031 #ifdef	HWPMC_HOOKS
1032 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1033 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1034 #endif
1035 
1036 		SDT_PROBE2(sched, , , off_cpu, td, td->td_proc);
1037 
1038                 /* I feel sleepy */
1039 		lock_profile_release_lock(&sched_lock.lock_object);
1040 #ifdef KDTRACE_HOOKS
1041 		/*
1042 		 * If DTrace has set the active vtime enum to anything
1043 		 * other than INACTIVE (0), then it should have set the
1044 		 * function to call.
1045 		 */
1046 		if (dtrace_vtime_active)
1047 			(*dtrace_vtime_switch_func)(newtd);
1048 #endif
1049 
1050 		cpu_switch(td, newtd, tmtx != NULL ? tmtx : td->td_lock);
1051 		lock_profile_obtain_lock_success(&sched_lock.lock_object,
1052 		    0, 0, __FILE__, __LINE__);
1053 		/*
1054 		 * Where am I?  What year is it?
1055 		 * We are in the same thread that went to sleep above,
1056 		 * but any amount of time may have passed. All our context
1057 		 * will still be available as will local variables.
1058 		 * PCPU values however may have changed as we may have
1059 		 * changed CPU so don't trust cached values of them.
1060 		 * New threads will go to fork_exit() instead of here
1061 		 * so if you change things here you may need to change
1062 		 * things there too.
1063 		 *
1064 		 * If the thread above was exiting it will never wake
1065 		 * up again here, so either it has saved everything it
1066 		 * needed to, or the thread_wait() or wait() will
1067 		 * need to reap it.
1068 		 */
1069 
1070 		SDT_PROBE0(sched, , , on_cpu);
1071 #ifdef	HWPMC_HOOKS
1072 		if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1073 			PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1074 #endif
1075 	} else
1076 		SDT_PROBE0(sched, , , remain_cpu);
1077 
1078 #ifdef SMP
1079 	if (td->td_flags & TDF_IDLETD)
1080 		CPU_SET(PCPU_GET(cpuid), &idle_cpus_mask);
1081 #endif
1082 	sched_lock.mtx_lock = (uintptr_t)td;
1083 	td->td_oncpu = PCPU_GET(cpuid);
1084 	MPASS(td->td_lock == &sched_lock);
1085 }
1086 
1087 void
1088 sched_wakeup(struct thread *td)
1089 {
1090 	struct td_sched *ts;
1091 
1092 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1093 	ts = td->td_sched;
1094 	td->td_flags &= ~TDF_CANSWAP;
1095 	if (ts->ts_slptime > 1) {
1096 		updatepri(td);
1097 		resetpriority(td);
1098 	}
1099 	td->td_slptick = 0;
1100 	ts->ts_slptime = 0;
1101 	ts->ts_slice = sched_slice;
1102 	sched_add(td, SRQ_BORING);
1103 }
1104 
1105 #ifdef SMP
1106 static int
1107 forward_wakeup(int cpunum)
1108 {
1109 	struct pcpu *pc;
1110 	cpuset_t dontuse, map, map2;
1111 	u_int id, me;
1112 	int iscpuset;
1113 
1114 	mtx_assert(&sched_lock, MA_OWNED);
1115 
1116 	CTR0(KTR_RUNQ, "forward_wakeup()");
1117 
1118 	if ((!forward_wakeup_enabled) ||
1119 	     (forward_wakeup_use_mask == 0 && forward_wakeup_use_loop == 0))
1120 		return (0);
1121 	if (!smp_started || cold || panicstr)
1122 		return (0);
1123 
1124 	forward_wakeups_requested++;
1125 
1126 	/*
1127 	 * Check the idle mask we received against what we calculated
1128 	 * before in the old version.
1129 	 */
1130 	me = PCPU_GET(cpuid);
1131 
1132 	/* Don't bother if we should be doing it ourself. */
1133 	if (CPU_ISSET(me, &idle_cpus_mask) &&
1134 	    (cpunum == NOCPU || me == cpunum))
1135 		return (0);
1136 
1137 	CPU_SETOF(me, &dontuse);
1138 	CPU_OR(&dontuse, &stopped_cpus);
1139 	CPU_OR(&dontuse, &hlt_cpus_mask);
1140 	CPU_ZERO(&map2);
1141 	if (forward_wakeup_use_loop) {
1142 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
1143 			id = pc->pc_cpuid;
1144 			if (!CPU_ISSET(id, &dontuse) &&
1145 			    pc->pc_curthread == pc->pc_idlethread) {
1146 				CPU_SET(id, &map2);
1147 			}
1148 		}
1149 	}
1150 
1151 	if (forward_wakeup_use_mask) {
1152 		map = idle_cpus_mask;
1153 		CPU_NAND(&map, &dontuse);
1154 
1155 		/* If they are both on, compare and use loop if different. */
1156 		if (forward_wakeup_use_loop) {
1157 			if (CPU_CMP(&map, &map2)) {
1158 				printf("map != map2, loop method preferred\n");
1159 				map = map2;
1160 			}
1161 		}
1162 	} else {
1163 		map = map2;
1164 	}
1165 
1166 	/* If we only allow a specific CPU, then mask off all the others. */
1167 	if (cpunum != NOCPU) {
1168 		KASSERT((cpunum <= mp_maxcpus),("forward_wakeup: bad cpunum."));
1169 		iscpuset = CPU_ISSET(cpunum, &map);
1170 		if (iscpuset == 0)
1171 			CPU_ZERO(&map);
1172 		else
1173 			CPU_SETOF(cpunum, &map);
1174 	}
1175 	if (!CPU_EMPTY(&map)) {
1176 		forward_wakeups_delivered++;
1177 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
1178 			id = pc->pc_cpuid;
1179 			if (!CPU_ISSET(id, &map))
1180 				continue;
1181 			if (cpu_idle_wakeup(pc->pc_cpuid))
1182 				CPU_CLR(id, &map);
1183 		}
1184 		if (!CPU_EMPTY(&map))
1185 			ipi_selected(map, IPI_AST);
1186 		return (1);
1187 	}
1188 	if (cpunum == NOCPU)
1189 		printf("forward_wakeup: Idle processor not found\n");
1190 	return (0);
1191 }
1192 
1193 static void
1194 kick_other_cpu(int pri, int cpuid)
1195 {
1196 	struct pcpu *pcpu;
1197 	int cpri;
1198 
1199 	pcpu = pcpu_find(cpuid);
1200 	if (CPU_ISSET(cpuid, &idle_cpus_mask)) {
1201 		forward_wakeups_delivered++;
1202 		if (!cpu_idle_wakeup(cpuid))
1203 			ipi_cpu(cpuid, IPI_AST);
1204 		return;
1205 	}
1206 
1207 	cpri = pcpu->pc_curthread->td_priority;
1208 	if (pri >= cpri)
1209 		return;
1210 
1211 #if defined(IPI_PREEMPTION) && defined(PREEMPTION)
1212 #if !defined(FULL_PREEMPTION)
1213 	if (pri <= PRI_MAX_ITHD)
1214 #endif /* ! FULL_PREEMPTION */
1215 	{
1216 		ipi_cpu(cpuid, IPI_PREEMPT);
1217 		return;
1218 	}
1219 #endif /* defined(IPI_PREEMPTION) && defined(PREEMPTION) */
1220 
1221 	pcpu->pc_curthread->td_flags |= TDF_NEEDRESCHED;
1222 	ipi_cpu(cpuid, IPI_AST);
1223 	return;
1224 }
1225 #endif /* SMP */
1226 
1227 #ifdef SMP
1228 static int
1229 sched_pickcpu(struct thread *td)
1230 {
1231 	int best, cpu;
1232 
1233 	mtx_assert(&sched_lock, MA_OWNED);
1234 
1235 	if (THREAD_CAN_SCHED(td, td->td_lastcpu))
1236 		best = td->td_lastcpu;
1237 	else
1238 		best = NOCPU;
1239 	CPU_FOREACH(cpu) {
1240 		if (!THREAD_CAN_SCHED(td, cpu))
1241 			continue;
1242 
1243 		if (best == NOCPU)
1244 			best = cpu;
1245 		else if (runq_length[cpu] < runq_length[best])
1246 			best = cpu;
1247 	}
1248 	KASSERT(best != NOCPU, ("no valid CPUs"));
1249 
1250 	return (best);
1251 }
1252 #endif
1253 
1254 void
1255 sched_add(struct thread *td, int flags)
1256 #ifdef SMP
1257 {
1258 	cpuset_t tidlemsk;
1259 	struct td_sched *ts;
1260 	u_int cpu, cpuid;
1261 	int forwarded = 0;
1262 	int single_cpu = 0;
1263 
1264 	ts = td->td_sched;
1265 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1266 	KASSERT((td->td_inhibitors == 0),
1267 	    ("sched_add: trying to run inhibited thread"));
1268 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1269 	    ("sched_add: bad thread state"));
1270 	KASSERT(td->td_flags & TDF_INMEM,
1271 	    ("sched_add: thread swapped out"));
1272 
1273 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add",
1274 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
1275 	    sched_tdname(curthread));
1276 	KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup",
1277 	    KTR_ATTR_LINKED, sched_tdname(td));
1278 	SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL,
1279 	    flags & SRQ_PREEMPTED);
1280 
1281 
1282 	/*
1283 	 * Now that the thread is moving to the run-queue, set the lock
1284 	 * to the scheduler's lock.
1285 	 */
1286 	if (td->td_lock != &sched_lock) {
1287 		mtx_lock_spin(&sched_lock);
1288 		thread_lock_set(td, &sched_lock);
1289 	}
1290 	TD_SET_RUNQ(td);
1291 
1292 	/*
1293 	 * If SMP is started and the thread is pinned or otherwise limited to
1294 	 * a specific set of CPUs, queue the thread to a per-CPU run queue.
1295 	 * Otherwise, queue the thread to the global run queue.
1296 	 *
1297 	 * If SMP has not yet been started we must use the global run queue
1298 	 * as per-CPU state may not be initialized yet and we may crash if we
1299 	 * try to access the per-CPU run queues.
1300 	 */
1301 	if (smp_started && (td->td_pinned != 0 || td->td_flags & TDF_BOUND ||
1302 	    ts->ts_flags & TSF_AFFINITY)) {
1303 		if (td->td_pinned != 0)
1304 			cpu = td->td_lastcpu;
1305 		else if (td->td_flags & TDF_BOUND) {
1306 			/* Find CPU from bound runq. */
1307 			KASSERT(SKE_RUNQ_PCPU(ts),
1308 			    ("sched_add: bound td_sched not on cpu runq"));
1309 			cpu = ts->ts_runq - &runq_pcpu[0];
1310 		} else
1311 			/* Find a valid CPU for our cpuset */
1312 			cpu = sched_pickcpu(td);
1313 		ts->ts_runq = &runq_pcpu[cpu];
1314 		single_cpu = 1;
1315 		CTR3(KTR_RUNQ,
1316 		    "sched_add: Put td_sched:%p(td:%p) on cpu%d runq", ts, td,
1317 		    cpu);
1318 	} else {
1319 		CTR2(KTR_RUNQ,
1320 		    "sched_add: adding td_sched:%p (td:%p) to gbl runq", ts,
1321 		    td);
1322 		cpu = NOCPU;
1323 		ts->ts_runq = &runq;
1324 	}
1325 
1326 	cpuid = PCPU_GET(cpuid);
1327 	if (single_cpu && cpu != cpuid) {
1328 	        kick_other_cpu(td->td_priority, cpu);
1329 	} else {
1330 		if (!single_cpu) {
1331 			tidlemsk = idle_cpus_mask;
1332 			CPU_NAND(&tidlemsk, &hlt_cpus_mask);
1333 			CPU_CLR(cpuid, &tidlemsk);
1334 
1335 			if (!CPU_ISSET(cpuid, &idle_cpus_mask) &&
1336 			    ((flags & SRQ_INTR) == 0) &&
1337 			    !CPU_EMPTY(&tidlemsk))
1338 				forwarded = forward_wakeup(cpu);
1339 		}
1340 
1341 		if (!forwarded) {
1342 			if ((flags & SRQ_YIELDING) == 0 && maybe_preempt(td))
1343 				return;
1344 			else
1345 				maybe_resched(td);
1346 		}
1347 	}
1348 
1349 	if ((td->td_flags & TDF_NOLOAD) == 0)
1350 		sched_load_add();
1351 	runq_add(ts->ts_runq, td, flags);
1352 	if (cpu != NOCPU)
1353 		runq_length[cpu]++;
1354 }
1355 #else /* SMP */
1356 {
1357 	struct td_sched *ts;
1358 
1359 	ts = td->td_sched;
1360 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1361 	KASSERT((td->td_inhibitors == 0),
1362 	    ("sched_add: trying to run inhibited thread"));
1363 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1364 	    ("sched_add: bad thread state"));
1365 	KASSERT(td->td_flags & TDF_INMEM,
1366 	    ("sched_add: thread swapped out"));
1367 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq add",
1368 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
1369 	    sched_tdname(curthread));
1370 	KTR_POINT1(KTR_SCHED, "thread", sched_tdname(curthread), "wokeup",
1371 	    KTR_ATTR_LINKED, sched_tdname(td));
1372 	SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL,
1373 	    flags & SRQ_PREEMPTED);
1374 
1375 	/*
1376 	 * Now that the thread is moving to the run-queue, set the lock
1377 	 * to the scheduler's lock.
1378 	 */
1379 	if (td->td_lock != &sched_lock) {
1380 		mtx_lock_spin(&sched_lock);
1381 		thread_lock_set(td, &sched_lock);
1382 	}
1383 	TD_SET_RUNQ(td);
1384 	CTR2(KTR_RUNQ, "sched_add: adding td_sched:%p (td:%p) to runq", ts, td);
1385 	ts->ts_runq = &runq;
1386 
1387 	/*
1388 	 * If we are yielding (on the way out anyhow) or the thread
1389 	 * being saved is US, then don't try be smart about preemption
1390 	 * or kicking off another CPU as it won't help and may hinder.
1391 	 * In the YIEDLING case, we are about to run whoever is being
1392 	 * put in the queue anyhow, and in the OURSELF case, we are
1393 	 * puting ourself on the run queue which also only happens
1394 	 * when we are about to yield.
1395 	 */
1396 	if ((flags & SRQ_YIELDING) == 0) {
1397 		if (maybe_preempt(td))
1398 			return;
1399 	}
1400 	if ((td->td_flags & TDF_NOLOAD) == 0)
1401 		sched_load_add();
1402 	runq_add(ts->ts_runq, td, flags);
1403 	maybe_resched(td);
1404 }
1405 #endif /* SMP */
1406 
1407 void
1408 sched_rem(struct thread *td)
1409 {
1410 	struct td_sched *ts;
1411 
1412 	ts = td->td_sched;
1413 	KASSERT(td->td_flags & TDF_INMEM,
1414 	    ("sched_rem: thread swapped out"));
1415 	KASSERT(TD_ON_RUNQ(td),
1416 	    ("sched_rem: thread not on run queue"));
1417 	mtx_assert(&sched_lock, MA_OWNED);
1418 	KTR_STATE2(KTR_SCHED, "thread", sched_tdname(td), "runq rem",
1419 	    "prio:%d", td->td_priority, KTR_ATTR_LINKED,
1420 	    sched_tdname(curthread));
1421 	SDT_PROBE3(sched, , , dequeue, td, td->td_proc, NULL);
1422 
1423 	if ((td->td_flags & TDF_NOLOAD) == 0)
1424 		sched_load_rem();
1425 #ifdef SMP
1426 	if (ts->ts_runq != &runq)
1427 		runq_length[ts->ts_runq - runq_pcpu]--;
1428 #endif
1429 	runq_remove(ts->ts_runq, td);
1430 	TD_SET_CAN_RUN(td);
1431 }
1432 
1433 /*
1434  * Select threads to run.  Note that running threads still consume a
1435  * slot.
1436  */
1437 struct thread *
1438 sched_choose(void)
1439 {
1440 	struct thread *td;
1441 	struct runq *rq;
1442 
1443 	mtx_assert(&sched_lock,  MA_OWNED);
1444 #ifdef SMP
1445 	struct thread *tdcpu;
1446 
1447 	rq = &runq;
1448 	td = runq_choose_fuzz(&runq, runq_fuzz);
1449 	tdcpu = runq_choose(&runq_pcpu[PCPU_GET(cpuid)]);
1450 
1451 	if (td == NULL ||
1452 	    (tdcpu != NULL &&
1453 	     tdcpu->td_priority < td->td_priority)) {
1454 		CTR2(KTR_RUNQ, "choosing td %p from pcpu runq %d", tdcpu,
1455 		     PCPU_GET(cpuid));
1456 		td = tdcpu;
1457 		rq = &runq_pcpu[PCPU_GET(cpuid)];
1458 	} else {
1459 		CTR1(KTR_RUNQ, "choosing td_sched %p from main runq", td);
1460 	}
1461 
1462 #else
1463 	rq = &runq;
1464 	td = runq_choose(&runq);
1465 #endif
1466 
1467 	if (td) {
1468 #ifdef SMP
1469 		if (td == tdcpu)
1470 			runq_length[PCPU_GET(cpuid)]--;
1471 #endif
1472 		runq_remove(rq, td);
1473 		td->td_flags |= TDF_DIDRUN;
1474 
1475 		KASSERT(td->td_flags & TDF_INMEM,
1476 		    ("sched_choose: thread swapped out"));
1477 		return (td);
1478 	}
1479 	return (PCPU_GET(idlethread));
1480 }
1481 
1482 void
1483 sched_preempt(struct thread *td)
1484 {
1485 
1486 	SDT_PROBE2(sched, , , surrender, td, td->td_proc);
1487 	thread_lock(td);
1488 	if (td->td_critnest > 1)
1489 		td->td_owepreempt = 1;
1490 	else
1491 		mi_switch(SW_INVOL | SW_PREEMPT | SWT_PREEMPT, NULL);
1492 	thread_unlock(td);
1493 }
1494 
1495 void
1496 sched_userret(struct thread *td)
1497 {
1498 	/*
1499 	 * XXX we cheat slightly on the locking here to avoid locking in
1500 	 * the usual case.  Setting td_priority here is essentially an
1501 	 * incomplete workaround for not setting it properly elsewhere.
1502 	 * Now that some interrupt handlers are threads, not setting it
1503 	 * properly elsewhere can clobber it in the window between setting
1504 	 * it here and returning to user mode, so don't waste time setting
1505 	 * it perfectly here.
1506 	 */
1507 	KASSERT((td->td_flags & TDF_BORROWING) == 0,
1508 	    ("thread with borrowed priority returning to userland"));
1509 	if (td->td_priority != td->td_user_pri) {
1510 		thread_lock(td);
1511 		td->td_priority = td->td_user_pri;
1512 		td->td_base_pri = td->td_user_pri;
1513 		thread_unlock(td);
1514 	}
1515 }
1516 
1517 void
1518 sched_bind(struct thread *td, int cpu)
1519 {
1520 	struct td_sched *ts;
1521 
1522 	THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
1523 	KASSERT(td == curthread, ("sched_bind: can only bind curthread"));
1524 
1525 	ts = td->td_sched;
1526 
1527 	td->td_flags |= TDF_BOUND;
1528 #ifdef SMP
1529 	ts->ts_runq = &runq_pcpu[cpu];
1530 	if (PCPU_GET(cpuid) == cpu)
1531 		return;
1532 
1533 	mi_switch(SW_VOL, NULL);
1534 #endif
1535 }
1536 
1537 void
1538 sched_unbind(struct thread* td)
1539 {
1540 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1541 	KASSERT(td == curthread, ("sched_unbind: can only bind curthread"));
1542 	td->td_flags &= ~TDF_BOUND;
1543 }
1544 
1545 int
1546 sched_is_bound(struct thread *td)
1547 {
1548 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1549 	return (td->td_flags & TDF_BOUND);
1550 }
1551 
1552 void
1553 sched_relinquish(struct thread *td)
1554 {
1555 	thread_lock(td);
1556 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
1557 	thread_unlock(td);
1558 }
1559 
1560 int
1561 sched_load(void)
1562 {
1563 	return (sched_tdcnt);
1564 }
1565 
1566 int
1567 sched_sizeof_proc(void)
1568 {
1569 	return (sizeof(struct proc));
1570 }
1571 
1572 int
1573 sched_sizeof_thread(void)
1574 {
1575 	return (sizeof(struct thread) + sizeof(struct td_sched));
1576 }
1577 
1578 fixpt_t
1579 sched_pctcpu(struct thread *td)
1580 {
1581 	struct td_sched *ts;
1582 
1583 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1584 	ts = td->td_sched;
1585 	return (ts->ts_pctcpu);
1586 }
1587 
1588 void
1589 sched_tick(int cnt)
1590 {
1591 }
1592 
1593 /*
1594  * The actual idle process.
1595  */
1596 void
1597 sched_idletd(void *dummy)
1598 {
1599 	struct pcpuidlestat *stat;
1600 
1601 	THREAD_NO_SLEEPING();
1602 	stat = DPCPU_PTR(idlestat);
1603 	for (;;) {
1604 		mtx_assert(&Giant, MA_NOTOWNED);
1605 
1606 		while (sched_runnable() == 0) {
1607 			cpu_idle(stat->idlecalls + stat->oldidlecalls > 64);
1608 			stat->idlecalls++;
1609 		}
1610 
1611 		mtx_lock_spin(&sched_lock);
1612 		mi_switch(SW_VOL | SWT_IDLE, NULL);
1613 		mtx_unlock_spin(&sched_lock);
1614 	}
1615 }
1616 
1617 /*
1618  * A CPU is entering for the first time or a thread is exiting.
1619  */
1620 void
1621 sched_throw(struct thread *td)
1622 {
1623 	/*
1624 	 * Correct spinlock nesting.  The idle thread context that we are
1625 	 * borrowing was created so that it would start out with a single
1626 	 * spin lock (sched_lock) held in fork_trampoline().  Since we've
1627 	 * explicitly acquired locks in this function, the nesting count
1628 	 * is now 2 rather than 1.  Since we are nested, calling
1629 	 * spinlock_exit() will simply adjust the counts without allowing
1630 	 * spin lock using code to interrupt us.
1631 	 */
1632 	if (td == NULL) {
1633 		mtx_lock_spin(&sched_lock);
1634 		spinlock_exit();
1635 		PCPU_SET(switchtime, cpu_ticks());
1636 		PCPU_SET(switchticks, ticks);
1637 	} else {
1638 		lock_profile_release_lock(&sched_lock.lock_object);
1639 		MPASS(td->td_lock == &sched_lock);
1640 	}
1641 	mtx_assert(&sched_lock, MA_OWNED);
1642 	KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
1643 	cpu_throw(td, choosethread());	/* doesn't return */
1644 }
1645 
1646 void
1647 sched_fork_exit(struct thread *td)
1648 {
1649 
1650 	/*
1651 	 * Finish setting up thread glue so that it begins execution in a
1652 	 * non-nested critical section with sched_lock held but not recursed.
1653 	 */
1654 	td->td_oncpu = PCPU_GET(cpuid);
1655 	sched_lock.mtx_lock = (uintptr_t)td;
1656 	lock_profile_obtain_lock_success(&sched_lock.lock_object,
1657 	    0, 0, __FILE__, __LINE__);
1658 	THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED);
1659 }
1660 
1661 char *
1662 sched_tdname(struct thread *td)
1663 {
1664 #ifdef KTR
1665 	struct td_sched *ts;
1666 
1667 	ts = td->td_sched;
1668 	if (ts->ts_name[0] == '\0')
1669 		snprintf(ts->ts_name, sizeof(ts->ts_name),
1670 		    "%s tid %d", td->td_name, td->td_tid);
1671 	return (ts->ts_name);
1672 #else
1673 	return (td->td_name);
1674 #endif
1675 }
1676 
1677 #ifdef KTR
1678 void
1679 sched_clear_tdname(struct thread *td)
1680 {
1681 	struct td_sched *ts;
1682 
1683 	ts = td->td_sched;
1684 	ts->ts_name[0] = '\0';
1685 }
1686 #endif
1687 
1688 void
1689 sched_affinity(struct thread *td)
1690 {
1691 #ifdef SMP
1692 	struct td_sched *ts;
1693 	int cpu;
1694 
1695 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1696 
1697 	/*
1698 	 * Set the TSF_AFFINITY flag if there is at least one CPU this
1699 	 * thread can't run on.
1700 	 */
1701 	ts = td->td_sched;
1702 	ts->ts_flags &= ~TSF_AFFINITY;
1703 	CPU_FOREACH(cpu) {
1704 		if (!THREAD_CAN_SCHED(td, cpu)) {
1705 			ts->ts_flags |= TSF_AFFINITY;
1706 			break;
1707 		}
1708 	}
1709 
1710 	/*
1711 	 * If this thread can run on all CPUs, nothing else to do.
1712 	 */
1713 	if (!(ts->ts_flags & TSF_AFFINITY))
1714 		return;
1715 
1716 	/* Pinned threads and bound threads should be left alone. */
1717 	if (td->td_pinned != 0 || td->td_flags & TDF_BOUND)
1718 		return;
1719 
1720 	switch (td->td_state) {
1721 	case TDS_RUNQ:
1722 		/*
1723 		 * If we are on a per-CPU runqueue that is in the set,
1724 		 * then nothing needs to be done.
1725 		 */
1726 		if (ts->ts_runq != &runq &&
1727 		    THREAD_CAN_SCHED(td, ts->ts_runq - runq_pcpu))
1728 			return;
1729 
1730 		/* Put this thread on a valid per-CPU runqueue. */
1731 		sched_rem(td);
1732 		sched_add(td, SRQ_BORING);
1733 		break;
1734 	case TDS_RUNNING:
1735 		/*
1736 		 * See if our current CPU is in the set.  If not, force a
1737 		 * context switch.
1738 		 */
1739 		if (THREAD_CAN_SCHED(td, td->td_oncpu))
1740 			return;
1741 
1742 		td->td_flags |= TDF_NEEDRESCHED;
1743 		if (td != curthread)
1744 			ipi_cpu(cpu, IPI_AST);
1745 		break;
1746 	default:
1747 		break;
1748 	}
1749 #endif
1750 }
1751