xref: /freebsd/sys/kern/kern_switch.c (revision 9493183e7732319b998b8c7b2061883fb9dc4f85)
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 
27 /***
28 Here is the logic..
29 
30 If there are N processors, then there are at most N KSEs (kernel
31 schedulable entities) working to process threads that belong to a
32 KSEGROUP (kg). If there are X of these KSEs actually running at the
33 moment in question, then there are at most M (N-X) of these KSEs on
34 the run queue, as running KSEs are not on the queue.
35 
36 Runnable threads are queued off the KSEGROUP in priority order.
37 If there are M or more threads runnable, the top M threads
38 (by priority) are 'preassigned' to the M KSEs not running. The KSEs take
39 their priority from those threads and are put on the run queue.
40 
41 The last thread that had a priority high enough to have a KSE associated
42 with it, AND IS ON THE RUN QUEUE is pointed to by
43 kg->kg_last_assigned. If no threads queued off the KSEGROUP have KSEs
44 assigned as all the available KSEs are activly running, or because there
45 are no threads queued, that pointer is NULL.
46 
47 When a KSE is removed from the run queue to become runnable, we know
48 it was associated with the highest priority thread in the queue (at the head
49 of the queue). If it is also the last assigned we know M was 1 and must
50 now be 0. Since the thread is no longer queued that pointer must be
51 removed from it. Since we know there were no more KSEs available,
52 (M was 1 and is now 0) and since we are not FREEING our KSE
53 but using it, we know there are STILL no more KSEs available, we can prove
54 that the next thread in the ksegrp list will not have a KSE to assign to
55 it, so we can show that the pointer must be made 'invalid' (NULL).
56 
57 The pointer exists so that when a new thread is made runnable, it can
58 have its priority compared with the last assigned thread to see if
59 it should 'steal' its KSE or not.. i.e. is it 'earlier'
60 on the list than that thread or later.. If it's earlier, then the KSE is
61 removed from the last assigned (which is now not assigned a KSE)
62 and reassigned to the new thread, which is placed earlier in the list.
63 The pointer is then backed up to the previous thread (which may or may not
64 be the new thread).
65 
66 When a thread sleeps or is removed, the KSE becomes available and if there
67 are queued threads that are not assigned KSEs, the highest priority one of
68 them is assigned the KSE, which is then placed back on the run queue at
69 the approipriate place, and the kg->kg_last_assigned pointer is adjusted down
70 to point to it.
71 
72 The following diagram shows 2 KSEs and 3 threads from a single process.
73 
74  RUNQ: --->KSE---KSE--...    (KSEs queued at priorities from threads)
75               \    \____
76                \        \
77     KSEGROUP---thread--thread--thread    (queued in priority order)
78         \                 /
79          \_______________/
80           (last_assigned)
81 
82 The result of this scheme is that the M available KSEs are always
83 queued at the priorities they have inherrited from the M highest priority
84 threads for that KSEGROUP. If this situation changes, the KSEs are
85 reassigned to keep this true.
86 ***/
87 
88 #include <sys/cdefs.h>
89 __FBSDID("$FreeBSD$");
90 
91 #include "opt_full_preemption.h"
92 
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/kdb.h>
96 #include <sys/kernel.h>
97 #include <sys/ktr.h>
98 #include <sys/lock.h>
99 #include <sys/mutex.h>
100 #include <sys/proc.h>
101 #include <sys/queue.h>
102 #include <sys/sched.h>
103 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
104 #include <sys/smp.h>
105 #endif
106 #include <machine/critical.h>
107 
108 CTASSERT((RQB_BPW * RQB_LEN) == RQ_NQS);
109 
110 void panc(char *string1, char *string2);
111 
112 #if 0
113 static void runq_readjust(struct runq *rq, struct kse *ke);
114 #endif
115 /************************************************************************
116  * Functions that manipulate runnability from a thread perspective.	*
117  ************************************************************************/
118 /*
119  * Select the KSE that will be run next.  From that find the thread, and
120  * remove it from the KSEGRP's run queue.  If there is thread clustering,
121  * this will be what does it.
122  */
123 struct thread *
124 choosethread(void)
125 {
126 	struct kse *ke;
127 	struct thread *td;
128 	struct ksegrp *kg;
129 
130 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
131 	if (smp_active == 0 && PCPU_GET(cpuid) != 0) {
132 		/* Shutting down, run idlethread on AP's */
133 		td = PCPU_GET(idlethread);
134 		ke = td->td_kse;
135 		CTR1(KTR_RUNQ, "choosethread: td=%p (idle)", td);
136 		ke->ke_flags |= KEF_DIDRUN;
137 		TD_SET_RUNNING(td);
138 		return (td);
139 	}
140 #endif
141 
142 retry:
143 	ke = sched_choose();
144 	if (ke) {
145 		td = ke->ke_thread;
146 		KASSERT((td->td_kse == ke), ("kse/thread mismatch"));
147 		kg = ke->ke_ksegrp;
148 		if (td->td_proc->p_flag & P_SA) {
149 			if (kg->kg_last_assigned == td) {
150 				kg->kg_last_assigned = TAILQ_PREV(td,
151 				    threadqueue, td_runq);
152 			}
153 			TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
154 		}
155 		kg->kg_runnable--;
156 		CTR2(KTR_RUNQ, "choosethread: td=%p pri=%d",
157 		    td, td->td_priority);
158 	} else {
159 		/* Simulate runq_choose() having returned the idle thread */
160 		td = PCPU_GET(idlethread);
161 		ke = td->td_kse;
162 		CTR1(KTR_RUNQ, "choosethread: td=%p (idle)", td);
163 	}
164 	ke->ke_flags |= KEF_DIDRUN;
165 
166 	/*
167 	 * If we are in panic, only allow system threads,
168 	 * plus the one we are running in, to be run.
169 	 */
170 	if (panicstr && ((td->td_proc->p_flag & P_SYSTEM) == 0 &&
171 	    (td->td_flags & TDF_INPANIC) == 0)) {
172 		/* note that it is no longer on the run queue */
173 		TD_SET_CAN_RUN(td);
174 		goto retry;
175 	}
176 
177 	TD_SET_RUNNING(td);
178 	return (td);
179 }
180 
181 /*
182  * Given a surplus KSE, either assign a new runable thread to it
183  * (and put it in the run queue) or put it in the ksegrp's idle KSE list.
184  * Assumes that the original thread is not runnable.
185  */
186 void
187 kse_reassign(struct kse *ke)
188 {
189 	struct ksegrp *kg;
190 	struct thread *td;
191 	struct thread *original;
192 
193 	mtx_assert(&sched_lock, MA_OWNED);
194 	original = ke->ke_thread;
195 	KASSERT(original == NULL || TD_IS_INHIBITED(original),
196     	    ("reassigning KSE with runnable thread"));
197 	kg = ke->ke_ksegrp;
198 	if (original)
199 		original->td_kse = NULL;
200 
201 	/*
202 	 * Find the first unassigned thread
203 	 */
204 	if ((td = kg->kg_last_assigned) != NULL)
205 		td = TAILQ_NEXT(td, td_runq);
206 	else
207 		td = TAILQ_FIRST(&kg->kg_runq);
208 
209 	/*
210 	 * If we found one, assign it the kse, otherwise idle the kse.
211 	 */
212 	if (td) {
213 		kg->kg_last_assigned = td;
214 		td->td_kse = ke;
215 		ke->ke_thread = td;
216 		sched_add(td);
217 		CTR2(KTR_RUNQ, "kse_reassign: ke%p -> td%p", ke, td);
218 		return;
219 	}
220 
221 	ke->ke_state = KES_IDLE;
222 	ke->ke_thread = NULL;
223 	TAILQ_INSERT_TAIL(&kg->kg_iq, ke, ke_kgrlist);
224 	kg->kg_idle_kses++;
225 	CTR1(KTR_RUNQ, "kse_reassign: ke%p on idle queue", ke);
226 	return;
227 }
228 
229 #if 0
230 /*
231  * Remove a thread from its KSEGRP's run queue.
232  * This in turn may remove it from a KSE if it was already assigned
233  * to one, possibly causing a new thread to be assigned to the KSE
234  * and the KSE getting a new priority.
235  */
236 static void
237 remrunqueue(struct thread *td)
238 {
239 	struct thread *td2, *td3;
240 	struct ksegrp *kg;
241 	struct kse *ke;
242 
243 	mtx_assert(&sched_lock, MA_OWNED);
244 	KASSERT((TD_ON_RUNQ(td)), ("remrunqueue: Bad state on run queue"));
245 	kg = td->td_ksegrp;
246 	ke = td->td_kse;
247 	CTR1(KTR_RUNQ, "remrunqueue: td%p", td);
248 	kg->kg_runnable--;
249 	TD_SET_CAN_RUN(td);
250 	/*
251 	 * If it is not a threaded process, take the shortcut.
252 	 */
253 	if ((td->td_proc->p_flag & P_SA) == 0) {
254 		/* Bring its kse with it, leave the thread attached */
255 		sched_rem(td);
256 		ke->ke_state = KES_THREAD;
257 		return;
258 	}
259    	td3 = TAILQ_PREV(td, threadqueue, td_runq);
260 	TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
261 	if (ke) {
262 		/*
263 		 * This thread has been assigned to a KSE.
264 		 * We need to dissociate it and try assign the
265 		 * KSE to the next available thread. Then, we should
266 		 * see if we need to move the KSE in the run queues.
267 		 */
268 		sched_rem(td);
269 		ke->ke_state = KES_THREAD;
270 		td2 = kg->kg_last_assigned;
271 		KASSERT((td2 != NULL), ("last assigned has wrong value"));
272 		if (td2 == td)
273 			kg->kg_last_assigned = td3;
274 		kse_reassign(ke);
275 	}
276 }
277 #endif
278 
279 /*
280  * Change the priority of a thread that is on the run queue.
281  */
282 void
283 adjustrunqueue( struct thread *td, int newpri)
284 {
285 	struct ksegrp *kg;
286 	struct kse *ke;
287 
288 	mtx_assert(&sched_lock, MA_OWNED);
289 	KASSERT((TD_ON_RUNQ(td)), ("adjustrunqueue: Bad state on run queue"));
290 
291 	ke = td->td_kse;
292 	CTR1(KTR_RUNQ, "adjustrunqueue: td%p", td);
293 	/*
294 	 * If it is not a threaded process, take the shortcut.
295 	 */
296 	if ((td->td_proc->p_flag & P_SA) == 0) {
297 		/* We only care about the kse in the run queue. */
298 		td->td_priority = newpri;
299 		if (ke->ke_rqindex != (newpri / RQ_PPQ)) {
300 			sched_rem(td);
301 			sched_add(td);
302 		}
303 		return;
304 	}
305 
306 	/* It is a threaded process */
307 	kg = td->td_ksegrp;
308 	kg->kg_runnable--;
309 	TD_SET_CAN_RUN(td);
310 	if (ke) {
311 		if (kg->kg_last_assigned == td) {
312 			kg->kg_last_assigned =
313 			    TAILQ_PREV(td, threadqueue, td_runq);
314 		}
315 		sched_rem(td);
316 	}
317 	TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
318 	td->td_priority = newpri;
319 	setrunqueue(td);
320 }
321 
322 void
323 setrunqueue(struct thread *td)
324 {
325 	struct kse *ke;
326 	struct ksegrp *kg;
327 	struct thread *td2;
328 	struct thread *tda;
329 
330 	CTR1(KTR_RUNQ, "setrunqueue: td%p", td);
331 	mtx_assert(&sched_lock, MA_OWNED);
332 	KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
333 	    ("setrunqueue: bad thread state"));
334 	TD_SET_RUNQ(td);
335 	kg = td->td_ksegrp;
336 	kg->kg_runnable++;
337 	if ((td->td_proc->p_flag & P_SA) == 0) {
338 		/*
339 		 * Common path optimisation: Only one of everything
340 		 * and the KSE is always already attached.
341 		 * Totally ignore the ksegrp run queue.
342 		 */
343 		sched_add(td);
344 		return;
345 	}
346 
347 	tda = kg->kg_last_assigned;
348 	if ((ke = td->td_kse) == NULL) {
349 		if (kg->kg_idle_kses) {
350 			/*
351 			 * There is a free one so it's ours for the asking..
352 			 */
353 			ke = TAILQ_FIRST(&kg->kg_iq);
354 			TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist);
355 			ke->ke_state = KES_THREAD;
356 			kg->kg_idle_kses--;
357 		} else if (tda && (tda->td_priority > td->td_priority)) {
358 			/*
359 			 * None free, but there is one we can commandeer.
360 			 */
361 			ke = tda->td_kse;
362 			sched_rem(tda);
363 			tda->td_kse = NULL;
364 			ke->ke_thread = NULL;
365 			tda = kg->kg_last_assigned =
366 		    	    TAILQ_PREV(tda, threadqueue, td_runq);
367 		}
368 	} else {
369 		/*
370 		 * Temporarily disassociate so it looks like the other cases.
371 		 */
372 		ke->ke_thread = NULL;
373 		td->td_kse = NULL;
374 	}
375 
376 	/*
377 	 * Add the thread to the ksegrp's run queue at
378 	 * the appropriate place.
379 	 */
380 	TAILQ_FOREACH(td2, &kg->kg_runq, td_runq) {
381 		if (td2->td_priority > td->td_priority) {
382 			TAILQ_INSERT_BEFORE(td2, td, td_runq);
383 			break;
384 		}
385 	}
386 	if (td2 == NULL) {
387 		/* We ran off the end of the TAILQ or it was empty. */
388 		TAILQ_INSERT_TAIL(&kg->kg_runq, td, td_runq);
389 	}
390 
391 	/*
392 	 * If we have a ke to use, then put it on the run queue and
393 	 * If needed, readjust the last_assigned pointer.
394 	 */
395 	if (ke) {
396 		if (tda == NULL) {
397 			/*
398 			 * No pre-existing last assigned so whoever is first
399 			 * gets the KSE we brought in.. (maybe us)
400 			 */
401 			td2 = TAILQ_FIRST(&kg->kg_runq);
402 			KASSERT((td2->td_kse == NULL),
403 			    ("unexpected ke present"));
404 			td2->td_kse = ke;
405 			ke->ke_thread = td2;
406 			kg->kg_last_assigned = td2;
407 		} else if (tda->td_priority > td->td_priority) {
408 			/*
409 			 * It's ours, grab it, but last_assigned is past us
410 			 * so don't change it.
411 			 */
412 			td->td_kse = ke;
413 			ke->ke_thread = td;
414 		} else {
415 			/*
416 			 * We are past last_assigned, so
417 			 * put the new kse on whatever is next,
418 			 * which may or may not be us.
419 			 */
420 			td2 = TAILQ_NEXT(tda, td_runq);
421 			kg->kg_last_assigned = td2;
422 			td2->td_kse = ke;
423 			ke->ke_thread = td2;
424 		}
425 		sched_add(ke->ke_thread);
426 	}
427 }
428 
429 /*
430  * Kernel thread preemption implementation.  Critical sections mark
431  * regions of code in which preemptions are not allowed.
432  */
433 void
434 critical_enter(void)
435 {
436 	struct thread *td;
437 
438 	td = curthread;
439 	if (td->td_critnest == 0)
440 		cpu_critical_enter();
441 	td->td_critnest++;
442 }
443 
444 void
445 critical_exit(void)
446 {
447 	struct thread *td;
448 
449 	td = curthread;
450 	KASSERT(td->td_critnest != 0,
451 	    ("critical_exit: td_critnest == 0"));
452 	if (td->td_critnest == 1) {
453 #if 0
454 #ifdef PREEMPTION
455 		mtx_assert(&sched_lock, MA_NOTOWNED);
456 		if (td->td_pflags & TDP_OWEPREEMPT) {
457 			mtx_lock_spin(&sched_lock);
458 			mi_switch(SW_INVOL, NULL);
459 			mtx_unlock_spin(&sched_lock);
460 		}
461 #endif
462 #endif
463 		td->td_critnest = 0;
464 		cpu_critical_exit();
465 	} else {
466 		td->td_critnest--;
467 	}
468 }
469 
470 /*
471  * This function is called when a thread is about to be put on run queue
472  * because it has been made runnable or its priority has been adjusted.  It
473  * determines if the new thread should be immediately preempted to.  If so,
474  * it switches to it and eventually returns true.  If not, it returns false
475  * so that the caller may place the thread on an appropriate run queue.
476  */
477 int
478 maybe_preempt(struct thread *td)
479 {
480 #ifdef PREEMPTION
481 	struct thread *ctd;
482 	int cpri, pri;
483 #endif
484 
485 	mtx_assert(&sched_lock, MA_OWNED);
486 #ifdef PREEMPTION
487 	/*
488 	 * The new thread should not preempt the current thread if any of the
489 	 * following conditions are true:
490 	 *
491 	 *  - The current thread has a higher (numerically lower) or
492 	 *    equivalent priority.  Note that this prevents curthread from
493 	 *    trying to preempt to itself.
494 	 *  - It is too early in the boot for context switches (cold is set).
495 	 *  - The current thread has an inhibitor set or is in the process of
496 	 *    exiting.  In this case, the current thread is about to switch
497 	 *    out anyways, so there's no point in preempting.  If we did,
498 	 *    the current thread would not be properly resumed as well, so
499 	 *    just avoid that whole landmine.
500 	 *  - If the new thread's priority is not a realtime priority and
501 	 *    the current thread's priority is not an idle priority and
502 	 *    FULL_PREEMPTION is disabled.
503 	 *
504 	 * If all of these conditions are false, but the current thread is in
505 	 * a nested critical section, then we have to defer the preemption
506 	 * until we exit the critical section.  Otherwise, switch immediately
507 	 * to the new thread.
508 	 */
509 	ctd = curthread;
510 	pri = td->td_priority;
511 	cpri = ctd->td_priority;
512 	if (pri >= cpri || cold /* || dumping */ || TD_IS_INHIBITED(ctd) ||
513 	    td->td_kse->ke_state != KES_THREAD)
514 		return (0);
515 #ifndef FULL_PREEMPTION
516 	if (!(pri >= PRI_MIN_ITHD && pri <= PRI_MAX_ITHD) &&
517 	    !(cpri >= PRI_MIN_IDLE))
518 		return (0);
519 #endif
520 	if (ctd->td_critnest > 1) {
521 		CTR1(KTR_PROC, "maybe_preempt: in critical section %d",
522 		    ctd->td_critnest);
523 		ctd->td_pflags |= TDP_OWEPREEMPT;
524 		return (0);
525 	}
526 
527 	/*
528 	 * Our thread state says that we are already on a run queue, so
529 	 * update our state as if we had been dequeued by choosethread().
530 	 */
531 	MPASS(TD_ON_RUNQ(td));
532 	TD_SET_RUNNING(td);
533 	CTR3(KTR_PROC, "preempting to thread %p (pid %d, %s)\n", td,
534 	    td->td_proc->p_pid, td->td_proc->p_comm);
535 	mi_switch(SW_INVOL, td);
536 	return (1);
537 #else
538 	return (0);
539 #endif
540 }
541 
542 #ifndef PREEMPTION
543 /* XXX: There should be a non-static version of this. */
544 static void
545 printf_caddr_t(void *data)
546 {
547 	printf("%s", (char *)data);
548 }
549 static char preempt_warning[] =
550     "WARNING: Kernel preemption is disabled, expect reduced performance.\n";
551 SYSINIT(preempt_warning, SI_SUB_COPYRIGHT, SI_ORDER_ANY, printf_caddr_t,
552     preempt_warning)
553 #endif
554 
555 /************************************************************************
556  * SYSTEM RUN QUEUE manipulations and tests				*
557  ************************************************************************/
558 /*
559  * Initialize a run structure.
560  */
561 void
562 runq_init(struct runq *rq)
563 {
564 	int i;
565 
566 	bzero(rq, sizeof *rq);
567 	for (i = 0; i < RQ_NQS; i++)
568 		TAILQ_INIT(&rq->rq_queues[i]);
569 }
570 
571 /*
572  * Clear the status bit of the queue corresponding to priority level pri,
573  * indicating that it is empty.
574  */
575 static __inline void
576 runq_clrbit(struct runq *rq, int pri)
577 {
578 	struct rqbits *rqb;
579 
580 	rqb = &rq->rq_status;
581 	CTR4(KTR_RUNQ, "runq_clrbit: bits=%#x %#x bit=%#x word=%d",
582 	    rqb->rqb_bits[RQB_WORD(pri)],
583 	    rqb->rqb_bits[RQB_WORD(pri)] & ~RQB_BIT(pri),
584 	    RQB_BIT(pri), RQB_WORD(pri));
585 	rqb->rqb_bits[RQB_WORD(pri)] &= ~RQB_BIT(pri);
586 }
587 
588 /*
589  * Find the index of the first non-empty run queue.  This is done by
590  * scanning the status bits, a set bit indicates a non-empty queue.
591  */
592 static __inline int
593 runq_findbit(struct runq *rq)
594 {
595 	struct rqbits *rqb;
596 	int pri;
597 	int i;
598 
599 	rqb = &rq->rq_status;
600 	for (i = 0; i < RQB_LEN; i++)
601 		if (rqb->rqb_bits[i]) {
602 			pri = RQB_FFS(rqb->rqb_bits[i]) + (i << RQB_L2BPW);
603 			CTR3(KTR_RUNQ, "runq_findbit: bits=%#x i=%d pri=%d",
604 			    rqb->rqb_bits[i], i, pri);
605 			return (pri);
606 		}
607 
608 	return (-1);
609 }
610 
611 /*
612  * Set the status bit of the queue corresponding to priority level pri,
613  * indicating that it is non-empty.
614  */
615 static __inline void
616 runq_setbit(struct runq *rq, int pri)
617 {
618 	struct rqbits *rqb;
619 
620 	rqb = &rq->rq_status;
621 	CTR4(KTR_RUNQ, "runq_setbit: bits=%#x %#x bit=%#x word=%d",
622 	    rqb->rqb_bits[RQB_WORD(pri)],
623 	    rqb->rqb_bits[RQB_WORD(pri)] | RQB_BIT(pri),
624 	    RQB_BIT(pri), RQB_WORD(pri));
625 	rqb->rqb_bits[RQB_WORD(pri)] |= RQB_BIT(pri);
626 }
627 
628 /*
629  * Add the KSE to the queue specified by its priority, and set the
630  * corresponding status bit.
631  */
632 void
633 runq_add(struct runq *rq, struct kse *ke)
634 {
635 	struct rqhead *rqh;
636 	int pri;
637 
638 	pri = ke->ke_thread->td_priority / RQ_PPQ;
639 	ke->ke_rqindex = pri;
640 	runq_setbit(rq, pri);
641 	rqh = &rq->rq_queues[pri];
642 	CTR4(KTR_RUNQ, "runq_add: p=%p pri=%d %d rqh=%p",
643 	    ke->ke_proc, ke->ke_thread->td_priority, pri, rqh);
644 	TAILQ_INSERT_TAIL(rqh, ke, ke_procq);
645 }
646 
647 /*
648  * Return true if there are runnable processes of any priority on the run
649  * queue, false otherwise.  Has no side effects, does not modify the run
650  * queue structure.
651  */
652 int
653 runq_check(struct runq *rq)
654 {
655 	struct rqbits *rqb;
656 	int i;
657 
658 	rqb = &rq->rq_status;
659 	for (i = 0; i < RQB_LEN; i++)
660 		if (rqb->rqb_bits[i]) {
661 			CTR2(KTR_RUNQ, "runq_check: bits=%#x i=%d",
662 			    rqb->rqb_bits[i], i);
663 			return (1);
664 		}
665 	CTR0(KTR_RUNQ, "runq_check: empty");
666 
667 	return (0);
668 }
669 
670 /*
671  * Find the highest priority process on the run queue.
672  */
673 struct kse *
674 runq_choose(struct runq *rq)
675 {
676 	struct rqhead *rqh;
677 	struct kse *ke;
678 	int pri;
679 
680 	mtx_assert(&sched_lock, MA_OWNED);
681 	while ((pri = runq_findbit(rq)) != -1) {
682 		rqh = &rq->rq_queues[pri];
683 		ke = TAILQ_FIRST(rqh);
684 		KASSERT(ke != NULL, ("runq_choose: no proc on busy queue"));
685 		CTR3(KTR_RUNQ,
686 		    "runq_choose: pri=%d kse=%p rqh=%p", pri, ke, rqh);
687 		return (ke);
688 	}
689 	CTR1(KTR_RUNQ, "runq_choose: idleproc pri=%d", pri);
690 
691 	return (NULL);
692 }
693 
694 /*
695  * Remove the KSE from the queue specified by its priority, and clear the
696  * corresponding status bit if the queue becomes empty.
697  * Caller must set ke->ke_state afterwards.
698  */
699 void
700 runq_remove(struct runq *rq, struct kse *ke)
701 {
702 	struct rqhead *rqh;
703 	int pri;
704 
705 	KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
706 		("runq_remove: process swapped out"));
707 	pri = ke->ke_rqindex;
708 	rqh = &rq->rq_queues[pri];
709 	CTR4(KTR_RUNQ, "runq_remove: p=%p pri=%d %d rqh=%p",
710 	    ke, ke->ke_thread->td_priority, pri, rqh);
711 	KASSERT(ke != NULL, ("runq_remove: no proc on busy queue"));
712 	TAILQ_REMOVE(rqh, ke, ke_procq);
713 	if (TAILQ_EMPTY(rqh)) {
714 		CTR0(KTR_RUNQ, "runq_remove: empty");
715 		runq_clrbit(rq, pri);
716 	}
717 }
718 
719 #if 0
720 void
721 panc(char *string1, char *string2)
722 {
723 	printf("%s", string1);
724 	kdb_enter(string2);
725 }
726 
727 void
728 thread_sanity_check(struct thread *td, char *string)
729 {
730 	struct proc *p;
731 	struct ksegrp *kg;
732 	struct kse *ke;
733 	struct thread *td2 = NULL;
734 	unsigned int prevpri;
735 	int	saw_lastassigned = 0;
736 	int unassigned = 0;
737 	int assigned = 0;
738 
739 	p = td->td_proc;
740 	kg = td->td_ksegrp;
741 	ke = td->td_kse;
742 
743 
744 	if (ke) {
745 		if (p != ke->ke_proc) {
746 			panc(string, "wrong proc");
747 		}
748 		if (ke->ke_thread != td) {
749 			panc(string, "wrong thread");
750 		}
751 	}
752 
753 	if ((p->p_flag & P_SA) == 0) {
754 		if (ke == NULL) {
755 			panc(string, "non KSE thread lost kse");
756 		}
757 	} else {
758 		prevpri = 0;
759 		saw_lastassigned = 0;
760 		unassigned = 0;
761 		assigned = 0;
762 		TAILQ_FOREACH(td2, &kg->kg_runq, td_runq) {
763 			if (td2->td_priority < prevpri) {
764 				panc(string, "thread runqueue unosorted");
765 			}
766 			if ((td2->td_state == TDS_RUNQ) &&
767 			    td2->td_kse &&
768 			    (td2->td_kse->ke_state != KES_ONRUNQ)) {
769 				panc(string, "KSE wrong state");
770 			}
771 			prevpri = td2->td_priority;
772 			if (td2->td_kse) {
773 				assigned++;
774 				if (unassigned) {
775 					panc(string, "unassigned before assigned");
776 				}
777  				if  (kg->kg_last_assigned == NULL) {
778 					panc(string, "lastassigned corrupt");
779 				}
780 				if (saw_lastassigned) {
781 					panc(string, "last assigned not last");
782 				}
783 				if (td2->td_kse->ke_thread != td2) {
784 					panc(string, "mismatched kse/thread");
785 				}
786 			} else {
787 				unassigned++;
788 			}
789 			if (td2 == kg->kg_last_assigned) {
790 				saw_lastassigned = 1;
791 				if (td2->td_kse == NULL) {
792 					panc(string, "last assigned not assigned");
793 				}
794 			}
795 		}
796 		if (kg->kg_last_assigned && (saw_lastassigned == 0)) {
797 			panc(string, "where on earth does lastassigned point?");
798 		}
799 #if 0
800 		FOREACH_THREAD_IN_GROUP(kg, td2) {
801 			if (((td2->td_flags & TDF_UNBOUND) == 0) &&
802 			    (TD_ON_RUNQ(td2))) {
803 				assigned++;
804 				if (td2->td_kse == NULL) {
805 					panc(string, "BOUND thread with no KSE");
806 				}
807 			}
808 		}
809 #endif
810 #if 0
811 		if ((unassigned + assigned) != kg->kg_runnable) {
812 			panc(string, "wrong number in runnable");
813 		}
814 #endif
815 	}
816 	if (assigned == 12345) {
817 		printf("%p %p %p %p %p %d, %d",
818 		    td, td2, ke, kg, p, assigned, saw_lastassigned);
819 	}
820 }
821 #endif
822 
823