kern_synch.c revision 78638
112795Swpaul/*-
212795Swpaul * Copyright (c) 1982, 1986, 1990, 1991, 1993
312795Swpaul *	The Regents of the University of California.  All rights reserved.
412795Swpaul * (c) UNIX System Laboratories, Inc.
512795Swpaul * All or some portions of this file are derived from material licensed
612795Swpaul * to the University of California by American Telephone and Telegraph
712795Swpaul * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8100441Scharnier * the permission of UNIX System Laboratories, Inc.
912795Swpaul *
1012795Swpaul * Redistribution and use in source and binary forms, with or without
1112795Swpaul * modification, are permitted provided that the following conditions
12100441Scharnier * are met:
1312795Swpaul * 1. Redistributions of source code must retain the above copyright
1412795Swpaul *    notice, this list of conditions and the following disclaimer.
1512795Swpaul * 2. Redistributions in binary form must reproduce the above copyright
16100441Scharnier *    notice, this list of conditions and the following disclaimer in the
1712795Swpaul *    documentation and/or other materials provided with the distribution.
1812795Swpaul * 3. All advertising materials mentioning features or use of this software
1912795Swpaul *    must display the following acknowledgement:
20100441Scharnier *	This product includes software developed by the University of
2112795Swpaul *	California, Berkeley and its contributors.
2212795Swpaul * 4. Neither the name of the University nor the names of its contributors
2312795Swpaul *    may be used to endorse or promote products derived from this software
24100441Scharnier *    without specific prior written permission.
2512795Swpaul *
2612795Swpaul * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2712795Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2812795Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2912795Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30100441Scharnier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3112795Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32146833Sstefanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3312795Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3412795Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3527935Scharnier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3612795Swpaul * SUCH DAMAGE.
37100441Scharnier *
38100441Scharnier *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
39100441Scharnier * $FreeBSD: head/sys/kern/kern_synch.c 78638 2001-06-22 23:11:26Z jhb $
4012795Swpaul */
4112795Swpaul
4212795Swpaul#include "opt_ktrace.h"
4312795Swpaul
4427935Scharnier#include <sys/param.h>
4512795Swpaul#include <sys/systm.h>
46200462Sdelphij#include <sys/condvar.h>
4712795Swpaul#include <sys/kernel.h>
48149682Sstefanf#include <sys/ktr.h>
4912795Swpaul#include <sys/lock.h>
5012795Swpaul#include <sys/mutex.h>
5112795Swpaul#include <sys/proc.h>
5212795Swpaul#include <sys/resourcevar.h>
5312795Swpaul#include <sys/signalvar.h>
5412795Swpaul#include <sys/smp.h>
5512795Swpaul#include <sys/sx.h>
5612795Swpaul#include <sys/sysctl.h>
5712795Swpaul#include <sys/sysproto.h>
58141614Salfred#include <sys/vmmeter.h>
5912795Swpaul#include <vm/vm.h>
60141614Salfred#include <vm/vm_extern.h>
6112795Swpaul#ifdef KTRACE
62141614Salfred#include <sys/uio.h>
6312795Swpaul#include <sys/ktrace.h>
6412795Swpaul#endif
6512795Swpaul
6612795Swpaul#include <machine/cpu.h>
6792921Simp
68152398Sdwmalonestatic void sched_setup __P((void *dummy));
6912795SwpaulSYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
7012795Swpaul
71152398Sdwmaloneint	hogticks;
7212795Swpaulint	lbolt;
7312795Swpaulint	sched_quantum;		/* Roundrobin scheduling quantum in ticks. */
7412795Swpaul
7512795Swpaulstatic struct callout schedcpu_callout;
7612795Swpaulstatic struct callout roundrobin_callout;
7712795Swpaul
7812795Swpaulstatic void	endtsleep __P((void *));
7912795Swpaulstatic void	roundrobin __P((void *arg));
8012795Swpaulstatic void	schedcpu __P((void *arg));
8112795Swpaul
8212795Swpaulstatic int
8312795Swpaulsysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
8412795Swpaul{
8517142Sjkh	int error, new_val;
86152398Sdwmalone
8712795Swpaul	new_val = sched_quantum * tick;
8812795Swpaul	error = sysctl_handle_int(oidp, &new_val, 0, req);
8912795Swpaul        if (error != 0 || req->newptr == NULL)
9012795Swpaul		return (error);
9112795Swpaul	if (new_val < tick)
9212795Swpaul		return (EINVAL);
9312795Swpaul	sched_quantum = new_val / tick;
9412795Swpaul	hogticks = 2 * sched_quantum;
9512795Swpaul	return (0);
9612795Swpaul}
9712795Swpaul
9812795SwpaulSYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
9912795Swpaul	0, sizeof sched_quantum, sysctl_kern_quantum, "I", "");
10012795Swpaul
10112795Swpaul/*
10212795Swpaul * Arrange to reschedule if necessary, taking the priorities and
10312795Swpaul * schedulers into account.
10412795Swpaul */
10512795Swpaulvoid
106222080Sbenlmaybe_resched(p)
10712795Swpaul	struct proc *p;
10812795Swpaul{
10912795Swpaul
11012795Swpaul	mtx_assert(&sched_lock, MA_OWNED);
11112795Swpaul	if (p->p_pri.pri_level < curproc->p_pri.pri_level)
11212795Swpaul		need_resched(curproc);
11312795Swpaul}
11427935Scharnier
11512795Swpaulint
11612795Swpaulroundrobin_interval(void)
11712795Swpaul{
11812795Swpaul	return (sched_quantum);
11912795Swpaul}
120141614Salfred
12112795Swpaul/*
12212795Swpaul * Force switch among equal priority processes every 100ms.
123149709Sstefanf */
12412795Swpaul/* ARGSUSED */
12512795Swpaulstatic void
12612795Swpaulroundrobin(arg)
12712795Swpaul	void *arg;
12812795Swpaul{
12912795Swpaul
13012795Swpaul	mtx_lock_spin(&sched_lock);
13112795Swpaul	need_resched(curproc);
13212795Swpaul#ifdef SMP
13312795Swpaul	forward_roundrobin();
13412795Swpaul#endif
135100441Scharnier	mtx_unlock_spin(&sched_lock);
13612795Swpaul
13712795Swpaul	callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL);
13812795Swpaul}
13912795Swpaul
14012795Swpaul/*
14112795Swpaul * Constants for digital decay and forget:
14212795Swpaul *	90% of (p_estcpu) usage in 5 * loadav time
14312795Swpaul *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
144222080Sbenl *          Note that, as ps(1) mentions, this can let percentages
14512795Swpaul *          total over 100% (I've seen 137.9% for 3 processes).
14612795Swpaul *
14712795Swpaul * Note that schedclock() updates p_estcpu and p_cpticks asynchronously.
14812795Swpaul *
14917142Sjkh * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
150152398Sdwmalone * That is, the system wants to compute a value of decay such
15112795Swpaul * that the following for loop:
15212795Swpaul * 	for (i = 0; i < (5 * loadavg); i++)
15312795Swpaul * 		p_estcpu *= decay;
15412795Swpaul * will compute
15512795Swpaul * 	p_estcpu *= 0.1;
156141614Salfred * for all values of loadavg:
15712795Swpaul *
15812795Swpaul * Mathematically this loop can be expressed by saying:
15912795Swpaul * 	decay ** (5 * loadavg) ~= .1
16012795Swpaul *
16112795Swpaul * The system computes decay as:
16212795Swpaul * 	decay = (2 * loadavg) / (2 * loadavg + 1)
16312795Swpaul *
16412795Swpaul * We wish to prove that the system's computation of decay
16512795Swpaul * will always fulfill the equation:
166141614Salfred * 	decay ** (5 * loadavg) ~= .1
16712795Swpaul *
16812795Swpaul * If we compute b as:
16912795Swpaul * 	b = 2 * loadavg
17012795Swpaul * then
17112795Swpaul * 	decay = b / (b + 1)
17212795Swpaul *
173 * We now need to prove two things:
174 *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
175 *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
176 *
177 * Facts:
178 *         For x close to zero, exp(x) =~ 1 + x, since
179 *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
180 *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
181 *         For x close to zero, ln(1+x) =~ x, since
182 *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
183 *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
184 *         ln(.1) =~ -2.30
185 *
186 * Proof of (1):
187 *    Solve (factor)**(power) =~ .1 given power (5*loadav):
188 *	solving for factor,
189 *      ln(factor) =~ (-2.30/5*loadav), or
190 *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
191 *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
192 *
193 * Proof of (2):
194 *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
195 *	solving for power,
196 *      power*ln(b/(b+1)) =~ -2.30, or
197 *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
198 *
199 * Actual power values for the implemented algorithm are as follows:
200 *      loadav: 1       2       3       4
201 *      power:  5.68    10.32   14.94   19.55
202 */
203
204/* calculations for digital decay to forget 90% of usage in 5*loadav sec */
205#define	loadfactor(loadav)	(2 * (loadav))
206#define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
207
208/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
209static fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;	/* exp(-1/20) */
210SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
211
212/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
213static int	fscale __unused = FSCALE;
214SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
215
216/*
217 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
218 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
219 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
220 *
221 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
222 *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
223 *
224 * If you don't want to bother with the faster/more-accurate formula, you
225 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
226 * (more general) method of calculating the %age of CPU used by a process.
227 */
228#define	CCPU_SHIFT	11
229
230/*
231 * Recompute process priorities, every hz ticks.
232 * MP-safe, called without the Giant mutex.
233 */
234/* ARGSUSED */
235static void
236schedcpu(arg)
237	void *arg;
238{
239	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
240	register struct proc *p;
241	register int realstathz, s;
242
243	realstathz = stathz ? stathz : hz;
244	sx_slock(&allproc_lock);
245	LIST_FOREACH(p, &allproc, p_list) {
246		/*
247		 * Increment time in/out of memory and sleep time
248		 * (if sleeping).  We ignore overflow; with 16-bit int's
249		 * (remember them?) overflow takes 45 days.
250		if (p->p_stat == SWAIT)
251			continue;
252		 */
253		mtx_lock_spin(&sched_lock);
254		p->p_swtime++;
255		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
256			p->p_slptime++;
257		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
258		/*
259		 * If the process has slept the entire second,
260		 * stop recalculating its priority until it wakes up.
261		 */
262		if (p->p_slptime > 1) {
263			mtx_unlock_spin(&sched_lock);
264			continue;
265		}
266
267		/*
268		 * prevent state changes and protect run queue
269		 */
270		s = splhigh();
271
272		/*
273		 * p_pctcpu is only for ps.
274		 */
275#if	(FSHIFT >= CCPU_SHIFT)
276		p->p_pctcpu += (realstathz == 100)?
277			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
278                	100 * (((fixpt_t) p->p_cpticks)
279				<< (FSHIFT - CCPU_SHIFT)) / realstathz;
280#else
281		p->p_pctcpu += ((FSCALE - ccpu) *
282			(p->p_cpticks * FSCALE / realstathz)) >> FSHIFT;
283#endif
284		p->p_cpticks = 0;
285		p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
286		resetpriority(p);
287		if (p->p_pri.pri_level >= PUSER) {
288			if ((p != curproc) &&
289#ifdef SMP
290			    p->p_oncpu == NOCPU && 	/* idle */
291#endif
292			    p->p_stat == SRUN &&
293			    (p->p_sflag & PS_INMEM) &&
294			    (p->p_pri.pri_level / RQ_PPQ) !=
295			    (p->p_pri.pri_user / RQ_PPQ)) {
296				remrunqueue(p);
297				p->p_pri.pri_level = p->p_pri.pri_user;
298				setrunqueue(p);
299			} else
300				p->p_pri.pri_level = p->p_pri.pri_user;
301		}
302		mtx_unlock_spin(&sched_lock);
303		splx(s);
304	}
305	sx_sunlock(&allproc_lock);
306	vmmeter();
307	wakeup((caddr_t)&lbolt);
308	callout_reset(&schedcpu_callout, hz, schedcpu, NULL);
309}
310
311/*
312 * Recalculate the priority of a process after it has slept for a while.
313 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
314 * least six times the loadfactor will decay p_estcpu to zero.
315 */
316void
317updatepri(p)
318	register struct proc *p;
319{
320	register unsigned int newcpu = p->p_estcpu;
321	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
322
323	if (p->p_slptime > 5 * loadfac)
324		p->p_estcpu = 0;
325	else {
326		p->p_slptime--;	/* the first time was done in schedcpu */
327		while (newcpu && --p->p_slptime)
328			newcpu = decay_cpu(loadfac, newcpu);
329		p->p_estcpu = newcpu;
330	}
331	resetpriority(p);
332}
333
334/*
335 * We're only looking at 7 bits of the address; everything is
336 * aligned to 4, lots of things are aligned to greater powers
337 * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
338 */
339#define TABLESIZE	128
340static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE];
341#define LOOKUP(x)	(((intptr_t)(x) >> 8) & (TABLESIZE - 1))
342
343void
344sleepinit(void)
345{
346	int i;
347
348	sched_quantum = hz/10;
349	hogticks = 2 * sched_quantum;
350	for (i = 0; i < TABLESIZE; i++)
351		TAILQ_INIT(&slpque[i]);
352}
353
354/*
355 * General sleep call.  Suspends the current process until a wakeup is
356 * performed on the specified identifier.  The process will then be made
357 * runnable with the specified priority.  Sleeps at most timo/hz seconds
358 * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
359 * before and after sleeping, else signals are not checked.  Returns 0 if
360 * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
361 * signal needs to be delivered, ERESTART is returned if the current system
362 * call should be restarted if possible, and EINTR is returned if the system
363 * call should be interrupted by the signal (return EINTR).
364 *
365 * The mutex argument is exited before the caller is suspended, and
366 * entered before msleep returns.  If priority includes the PDROP
367 * flag the mutex is not entered before returning.
368 */
369int
370msleep(ident, mtx, priority, wmesg, timo)
371	void *ident;
372	struct mtx *mtx;
373	int priority, timo;
374	const char *wmesg;
375{
376	struct proc *p = curproc;
377	int sig, catch = priority & PCATCH;
378	int rval = 0;
379	WITNESS_SAVE_DECL(mtx);
380
381#ifdef KTRACE
382	if (p && KTRPOINT(p, KTR_CSW))
383		ktrcsw(p->p_tracep, 1, 0);
384#endif
385	WITNESS_SLEEP(0, &mtx->mtx_object);
386	KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL,
387	    ("sleeping without a mutex"));
388	mtx_lock_spin(&sched_lock);
389	if (cold || panicstr) {
390		/*
391		 * After a panic, or during autoconfiguration,
392		 * just give interrupts a chance, then just return;
393		 * don't run any other procs or panic below,
394		 * in case this is the idle process and already asleep.
395		 */
396		if (mtx != NULL && priority & PDROP)
397			mtx_unlock_flags(mtx, MTX_NOSWITCH);
398		mtx_unlock_spin(&sched_lock);
399		return (0);
400	}
401
402	DROP_GIANT_NOSWITCH();
403
404	if (mtx != NULL) {
405		mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
406		WITNESS_SAVE(&mtx->mtx_object, mtx);
407		mtx_unlock_flags(mtx, MTX_NOSWITCH);
408		if (priority & PDROP)
409			mtx = NULL;
410	}
411
412	KASSERT(p != NULL, ("msleep1"));
413	KASSERT(ident != NULL && p->p_stat == SRUN, ("msleep"));
414	/*
415	 * Process may be sitting on a slpque if asleep() was called, remove
416	 * it before re-adding.
417	 */
418	if (p->p_wchan != NULL)
419		unsleep(p);
420
421	p->p_wchan = ident;
422	p->p_wmesg = wmesg;
423	p->p_slptime = 0;
424	p->p_pri.pri_level = priority & PRIMASK;
425	CTR3(KTR_PROC, "msleep: proc %p (pid %d, %s)", p, p->p_pid, p->p_comm);
426	TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_slpq);
427	if (timo)
428		callout_reset(&p->p_slpcallout, timo, endtsleep, p);
429	/*
430	 * We put ourselves on the sleep queue and start our timeout
431	 * before calling CURSIG, as we could stop there, and a wakeup
432	 * or a SIGCONT (or both) could occur while we were stopped.
433	 * A SIGCONT would cause us to be marked as SSLEEP
434	 * without resuming us, thus we must be ready for sleep
435	 * when CURSIG is called.  If the wakeup happens while we're
436	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
437	 */
438	if (catch) {
439		CTR3(KTR_PROC, "msleep caught: proc %p (pid %d, %s)", p,
440		    p->p_pid, p->p_comm);
441		p->p_sflag |= PS_SINTR;
442		mtx_unlock_spin(&sched_lock);
443		PROC_LOCK(p);
444		sig = CURSIG(p);
445		mtx_lock_spin(&sched_lock);
446		PROC_UNLOCK_NOSWITCH(p);
447		if (sig != 0) {
448			if (p->p_wchan)
449				unsleep(p);
450		} else if (p->p_wchan == NULL)
451			catch = 0;
452	} else
453		sig = 0;
454	if (p->p_wchan != NULL) {
455		p->p_stat = SSLEEP;
456		p->p_stats->p_ru.ru_nvcsw++;
457		mi_switch();
458	}
459	CTR3(KTR_PROC, "msleep resume: proc %p (pid %d, %s)", p, p->p_pid,
460	    p->p_comm);
461	KASSERT(p->p_stat == SRUN, ("running but not SRUN"));
462	p->p_sflag &= ~PS_SINTR;
463	if (p->p_sflag & PS_TIMEOUT) {
464		p->p_sflag &= ~PS_TIMEOUT;
465		if (sig == 0)
466			rval = EWOULDBLOCK;
467	} else if (timo)
468		callout_stop(&p->p_slpcallout);
469	mtx_unlock_spin(&sched_lock);
470
471	if (rval == 0 && catch) {
472		PROC_LOCK(p);
473		/* XXX: shouldn't we always be calling CURSIG() */
474		if (sig != 0 || (sig = CURSIG(p))) {
475			if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
476				rval = EINTR;
477			else
478				rval = ERESTART;
479		}
480		PROC_UNLOCK(p);
481	}
482	PICKUP_GIANT();
483#ifdef KTRACE
484	mtx_lock(&Giant);
485	if (KTRPOINT(p, KTR_CSW))
486		ktrcsw(p->p_tracep, 0, 0);
487	mtx_unlock(&Giant);
488#endif
489	if (mtx != NULL) {
490		mtx_lock(mtx);
491		WITNESS_RESTORE(&mtx->mtx_object, mtx);
492	}
493	return (rval);
494}
495
496/*
497 * asleep() - async sleep call.  Place process on wait queue and return
498 * immediately without blocking.  The process stays runnable until mawait()
499 * is called.  If ident is NULL, remove process from wait queue if it is still
500 * on one.
501 *
502 * Only the most recent sleep condition is effective when making successive
503 * calls to asleep() or when calling msleep().
504 *
505 * The timeout, if any, is not initiated until mawait() is called.  The sleep
506 * priority, signal, and timeout is specified in the asleep() call but may be
507 * overriden in the mawait() call.
508 *
509 * <<<<<<<< EXPERIMENTAL, UNTESTED >>>>>>>>>>
510 */
511
512int
513asleep(void *ident, int priority, const char *wmesg, int timo)
514{
515	struct proc *p = curproc;
516
517	/*
518	 * Remove preexisting wait condition (if any) and place process
519	 * on appropriate slpque, but do not put process to sleep.
520	 */
521
522	mtx_lock_spin(&sched_lock);
523
524	if (p->p_wchan != NULL)
525		unsleep(p);
526
527	if (ident) {
528		p->p_wchan = ident;
529		p->p_wmesg = wmesg;
530		p->p_slptime = 0;
531		p->p_asleep.as_priority = priority;
532		p->p_asleep.as_timo = timo;
533		TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_slpq);
534	}
535
536	mtx_unlock_spin(&sched_lock);
537
538	return(0);
539}
540
541/*
542 * mawait() - wait for async condition to occur.   The process blocks until
543 * wakeup() is called on the most recent asleep() address.  If wakeup is called
544 * prior to mawait(), mawait() winds up being a NOP.
545 *
546 * If mawait() is called more then once (without an intervening asleep() call),
547 * mawait() is still effectively a NOP but it calls mi_switch() to give other
548 * processes some cpu before returning.  The process is left runnable.
549 *
550 * <<<<<<<< EXPERIMENTAL, UNTESTED >>>>>>>>>>
551 */
552
553int
554mawait(struct mtx *mtx, int priority, int timo)
555{
556	struct proc *p = curproc;
557	int rval = 0;
558	WITNESS_SAVE_DECL(mtx);
559
560	WITNESS_SLEEP(0, &mtx->mtx_object);
561	KASSERT(timo > 0 || mtx_owned(&Giant) || mtx != NULL,
562	    ("sleeping without a mutex"));
563	mtx_lock_spin(&sched_lock);
564	DROP_GIANT_NOSWITCH();
565	if (mtx != NULL) {
566		mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
567		WITNESS_SAVE(&mtx->mtx_object, mtx);
568		mtx_unlock_flags(mtx, MTX_NOSWITCH);
569		if (priority & PDROP)
570			mtx = NULL;
571	}
572
573	if (p->p_wchan != NULL) {
574		int sig;
575		int catch;
576
577#ifdef KTRACE
578		if (p && KTRPOINT(p, KTR_CSW))
579			ktrcsw(p->p_tracep, 1, 0);
580#endif
581		/*
582		 * The call to mawait() can override defaults specified in
583		 * the original asleep().
584		 */
585		if (priority < 0)
586			priority = p->p_asleep.as_priority;
587		if (timo < 0)
588			timo = p->p_asleep.as_timo;
589
590		/*
591		 * Install timeout
592		 */
593
594		if (timo)
595			callout_reset(&p->p_slpcallout, timo, endtsleep, p);
596
597		sig = 0;
598		catch = priority & PCATCH;
599
600		if (catch) {
601			p->p_sflag |= PS_SINTR;
602			mtx_unlock_spin(&sched_lock);
603			PROC_LOCK(p);
604			sig = CURSIG(p);
605			mtx_lock_spin(&sched_lock);
606			PROC_UNLOCK_NOSWITCH(p);
607			if (sig != 0) {
608				if (p->p_wchan)
609					unsleep(p);
610			} else if (p->p_wchan == NULL)
611				catch = 0;
612		}
613		if (p->p_wchan != NULL) {
614			p->p_stat = SSLEEP;
615			p->p_stats->p_ru.ru_nvcsw++;
616			mi_switch();
617		}
618		KASSERT(p->p_stat == SRUN, ("running but not SRUN"));
619		p->p_sflag &= ~PS_SINTR;
620		if (p->p_sflag & PS_TIMEOUT) {
621			p->p_sflag &= ~PS_TIMEOUT;
622			if (sig == 0)
623				rval = EWOULDBLOCK;
624		} else if (timo)
625			callout_stop(&p->p_slpcallout);
626		mtx_unlock_spin(&sched_lock);
627		if (rval == 0 && catch) {
628			PROC_LOCK(p);
629			if (sig != 0 || (sig = CURSIG(p))) {
630				if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
631					rval = EINTR;
632				else
633					rval = ERESTART;
634			}
635			PROC_UNLOCK(p);
636		}
637#ifdef KTRACE
638		mtx_lock(&Giant);
639		if (KTRPOINT(p, KTR_CSW))
640			ktrcsw(p->p_tracep, 0, 0);
641		mtx_unlock(&Giant);
642#endif
643	} else {
644		/*
645		 * If as_priority is 0, mawait() has been called without an
646		 * intervening asleep().  We are still effectively a NOP,
647		 * but we call mi_switch() for safety.
648		 */
649
650		if (p->p_asleep.as_priority == 0) {
651			p->p_stats->p_ru.ru_nvcsw++;
652			mi_switch();
653		}
654		mtx_unlock_spin(&sched_lock);
655	}
656
657	/*
658	 * clear p_asleep.as_priority as an indication that mawait() has been
659	 * called.  If mawait() is called again without an intervening asleep(),
660	 * mawait() is still effectively a NOP but the above mi_switch() code
661	 * is triggered as a safety.
662	 */
663	if (rval == 0)
664		p->p_asleep.as_priority = 0;
665
666	PICKUP_GIANT();
667	if (mtx != NULL) {
668		mtx_lock(mtx);
669		WITNESS_RESTORE(&mtx->mtx_object, mtx);
670	}
671	return (rval);
672}
673
674/*
675 * Implement timeout for msleep or asleep()/mawait()
676 *
677 * If process hasn't been awakened (wchan non-zero),
678 * set timeout flag and undo the sleep.  If proc
679 * is stopped, just unsleep so it will remain stopped.
680 * MP-safe, called without the Giant mutex.
681 */
682static void
683endtsleep(arg)
684	void *arg;
685{
686	register struct proc *p;
687	int s;
688
689	p = (struct proc *)arg;
690	CTR3(KTR_PROC, "endtsleep: proc %p (pid %d, %s)", p, p->p_pid,
691	    p->p_comm);
692	s = splhigh();
693	mtx_lock_spin(&sched_lock);
694	if (p->p_wchan) {
695		if (p->p_stat == SSLEEP)
696			setrunnable(p);
697		else
698			unsleep(p);
699		p->p_sflag |= PS_TIMEOUT;
700	}
701	mtx_unlock_spin(&sched_lock);
702	splx(s);
703}
704
705/*
706 * Remove a process from its wait queue
707 */
708void
709unsleep(p)
710	register struct proc *p;
711{
712	int s;
713
714	s = splhigh();
715	mtx_lock_spin(&sched_lock);
716	if (p->p_wchan) {
717		TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_slpq);
718		p->p_wchan = NULL;
719	}
720	mtx_unlock_spin(&sched_lock);
721	splx(s);
722}
723
724/*
725 * Make all processes sleeping on the specified identifier runnable.
726 */
727void
728wakeup(ident)
729	register void *ident;
730{
731	register struct slpquehead *qp;
732	register struct proc *p;
733	int s;
734
735	s = splhigh();
736	mtx_lock_spin(&sched_lock);
737	qp = &slpque[LOOKUP(ident)];
738restart:
739	TAILQ_FOREACH(p, qp, p_slpq) {
740		if (p->p_wchan == ident) {
741			TAILQ_REMOVE(qp, p, p_slpq);
742			p->p_wchan = NULL;
743			if (p->p_stat == SSLEEP) {
744				/* OPTIMIZED EXPANSION OF setrunnable(p); */
745				CTR3(KTR_PROC, "wakeup: proc %p (pid %d, %s)",
746				    p, p->p_pid, p->p_comm);
747				if (p->p_slptime > 1)
748					updatepri(p);
749				p->p_slptime = 0;
750				p->p_stat = SRUN;
751				if (p->p_sflag & PS_INMEM) {
752					setrunqueue(p);
753					maybe_resched(p);
754				} else {
755					p->p_sflag |= PS_SWAPINREQ;
756					wakeup((caddr_t)&proc0);
757				}
758				/* END INLINE EXPANSION */
759				goto restart;
760			}
761		}
762	}
763	mtx_unlock_spin(&sched_lock);
764	splx(s);
765}
766
767/*
768 * Make a process sleeping on the specified identifier runnable.
769 * May wake more than one process if a target process is currently
770 * swapped out.
771 */
772void
773wakeup_one(ident)
774	register void *ident;
775{
776	register struct slpquehead *qp;
777	register struct proc *p;
778	int s;
779
780	s = splhigh();
781	mtx_lock_spin(&sched_lock);
782	qp = &slpque[LOOKUP(ident)];
783
784	TAILQ_FOREACH(p, qp, p_slpq) {
785		if (p->p_wchan == ident) {
786			TAILQ_REMOVE(qp, p, p_slpq);
787			p->p_wchan = NULL;
788			if (p->p_stat == SSLEEP) {
789				/* OPTIMIZED EXPANSION OF setrunnable(p); */
790				CTR3(KTR_PROC, "wakeup1: proc %p (pid %d, %s)",
791				    p, p->p_pid, p->p_comm);
792				if (p->p_slptime > 1)
793					updatepri(p);
794				p->p_slptime = 0;
795				p->p_stat = SRUN;
796				if (p->p_sflag & PS_INMEM) {
797					setrunqueue(p);
798					maybe_resched(p);
799					break;
800				} else {
801					p->p_sflag |= PS_SWAPINREQ;
802					wakeup((caddr_t)&proc0);
803				}
804				/* END INLINE EXPANSION */
805			}
806		}
807	}
808	mtx_unlock_spin(&sched_lock);
809	splx(s);
810}
811
812/*
813 * The machine independent parts of mi_switch().
814 * Must be called at splstatclock() or higher.
815 */
816void
817mi_switch()
818{
819	struct timeval new_switchtime;
820	register struct proc *p = curproc;	/* XXX */
821#if 0
822	register struct rlimit *rlim;
823#endif
824	u_int sched_nest;
825
826	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
827
828	/*
829	 * Compute the amount of time during which the current
830	 * process was running, and add that to its total so far.
831	 */
832	microuptime(&new_switchtime);
833	if (timevalcmp(&new_switchtime, PCPU_PTR(switchtime), <)) {
834#if 0
835		/* XXX: This doesn't play well with sched_lock right now. */
836		printf("microuptime() went backwards (%ld.%06ld -> %ld.%06ld)\n",
837		    PCPU_GET(switchtime.tv_sec), PCPU_GET(switchtime.tv_usec),
838		    new_switchtime.tv_sec, new_switchtime.tv_usec);
839#endif
840		new_switchtime = PCPU_GET(switchtime);
841	} else {
842		p->p_runtime += (new_switchtime.tv_usec - PCPU_GET(switchtime.tv_usec)) +
843		    (new_switchtime.tv_sec - PCPU_GET(switchtime.tv_sec)) *
844		    (int64_t)1000000;
845	}
846
847#if 0
848	/*
849	 * Check if the process exceeds its cpu resource allocation.
850	 * If over max, kill it.
851	 *
852	 * XXX drop sched_lock, pickup Giant
853	 */
854	if (p->p_stat != SZOMB && p->p_limit->p_cpulimit != RLIM_INFINITY &&
855	    p->p_runtime > p->p_limit->p_cpulimit) {
856		rlim = &p->p_rlimit[RLIMIT_CPU];
857		if (p->p_runtime / (rlim_t)1000000 >= rlim->rlim_max) {
858			mtx_unlock_spin(&sched_lock);
859			PROC_LOCK(p);
860			killproc(p, "exceeded maximum CPU limit");
861			mtx_lock_spin(&sched_lock);
862			PROC_UNLOCK_NOSWITCH(p);
863		} else {
864			mtx_unlock_spin(&sched_lock);
865			PROC_LOCK(p);
866			psignal(p, SIGXCPU);
867			mtx_lock_spin(&sched_lock);
868			PROC_UNLOCK_NOSWITCH(p);
869			if (rlim->rlim_cur < rlim->rlim_max) {
870				/* XXX: we should make a private copy */
871				rlim->rlim_cur += 5;
872			}
873		}
874	}
875#endif
876
877	/*
878	 * Pick a new current process and record its start time.
879	 */
880	cnt.v_swtch++;
881	PCPU_SET(switchtime, new_switchtime);
882	CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d, %s)", p, p->p_pid,
883	    p->p_comm);
884	sched_nest = sched_lock.mtx_recurse;
885	curproc->p_lastcpu = curproc->p_oncpu;
886	curproc->p_oncpu = NOCPU;
887	clear_resched(curproc);
888	cpu_switch();
889	curproc->p_oncpu = PCPU_GET(cpuid);
890	sched_lock.mtx_recurse = sched_nest;
891	sched_lock.mtx_lock = (uintptr_t)curproc;
892	CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d, %s)", p, p->p_pid,
893	    p->p_comm);
894	if (PCPU_GET(switchtime.tv_sec) == 0)
895		microuptime(PCPU_PTR(switchtime));
896	PCPU_SET(switchticks, ticks);
897}
898
899/*
900 * Change process state to be runnable,
901 * placing it on the run queue if it is in memory,
902 * and awakening the swapper if it isn't in memory.
903 */
904void
905setrunnable(p)
906	register struct proc *p;
907{
908	register int s;
909
910	s = splhigh();
911	mtx_lock_spin(&sched_lock);
912	switch (p->p_stat) {
913	case 0:
914	case SRUN:
915	case SZOMB:
916	case SWAIT:
917	default:
918		panic("setrunnable");
919	case SSTOP:
920	case SSLEEP:			/* e.g. when sending signals */
921		if (p->p_sflag & PS_CVWAITQ)
922			cv_waitq_remove(p);
923		else
924			unsleep(p);
925		break;
926
927	case SIDL:
928		break;
929	}
930	p->p_stat = SRUN;
931	if (p->p_sflag & PS_INMEM)
932		setrunqueue(p);
933	splx(s);
934	if (p->p_slptime > 1)
935		updatepri(p);
936	p->p_slptime = 0;
937	if ((p->p_sflag & PS_INMEM) == 0) {
938		p->p_sflag |= PS_SWAPINREQ;
939		wakeup((caddr_t)&proc0);
940	}
941	else
942		maybe_resched(p);
943	mtx_unlock_spin(&sched_lock);
944}
945
946/*
947 * Compute the priority of a process when running in user mode.
948 * Arrange to reschedule if the resulting priority is better
949 * than that of the current process.
950 */
951void
952resetpriority(p)
953	register struct proc *p;
954{
955	register unsigned int newpriority;
956
957	mtx_lock_spin(&sched_lock);
958	if (p->p_pri.pri_class == PRI_TIMESHARE) {
959		newpriority = PUSER + p->p_estcpu / INVERSE_ESTCPU_WEIGHT +
960		    NICE_WEIGHT * (p->p_nice - PRIO_MIN);
961		newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
962		    PRI_MAX_TIMESHARE);
963		p->p_pri.pri_user = newpriority;
964	}
965	maybe_resched(p);
966	mtx_unlock_spin(&sched_lock);
967}
968
969/* ARGSUSED */
970static void
971sched_setup(dummy)
972	void *dummy;
973{
974
975	callout_init(&schedcpu_callout, 1);
976	callout_init(&roundrobin_callout, 0);
977
978	/* Kick off timeout driven events by calling first time. */
979	roundrobin(NULL);
980	schedcpu(NULL);
981}
982
983/*
984 * We adjust the priority of the current process.  The priority of
985 * a process gets worse as it accumulates CPU time.  The cpu usage
986 * estimator (p_estcpu) is increased here.  resetpriority() will
987 * compute a different priority each time p_estcpu increases by
988 * INVERSE_ESTCPU_WEIGHT
989 * (until MAXPRI is reached).  The cpu usage estimator ramps up
990 * quite quickly when the process is running (linearly), and decays
991 * away exponentially, at a rate which is proportionally slower when
992 * the system is busy.  The basic principle is that the system will
993 * 90% forget that the process used a lot of CPU time in 5 * loadav
994 * seconds.  This causes the system to favor processes which haven't
995 * run much recently, and to round-robin among other processes.
996 */
997void
998schedclock(p)
999	struct proc *p;
1000{
1001
1002	p->p_cpticks++;
1003	p->p_estcpu = ESTCPULIM(p->p_estcpu + 1);
1004	if ((p->p_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
1005		resetpriority(p);
1006		if (p->p_pri.pri_level >= PUSER)
1007			p->p_pri.pri_level = p->p_pri.pri_user;
1008	}
1009}
1010
1011/*
1012 * General purpose yield system call
1013 */
1014int
1015yield(struct proc *p, struct yield_args *uap)
1016{
1017	int s;
1018
1019	p->p_retval[0] = 0;
1020
1021	s = splhigh();
1022	mtx_lock_spin(&sched_lock);
1023	DROP_GIANT_NOSWITCH();
1024	p->p_pri.pri_level = PRI_MAX_TIMESHARE;
1025	setrunqueue(p);
1026	p->p_stats->p_ru.ru_nvcsw++;
1027	mi_switch();
1028	mtx_unlock_spin(&sched_lock);
1029	PICKUP_GIANT();
1030	splx(s);
1031
1032	return (0);
1033}
1034