2 * Copyright (c) 2001 Jake Burkholder <jake@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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
34 #include <sys/mutex.h>
36 #include <sys/queue.h>
41 static struct runq runq;
42 SYSINIT(runq, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, runq_init, &runq)
45 * Wrappers which implement old interface; act on global run queue.
51 return (runq_choose(&runq)->ke_thread);
57 return runq_check(&runq);
61 remrunqueue(struct thread *td)
63 runq_remove(&runq, td->td_kse);
67 setrunqueue(struct thread *td)
69 runq_add(&runq, td->td_kse);
72 /* Critical sections that prevent preemption. */
79 if (td->td_critnest == 0)
80 td->td_savecrit = cpu_critical_enter();
90 if (td->td_critnest == 1) {
92 cpu_critical_exit(td->td_savecrit);
98 * Clear the status bit of the queue corresponding to priority level pri,
99 * indicating that it is empty.
102 runq_clrbit(struct runq *rq, int pri)
106 rqb = &rq->rq_status;
107 CTR4(KTR_RUNQ, "runq_clrbit: bits=%#x %#x bit=%#x word=%d",
108 rqb->rqb_bits[RQB_WORD(pri)],
109 rqb->rqb_bits[RQB_WORD(pri)] & ~RQB_BIT(pri),
110 RQB_BIT(pri), RQB_WORD(pri));
111 rqb->rqb_bits[RQB_WORD(pri)] &= ~RQB_BIT(pri);
115 * Find the index of the first non-empty run queue. This is done by
116 * scanning the status bits, a set bit indicates a non-empty queue.
119 runq_findbit(struct runq *rq)
125 rqb = &rq->rq_status;
126 for (i = 0; i < RQB_LEN; i++)
127 if (rqb->rqb_bits[i]) {
128 pri = (RQB_FFS(rqb->rqb_bits[i]) - 1) +
130 CTR3(KTR_RUNQ, "runq_findbit: bits=%#x i=%d pri=%d",
131 rqb->rqb_bits[i], i, pri);
139 * Set the status bit of the queue corresponding to priority level pri,
140 * indicating that it is non-empty.
143 runq_setbit(struct runq *rq, int pri)
147 rqb = &rq->rq_status;
148 CTR4(KTR_RUNQ, "runq_setbit: bits=%#x %#x bit=%#x word=%d",
149 rqb->rqb_bits[RQB_WORD(pri)],
150 rqb->rqb_bits[RQB_WORD(pri)] | RQB_BIT(pri),
151 RQB_BIT(pri), RQB_WORD(pri));
152 rqb->rqb_bits[RQB_WORD(pri)] |= RQB_BIT(pri);
155 #if defined(INVARIANT_SUPPORT) && defined(DIAGNOSTIC)
157 * Return true if the specified process is already in the run queue.
160 runq_findproc(struct runq *rq, struct kse *ke)
165 mtx_assert(&sched_lock, MA_OWNED);
166 for (i = 0; i < RQB_LEN; i++)
167 TAILQ_FOREACH(ke2, &rq->rq_queues[i], ke_procq)
175 * Add the process to the queue specified by its priority, and set the
176 * corresponding status bit.
179 runq_add(struct runq *rq, struct kse *ke)
185 struct proc *p = ke->ke_proc;
187 if (ke->ke_flags & KEF_ONRUNQ)
189 mtx_assert(&sched_lock, MA_OWNED);
190 KASSERT(p->p_stat == SRUN, ("runq_add: proc %p (%s) not SRUN",
192 #if defined(INVARIANTS) && defined(DIAGNOSTIC)
193 KASSERT(runq_findproc(rq, ke) == 0,
194 ("runq_add: proc %p (%s) already in run queue", ke, p->p_comm));
196 pri = ke->ke_thread->td_priority / RQ_PPQ;
197 ke->ke_rqindex = pri;
198 runq_setbit(rq, pri);
199 rqh = &rq->rq_queues[pri];
200 CTR4(KTR_RUNQ, "runq_add: p=%p pri=%d %d rqh=%p",
201 ke->ke_proc, ke->ke_thread->td_priority, pri, rqh);
202 TAILQ_INSERT_TAIL(rqh, ke, ke_procq);
203 ke->ke_flags |= KEF_ONRUNQ;
207 * Return true if there are runnable processes of any priority on the run
208 * queue, false otherwise. Has no side effects, does not modify the run
212 runq_check(struct runq *rq)
217 rqb = &rq->rq_status;
218 for (i = 0; i < RQB_LEN; i++)
219 if (rqb->rqb_bits[i]) {
220 CTR2(KTR_RUNQ, "runq_check: bits=%#x i=%d",
221 rqb->rqb_bits[i], i);
224 CTR0(KTR_RUNQ, "runq_check: empty");
230 * Find and remove the highest priority process from the run queue.
231 * If there are no runnable processes, the per-cpu idle process is
232 * returned. Will not return NULL under any circumstances.
235 runq_choose(struct runq *rq)
241 mtx_assert(&sched_lock, MA_OWNED);
242 if ((pri = runq_findbit(rq)) != -1) {
243 rqh = &rq->rq_queues[pri];
244 ke = TAILQ_FIRST(rqh);
245 KASSERT(ke != NULL, ("runq_choose: no proc on busy queue"));
246 KASSERT(ke->ke_proc->p_stat == SRUN,
247 ("runq_choose: process %d(%s) in state %d", ke->ke_proc->p_pid,
248 ke->ke_proc->p_comm, ke->ke_proc->p_stat));
249 CTR3(KTR_RUNQ, "runq_choose: pri=%d kse=%p rqh=%p", pri, ke, rqh);
250 TAILQ_REMOVE(rqh, ke, ke_procq);
251 if (TAILQ_EMPTY(rqh)) {
252 CTR0(KTR_RUNQ, "runq_choose: empty");
253 runq_clrbit(rq, pri);
255 ke->ke_flags &= ~KEF_ONRUNQ;
258 CTR1(KTR_RUNQ, "runq_choose: idleproc pri=%d", pri);
260 return (PCPU_GET(idlethread)->td_kse);
264 * Initialize a run structure.
267 runq_init(struct runq *rq)
271 bzero(rq, sizeof *rq);
272 for (i = 0; i < RQ_NQS; i++)
273 TAILQ_INIT(&rq->rq_queues[i]);
277 * Remove the process from the queue specified by its priority, and clear the
278 * corresponding status bit if the queue becomes empty.
281 runq_remove(struct runq *rq, struct kse *ke)
286 if (!(ke->ke_flags & KEF_ONRUNQ))
288 mtx_assert(&sched_lock, MA_OWNED);
289 pri = ke->ke_rqindex;
290 rqh = &rq->rq_queues[pri];
291 CTR4(KTR_RUNQ, "runq_remove: p=%p pri=%d %d rqh=%p",
292 ke, ke->ke_thread->td_priority, pri, rqh);
293 KASSERT(ke != NULL, ("runq_remove: no proc on busy queue"));
294 TAILQ_REMOVE(rqh, ke, ke_procq);
295 if (TAILQ_EMPTY(rqh)) {
296 CTR0(KTR_RUNQ, "runq_remove: empty");
297 runq_clrbit(rq, pri);
299 ke->ke_flags &= ~KEF_ONRUNQ;