38 #include <sys/cdefs.h>
41 #include "opt_hwpmc_hooks.h"
42 #include "opt_kdtrace.h"
43 #include "opt_sched.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
51 #include <sys/mutex.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sched.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysproto.h>
61 #include <sys/turnstile.h>
63 #include <sys/vmmeter.h>
64 #include <sys/cpuset.h>
68 #include <sys/pmckern.h>
72 #include <sys/dtrace_bsd.h>
73 int dtrace_vtime_active;
74 dtrace_vtime_switch_func_t dtrace_vtime_switch_func;
77 #include <machine/cpu.h>
78 #include <machine/smp.h>
80 #if defined(__powerpc__) && defined(E500)
81 #error "This architecture is not currently compatible with ULE"
86 #define TS_NAME_LEN (MAXCOMLEN + sizeof(" td ") + sizeof(__XSTRING(UINT_MAX)))
87 #define TDQ_NAME_LEN (sizeof("sched lock ") + sizeof(__XSTRING(MAXCPU)))
88 #define TDQ_LOADNAME_LEN (sizeof("CPU ") + sizeof(__XSTRING(MAXCPU)) - 1 + sizeof(" load"))
110 #define TSF_BOUND 0x0001
111 #define TSF_XFERABLE 0x0002
115 #define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0)
116 #define THREAD_CAN_SCHED(td, cpu) \
117 CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
126 #define PRI_TIMESHARE_RANGE (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE + 1)
127 #define PRI_INTERACT_RANGE ((PRI_TIMESHARE_RANGE - SCHED_PRI_NRESV) / 2)
128 #define PRI_BATCH_RANGE (PRI_TIMESHARE_RANGE - PRI_INTERACT_RANGE)
130 #define PRI_MIN_INTERACT PRI_MIN_TIMESHARE
131 #define PRI_MAX_INTERACT (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE - 1)
132 #define PRI_MIN_BATCH (PRI_MIN_TIMESHARE + PRI_INTERACT_RANGE)
133 #define PRI_MAX_BATCH PRI_MAX_TIMESHARE
145 #define SCHED_TICK_SECS 10
146 #define SCHED_TICK_TARG (hz * SCHED_TICK_SECS)
147 #define SCHED_TICK_MAX (SCHED_TICK_TARG + hz)
148 #define SCHED_TICK_SHIFT 10
149 #define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT)
150 #define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz))
164 #define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN)
165 #define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2)
166 #define SCHED_PRI_MIN (PRI_MIN_BATCH + SCHED_PRI_NHALF)
167 #define SCHED_PRI_MAX (PRI_MAX_BATCH - SCHED_PRI_NHALF)
168 #define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1)
169 #define SCHED_PRI_TICKS(ts) \
170 (SCHED_TICK_HZ((ts)) / \
171 (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
172 #define SCHED_PRI_NICE(nice) (nice)
186 #define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT)
187 #define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT)
188 #define SCHED_INTERACT_MAX (100)
189 #define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2)
190 #define SCHED_INTERACT_THRESH (30)
193 #define TDF_SLICEEND TDF_SCHED2
208 #ifdef FULL_PREEMPTION
249 #define TDQ_RUNNING 1
253 struct cpu_group *cpu_top;
255 #define SCHED_AFFINITY_DEFAULT (max(1, hz / 1000))
256 #define SCHED_AFFINITY(ts, t) ((ts)->ts_rltick > ticks - ((t) * affinity))
261 static int rebalance = 1;
262 static int balance_interval = 128;
264 static int steal_idle = 1;
265 static int steal_thresh = 2;
271 static struct tdq *balance_tdq;
272 static int balance_ticks;
275 #define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)])
276 #define TDQ_CPU(x) (&tdq_cpu[(x)])
277 #define TDQ_ID(x) ((int)((x) - tdq_cpu))
281 #define TDQ_ID(x) (0)
282 #define TDQ_SELF() (&tdq_cpu)
283 #define TDQ_CPU(x) (&tdq_cpu)
286 #define TDQ_LOCK_ASSERT(t, type) mtx_assert(TDQ_LOCKPTR((t)), (type))
287 #define TDQ_LOCK(t) mtx_lock_spin(TDQ_LOCKPTR((t)))
288 #define TDQ_LOCK_FLAGS(t, f) mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
289 #define TDQ_UNLOCK(t) mtx_unlock_spin(TDQ_LOCKPTR((t)))
290 #define TDQ_LOCKPTR(t) (&(t)->tdq_lock)
309 static void tdq_add(
struct tdq *,
struct thread *,
int);
311 static int tdq_move(
struct tdq *,
struct tdq *);
312 static int tdq_idled(
struct tdq *);
313 static void tdq_notify(
struct tdq *,
struct thread *);
314 static struct thread *tdq_steal(
struct tdq *,
int);
315 static struct thread *runq_steal(
struct runq *,
int);
316 static int sched_pickcpu(
struct thread *,
int);
317 static void sched_balance(
void);
318 static int sched_balance_pair(
struct tdq *,
struct tdq *);
319 static inline struct tdq *sched_setcpu(
struct thread *,
int,
int);
322 static int sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS);
323 static int sysctl_kern_sched_topology_spec_internal(
struct sbuf *sb,
324 struct cpu_group *cg,
int indent);
337 "struct proc *",
"uint8_t");
339 "struct proc *",
"void *");
341 "struct proc *",
"void *",
"int");
343 "struct proc *",
"uint8_t",
"struct thread *");
364 for (i = 0; i < RQB_LEN; i++) {
365 printf(
"\t\trunq bits %d 0x%zx\n",
366 i, rq->rq_status.rqb_bits[i]);
367 for (j = 0; j < RQB_BPW; j++)
368 if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
369 pri = j + (i << RQB_L2BPW);
370 rqh = &rq->rq_queues[pri];
371 TAILQ_FOREACH(td, rqh, td_runq) {
372 printf(
"\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
373 td, td->td_name, td->td_priority,
374 td->td_rqindex, pri);
400 printf(
"\trealtime runq:\n");
402 printf(
"\ttimeshare runq:\n");
420 if (cpri >= PRI_MIN_IDLE)
453 THREAD_LOCK_ASSERT(td, MA_OWNED);
455 pri = td->td_priority;
467 (
"Invalid priority %d on timeshare runq", pri));
472 if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
474 pri = (pri + tdq->
tdq_idx) % RQ_NQS;
482 pri = (
unsigned char)(pri - 1) % RQ_NQS;
505 (
"tdq_runq_remove: thread %p null ts_runq", td));
528 THREAD_LOCK_ASSERT(td, MA_OWNED);
531 if ((td->td_flags & TDF_NOLOAD) == 0)
533 KTR_COUNTER0(KTR_SCHED,
"load", tdq->tdq_loadname, tdq->
tdq_load);
534 SDT_PROBE2(sched, , , load__change, (
int)
TDQ_ID(tdq), tdq->
tdq_load);
545 THREAD_LOCK_ASSERT(td, MA_OWNED);
548 (
"tdq_load_rem: Removing with 0 load on queue %d",
TDQ_ID(tdq)));
551 if ((td->td_flags & TDF_NOLOAD) == 0)
553 KTR_COUNTER0(KTR_SCHED,
"load", tdq->tdq_loadname, tdq->
tdq_load);
554 SDT_PROBE2(sched, , , load__change, (
int)
TDQ_ID(tdq), tdq->
tdq_load);
570 if (td == NULL || td->td_priority > ctd->td_priority)
586 #define CPU_SEARCH_LOWEST 0x1
587 #define CPU_SEARCH_HIGHEST 0x2
588 #define CPU_SEARCH_BOTH (CPU_SEARCH_LOWEST|CPU_SEARCH_HIGHEST)
590 #define CPUSET_FOREACH(cpu, mask) \
591 for ((cpu) = 0; (cpu) <= mp_maxid; (cpu)++) \
592 if (CPU_ISSET(cpu, &mask))
594 static __inline
int cpu_search(
const struct cpu_group *cg,
struct cpu_search *low,
595 struct cpu_search *high,
const int match);
596 int cpu_search_lowest(
const struct cpu_group *cg,
struct cpu_search *low);
597 int cpu_search_highest(
const struct cpu_group *cg,
struct cpu_search *high);
598 int cpu_search_both(
const struct cpu_group *cg,
struct cpu_search *low,
599 struct cpu_search *high);
613 cpu_search(
const struct cpu_group *cg,
struct cpu_search *low,
614 struct cpu_search *high,
const int match)
616 struct cpu_search lgroup;
617 struct cpu_search hgroup;
619 struct cpu_group *child;
621 int cpu, i, hload, lload, load, total, rnd, *rndptr;
624 cpumask = cg->cg_mask;
625 if (match & CPU_SEARCH_LOWEST) {
629 if (match & CPU_SEARCH_HIGHEST) {
635 for (i = cg->cg_children, cpu =
mp_maxid; ; ) {
637 #ifdef HAVE_INLINE_FFSL
638 cpu = CPU_FFS(&cpumask) - 1;
640 while (cpu >= 0 && !CPU_ISSET(cpu, &cpumask))
647 child = &cg->cg_child[i - 1];
649 if (match & CPU_SEARCH_LOWEST)
651 if (match & CPU_SEARCH_HIGHEST)
654 CPU_NAND(&cpumask, &child->cg_mask);
656 case CPU_SEARCH_LOWEST:
657 load = cpu_search_lowest(child, &lgroup);
659 case CPU_SEARCH_HIGHEST:
660 load = cpu_search_highest(child, &hgroup);
662 case CPU_SEARCH_BOTH:
663 load = cpu_search_both(child, &lgroup, &hgroup);
667 CPU_CLR(cpu, &cpumask);
670 rndptr = DPCPU_PTR(randomval);
671 rnd = (*rndptr = *rndptr * 69069 + 5) >> 26;
672 if (match & CPU_SEARCH_LOWEST) {
673 if (cpu == low->cs_prefer)
678 CPU_ISSET(cpu, &lgroup.cs_mask)) {
680 lgroup.cs_load = load - rnd;
683 if (match & CPU_SEARCH_HIGHEST)
684 if (tdq->
tdq_load >= hgroup.cs_limit &&
686 CPU_ISSET(cpu, &hgroup.cs_mask)) {
688 hgroup.cs_load = load - rnd;
694 if (match & CPU_SEARCH_LOWEST) {
695 if (lgroup.cs_cpu >= 0 &&
697 (load == lload && lgroup.cs_load < low->cs_load))) {
699 low->cs_cpu = lgroup.cs_cpu;
700 low->cs_load = lgroup.cs_load;
703 if (match & CPU_SEARCH_HIGHEST)
704 if (hgroup.cs_cpu >= 0 &&
706 (load == hload && hgroup.cs_load > high->cs_load))) {
708 high->cs_cpu = hgroup.cs_cpu;
709 high->cs_load = hgroup.cs_load;
713 if (i == 0 && CPU_EMPTY(&cpumask))
716 #ifndef HAVE_INLINE_FFSL
729 cpu_search_lowest(
const struct cpu_group *cg,
struct cpu_search *low)
731 return cpu_search(cg, low, NULL, CPU_SEARCH_LOWEST);
735 cpu_search_highest(
const struct cpu_group *cg,
struct cpu_search *high)
737 return cpu_search(cg, NULL, high, CPU_SEARCH_HIGHEST);
741 cpu_search_both(
const struct cpu_group *cg,
struct cpu_search *low,
742 struct cpu_search *high)
744 return cpu_search(cg, low, high, CPU_SEARCH_BOTH);
753 sched_lowest(
const struct cpu_group *cg, cpuset_t
mask,
int pri,
int maxload,
756 struct cpu_search low;
759 low.cs_prefer = prefer;
762 low.cs_limit = maxload;
763 cpu_search_lowest(cg, &low);
771 sched_highest(
const struct cpu_group *cg, cpuset_t
mask,
int minload)
773 struct cpu_search high;
777 high.cs_limit = minload;
778 cpu_search_highest(cg, &high);
783 sched_balance_group(
struct cpu_group *cg)
785 cpuset_t hmask, lmask;
786 int high, low, anylow;
790 high = sched_highest(cg, hmask, 1);
794 CPU_CLR(high, &hmask);
795 CPU_COPY(&hmask, &lmask);
797 if (CPU_EMPTY(&lmask))
801 low = sched_lowest(cg, lmask, -1,
804 if (anylow && low == -1)
812 CPU_CLR(low, &hmask);
819 CPU_CLR(low, &lmask);
835 balance_ticks = max(balance_interval / 2, 1);
836 balance_ticks += random() % balance_interval;
841 sched_balance_group(cpu_top);
849 tdq_lock_pair(
struct tdq *one,
struct tdq *two)
864 tdq_unlock_pair(
struct tdq *one,
struct tdq *two)
874 sched_balance_pair(
struct tdq *high,
struct tdq *low)
879 tdq_lock_pair(high, low);
886 (moved = tdq_move(high, low)) > 0) {
893 if (cpu != PCPU_GET(cpuid))
894 ipi_cpu(cpu, IPI_PREEMPT);
897 tdq_unlock_pair(high, low);
905 tdq_move(
struct tdq *from,
struct tdq *to)
917 td = tdq_steal(tdq, cpu);
940 tdq_idled(
struct tdq *tdq)
942 struct cpu_group *cg;
951 CPU_CLR(PCPU_GET(cpuid), &mask);
954 for (cg = tdq->
tdq_cg; cg != NULL; ) {
955 if ((cg->cg_flags & CG_FLAG_THREAD) == 0)
956 thresh = steal_thresh;
959 cpu = sched_highest(cg, mask, thresh);
966 tdq_lock_pair(tdq, steal);
968 tdq_unlock_pair(tdq, steal);
977 if (tdq->
tdq_load == 0 && tdq_move(steal, tdq) == 0) {
978 tdq_unlock_pair(tdq, steal);
984 thread_unlock(curthread);
996 tdq_notify(
struct tdq *tdq,
struct thread *td)
1004 cpu = td->td_sched->ts_cpu;
1005 pri = td->td_priority;
1017 if (TD_IS_IDLETHREAD(ctd)) {
1026 ipi_cpu(cpu, IPI_PREEMPT);
1033 static struct thread *
1034 runq_steal_from(
struct runq *rq,
int cpu, u_char
start)
1038 struct thread *td, *first;
1043 rqb = &rq->rq_status;
1044 bit = start & (RQB_BPW -1);
1048 for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
1049 if (rqb->rqb_bits[i] == 0)
1052 for (pri = bit; pri < RQB_BPW; pri++)
1053 if (rqb->rqb_bits[i] & (1ul << pri))
1058 pri = RQB_FFS(rqb->rqb_bits[i]);
1059 pri += (i << RQB_L2BPW);
1060 rqh = &rq->rq_queues[pri];
1061 TAILQ_FOREACH(td, rqh, td_runq) {
1082 static struct thread *
1083 runq_steal(
struct runq *rq,
int cpu)
1091 rqb = &rq->rq_status;
1092 for (word = 0; word < RQB_LEN; word++) {
1093 if (rqb->rqb_bits[word] == 0)
1095 for (bit = 0; bit < RQB_BPW; bit++) {
1096 if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
1098 rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
1099 TAILQ_FOREACH(td, rqh, td_runq)
1111 static struct thread *
1112 tdq_steal(struct tdq *tdq,
int cpu)
1117 if ((td = runq_steal(&tdq->tdq_realtime, cpu)) != NULL)
1119 if ((td = runq_steal_from(&tdq->tdq_timeshare,
1120 cpu, tdq->tdq_ridx)) != NULL)
1122 return (runq_steal(&tdq->tdq_idle, cpu));
1129 static inline struct tdq *
1130 sched_setcpu(
struct thread *td,
int cpu,
int flags)
1135 THREAD_LOCK_ASSERT(td, MA_OWNED);
1137 td->td_sched->ts_cpu = cpu;
1149 if (TD_CAN_RUN(td)) {
1167 SCHED_STAT_DEFINE(pickcpu_intrbind,
"Soft interrupt binding");
1168 SCHED_STAT_DEFINE(pickcpu_idle_affinity,
"Picked idle cpu based on affinity");
1169 SCHED_STAT_DEFINE(pickcpu_affinity,
"Picked cpu based on affinity");
1170 SCHED_STAT_DEFINE(pickcpu_lowest,
"Selected lowest load");
1171 SCHED_STAT_DEFINE(pickcpu_local,
"Migrated to current cpu");
1172 SCHED_STAT_DEFINE(pickcpu_migration,
"Selection may have caused migration");
1175 sched_pickcpu(
struct thread *td,
int flags)
1177 struct cpu_group *cg, *ccg;
1183 self = PCPU_GET(cpuid);
1196 pri = td->td_priority;
1198 curthread->td_intr_nesting_level && ts->
ts_cpu !=
self) {
1199 SCHED_STAT_INC(pickcpu_intrbind);
1202 SCHED_STAT_INC(pickcpu_affinity);
1214 SCHED_AFFINITY(ts, CG_SHARE_L2)) {
1215 if (cg->cg_flags & CG_FLAG_THREAD) {
1216 CPUSET_FOREACH(cpu, cg->cg_mask) {
1223 SCHED_STAT_INC(pickcpu_idle_affinity);
1232 for (ccg = NULL; cg != NULL; cg = cg->cg_parent) {
1233 if (cg->cg_flags & CG_FLAG_THREAD)
1235 if (!SCHED_AFFINITY(ts, cg->cg_level))
1243 mask = td->td_cpuset->cs_mask;
1244 if (cg != NULL && cg != cpu_top &&
1245 CPU_CMP(&cg->cg_mask, &cpu_top->cg_mask) != 0)
1246 cpu = sched_lowest(cg, mask, max(pri, PRI_MAX_TIMESHARE),
1250 cpu = sched_lowest(cpu_top, mask, pri, INT_MAX, ts->
ts_cpu);
1253 cpu = sched_lowest(cpu_top, mask, -1, INT_MAX, ts->
ts_cpu);
1254 KASSERT(cpu != -1, (
"sched_pickcpu: Failed to find a cpu."));
1261 SCHED_STAT_INC(pickcpu_local);
1264 SCHED_STAT_INC(pickcpu_lowest);
1266 SCHED_STAT_INC(pickcpu_migration);
1274 static struct thread *
1286 (
"tdq_choose: Invalid priority on timeshare queue %d",
1292 KASSERT(td->td_priority >= PRI_MIN_IDLE,
1293 (
"tdq_choose: Invalid priority on idle queue %d",
1314 "sched lock %d", (
int)
TDQ_ID(tdq));
1316 MTX_SPIN | MTX_RECURSE);
1318 snprintf(tdq->tdq_loadname,
sizeof(tdq->tdq_loadname),
1319 "CPU %d load", (
int)
TDQ_ID(tdq));
1325 sched_setup_smp(
void)
1330 cpu_top = smp_topo();
1334 tdq->
tdq_cg = smp_topo_find(cpu_top, i);
1336 panic(
"Can't find cpu group for %d\n", i);
1399 affinity = SCHED_AFFINITY_DEFAULT;
1459 if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE)
1480 (
"sched_priority: invalid interactive priority %d score %d",
1484 if (td->td_sched->ts_ticks)
1489 (
"sched_priority: invalid priority %d: nice %d, "
1490 "ticks %d ftick %d ltick %d tick pri %d",
1491 pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
1492 td->td_sched->ts_ftick, td->td_sched->ts_ltick,
1556 sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
1559 td->td_sched->ts_runtime /= ratio;
1560 td->td_sched->ts_slptime /= ratio;
1574 proc0.p_sched = NULL;
1630 KTR_POINT3(KTR_SCHED,
"thread",
sched_tdname(td),
"prio",
1631 "prio:%d", td->td_priority,
"new prio:%d", prio,
1633 SDT_PROBE3(sched, , , change__pri, td, td->td_proc, prio);
1634 if (td != curthread && prio < td->td_priority) {
1635 KTR_POINT3(KTR_SCHED,
"thread",
sched_tdname(curthread),
1636 "lend prio",
"prio:%d", td->td_priority,
"new prio:%d",
1638 SDT_PROBE4(sched, , , lend__pri, td, td->td_proc, prio,
1642 THREAD_LOCK_ASSERT(td, MA_OWNED);
1643 if (td->td_priority == prio)
1651 if (TD_ON_RUNQ(td) && prio < td->td_priority) {
1653 td->td_priority = prio;
1661 if (TD_IS_RUNNING(td)) {
1663 oldpri = td->td_priority;
1664 td->td_priority = prio;
1671 td->td_priority = prio;
1682 td->td_flags |= TDF_BORROWING;
1699 if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1700 td->td_base_pri <= PRI_MAX_TIMESHARE)
1701 base_pri = td->td_user_pri;
1703 base_pri = td->td_base_pri;
1704 if (prio >= base_pri) {
1705 td->td_flags &= ~TDF_BORROWING;
1720 td->td_base_pri = prio;
1726 if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1730 oldprio = td->td_priority;
1737 if (TD_ON_LOCK(td) && oldprio != prio)
1748 td->td_base_user_pri = prio;
1749 if (td->td_lend_user_pri <= prio)
1751 td->td_user_pri = prio;
1758 THREAD_LOCK_ASSERT(td, MA_OWNED);
1759 td->td_lend_user_pri = prio;
1760 td->td_user_pri = min(prio, td->td_base_user_pri);
1761 if (td->td_priority > td->td_user_pri)
1763 else if (td->td_priority != td->td_user_pri)
1764 td->td_flags |= TDF_NEEDRESCHED;
1776 tdn =
TDQ_CPU(td->td_sched->ts_cpu);
1795 tdq_lock_pair(tdn, tdq);
1797 tdq_notify(tdn, td);
1811 atomic_store_rel_ptr((
volatile uintptr_t *)&td->td_lock,
1828 int cpuid, preempted;
1830 THREAD_LOCK_ASSERT(td, MA_OWNED);
1831 KASSERT(newtd == NULL, (
"sched_switch: Unsupported newtd argument"));
1833 cpuid = PCPU_GET(cpuid);
1839 td->td_lastcpu = td->td_oncpu;
1840 td->td_oncpu = NOCPU;
1842 (flags & SWT_RELINQUISH));
1844 td->td_owepreempt = 0;
1845 if (!TD_IS_IDLETHREAD(td))
1851 if (TD_IS_IDLETHREAD(td)) {
1854 }
else if (TD_IS_RUNNING(td)) {
1856 srqflag = preempted ?
1857 SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1858 SRQ_OURSELF|SRQ_YIELDING;
1861 ts->
ts_cpu = sched_pickcpu(td, 0);
1868 (
"Thread %p shouldn't migrate", td));
1889 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1890 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1892 SDT_PROBE2(sched, , , off__cpu, newtd, newtd->td_proc);
1893 lock_profile_release_lock(&
TDQ_LOCKPTR(tdq)->lock_object);
1897 #ifdef KDTRACE_HOOKS
1903 if (dtrace_vtime_active)
1904 (*dtrace_vtime_switch_func)(newtd);
1907 cpu_switch(td, newtd, mtx);
1913 cpuid = PCPU_GET(cpuid);
1915 lock_profile_obtain_lock_success(
1916 &
TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
1918 SDT_PROBE0(sched, , , on__cpu);
1920 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1921 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1925 SDT_PROBE0(sched, , , remain__cpu);
1932 td->td_oncpu = cpuid;
1943 PROC_LOCK_ASSERT(p, MA_OWNED);
1946 FOREACH_THREAD_IN_PROC(p, td) {
1961 THREAD_LOCK_ASSERT(td, MA_OWNED);
1963 td->td_slptick =
ticks;
1964 if (TD_IS_SUSPENDED(td) || prio >= PSOCK)
1965 td->td_flags |= TDF_CANSWAP;
1966 if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE)
1984 THREAD_LOCK_ASSERT(td, MA_OWNED);
1986 td->td_flags &= ~TDF_CANSWAP;
1991 slptick = td->td_slptick;
1993 if (slptick && slptick !=
ticks) {
2010 THREAD_LOCK_ASSERT(td, MA_OWNED);
2018 td->td_sched->ts_runtime +=
tickincr;
2032 THREAD_LOCK_ASSERT(td, MA_OWNED);
2037 ts2 = child->td_sched;
2038 child->td_oncpu = NOCPU;
2039 child->td_lastcpu = NOCPU;
2041 child->td_cpuset =
cpuset_ref(td->td_cpuset);
2053 child->td_priority = child->td_base_pri;
2061 bzero(ts2->ts_name,
sizeof(ts2->ts_name));
2072 THREAD_LOCK_ASSERT(td, MA_OWNED);
2073 if (td->td_pri_class ==
class)
2075 td->td_pri_class =
class;
2086 KTR_STATE1(KTR_SCHED,
"thread",
sched_tdname(child),
"proc exit",
2087 "prio:%d", child->td_priority);
2088 PROC_LOCK_ASSERT(p, MA_OWNED);
2089 td = FIRST_THREAD_IN_PROC(p);
2103 KTR_STATE1(KTR_SCHED,
"thread",
sched_tdname(child),
"thread exit",
2104 "prio:%d", child->td_priority);
2111 td->td_sched->ts_runtime += child->td_sched->ts_runtime;
2122 SDT_PROBE2(sched, , , surrender, td, td->td_proc);
2131 flags = SW_INVOL | SW_PREEMPT;
2132 if (td->td_critnest > 1)
2133 td->td_owepreempt = 1;
2134 else if (TD_IS_IDLETHREAD(td))
2135 mi_switch(flags | SWT_REMOTEWAKEIDLE, NULL);
2137 mi_switch(flags | SWT_REMOTEPREEMPT, NULL);
2158 KASSERT((td->td_flags & TDF_BORROWING) == 0,
2159 (
"thread with borrowed priority returning to userland"));
2160 if (td->td_priority != td->td_user_pri) {
2162 td->td_priority = td->td_user_pri;
2163 td->td_base_pri = td->td_user_pri;
2179 THREAD_LOCK_ASSERT(td, MA_OWNED);
2185 if (balance_tdq == tdq) {
2186 if (balance_ticks && --balance_ticks == 0)
2208 if (td->td_pri_class & PRI_FIFO_BIT)
2210 if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) {
2215 td->td_sched->ts_runtime +=
tickincr;
2224 if (!TD_IS_IDLETHREAD(td) && --ts->
ts_slice <= 0) {
2252 if ((curthread->td_flags & TDF_IDLETD) != 0) {
2283 return (PCPU_GET(idlethread));
2297 THREAD_LOCK_ASSERT(curthread, MA_OWNED);
2300 pri = td->td_priority;
2301 cpri = ctd->td_priority;
2303 ctd->td_flags |= TDF_NEEDRESCHED;
2304 if (
panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
2308 ctd->td_owepreempt = 1;
2317 tdq_add(
struct tdq *tdq,
struct thread *td,
int flags)
2321 KASSERT((td->td_inhibitors == 0),
2322 (
"sched_add: trying to run inhibited thread"));
2323 KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
2324 (
"sched_add: bad thread state"));
2325 KASSERT(td->td_flags & TDF_INMEM,
2326 (
"sched_add: thread swapped out"));
2346 KTR_STATE2(KTR_SCHED,
"thread",
sched_tdname(td),
"runq add",
2347 "prio:%d", td->td_priority, KTR_ATTR_LINKED,
2349 KTR_POINT1(KTR_SCHED,
"thread",
sched_tdname(curthread),
"wokeup",
2351 SDT_PROBE4(sched, , , enqueue, td, td->td_proc, NULL,
2352 flags & SRQ_PREEMPTED);
2353 THREAD_LOCK_ASSERT(td, MA_OWNED);
2358 if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
2365 cpu = sched_pickcpu(td, flags);
2366 tdq = sched_setcpu(td, cpu, flags);
2368 if (cpu != PCPU_GET(cpuid)) {
2369 tdq_notify(tdq, td);
2382 if (!(flags & SRQ_YIELDING))
2396 KTR_STATE1(KTR_SCHED,
"thread",
sched_tdname(td),
"runq rem",
2397 "prio:%d", td->td_priority);
2398 SDT_PROBE3(sched, , , dequeue, td, td->td_proc, NULL);
2399 tdq =
TDQ_CPU(td->td_sched->ts_cpu);
2402 KASSERT(TD_ON_RUNQ(td),
2403 (
"sched_rem: thread not on run queue"));
2425 THREAD_LOCK_ASSERT(td, MA_OWNED);
2432 pctcpu = (FSCALE * ((FSCALE * rtick)/
hz)) >> FSHIFT;
2448 THREAD_LOCK_ASSERT(td, MA_OWNED);
2452 if (TD_ON_RUNQ(td)) {
2457 if (!TD_IS_RUNNING(td))
2464 td->td_flags |= TDF_NEEDRESCHED;
2465 if (td != curthread)
2466 ipi_cpu(ts->
ts_cpu, IPI_PREEMPT);
2478 THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
2479 KASSERT(td == curthread, (
"sched_bind: can only bind curthread"));
2486 if (PCPU_GET(cpuid) == cpu)
2501 THREAD_LOCK_ASSERT(td, MA_OWNED);
2502 KASSERT(td == curthread, (
"sched_unbind: can only bind curthread"));
2513 THREAD_LOCK_ASSERT(td, MA_OWNED);
2514 return (td->td_sched->ts_flags &
TSF_BOUND);
2524 mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
2540 total +=
TDQ_CPU(i)->tdq_sysload;
2550 return (
sizeof(
struct proc));
2556 return (
sizeof(
struct thread) +
sizeof(
struct td_sched));
2560 #define TDQ_IDLESPIN(tdq) \
2561 ((tdq)->tdq_cg != NULL && ((tdq)->tdq_cg->cg_flags & CG_FLAG_THREAD) == 0)
2563 #define TDQ_IDLESPIN(tdq) 1
2574 int oldswitchcnt, switchcnt;
2577 mtx_assert(&
Giant, MA_NOTOWNED);
2589 if (switchcnt != oldswitchcnt) {
2590 oldswitchcnt = switchcnt;
2591 if (tdq_idled(tdq) == 0)
2596 oldswitchcnt = switchcnt;
2615 if (tdq->
tdq_load != 0 || switchcnt != oldswitchcnt)
2634 if (switchcnt != oldswitchcnt)
2647 struct thread *newtd;
2656 PCPU_SET(switchticks,
ticks);
2660 lock_profile_release_lock(&
TDQ_LOCKPTR(tdq)->lock_object);
2661 td->td_lastcpu = td->td_oncpu;
2662 td->td_oncpu = NOCPU;
2664 KASSERT(curthread->td_md.md_spinlock_count == 1, (
"invalid count"));
2667 cpu_throw(td, newtd);
2685 cpuid = PCPU_GET(cpuid);
2688 if (TD_IS_IDLETHREAD(td))
2691 td->td_oncpu = cpuid;
2693 lock_profile_obtain_lock_success(
2694 &
TDQ_LOCKPTR(tdq)->lock_object, 0, 0, __FILE__, __LINE__);
2707 if (ts->ts_name[0] ==
'\0')
2708 snprintf(ts->ts_name,
sizeof(ts->ts_name),
2709 "%s tid %d", td->td_name, td->td_tid);
2710 return (ts->ts_name);
2712 return (td->td_name);
2718 sched_clear_tdname(
struct thread *td)
2723 ts->ts_name[0] =
'\0';
2734 sysctl_kern_sched_topology_spec_internal(
struct sbuf *sb,
struct cpu_group *cg,
2737 char cpusetbuf[CPUSETBUFSIZ];
2740 sbuf_printf(sb,
"%*s<group level=\"%d\" cache-level=\"%d\">\n", indent,
2741 "", 1 + indent / 2, cg->cg_level);
2742 sbuf_printf(sb,
"%*s <cpu count=\"%d\" mask=\"%s\">", indent,
"",
2745 for (i = 0; i < MAXCPU; i++) {
2746 if (CPU_ISSET(i, &cg->cg_mask)) {
2756 if (cg->cg_flags != 0) {
2758 if ((cg->cg_flags & CG_FLAG_HTT) != 0)
2759 sbuf_printf(sb,
"<flag name=\"HTT\">HTT group</flag>");
2760 if ((cg->cg_flags & CG_FLAG_THREAD) != 0)
2761 sbuf_printf(sb,
"<flag name=\"THREAD\">THREAD group</flag>");
2762 if ((cg->cg_flags & CG_FLAG_SMT) != 0)
2763 sbuf_printf(sb,
"<flag name=\"SMT\">SMT group</flag>");
2767 if (cg->cg_children > 0) {
2769 for (i = 0; i < cg->cg_children; i++)
2770 sysctl_kern_sched_topology_spec_internal(sb,
2771 &cg->cg_child[i], indent+2);
2783 sysctl_kern_sched_topology_spec(SYSCTL_HANDLER_ARGS)
2788 KASSERT(cpu_top != NULL, (
"cpu_top isn't initialized"));
2790 topo =
sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
2795 err = sysctl_kern_sched_topology_spec_internal(topo, cpu_top, 1);
2811 int error, new_val, period;
2816 if (error != 0 || req->newptr == NULL)
2820 sched_slice = imax(1, (new_val + period / 2) / period);
2826 SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0,
"Scheduler");
2829 SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, CTLTYPE_INT | CTLFLAG_RW,
2831 "Quantum for timeshare threads in microseconds");
2833 "Quantum for timeshare threads in stathz ticks");
2835 "Interactivity score threshold");
2838 "Maximal (lowest) priority for preemption");
2840 "Assign static kernel priorities to sleeping threads");
2842 "Number of times idle thread will spin waiting for new work");
2843 SYSCTL_INT(_kern_sched, OID_AUTO, idlespinthresh, CTLFLAG_RW,
2845 "Threshold before we will permit idle thread spinning");
2847 SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
2848 "Number of hz ticks to keep thread affinity for");
2849 SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
2850 "Enables the long-term load balancer");
2851 SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
2852 &balance_interval, 0,
2853 "Average period in stathz ticks to run the long-term balancer");
2854 SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
2855 "Attempts to steal work from other cores before idling");
2856 SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
2857 "Minimum load on remote CPU before we'll steal");
2858 SYSCTL_PROC(_kern_sched, OID_AUTO, topology_spec, CTLTYPE_STRING |
2859 CTLFLAG_RD, NULL, 0, sysctl_kern_sched_topology_spec,
"A",
2860 "XML dump of detected CPU topology");
2865 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0,
"");
static struct tdq tdq_cpu
static void sched_initticks(void *dummy)
#define TDQ_LOCK_ASSERT(t, type)
static int sched_shouldpreempt(int, int, int)
int sched_rr_interval(void)
SDT_PROBE_DEFINE2(sched,,, load__change,"int","int")
void runq_add(struct runq *rq, struct thread *td, int flags)
volatile int tdq_cpu_idle
void turnstile_adjust(struct thread *td, u_char oldpri)
void sched_lend_prio(struct thread *td, u_char prio)
struct thread * sched_choose(void)
void sched_class(struct thread *td, int class)
int snprintf(char *str, size_t size, const char *format,...)
ssize_t sbuf_len(struct sbuf *s)
void sched_exit_thread(struct thread *td, struct thread *child)
void sched_fork_thread(struct thread *td, struct thread *child)
static void sched_setup(void *dummy)
#define THREAD_CAN_SCHED(td, cpu)
static int sched_interact_score(struct thread *)
static __inline void tdq_runq_rem(struct tdq *, struct thread *)
void sched_user_prio(struct thread *td, u_char prio)
SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD,"ULE", 0,"Scheduler name")
static int sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
void panic(const char *fmt,...)
static void sched_interact_update(struct thread *)
struct thread * choosethread(void)
void runq_remove_idx(struct runq *rq, struct thread *td, u_char *idx)
char * sched_tdname(struct thread *td)
struct pcpu * pcpu_find(u_int cpuid)
static void tdq_setup(struct tdq *)
void mi_switch(int flags, struct thread *newtd)
void sched_sleep(struct thread *td, int prio)
SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL)
static void tdq_load_rem(struct tdq *, struct thread *)
SDT_PROBE_DEFINE(sched,,, on__cpu)
struct thread * runq_choose_from(struct runq *rq, u_char idx)
static int sched_idlespins
char tdq_name[TDQ_NAME_LEN]
#define SCHED_INTERACT_HALF
#define TDQ_IDLESPIN(tdq)
static int sched_idlespinthresh
void runq_add_pri(struct runq *rq, struct thread *td, u_char pri, int flags)
void runq_init(struct runq *rq)
static void tdq_setlowpri(struct tdq *tdq, struct thread *ctd)
void sched_throw(struct thread *td)
struct thread * runq_choose(struct runq *rq)
void thread_lock_set(struct thread *td, struct mtx *new)
void sched_fork_exit(struct thread *td)
struct cpuset * cpuset_ref(struct cpuset *set)
void sched_relinquish(struct thread *td)
void sched_wakeup(struct thread *td)
static struct td_sched td_sched0
int sched_sizeof_thread(void)
void sched_switch(struct thread *td, struct thread *newtd, int flags)
void sched_rem(struct thread *td)
struct cpu_group * tdq_cg
SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW,&sched_slice, 0,"Quantum for timeshare threads in stathz ticks")
int sbuf_printf(struct sbuf *s, const char *fmt,...)
#define SCHED_SLP_RUN_FORK
void sched_lend_user_prio(struct thread *td, u_char prio)
static struct thread * tdq_choose(struct tdq *)
int sched_is_bound(struct thread *td)
static struct mtx * sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
struct runq tdq_timeshare
void sched_clock(struct thread *td)
static void runq_print(struct runq *rq)
struct sbuf * sbuf_new(struct sbuf *s, char *buf, int length, int flags)
static void sched_thread_priority(struct thread *, u_char)
void runq_remove(struct runq *rq, struct thread *td)
static __inline void tdq_runq_add(struct tdq *, struct thread *, int)
#define SCHED_PRI_NICE(nice)
void sched_affinity(struct thread *td)
int printf(const char *fmt,...)
void sched_exit(struct proc *p, struct thread *child)
#define SCHED_PRI_TICKS(ts)
void sbuf_delete(struct sbuf *s)
#define TDQ_LOCK_FLAGS(t, f)
SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW, NULL, 0, sysctl_kern_quantum,"I","Quantum for timeshare threads in microseconds")
void sched_idletd(void *dummy)
void sched_unlend_prio(struct thread *td, u_char prio)
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
#define SCHED_TICK_HZ(ts)
char * sbuf_data(struct sbuf *s)
static void thread_unblock_switch(struct thread *td, struct mtx *mtx)
int sbuf_finish(struct sbuf *s)
void sched_preempt(struct thread *td)
SDT_PROBE_DEFINE4(sched,,, enqueue,"struct thread *","struct proc *","void *","int")
SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0,"Scheduler")
SDT_PROVIDER_DEFINE(sched)
static int sched_interact
static void sched_interact_fork(struct thread *)
static void sched_priority(struct thread *)
void sched_nice(struct proc *p, int nice)
static DPCPU_DEFINE(int, pcputicks)
#define SCHED_SLP_RUN_MAX
static void sched_setpreempt(struct thread *td)
char * cpusetobj_strprint(char *buf, const cpuset_t *set)
void sched_userret(struct thread *td)
void sched_fork(struct thread *td, struct thread *child)
#define SCHED_INTERACT_THRESH
static void tdq_add(struct tdq *, struct thread *, int)
struct mtx * thread_lock_block(struct thread *td)
void sched_unbind(struct thread *td)
static int preempt_thresh
#define THREAD_CAN_MIGRATE(td)
void sched_prio(struct thread *td, u_char prio)
SDT_PROBE_DEFINE3(sched,,, change__pri,"struct thread *","struct proc *","uint8_t")
void sched_add(struct thread *td, int flags)
static void tdq_load_add(struct tdq *, struct thread *)
void thread_lock_unblock(struct thread *td, struct mtx *new)
void sched_bind(struct thread *td, int cpu)
fixpt_t sched_pctcpu(struct thread *td)
int sched_sizeof_proc(void)
static void sched_pctcpu_update(struct td_sched *, int)