Move more scheduler-specific defines from various places into usched_bsd4.c
[dragonfly.git] / sys / kern / kern_synch.c
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  * $FreeBSD: src/sys/kern/kern_synch.c,v 1.87.2.6 2002/10/13 07:29:53 kbyanc Exp $
40  * $DragonFly: src/sys/kern/kern_synch.c,v 1.46 2005/06/26 22:03:22 dillon Exp $
41  */
42
43 #include "opt_ktrace.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/vmmeter.h>
52 #include <sys/sysctl.h>
53 #include <sys/thread2.h>
54 #ifdef KTRACE
55 #include <sys/uio.h>
56 #include <sys/ktrace.h>
57 #endif
58 #include <sys/xwait.h>
59
60 #include <machine/cpu.h>
61 #include <machine/ipl.h>
62 #include <machine/smp.h>
63
64 static void sched_setup (void *dummy);
65 SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
66
67 int     hogticks;
68 int     lbolt;
69 int     sched_quantum;          /* Roundrobin scheduling quantum in ticks. */
70 int     ncpus;
71 int     ncpus2, ncpus2_shift, ncpus2_mask;
72 int     safepri;
73
74 static struct callout loadav_callout;
75 static struct callout roundrobin_callout;
76 static struct callout schedcpu_callout;
77
78 struct loadavg averunnable =
79         { {0, 0, 0}, FSCALE };  /* load average, of runnable procs */
80 /*
81  * Constants for averages over 1, 5, and 15 minutes
82  * when sampling at 5 second intervals.
83  */
84 static fixpt_t cexp[3] = {
85         0.9200444146293232 * FSCALE,    /* exp(-1/12) */
86         0.9834714538216174 * FSCALE,    /* exp(-1/60) */
87         0.9944598480048967 * FSCALE,    /* exp(-1/180) */
88 };
89
90 static void     endtsleep (void *);
91 static void     loadav (void *arg);
92 static void     roundrobin (void *arg);
93 static void     schedcpu (void *arg);
94 static void     updatepri (struct proc *p);
95
96 /*
97  * Adjust the scheduler quantum.  The quantum is specified in microseconds.
98  * Note that 'tick' is in microseconds per tick.
99  */
100 static int
101 sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
102 {
103         int error, new_val;
104
105         new_val = sched_quantum * tick;
106         error = sysctl_handle_int(oidp, &new_val, 0, req);
107         if (error != 0 || req->newptr == NULL)
108                 return (error);
109         if (new_val < tick)
110                 return (EINVAL);
111         sched_quantum = new_val / tick;
112         hogticks = 2 * sched_quantum;
113         return (0);
114 }
115
116 SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
117         0, sizeof sched_quantum, sysctl_kern_quantum, "I", "");
118
119 int 
120 roundrobin_interval(void)
121 {
122         return (sched_quantum);
123 }
124
125 /*
126  * Force switch among equal priority processes every 100ms. 
127  *
128  * WARNING!  The MP lock is not held on ipi message remotes.
129  */
130 #ifdef SMP
131
132 static void
133 roundrobin_remote(void *arg)
134 {
135         struct proc *p = lwkt_preempted_proc();
136         if (p == NULL || RTP_PRIO_NEED_RR(p->p_rtprio.type))
137                 need_user_resched();
138 }
139
140 #endif
141
142 static void
143 roundrobin(void *arg)
144 {
145         struct proc *p = lwkt_preempted_proc();
146         if (p == NULL || RTP_PRIO_NEED_RR(p->p_rtprio.type))
147                 need_user_resched();
148 #ifdef SMP
149         lwkt_send_ipiq_mask(mycpu->gd_other_cpus, roundrobin_remote, NULL);
150 #endif
151         callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL);
152 }
153
154 #ifdef SMP
155
156 void
157 resched_cpus(u_int32_t mask)
158 {
159         lwkt_send_ipiq_mask(mask, roundrobin_remote, NULL);
160 }
161
162 #endif
163
164 /*
165  * The load average is scaled by FSCALE (2048 typ).  The estimated cpu is
166  * incremented at a rate of ESTCPUFREQ per second (50hz typ), but this is
167  * divided up across all cpu bound processes running in the system so an
168  * individual process will get less under load.  ESTCPULIM typicaly caps
169  * out at ESTCPUMAX (around 376, or 11 nice levels).
170  *
171  * Generally speaking the decay equation needs to break-even on growth
172  * at the limit at all load levels >= 1.0, so if the estimated cpu for
173  * a process increases by (ESTVCPUFREQ / load) per second, then the decay
174  * should reach this value when estcpu reaches ESTCPUMAX.  That calculation
175  * is:
176  *
177  *      ESTCPUMAX * decay = ESTCPUFREQ / load
178  *      decay = ESTCPUFREQ / (load * ESTCPUMAX)
179  *      decay = estcpu * 0.053 / load
180  *
181  * If the load is less then 1.0 we assume a load of 1.0.
182  */
183
184 #define cload(loadav)   ((loadav) < FSCALE ? FSCALE : (loadav))
185
186 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
187 static fixpt_t  ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
188 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
189
190 /* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
191 static int      fscale __unused = FSCALE;
192 SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
193
194 /*
195  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
196  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
197  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
198  *
199  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
200  *      1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
201  *
202  * If you don't want to bother with the faster/more-accurate formula, you
203  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
204  * (more general) method of calculating the %age of CPU used by a process.
205  */
206 #define CCPU_SHIFT      11
207
208 /*
209  * Recompute process priorities, once a second.
210  */
211 /* ARGSUSED */
212 static void
213 schedcpu(void *arg)
214 {
215         fixpt_t loadfac = averunnable.ldavg[0];
216         struct proc *p;
217         int ndecay;
218
219         FOREACH_PROC_IN_SYSTEM(p) {
220                 /*
221                  * Increment time in/out of memory and sleep time
222                  * (if sleeping).  We ignore overflow; with 16-bit int's
223                  * (remember them?) overflow takes 45 days.
224                  */
225                 p->p_swtime++;
226                 if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
227                         p->p_slptime++;
228                 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
229
230                 /*
231                  * If the process has slept the entire second,
232                  * stop recalculating its priority until it wakes up.
233                  */
234                 if (p->p_slptime > 1)
235                         continue;
236                 /* prevent state changes and protect run queue */
237                 crit_enter();
238
239                 /*
240                  * p_pctcpu is only for ps.
241                  */
242 #if     (FSHIFT >= CCPU_SHIFT)
243                 p->p_pctcpu += (ESTCPUFREQ == 100)?
244                         ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
245                         100 * (((fixpt_t) p->p_cpticks)
246                                 << (FSHIFT - CCPU_SHIFT)) / ESTCPUFREQ;
247 #else
248                 p->p_pctcpu += ((FSCALE - ccpu) *
249                         (p->p_cpticks * FSCALE / ESTCPUFREQ)) >> FSHIFT;
250 #endif
251                 /*
252                  * A single cpu-bound process with a system load of 1.0 will
253                  * increment cpticks by ESTCPUFREQ per second.  Scale
254                  * cpticks by the load to normalize it relative to 
255                  * ESTCPUFREQ.  This gives us an indication as to what
256                  * proportional percentage of available cpu the process has
257                  * used with a nominal range of 0 to ESTCPUFREQ.
258                  *
259                  * It should be noted that since the load average is a
260                  * trailing indicator, a jump in the load will cause this
261                  * calculation to be higher then normal.  This is desireable
262                  * because it penalizes the processes responsible for the
263                  * spike.
264                  */
265                 ndecay = (int)((p->p_cpticks * cload(loadfac)) >> FSHIFT);
266
267                 /*
268                  * Reduce p_estcpu based on the amount of cpu that could
269                  * have been used but wasn't.  Convert ndecay from the
270                  * amount used to the amount not used, and scale with our
271                  * nice value.
272                  *
273                  * The nice scaling determines how much the nice value
274                  * effects the cpu the process gets.
275                  */
276                 ndecay = ESTCPUFREQ - ndecay;
277                 ndecay -= p->p_nice * (ESTCPUMAX / 16) / PRIO_MAX;
278
279                 p->p_cpticks = 0;
280                 if (ndecay > 0) {
281                         if (ndecay > p->p_estcpu / 2)
282                                 ndecay = p->p_estcpu / 2;
283                         p->p_estcpu -= ndecay;
284                         p->p_usched->resetpriority(p);
285                 }
286                 crit_exit();
287         }
288         wakeup((caddr_t)&lbolt);
289         callout_reset(&schedcpu_callout, hz, schedcpu, NULL);
290 }
291
292 /*
293  * Recalculate the priority of a process after it has slept for a while.
294  * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
295  * least six times the loadfactor will decay p_estcpu to zero.
296  */
297 static void
298 updatepri(struct proc *p)
299 {
300         int ndecay;
301
302         ndecay = p->p_slptime * ESTCPUFREQ;
303         if (ndecay > 0) {
304                 if (p->p_estcpu > ndecay)
305                         p->p_estcpu -= ndecay;
306                 else
307                         p->p_estcpu = 0;
308                 p->p_usched->resetpriority(p);
309         }
310 }
311
312 /*
313  * We're only looking at 7 bits of the address; everything is
314  * aligned to 4, lots of things are aligned to greater powers
315  * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
316  */
317 #define TABLESIZE       128
318 static TAILQ_HEAD(slpquehead, thread) slpque[TABLESIZE];
319 #define LOOKUP(x)       (((intptr_t)(x) >> 8) & (TABLESIZE - 1))
320
321 /*
322  * General scheduler initialization.  We force a reschedule 25 times
323  * a second by default.
324  */
325 void
326 sleepinit(void)
327 {
328         int i;
329
330         sched_quantum = (hz + 24) / 25;
331         hogticks = 2 * sched_quantum;
332         for (i = 0; i < TABLESIZE; i++)
333                 TAILQ_INIT(&slpque[i]);
334 }
335
336 /*
337  * General sleep call.  Suspends the current process until a wakeup is
338  * performed on the specified identifier.  The process will then be made
339  * runnable with the specified priority.  Sleeps at most timo/hz seconds
340  * (0 means no timeout).  If flags includes PCATCH flag, signals are checked
341  * before and after sleeping, else signals are not checked.  Returns 0 if
342  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
343  * signal needs to be delivered, ERESTART is returned if the current system
344  * call should be restarted if possible, and EINTR is returned if the system
345  * call should be interrupted by the signal (return EINTR).
346  *
347  * Note that if we are a process, we release_curproc() before messing with
348  * the LWKT scheduler.
349  *
350  * During autoconfiguration or after a panic, a sleep will simply
351  * lower the priority briefly to allow interrupts, then return.
352  */
353 int
354 tsleep(void *ident, int flags, const char *wmesg, int timo)
355 {
356         struct thread *td = curthread;
357         struct proc *p = td->td_proc;           /* may be NULL */
358         int sig = 0, catch = flags & PCATCH;
359         int id = LOOKUP(ident);
360         int oldpri;
361         struct callout thandle;
362
363         /*
364          * NOTE: removed KTRPOINT, it could cause races due to blocking
365          * even in stable.  Just scrap it for now.
366          */
367         if (cold || panicstr) {
368                 /*
369                  * After a panic, or during autoconfiguration,
370                  * just give interrupts a chance, then just return;
371                  * don't run any other procs or panic below,
372                  * in case this is the idle process and already asleep.
373                  */
374                 splz();
375                 oldpri = td->td_pri & TDPRI_MASK;
376                 lwkt_setpri_self(safepri);
377                 lwkt_switch();
378                 lwkt_setpri_self(oldpri);
379                 return (0);
380         }
381         KKASSERT(td != &mycpu->gd_idlethread);  /* you must be kidding! */
382         crit_enter_quick(td);
383         KASSERT(ident != NULL, ("tsleep: no ident"));
384         KASSERT(p == NULL || p->p_stat == SRUN, ("tsleep %p %s %d",
385                 ident, wmesg, p->p_stat));
386
387         td->td_wchan = ident;
388         td->td_wmesg = wmesg;
389         td->td_wdomain = flags & PDOMAIN_MASK;
390         if (p) {
391                 if (flags & PNORESCHED)
392                         td->td_flags |= TDF_NORESCHED;
393                 p->p_usched->release_curproc(p);
394                 p->p_slptime = 0;
395         }
396         lwkt_deschedule_self(td);
397         TAILQ_INSERT_TAIL(&slpque[id], td, td_threadq);
398         if (timo) {
399                 callout_init(&thandle);
400                 callout_reset(&thandle, timo, endtsleep, td);
401         }
402         /*
403          * We put ourselves on the sleep queue and start our timeout
404          * before calling CURSIG, as we could stop there, and a wakeup
405          * or a SIGCONT (or both) could occur while we were stopped.
406          * A SIGCONT would cause us to be marked as SSLEEP
407          * without resuming us, thus we must be ready for sleep
408          * when CURSIG is called.  If the wakeup happens while we're
409          * stopped, td->td_wchan will be 0 upon return from CURSIG.
410          */
411         if (p) {
412                 if (catch) {
413                         p->p_flag |= P_SINTR;
414                         if ((sig = CURSIG(p))) {
415                                 if (td->td_wchan) {
416                                         unsleep(td);
417                                         lwkt_schedule_self(td);
418                                 }
419                                 p->p_stat = SRUN;
420                                 goto resume;
421                         }
422                         if (td->td_wchan == NULL) {
423                                 catch = 0;
424                                 goto resume;
425                         }
426                 } else {
427                         sig = 0;
428                 }
429
430                 /*
431                  * If we are not the current process we have to remove ourself
432                  * from the run queue.
433                  */
434                 KASSERT(p->p_stat == SRUN, ("PSTAT NOT SRUN %d %d", p->p_pid, p->p_stat));
435                 /*
436                  * If this is the current 'user' process schedule another one.
437                  */
438                 clrrunnable(p, SSLEEP);
439                 p->p_stats->p_ru.ru_nvcsw++;
440                 mi_switch(p);
441                 KASSERT(p->p_stat == SRUN, ("tsleep: stat not srun"));
442         } else {
443                 lwkt_switch();
444         }
445 resume:
446         if (p)
447                 p->p_flag &= ~P_SINTR;
448         crit_exit_quick(td);
449         td->td_flags &= ~TDF_NORESCHED;
450         if (td->td_flags & TDF_TIMEOUT) {
451                 td->td_flags &= ~TDF_TIMEOUT;
452                 if (sig == 0)
453                         return (EWOULDBLOCK);
454         } else if (timo) {
455                 callout_stop(&thandle);
456         } else if (td->td_wmesg) {
457                 /*
458                  * This can happen if a thread is woken up directly.  Clear
459                  * wmesg to avoid debugging confusion.
460                  */
461                 td->td_wmesg = NULL;
462         }
463         /* inline of iscaught() */
464         if (p) {
465                 if (catch && (sig != 0 || (sig = CURSIG(p)))) {
466                         if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
467                                 return (EINTR);
468                         return (ERESTART);
469                 }
470         }
471         return (0);
472 }
473
474 /*
475  * Implement the timeout for tsleep.  We interlock against
476  * wchan when setting TDF_TIMEOUT.  For processes we remove
477  * the sleep if the process is stopped rather then sleeping,
478  * so it remains stopped.
479  */
480 static void
481 endtsleep(void *arg)
482 {
483         thread_t td = arg;
484         struct proc *p;
485
486         crit_enter();
487         if (td->td_wchan) {
488                 td->td_flags |= TDF_TIMEOUT;
489                 if ((p = td->td_proc) != NULL) {
490                         if (p->p_stat == SSLEEP)
491                                 setrunnable(p);
492                         else
493                                 unsleep(td);
494                 } else {
495                         unsleep(td);
496                         lwkt_schedule(td);
497                 }
498         }
499         crit_exit();
500 }
501
502 /*
503  * Remove a process from its wait queue
504  */
505 void
506 unsleep(struct thread *td)
507 {
508         crit_enter();
509         if (td->td_wchan) {
510                 TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_threadq);
511                 td->td_wchan = NULL;
512         }
513         crit_exit();
514 }
515
516 /*
517  * Make all processes sleeping on the specified identifier runnable.
518  */
519 static void
520 _wakeup(void *ident, int domain, int count)
521 {
522         struct slpquehead *qp;
523         struct thread *td;
524         struct thread *ntd;
525         struct proc *p;
526         int id = LOOKUP(ident);
527
528         crit_enter();
529         qp = &slpque[id];
530 restart:
531         for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) {
532                 ntd = TAILQ_NEXT(td, td_threadq);
533                 if (td->td_wchan == ident && td->td_wdomain == domain) {
534                         TAILQ_REMOVE(qp, td, td_threadq);
535                         td->td_wchan = NULL;
536                         if ((p = td->td_proc) != NULL && p->p_stat == SSLEEP) {
537                                 /* OPTIMIZED EXPANSION OF setrunnable(p); */
538                                 if (p->p_slptime > 1)
539                                         updatepri(p);
540                                 p->p_slptime = 0;
541                                 p->p_stat = SRUN;
542                                 if (p->p_flag & P_INMEM) {
543                                         /*
544                                          * LWKT scheduled now, there is no
545                                          * userland runq interaction until
546                                          * the thread tries to return to user
547                                          * mode.
548                                          *
549                                          * setrunqueue(p); 
550                                          */
551                                         lwkt_schedule(td);
552                                 } else {
553                                         p->p_flag |= P_SWAPINREQ;
554                                         wakeup((caddr_t)&proc0);
555                                 }
556                                 /* END INLINE EXPANSION */
557                         } else if (p == NULL) {
558                                 lwkt_schedule(td);
559                         }
560                         if (--count == 0)
561                                 break;
562                         goto restart;
563                 }
564         }
565         crit_exit();
566 }
567
568 void
569 wakeup(void *ident)
570 {
571     _wakeup(ident, 0, 0);
572 }
573
574 void
575 wakeup_one(void *ident)
576 {
577     _wakeup(ident, 0, 1);
578 }
579
580 void
581 wakeup_domain(void *ident, int domain)
582 {
583     _wakeup(ident, domain, 0);
584 }
585
586 void
587 wakeup_domain_one(void *ident, int domain)
588 {
589     _wakeup(ident, domain, 1);
590 }
591
592 /*
593  * The machine independent parts of mi_switch().
594  *
595  * 'p' must be the current process.
596  */
597 void
598 mi_switch(struct proc *p)
599 {
600         thread_t td = p->p_thread;
601         struct rlimit *rlim;
602         u_int64_t ttime;
603
604         KKASSERT(td == mycpu->gd_curthread);
605
606         crit_enter_quick(td);
607
608         /*
609          * Check if the process exceeds its cpu resource allocation.
610          * If over max, kill it.  Time spent in interrupts is not 
611          * included.  YYY 64 bit match is expensive.  Ick.
612          */
613         ttime = td->td_sticks + td->td_uticks;
614         if (p->p_stat != SZOMB && p->p_limit->p_cpulimit != RLIM_INFINITY &&
615             ttime > p->p_limit->p_cpulimit) {
616                 rlim = &p->p_rlimit[RLIMIT_CPU];
617                 if (ttime / (rlim_t)1000000 >= rlim->rlim_max) {
618                         killproc(p, "exceeded maximum CPU limit");
619                 } else {
620                         psignal(p, SIGXCPU);
621                         if (rlim->rlim_cur < rlim->rlim_max) {
622                                 /* XXX: we should make a private copy */
623                                 rlim->rlim_cur += 5;
624                         }
625                 }
626         }
627
628         /*
629          * If we are in a SSTOPped state we deschedule ourselves.  
630          * YYY this needs to be cleaned up, remember that LWKTs stay on
631          * their run queue which works differently then the user scheduler
632          * which removes the process from the runq when it runs it.
633          */
634         mycpu->gd_cnt.v_swtch++;
635         if (p->p_stat == SSTOP)
636                 lwkt_deschedule_self(td);
637         lwkt_switch();
638         crit_exit_quick(td);
639 }
640
641 /*
642  * Change process state to be runnable,
643  * placing it on the run queue if it is in memory,
644  * and awakening the swapper if it isn't in memory.
645  */
646 void
647 setrunnable(struct proc *p)
648 {
649         crit_enter();
650
651         switch (p->p_stat) {
652         case 0:
653         case SRUN:
654         case SZOMB:
655         default:
656                 panic("setrunnable");
657         case SSTOP:
658         case SSLEEP:
659                 unsleep(p->p_thread);   /* e.g. when sending signals */
660                 break;
661
662         case SIDL:
663                 break;
664         }
665         p->p_stat = SRUN;
666
667         /*
668          * The process is controlled by LWKT at this point, we do not mess
669          * around with the userland scheduler until the thread tries to 
670          * return to user mode.
671          */
672         if (p->p_flag & P_INMEM)
673                 lwkt_schedule(p->p_thread);
674         crit_exit();
675         if (p->p_slptime > 1)
676                 updatepri(p);
677         p->p_slptime = 0;
678         if ((p->p_flag & P_INMEM) == 0) {
679                 p->p_flag |= P_SWAPINREQ;
680                 wakeup((caddr_t)&proc0);
681         }
682 }
683
684 /*
685  * Yield / synchronous reschedule.  This is a bit tricky because the trap
686  * code might have set a lazy release on the switch function.   Setting
687  * P_PASSIVE_ACQ will ensure that the lazy release executes when we call
688  * switch, and that we are given a greater chance of affinity with our
689  * current cpu.
690  *
691  * We call lwkt_setpri_self() to rotate our thread to the end of the lwkt
692  * run queue.  lwkt_switch() will also execute any assigned passive release
693  * (which usually calls release_curproc()), allowing a same/higher priority
694  * process to be designated as the current process.  
695  *
696  * While it is possible for a lower priority process to be designated,
697  * it's call to lwkt_maybe_switch() in acquire_curproc() will likely
698  * round-robin back to us and we will be able to re-acquire the current
699  * process designation.
700  */
701 void
702 uio_yield(void)
703 {
704         struct thread *td = curthread;
705         struct proc *p = td->td_proc;
706
707         lwkt_setpri_self(td->td_pri & TDPRI_MASK);
708         if (p) {
709                 p->p_flag |= P_PASSIVE_ACQ;
710                 lwkt_switch();
711                 p->p_flag &= ~P_PASSIVE_ACQ;
712         } else {
713                 lwkt_switch();
714         }
715 }
716
717 /*
718  * Change the process state to NOT be runnable, removing it from the run
719  * queue.
720  */
721 void
722 clrrunnable(struct proc *p, int stat)
723 {
724         crit_enter_quick(p->p_thread);
725         if (p->p_stat == SRUN && (p->p_flag & P_ONRUNQ))
726                 p->p_usched->remrunqueue(p);
727         p->p_stat = stat;
728         crit_exit_quick(p->p_thread);
729 }
730
731 /*
732  * Compute a tenex style load average of a quantity on
733  * 1, 5 and 15 minute intervals.
734  */
735 static void
736 loadav(void *arg)
737 {
738         int i, nrun;
739         struct loadavg *avg;
740         struct proc *p;
741         thread_t td;
742
743         avg = &averunnable;
744         nrun = 0;
745         FOREACH_PROC_IN_SYSTEM(p) {
746                 switch (p->p_stat) {
747                 case SRUN:
748                         if ((td = p->p_thread) == NULL)
749                                 break;
750                         if (td->td_flags & TDF_BLOCKED)
751                                 break;
752                         /* fall through */
753                 case SIDL:
754                         nrun++;
755                         break;
756                 default:
757                         break;
758                 }
759         }
760         for (i = 0; i < 3; i++)
761                 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
762                     nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
763
764         /*
765          * Schedule the next update to occur after 5 seconds, but add a
766          * random variation to avoid synchronisation with processes that
767          * run at regular intervals.
768          */
769         callout_reset(&loadav_callout, hz * 4 + (int)(random() % (hz * 2 + 1)),
770             loadav, NULL);
771 }
772
773 /* ARGSUSED */
774 static void
775 sched_setup(void *dummy)
776 {
777         callout_init(&loadav_callout);
778         callout_init(&roundrobin_callout);
779         callout_init(&schedcpu_callout);
780
781         /* Kick off timeout driven events by calling first time. */
782         roundrobin(NULL);
783         schedcpu(NULL);
784         loadav(NULL);
785 }
786
787 /*
788  * We adjust the priority of the current process.  The priority of
789  * a process gets worse as it accumulates CPU time.  The cpu usage
790  * estimator (p_estcpu) is increased here.  resetpriority() will
791  * compute a different priority each time p_estcpu increases by
792  * INVERSE_ESTCPU_WEIGHT * (until MAXPRI is reached).
793  *
794  * The cpu usage estimator ramps up quite quickly when the process is 
795  * running (linearly), and decays away exponentially, at a rate which
796  * is proportionally slower when the system is busy.  The basic principle
797  * is that the system will 90% forget that the process used a lot of CPU
798  * time in 5 * loadav seconds.  This causes the system to favor processes
799  * which haven't run much recently, and to round-robin among other processes.
800  *
801  * WARNING! called from a fast-int or an IPI, the MP lock MIGHT NOT BE HELD
802  * and we cannot block.
803  */
804 void
805 schedulerclock(void *dummy)
806 {
807         struct thread *td;
808         struct proc *p;
809
810         td = curthread;
811         if ((p = td->td_proc) != NULL) {
812                 p->p_cpticks++;         /* cpticks runs at ESTCPUFREQ */
813                 p->p_estcpu = ESTCPULIM(p->p_estcpu + 1);
814                 if (try_mplock()) {
815                         p->p_usched->resetpriority(p);
816                         rel_mplock();
817                 }
818         }
819 }
820