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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/condvar.h>
47 #include <sys/kernel.h>
50 #include <sys/mutex.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
58 #include <sys/vmmeter.h>
60 #include <vm/vm_extern.h>
63 #include <sys/ktrace.h>
66 #include <machine/cpu.h>
68 static void sched_setup __P((void *dummy));
69 SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
73 int sched_quantum; /* Roundrobin scheduling quantum in ticks. */
75 static struct callout schedcpu_callout;
76 static struct callout roundrobin_callout;
78 static void endtsleep __P((void *));
79 static void roundrobin __P((void *arg));
80 static void schedcpu __P((void *arg));
83 sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
87 new_val = sched_quantum * tick;
88 error = sysctl_handle_int(oidp, &new_val, 0, req);
89 if (error != 0 || req->newptr == NULL)
93 sched_quantum = new_val / tick;
94 hogticks = 2 * sched_quantum;
98 SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
99 0, sizeof sched_quantum, sysctl_kern_quantum, "I", "");
102 * Arrange to reschedule if necessary, taking the priorities and
103 * schedulers into account.
110 mtx_assert(&sched_lock, MA_OWNED);
111 if (p->p_pri.pri_level < curproc->p_pri.pri_level)
112 need_resched(curproc);
116 roundrobin_interval(void)
118 return (sched_quantum);
122 * Force switch among equal priority processes every 100ms.
130 mtx_lock_spin(&sched_lock);
131 need_resched(curproc);
133 forward_roundrobin();
135 mtx_unlock_spin(&sched_lock);
137 callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL);
141 * Constants for digital decay and forget:
142 * 90% of (p_estcpu) usage in 5 * loadav time
143 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
144 * Note that, as ps(1) mentions, this can let percentages
145 * total over 100% (I've seen 137.9% for 3 processes).
147 * Note that schedclock() updates p_estcpu and p_cpticks asynchronously.
149 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
150 * That is, the system wants to compute a value of decay such
151 * that the following for loop:
152 * for (i = 0; i < (5 * loadavg); i++)
156 * for all values of loadavg:
158 * Mathematically this loop can be expressed by saying:
159 * decay ** (5 * loadavg) ~= .1
161 * The system computes decay as:
162 * decay = (2 * loadavg) / (2 * loadavg + 1)
164 * We wish to prove that the system's computation of decay
165 * will always fulfill the equation:
166 * decay ** (5 * loadavg) ~= .1
168 * If we compute b as:
171 * decay = b / (b + 1)
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)
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).
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
194 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
196 * power*ln(b/(b+1)) =~ -2.30, or
197 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
199 * Actual power values for the implemented algorithm are as follows:
201 * power: 5.68 10.32 14.94 19.55
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))
208 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
209 static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
210 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
212 /* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
213 static int fscale __unused = FSCALE;
214 SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
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).
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).
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.
228 #define CCPU_SHIFT 11
231 * Recompute process priorities, every hz ticks.
232 * MP-safe, called without the Giant mutex.
239 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
240 register struct proc *p;
241 register int realstathz, s;
243 realstathz = stathz ? stathz : hz;
244 sx_slock(&allproc_lock);
245 LIST_FOREACH(p, &allproc, p_list) {
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)
253 mtx_lock_spin(&sched_lock);
255 if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
257 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
259 * If the process has slept the entire second,
260 * stop recalculating its priority until it wakes up.
262 if (p->p_slptime > 1) {
263 mtx_unlock_spin(&sched_lock);
268 * prevent state changes and protect run queue
273 * p_pctcpu is only for ps.
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;
281 p->p_pctcpu += ((FSCALE - ccpu) *
282 (p->p_cpticks * FSCALE / realstathz)) >> FSHIFT;
285 p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
287 if (p->p_pri.pri_level >= PUSER) {
288 if ((p != curproc) &&
290 p->p_oncpu == NOCPU && /* idle */
293 (p->p_sflag & PS_INMEM) &&
294 (p->p_pri.pri_level / RQ_PPQ) !=
295 (p->p_pri.pri_user / RQ_PPQ)) {
297 p->p_pri.pri_level = p->p_pri.pri_user;
300 p->p_pri.pri_level = p->p_pri.pri_user;
302 mtx_unlock_spin(&sched_lock);
305 sx_sunlock(&allproc_lock);
307 wakeup((caddr_t)&lbolt);
308 callout_reset(&schedcpu_callout, hz, schedcpu, NULL);
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.
318 register struct proc *p;
320 register unsigned int newcpu = p->p_estcpu;
321 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
323 if (p->p_slptime > 5 * loadfac)
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;
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.
339 #define TABLESIZE 128
340 static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE];
341 #define LOOKUP(x) (((intptr_t)(x) >> 8) & (TABLESIZE - 1))
348 sched_quantum = hz/10;
349 hogticks = 2 * sched_quantum;
350 for (i = 0; i < TABLESIZE; i++)
351 TAILQ_INIT(&slpque[i]);
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).
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.
370 msleep(ident, mtx, priority, wmesg, timo)
376 struct proc *p = curproc;
377 int sig, catch = priority & PCATCH;
379 WITNESS_SAVE_DECL(mtx);
382 if (p && KTRPOINT(p, KTR_CSW))
383 ktrcsw(p->p_tracep, 1, 0);
385 WITNESS_SLEEP(0, &mtx->mtx_object);
386 mtx_lock_spin(&sched_lock);
387 if (cold || panicstr) {
389 * After a panic, or during autoconfiguration,
390 * just give interrupts a chance, then just return;
391 * don't run any other procs or panic below,
392 * in case this is the idle process and already asleep.
394 if (mtx != NULL && priority & PDROP)
395 mtx_unlock_flags(mtx, MTX_NOSWITCH);
396 mtx_unlock_spin(&sched_lock);
400 DROP_GIANT_NOSWITCH();
403 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
404 WITNESS_SAVE(&mtx->mtx_object, mtx);
405 mtx_unlock_flags(mtx, MTX_NOSWITCH);
406 if (priority & PDROP)
410 KASSERT(p != NULL, ("msleep1"));
411 KASSERT(ident != NULL && p->p_stat == SRUN, ("msleep"));
413 * Process may be sitting on a slpque if asleep() was called, remove
414 * it before re-adding.
416 if (p->p_wchan != NULL)
422 p->p_pri.pri_level = priority & PRIMASK;
423 CTR4(KTR_PROC, "msleep: proc %p (pid %d, %s), schedlock %p",
424 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
425 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_slpq);
427 callout_reset(&p->p_slpcallout, timo, endtsleep, p);
429 * We put ourselves on the sleep queue and start our timeout
430 * before calling CURSIG, as we could stop there, and a wakeup
431 * or a SIGCONT (or both) could occur while we were stopped.
432 * A SIGCONT would cause us to be marked as SSLEEP
433 * without resuming us, thus we must be ready for sleep
434 * when CURSIG is called. If the wakeup happens while we're
435 * stopped, p->p_wchan will be 0 upon return from CURSIG.
439 "msleep caught: proc %p (pid %d, %s), schedlock %p",
440 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
441 p->p_sflag |= PS_SINTR;
442 mtx_unlock_spin(&sched_lock);
443 if ((sig = CURSIG(p))) {
444 mtx_lock_spin(&sched_lock);
450 mtx_lock_spin(&sched_lock);
451 if (p->p_wchan == NULL) {
458 p->p_stats->p_ru.ru_nvcsw++;
461 "msleep resume: proc %p (pid %d, %s), schedlock %p",
462 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
464 p->p_sflag &= ~PS_SINTR;
465 if (p->p_sflag & PS_TIMEOUT) {
466 p->p_sflag &= ~PS_TIMEOUT;
469 if (KTRPOINT(p, KTR_CSW))
470 ktrcsw(p->p_tracep, 0, 0);
473 mtx_unlock_spin(&sched_lock);
477 callout_stop(&p->p_slpcallout);
478 mtx_unlock_spin(&sched_lock);
480 if (catch && (sig != 0 || (sig = CURSIG(p)))) {
482 if (KTRPOINT(p, KTR_CSW))
483 ktrcsw(p->p_tracep, 0, 0);
486 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
495 if (KTRPOINT(p, KTR_CSW))
496 ktrcsw(p->p_tracep, 0, 0);
501 WITNESS_RESTORE(&mtx->mtx_object, mtx);
507 * asleep() - async sleep call. Place process on wait queue and return
508 * immediately without blocking. The process stays runnable until mawait()
509 * is called. If ident is NULL, remove process from wait queue if it is still
512 * Only the most recent sleep condition is effective when making successive
513 * calls to asleep() or when calling msleep().
515 * The timeout, if any, is not initiated until mawait() is called. The sleep
516 * priority, signal, and timeout is specified in the asleep() call but may be
517 * overriden in the mawait() call.
519 * <<<<<<<< EXPERIMENTAL, UNTESTED >>>>>>>>>>
523 asleep(void *ident, int priority, const char *wmesg, int timo)
525 struct proc *p = curproc;
529 * obtain sched_lock while manipulating sleep structures and slpque.
531 * Remove preexisting wait condition (if any) and place process
532 * on appropriate slpque, but do not put process to sleep.
536 mtx_lock_spin(&sched_lock);
538 if (p->p_wchan != NULL)
545 p->p_asleep.as_priority = priority;
546 p->p_asleep.as_timo = timo;
547 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_slpq);
550 mtx_unlock_spin(&sched_lock);
557 * mawait() - wait for async condition to occur. The process blocks until
558 * wakeup() is called on the most recent asleep() address. If wakeup is called
559 * prior to mawait(), mawait() winds up being a NOP.
561 * If mawait() is called more then once (without an intervening asleep() call),
562 * mawait() is still effectively a NOP but it calls mi_switch() to give other
563 * processes some cpu before returning. The process is left runnable.
565 * <<<<<<<< EXPERIMENTAL, UNTESTED >>>>>>>>>>
569 mawait(struct mtx *mtx, int priority, int timo)
571 struct proc *p = curproc;
574 WITNESS_SAVE_DECL(mtx);
576 WITNESS_SLEEP(0, &mtx->mtx_object);
577 mtx_lock_spin(&sched_lock);
578 DROP_GIANT_NOSWITCH();
580 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
581 WITNESS_SAVE(&mtx->mtx_object, mtx);
582 mtx_unlock_flags(mtx, MTX_NOSWITCH);
583 if (priority & PDROP)
589 if (p->p_wchan != NULL) {
594 * The call to mawait() can override defaults specified in
595 * the original asleep().
598 priority = p->p_asleep.as_priority;
600 timo = p->p_asleep.as_timo;
607 callout_reset(&p->p_slpcallout, timo, endtsleep, p);
610 catch = priority & PCATCH;
613 p->p_sflag |= PS_SINTR;
614 mtx_unlock_spin(&sched_lock);
615 if ((sig = CURSIG(p))) {
616 mtx_lock_spin(&sched_lock);
622 mtx_lock_spin(&sched_lock);
623 if (p->p_wchan == NULL) {
629 p->p_stats->p_ru.ru_nvcsw++;
634 p->p_sflag &= ~PS_SINTR;
635 if (p->p_sflag & PS_TIMEOUT) {
636 p->p_sflag &= ~PS_TIMEOUT;
639 if (KTRPOINT(p, KTR_CSW))
640 ktrcsw(p->p_tracep, 0, 0);
643 mtx_unlock_spin(&sched_lock);
647 callout_stop(&p->p_slpcallout);
648 mtx_unlock_spin(&sched_lock);
650 if (catch && (sig != 0 || (sig = CURSIG(p)))) {
652 if (KTRPOINT(p, KTR_CSW))
653 ktrcsw(p->p_tracep, 0, 0);
656 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
664 if (KTRPOINT(p, KTR_CSW))
665 ktrcsw(p->p_tracep, 0, 0);
669 * If as_priority is 0, mawait() has been called without an
670 * intervening asleep(). We are still effectively a NOP,
671 * but we call mi_switch() for safety.
674 if (p->p_asleep.as_priority == 0) {
675 p->p_stats->p_ru.ru_nvcsw++;
678 mtx_unlock_spin(&sched_lock);
683 * clear p_asleep.as_priority as an indication that mawait() has been
684 * called. If mawait() is called again without an intervening asleep(),
685 * mawait() is still effectively a NOP but the above mi_switch() code
686 * is triggered as a safety.
688 p->p_asleep.as_priority = 0;
694 WITNESS_RESTORE(&mtx->mtx_object, mtx);
700 * Implement timeout for msleep or asleep()/mawait()
702 * If process hasn't been awakened (wchan non-zero),
703 * set timeout flag and undo the sleep. If proc
704 * is stopped, just unsleep so it will remain stopped.
705 * MP-safe, called without the Giant mutex.
711 register struct proc *p;
714 p = (struct proc *)arg;
716 "endtsleep: proc %p (pid %d, %s), schedlock %p",
717 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
719 mtx_lock_spin(&sched_lock);
721 if (p->p_stat == SSLEEP)
725 p->p_sflag |= PS_TIMEOUT;
727 mtx_unlock_spin(&sched_lock);
732 * Remove a process from its wait queue
736 register struct proc *p;
741 mtx_lock_spin(&sched_lock);
743 TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_slpq);
746 mtx_unlock_spin(&sched_lock);
751 * Make all processes sleeping on the specified identifier runnable.
755 register void *ident;
757 register struct slpquehead *qp;
758 register struct proc *p;
762 mtx_lock_spin(&sched_lock);
763 qp = &slpque[LOOKUP(ident)];
765 TAILQ_FOREACH(p, qp, p_slpq) {
766 if (p->p_wchan == ident) {
767 TAILQ_REMOVE(qp, p, p_slpq);
769 if (p->p_stat == SSLEEP) {
770 /* OPTIMIZED EXPANSION OF setrunnable(p); */
772 "wakeup: proc %p (pid %d, %s), schedlock %p",
773 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
774 if (p->p_slptime > 1)
778 if (p->p_sflag & PS_INMEM) {
782 p->p_sflag |= PS_SWAPINREQ;
783 wakeup((caddr_t)&proc0);
785 /* END INLINE EXPANSION */
790 mtx_unlock_spin(&sched_lock);
795 * Make a process sleeping on the specified identifier runnable.
796 * May wake more than one process if a target process is currently
801 register void *ident;
803 register struct slpquehead *qp;
804 register struct proc *p;
808 mtx_lock_spin(&sched_lock);
809 qp = &slpque[LOOKUP(ident)];
811 TAILQ_FOREACH(p, qp, p_slpq) {
812 if (p->p_wchan == ident) {
813 TAILQ_REMOVE(qp, p, p_slpq);
815 if (p->p_stat == SSLEEP) {
816 /* OPTIMIZED EXPANSION OF setrunnable(p); */
818 "wakeup1: proc %p (pid %d, %s), schedlock %p",
819 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
820 if (p->p_slptime > 1)
824 if (p->p_sflag & PS_INMEM) {
829 p->p_sflag |= PS_SWAPINREQ;
830 wakeup((caddr_t)&proc0);
832 /* END INLINE EXPANSION */
836 mtx_unlock_spin(&sched_lock);
841 * The machine independent parts of mi_switch().
842 * Must be called at splstatclock() or higher.
847 struct timeval new_switchtime;
848 register struct proc *p = curproc; /* XXX */
850 register struct rlimit *rlim;
854 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
857 * Compute the amount of time during which the current
858 * process was running, and add that to its total so far.
860 microuptime(&new_switchtime);
861 if (timevalcmp(&new_switchtime, PCPU_PTR(switchtime), <)) {
863 /* XXX: This doesn't play well with sched_lock right now. */
864 printf("microuptime() went backwards (%ld.%06ld -> %ld.%06ld)\n",
865 PCPU_GET(switchtime.tv_sec), PCPU_GET(switchtime.tv_usec),
866 new_switchtime.tv_sec, new_switchtime.tv_usec);
868 new_switchtime = PCPU_GET(switchtime);
870 p->p_runtime += (new_switchtime.tv_usec - PCPU_GET(switchtime.tv_usec)) +
871 (new_switchtime.tv_sec - PCPU_GET(switchtime.tv_sec)) *
877 * Check if the process exceeds its cpu resource allocation.
878 * If over max, kill it.
880 * XXX drop sched_lock, pickup Giant
882 if (p->p_stat != SZOMB && p->p_limit->p_cpulimit != RLIM_INFINITY &&
883 p->p_runtime > p->p_limit->p_cpulimit) {
884 rlim = &p->p_rlimit[RLIMIT_CPU];
885 if (p->p_runtime / (rlim_t)1000000 >= rlim->rlim_max) {
886 mtx_unlock_spin(&sched_lock);
888 killproc(p, "exceeded maximum CPU limit");
889 mtx_lock_spin(&sched_lock);
890 PROC_UNLOCK_NOSWITCH(p);
892 mtx_unlock_spin(&sched_lock);
895 mtx_lock_spin(&sched_lock);
896 PROC_UNLOCK_NOSWITCH(p);
897 if (rlim->rlim_cur < rlim->rlim_max) {
898 /* XXX: we should make a private copy */
906 * Pick a new current process and record its start time.
909 PCPU_SET(switchtime, new_switchtime);
910 CTR4(KTR_PROC, "mi_switch: old proc %p (pid %d, %s), schedlock %p",
911 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
912 sched_nest = sched_lock.mtx_recurse;
913 curproc->p_lastcpu = curproc->p_oncpu;
914 curproc->p_oncpu = NOCPU;
915 clear_resched(curproc);
917 curproc->p_oncpu = PCPU_GET(cpuid);
918 sched_lock.mtx_recurse = sched_nest;
919 sched_lock.mtx_lock = (uintptr_t)curproc;
920 CTR4(KTR_PROC, "mi_switch: new proc %p (pid %d, %s), schedlock %p",
921 p, p->p_pid, p->p_comm, (void *) sched_lock.mtx_lock);
922 if (PCPU_GET(switchtime.tv_sec) == 0)
923 microuptime(PCPU_PTR(switchtime));
924 PCPU_SET(switchticks, ticks);
928 * Change process state to be runnable,
929 * placing it on the run queue if it is in memory,
930 * and awakening the swapper if it isn't in memory.
934 register struct proc *p;
939 mtx_lock_spin(&sched_lock);
946 panic("setrunnable");
948 case SSLEEP: /* e.g. when sending signals */
949 if (p->p_sflag & PS_CVWAITQ)
959 if (p->p_sflag & PS_INMEM)
962 if (p->p_slptime > 1)
965 if ((p->p_sflag & PS_INMEM) == 0) {
966 p->p_sflag |= PS_SWAPINREQ;
967 wakeup((caddr_t)&proc0);
971 mtx_unlock_spin(&sched_lock);
975 * Compute the priority of a process when running in user mode.
976 * Arrange to reschedule if the resulting priority is better
977 * than that of the current process.
981 register struct proc *p;
983 register unsigned int newpriority;
985 mtx_lock_spin(&sched_lock);
986 if (p->p_pri.pri_class == PRI_TIMESHARE) {
987 newpriority = PUSER + p->p_estcpu / INVERSE_ESTCPU_WEIGHT +
988 NICE_WEIGHT * (p->p_nice - PRIO_MIN);
989 newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
991 p->p_pri.pri_user = newpriority;
994 mtx_unlock_spin(&sched_lock);
1003 callout_init(&schedcpu_callout, 1);
1004 callout_init(&roundrobin_callout, 0);
1006 /* Kick off timeout driven events by calling first time. */
1012 * We adjust the priority of the current process. The priority of
1013 * a process gets worse as it accumulates CPU time. The cpu usage
1014 * estimator (p_estcpu) is increased here. resetpriority() will
1015 * compute a different priority each time p_estcpu increases by
1016 * INVERSE_ESTCPU_WEIGHT
1017 * (until MAXPRI is reached). The cpu usage estimator ramps up
1018 * quite quickly when the process is running (linearly), and decays
1019 * away exponentially, at a rate which is proportionally slower when
1020 * the system is busy. The basic principle is that the system will
1021 * 90% forget that the process used a lot of CPU time in 5 * loadav
1022 * seconds. This causes the system to favor processes which haven't
1023 * run much recently, and to round-robin among other processes.
1031 p->p_estcpu = ESTCPULIM(p->p_estcpu + 1);
1032 if ((p->p_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
1034 if (p->p_pri.pri_level >= PUSER)
1035 p->p_pri.pri_level = p->p_pri.pri_user;
1040 * General purpose yield system call
1043 yield(struct proc *p, struct yield_args *uap)
1050 mtx_lock_spin(&sched_lock);
1051 DROP_GIANT_NOSWITCH();
1052 p->p_pri.pri_level = PRI_MAX_TIMESHARE;
1054 p->p_stats->p_ru.ru_nvcsw++;
1056 mtx_unlock_spin(&sched_lock);