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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_hwpmc_hooks.h"
39 #include "opt_kdtrace.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/cpuset.h>
44 #include <sys/kernel.h>
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sched.h>
53 #include <sys/sysctl.h>
55 #include <sys/turnstile.h>
57 #include <machine/pcb.h>
58 #include <machine/smp.h>
61 #include <sys/pmckern.h>
65 #include <sys/dtrace_bsd.h>
66 int dtrace_vtime_active;
67 dtrace_vtime_switch_func_t dtrace_vtime_switch_func;
71 * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
72 * the range 100-256 Hz (approximately).
74 #define ESTCPULIM(e) \
75 min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
76 RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
78 #define INVERSE_ESTCPU_WEIGHT (8 * smp_cpus)
80 #define INVERSE_ESTCPU_WEIGHT 8 /* 1 / (priorities per estcpu level). */
82 #define NICE_WEIGHT 1 /* Priorities per nice level. */
85 * The schedulable entity that runs a context.
86 * This is an extension to the thread structure and is tailored to
87 * the requirements of this scheduler
90 TAILQ_ENTRY(td_sched) ts_procq; /* (j/z) Run queue. */
91 struct thread *ts_thread; /* (*) Active associated thread. */
92 fixpt_t ts_pctcpu; /* (j) %cpu during p_swtime. */
93 u_char ts_rqindex; /* (j) Run queue index. */
94 int ts_cpticks; /* (j) Ticks of cpu time. */
95 int ts_slptime; /* (j) Seconds !RUNNING. */
97 struct runq *ts_runq; /* runq the thread is currently on */
100 /* flags kept in td_flags */
101 #define TDF_DIDRUN TDF_SCHED0 /* thread actually ran. */
102 #define TDF_BOUND TDF_SCHED1 /* Bound to one CPU. */
104 /* flags kept in ts_flags */
105 #define TSF_AFFINITY 0x0001 /* Has a non-"full" CPU set. */
107 #define SKE_RUNQ_PCPU(ts) \
108 ((ts)->ts_runq != 0 && (ts)->ts_runq != &runq)
110 #define THREAD_CAN_SCHED(td, cpu) \
111 CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
113 static struct td_sched td_sched0;
114 struct mtx sched_lock;
116 static int sched_tdcnt; /* Total runnable threads in the system. */
117 static int sched_quantum; /* Roundrobin scheduling quantum in ticks. */
118 #define SCHED_QUANTUM (hz / 10) /* Default sched quantum */
120 static void setup_runqs(void);
121 static void schedcpu(void);
122 static void schedcpu_thread(void);
123 static void sched_priority(struct thread *td, u_char prio);
124 static void sched_setup(void *dummy);
125 static void maybe_resched(struct thread *td);
126 static void updatepri(struct thread *td);
127 static void resetpriority(struct thread *td);
128 static void resetpriority_thread(struct thread *td);
130 static int sched_pickcpu(struct thread *td);
131 static int forward_wakeup(int cpunum);
132 static void kick_other_cpu(int pri, int cpuid);
135 static struct kproc_desc sched_kp = {
140 SYSINIT(schedcpu, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, kproc_start,
142 SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
147 static struct runq runq;
153 static struct runq runq_pcpu[MAXCPU];
154 long runq_length[MAXCPU];
163 for (i = 0; i < MAXCPU; ++i)
164 runq_init(&runq_pcpu[i]);
171 sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
175 new_val = sched_quantum * tick;
176 error = sysctl_handle_int(oidp, &new_val, 0, req);
177 if (error != 0 || req->newptr == NULL)
181 sched_quantum = new_val / tick;
182 hogticks = 2 * sched_quantum;
186 SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RD, 0, "Scheduler");
188 SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "4BSD", 0,
191 SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, CTLTYPE_INT | CTLFLAG_RW,
192 0, sizeof sched_quantum, sysctl_kern_quantum, "I",
193 "Roundrobin scheduling quantum in microseconds");
196 /* Enable forwarding of wakeups to all other cpus */
197 SYSCTL_NODE(_kern_sched, OID_AUTO, ipiwakeup, CTLFLAG_RD, NULL, "Kernel SMP");
199 static int forward_wakeup_enabled = 1;
200 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, enabled, CTLFLAG_RW,
201 &forward_wakeup_enabled, 0,
202 "Forwarding of wakeup to idle CPUs");
204 static int forward_wakeups_requested = 0;
205 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, requested, CTLFLAG_RD,
206 &forward_wakeups_requested, 0,
207 "Requests for Forwarding of wakeup to idle CPUs");
209 static int forward_wakeups_delivered = 0;
210 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, delivered, CTLFLAG_RD,
211 &forward_wakeups_delivered, 0,
212 "Completed Forwarding of wakeup to idle CPUs");
214 static int forward_wakeup_use_mask = 1;
215 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, usemask, CTLFLAG_RW,
216 &forward_wakeup_use_mask, 0,
217 "Use the mask of idle cpus");
219 static int forward_wakeup_use_loop = 0;
220 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, useloop, CTLFLAG_RW,
221 &forward_wakeup_use_loop, 0,
222 "Use a loop to find idle cpus");
224 static int forward_wakeup_use_single = 0;
225 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, onecpu, CTLFLAG_RW,
226 &forward_wakeup_use_single, 0,
227 "Only signal one idle cpu");
229 static int forward_wakeup_use_htt = 0;
230 SYSCTL_INT(_kern_sched_ipiwakeup, OID_AUTO, htt2, CTLFLAG_RW,
231 &forward_wakeup_use_htt, 0,
236 static int sched_followon = 0;
237 SYSCTL_INT(_kern_sched, OID_AUTO, followon, CTLFLAG_RW,
239 "allow threads to share a quantum");
246 CTR1(KTR_SCHED, "global load: %d", sched_tdcnt);
253 CTR1(KTR_SCHED, "global load: %d", sched_tdcnt);
256 * Arrange to reschedule if necessary, taking the priorities and
257 * schedulers into account.
260 maybe_resched(struct thread *td)
263 THREAD_LOCK_ASSERT(td, MA_OWNED);
264 if (td->td_priority < curthread->td_priority)
265 curthread->td_flags |= TDF_NEEDRESCHED;
269 * Constants for digital decay and forget:
270 * 90% of (td_estcpu) usage in 5 * loadav time
271 * 95% of (ts_pctcpu) usage in 60 seconds (load insensitive)
272 * Note that, as ps(1) mentions, this can let percentages
273 * total over 100% (I've seen 137.9% for 3 processes).
275 * Note that schedclock() updates td_estcpu and p_cpticks asynchronously.
277 * We wish to decay away 90% of td_estcpu in (5 * loadavg) seconds.
278 * That is, the system wants to compute a value of decay such
279 * that the following for loop:
280 * for (i = 0; i < (5 * loadavg); i++)
281 * td_estcpu *= decay;
284 * for all values of loadavg:
286 * Mathematically this loop can be expressed by saying:
287 * decay ** (5 * loadavg) ~= .1
289 * The system computes decay as:
290 * decay = (2 * loadavg) / (2 * loadavg + 1)
292 * We wish to prove that the system's computation of decay
293 * will always fulfill the equation:
294 * decay ** (5 * loadavg) ~= .1
296 * If we compute b as:
299 * decay = b / (b + 1)
301 * We now need to prove two things:
302 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
303 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
306 * For x close to zero, exp(x) =~ 1 + x, since
307 * exp(x) = 0! + x**1/1! + x**2/2! + ... .
308 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
309 * For x close to zero, ln(1+x) =~ x, since
310 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
311 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
315 * Solve (factor)**(power) =~ .1 given power (5*loadav):
316 * solving for factor,
317 * ln(factor) =~ (-2.30/5*loadav), or
318 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
319 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
322 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
324 * power*ln(b/(b+1)) =~ -2.30, or
325 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
327 * Actual power values for the implemented algorithm are as follows:
329 * power: 5.68 10.32 14.94 19.55
332 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
333 #define loadfactor(loadav) (2 * (loadav))
334 #define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE))
336 /* decay 95% of `ts_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
337 static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
338 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
341 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
342 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
343 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
345 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
346 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
348 * If you don't want to bother with the faster/more-accurate formula, you
349 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
350 * (more general) method of calculating the %age of CPU used by a process.
352 #define CCPU_SHIFT 11
355 * Recompute process priorities, every hz ticks.
356 * MP-safe, called without the Giant mutex.
362 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
366 int awake, realstathz;
368 realstathz = stathz ? stathz : hz;
369 sx_slock(&allproc_lock);
370 FOREACH_PROC_IN_SYSTEM(p) {
372 FOREACH_THREAD_IN_PROC(p, td) {
377 * Increment sleep time (if sleeping). We
378 * ignore overflow, as above.
381 * The td_sched slptimes are not touched in wakeup
382 * because the thread may not HAVE everything in
383 * memory? XXX I think this is out of date.
385 if (TD_ON_RUNQ(td)) {
387 td->td_flags &= ~TDF_DIDRUN;
388 } else if (TD_IS_RUNNING(td)) {
390 /* Do not clear TDF_DIDRUN */
391 } else if (td->td_flags & TDF_DIDRUN) {
393 td->td_flags &= ~TDF_DIDRUN;
397 * ts_pctcpu is only for ps and ttyinfo().
398 * Do it per td_sched, and add them up at the end?
401 ts->ts_pctcpu = (ts->ts_pctcpu * ccpu) >> FSHIFT;
403 * If the td_sched has been idle the entire second,
404 * stop recalculating its priority until
407 if (ts->ts_cpticks != 0) {
408 #if (FSHIFT >= CCPU_SHIFT)
409 ts->ts_pctcpu += (realstathz == 100)
410 ? ((fixpt_t) ts->ts_cpticks) <<
411 (FSHIFT - CCPU_SHIFT) :
412 100 * (((fixpt_t) ts->ts_cpticks)
413 << (FSHIFT - CCPU_SHIFT)) / realstathz;
415 ts->ts_pctcpu += ((FSCALE - ccpu) *
417 FSCALE / realstathz)) >> FSHIFT;
422 * If there are ANY running threads in this process,
423 * then don't count it as sleeping.
424 * XXX: this is broken.
427 if (ts->ts_slptime > 1) {
429 * In an ideal world, this should not
430 * happen, because whoever woke us
431 * up from the long sleep should have
432 * unwound the slptime and reset our
433 * priority before we run at the stale
434 * priority. Should KASSERT at some
435 * point when all the cases are fixed.
442 if (ts->ts_slptime > 1) {
446 td->td_estcpu = decay_cpu(loadfac, td->td_estcpu);
448 resetpriority_thread(td);
453 sx_sunlock(&allproc_lock);
457 * Main loop for a kthread that executes schedcpu once a second.
460 schedcpu_thread(void)
470 * Recalculate the priority of a process after it has slept for a while.
471 * For all load averages >= 1 and max td_estcpu of 255, sleeping for at
472 * least six times the loadfactor will decay td_estcpu to zero.
475 updatepri(struct thread *td)
482 loadfac = loadfactor(averunnable.ldavg[0]);
483 if (ts->ts_slptime > 5 * loadfac)
486 newcpu = td->td_estcpu;
487 ts->ts_slptime--; /* was incremented in schedcpu() */
488 while (newcpu && --ts->ts_slptime)
489 newcpu = decay_cpu(loadfac, newcpu);
490 td->td_estcpu = newcpu;
495 * Compute the priority of a process when running in user mode.
496 * Arrange to reschedule if the resulting priority is better
497 * than that of the current process.
500 resetpriority(struct thread *td)
502 register unsigned int newpriority;
504 if (td->td_pri_class == PRI_TIMESHARE) {
505 newpriority = PUSER + td->td_estcpu / INVERSE_ESTCPU_WEIGHT +
506 NICE_WEIGHT * (td->td_proc->p_nice - PRIO_MIN);
507 newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
509 sched_user_prio(td, newpriority);
514 * Update the thread's priority when the associated process's user
518 resetpriority_thread(struct thread *td)
521 /* Only change threads with a time sharing user priority. */
522 if (td->td_priority < PRI_MIN_TIMESHARE ||
523 td->td_priority > PRI_MAX_TIMESHARE)
526 /* XXX the whole needresched thing is broken, but not silly. */
529 sched_prio(td, td->td_user_pri);
534 sched_setup(void *dummy)
538 if (sched_quantum == 0)
539 sched_quantum = SCHED_QUANTUM;
540 hogticks = 2 * sched_quantum;
542 /* Account for thread0. */
546 /* External interfaces start here */
549 * Very early in the boot some setup of scheduler-specific
550 * parts of proc0 and of some scheduler resources needs to be done.
558 * Set up the scheduler specific parts of proc0.
560 proc0.p_sched = NULL; /* XXX */
561 thread0.td_sched = &td_sched0;
562 thread0.td_lock = &sched_lock;
563 td_sched0.ts_thread = &thread0;
564 mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
571 return runq_check(&runq) + runq_check(&runq_pcpu[PCPU_GET(cpuid)]);
573 return runq_check(&runq);
578 sched_rr_interval(void)
580 if (sched_quantum == 0)
581 sched_quantum = SCHED_QUANTUM;
582 return (sched_quantum);
586 * We adjust the priority of the current process. The priority of
587 * a process gets worse as it accumulates CPU time. The cpu usage
588 * estimator (td_estcpu) is increased here. resetpriority() will
589 * compute a different priority each time td_estcpu increases by
590 * INVERSE_ESTCPU_WEIGHT
591 * (until MAXPRI is reached). The cpu usage estimator ramps up
592 * quite quickly when the process is running (linearly), and decays
593 * away exponentially, at a rate which is proportionally slower when
594 * the system is busy. The basic principle is that the system will
595 * 90% forget that the process used a lot of CPU time in 5 * loadav
596 * seconds. This causes the system to favor processes which haven't
597 * run much recently, and to round-robin among other processes.
600 sched_clock(struct thread *td)
604 THREAD_LOCK_ASSERT(td, MA_OWNED);
608 td->td_estcpu = ESTCPULIM(td->td_estcpu + 1);
609 if ((td->td_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
611 resetpriority_thread(td);
615 * Force a context switch if the current thread has used up a full
616 * quantum (default quantum is 100ms).
618 if (!TD_IS_IDLETHREAD(td) &&
619 ticks - PCPU_GET(switchticks) >= sched_quantum)
620 td->td_flags |= TDF_NEEDRESCHED;
624 * Charge child's scheduling CPU usage to parent.
627 sched_exit(struct proc *p, struct thread *td)
630 CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d",
631 td, td->td_proc->p_comm, td->td_priority);
632 PROC_SLOCK_ASSERT(p, MA_OWNED);
633 sched_exit_thread(FIRST_THREAD_IN_PROC(p), td);
637 sched_exit_thread(struct thread *td, struct thread *child)
640 CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d",
641 child, child->td_proc->p_comm, child->td_priority);
643 td->td_estcpu = ESTCPULIM(td->td_estcpu + child->td_estcpu);
645 mtx_lock_spin(&sched_lock);
646 if ((child->td_proc->p_flag & P_NOLOAD) == 0)
648 mtx_unlock_spin(&sched_lock);
652 sched_fork(struct thread *td, struct thread *childtd)
654 sched_fork_thread(td, childtd);
658 sched_fork_thread(struct thread *td, struct thread *childtd)
660 childtd->td_estcpu = td->td_estcpu;
661 childtd->td_lock = &sched_lock;
662 childtd->td_cpuset = cpuset_ref(td->td_cpuset);
663 sched_newthread(childtd);
664 childtd->td_sched->ts_flags |= (td->td_sched->ts_flags & TSF_AFFINITY);
668 sched_nice(struct proc *p, int nice)
672 PROC_LOCK_ASSERT(p, MA_OWNED);
673 PROC_SLOCK_ASSERT(p, MA_OWNED);
675 FOREACH_THREAD_IN_PROC(p, td) {
678 resetpriority_thread(td);
684 sched_class(struct thread *td, int class)
686 THREAD_LOCK_ASSERT(td, MA_OWNED);
687 td->td_pri_class = class;
691 * Adjust the priority of a thread.
694 sched_priority(struct thread *td, u_char prio)
696 CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)",
697 td, td->td_proc->p_comm, td->td_priority, prio, curthread,
698 curthread->td_proc->p_comm);
700 THREAD_LOCK_ASSERT(td, MA_OWNED);
701 if (td->td_priority == prio)
703 td->td_priority = prio;
704 if (TD_ON_RUNQ(td) &&
705 td->td_sched->ts_rqindex != (prio / RQ_PPQ)) {
707 sched_add(td, SRQ_BORING);
712 * Update a thread's priority when it is lent another thread's
716 sched_lend_prio(struct thread *td, u_char prio)
719 td->td_flags |= TDF_BORROWING;
720 sched_priority(td, prio);
724 * Restore a thread's priority when priority propagation is
725 * over. The prio argument is the minimum priority the thread
726 * needs to have to satisfy other possible priority lending
727 * requests. If the thread's regulary priority is less
728 * important than prio the thread will keep a priority boost
732 sched_unlend_prio(struct thread *td, u_char prio)
736 if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
737 td->td_base_pri <= PRI_MAX_TIMESHARE)
738 base_pri = td->td_user_pri;
740 base_pri = td->td_base_pri;
741 if (prio >= base_pri) {
742 td->td_flags &= ~TDF_BORROWING;
743 sched_prio(td, base_pri);
745 sched_lend_prio(td, prio);
749 sched_prio(struct thread *td, u_char prio)
753 /* First, update the base priority. */
754 td->td_base_pri = prio;
757 * If the thread is borrowing another thread's priority, don't ever
758 * lower the priority.
760 if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
763 /* Change the real priority. */
764 oldprio = td->td_priority;
765 sched_priority(td, prio);
768 * If the thread is on a turnstile, then let the turnstile update
771 if (TD_ON_LOCK(td) && oldprio != prio)
772 turnstile_adjust(td, oldprio);
776 sched_user_prio(struct thread *td, u_char prio)
780 THREAD_LOCK_ASSERT(td, MA_OWNED);
781 td->td_base_user_pri = prio;
782 if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
784 oldprio = td->td_user_pri;
785 td->td_user_pri = prio;
789 sched_lend_user_prio(struct thread *td, u_char prio)
793 THREAD_LOCK_ASSERT(td, MA_OWNED);
794 td->td_flags |= TDF_UBORROWING;
796 oldprio = td->td_user_pri;
797 td->td_user_pri = prio;
801 sched_unlend_user_prio(struct thread *td, u_char prio)
805 THREAD_LOCK_ASSERT(td, MA_OWNED);
806 base_pri = td->td_base_user_pri;
807 if (prio >= base_pri) {
808 td->td_flags &= ~TDF_UBORROWING;
809 sched_user_prio(td, base_pri);
811 sched_lend_user_prio(td, prio);
816 sched_sleep(struct thread *td)
819 THREAD_LOCK_ASSERT(td, MA_OWNED);
820 td->td_slptick = ticks;
821 td->td_sched->ts_slptime = 0;
825 sched_switch(struct thread *td, struct thread *newtd, int flags)
833 THREAD_LOCK_ASSERT(td, MA_OWNED);
836 * Switch to the sched lock to fix things up and pick
839 if (td->td_lock != &sched_lock) {
840 mtx_lock_spin(&sched_lock);
844 if ((p->p_flag & P_NOLOAD) == 0)
848 newtd->td_flags |= (td->td_flags & TDF_NEEDRESCHED);
850 td->td_lastcpu = td->td_oncpu;
851 td->td_flags &= ~TDF_NEEDRESCHED;
852 td->td_owepreempt = 0;
853 td->td_oncpu = NOCPU;
856 * At the last moment, if this thread is still marked RUNNING,
857 * then put it back on the run queue as it has not been suspended
858 * or stopped or any thing else similar. We never put the idle
859 * threads on the run queue, however.
861 if (td->td_flags & TDF_IDLETD) {
864 idle_cpus_mask &= ~PCPU_GET(cpumask);
867 if (TD_IS_RUNNING(td)) {
868 /* Put us back on the run queue. */
869 sched_add(td, (flags & SW_PREEMPT) ?
870 SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
871 SRQ_OURSELF|SRQ_YIELDING);
876 * The thread we are about to run needs to be counted
877 * as if it had been added to the run queue and selected.
883 KASSERT((newtd->td_inhibitors == 0),
884 ("trying to run inhibited thread"));
885 newtd->td_flags |= TDF_DIDRUN;
886 TD_SET_RUNNING(newtd);
887 if ((newtd->td_proc->p_flag & P_NOLOAD) == 0)
890 newtd = choosethread();
892 MPASS(newtd->td_lock == &sched_lock);
896 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
897 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
902 * If DTrace has set the active vtime enum to anything
903 * other than INACTIVE (0), then it should have set the
906 if (dtrace_vtime_active)
907 (*dtrace_vtime_switch_func)(newtd);
910 cpu_switch(td, newtd, td->td_lock);
912 * Where am I? What year is it?
913 * We are in the same thread that went to sleep above,
914 * but any amount of time may have passed. All our context
915 * will still be available as will local variables.
916 * PCPU values however may have changed as we may have
917 * changed CPU so don't trust cached values of them.
918 * New threads will go to fork_exit() instead of here
919 * so if you change things here you may need to change
922 * If the thread above was exiting it will never wake
923 * up again here, so either it has saved everything it
924 * needed to, or the thread_wait() or wait() will
928 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
929 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
934 if (td->td_flags & TDF_IDLETD)
935 idle_cpus_mask |= PCPU_GET(cpumask);
937 sched_lock.mtx_lock = (uintptr_t)td;
938 td->td_oncpu = PCPU_GET(cpuid);
939 MPASS(td->td_lock == &sched_lock);
943 sched_wakeup(struct thread *td)
947 THREAD_LOCK_ASSERT(td, MA_OWNED);
949 if (ts->ts_slptime > 1) {
953 td->td_slptick = ticks;
955 sched_add(td, SRQ_BORING);
960 forward_wakeup(int cpunum)
963 cpumask_t dontuse, id, map, map2, map3, me;
965 mtx_assert(&sched_lock, MA_OWNED);
967 CTR0(KTR_RUNQ, "forward_wakeup()");
969 if ((!forward_wakeup_enabled) ||
970 (forward_wakeup_use_mask == 0 && forward_wakeup_use_loop == 0))
972 if (!smp_started || cold || panicstr)
975 forward_wakeups_requested++;
978 * Check the idle mask we received against what we calculated
979 * before in the old version.
981 me = PCPU_GET(cpumask);
983 /* Don't bother if we should be doing it ourself. */
984 if ((me & idle_cpus_mask) && (cpunum == NOCPU || me == (1 << cpunum)))
987 dontuse = me | stopped_cpus | hlt_cpus_mask;
989 if (forward_wakeup_use_loop) {
990 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
992 if ((id & dontuse) == 0 &&
993 pc->pc_curthread == pc->pc_idlethread) {
999 if (forward_wakeup_use_mask) {
1001 map = idle_cpus_mask & ~dontuse;
1003 /* If they are both on, compare and use loop if different. */
1004 if (forward_wakeup_use_loop) {
1006 printf("map (%02X) != map3 (%02X)\n", map,
1015 /* If we only allow a specific CPU, then mask off all the others. */
1016 if (cpunum != NOCPU) {
1017 KASSERT((cpunum <= mp_maxcpus),("forward_wakeup: bad cpunum."));
1018 map &= (1 << cpunum);
1020 /* Try choose an idle die. */
1021 if (forward_wakeup_use_htt) {
1022 map2 = (map & (map >> 1)) & 0x5555;
1028 /* Set only one bit. */
1029 if (forward_wakeup_use_single) {
1030 map = map & ((~map) + 1);
1034 forward_wakeups_delivered++;
1035 ipi_selected(map, IPI_AST);
1038 if (cpunum == NOCPU)
1039 printf("forward_wakeup: Idle processor not found\n");
1044 kick_other_cpu(int pri, int cpuid)
1049 pcpu = pcpu_find(cpuid);
1050 if (idle_cpus_mask & pcpu->pc_cpumask) {
1051 forward_wakeups_delivered++;
1052 ipi_selected(pcpu->pc_cpumask, IPI_AST);
1056 cpri = pcpu->pc_curthread->td_priority;
1060 #if defined(IPI_PREEMPTION) && defined(PREEMPTION)
1061 #if !defined(FULL_PREEMPTION)
1062 if (pri <= PRI_MAX_ITHD)
1063 #endif /* ! FULL_PREEMPTION */
1065 ipi_selected(pcpu->pc_cpumask, IPI_PREEMPT);
1068 #endif /* defined(IPI_PREEMPTION) && defined(PREEMPTION) */
1070 pcpu->pc_curthread->td_flags |= TDF_NEEDRESCHED;
1071 ipi_selected(pcpu->pc_cpumask, IPI_AST);
1078 sched_pickcpu(struct thread *td)
1082 mtx_assert(&sched_lock, MA_OWNED);
1084 if (THREAD_CAN_SCHED(td, td->td_lastcpu))
1085 best = td->td_lastcpu;
1088 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1089 if (CPU_ABSENT(cpu))
1091 if (!THREAD_CAN_SCHED(td, cpu))
1096 else if (runq_length[cpu] < runq_length[best])
1099 KASSERT(best != NOCPU, ("no valid CPUs"));
1106 sched_add(struct thread *td, int flags)
1109 struct td_sched *ts;
1115 THREAD_LOCK_ASSERT(td, MA_OWNED);
1116 KASSERT((td->td_inhibitors == 0),
1117 ("sched_add: trying to run inhibited thread"));
1118 KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1119 ("sched_add: bad thread state"));
1120 KASSERT(td->td_flags & TDF_INMEM,
1121 ("sched_add: thread swapped out"));
1122 CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
1123 td, td->td_proc->p_comm, td->td_priority, curthread,
1124 curthread->td_proc->p_comm);
1127 * Now that the thread is moving to the run-queue, set the lock
1128 * to the scheduler's lock.
1130 if (td->td_lock != &sched_lock) {
1131 mtx_lock_spin(&sched_lock);
1132 thread_lock_set(td, &sched_lock);
1136 if (td->td_pinned != 0) {
1137 cpu = td->td_lastcpu;
1138 ts->ts_runq = &runq_pcpu[cpu];
1141 "sched_add: Put td_sched:%p(td:%p) on cpu%d runq", ts, td,
1143 } else if (td->td_flags & TDF_BOUND) {
1144 /* Find CPU from bound runq. */
1145 KASSERT(SKE_RUNQ_PCPU(ts),
1146 ("sched_add: bound td_sched not on cpu runq"));
1147 cpu = ts->ts_runq - &runq_pcpu[0];
1150 "sched_add: Put td_sched:%p(td:%p) on cpu%d runq", ts, td,
1152 } else if (ts->ts_flags & TSF_AFFINITY) {
1153 /* Find a valid CPU for our cpuset */
1154 cpu = sched_pickcpu(td);
1155 ts->ts_runq = &runq_pcpu[cpu];
1158 "sched_add: Put td_sched:%p(td:%p) on cpu%d runq", ts, td,
1162 "sched_add: adding td_sched:%p (td:%p) to gbl runq", ts,
1165 ts->ts_runq = &runq;
1168 if (single_cpu && (cpu != PCPU_GET(cpuid))) {
1169 kick_other_cpu(td->td_priority, cpu);
1172 cpumask_t me = PCPU_GET(cpumask);
1173 cpumask_t idle = idle_cpus_mask & me;
1175 if (!idle && ((flags & SRQ_INTR) == 0) &&
1176 (idle_cpus_mask & ~(hlt_cpus_mask | me)))
1177 forwarded = forward_wakeup(cpu);
1181 if ((flags & SRQ_YIELDING) == 0 && maybe_preempt(td))
1188 if ((td->td_proc->p_flag & P_NOLOAD) == 0)
1190 runq_add(ts->ts_runq, ts, flags);
1196 struct td_sched *ts;
1199 THREAD_LOCK_ASSERT(td, MA_OWNED);
1200 KASSERT((td->td_inhibitors == 0),
1201 ("sched_add: trying to run inhibited thread"));
1202 KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1203 ("sched_add: bad thread state"));
1204 KASSERT(td->td_flags & TDF_INMEM,
1205 ("sched_add: thread swapped out"));
1206 CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
1207 td, td->td_proc->p_comm, td->td_priority, curthread,
1208 curthread->td_proc->p_comm);
1211 * Now that the thread is moving to the run-queue, set the lock
1212 * to the scheduler's lock.
1214 if (td->td_lock != &sched_lock) {
1215 mtx_lock_spin(&sched_lock);
1216 thread_lock_set(td, &sched_lock);
1219 CTR2(KTR_RUNQ, "sched_add: adding td_sched:%p (td:%p) to runq", ts, td);
1220 ts->ts_runq = &runq;
1223 * If we are yielding (on the way out anyhow) or the thread
1224 * being saved is US, then don't try be smart about preemption
1225 * or kicking off another CPU as it won't help and may hinder.
1226 * In the YIEDLING case, we are about to run whoever is being
1227 * put in the queue anyhow, and in the OURSELF case, we are
1228 * puting ourself on the run queue which also only happens
1229 * when we are about to yield.
1231 if ((flags & SRQ_YIELDING) == 0) {
1232 if (maybe_preempt(td))
1235 if ((td->td_proc->p_flag & P_NOLOAD) == 0)
1237 runq_add(ts->ts_runq, ts, flags);
1243 sched_rem(struct thread *td)
1245 struct td_sched *ts;
1248 KASSERT(td->td_flags & TDF_INMEM,
1249 ("sched_rem: thread swapped out"));
1250 KASSERT(TD_ON_RUNQ(td),
1251 ("sched_rem: thread not on run queue"));
1252 mtx_assert(&sched_lock, MA_OWNED);
1253 CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)",
1254 td, td->td_proc->p_comm, td->td_priority, curthread,
1255 curthread->td_proc->p_comm);
1257 if ((td->td_proc->p_flag & P_NOLOAD) == 0)
1260 if (ts->ts_runq != &runq)
1261 runq_length[ts->ts_runq - runq_pcpu]--;
1263 runq_remove(ts->ts_runq, ts);
1268 * Select threads to run. Note that running threads still consume a
1274 struct td_sched *ts;
1277 mtx_assert(&sched_lock, MA_OWNED);
1279 struct td_sched *kecpu;
1282 ts = runq_choose(&runq);
1283 kecpu = runq_choose(&runq_pcpu[PCPU_GET(cpuid)]);
1287 kecpu->ts_thread->td_priority < ts->ts_thread->td_priority)) {
1288 CTR2(KTR_RUNQ, "choosing td_sched %p from pcpu runq %d", kecpu,
1291 rq = &runq_pcpu[PCPU_GET(cpuid)];
1293 CTR1(KTR_RUNQ, "choosing td_sched %p from main runq", ts);
1298 ts = runq_choose(&runq);
1304 runq_length[PCPU_GET(cpuid)]--;
1306 runq_remove(rq, ts);
1307 ts->ts_thread->td_flags |= TDF_DIDRUN;
1309 KASSERT(ts->ts_thread->td_flags & TDF_INMEM,
1310 ("sched_choose: thread swapped out"));
1311 return (ts->ts_thread);
1313 return (PCPU_GET(idlethread));
1317 sched_userret(struct thread *td)
1320 * XXX we cheat slightly on the locking here to avoid locking in
1321 * the usual case. Setting td_priority here is essentially an
1322 * incomplete workaround for not setting it properly elsewhere.
1323 * Now that some interrupt handlers are threads, not setting it
1324 * properly elsewhere can clobber it in the window between setting
1325 * it here and returning to user mode, so don't waste time setting
1326 * it perfectly here.
1328 KASSERT((td->td_flags & TDF_BORROWING) == 0,
1329 ("thread with borrowed priority returning to userland"));
1330 if (td->td_priority != td->td_user_pri) {
1332 td->td_priority = td->td_user_pri;
1333 td->td_base_pri = td->td_user_pri;
1339 sched_bind(struct thread *td, int cpu)
1341 struct td_sched *ts;
1343 THREAD_LOCK_ASSERT(td, MA_OWNED);
1344 KASSERT(TD_IS_RUNNING(td),
1345 ("sched_bind: cannot bind non-running thread"));
1349 td->td_flags |= TDF_BOUND;
1351 ts->ts_runq = &runq_pcpu[cpu];
1352 if (PCPU_GET(cpuid) == cpu)
1355 mi_switch(SW_VOL, NULL);
1360 sched_unbind(struct thread* td)
1362 THREAD_LOCK_ASSERT(td, MA_OWNED);
1363 td->td_flags &= ~TDF_BOUND;
1367 sched_is_bound(struct thread *td)
1369 THREAD_LOCK_ASSERT(td, MA_OWNED);
1370 return (td->td_flags & TDF_BOUND);
1374 sched_relinquish(struct thread *td)
1377 SCHED_STAT_INC(switch_relinquish);
1378 mi_switch(SW_VOL, NULL);
1385 return (sched_tdcnt);
1389 sched_sizeof_proc(void)
1391 return (sizeof(struct proc));
1395 sched_sizeof_thread(void)
1397 return (sizeof(struct thread) + sizeof(struct td_sched));
1401 sched_pctcpu(struct thread *td)
1403 struct td_sched *ts;
1406 return (ts->ts_pctcpu);
1415 * The actual idle process.
1418 sched_idletd(void *dummy)
1426 mtx_assert(&Giant, MA_NOTOWNED);
1428 while (sched_runnable() == 0)
1431 mtx_lock_spin(&sched_lock);
1432 mi_switch(SW_VOL, NULL);
1433 mtx_unlock_spin(&sched_lock);
1438 * A CPU is entering for the first time or a thread is exiting.
1441 sched_throw(struct thread *td)
1444 * Correct spinlock nesting. The idle thread context that we are
1445 * borrowing was created so that it would start out with a single
1446 * spin lock (sched_lock) held in fork_trampoline(). Since we've
1447 * explicitly acquired locks in this function, the nesting count
1448 * is now 2 rather than 1. Since we are nested, calling
1449 * spinlock_exit() will simply adjust the counts without allowing
1450 * spin lock using code to interrupt us.
1453 mtx_lock_spin(&sched_lock);
1456 MPASS(td->td_lock == &sched_lock);
1458 mtx_assert(&sched_lock, MA_OWNED);
1459 KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
1460 PCPU_SET(switchtime, cpu_ticks());
1461 PCPU_SET(switchticks, ticks);
1462 cpu_throw(td, choosethread()); /* doesn't return */
1466 sched_fork_exit(struct thread *td)
1470 * Finish setting up thread glue so that it begins execution in a
1471 * non-nested critical section with sched_lock held but not recursed.
1473 td->td_oncpu = PCPU_GET(cpuid);
1474 sched_lock.mtx_lock = (uintptr_t)td;
1475 THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED);
1479 sched_affinity(struct thread *td)
1482 struct td_sched *ts;
1485 THREAD_LOCK_ASSERT(td, MA_OWNED);
1488 * Set the TSF_AFFINITY flag if there is at least one CPU this
1489 * thread can't run on.
1492 ts->ts_flags &= ~TSF_AFFINITY;
1493 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1494 if (CPU_ABSENT(cpu))
1496 if (!THREAD_CAN_SCHED(td, cpu)) {
1497 ts->ts_flags |= TSF_AFFINITY;
1503 * If this thread can run on all CPUs, nothing else to do.
1505 if (!(ts->ts_flags & TSF_AFFINITY))
1508 /* Pinned threads and bound threads should be left alone. */
1509 if (td->td_pinned != 0 || td->td_flags & TDF_BOUND)
1512 switch (td->td_state) {
1515 * If we are on a per-CPU runqueue that is in the set,
1516 * then nothing needs to be done.
1518 if (ts->ts_runq != &runq &&
1519 THREAD_CAN_SCHED(td, ts->ts_runq - runq_pcpu))
1522 /* Put this thread on a valid per-CPU runqueue. */
1524 sched_add(td, SRQ_BORING);
1528 * See if our current CPU is in the set. If not, force a
1531 if (THREAD_CAN_SCHED(td, td->td_oncpu))
1534 td->td_flags |= TDF_NEEDRESCHED;
1535 if (td != curthread)
1536 ipi_selected(1 << cpu, IPI_AST);
1544 #define KERN_SWITCH_INCLUDE 1
1545 #include "kern/kern_switch.c"