kern_switch.c revision 97261
1/*
2 * Copyright (c) 2001 Jake Burkholder <jake@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_switch.c 97261 2002-05-25 01:12:23Z jake $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/ktr.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/proc.h>
36#include <sys/queue.h>
37#include <machine/critical.h>
38
39CTASSERT((RQB_BPW * RQB_LEN) == RQ_NQS);
40
41/*
42 * Global run queue.
43 */
44static struct runq runq;
45SYSINIT(runq, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, runq_init, &runq)
46
47/*
48 * Wrappers which implement old interface; act on global run queue.
49 */
50
51struct thread *
52choosethread(void)
53{
54	return (runq_choose(&runq)->ke_thread);
55}
56
57int
58procrunnable(void)
59{
60	return runq_check(&runq);
61}
62
63void
64remrunqueue(struct thread *td)
65{
66	runq_remove(&runq, td->td_kse);
67}
68
69void
70setrunqueue(struct thread *td)
71{
72	runq_add(&runq, td->td_kse);
73}
74
75/* Critical sections that prevent preemption. */
76void
77critical_enter(void)
78{
79	struct thread *td;
80
81	td = curthread;
82	if (td->td_critnest == 0)
83		cpu_critical_enter();
84	td->td_critnest++;
85}
86
87void
88critical_exit(void)
89{
90	struct thread *td;
91
92	td = curthread;
93	if (td->td_critnest == 1) {
94		td->td_critnest = 0;
95		cpu_critical_exit();
96	} else {
97		td->td_critnest--;
98	}
99}
100
101/*
102 * Clear the status bit of the queue corresponding to priority level pri,
103 * indicating that it is empty.
104 */
105static __inline void
106runq_clrbit(struct runq *rq, int pri)
107{
108	struct rqbits *rqb;
109
110	rqb = &rq->rq_status;
111	CTR4(KTR_RUNQ, "runq_clrbit: bits=%#x %#x bit=%#x word=%d",
112	    rqb->rqb_bits[RQB_WORD(pri)],
113	    rqb->rqb_bits[RQB_WORD(pri)] & ~RQB_BIT(pri),
114	    RQB_BIT(pri), RQB_WORD(pri));
115	rqb->rqb_bits[RQB_WORD(pri)] &= ~RQB_BIT(pri);
116}
117
118/*
119 * Find the index of the first non-empty run queue.  This is done by
120 * scanning the status bits, a set bit indicates a non-empty queue.
121 */
122static __inline int
123runq_findbit(struct runq *rq)
124{
125	struct rqbits *rqb;
126	int pri;
127	int i;
128
129	rqb = &rq->rq_status;
130	for (i = 0; i < RQB_LEN; i++)
131		if (rqb->rqb_bits[i]) {
132			pri = (RQB_FFS(rqb->rqb_bits[i]) - 1) +
133			    (i << RQB_L2BPW);
134			CTR3(KTR_RUNQ, "runq_findbit: bits=%#x i=%d pri=%d",
135			    rqb->rqb_bits[i], i, pri);
136			return (pri);
137		}
138
139	return (-1);
140}
141
142/*
143 * Set the status bit of the queue corresponding to priority level pri,
144 * indicating that it is non-empty.
145 */
146static __inline void
147runq_setbit(struct runq *rq, int pri)
148{
149	struct rqbits *rqb;
150
151	rqb = &rq->rq_status;
152	CTR4(KTR_RUNQ, "runq_setbit: bits=%#x %#x bit=%#x word=%d",
153	    rqb->rqb_bits[RQB_WORD(pri)],
154	    rqb->rqb_bits[RQB_WORD(pri)] | RQB_BIT(pri),
155	    RQB_BIT(pri), RQB_WORD(pri));
156	rqb->rqb_bits[RQB_WORD(pri)] |= RQB_BIT(pri);
157}
158
159/*
160 * Add the process to the queue specified by its priority, and set the
161 * corresponding status bit.
162 */
163void
164runq_add(struct runq *rq, struct kse *ke)
165{
166	struct rqhead *rqh;
167	int pri;
168
169#ifdef INVARIANTS
170	struct proc *p = ke->ke_proc;
171#endif
172	if (ke->ke_flags & KEF_ONRUNQ)
173		return;
174	mtx_assert(&sched_lock, MA_OWNED);
175	KASSERT(p->p_stat == SRUN, ("runq_add: proc %p (%s) not SRUN",
176	    p, p->p_comm));
177	pri = ke->ke_thread->td_priority / RQ_PPQ;
178	ke->ke_rqindex = pri;
179	runq_setbit(rq, pri);
180	rqh = &rq->rq_queues[pri];
181	CTR4(KTR_RUNQ, "runq_add: p=%p pri=%d %d rqh=%p",
182	    ke->ke_proc, ke->ke_thread->td_priority, pri, rqh);
183	TAILQ_INSERT_TAIL(rqh, ke, ke_procq);
184	ke->ke_flags |= KEF_ONRUNQ;
185}
186
187/*
188 * Return true if there are runnable processes of any priority on the run
189 * queue, false otherwise.  Has no side effects, does not modify the run
190 * queue structure.
191 */
192int
193runq_check(struct runq *rq)
194{
195	struct rqbits *rqb;
196	int i;
197
198	rqb = &rq->rq_status;
199	for (i = 0; i < RQB_LEN; i++)
200		if (rqb->rqb_bits[i]) {
201			CTR2(KTR_RUNQ, "runq_check: bits=%#x i=%d",
202			    rqb->rqb_bits[i], i);
203			return (1);
204		}
205	CTR0(KTR_RUNQ, "runq_check: empty");
206
207	return (0);
208}
209
210/*
211 * Find and remove the highest priority process from the run queue.
212 * If there are no runnable processes, the per-cpu idle process is
213 * returned.  Will not return NULL under any circumstances.
214 */
215struct kse *
216runq_choose(struct runq *rq)
217{
218	struct rqhead *rqh;
219	struct kse *ke;
220	int pri;
221
222	mtx_assert(&sched_lock, MA_OWNED);
223	if ((pri = runq_findbit(rq)) != -1) {
224		rqh = &rq->rq_queues[pri];
225		ke = TAILQ_FIRST(rqh);
226		KASSERT(ke != NULL, ("runq_choose: no proc on busy queue"));
227		KASSERT(ke->ke_proc->p_stat == SRUN,
228		    ("runq_choose: process %d(%s) in state %d", ke->ke_proc->p_pid,
229		    ke->ke_proc->p_comm, ke->ke_proc->p_stat));
230		CTR3(KTR_RUNQ, "runq_choose: pri=%d kse=%p rqh=%p", pri, ke, rqh);
231		TAILQ_REMOVE(rqh, ke, ke_procq);
232		if (TAILQ_EMPTY(rqh)) {
233			CTR0(KTR_RUNQ, "runq_choose: empty");
234			runq_clrbit(rq, pri);
235		}
236		ke->ke_flags &= ~KEF_ONRUNQ;
237		return (ke);
238	}
239	CTR1(KTR_RUNQ, "runq_choose: idleproc pri=%d", pri);
240
241	return (PCPU_GET(idlethread)->td_kse);
242}
243
244/*
245 * Initialize a run structure.
246 */
247void
248runq_init(struct runq *rq)
249{
250	int i;
251
252	bzero(rq, sizeof *rq);
253	for (i = 0; i < RQ_NQS; i++)
254		TAILQ_INIT(&rq->rq_queues[i]);
255}
256
257/*
258 * Remove the process from the queue specified by its priority, and clear the
259 * corresponding status bit if the queue becomes empty.
260 */
261void
262runq_remove(struct runq *rq, struct kse *ke)
263{
264	struct rqhead *rqh;
265	int pri;
266
267	if (!(ke->ke_flags & KEF_ONRUNQ))
268		return;
269	mtx_assert(&sched_lock, MA_OWNED);
270	pri = ke->ke_rqindex;
271	rqh = &rq->rq_queues[pri];
272	CTR4(KTR_RUNQ, "runq_remove: p=%p pri=%d %d rqh=%p",
273	    ke, ke->ke_thread->td_priority, pri, rqh);
274	KASSERT(ke != NULL, ("runq_remove: no proc on busy queue"));
275	TAILQ_REMOVE(rqh, ke, ke_procq);
276	if (TAILQ_EMPTY(rqh)) {
277		CTR0(KTR_RUNQ, "runq_remove: empty");
278		runq_clrbit(rq, pri);
279	}
280	ke->ke_flags &= ~KEF_ONRUNQ;
281}
282