2 * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * This file implements the ULE scheduler. ULE supports independent CPU
29 * run queues and fine grain locking. It has superior interactive
30 * performance under load even on uni-processor systems.
33 * ULE is the last three letters in schedule. It owes its name to a
34 * generic user created for a scheduling system by Paul Mikesell at
35 * Isilon Systems and a general lack of creativity on the part of the author.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_hwpmc_hooks.h"
42 #include "opt_sched.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
50 #include <sys/mutex.h>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysproto.h>
59 #include <sys/turnstile.h>
61 #include <sys/vmmeter.h>
62 #include <sys/cpuset.h>
65 #include <sys/ktrace.h>
69 #include <sys/pmckern.h>
72 #include <machine/cpu.h>
73 #include <machine/smp.h>
75 #if !defined(__i386__) && !defined(__amd64__) && !defined(__powerpc__) && !defined(__arm__)
76 #error "This architecture is not currently compatible with ULE"
82 * Thread scheduler specific section. All fields are protected
86 TAILQ_ENTRY(td_sched) ts_procq; /* Run queue. */
87 struct thread *ts_thread; /* Active associated thread. */
88 struct runq *ts_runq; /* Run-queue we're queued on. */
89 short ts_flags; /* TSF_* flags. */
90 u_char ts_rqindex; /* Run queue index. */
91 u_char ts_cpu; /* CPU that we have affinity for. */
92 int ts_rltick; /* Real last tick, for affinity. */
93 int ts_slice; /* Ticks of slice remaining. */
94 u_int ts_slptime; /* Number of ticks we vol. slept */
95 u_int ts_runtime; /* Number of ticks we were running */
96 int ts_ltick; /* Last tick that we were running on */
97 int ts_ftick; /* First tick that we were running on */
98 int ts_ticks; /* Tick count */
100 /* flags kept in ts_flags */
101 #define TSF_BOUND 0x0001 /* Thread can not migrate. */
102 #define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */
104 static struct td_sched td_sched0;
106 #define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0)
107 #define THREAD_CAN_SCHED(td, cpu) \
108 CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
111 * Cpu percentage computation macros and defines.
113 * SCHED_TICK_SECS: Number of seconds to average the cpu usage across.
114 * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across.
115 * SCHED_TICK_MAX: Maximum number of ticks before scaling back.
116 * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results.
117 * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count.
118 * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks.
120 #define SCHED_TICK_SECS 10
121 #define SCHED_TICK_TARG (hz * SCHED_TICK_SECS)
122 #define SCHED_TICK_MAX (SCHED_TICK_TARG + hz)
123 #define SCHED_TICK_SHIFT 10
124 #define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT)
125 #define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz))
128 * These macros determine priorities for non-interactive threads. They are
129 * assigned a priority based on their recent cpu utilization as expressed
130 * by the ratio of ticks to the tick total. NHALF priorities at the start
131 * and end of the MIN to MAX timeshare range are only reachable with negative
132 * or positive nice respectively.
134 * PRI_RANGE: Priority range for utilization dependent priorities.
135 * PRI_NRESV: Number of nice values.
136 * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total.
137 * PRI_NICE: Determines the part of the priority inherited from nice.
139 #define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN)
140 #define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2)
141 #define SCHED_PRI_MIN (PRI_MIN_TIMESHARE + SCHED_PRI_NHALF)
142 #define SCHED_PRI_MAX (PRI_MAX_TIMESHARE - SCHED_PRI_NHALF)
143 #define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN)
144 #define SCHED_PRI_TICKS(ts) \
145 (SCHED_TICK_HZ((ts)) / \
146 (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
147 #define SCHED_PRI_NICE(nice) (nice)
150 * These determine the interactivity of a process. Interactivity differs from
151 * cpu utilization in that it expresses the voluntary time slept vs time ran
152 * while cpu utilization includes all time not running. This more accurately
153 * models the intent of the thread.
155 * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate
156 * before throttling back.
157 * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time.
158 * INTERACT_MAX: Maximum interactivity value. Smaller is better.
159 * INTERACT_THRESH: Threshhold for placement on the current runq.
161 #define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT)
162 #define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT)
163 #define SCHED_INTERACT_MAX (100)
164 #define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2)
165 #define SCHED_INTERACT_THRESH (30)
168 * tickincr: Converts a stathz tick into a hz domain scaled by
169 * the shift factor. Without the shift the error rate
170 * due to rounding would be unacceptably high.
171 * realstathz: stathz is sometimes 0 and run off of hz.
172 * sched_slice: Runtime of each thread before rescheduling.
173 * preempt_thresh: Priority threshold for preemption and remote IPIs.
175 static int sched_interact = SCHED_INTERACT_THRESH;
176 static int realstathz;
178 static int sched_slice = 1;
180 #ifdef FULL_PREEMPTION
181 static int preempt_thresh = PRI_MAX_IDLE;
183 static int preempt_thresh = PRI_MIN_KERN;
186 static int preempt_thresh = 0;
190 * tdq - per processor runqs and statistics. All fields are protected by the
191 * tdq_lock. The load and lowpri may be accessed without to avoid excess
192 * locking in sched_pickcpu();
195 /* Ordered to improve efficiency of cpu_search() and switch(). */
196 struct mtx tdq_lock; /* run queue lock. */
197 struct cpu_group *tdq_cg; /* Pointer to cpu topology. */
198 int tdq_load; /* Aggregate load. */
199 int tdq_sysload; /* For loadavg, !ITHD load. */
200 int tdq_transferable; /* Transferable thread count. */
201 u_char tdq_lowpri; /* Lowest priority thread. */
202 u_char tdq_ipipending; /* IPI pending. */
203 u_char tdq_idx; /* Current insert index. */
204 u_char tdq_ridx; /* Current removal index. */
205 struct runq tdq_realtime; /* real-time run queue. */
206 struct runq tdq_timeshare; /* timeshare run queue. */
207 struct runq tdq_idle; /* Queue of IDLE threads. */
208 char tdq_name[sizeof("sched lock") + 6];
213 struct cpu_group *cpu_top;
215 #define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000))
216 #define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity))
221 static int rebalance = 1;
222 static int balance_interval = 128; /* Default set in sched_initticks(). */
224 static int steal_htt = 1;
225 static int steal_idle = 1;
226 static int steal_thresh = 2;
229 * One thread queue per processor.
231 static struct tdq tdq_cpu[MAXCPU];
232 static struct tdq *balance_tdq;
233 static int balance_ticks;
235 #define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)])
236 #define TDQ_CPU(x) (&tdq_cpu[(x)])
237 #define TDQ_ID(x) ((int)((x) - tdq_cpu))
239 static struct tdq tdq_cpu;
241 #define TDQ_ID(x) (0)
242 #define TDQ_SELF() (&tdq_cpu)
243 #define TDQ_CPU(x) (&tdq_cpu)
246 #define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type))
247 #define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t)))
248 #define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
249 #define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t)))
250 #define TDQ_LOCKPTR(t) (&(t)->tdq_lock)
252 static void sched_priority(struct thread *);
253 static void sched_thread_priority(struct thread *, u_char);
254 static int sched_interact_score(struct thread *);
255 static void sched_interact_update(struct thread *);
256 static void sched_interact_fork(struct thread *);
257 static void sched_pctcpu_update(struct td_sched *);
259 /* Operations on per processor queues */
260 static struct td_sched * tdq_choose(struct tdq *);
261 static void tdq_setup(struct tdq *);
262 static void tdq_load_add(struct tdq *, struct td_sched *);
263 static void tdq_load_rem(struct tdq *, struct td_sched *);
264 static __inline void tdq_runq_add(struct tdq *, struct td_sched *, int);
265 static __inline void tdq_runq_rem(struct tdq *, struct td_sched *);
266 static inline int sched_shouldpreempt(int, int, int);
267 void tdq_print(int cpu);
268 static void runq_print(struct runq *rq);
269 static void tdq_add(struct tdq *, struct thread *, int);
271 static int tdq_move(struct tdq *, struct tdq *);
272 static int tdq_idled(struct tdq *);
273 static void tdq_notify(struct tdq *, struct td_sched *);
274 static struct td_sched *tdq_steal(struct tdq *, int);
275 static struct td_sched *runq_steal(struct runq *, int);
276 static int sched_pickcpu(struct td_sched *, int);
277 static void sched_balance(void);
278 static int sched_balance_pair(struct tdq *, struct tdq *);
279 static inline struct tdq *sched_setcpu(struct td_sched *, int, int);
280 static inline struct mtx *thread_block_switch(struct thread *);
281 static inline void thread_unblock_switch(struct thread *, struct mtx *);
282 static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int);
285 static void sched_setup(void *dummy);
286 SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL)
288 static void sched_initticks(void *dummy);
289 SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, NULL)
292 * Print the threads waiting on a run-queue.
295 runq_print(struct runq *rq)
303 for (i = 0; i < RQB_LEN; i++) {
304 printf("\t\trunq bits %d 0x%zx\n",
305 i, rq->rq_status.rqb_bits[i]);
306 for (j = 0; j < RQB_BPW; j++)
307 if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
308 pri = j + (i << RQB_L2BPW);
309 rqh = &rq->rq_queues[pri];
310 TAILQ_FOREACH(ts, rqh, ts_procq) {
311 printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
312 ts->ts_thread, ts->ts_thread->td_name, ts->ts_thread->td_priority, ts->ts_rqindex, pri);
319 * Print the status of a per-cpu thread queue. Should be a ddb show cmd.
328 printf("tdq %d:\n", TDQ_ID(tdq));
329 printf("\tlock %p\n", TDQ_LOCKPTR(tdq));
330 printf("\tLock name: %s\n", tdq->tdq_name);
331 printf("\tload: %d\n", tdq->tdq_load);
332 printf("\ttimeshare idx: %d\n", tdq->tdq_idx);
333 printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
334 printf("\trealtime runq:\n");
335 runq_print(&tdq->tdq_realtime);
336 printf("\ttimeshare runq:\n");
337 runq_print(&tdq->tdq_timeshare);
338 printf("\tidle runq:\n");
339 runq_print(&tdq->tdq_idle);
340 printf("\tload transferable: %d\n", tdq->tdq_transferable);
341 printf("\tlowest priority: %d\n", tdq->tdq_lowpri);
345 sched_shouldpreempt(int pri, int cpri, int remote)
348 * If the new priority is not better than the current priority there is
354 * Always preempt idle.
356 if (cpri >= PRI_MIN_IDLE)
359 * If preemption is disabled don't preempt others.
361 if (preempt_thresh == 0)
364 * Preempt if we exceed the threshold.
366 if (pri <= preempt_thresh)
369 * If we're realtime or better and there is timeshare or worse running
370 * preempt only remote processors.
372 if (remote && pri <= PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
377 #define TS_RQ_PPQ (((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS)
379 * Add a thread to the actual run-queue. Keeps transferable counts up to
380 * date with what is actually on the run-queue. Selects the correct
381 * queue position for timeshare threads.
384 tdq_runq_add(struct tdq *tdq, struct td_sched *ts, int flags)
386 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
387 THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
389 TD_SET_RUNQ(ts->ts_thread);
390 if (THREAD_CAN_MIGRATE(ts->ts_thread)) {
391 tdq->tdq_transferable++;
392 ts->ts_flags |= TSF_XFERABLE;
394 if (ts->ts_runq == &tdq->tdq_timeshare) {
397 pri = ts->ts_thread->td_priority;
398 KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
399 ("Invalid priority %d on timeshare runq", pri));
401 * This queue contains only priorities between MIN and MAX
402 * realtime. Use the whole queue to represent these values.
404 if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
405 pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
406 pri = (pri + tdq->tdq_idx) % RQ_NQS;
408 * This effectively shortens the queue by one so we
409 * can have a one slot difference between idx and
410 * ridx while we wait for threads to drain.
412 if (tdq->tdq_ridx != tdq->tdq_idx &&
413 pri == tdq->tdq_ridx)
414 pri = (unsigned char)(pri - 1) % RQ_NQS;
417 runq_add_pri(ts->ts_runq, ts, pri, flags);
419 runq_add(ts->ts_runq, ts, flags);
423 * Pick the run queue based on priority.
426 tdq_runq_pick(struct tdq *tdq, struct td_sched *ts)
430 pri = ts->ts_thread->td_priority;
431 if (pri <= PRI_MAX_REALTIME)
432 ts->ts_runq = &tdq->tdq_realtime;
433 else if (pri <= PRI_MAX_TIMESHARE)
434 ts->ts_runq = &tdq->tdq_timeshare;
436 ts->ts_runq = &tdq->tdq_idle;
440 * Remove a thread from a run-queue. This typically happens when a thread
441 * is selected to run. Running threads are not on the queue and the
442 * transferable count does not reflect them.
445 tdq_runq_rem(struct tdq *tdq, struct td_sched *ts)
447 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
448 KASSERT(ts->ts_runq != NULL,
449 ("tdq_runq_remove: thread %p null ts_runq", ts->ts_thread));
450 if (ts->ts_flags & TSF_XFERABLE) {
451 tdq->tdq_transferable--;
452 ts->ts_flags &= ~TSF_XFERABLE;
454 if (ts->ts_runq == &tdq->tdq_timeshare) {
455 if (tdq->tdq_idx != tdq->tdq_ridx)
456 runq_remove_idx(ts->ts_runq, ts, &tdq->tdq_ridx);
458 runq_remove_idx(ts->ts_runq, ts, NULL);
459 ts->ts_ltick = ticks;
461 runq_remove(ts->ts_runq, ts);
465 * Load is maintained for all threads RUNNING and ON_RUNQ. Add the load
466 * for this thread to the referenced thread queue.
469 tdq_load_add(struct tdq *tdq, struct td_sched *ts)
473 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
474 THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
475 class = PRI_BASE(ts->ts_thread->td_pri_class);
477 CTR2(KTR_SCHED, "cpu %d load: %d", TDQ_ID(tdq), tdq->tdq_load);
478 if (class != PRI_ITHD &&
479 (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
484 * Remove the load from a thread that is transitioning to a sleep state or
488 tdq_load_rem(struct tdq *tdq, struct td_sched *ts)
492 THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
493 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
494 class = PRI_BASE(ts->ts_thread->td_pri_class);
495 if (class != PRI_ITHD &&
496 (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
498 KASSERT(tdq->tdq_load != 0,
499 ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq)));
501 CTR1(KTR_SCHED, "load: %d", tdq->tdq_load);
506 * Set lowpri to its exact value by searching the run-queue and
507 * evaluating curthread. curthread may be passed as an optimization.
510 tdq_setlowpri(struct tdq *tdq, struct thread *ctd)
515 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
517 ctd = pcpu_find(TDQ_ID(tdq))->pc_curthread;
518 ts = tdq_choose(tdq);
521 if (ts == NULL || td->td_priority > ctd->td_priority)
522 tdq->tdq_lowpri = ctd->td_priority;
524 tdq->tdq_lowpri = td->td_priority;
529 cpumask_t cs_mask; /* Mask of valid cpus. */
532 int cs_limit; /* Min priority for low min load for high. */
535 #define CPU_SEARCH_LOWEST 0x1
536 #define CPU_SEARCH_HIGHEST 0x2
537 #define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST)
539 #define CPUMASK_FOREACH(cpu, mask) \
540 for ((cpu) = 0; (cpu) < sizeof((mask)) * 8; (cpu)++) \
541 if ((mask) & 1 << (cpu))
543 __inline int cpu_search(struct cpu_group *cg, struct cpu_search *low,
544 struct cpu_search *high, const int match);
545 int cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low);
546 int cpu_search_highest(struct cpu_group *cg, struct cpu_search *high);
547 int cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
548 struct cpu_search *high);
551 * This routine compares according to the match argument and should be
552 * reduced in actual instantiations via constant propagation and dead code
556 cpu_compare(int cpu, struct cpu_search *low, struct cpu_search *high,
562 if (match & CPU_SEARCH_LOWEST)
563 if (low->cs_mask & (1 << cpu) &&
564 tdq->tdq_load < low->cs_load &&
565 tdq->tdq_lowpri > low->cs_limit) {
567 low->cs_load = tdq->tdq_load;
569 if (match & CPU_SEARCH_HIGHEST)
570 if (high->cs_mask & (1 << cpu) &&
571 tdq->tdq_load >= high->cs_limit &&
572 tdq->tdq_load > high->cs_load &&
573 tdq->tdq_transferable) {
575 high->cs_load = tdq->tdq_load;
577 return (tdq->tdq_load);
581 * Search the tree of cpu_groups for the lowest or highest loaded cpu
582 * according to the match argument. This routine actually compares the
583 * load on all paths through the tree and finds the least loaded cpu on
584 * the least loaded path, which may differ from the least loaded cpu in
585 * the system. This balances work among caches and busses.
587 * This inline is instantiated in three forms below using constants for the
588 * match argument. It is reduced to the minimum set for each case. It is
589 * also recursive to the depth of the tree.
592 cpu_search(struct cpu_group *cg, struct cpu_search *low,
593 struct cpu_search *high, const int match)
598 if (cg->cg_children) {
599 struct cpu_search lgroup;
600 struct cpu_search hgroup;
601 struct cpu_group *child;
609 for (i = 0; i < cg->cg_children; i++) {
610 child = &cg->cg_child[i];
611 if (match & CPU_SEARCH_LOWEST) {
615 if (match & CPU_SEARCH_HIGHEST) {
620 case CPU_SEARCH_LOWEST:
621 load = cpu_search_lowest(child, &lgroup);
623 case CPU_SEARCH_HIGHEST:
624 load = cpu_search_highest(child, &hgroup);
626 case CPU_SEARCH_BOTH:
627 load = cpu_search_both(child, &lgroup, &hgroup);
631 if (match & CPU_SEARCH_LOWEST)
632 if (load < lload || low->cs_cpu == -1) {
636 if (match & CPU_SEARCH_HIGHEST)
637 if (load > hload || high->cs_cpu == -1) {
645 CPUMASK_FOREACH(cpu, cg->cg_mask)
646 total += cpu_compare(cpu, low, high, match);
652 * cpu_search instantiations must pass constants to maintain the inline
656 cpu_search_lowest(struct cpu_group *cg, struct cpu_search *low)
658 return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST);
662 cpu_search_highest(struct cpu_group *cg, struct cpu_search *high)
664 return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST);
668 cpu_search_both(struct cpu_group *cg, struct cpu_search *low,
669 struct cpu_search *high)
671 return cpu_search(cg, low, high, CPU_SEARCH_BOTH);
675 * Find the cpu with the least load via the least loaded path that has a
676 * lowpri greater than pri pri. A pri of -1 indicates any priority is
680 sched_lowest(struct cpu_group *cg, cpumask_t mask, int pri)
682 struct cpu_search low;
688 cpu_search_lowest(cg, &low);
693 * Find the cpu with the highest load via the highest loaded path.
696 sched_highest(struct cpu_group *cg, cpumask_t mask, int minload)
698 struct cpu_search high;
703 high.cs_limit = minload;
704 cpu_search_highest(cg, &high);
709 * Simultaneously find the highest and lowest loaded cpu reachable via
713 sched_both(struct cpu_group *cg, cpumask_t mask, int *lowcpu, int *highcpu)
715 struct cpu_search high;
716 struct cpu_search low;
726 cpu_search_both(cg, &low, &high);
727 *lowcpu = low.cs_cpu;
728 *highcpu = high.cs_cpu;
733 sched_balance_group(struct cpu_group *cg)
742 sched_both(cg, mask, &low, &high);
743 if (low == high || low == -1 || high == -1)
745 if (sched_balance_pair(TDQ_CPU(high), TDQ_CPU(low)))
748 * If we failed to move any threads determine which cpu
749 * to kick out of the set and try again.
751 if (TDQ_CPU(high)->tdq_transferable == 0)
752 mask &= ~(1 << high);
757 for (i = 0; i < cg->cg_children; i++)
758 sched_balance_group(&cg->cg_child[i]);
767 * Select a random time between .5 * balance_interval and
768 * 1.5 * balance_interval.
770 balance_ticks = max(balance_interval / 2, 1);
771 balance_ticks += random() % balance_interval;
772 if (smp_started == 0 || rebalance == 0)
776 sched_balance_group(cpu_top);
781 * Lock two thread queues using their address to maintain lock order.
784 tdq_lock_pair(struct tdq *one, struct tdq *two)
788 TDQ_LOCK_FLAGS(two, MTX_DUPOK);
791 TDQ_LOCK_FLAGS(one, MTX_DUPOK);
796 * Unlock two thread queues. Order is not important here.
799 tdq_unlock_pair(struct tdq *one, struct tdq *two)
806 * Transfer load between two imbalanced thread queues.
809 sched_balance_pair(struct tdq *high, struct tdq *low)
819 tdq_lock_pair(high, low);
820 transferable = high->tdq_transferable;
821 high_load = high->tdq_load;
822 low_load = low->tdq_load;
825 * Determine what the imbalance is and then adjust that to how many
826 * threads we actually have to give up (transferable).
828 if (transferable != 0) {
829 diff = high_load - low_load;
833 move = min(move, transferable);
834 for (i = 0; i < move; i++)
835 moved += tdq_move(high, low);
837 * IPI the target cpu to force it to reschedule with the new
840 ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT);
842 tdq_unlock_pair(high, low);
847 * Move a thread from one thread queue to another.
850 tdq_move(struct tdq *from, struct tdq *to)
857 TDQ_LOCK_ASSERT(from, MA_OWNED);
858 TDQ_LOCK_ASSERT(to, MA_OWNED);
862 ts = tdq_steal(tdq, cpu);
867 * Although the run queue is locked the thread may be blocked. Lock
868 * it to clear this and acquire the run-queue lock.
871 /* Drop recursive lock on from acquired via thread_lock(). */
875 td->td_lock = TDQ_LOCKPTR(to);
876 tdq_add(to, td, SRQ_YIELDING);
881 * This tdq has idled. Try to steal a thread from another cpu and switch
885 tdq_idled(struct tdq *tdq)
887 struct cpu_group *cg;
893 if (smp_started == 0 || steal_idle == 0)
896 mask &= ~PCPU_GET(cpumask);
897 /* We don't want to be preempted while we're iterating. */
899 for (cg = tdq->tdq_cg; cg != NULL; ) {
900 if ((cg->cg_flags & (CG_FLAG_HTT | CG_FLAG_THREAD)) == 0)
901 thresh = steal_thresh;
904 cpu = sched_highest(cg, mask, thresh);
909 steal = TDQ_CPU(cpu);
911 tdq_lock_pair(tdq, steal);
912 if (steal->tdq_load < thresh || steal->tdq_transferable == 0) {
913 tdq_unlock_pair(tdq, steal);
917 * If a thread was added while interrupts were disabled don't
918 * steal one here. If we fail to acquire one due to affinity
919 * restrictions loop again with this cpu removed from the
922 if (tdq->tdq_load == 0 && tdq_move(steal, tdq) == 0) {
923 tdq_unlock_pair(tdq, steal);
928 mi_switch(SW_VOL, NULL);
929 thread_unlock(curthread);
938 * Notify a remote cpu of new work. Sends an IPI if criteria are met.
941 tdq_notify(struct tdq *tdq, struct td_sched *ts)
947 if (tdq->tdq_ipipending)
950 pri = ts->ts_thread->td_priority;
951 cpri = pcpu_find(cpu)->pc_curthread->td_priority;
952 if (!sched_shouldpreempt(pri, cpri, 1))
954 tdq->tdq_ipipending = 1;
955 ipi_selected(1 << cpu, IPI_PREEMPT);
959 * Steals load from a timeshare queue. Honors the rotating queue head
962 static struct td_sched *
963 runq_steal_from(struct runq *rq, int cpu, u_char start)
973 rqb = &rq->rq_status;
974 bit = start & (RQB_BPW -1);
978 for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
979 if (rqb->rqb_bits[i] == 0)
982 for (pri = bit; pri < RQB_BPW; pri++)
983 if (rqb->rqb_bits[i] & (1ul << pri))
988 pri = RQB_FFS(rqb->rqb_bits[i]);
989 pri += (i << RQB_L2BPW);
990 rqh = &rq->rq_queues[pri];
991 TAILQ_FOREACH(ts, rqh, ts_procq) {
992 if (first && THREAD_CAN_MIGRATE(ts->ts_thread) &&
993 THREAD_CAN_SCHED(ts->ts_thread, cpu))
1007 * Steals load from a standard linear queue.
1009 static struct td_sched *
1010 runq_steal(struct runq *rq, int cpu)
1014 struct td_sched *ts;
1018 rqb = &rq->rq_status;
1019 for (word = 0; word < RQB_LEN; word++) {
1020 if (rqb->rqb_bits[word] == 0)
1022 for (bit = 0; bit < RQB_BPW; bit++) {
1023 if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
1025 rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
1026 TAILQ_FOREACH(ts, rqh, ts_procq)
1027 if (THREAD_CAN_MIGRATE(ts->ts_thread) &&
1028 THREAD_CAN_SCHED(ts->ts_thread, cpu))
1036 * Attempt to steal a thread in priority order from a thread queue.
1038 static struct td_sched *
1039 tdq_steal(struct tdq *tdq, int cpu)
1041 struct td_sched *ts;
1043 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
1044 if ((ts = runq_steal(&tdq->tdq_realtime, cpu)) != NULL)
1046 if ((ts = runq_steal_from(&tdq->tdq_timeshare, cpu, tdq->tdq_ridx))
1049 return (runq_steal(&tdq->tdq_idle, cpu));
1053 * Sets the thread lock and ts_cpu to match the requested cpu. Unlocks the
1054 * current lock and returns with the assigned queue locked.
1056 static inline struct tdq *
1057 sched_setcpu(struct td_sched *ts, int cpu, int flags)
1062 THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
1068 /* If the lock matches just return the queue. */
1069 if (td->td_lock == TDQ_LOCKPTR(tdq))
1073 * If the thread isn't running its lockptr is a
1074 * turnstile or a sleepqueue. We can just lock_set without
1077 if (TD_CAN_RUN(td)) {
1079 thread_lock_set(td, TDQ_LOCKPTR(tdq));
1084 * The hard case, migration, we need to block the thread first to
1085 * prevent order reversals with other cpus locks.
1087 thread_lock_block(td);
1089 thread_lock_unblock(td, TDQ_LOCKPTR(tdq));
1094 sched_pickcpu(struct td_sched *ts, int flags)
1096 struct cpu_group *cg;
1104 self = PCPU_GET(cpuid);
1106 if (smp_started == 0)
1109 * Don't migrate a running thread from sched_switch().
1111 if ((flags & SRQ_OURSELF) || !THREAD_CAN_MIGRATE(td))
1112 return (ts->ts_cpu);
1114 * Prefer to run interrupt threads on the processors that generate
1117 if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_SCHED(td, self) &&
1118 curthread->td_intr_nesting_level)
1121 * If the thread can run on the last cpu and the affinity has not
1122 * expired or it is idle run it there.
1124 pri = td->td_priority;
1125 tdq = TDQ_CPU(ts->ts_cpu);
1126 if (THREAD_CAN_SCHED(td, ts->ts_cpu)) {
1127 if (tdq->tdq_lowpri > PRI_MIN_IDLE)
1128 return (ts->ts_cpu);
1129 if (SCHED_AFFINITY(ts, CG_SHARE_L2) && tdq->tdq_lowpri > pri)
1130 return (ts->ts_cpu);
1133 * Search for the highest level in the tree that still has affinity.
1136 for (cg = tdq->tdq_cg; cg != NULL; cg = cg->cg_parent)
1137 if (SCHED_AFFINITY(ts, cg->cg_level))
1140 mask = td->td_cpuset->cs_mask.__bits[0];
1142 cpu = sched_lowest(cg, mask, pri);
1144 cpu = sched_lowest(cpu_top, mask, -1);
1146 * Compare the lowest loaded cpu to current cpu.
1148 if (THREAD_CAN_SCHED(td, self) && TDQ_CPU(self)->tdq_lowpri > pri &&
1149 TDQ_CPU(cpu)->tdq_lowpri < PRI_MIN_IDLE)
1151 KASSERT(cpu != -1, ("sched_pickcpu: Failed to find a cpu."));
1157 * Pick the highest priority task we have and return it.
1159 static struct td_sched *
1160 tdq_choose(struct tdq *tdq)
1162 struct td_sched *ts;
1164 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
1165 ts = runq_choose(&tdq->tdq_realtime);
1168 ts = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
1170 KASSERT(ts->ts_thread->td_priority >= PRI_MIN_TIMESHARE,
1171 ("tdq_choose: Invalid priority on timeshare queue %d",
1172 ts->ts_thread->td_priority));
1176 ts = runq_choose(&tdq->tdq_idle);
1178 KASSERT(ts->ts_thread->td_priority >= PRI_MIN_IDLE,
1179 ("tdq_choose: Invalid priority on idle queue %d",
1180 ts->ts_thread->td_priority));
1188 * Initialize a thread queue.
1191 tdq_setup(struct tdq *tdq)
1195 printf("ULE: setup cpu %d\n", TDQ_ID(tdq));
1196 runq_init(&tdq->tdq_realtime);
1197 runq_init(&tdq->tdq_timeshare);
1198 runq_init(&tdq->tdq_idle);
1199 snprintf(tdq->tdq_name, sizeof(tdq->tdq_name),
1200 "sched lock %d", (int)TDQ_ID(tdq));
1201 mtx_init(&tdq->tdq_lock, tdq->tdq_name, "sched lock",
1202 MTX_SPIN | MTX_RECURSE);
1207 sched_setup_smp(void)
1212 cpu_top = smp_topo();
1213 for (i = 0; i < MAXCPU; i++) {
1218 tdq->tdq_cg = smp_topo_find(cpu_top, i);
1219 if (tdq->tdq_cg == NULL)
1220 panic("Can't find cpu group for %d\n", i);
1222 balance_tdq = TDQ_SELF();
1228 * Setup the thread queues and initialize the topology based on MD
1232 sched_setup(void *dummy)
1243 * To avoid divide-by-zero, we set realstathz a dummy value
1244 * in case which sched_clock() called before sched_initticks().
1247 sched_slice = (realstathz/10); /* ~100ms */
1248 tickincr = 1 << SCHED_TICK_SHIFT;
1250 /* Add thread0's load since it's running. */
1252 thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF());
1253 tdq_runq_pick(tdq, &td_sched0);
1254 tdq_load_add(tdq, &td_sched0);
1255 tdq->tdq_lowpri = thread0.td_priority;
1260 * This routine determines the tickincr after stathz and hz are setup.
1264 sched_initticks(void *dummy)
1268 realstathz = stathz ? stathz : hz;
1269 sched_slice = (realstathz/10); /* ~100ms */
1272 * tickincr is shifted out by 10 to avoid rounding errors due to
1273 * hz not being evenly divisible by stathz on all platforms.
1275 incr = (hz << SCHED_TICK_SHIFT) / realstathz;
1277 * This does not work for values of stathz that are more than
1278 * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen.
1285 * Set the default balance interval now that we know
1286 * what realstathz is.
1288 balance_interval = realstathz;
1290 * Set steal thresh to log2(mp_ncpu) but no greater than 4. This
1291 * prevents excess thrashing on large machines and excess idle on
1294 steal_thresh = min(ffs(mp_ncpus) - 1, 3);
1295 affinity = SCHED_AFFINITY_DEFAULT;
1301 * This is the core of the interactivity algorithm. Determines a score based
1302 * on past behavior. It is the ratio of sleep time to run time scaled to
1303 * a [0, 100] integer. This is the voluntary sleep time of a process, which
1304 * differs from the cpu usage because it does not account for time spent
1305 * waiting on a run-queue. Would be prettier if we had floating point.
1308 sched_interact_score(struct thread *td)
1310 struct td_sched *ts;
1315 * The score is only needed if this is likely to be an interactive
1316 * task. Don't go through the expense of computing it if there's
1319 if (sched_interact <= SCHED_INTERACT_HALF &&
1320 ts->ts_runtime >= ts->ts_slptime)
1321 return (SCHED_INTERACT_HALF);
1323 if (ts->ts_runtime > ts->ts_slptime) {
1324 div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF);
1325 return (SCHED_INTERACT_HALF +
1326 (SCHED_INTERACT_HALF - (ts->ts_slptime / div)));
1328 if (ts->ts_slptime > ts->ts_runtime) {
1329 div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF);
1330 return (ts->ts_runtime / div);
1332 /* runtime == slptime */
1334 return (SCHED_INTERACT_HALF);
1337 * This can happen if slptime and runtime are 0.
1344 * Scale the scheduling priority according to the "interactivity" of this
1348 sched_priority(struct thread *td)
1353 if (td->td_pri_class != PRI_TIMESHARE)
1356 * If the score is interactive we place the thread in the realtime
1357 * queue with a priority that is less than kernel and interrupt
1358 * priorities. These threads are not subject to nice restrictions.
1360 * Scores greater than this are placed on the normal timeshare queue
1361 * where the priority is partially decided by the most recent cpu
1362 * utilization and the rest is decided by nice value.
1364 * The nice value of the process has a linear effect on the calculated
1365 * score. Negative nice values make it easier for a thread to be
1366 * considered interactive.
1368 score = imax(0, sched_interact_score(td) - td->td_proc->p_nice);
1369 if (score < sched_interact) {
1370 pri = PRI_MIN_REALTIME;
1371 pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
1373 KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
1374 ("sched_priority: invalid interactive priority %d score %d",
1377 pri = SCHED_PRI_MIN;
1378 if (td->td_sched->ts_ticks)
1379 pri += SCHED_PRI_TICKS(td->td_sched);
1380 pri += SCHED_PRI_NICE(td->td_proc->p_nice);
1381 KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE,
1382 ("sched_priority: invalid priority %d: nice %d, "
1383 "ticks %d ftick %d ltick %d tick pri %d",
1384 pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
1385 td->td_sched->ts_ftick, td->td_sched->ts_ltick,
1386 SCHED_PRI_TICKS(td->td_sched)));
1388 sched_user_prio(td, pri);
1394 * This routine enforces a maximum limit on the amount of scheduling history
1395 * kept. It is called after either the slptime or runtime is adjusted. This
1396 * function is ugly due to integer math.
1399 sched_interact_update(struct thread *td)
1401 struct td_sched *ts;
1405 sum = ts->ts_runtime + ts->ts_slptime;
1406 if (sum < SCHED_SLP_RUN_MAX)
1409 * This only happens from two places:
1410 * 1) We have added an unusual amount of run time from fork_exit.
1411 * 2) We have added an unusual amount of sleep time from sched_sleep().
1413 if (sum > SCHED_SLP_RUN_MAX * 2) {
1414 if (ts->ts_runtime > ts->ts_slptime) {
1415 ts->ts_runtime = SCHED_SLP_RUN_MAX;
1418 ts->ts_slptime = SCHED_SLP_RUN_MAX;
1424 * If we have exceeded by more than 1/5th then the algorithm below
1425 * will not bring us back into range. Dividing by two here forces
1426 * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
1428 if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
1429 ts->ts_runtime /= 2;
1430 ts->ts_slptime /= 2;
1433 ts->ts_runtime = (ts->ts_runtime / 5) * 4;
1434 ts->ts_slptime = (ts->ts_slptime / 5) * 4;
1438 * Scale back the interactivity history when a child thread is created. The
1439 * history is inherited from the parent but the thread may behave totally
1440 * differently. For example, a shell spawning a compiler process. We want
1441 * to learn that the compiler is behaving badly very quickly.
1444 sched_interact_fork(struct thread *td)
1449 sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
1450 if (sum > SCHED_SLP_RUN_FORK) {
1451 ratio = sum / SCHED_SLP_RUN_FORK;
1452 td->td_sched->ts_runtime /= ratio;
1453 td->td_sched->ts_slptime /= ratio;
1458 * Called from proc0_init() to setup the scheduler fields.
1465 * Set up the scheduler specific parts of proc0.
1467 proc0.p_sched = NULL; /* XXX */
1468 thread0.td_sched = &td_sched0;
1469 td_sched0.ts_ltick = ticks;
1470 td_sched0.ts_ftick = ticks;
1471 td_sched0.ts_thread = &thread0;
1472 td_sched0.ts_slice = sched_slice;
1476 * This is only somewhat accurate since given many processes of the same
1477 * priority they will switch when their slices run out, which will be
1478 * at most sched_slice stathz ticks.
1481 sched_rr_interval(void)
1484 /* Convert sched_slice to hz */
1485 return (hz/(realstathz/sched_slice));
1489 * Update the percent cpu tracking information when it is requested or
1490 * the total history exceeds the maximum. We keep a sliding history of
1491 * tick counts that slowly decays. This is less precise than the 4BSD
1492 * mechanism since it happens with less regular and frequent events.
1495 sched_pctcpu_update(struct td_sched *ts)
1498 if (ts->ts_ticks == 0)
1500 if (ticks - (hz / 10) < ts->ts_ltick &&
1501 SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
1504 * Adjust counters and watermark for pctcpu calc.
1506 if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
1507 ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
1511 ts->ts_ltick = ticks;
1512 ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
1516 * Adjust the priority of a thread. Move it to the appropriate run-queue
1517 * if necessary. This is the back-end for several priority related
1521 sched_thread_priority(struct thread *td, u_char prio)
1523 struct td_sched *ts;
1527 CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)",
1528 td, td->td_name, td->td_priority, prio, curthread,
1529 curthread->td_name);
1531 THREAD_LOCK_ASSERT(td, MA_OWNED);
1532 if (td->td_priority == prio)
1535 if (TD_ON_RUNQ(td) && prio < td->td_priority) {
1537 * If the priority has been elevated due to priority
1538 * propagation, we may have to move ourselves to a new
1539 * queue. This could be optimized to not re-add in some
1543 td->td_priority = prio;
1544 sched_add(td, SRQ_BORROWING);
1547 tdq = TDQ_CPU(ts->ts_cpu);
1548 oldpri = td->td_priority;
1549 td->td_priority = prio;
1550 tdq_runq_pick(tdq, ts);
1551 if (TD_IS_RUNNING(td)) {
1552 if (prio < tdq->tdq_lowpri)
1553 tdq->tdq_lowpri = prio;
1554 else if (tdq->tdq_lowpri == oldpri)
1555 tdq_setlowpri(tdq, td);
1560 * Update a thread's priority when it is lent another thread's
1564 sched_lend_prio(struct thread *td, u_char prio)
1567 td->td_flags |= TDF_BORROWING;
1568 sched_thread_priority(td, prio);
1572 * Restore a thread's priority when priority propagation is
1573 * over. The prio argument is the minimum priority the thread
1574 * needs to have to satisfy other possible priority lending
1575 * requests. If the thread's regular priority is less
1576 * important than prio, the thread will keep a priority boost
1580 sched_unlend_prio(struct thread *td, u_char prio)
1584 if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1585 td->td_base_pri <= PRI_MAX_TIMESHARE)
1586 base_pri = td->td_user_pri;
1588 base_pri = td->td_base_pri;
1589 if (prio >= base_pri) {
1590 td->td_flags &= ~TDF_BORROWING;
1591 sched_thread_priority(td, base_pri);
1593 sched_lend_prio(td, prio);
1597 * Standard entry for setting the priority to an absolute value.
1600 sched_prio(struct thread *td, u_char prio)
1604 /* First, update the base priority. */
1605 td->td_base_pri = prio;
1608 * If the thread is borrowing another thread's priority, don't
1609 * ever lower the priority.
1611 if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1614 /* Change the real priority. */
1615 oldprio = td->td_priority;
1616 sched_thread_priority(td, prio);
1619 * If the thread is on a turnstile, then let the turnstile update
1622 if (TD_ON_LOCK(td) && oldprio != prio)
1623 turnstile_adjust(td, oldprio);
1627 * Set the base user priority, does not effect current running priority.
1630 sched_user_prio(struct thread *td, u_char prio)
1634 td->td_base_user_pri = prio;
1635 if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
1637 oldprio = td->td_user_pri;
1638 td->td_user_pri = prio;
1642 sched_lend_user_prio(struct thread *td, u_char prio)
1646 THREAD_LOCK_ASSERT(td, MA_OWNED);
1647 td->td_flags |= TDF_UBORROWING;
1648 oldprio = td->td_user_pri;
1649 td->td_user_pri = prio;
1653 sched_unlend_user_prio(struct thread *td, u_char prio)
1657 THREAD_LOCK_ASSERT(td, MA_OWNED);
1658 base_pri = td->td_base_user_pri;
1659 if (prio >= base_pri) {
1660 td->td_flags &= ~TDF_UBORROWING;
1661 sched_user_prio(td, base_pri);
1663 sched_lend_user_prio(td, prio);
1668 * Add the thread passed as 'newtd' to the run queue before selecting
1669 * the next thread to run. This is only used for KSE.
1672 sched_switchin(struct tdq *tdq, struct thread *td)
1679 sched_setcpu(td->td_sched, TDQ_ID(tdq), SRQ_YIELDING);
1681 td->td_lock = TDQ_LOCKPTR(tdq);
1683 tdq_add(tdq, td, SRQ_YIELDING);
1684 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1688 * Block a thread for switching. Similar to thread_block() but does not
1689 * bump the spin count.
1691 static inline struct mtx *
1692 thread_block_switch(struct thread *td)
1696 THREAD_LOCK_ASSERT(td, MA_OWNED);
1698 td->td_lock = &blocked_lock;
1699 mtx_unlock_spin(lock);
1705 * Handle migration from sched_switch(). This happens only for
1709 sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
1713 tdn = TDQ_CPU(td->td_sched->ts_cpu);
1715 tdq_load_rem(tdq, td->td_sched);
1717 * Do the lock dance required to avoid LOR. We grab an extra
1718 * spinlock nesting to prevent preemption while we're
1719 * not holding either run-queue lock.
1722 thread_block_switch(td); /* This releases the lock on tdq. */
1724 tdq_add(tdn, td, flags);
1725 tdq_notify(tdn, td->td_sched);
1727 * After we unlock tdn the new cpu still can't switch into this
1728 * thread until we've unblocked it in cpu_switch(). The lock
1729 * pointers may match in the case of HTT cores. Don't unlock here
1730 * or we can deadlock when the other CPU runs the IPI handler.
1732 if (TDQ_LOCKPTR(tdn) != TDQ_LOCKPTR(tdq)) {
1738 return (TDQ_LOCKPTR(tdn));
1742 * Release a thread that was blocked with thread_block_switch().
1745 thread_unblock_switch(struct thread *td, struct mtx *mtx)
1747 atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock,
1752 * Switch threads. This function has to handle threads coming in while
1753 * blocked for some reason, running, or idle. It also must deal with
1754 * migrating a thread from one queue to another as running threads may
1755 * be assigned elsewhere via binding.
1758 sched_switch(struct thread *td, struct thread *newtd, int flags)
1761 struct td_sched *ts;
1766 THREAD_LOCK_ASSERT(td, MA_OWNED);
1768 cpuid = PCPU_GET(cpuid);
1769 tdq = TDQ_CPU(cpuid);
1772 ts->ts_rltick = ticks;
1773 td->td_lastcpu = td->td_oncpu;
1774 td->td_oncpu = NOCPU;
1775 td->td_flags &= ~TDF_NEEDRESCHED;
1776 td->td_owepreempt = 0;
1778 * The lock pointer in an idle thread should never change. Reset it
1779 * to CAN_RUN as well.
1781 if (TD_IS_IDLETHREAD(td)) {
1782 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1784 } else if (TD_IS_RUNNING(td)) {
1785 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1786 srqflag = (flags & SW_PREEMPT) ?
1787 SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1788 SRQ_OURSELF|SRQ_YIELDING;
1789 if (ts->ts_cpu == cpuid)
1790 tdq_runq_add(tdq, ts, srqflag);
1792 mtx = sched_switch_migrate(tdq, td, srqflag);
1794 /* This thread must be going to sleep. */
1796 mtx = thread_block_switch(td);
1797 tdq_load_rem(tdq, ts);
1800 * We enter here with the thread blocked and assigned to the
1801 * appropriate cpu run-queue or sleep-queue and with the current
1802 * thread-queue locked.
1804 TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
1806 * If KSE assigned a new thread just add it here and let choosethread
1807 * select the best one.
1810 sched_switchin(tdq, newtd);
1811 newtd = choosethread();
1813 * Call the MD code to switch contexts if necessary.
1817 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1818 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1820 lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
1821 TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
1822 cpu_switch(td, newtd, mtx);
1824 * We may return from cpu_switch on a different cpu. However,
1825 * we always return with td_lock pointing to the current cpu's
1828 cpuid = PCPU_GET(cpuid);
1829 tdq = TDQ_CPU(cpuid);
1830 lock_profile_obtain_lock_success(
1831 &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
1833 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1834 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1837 thread_unblock_switch(td, mtx);
1839 * We should always get here with the lowest priority td possible.
1841 tdq->tdq_lowpri = td->td_priority;
1843 * Assert that all went well and return.
1845 TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED);
1846 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
1847 td->td_oncpu = cpuid;
1851 * Adjust thread priorities as a result of a nice request.
1854 sched_nice(struct proc *p, int nice)
1858 PROC_LOCK_ASSERT(p, MA_OWNED);
1859 PROC_SLOCK_ASSERT(p, MA_OWNED);
1862 FOREACH_THREAD_IN_PROC(p, td) {
1865 sched_prio(td, td->td_base_user_pri);
1871 * Record the sleep time for the interactivity scorer.
1874 sched_sleep(struct thread *td)
1877 THREAD_LOCK_ASSERT(td, MA_OWNED);
1879 td->td_slptick = ticks;
1883 * Schedule a thread to resume execution and record how long it voluntarily
1884 * slept. We also update the pctcpu, interactivity, and priority.
1887 sched_wakeup(struct thread *td)
1889 struct td_sched *ts;
1892 THREAD_LOCK_ASSERT(td, MA_OWNED);
1895 * If we slept for more than a tick update our interactivity and
1898 slptick = td->td_slptick;
1900 if (slptick && slptick != ticks) {
1903 hzticks = (ticks - slptick) << SCHED_TICK_SHIFT;
1904 ts->ts_slptime += hzticks;
1905 sched_interact_update(td);
1906 sched_pctcpu_update(ts);
1908 /* Reset the slice value after we sleep. */
1909 ts->ts_slice = sched_slice;
1910 sched_add(td, SRQ_BORING);
1914 * Penalize the parent for creating a new child and initialize the child's
1918 sched_fork(struct thread *td, struct thread *child)
1920 THREAD_LOCK_ASSERT(td, MA_OWNED);
1921 sched_fork_thread(td, child);
1923 * Penalize the parent and child for forking.
1925 sched_interact_fork(child);
1926 sched_priority(child);
1927 td->td_sched->ts_runtime += tickincr;
1928 sched_interact_update(td);
1933 * Fork a new thread, may be within the same process.
1936 sched_fork_thread(struct thread *td, struct thread *child)
1938 struct td_sched *ts;
1939 struct td_sched *ts2;
1944 THREAD_LOCK_ASSERT(td, MA_OWNED);
1945 sched_newthread(child);
1946 child->td_lock = TDQ_LOCKPTR(TDQ_SELF());
1947 child->td_cpuset = cpuset_ref(td->td_cpuset);
1949 ts2 = child->td_sched;
1950 ts2->ts_cpu = ts->ts_cpu;
1951 ts2->ts_runq = NULL;
1953 * Grab our parents cpu estimation information and priority.
1955 ts2->ts_ticks = ts->ts_ticks;
1956 ts2->ts_ltick = ts->ts_ltick;
1957 ts2->ts_ftick = ts->ts_ftick;
1958 child->td_user_pri = td->td_user_pri;
1959 child->td_base_user_pri = td->td_base_user_pri;
1961 * And update interactivity score.
1963 ts2->ts_slptime = ts->ts_slptime;
1964 ts2->ts_runtime = ts->ts_runtime;
1965 ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */
1969 * Adjust the priority class of a thread.
1972 sched_class(struct thread *td, int class)
1975 THREAD_LOCK_ASSERT(td, MA_OWNED);
1976 if (td->td_pri_class == class)
1979 * On SMP if we're on the RUNQ we must adjust the transferable
1980 * count because could be changing to or from an interrupt
1983 if (TD_ON_RUNQ(td)) {
1986 tdq = TDQ_CPU(td->td_sched->ts_cpu);
1987 if (THREAD_CAN_MIGRATE(td))
1988 tdq->tdq_transferable--;
1989 td->td_pri_class = class;
1990 if (THREAD_CAN_MIGRATE(td))
1991 tdq->tdq_transferable++;
1993 td->td_pri_class = class;
1997 * Return some of the child's priority and interactivity to the parent.
2000 sched_exit(struct proc *p, struct thread *child)
2004 CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d",
2005 child, child->td_name, child->td_priority);
2007 PROC_SLOCK_ASSERT(p, MA_OWNED);
2008 td = FIRST_THREAD_IN_PROC(p);
2009 sched_exit_thread(td, child);
2013 * Penalize another thread for the time spent on this one. This helps to
2014 * worsen the priority and interactivity of processes which schedule batch
2015 * jobs such as make. This has little effect on the make process itself but
2016 * causes new processes spawned by it to receive worse scores immediately.
2019 sched_exit_thread(struct thread *td, struct thread *child)
2022 CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d",
2023 child, child->td_name, child->td_priority);
2027 * KSE forks and exits so often that this penalty causes short-lived
2028 * threads to always be non-interactive. This causes mozilla to
2031 if ((td->td_pflags & TDP_SA) && td->td_proc == child->td_proc)
2035 * Give the child's runtime to the parent without returning the
2036 * sleep time as a penalty to the parent. This causes shells that
2037 * launch expensive things to mark their children as expensive.
2040 td->td_sched->ts_runtime += child->td_sched->ts_runtime;
2041 sched_interact_update(td);
2047 sched_preempt(struct thread *td)
2053 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2054 tdq->tdq_ipipending = 0;
2055 if (td->td_priority > tdq->tdq_lowpri) {
2056 if (td->td_critnest > 1)
2057 td->td_owepreempt = 1;
2059 mi_switch(SW_INVOL | SW_PREEMPT, NULL);
2065 * Fix priorities on return to user-space. Priorities may be elevated due
2066 * to static priorities in msleep() or similar.
2069 sched_userret(struct thread *td)
2072 * XXX we cheat slightly on the locking here to avoid locking in
2073 * the usual case. Setting td_priority here is essentially an
2074 * incomplete workaround for not setting it properly elsewhere.
2075 * Now that some interrupt handlers are threads, not setting it
2076 * properly elsewhere can clobber it in the window between setting
2077 * it here and returning to user mode, so don't waste time setting
2078 * it perfectly here.
2080 KASSERT((td->td_flags & TDF_BORROWING) == 0,
2081 ("thread with borrowed priority returning to userland"));
2082 if (td->td_priority != td->td_user_pri) {
2084 td->td_priority = td->td_user_pri;
2085 td->td_base_pri = td->td_user_pri;
2086 tdq_setlowpri(TDQ_SELF(), td);
2092 * Handle a stathz tick. This is really only relevant for timeshare
2096 sched_clock(struct thread *td)
2099 struct td_sched *ts;
2101 THREAD_LOCK_ASSERT(td, MA_OWNED);
2105 * We run the long term load balancer infrequently on the first cpu.
2107 if (balance_tdq == tdq) {
2108 if (balance_ticks && --balance_ticks == 0)
2113 * Advance the insert index once for each tick to ensure that all
2114 * threads get a chance to run.
2116 if (tdq->tdq_idx == tdq->tdq_ridx) {
2117 tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
2118 if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
2119 tdq->tdq_ridx = tdq->tdq_idx;
2122 if (td->td_pri_class & PRI_FIFO_BIT)
2124 if (td->td_pri_class == PRI_TIMESHARE) {
2126 * We used a tick; charge it to the thread so
2127 * that we can compute our interactivity.
2129 td->td_sched->ts_runtime += tickincr;
2130 sched_interact_update(td);
2134 * We used up one time slice.
2136 if (--ts->ts_slice > 0)
2139 * We're out of time, force a requeue at userret().
2141 ts->ts_slice = sched_slice;
2142 td->td_flags |= TDF_NEEDRESCHED;
2146 * Called once per hz tick. Used for cpu utilization information. This
2147 * is easier than trying to scale based on stathz.
2152 struct td_sched *ts;
2154 ts = curthread->td_sched;
2155 /* Adjust ticks for pctcpu */
2156 ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
2157 ts->ts_ltick = ticks;
2159 * Update if we've exceeded our desired tick threshhold by over one
2162 if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
2163 sched_pctcpu_update(ts);
2167 * Return whether the current CPU has runnable tasks. Used for in-kernel
2168 * cooperative idle threads.
2171 sched_runnable(void)
2179 if ((curthread->td_flags & TDF_IDLETD) != 0) {
2180 if (tdq->tdq_load > 0)
2183 if (tdq->tdq_load - 1 > 0)
2191 * Choose the highest priority thread to run. The thread is removed from
2192 * the run-queue while running however the load remains. For SMP we set
2193 * the tdq in the global idle bitmask if it idles here.
2198 struct td_sched *ts;
2202 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2203 ts = tdq_choose(tdq);
2205 tdq_runq_rem(tdq, ts);
2206 return (ts->ts_thread);
2208 return (PCPU_GET(idlethread));
2212 * Set owepreempt if necessary. Preemption never happens directly in ULE,
2213 * we always request it once we exit a critical section.
2216 sched_setpreempt(struct thread *td)
2222 THREAD_LOCK_ASSERT(curthread, MA_OWNED);
2225 pri = td->td_priority;
2226 cpri = ctd->td_priority;
2228 ctd->td_flags |= TDF_NEEDRESCHED;
2229 if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
2231 if (!sched_shouldpreempt(pri, cpri, 0))
2233 ctd->td_owepreempt = 1;
2237 * Add a thread to a thread queue. Select the appropriate runq and add the
2238 * thread to it. This is the internal function called when the tdq is
2242 tdq_add(struct tdq *tdq, struct thread *td, int flags)
2244 struct td_sched *ts;
2246 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2247 KASSERT((td->td_inhibitors == 0),
2248 ("sched_add: trying to run inhibited thread"));
2249 KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
2250 ("sched_add: bad thread state"));
2251 KASSERT(td->td_flags & TDF_INMEM,
2252 ("sched_add: thread swapped out"));
2255 if (td->td_priority < tdq->tdq_lowpri)
2256 tdq->tdq_lowpri = td->td_priority;
2257 tdq_runq_pick(tdq, ts);
2258 tdq_runq_add(tdq, ts, flags);
2259 tdq_load_add(tdq, ts);
2263 * Select the target thread queue and add a thread to it. Request
2264 * preemption or IPI a remote processor if required.
2267 sched_add(struct thread *td, int flags)
2271 struct td_sched *ts;
2274 CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
2275 td, td->td_name, td->td_priority, curthread,
2276 curthread->td_name);
2277 THREAD_LOCK_ASSERT(td, MA_OWNED);
2279 * Recalculate the priority before we select the target cpu or
2282 if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
2286 * Pick the destination cpu and if it isn't ours transfer to the
2290 cpu = sched_pickcpu(ts, flags);
2291 tdq = sched_setcpu(ts, cpu, flags);
2292 tdq_add(tdq, td, flags);
2293 if (cpu != PCPU_GET(cpuid)) {
2294 tdq_notify(tdq, ts);
2301 * Now that the thread is moving to the run-queue, set the lock
2302 * to the scheduler's lock.
2304 thread_lock_set(td, TDQ_LOCKPTR(tdq));
2305 tdq_add(tdq, td, flags);
2307 if (!(flags & SRQ_YIELDING))
2308 sched_setpreempt(td);
2312 * Remove a thread from a run-queue without running it. This is used
2313 * when we're stealing a thread from a remote queue. Otherwise all threads
2314 * exit by calling sched_exit_thread() and sched_throw() themselves.
2317 sched_rem(struct thread *td)
2320 struct td_sched *ts;
2322 CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)",
2323 td, td->td_name, td->td_priority, curthread,
2324 curthread->td_name);
2326 tdq = TDQ_CPU(ts->ts_cpu);
2327 TDQ_LOCK_ASSERT(tdq, MA_OWNED);
2328 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2329 KASSERT(TD_ON_RUNQ(td),
2330 ("sched_rem: thread not on run queue"));
2331 tdq_runq_rem(tdq, ts);
2332 tdq_load_rem(tdq, ts);
2334 if (td->td_priority == tdq->tdq_lowpri)
2335 tdq_setlowpri(tdq, NULL);
2339 * Fetch cpu utilization information. Updates on demand.
2342 sched_pctcpu(struct thread *td)
2345 struct td_sched *ts;
2356 sched_pctcpu_update(ts);
2357 /* How many rtick per second ? */
2358 rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
2359 pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
2367 * Enforce affinity settings for a thread. Called after adjustments to
2371 sched_affinity(struct thread *td)
2374 struct td_sched *ts;
2377 THREAD_LOCK_ASSERT(td, MA_OWNED);
2379 if (THREAD_CAN_SCHED(td, ts->ts_cpu))
2381 if (!TD_IS_RUNNING(td))
2383 td->td_flags |= TDF_NEEDRESCHED;
2384 if (!THREAD_CAN_MIGRATE(td))
2387 * Assign the new cpu and force a switch before returning to
2388 * userspace. If the target thread is not running locally send
2389 * an ipi to force the issue.
2392 ts->ts_cpu = sched_pickcpu(ts, 0);
2393 if (cpu != PCPU_GET(cpuid))
2394 ipi_selected(1 << cpu, IPI_PREEMPT);
2399 * Bind a thread to a target cpu.
2402 sched_bind(struct thread *td, int cpu)
2404 struct td_sched *ts;
2406 THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
2408 if (ts->ts_flags & TSF_BOUND)
2410 ts->ts_flags |= TSF_BOUND;
2412 if (PCPU_GET(cpuid) == cpu)
2415 /* When we return from mi_switch we'll be on the correct cpu. */
2416 mi_switch(SW_VOL, NULL);
2420 * Release a bound thread.
2423 sched_unbind(struct thread *td)
2425 struct td_sched *ts;
2427 THREAD_LOCK_ASSERT(td, MA_OWNED);
2429 if ((ts->ts_flags & TSF_BOUND) == 0)
2431 ts->ts_flags &= ~TSF_BOUND;
2436 sched_is_bound(struct thread *td)
2438 THREAD_LOCK_ASSERT(td, MA_OWNED);
2439 return (td->td_sched->ts_flags & TSF_BOUND);
2446 sched_relinquish(struct thread *td)
2449 SCHED_STAT_INC(switch_relinquish);
2450 mi_switch(SW_VOL, NULL);
2455 * Return the total system load.
2465 for (i = 0; i <= mp_maxid; i++)
2466 total += TDQ_CPU(i)->tdq_sysload;
2469 return (TDQ_SELF()->tdq_sysload);
2474 sched_sizeof_proc(void)
2476 return (sizeof(struct proc));
2480 sched_sizeof_thread(void)
2482 return (sizeof(struct thread) + sizeof(struct td_sched));
2486 * The actual idle process.
2489 sched_idletd(void *dummy)
2496 mtx_assert(&Giant, MA_NOTOWNED);
2497 /* ULE relies on preemption for idle interruption. */
2509 * A CPU is entering for the first time or a thread is exiting.
2512 sched_throw(struct thread *td)
2514 struct thread *newtd;
2519 /* Correct spinlock nesting and acquire the correct lock. */
2523 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2524 tdq_load_rem(tdq, td->td_sched);
2525 lock_profile_release_lock(&TDQ_LOCKPTR(tdq)->lock_object);
2527 KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
2528 newtd = choosethread();
2529 TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
2530 PCPU_SET(switchtime, cpu_ticks());
2531 PCPU_SET(switchticks, ticks);
2532 cpu_throw(td, newtd); /* doesn't return */
2536 * This is called from fork_exit(). Just acquire the correct locks and
2537 * let fork do the rest of the work.
2540 sched_fork_exit(struct thread *td)
2542 struct td_sched *ts;
2547 * Finish setting up thread glue so that it begins execution in a
2548 * non-nested critical section with the scheduler lock held.
2550 cpuid = PCPU_GET(cpuid);
2551 tdq = TDQ_CPU(cpuid);
2553 if (TD_IS_IDLETHREAD(td))
2554 td->td_lock = TDQ_LOCKPTR(tdq);
2555 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
2556 td->td_oncpu = cpuid;
2557 TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
2558 lock_profile_obtain_lock_success(
2559 &TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
2560 tdq->tdq_lowpri = td->td_priority;
2563 static SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0,
2565 SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0,
2567 SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
2568 "Slice size for timeshare threads");
2569 SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
2570 "Interactivity score threshold");
2571 SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh,
2572 0,"Min priority for preemption, lower priorities have greater precedence");
2574 SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
2575 "Number of hz ticks to keep thread affinity for");
2576 SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
2577 "Enables the long-term load balancer");
2578 SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
2579 &balance_interval, 0,
2580 "Average frequency in stathz ticks to run the long-term balancer");
2581 SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0,
2582 "Steals work from another hyper-threaded core on idle");
2583 SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
2584 "Attempts to steal work from other cores before idling");
2585 SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
2586 "Minimum load on remote cpu before we'll steal");
2589 /* ps compat. All cpu percentages from ULE are weighted. */
2590 static int ccpu = 0;
2591 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
2594 #define KERN_SWITCH_INCLUDE 1
2595 #include "kern/kern_switch.c"