xref: /freebsd/sys/kern/sched_4bsd.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * $FreeBSD$
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/sched.h>
50 #include <sys/smp.h>
51 #include <sys/sysctl.h>
52 #include <sys/sx.h>
53 
54 /*
55  * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
56  * the range 100-256 Hz (approximately).
57  */
58 #define	ESTCPULIM(e) \
59     min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
60     RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
61 #define	INVERSE_ESTCPU_WEIGHT	8	/* 1 / (priorities per estcpu level). */
62 #define	NICE_WEIGHT		1	/* Priorities per nice level. */
63 
64 struct ke_sched {
65 	int	ske_cpticks;	/* (j) Ticks of cpu time. */
66 };
67 
68 struct ke_sched ke_sched;
69 
70 struct ke_sched *kse0_sched = &ke_sched;
71 struct kg_sched *ksegrp0_sched = NULL;
72 struct p_sched *proc0_sched = NULL;
73 struct td_sched *thread0_sched = NULL;
74 
75 static int	sched_quantum;	/* Roundrobin scheduling quantum in ticks. */
76 #define	SCHED_QUANTUM	(hz / 10);	/* Default sched quantum */
77 
78 static struct callout schedcpu_callout;
79 static struct callout roundrobin_callout;
80 
81 static void	roundrobin(void *arg);
82 static void	schedcpu(void *arg);
83 static void	sched_setup(void *dummy);
84 static void	maybe_resched(struct thread *td);
85 static void	updatepri(struct ksegrp *kg);
86 static void	resetpriority(struct ksegrp *kg);
87 
88 SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
89 
90 /*
91  * Global run queue.
92  */
93 static struct runq runq;
94 SYSINIT(runq, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, runq_init, &runq)
95 
96 static int
97 sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
98 {
99 	int error, new_val;
100 
101 	new_val = sched_quantum * tick;
102 	error = sysctl_handle_int(oidp, &new_val, 0, req);
103         if (error != 0 || req->newptr == NULL)
104 		return (error);
105 	if (new_val < tick)
106 		return (EINVAL);
107 	sched_quantum = new_val / tick;
108 	hogticks = 2 * sched_quantum;
109 	return (0);
110 }
111 
112 SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
113 	0, sizeof sched_quantum, sysctl_kern_quantum, "I",
114 	"Roundrobin scheduling quantum in microseconds");
115 
116 /*
117  * Arrange to reschedule if necessary, taking the priorities and
118  * schedulers into account.
119  */
120 static void
121 maybe_resched(struct thread *td)
122 {
123 
124 	mtx_assert(&sched_lock, MA_OWNED);
125 	if (td->td_priority < curthread->td_priority && curthread->td_kse)
126 		curthread->td_flags |= TDF_NEEDRESCHED;
127 }
128 
129 /*
130  * Force switch among equal priority processes every 100ms.
131  * We don't actually need to force a context switch of the current process.
132  * The act of firing the event triggers a context switch to softclock() and
133  * then switching back out again which is equivalent to a preemption, thus
134  * no further work is needed on the local CPU.
135  */
136 /* ARGSUSED */
137 static void
138 roundrobin(void *arg)
139 {
140 
141 #ifdef SMP
142 	mtx_lock_spin(&sched_lock);
143 	forward_roundrobin();
144 	mtx_unlock_spin(&sched_lock);
145 #endif
146 
147 	callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL);
148 }
149 
150 /*
151  * Constants for digital decay and forget:
152  *	90% of (p_estcpu) usage in 5 * loadav time
153  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
154  *          Note that, as ps(1) mentions, this can let percentages
155  *          total over 100% (I've seen 137.9% for 3 processes).
156  *
157  * Note that schedclock() updates p_estcpu and p_cpticks asynchronously.
158  *
159  * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
160  * That is, the system wants to compute a value of decay such
161  * that the following for loop:
162  * 	for (i = 0; i < (5 * loadavg); i++)
163  * 		p_estcpu *= decay;
164  * will compute
165  * 	p_estcpu *= 0.1;
166  * for all values of loadavg:
167  *
168  * Mathematically this loop can be expressed by saying:
169  * 	decay ** (5 * loadavg) ~= .1
170  *
171  * The system computes decay as:
172  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
173  *
174  * We wish to prove that the system's computation of decay
175  * will always fulfill the equation:
176  * 	decay ** (5 * loadavg) ~= .1
177  *
178  * If we compute b as:
179  * 	b = 2 * loadavg
180  * then
181  * 	decay = b / (b + 1)
182  *
183  * We now need to prove two things:
184  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
185  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
186  *
187  * Facts:
188  *         For x close to zero, exp(x) =~ 1 + x, since
189  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
190  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
191  *         For x close to zero, ln(1+x) =~ x, since
192  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
193  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
194  *         ln(.1) =~ -2.30
195  *
196  * Proof of (1):
197  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
198  *	solving for factor,
199  *      ln(factor) =~ (-2.30/5*loadav), or
200  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
201  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
202  *
203  * Proof of (2):
204  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
205  *	solving for power,
206  *      power*ln(b/(b+1)) =~ -2.30, or
207  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
208  *
209  * Actual power values for the implemented algorithm are as follows:
210  *      loadav: 1       2       3       4
211  *      power:  5.68    10.32   14.94   19.55
212  */
213 
214 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
215 #define	loadfactor(loadav)	(2 * (loadav))
216 #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
217 
218 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
219 static fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;	/* exp(-1/20) */
220 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
221 
222 /*
223  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
224  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
225  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
226  *
227  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
228  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
229  *
230  * If you don't want to bother with the faster/more-accurate formula, you
231  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
232  * (more general) method of calculating the %age of CPU used by a process.
233  */
234 #define	CCPU_SHIFT	11
235 
236 /*
237  * Recompute process priorities, every hz ticks.
238  * MP-safe, called without the Giant mutex.
239  */
240 /* ARGSUSED */
241 static void
242 schedcpu(void *arg)
243 {
244 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
245 	struct thread *td;
246 	struct proc *p;
247 	struct kse *ke;
248 	struct ksegrp *kg;
249 	int realstathz;
250 	int awake;
251 
252 	realstathz = stathz ? stathz : hz;
253 	sx_slock(&allproc_lock);
254 	FOREACH_PROC_IN_SYSTEM(p) {
255 		mtx_lock_spin(&sched_lock);
256 		p->p_swtime++;
257 		FOREACH_KSEGRP_IN_PROC(p, kg) {
258 			awake = 0;
259 			FOREACH_KSE_IN_GROUP(kg, ke) {
260 				/*
261 				 * Increment time in/out of memory and sleep
262 				 * time (if sleeping).  We ignore overflow;
263 				 * with 16-bit int's (remember them?)
264 				 * overflow takes 45 days.
265 				 */
266 				/*
267 				 * The kse slptimes are not touched in wakeup
268 				 * because the thread may not HAVE a KSE.
269 				 */
270 				if (ke->ke_state == KES_ONRUNQ) {
271 					awake = 1;
272 					ke->ke_flags &= ~KEF_DIDRUN;
273 				} else if ((ke->ke_state == KES_THREAD) &&
274 				    (TD_IS_RUNNING(ke->ke_thread))) {
275 					awake = 1;
276 					/* Do not clear KEF_DIDRUN */
277 				} else if (ke->ke_flags & KEF_DIDRUN) {
278 					awake = 1;
279 					ke->ke_flags &= ~KEF_DIDRUN;
280 				}
281 
282 				/*
283 				 * pctcpu is only for ps?
284 				 * Do it per kse.. and add them up at the end?
285 				 * XXXKSE
286 				 */
287 				ke->ke_pctcpu
288 				    = (ke->ke_pctcpu * ccpu) >>
289 				    FSHIFT;
290 				/*
291 				 * If the kse has been idle the entire second,
292 				 * stop recalculating its priority until
293 				 * it wakes up.
294 				 */
295 				if (ke->ke_sched->ske_cpticks == 0)
296 					continue;
297 #if	(FSHIFT >= CCPU_SHIFT)
298 				ke->ke_pctcpu += (realstathz == 100)
299 				    ? ((fixpt_t) ke->ke_sched->ske_cpticks) <<
300 				    (FSHIFT - CCPU_SHIFT) :
301 				    100 * (((fixpt_t) ke->ke_sched->ske_cpticks)
302 				    << (FSHIFT - CCPU_SHIFT)) / realstathz;
303 #else
304 				ke->ke_pctcpu += ((FSCALE - ccpu) *
305 				    (ke->ke_sched->ske_cpticks *
306 				    FSCALE / realstathz)) >> FSHIFT;
307 #endif
308 				ke->ke_sched->ske_cpticks = 0;
309 			} /* end of kse loop */
310 			/*
311 			 * If there are ANY running threads in this KSEGRP,
312 			 * then don't count it as sleeping.
313 			 */
314 			if (awake) {
315 				if (kg->kg_slptime > 1) {
316 					/*
317 					 * In an ideal world, this should not
318 					 * happen, because whoever woke us
319 					 * up from the long sleep should have
320 					 * unwound the slptime and reset our
321 					 * priority before we run at the stale
322 					 * priority.  Should KASSERT at some
323 					 * point when all the cases are fixed.
324 					 */
325 					updatepri(kg);
326 				}
327 				kg->kg_slptime = 0;
328 			} else {
329 				kg->kg_slptime++;
330 			}
331 			if (kg->kg_slptime > 1)
332 				continue;
333 			kg->kg_estcpu = decay_cpu(loadfac, kg->kg_estcpu);
334 		      	resetpriority(kg);
335 			FOREACH_THREAD_IN_GROUP(kg, td) {
336 				if (td->td_priority >= PUSER) {
337 					sched_prio(td, kg->kg_user_pri);
338 				}
339 			}
340 		} /* end of ksegrp loop */
341 		mtx_unlock_spin(&sched_lock);
342 	} /* end of process loop */
343 	sx_sunlock(&allproc_lock);
344 	callout_reset(&schedcpu_callout, hz, schedcpu, NULL);
345 }
346 
347 /*
348  * Recalculate the priority of a process after it has slept for a while.
349  * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
350  * least six times the loadfactor will decay p_estcpu to zero.
351  */
352 static void
353 updatepri(struct ksegrp *kg)
354 {
355 	register unsigned int newcpu;
356 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
357 
358 	newcpu = kg->kg_estcpu;
359 	if (kg->kg_slptime > 5 * loadfac)
360 		kg->kg_estcpu = 0;
361 	else {
362 		kg->kg_slptime--;	/* the first time was done in schedcpu */
363 		while (newcpu && --kg->kg_slptime)
364 			newcpu = decay_cpu(loadfac, newcpu);
365 		kg->kg_estcpu = newcpu;
366 	}
367 	resetpriority(kg);
368 }
369 
370 /*
371  * Compute the priority of a process when running in user mode.
372  * Arrange to reschedule if the resulting priority is better
373  * than that of the current process.
374  */
375 static void
376 resetpriority(struct ksegrp *kg)
377 {
378 	register unsigned int newpriority;
379 	struct thread *td;
380 
381 	mtx_lock_spin(&sched_lock);
382 	if (kg->kg_pri_class == PRI_TIMESHARE) {
383 		newpriority = PUSER + kg->kg_estcpu / INVERSE_ESTCPU_WEIGHT +
384 		    NICE_WEIGHT * (kg->kg_nice - PRIO_MIN);
385 		newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
386 		    PRI_MAX_TIMESHARE);
387 		kg->kg_user_pri = newpriority;
388 	}
389 	FOREACH_THREAD_IN_GROUP(kg, td) {
390 		maybe_resched(td);			/* XXXKSE silly */
391 	}
392 	mtx_unlock_spin(&sched_lock);
393 }
394 
395 /* ARGSUSED */
396 static void
397 sched_setup(void *dummy)
398 {
399 	if (sched_quantum == 0)
400 		sched_quantum = SCHED_QUANTUM;
401 	hogticks = 2 * sched_quantum;
402 
403 	callout_init(&schedcpu_callout, 1);
404 	callout_init(&roundrobin_callout, 0);
405 
406 	/* Kick off timeout driven events by calling first time. */
407 	roundrobin(NULL);
408 	schedcpu(NULL);
409 }
410 
411 /* External interfaces start here */
412 int
413 sched_runnable(void)
414 {
415         return runq_check(&runq);
416 }
417 
418 int
419 sched_rr_interval(void)
420 {
421 	if (sched_quantum == 0)
422 		sched_quantum = SCHED_QUANTUM;
423 	return (sched_quantum);
424 }
425 
426 /*
427  * We adjust the priority of the current process.  The priority of
428  * a process gets worse as it accumulates CPU time.  The cpu usage
429  * estimator (p_estcpu) is increased here.  resetpriority() will
430  * compute a different priority each time p_estcpu increases by
431  * INVERSE_ESTCPU_WEIGHT
432  * (until MAXPRI is reached).  The cpu usage estimator ramps up
433  * quite quickly when the process is running (linearly), and decays
434  * away exponentially, at a rate which is proportionally slower when
435  * the system is busy.  The basic principle is that the system will
436  * 90% forget that the process used a lot of CPU time in 5 * loadav
437  * seconds.  This causes the system to favor processes which haven't
438  * run much recently, and to round-robin among other processes.
439  */
440 void
441 sched_clock(struct thread *td)
442 {
443 	struct kse *ke;
444 	struct ksegrp *kg;
445 
446 	KASSERT((td != NULL), ("schedclock: null thread pointer"));
447 	ke = td->td_kse;
448 	kg = td->td_ksegrp;
449 	ke->ke_sched->ske_cpticks++;
450 	kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + 1);
451 	if ((kg->kg_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
452 		resetpriority(kg);
453 		if (td->td_priority >= PUSER)
454 			td->td_priority = kg->kg_user_pri;
455 	}
456 }
457 /*
458  * charge childs scheduling cpu usage to parent.
459  *
460  * XXXKSE assume only one thread & kse & ksegrp keep estcpu in each ksegrp.
461  * Charge it to the ksegrp that did the wait since process estcpu is sum of
462  * all ksegrps, this is strictly as expected.  Assume that the child process
463  * aggregated all the estcpu into the 'built-in' ksegrp.
464  */
465 void
466 sched_exit(struct ksegrp *kg, struct ksegrp *child)
467 {
468 	kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + child->kg_estcpu);
469 }
470 
471 void
472 sched_fork(struct ksegrp *kg, struct ksegrp *child)
473 {
474 	struct kse *ke;
475 
476 	/*
477 	 * set priority of child to be that of parent.
478 	 * XXXKSE this needs redefining..
479 	 */
480 	child->kg_estcpu = kg->kg_estcpu;
481 
482 	/* Set up scheduler specific data */
483 	ke = FIRST_KSE_IN_KSEGRP(kg);
484 	ke->ke_sched->ske_cpticks = 0;
485 }
486 
487 void
488 sched_nice(struct ksegrp *kg, int nice)
489 {
490 	kg->kg_nice = nice;
491 	resetpriority(kg);
492 }
493 
494 /*
495  * Adjust the priority of a thread.
496  * This may include moving the thread within the KSEGRP,
497  * changing the assignment of a kse to the thread,
498  * and moving a KSE in the system run queue.
499  */
500 void
501 sched_prio(struct thread *td, u_char prio)
502 {
503 
504 	if (TD_ON_RUNQ(td)) {
505 		adjustrunqueue(td, prio);
506 	} else {
507 		td->td_priority = prio;
508 	}
509 }
510 
511 void
512 sched_sleep(struct thread *td, u_char prio)
513 {
514 	td->td_ksegrp->kg_slptime = 0;
515 	td->td_priority = prio;
516 }
517 
518 void
519 sched_switchin(struct thread *td)
520 {
521 	td->td_kse->ke_oncpu = PCPU_GET(cpuid);
522 }
523 
524 void
525 sched_switchout(struct thread *td)
526 {
527 	struct kse *ke;
528 	struct proc *p;
529 
530 	ke = td->td_kse;
531 	p = td->td_proc;
532 
533 	KASSERT((ke->ke_state == KES_THREAD), ("mi_switch: kse state?"));
534 
535 	td->td_lastcpu = ke->ke_oncpu;
536 	td->td_last_kse = ke;
537 	ke->ke_oncpu = NOCPU;
538 	td->td_flags &= ~TDF_NEEDRESCHED;
539 	/*
540 	 * At the last moment, if this thread is still marked RUNNING,
541 	 * then put it back on the run queue as it has not been suspended
542 	 * or stopped or any thing else similar.
543 	 */
544 	if (TD_IS_RUNNING(td)) {
545 		/* Put us back on the run queue (kse and all). */
546 		setrunqueue(td);
547 	} else if (p->p_flag & P_THREADED) {
548 		/*
549 		 * We will not be on the run queue. So we must be
550 		 * sleeping or similar. As it's available,
551 		 * someone else can use the KSE if they need it.
552 		 */
553 		kse_reassign(ke);
554 	}
555 }
556 
557 void
558 sched_wakeup(struct thread *td)
559 {
560 	struct ksegrp *kg;
561 
562 	kg = td->td_ksegrp;
563 	if (kg->kg_slptime > 1)
564 		updatepri(kg);
565 	kg->kg_slptime = 0;
566 	setrunqueue(td);
567 	maybe_resched(td);
568 }
569 
570 void
571 sched_add(struct kse *ke)
572 {
573 	mtx_assert(&sched_lock, MA_OWNED);
574 	KASSERT((ke->ke_thread != NULL), ("runq_add: No thread on KSE"));
575 	KASSERT((ke->ke_thread->td_kse != NULL),
576 	    ("runq_add: No KSE on thread"));
577 	KASSERT(ke->ke_state != KES_ONRUNQ,
578 	    ("runq_add: kse %p (%s) already in run queue", ke,
579 	    ke->ke_proc->p_comm));
580 	KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
581 	    ("runq_add: process swapped out"));
582 	ke->ke_ksegrp->kg_runq_kses++;
583 	ke->ke_state = KES_ONRUNQ;
584 
585 	runq_add(&runq, ke);
586 }
587 
588 void
589 sched_rem(struct kse *ke)
590 {
591 	KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
592 	    ("runq_remove: process swapped out"));
593 	KASSERT((ke->ke_state == KES_ONRUNQ), ("KSE not on run queue"));
594 	mtx_assert(&sched_lock, MA_OWNED);
595 
596 	runq_remove(&runq, ke);
597 	ke->ke_state = KES_THREAD;
598 	ke->ke_ksegrp->kg_runq_kses--;
599 }
600 
601 struct kse *
602 sched_choose(void)
603 {
604 	struct kse *ke;
605 
606 	ke = runq_choose(&runq);
607 
608 	if (ke != NULL) {
609 		runq_remove(&runq, ke);
610 		ke->ke_state = KES_THREAD;
611 
612 		KASSERT((ke->ke_thread != NULL),
613 		    ("runq_choose: No thread on KSE"));
614 		KASSERT((ke->ke_thread->td_kse != NULL),
615 		    ("runq_choose: No KSE on thread"));
616 		KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
617 		    ("runq_choose: process swapped out"));
618 	}
619 	return (ke);
620 }
621 
622 void
623 sched_userret(struct thread *td)
624 {
625 	struct ksegrp *kg;
626 	/*
627 	 * XXX we cheat slightly on the locking here to avoid locking in
628 	 * the usual case.  Setting td_priority here is essentially an
629 	 * incomplete workaround for not setting it properly elsewhere.
630 	 * Now that some interrupt handlers are threads, not setting it
631 	 * properly elsewhere can clobber it in the window between setting
632 	 * it here and returning to user mode, so don't waste time setting
633 	 * it perfectly here.
634 	 */
635 	kg = td->td_ksegrp;
636 	if (td->td_priority != kg->kg_user_pri) {
637 		mtx_lock_spin(&sched_lock);
638 		td->td_priority = kg->kg_user_pri;
639 		mtx_unlock_spin(&sched_lock);
640 	}
641 }
642 
643 int
644 sched_sizeof_kse(void)
645 {
646 	return (sizeof(struct kse) + sizeof(struct ke_sched));
647 }
648 int
649 sched_sizeof_ksegrp(void)
650 {
651 	return (sizeof(struct ksegrp));
652 }
653 int
654 sched_sizeof_proc(void)
655 {
656 	return (sizeof(struct proc));
657 }
658 int
659 sched_sizeof_thread(void)
660 {
661 	return (sizeof(struct thread));
662 }
663 
664 fixpt_t
665 sched_pctcpu(struct kse *ke)
666 {
667 	return (ke->ke_pctcpu);
668 }
669