xref: /freebsd/sys/kern/kern_synch.c (revision 3d238d9e981227b3bf739a51281e5d200bff3f8c)
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  *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_ddb.h"
45 #include "opt_ktrace.h"
46 #ifdef __i386__
47 #include "opt_swtch.h"
48 #endif
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/condvar.h>
53 #include <sys/kernel.h>
54 #include <sys/ktr.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/proc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sched.h>
60 #include <sys/signalvar.h>
61 #include <sys/smp.h>
62 #include <sys/sx.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysproto.h>
65 #include <sys/vmmeter.h>
66 #ifdef DDB
67 #include <ddb/ddb.h>
68 #endif
69 #ifdef KTRACE
70 #include <sys/uio.h>
71 #include <sys/ktrace.h>
72 #endif
73 
74 #include <machine/cpu.h>
75 
76 static void sched_setup(void *dummy);
77 SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
78 
79 int	hogticks;
80 int	lbolt;
81 
82 static struct callout loadav_callout;
83 static struct callout lbolt_callout;
84 
85 struct loadavg averunnable =
86 	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
87 /*
88  * Constants for averages over 1, 5, and 15 minutes
89  * when sampling at 5 second intervals.
90  */
91 static fixpt_t cexp[3] = {
92 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
93 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
94 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
95 };
96 
97 /* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
98 static int      fscale __unused = FSCALE;
99 SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
100 
101 static void	endtsleep(void *);
102 static void	loadav(void *arg);
103 static void	lboltcb(void *arg);
104 
105 /*
106  * We're only looking at 7 bits of the address; everything is
107  * aligned to 4, lots of things are aligned to greater powers
108  * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
109  */
110 #define TABLESIZE	128
111 static TAILQ_HEAD(slpquehead, thread) slpque[TABLESIZE];
112 #define LOOKUP(x)	(((intptr_t)(x) >> 8) & (TABLESIZE - 1))
113 
114 void
115 sleepinit(void)
116 {
117 	int i;
118 
119 	hogticks = (hz / 10) * 2;	/* Default only. */
120 	for (i = 0; i < TABLESIZE; i++)
121 		TAILQ_INIT(&slpque[i]);
122 }
123 
124 /*
125  * General sleep call.  Suspends the current process until a wakeup is
126  * performed on the specified identifier.  The process will then be made
127  * runnable with the specified priority.  Sleeps at most timo/hz seconds
128  * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
129  * before and after sleeping, else signals are not checked.  Returns 0 if
130  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
131  * signal needs to be delivered, ERESTART is returned if the current system
132  * call should be restarted if possible, and EINTR is returned if the system
133  * call should be interrupted by the signal (return EINTR).
134  *
135  * The mutex argument is exited before the caller is suspended, and
136  * entered before msleep returns.  If priority includes the PDROP
137  * flag the mutex is not entered before returning.
138  */
139 
140 int
141 msleep(ident, mtx, priority, wmesg, timo)
142 	void *ident;
143 	struct mtx *mtx;
144 	int priority, timo;
145 	const char *wmesg;
146 {
147 	struct thread *td = curthread;
148 	struct proc *p = td->td_proc;
149 	int sig, catch = priority & PCATCH;
150 	int rval = 0;
151 	WITNESS_SAVE_DECL(mtx);
152 
153 #ifdef KTRACE
154 	if (KTRPOINT(td, KTR_CSW))
155 		ktrcsw(1, 0);
156 #endif
157 	/* XXX: mtx == NULL ?? */
158 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mtx->mtx_object,
159 	    "Sleeping on \"%s\"", wmesg);
160 	KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL,
161 	    ("sleeping without a mutex"));
162 	/*
163 	 * If we are capable of async syscalls and there isn't already
164 	 * another one ready to return, start a new thread
165 	 * and queue it as ready to run. Note that there is danger here
166 	 * because we need to make sure that we don't sleep allocating
167 	 * the thread (recursion here might be bad).
168 	 */
169 	mtx_lock_spin(&sched_lock);
170 	if (p->p_flag & P_SA || p->p_numthreads > 1) {
171 		/*
172 		 * Just don't bother if we are exiting
173 		 * and not the exiting thread or thread was marked as
174 		 * interrupted.
175 		 */
176 		if (catch) {
177 			if ((p->p_flag & P_WEXIT) && p->p_singlethread != td) {
178 				mtx_unlock_spin(&sched_lock);
179 				return (EINTR);
180 			}
181 			if (td->td_flags & TDF_INTERRUPT) {
182 				mtx_unlock_spin(&sched_lock);
183 				return (td->td_intrval);
184 			}
185 		}
186 	}
187 	if (cold ) {
188 		/*
189 		 * During autoconfiguration, just give interrupts
190 		 * a chance, then just return.
191 		 * Don't run any other procs or panic below,
192 		 * in case this is the idle process and already asleep.
193 		 */
194 		if (mtx != NULL && priority & PDROP)
195 			mtx_unlock(mtx);
196 		mtx_unlock_spin(&sched_lock);
197 		return (0);
198 	}
199 
200 	DROP_GIANT();
201 
202 	if (mtx != NULL) {
203 		mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
204 		WITNESS_SAVE(&mtx->mtx_object, mtx);
205 		mtx_unlock(mtx);
206 		if (priority & PDROP)
207 			mtx = NULL;
208 	}
209 
210 	KASSERT(p != NULL, ("msleep1"));
211 	KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
212 
213 	CTR5(KTR_PROC, "msleep: thread %p (pid %d, %s) on %s (%p)",
214 	    td, p->p_pid, p->p_comm, wmesg, ident);
215 
216 	td->td_wchan = ident;
217 	td->td_wmesg = wmesg;
218 	TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], td, td_slpq);
219 	TD_SET_ON_SLEEPQ(td);
220 	if (timo)
221 		callout_reset(&td->td_slpcallout, timo, endtsleep, td);
222 	/*
223 	 * We put ourselves on the sleep queue and start our timeout
224 	 * before calling thread_suspend_check, as we could stop there, and
225 	 * a wakeup or a SIGCONT (or both) could occur while we were stopped.
226 	 * without resuming us, thus we must be ready for sleep
227 	 * when cursig is called.  If the wakeup happens while we're
228 	 * stopped, td->td_wchan will be 0 upon return from cursig.
229 	 */
230 	if (catch) {
231 		CTR3(KTR_PROC, "msleep caught: thread %p (pid %d, %s)", td,
232 		    p->p_pid, p->p_comm);
233 		td->td_flags |= TDF_SINTR;
234 		mtx_unlock_spin(&sched_lock);
235 		PROC_LOCK(p);
236 		mtx_lock(&p->p_sigacts->ps_mtx);
237 		sig = cursig(td);
238 		mtx_unlock(&p->p_sigacts->ps_mtx);
239 		if (sig == 0 && thread_suspend_check(1))
240 			sig = SIGSTOP;
241 		mtx_lock_spin(&sched_lock);
242 		PROC_UNLOCK(p);
243 		if (sig != 0) {
244 			if (TD_ON_SLEEPQ(td))
245 				unsleep(td);
246 		} else if (!TD_ON_SLEEPQ(td))
247 			catch = 0;
248 	} else
249 		sig = 0;
250 
251 	/*
252 	 * Let the scheduler know we're about to voluntarily go to sleep.
253 	 */
254 	sched_sleep(td, priority & PRIMASK);
255 
256 	if (TD_ON_SLEEPQ(td)) {
257 		p->p_stats->p_ru.ru_nvcsw++;
258 		TD_SET_SLEEPING(td);
259 		mi_switch();
260 	}
261 	/*
262 	 * We're awake from voluntary sleep.
263 	 */
264 	CTR3(KTR_PROC, "msleep resume: thread %p (pid %d, %s)", td, p->p_pid,
265 	    p->p_comm);
266 	KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
267 	td->td_flags &= ~TDF_SINTR;
268 	if (td->td_flags & TDF_TIMEOUT) {
269 		td->td_flags &= ~TDF_TIMEOUT;
270 		if (sig == 0)
271 			rval = EWOULDBLOCK;
272 	} else if (td->td_flags & TDF_TIMOFAIL) {
273 		td->td_flags &= ~TDF_TIMOFAIL;
274 	} else if (timo && callout_stop(&td->td_slpcallout) == 0) {
275 		/*
276 		 * This isn't supposed to be pretty.  If we are here, then
277 		 * the endtsleep() callout is currently executing on another
278 		 * CPU and is either spinning on the sched_lock or will be
279 		 * soon.  If we don't synchronize here, there is a chance
280 		 * that this process may msleep() again before the callout
281 		 * has a chance to run and the callout may end up waking up
282 		 * the wrong msleep().  Yuck.
283 		 */
284 		TD_SET_SLEEPING(td);
285 		p->p_stats->p_ru.ru_nivcsw++;
286 		mi_switch();
287 		td->td_flags &= ~TDF_TIMOFAIL;
288 	}
289 	if ((td->td_flags & TDF_INTERRUPT) && (priority & PCATCH) &&
290 	    (rval == 0)) {
291 		rval = td->td_intrval;
292 	}
293 	mtx_unlock_spin(&sched_lock);
294 
295 	if (rval == 0 && catch) {
296 		PROC_LOCK(p);
297 		/* XXX: shouldn't we always be calling cursig() */
298 		mtx_lock(&p->p_sigacts->ps_mtx);
299 		if (sig != 0 || (sig = cursig(td))) {
300 			if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
301 				rval = EINTR;
302 			else
303 				rval = ERESTART;
304 		}
305 		mtx_unlock(&p->p_sigacts->ps_mtx);
306 		PROC_UNLOCK(p);
307 	}
308 #ifdef KTRACE
309 	if (KTRPOINT(td, KTR_CSW))
310 		ktrcsw(0, 0);
311 #endif
312 	PICKUP_GIANT();
313 	if (mtx != NULL) {
314 		mtx_lock(mtx);
315 		WITNESS_RESTORE(&mtx->mtx_object, mtx);
316 	}
317 	return (rval);
318 }
319 
320 /*
321  * Implement timeout for msleep()
322  *
323  * If process hasn't been awakened (wchan non-zero),
324  * set timeout flag and undo the sleep.  If proc
325  * is stopped, just unsleep so it will remain stopped.
326  * MP-safe, called without the Giant mutex.
327  */
328 static void
329 endtsleep(arg)
330 	void *arg;
331 {
332 	register struct thread *td = arg;
333 
334 	CTR3(KTR_PROC, "endtsleep: thread %p (pid %d, %s)",
335 	    td, td->td_proc->p_pid, td->td_proc->p_comm);
336 	mtx_lock_spin(&sched_lock);
337 	/*
338 	 * This is the other half of the synchronization with msleep()
339 	 * described above.  If the TDS_TIMEOUT flag is set, we lost the
340 	 * race and just need to put the process back on the runqueue.
341 	 */
342 	if (TD_ON_SLEEPQ(td)) {
343 		TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq);
344 		TD_CLR_ON_SLEEPQ(td);
345 		td->td_flags |= TDF_TIMEOUT;
346 		td->td_wmesg = NULL;
347 	} else {
348 		td->td_flags |= TDF_TIMOFAIL;
349 	}
350 	TD_CLR_SLEEPING(td);
351 	setrunnable(td);
352 	mtx_unlock_spin(&sched_lock);
353 }
354 
355 /*
356  * Abort a thread, as if an interrupt had occured.  Only abort
357  * interruptable waits (unfortunatly it isn't only safe to abort others).
358  * This is about identical to cv_abort().
359  * Think about merging them?
360  * Also, whatever the signal code does...
361  */
362 void
363 abortsleep(struct thread *td)
364 {
365 
366 	mtx_assert(&sched_lock, MA_OWNED);
367 	/*
368 	 * If the TDF_TIMEOUT flag is set, just leave. A
369 	 * timeout is scheduled anyhow.
370 	 */
371 	if ((td->td_flags & (TDF_TIMEOUT | TDF_SINTR)) == TDF_SINTR) {
372 		if (TD_ON_SLEEPQ(td)) {
373 			unsleep(td);
374 			TD_CLR_SLEEPING(td);
375 			setrunnable(td);
376 		}
377 	}
378 }
379 
380 /*
381  * Remove a process from its wait queue
382  */
383 void
384 unsleep(struct thread *td)
385 {
386 
387 	mtx_lock_spin(&sched_lock);
388 	if (TD_ON_SLEEPQ(td)) {
389 		TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq);
390 		TD_CLR_ON_SLEEPQ(td);
391 		td->td_wmesg = NULL;
392 	}
393 	mtx_unlock_spin(&sched_lock);
394 }
395 
396 /*
397  * Make all processes sleeping on the specified identifier runnable.
398  */
399 void
400 wakeup(ident)
401 	register void *ident;
402 {
403 	register struct slpquehead *qp;
404 	register struct thread *td;
405 	struct thread *ntd;
406 	struct proc *p;
407 
408 	mtx_lock_spin(&sched_lock);
409 	qp = &slpque[LOOKUP(ident)];
410 restart:
411 	for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) {
412 		ntd = TAILQ_NEXT(td, td_slpq);
413 		if (td->td_wchan == ident) {
414 			unsleep(td);
415 			TD_CLR_SLEEPING(td);
416 			setrunnable(td);
417 			p = td->td_proc;
418 			CTR3(KTR_PROC,"wakeup: thread %p (pid %d, %s)",
419 			    td, p->p_pid, p->p_comm);
420 			goto restart;
421 		}
422 	}
423 	mtx_unlock_spin(&sched_lock);
424 }
425 
426 /*
427  * Make a process sleeping on the specified identifier runnable.
428  * May wake more than one process if a target process is currently
429  * swapped out.
430  */
431 void
432 wakeup_one(ident)
433 	register void *ident;
434 {
435 	register struct slpquehead *qp;
436 	register struct thread *td;
437 	register struct proc *p;
438 	struct thread *ntd;
439 
440 	mtx_lock_spin(&sched_lock);
441 	qp = &slpque[LOOKUP(ident)];
442 	for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) {
443 		ntd = TAILQ_NEXT(td, td_slpq);
444 		if (td->td_wchan == ident) {
445 			unsleep(td);
446 			TD_CLR_SLEEPING(td);
447 			setrunnable(td);
448 			p = td->td_proc;
449 			CTR3(KTR_PROC,"wakeup1: thread %p (pid %d, %s)",
450 			    td, p->p_pid, p->p_comm);
451 			break;
452 		}
453 	}
454 	mtx_unlock_spin(&sched_lock);
455 }
456 
457 /*
458  * The machine independent parts of mi_switch().
459  */
460 void
461 mi_switch(void)
462 {
463 	struct bintime new_switchtime;
464 	struct thread *td;
465 #if !defined(__alpha__) && !defined(__powerpc__)
466 	struct thread *newtd;
467 #endif
468 	struct proc *p;
469 	u_int sched_nest;
470 
471 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
472 	td = curthread;			/* XXX */
473 	p = td->td_proc;		/* XXX */
474 	KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code"));
475 #ifdef INVARIANTS
476 	if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td))
477 		mtx_assert(&Giant, MA_NOTOWNED);
478 #endif
479 	KASSERT(td->td_critnest == 1,
480 	    ("mi_switch: switch in a critical section"));
481 
482 	/*
483 	 * Compute the amount of time during which the current
484 	 * process was running, and add that to its total so far.
485 	 */
486 	binuptime(&new_switchtime);
487 	bintime_add(&p->p_runtime, &new_switchtime);
488 	bintime_sub(&p->p_runtime, PCPU_PTR(switchtime));
489 
490 #ifdef DDB
491 	/*
492 	 * Don't perform context switches from the debugger.
493 	 */
494 	if (db_active) {
495 		mtx_unlock_spin(&sched_lock);
496 		db_print_backtrace();
497 		db_error("Context switches not allowed in the debugger.");
498 	}
499 #endif
500 
501 	/*
502 	 * Check if the process exceeds its cpu resource allocation.  If
503 	 * over max, arrange to kill the process in ast().
504 	 */
505 	if (p->p_cpulimit != RLIM_INFINITY &&
506 	    p->p_runtime.sec > p->p_cpulimit) {
507 		p->p_sflag |= PS_XCPU;
508 		td->td_flags |= TDF_ASTPENDING;
509 	}
510 
511 	/*
512 	 * Finish up stats for outgoing thread.
513 	 */
514 	cnt.v_swtch++;
515 	PCPU_SET(switchtime, new_switchtime);
516 	CTR3(KTR_PROC, "mi_switch: old thread %p (pid %d, %s)", td, p->p_pid,
517 	    p->p_comm);
518 	sched_nest = sched_lock.mtx_recurse;
519 	if (td->td_proc->p_flag & P_SA)
520 		thread_switchout(td);
521 	sched_switchout(td);
522 
523 #if !defined(__alpha__) && !defined(__powerpc__)
524 	newtd = choosethread();
525 	if (td != newtd)
526 		cpu_switch(td, newtd);	/* SHAZAM!! */
527 #else
528 	cpu_switch();		/* SHAZAM!!*/
529 #endif
530 
531 	sched_lock.mtx_recurse = sched_nest;
532 	sched_lock.mtx_lock = (uintptr_t)td;
533 	sched_switchin(td);
534 
535 	/*
536 	 * Start setting up stats etc. for the incoming thread.
537 	 * Similar code in fork_exit() is returned to by cpu_switch()
538 	 * in the case of a new thread/process.
539 	 */
540 	CTR3(KTR_PROC, "mi_switch: new thread %p (pid %d, %s)", td, p->p_pid,
541 	    p->p_comm);
542 	if (PCPU_GET(switchtime.sec) == 0)
543 		binuptime(PCPU_PTR(switchtime));
544 	PCPU_SET(switchticks, ticks);
545 
546 	/*
547 	 * Call the switchin function while still holding the scheduler lock
548 	 * (used by the idlezero code and the general page-zeroing code)
549 	 */
550 	if (td->td_switchin)
551 		td->td_switchin();
552 
553 	/*
554 	 * If the last thread was exiting, finish cleaning it up.
555 	 */
556 	if ((td = PCPU_GET(deadthread))) {
557 		PCPU_SET(deadthread, NULL);
558 		thread_stash(td);
559 	}
560 }
561 
562 /*
563  * Change process state to be runnable,
564  * placing it on the run queue if it is in memory,
565  * and awakening the swapper if it isn't in memory.
566  */
567 void
568 setrunnable(struct thread *td)
569 {
570 	struct proc *p = td->td_proc;
571 
572 	mtx_assert(&sched_lock, MA_OWNED);
573 	switch (p->p_state) {
574 	case PRS_ZOMBIE:
575 		panic("setrunnable(1)");
576 	default:
577 		break;
578 	}
579 	switch (td->td_state) {
580 	case TDS_RUNNING:
581 	case TDS_RUNQ:
582 		return;
583 	case TDS_INHIBITED:
584 		/*
585 		 * If we are only inhibited because we are swapped out
586 		 * then arange to swap in this process. Otherwise just return.
587 		 */
588 		if (td->td_inhibitors != TDI_SWAPPED)
589 			return;
590 		/* XXX: intentional fall-through ? */
591 	case TDS_CAN_RUN:
592 		break;
593 	default:
594 		printf("state is 0x%x", td->td_state);
595 		panic("setrunnable(2)");
596 	}
597 	if ((p->p_sflag & PS_INMEM) == 0) {
598 		if ((p->p_sflag & PS_SWAPPINGIN) == 0) {
599 			p->p_sflag |= PS_SWAPINREQ;
600 			wakeup(&proc0);
601 		}
602 	} else
603 		sched_wakeup(td);
604 }
605 
606 /*
607  * Compute a tenex style load average of a quantity on
608  * 1, 5 and 15 minute intervals.
609  * XXXKSE   Needs complete rewrite when correct info is available.
610  * Completely Bogus.. only works with 1:1 (but compiles ok now :-)
611  */
612 static void
613 loadav(void *arg)
614 {
615 	int i, nrun;
616 	struct loadavg *avg;
617 	struct proc *p;
618 	struct thread *td;
619 
620 	avg = &averunnable;
621 	sx_slock(&allproc_lock);
622 	nrun = 0;
623 	FOREACH_PROC_IN_SYSTEM(p) {
624 		FOREACH_THREAD_IN_PROC(p, td) {
625 			switch (td->td_state) {
626 			case TDS_RUNQ:
627 			case TDS_RUNNING:
628 				if ((p->p_flag & P_NOLOAD) != 0)
629 					goto nextproc;
630 				nrun++; /* XXXKSE */
631 			default:
632 				break;
633 			}
634 nextproc:
635 			continue;
636 		}
637 	}
638 	sx_sunlock(&allproc_lock);
639 	for (i = 0; i < 3; i++)
640 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
641 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
642 
643 	/*
644 	 * Schedule the next update to occur after 5 seconds, but add a
645 	 * random variation to avoid synchronisation with processes that
646 	 * run at regular intervals.
647 	 */
648 	callout_reset(&loadav_callout, hz * 4 + (int)(random() % (hz * 2 + 1)),
649 	    loadav, NULL);
650 }
651 
652 static void
653 lboltcb(void *arg)
654 {
655 	wakeup(&lbolt);
656 	callout_reset(&lbolt_callout, hz, lboltcb, NULL);
657 }
658 
659 /* ARGSUSED */
660 static void
661 sched_setup(dummy)
662 	void *dummy;
663 {
664 	callout_init(&loadav_callout, 0);
665 	callout_init(&lbolt_callout, 1);
666 
667 	/* Kick off timeout driven events by calling first time. */
668 	loadav(NULL);
669 	lboltcb(NULL);
670 }
671 
672 /*
673  * General purpose yield system call
674  */
675 int
676 yield(struct thread *td, struct yield_args *uap)
677 {
678 	struct ksegrp *kg = td->td_ksegrp;
679 
680 	mtx_assert(&Giant, MA_NOTOWNED);
681 	mtx_lock_spin(&sched_lock);
682 	kg->kg_proc->p_stats->p_ru.ru_nvcsw++;
683 	sched_prio(td, PRI_MAX_TIMESHARE);
684 	mi_switch();
685 	mtx_unlock_spin(&sched_lock);
686 	td->td_retval[0] = 0;
687 
688 	return (0);
689 }
690 
691