37#include <sys/kernel.h>
45#include <sys/sysctl.h>
47#include <machine/cpu.h>
51#define KTR_CRITICAL KTR_SCHED
58#error "The FULL_PREEMPTION option requires the PREEMPTION option"
83SYSCTL_NODE(_kern_sched, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88SCHED_STAT_DEFINE_VAR(uncategorized,
89 &DPCPU_NAME(sched_switch_stats[SWT_NONE]),
"");
90SCHED_STAT_DEFINE_VAR(preempt,
91 &DPCPU_NAME(sched_switch_stats[SWT_PREEMPT]),
"");
92SCHED_STAT_DEFINE_VAR(owepreempt,
93 &DPCPU_NAME(sched_switch_stats[SWT_OWEPREEMPT]),
"");
95 &DPCPU_NAME(sched_switch_stats[SWT_TURNSTILE]),
"");
96SCHED_STAT_DEFINE_VAR(sleepq,
97 &DPCPU_NAME(sched_switch_stats[SWT_SLEEPQ]),
"");
98SCHED_STAT_DEFINE_VAR(sleepqtimo,
99 &DPCPU_NAME(sched_switch_stats[SWT_SLEEPQTIMO]),
"");
100SCHED_STAT_DEFINE_VAR(relinquish,
101 &DPCPU_NAME(sched_switch_stats[SWT_RELINQUISH]),
"");
102SCHED_STAT_DEFINE_VAR(needresched,
103 &DPCPU_NAME(sched_switch_stats[SWT_NEEDRESCHED]),
"");
104SCHED_STAT_DEFINE_VAR(idle,
105 &DPCPU_NAME(sched_switch_stats[SWT_IDLE]),
"");
106SCHED_STAT_DEFINE_VAR(iwait,
107 &DPCPU_NAME(sched_switch_stats[SWT_IWAIT]),
"");
109 &DPCPU_NAME(sched_switch_stats[SWT_SUSPEND]),
"");
110SCHED_STAT_DEFINE_VAR(remotepreempt,
111 &DPCPU_NAME(sched_switch_stats[SWT_REMOTEPREEMPT]),
"");
112SCHED_STAT_DEFINE_VAR(remotewakeidle,
113 &DPCPU_NAME(sched_switch_stats[SWT_REMOTEWAKEIDLE]),
"");
116sysctl_stats_reset(SYSCTL_HANDLER_ARGS)
118 struct sysctl_oid *p;
126 if (error != 0 || req->newptr == NULL)
134 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
135 if (p == oidp || p->oid_arg1 == NULL)
137 counter = (uintptr_t)p->oid_arg1;
139 *(
long *)(dpcpu_off[i] + counter) = 0;
146 CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_MPSAFE, NULL, 0,
147 sysctl_stats_reset,
"I",
148 "Reset scheduler statistics");
158static __noinline
struct thread *
167 if (((td->td_proc->p_flag & P_SYSTEM) == 0 &&
168 (td->td_flags & TDF_INPANIC) == 0)) {
186 if (KERNEL_PANICKED())
207 struct thread *td = curthread;
210 CTR4(
KTR_CRITICAL,
"critical_enter by thread %p (%ld, %s) to %d", td,
211 (
long)td->td_proc->p_pid, td->td_name, td->td_critnest);
227 if (td->td_critnest != 0)
240 flags = SW_INVOL | SW_PREEMPT;
241 if (TD_IS_IDLETHREAD(td))
244 flags |= SWT_OWEPREEMPT;
252 struct thread *td = curthread;
255 CTR4(
KTR_CRITICAL,
"critical_exit by thread %p (%ld, %s) to %d", td,
256 (
long)td->td_proc->p_pid, td->td_name, td->td_critnest);
270 bzero(rq,
sizeof *rq);
271 for (i = 0; i < RQ_NQS; i++)
272 TAILQ_INIT(&rq->rq_queues[i]);
284 rqb = &rq->rq_status;
285 CTR4(KTR_RUNQ,
"runq_clrbit: bits=%#x %#x bit=%#x word=%d",
286 rqb->rqb_bits[RQB_WORD(pri)],
287 rqb->rqb_bits[RQB_WORD(pri)] & ~RQB_BIT(pri),
288 RQB_BIT(pri), RQB_WORD(pri));
289 rqb->rqb_bits[RQB_WORD(pri)] &= ~RQB_BIT(pri);
303 rqb = &rq->rq_status;
304 for (i = 0; i < RQB_LEN; i++)
305 if (rqb->rqb_bits[i]) {
306 pri = RQB_FFS(rqb->rqb_bits[i]) + (i << RQB_L2BPW);
307 CTR3(KTR_RUNQ,
"runq_findbit: bits=%#x i=%d pri=%d",
308 rqb->rqb_bits[i], i, pri);
325 mask = (rqb_word_t)-1 << (pri & (RQB_BPW - 1));
326 rqb = &rq->rq_status;
328 for (i = RQB_WORD(pri); i < RQB_LEN;
mask = -1, i++) {
332 pri = RQB_FFS(
mask) + (i << RQB_L2BPW);
333 CTR3(KTR_RUNQ,
"runq_findbit_from: bits=%#x i=%d pri=%d",
356 rqb = &rq->rq_status;
357 CTR4(KTR_RUNQ,
"runq_setbit: bits=%#x %#x bit=%#x word=%d",
358 rqb->rqb_bits[RQB_WORD(pri)],
359 rqb->rqb_bits[RQB_WORD(pri)] | RQB_BIT(pri),
360 RQB_BIT(pri), RQB_WORD(pri));
361 rqb->rqb_bits[RQB_WORD(pri)] |= RQB_BIT(pri);
374 pri = td->td_priority / RQ_PPQ;
375 td->td_rqindex = pri;
377 rqh = &rq->rq_queues[pri];
378 CTR4(KTR_RUNQ,
"runq_add: td=%p pri=%d %d rqh=%p",
379 td, td->td_priority, pri, rqh);
380 if (
flags & SRQ_PREEMPTED) {
381 TAILQ_INSERT_HEAD(rqh, td, td_runq);
383 TAILQ_INSERT_TAIL(rqh, td, td_runq);
392 KASSERT(pri < RQ_NQS, (
"runq_add_pri: %d out of range", pri));
393 td->td_rqindex = pri;
395 rqh = &rq->rq_queues[pri];
396 CTR4(KTR_RUNQ,
"runq_add_pri: td=%p pri=%d idx=%d rqh=%p",
397 td, td->td_priority, pri, rqh);
398 if (
flags & SRQ_PREEMPTED) {
399 TAILQ_INSERT_HEAD(rqh, td, td_runq);
401 TAILQ_INSERT_TAIL(rqh, td, td_runq);
415 rqb = &rq->rq_status;
416 for (i = 0; i < RQB_LEN; i++)
417 if (rqb->rqb_bits[i]) {
418 CTR2(KTR_RUNQ,
"runq_check: bits=%#x i=%d",
419 rqb->rqb_bits[i], i);
422 CTR0(KTR_RUNQ,
"runq_check: empty");
438 rqh = &rq->rq_queues[pri];
446 int cpu = PCPU_GET(cpuid);
448 td2 = td = TAILQ_FIRST(rqh);
450 while (
count-- && td2) {
451 if (td2->td_lastcpu == cpu) {
455 td2 = TAILQ_NEXT(td2, td_runq);
458 td = TAILQ_FIRST(rqh);
459 KASSERT(td != NULL, (
"runq_choose_fuzz: no proc on busy queue"));
461 "runq_choose_fuzz: pri=%d thread=%p rqh=%p", pri, td, rqh);
464 CTR1(KTR_RUNQ,
"runq_choose_fuzz: idleproc pri=%d", pri);
480 rqh = &rq->rq_queues[pri];
481 td = TAILQ_FIRST(rqh);
482 KASSERT(td != NULL, (
"runq_choose: no thread on busy queue"));
484 "runq_choose: pri=%d thread=%p rqh=%p", pri, td, rqh);
487 CTR1(KTR_RUNQ,
"runq_choose: idlethread pri=%d", pri);
500 rqh = &rq->rq_queues[pri];
501 td = TAILQ_FIRST(rqh);
502 KASSERT(td != NULL, (
"runq_choose: no thread on busy queue"));
504 "runq_choose_from: pri=%d thread=%p idx=%d rqh=%p",
505 pri, td, td->td_rqindex, rqh);
508 CTR1(KTR_RUNQ,
"runq_choose_from: idlethread pri=%d", pri);
530 KASSERT(td->td_flags & TDF_INMEM,
531 (
"runq_remove_idx: thread swapped out"));
532 pri = td->td_rqindex;
533 KASSERT(pri < RQ_NQS, (
"runq_remove_idx: Invalid index %d\n", pri));
534 rqh = &rq->rq_queues[pri];
535 CTR4(KTR_RUNQ,
"runq_remove_idx: td=%p, pri=%d %d rqh=%p",
536 td, td->td_priority, pri, rqh);
537 TAILQ_REMOVE(rqh, td, td_runq);
538 if (TAILQ_EMPTY(rqh)) {
539 CTR0(KTR_RUNQ,
"runq_remove_idx: empty");
541 if (idx != NULL && *idx == pri)
542 *idx = (pri + 1) % RQ_NQS;
METHOD int suspend
This is called by the power-management subsystem when a suspend has been requested by the user or by ...
SYSCTL_NODE(_kern, OID_AUTO, binmisc, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "Image activator for miscellaneous binaries")
SYSCTL_PROC(_kern_binmisc, OID_AUTO, add, CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_ADD, sysctl_kern_binmisc, "S,ximgact_binmisc_entry", "Add an activator entry")
DPCPU_DEFINE(sbintime_t, hardclocktime)
void runq_add(struct runq *rq, struct thread *td, int flags)
struct thread * choosethread(void)
void runq_remove(struct runq *rq, struct thread *td)
static __inline int runq_findbit_from(struct runq *rq, u_char pri)
void runq_init(struct runq *rq)
struct thread * runq_choose_fuzz(struct runq *rq, int fuzz)
static __inline void runq_clrbit(struct runq *rq, int pri)
static __inline void runq_setbit(struct runq *rq, int pri)
struct thread * runq_choose(struct runq *rq)
void critical_exit_KBI(void)
void runq_add_pri(struct runq *rq, struct thread *td, u_char pri, int flags)
static int kern_sched_preemption
static __noinline struct thread * choosethread_panic(struct thread *td)
void runq_remove_idx(struct runq *rq, struct thread *td, u_char *idx)
static __inline int runq_findbit(struct runq *rq)
SYSCTL_INT(_kern_sched, OID_AUTO, preemption, CTLFLAG_RD, &kern_sched_preemption, 0, "Kernel preemption enabled")
int runq_check(struct runq *rq)
void __noinline critical_exit_preempt(void)
CTASSERT((RQB_BPW *RQB_LEN)==RQ_NQS)
void critical_enter_KBI(void)
struct thread * runq_choose_from(struct runq *rq, u_char idx)
void mi_switch(int flags)
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
struct thread * sched_choose(void)
u_char __read_frequently kdb_active