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