kern_synch.c revision 247787
1343835Sdes/*-
2343835Sdes * Copyright (c) 1982, 1986, 1990, 1991, 1993
3343835Sdes *	The Regents of the University of California.  All rights reserved.
4343835Sdes * (c) UNIX System Laboratories, Inc.
5343835Sdes * All or some portions of this file are derived from material licensed
6343835Sdes * to the University of California by American Telephone and Telegraph
7343835Sdes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8343835Sdes * the permission of UNIX System Laboratories, Inc.
9343835Sdes *
10343835Sdes * Redistribution and use in source and binary forms, with or without
11343835Sdes * modification, are permitted provided that the following conditions
12343835Sdes * are met:
13343835Sdes * 1. Redistributions of source code must retain the above copyright
14343835Sdes *    notice, this list of conditions and the following disclaimer.
15343835Sdes * 2. Redistributions in binary form must reproduce the above copyright
16343835Sdes *    notice, this list of conditions and the following disclaimer in the
17343835Sdes *    documentation and/or other materials provided with the distribution.
18343835Sdes * 4. Neither the name of the University nor the names of its contributors
19343835Sdes *    may be used to endorse or promote products derived from this software
20343835Sdes *    without specific prior written permission.
21343835Sdes *
22343835Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23343835Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24343835Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25343835Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26343835Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27343835Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28343835Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29343835Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30343835Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31343835Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32343835Sdes * SUCH DAMAGE.
33343835Sdes *
34343835Sdes *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
35343835Sdes */
36343835Sdes
37343835Sdes#include <sys/cdefs.h>
38343835Sdes__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 247787 2013-03-04 12:48:41Z davide $");
39343835Sdes
40343835Sdes#include "opt_kdtrace.h"
41343835Sdes#include "opt_ktrace.h"
42343835Sdes#include "opt_sched.h"
43343835Sdes
44343835Sdes#include <sys/param.h>
45343835Sdes#include <sys/systm.h>
46343835Sdes#include <sys/condvar.h>
47343835Sdes#include <sys/kdb.h>
48343835Sdes#include <sys/kernel.h>
49343835Sdes#include <sys/ktr.h>
50343835Sdes#include <sys/lock.h>
51343835Sdes#include <sys/mutex.h>
52343835Sdes#include <sys/proc.h>
53343835Sdes#include <sys/resourcevar.h>
54343835Sdes#include <sys/sched.h>
55343835Sdes#include <sys/sdt.h>
56343835Sdes#include <sys/signalvar.h>
57343835Sdes#include <sys/sleepqueue.h>
58343835Sdes#include <sys/smp.h>
59343835Sdes#include <sys/sx.h>
60343835Sdes#include <sys/sysctl.h>
61343835Sdes#include <sys/sysproto.h>
62343835Sdes#include <sys/vmmeter.h>
63343835Sdes#ifdef KTRACE
64343835Sdes#include <sys/uio.h>
65343835Sdes#include <sys/ktrace.h>
66343835Sdes#endif
67343835Sdes
68343835Sdes#include <machine/cpu.h>
69343835Sdes
70343835Sdes#ifdef XEN
71343835Sdes#include <vm/vm.h>
72343835Sdes#include <vm/vm_param.h>
73343835Sdes#include <vm/pmap.h>
74343835Sdes#endif
75343835Sdes
76343835Sdes#define	KTDSTATE(td)							\
77343835Sdes	(((td)->td_inhibitors & TDI_SLEEPING) != 0 ? "sleep"  :		\
78343835Sdes	((td)->td_inhibitors & TDI_SUSPENDED) != 0 ? "suspended" :	\
79343835Sdes	((td)->td_inhibitors & TDI_SWAPPED) != 0 ? "swapped" :		\
80343835Sdes	((td)->td_inhibitors & TDI_LOCK) != 0 ? "blocked" :		\
81343835Sdes	((td)->td_inhibitors & TDI_IWAIT) != 0 ? "iwait" : "yielding")
82343835Sdes
83343835Sdesstatic void synch_setup(void *dummy);
84343835SdesSYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup,
85343835Sdes    NULL);
86343835Sdes
87343835Sdesint	hogticks;
88343835Sdesstatic int pause_wchan;
89343835Sdes
90343835Sdesstatic struct callout loadav_callout;
91343835Sdes
92343835Sdesstruct loadavg averunnable =
93343835Sdes	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
94343835Sdes/*
95343835Sdes * Constants for averages over 1, 5, and 15 minutes
96343835Sdes * when sampling at 5 second intervals.
97343835Sdes */
98343835Sdesstatic fixpt_t cexp[3] = {
99343835Sdes	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
100343835Sdes	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
101343835Sdes	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
102343835Sdes};
103343835Sdes
104343835Sdes/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
105343835Sdesstatic int      fscale __unused = FSCALE;
106343835SdesSYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
107343835Sdes
108343835Sdesstatic void	loadav(void *arg);
109343835Sdes
110343835SdesSDT_PROVIDER_DECLARE(sched);
111343835SdesSDT_PROBE_DEFINE(sched, , , preempt, preempt);
112343835Sdes
113343835Sdes/*
114343835Sdes * These probes reference Solaris features that are not implemented in FreeBSD.
115343835Sdes * Create the probes anyway for compatibility with existing D scripts; they'll
116343835Sdes * just never fire.
117343835Sdes */
118343835SdesSDT_PROBE_DEFINE(sched, , , cpucaps_sleep, cpucaps-sleep);
119343835SdesSDT_PROBE_DEFINE(sched, , , cpucaps_wakeup, cpucaps-wakeup);
120343835SdesSDT_PROBE_DEFINE(sched, , , schedctl_nopreempt, schedctl-nopreempt);
121343835SdesSDT_PROBE_DEFINE(sched, , , schedctl_preempt, schedctl-preempt);
122343835SdesSDT_PROBE_DEFINE(sched, , , schedctl_yield, schedctl-yield);
123343835Sdes
124343835Sdesvoid
125343835Sdessleepinit(void)
126343835Sdes{
127343835Sdes
128343835Sdes	hogticks = (hz / 10) * 2;	/* Default only. */
129343835Sdes	init_sleepqueues();
130343835Sdes}
131343835Sdes
132343835Sdes/*
133343835Sdes * General sleep call.  Suspends the current thread until a wakeup is
134343835Sdes * performed on the specified identifier.  The thread will then be made
135343835Sdes * runnable with the specified priority.  Sleeps at most timo/hz seconds
136343835Sdes * (0 means no timeout).  If pri includes the PCATCH flag, let signals
137343835Sdes * interrupt the sleep, otherwise ignore them while sleeping.  Returns 0 if
138343835Sdes * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
139343835Sdes * signal becomes pending, ERESTART is returned if the current system
140343835Sdes * call should be restarted if possible, and EINTR is returned if the system
141343835Sdes * call should be interrupted by the signal (return EINTR).
142343835Sdes *
143343835Sdes * The lock argument is unlocked before the caller is suspended, and
144343835Sdes * re-locked before _sleep() returns.  If priority includes the PDROP
145343835Sdes * flag the lock is not re-locked before returning.
146343835Sdes */
147343835Sdesint
148343835Sdes_sleep(void *ident, struct lock_object *lock, int priority,
149    const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
150{
151	struct thread *td;
152	struct proc *p;
153	struct lock_class *class;
154	int catch, lock_state, pri, rval, sleepq_flags;
155	WITNESS_SAVE_DECL(lock_witness);
156
157	td = curthread;
158	p = td->td_proc;
159#ifdef KTRACE
160	if (KTRPOINT(td, KTR_CSW))
161		ktrcsw(1, 0, wmesg);
162#endif
163	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
164	    "Sleeping on \"%s\"", wmesg);
165	KASSERT(sbt != 0 || mtx_owned(&Giant) || lock != NULL,
166	    ("sleeping without a lock"));
167	KASSERT(p != NULL, ("msleep1"));
168	KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
169	if (priority & PDROP)
170		KASSERT(lock != NULL && lock != &Giant.lock_object,
171		    ("PDROP requires a non-Giant lock"));
172	if (lock != NULL)
173		class = LOCK_CLASS(lock);
174	else
175		class = NULL;
176
177	if (cold || SCHEDULER_STOPPED()) {
178		/*
179		 * During autoconfiguration, just return;
180		 * don't run any other threads or panic below,
181		 * in case this is the idle thread and already asleep.
182		 * XXX: this used to do "s = splhigh(); splx(safepri);
183		 * splx(s);" to give interrupts a chance, but there is
184		 * no way to give interrupts a chance now.
185		 */
186		if (lock != NULL && priority & PDROP)
187			class->lc_unlock(lock);
188		return (0);
189	}
190	catch = priority & PCATCH;
191	pri = priority & PRIMASK;
192
193	/*
194	 * If we are already on a sleep queue, then remove us from that
195	 * sleep queue first.  We have to do this to handle recursive
196	 * sleeps.
197	 */
198	if (TD_ON_SLEEPQ(td))
199		sleepq_remove(td, td->td_wchan);
200
201	if (ident == &pause_wchan)
202		sleepq_flags = SLEEPQ_PAUSE;
203	else
204		sleepq_flags = SLEEPQ_SLEEP;
205	if (catch)
206		sleepq_flags |= SLEEPQ_INTERRUPTIBLE;
207	if (priority & PBDRY)
208		sleepq_flags |= SLEEPQ_STOP_ON_BDRY;
209
210	sleepq_lock(ident);
211	CTR5(KTR_PROC, "sleep: thread %ld (pid %ld, %s) on %s (%p)",
212	    td->td_tid, p->p_pid, td->td_name, wmesg, ident);
213
214	if (lock == &Giant.lock_object)
215		mtx_assert(&Giant, MA_OWNED);
216	DROP_GIANT();
217	if (lock != NULL && lock != &Giant.lock_object &&
218	    !(class->lc_flags & LC_SLEEPABLE)) {
219		WITNESS_SAVE(lock, lock_witness);
220		lock_state = class->lc_unlock(lock);
221	} else
222		/* GCC needs to follow the Yellow Brick Road */
223		lock_state = -1;
224
225	/*
226	 * We put ourselves on the sleep queue and start our timeout
227	 * before calling thread_suspend_check, as we could stop there,
228	 * and a wakeup or a SIGCONT (or both) could occur while we were
229	 * stopped without resuming us.  Thus, we must be ready for sleep
230	 * when cursig() is called.  If the wakeup happens while we're
231	 * stopped, then td will no longer be on a sleep queue upon
232	 * return from cursig().
233	 */
234	sleepq_add(ident, lock, wmesg, sleepq_flags, 0);
235	if (sbt != 0)
236		sleepq_set_timeout_sbt(ident, sbt, pr, flags);
237	if (lock != NULL && class->lc_flags & LC_SLEEPABLE) {
238		sleepq_release(ident);
239		WITNESS_SAVE(lock, lock_witness);
240		lock_state = class->lc_unlock(lock);
241		sleepq_lock(ident);
242	}
243	if (sbt != 0 && catch)
244		rval = sleepq_timedwait_sig(ident, pri);
245	else if (sbt != 0)
246		rval = sleepq_timedwait(ident, pri);
247	else if (catch)
248		rval = sleepq_wait_sig(ident, pri);
249	else {
250		sleepq_wait(ident, pri);
251		rval = 0;
252	}
253#ifdef KTRACE
254	if (KTRPOINT(td, KTR_CSW))
255		ktrcsw(0, 0, wmesg);
256#endif
257	PICKUP_GIANT();
258	if (lock != NULL && lock != &Giant.lock_object && !(priority & PDROP)) {
259		class->lc_lock(lock, lock_state);
260		WITNESS_RESTORE(lock, lock_witness);
261	}
262	return (rval);
263}
264
265int
266msleep_spin_sbt(void *ident, struct mtx *mtx, const char *wmesg,
267    sbintime_t sbt, sbintime_t pr, int flags)
268{
269	struct thread *td;
270	struct proc *p;
271	int rval;
272	WITNESS_SAVE_DECL(mtx);
273
274	td = curthread;
275	p = td->td_proc;
276	KASSERT(mtx != NULL, ("sleeping without a mutex"));
277	KASSERT(p != NULL, ("msleep1"));
278	KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
279
280	if (cold || SCHEDULER_STOPPED()) {
281		/*
282		 * During autoconfiguration, just return;
283		 * don't run any other threads or panic below,
284		 * in case this is the idle thread and already asleep.
285		 * XXX: this used to do "s = splhigh(); splx(safepri);
286		 * splx(s);" to give interrupts a chance, but there is
287		 * no way to give interrupts a chance now.
288		 */
289		return (0);
290	}
291
292	sleepq_lock(ident);
293	CTR5(KTR_PROC, "msleep_spin: thread %ld (pid %ld, %s) on %s (%p)",
294	    td->td_tid, p->p_pid, td->td_name, wmesg, ident);
295
296	DROP_GIANT();
297	mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
298	WITNESS_SAVE(&mtx->lock_object, mtx);
299	mtx_unlock_spin(mtx);
300
301	/*
302	 * We put ourselves on the sleep queue and start our timeout.
303	 */
304	sleepq_add(ident, &mtx->lock_object, wmesg, SLEEPQ_SLEEP, 0);
305	if (sbt != 0)
306		sleepq_set_timeout_sbt(ident, sbt, pr, flags);
307
308	/*
309	 * Can't call ktrace with any spin locks held so it can lock the
310	 * ktrace_mtx lock, and WITNESS_WARN considers it an error to hold
311	 * any spin lock.  Thus, we have to drop the sleepq spin lock while
312	 * we handle those requests.  This is safe since we have placed our
313	 * thread on the sleep queue already.
314	 */
315#ifdef KTRACE
316	if (KTRPOINT(td, KTR_CSW)) {
317		sleepq_release(ident);
318		ktrcsw(1, 0, wmesg);
319		sleepq_lock(ident);
320	}
321#endif
322#ifdef WITNESS
323	sleepq_release(ident);
324	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "Sleeping on \"%s\"",
325	    wmesg);
326	sleepq_lock(ident);
327#endif
328	if (sbt != 0)
329		rval = sleepq_timedwait(ident, 0);
330	else {
331		sleepq_wait(ident, 0);
332		rval = 0;
333	}
334#ifdef KTRACE
335	if (KTRPOINT(td, KTR_CSW))
336		ktrcsw(0, 0, wmesg);
337#endif
338	PICKUP_GIANT();
339	mtx_lock_spin(mtx);
340	WITNESS_RESTORE(&mtx->lock_object, mtx);
341	return (rval);
342}
343
344/*
345 * pause() delays the calling thread by the given number of system ticks.
346 * During cold bootup, pause() uses the DELAY() function instead of
347 * the tsleep() function to do the waiting. The "timo" argument must be
348 * greater than or equal to zero. A "timo" value of zero is equivalent
349 * to a "timo" value of one.
350 */
351int
352pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
353{
354	int sbt_sec;
355
356	sbt_sec = sbintime_getsec(sbt);
357	KASSERT(sbt_sec >= 0, ("pause: timo must be >= 0"));
358
359	/* silently convert invalid timeouts */
360	if (sbt == 0)
361		sbt = tick_sbt;
362
363	if (cold) {
364		/*
365		 * We delay one second at a time to avoid overflowing the
366		 * system specific DELAY() function(s):
367		 */
368		while (sbt_sec > 0) {
369			DELAY(1000000);
370			sbt_sec--;
371		}
372		DELAY((sbt & 0xffffffff) / SBT_1US);
373		return (0);
374	}
375	return (_sleep(&pause_wchan, NULL, 0, wmesg, sbt, pr, flags));
376}
377
378/*
379 * Make all threads sleeping on the specified identifier runnable.
380 */
381void
382wakeup(void *ident)
383{
384	int wakeup_swapper;
385
386	sleepq_lock(ident);
387	wakeup_swapper = sleepq_broadcast(ident, SLEEPQ_SLEEP, 0, 0);
388	sleepq_release(ident);
389	if (wakeup_swapper) {
390		KASSERT(ident != &proc0,
391		    ("wakeup and wakeup_swapper and proc0"));
392		kick_proc0();
393	}
394}
395
396/*
397 * Make a thread sleeping on the specified identifier runnable.
398 * May wake more than one thread if a target thread is currently
399 * swapped out.
400 */
401void
402wakeup_one(void *ident)
403{
404	int wakeup_swapper;
405
406	sleepq_lock(ident);
407	wakeup_swapper = sleepq_signal(ident, SLEEPQ_SLEEP, 0, 0);
408	sleepq_release(ident);
409	if (wakeup_swapper)
410		kick_proc0();
411}
412
413static void
414kdb_switch(void)
415{
416	thread_unlock(curthread);
417	kdb_backtrace();
418	kdb_reenter();
419	panic("%s: did not reenter debugger", __func__);
420}
421
422/*
423 * The machine independent parts of context switching.
424 */
425void
426mi_switch(int flags, struct thread *newtd)
427{
428	uint64_t runtime, new_switchtime;
429	struct thread *td;
430	struct proc *p;
431
432	td = curthread;			/* XXX */
433	THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED);
434	p = td->td_proc;		/* XXX */
435	KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code"));
436#ifdef INVARIANTS
437	if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td))
438		mtx_assert(&Giant, MA_NOTOWNED);
439#endif
440	KASSERT(td->td_critnest == 1 || panicstr,
441	    ("mi_switch: switch in a critical section"));
442	KASSERT((flags & (SW_INVOL | SW_VOL)) != 0,
443	    ("mi_switch: switch must be voluntary or involuntary"));
444	KASSERT(newtd != curthread, ("mi_switch: preempting back to ourself"));
445
446	/*
447	 * Don't perform context switches from the debugger.
448	 */
449	if (kdb_active)
450		kdb_switch();
451	if (SCHEDULER_STOPPED())
452		return;
453	if (flags & SW_VOL) {
454		td->td_ru.ru_nvcsw++;
455		td->td_swvoltick = ticks;
456	} else
457		td->td_ru.ru_nivcsw++;
458#ifdef SCHED_STATS
459	SCHED_STAT_INC(sched_switch_stats[flags & SW_TYPE_MASK]);
460#endif
461	/*
462	 * Compute the amount of time during which the current
463	 * thread was running, and add that to its total so far.
464	 */
465	new_switchtime = cpu_ticks();
466	runtime = new_switchtime - PCPU_GET(switchtime);
467	td->td_runtime += runtime;
468	td->td_incruntime += runtime;
469	PCPU_SET(switchtime, new_switchtime);
470	td->td_generation++;	/* bump preempt-detect counter */
471	PCPU_INC(cnt.v_swtch);
472	PCPU_SET(switchticks, ticks);
473	CTR4(KTR_PROC, "mi_switch: old thread %ld (td_sched %p, pid %ld, %s)",
474	    td->td_tid, td->td_sched, p->p_pid, td->td_name);
475#if (KTR_COMPILE & KTR_SCHED) != 0
476	if (TD_IS_IDLETHREAD(td))
477		KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "idle",
478		    "prio:%d", td->td_priority);
479	else
480		KTR_STATE3(KTR_SCHED, "thread", sched_tdname(td), KTDSTATE(td),
481		    "prio:%d", td->td_priority, "wmesg:\"%s\"", td->td_wmesg,
482		    "lockname:\"%s\"", td->td_lockname);
483#endif
484	SDT_PROBE0(sched, , , preempt);
485#ifdef XEN
486	PT_UPDATES_FLUSH();
487#endif
488	sched_switch(td, newtd, flags);
489	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "running",
490	    "prio:%d", td->td_priority);
491
492	CTR4(KTR_PROC, "mi_switch: new thread %ld (td_sched %p, pid %ld, %s)",
493	    td->td_tid, td->td_sched, p->p_pid, td->td_name);
494
495	/*
496	 * If the last thread was exiting, finish cleaning it up.
497	 */
498	if ((td = PCPU_GET(deadthread))) {
499		PCPU_SET(deadthread, NULL);
500		thread_stash(td);
501	}
502}
503
504/*
505 * Change thread state to be runnable, placing it on the run queue if
506 * it is in memory.  If it is swapped out, return true so our caller
507 * will know to awaken the swapper.
508 */
509int
510setrunnable(struct thread *td)
511{
512
513	THREAD_LOCK_ASSERT(td, MA_OWNED);
514	KASSERT(td->td_proc->p_state != PRS_ZOMBIE,
515	    ("setrunnable: pid %d is a zombie", td->td_proc->p_pid));
516	switch (td->td_state) {
517	case TDS_RUNNING:
518	case TDS_RUNQ:
519		return (0);
520	case TDS_INHIBITED:
521		/*
522		 * If we are only inhibited because we are swapped out
523		 * then arange to swap in this process. Otherwise just return.
524		 */
525		if (td->td_inhibitors != TDI_SWAPPED)
526			return (0);
527		/* FALLTHROUGH */
528	case TDS_CAN_RUN:
529		break;
530	default:
531		printf("state is 0x%x", td->td_state);
532		panic("setrunnable(2)");
533	}
534	if ((td->td_flags & TDF_INMEM) == 0) {
535		if ((td->td_flags & TDF_SWAPINREQ) == 0) {
536			td->td_flags |= TDF_SWAPINREQ;
537			return (1);
538		}
539	} else
540		sched_wakeup(td);
541	return (0);
542}
543
544/*
545 * Compute a tenex style load average of a quantity on
546 * 1, 5 and 15 minute intervals.
547 */
548static void
549loadav(void *arg)
550{
551	int i, nrun;
552	struct loadavg *avg;
553
554	nrun = sched_load();
555	avg = &averunnable;
556
557	for (i = 0; i < 3; i++)
558		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
559		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
560
561	/*
562	 * Schedule the next update to occur after 5 seconds, but add a
563	 * random variation to avoid synchronisation with processes that
564	 * run at regular intervals.
565	 */
566	callout_reset_sbt(&loadav_callout,
567	    tick_sbt * (hz * 4 + (int)(random() % (hz * 2 + 1))), 0,
568	    loadav, NULL, C_DIRECT_EXEC | C_HARDCLOCK);
569}
570
571/* ARGSUSED */
572static void
573synch_setup(void *dummy)
574{
575	callout_init(&loadav_callout, CALLOUT_MPSAFE);
576
577	/* Kick off timeout driven events by calling first time. */
578	loadav(NULL);
579}
580
581int
582should_yield(void)
583{
584
585	return (ticks - curthread->td_swvoltick >= hogticks);
586}
587
588void
589maybe_yield(void)
590{
591
592	if (should_yield())
593		kern_yield(PRI_USER);
594}
595
596void
597kern_yield(int prio)
598{
599	struct thread *td;
600
601	td = curthread;
602	DROP_GIANT();
603	thread_lock(td);
604	if (prio == PRI_USER)
605		prio = td->td_user_pri;
606	if (prio >= 0)
607		sched_prio(td, prio);
608	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
609	thread_unlock(td);
610	PICKUP_GIANT();
611}
612
613/*
614 * General purpose yield system call.
615 */
616int
617sys_yield(struct thread *td, struct yield_args *uap)
618{
619
620	thread_lock(td);
621	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
622		sched_prio(td, PRI_MAX_TIMESHARE);
623	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
624	thread_unlock(td);
625	td->td_retval[0] = 0;
626	return (0);
627}
628