BSD 4_4 release
[unix-history] / usr / src / sys / kern / kern_synch.c
index a4277dc..609100b 100644 (file)
@@ -1,28 +1,67 @@
-/*
- * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+/*-
+ * Copyright (c) 1982, 1986, 1990, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  *
  *
- *     @(#)kern_synch.c        7.14 (Berkeley) %G%
+ *     @(#)kern_synch.c        8.1 (Berkeley) 6/10/93
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "user.h"
-#include "proc.h"
-#include "kernel.h"
-#include "buf.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/kernel.h>
+#include <sys/buf.h>
+#include <sys/signalvar.h>
+#include <sys/resourcevar.h>
+#include <sys/vmmeter.h>
+#ifdef KTRACE
+#include <sys/ktrace.h>
+#endif
+
+#include <machine/cpu.h>
 
 
-#include "machine/cpu.h"
+u_char curpri;                 /* usrpri of curproc */
+int    lbolt;                  /* once a second sleep address */
 
 /*
  * Force switch among equal priority processes every 100ms.
  */
 
 /*
  * Force switch among equal priority processes every 100ms.
  */
-roundrobin()
+/* ARGSUSED */
+void
+roundrobin(arg)
+       void *arg;
 {
 
        need_resched();
 {
 
        need_resched();
-       timeout(roundrobin, (caddr_t)0, hz / 10);
+       timeout(roundrobin, (void *)0, hz / 10);
 }
 
 /*
 }
 
 /*
@@ -113,15 +152,18 @@ fixpt_t   ccpu = 0.95122942450071400909 * FSCALE;         /* exp(-1/20) */
 /*
  * Recompute process priorities, once a second
  */
 /*
  * Recompute process priorities, once a second
  */
-schedcpu()
+/* ARGSUSED */
+void
+schedcpu(arg)
+       void *arg;
 {
 {
-       register fixpt_t loadfac = loadfactor(averunnable[0]);
+       register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
        register struct proc *p;
        register int s;
        register unsigned int newcpu;
 
        wakeup((caddr_t)&lbolt);
        register struct proc *p;
        register int s;
        register unsigned int newcpu;
 
        wakeup((caddr_t)&lbolt);
-       for (p = allproc; p != NULL; p = p->p_nxt) {
+       for (p = (struct proc *)allproc; p != NULL; p = p->p_nxt) {
                /*
                 * Increment time in/out of memory and sleep time
                 * (if sleeping).  We ignore overflow; with 16-bit int's
                /*
                 * Increment time in/out of memory and sleep time
                 * (if sleeping).  We ignore overflow; with 16-bit int's
@@ -137,6 +179,7 @@ schedcpu()
                 */
                if (p->p_slptime > 1)
                        continue;
                 */
                if (p->p_slptime > 1)
                        continue;
+               s = splstatclock();     /* prevent state changes */
                /*
                 * p_pctcpu is only for ps.
                 */
                /*
                 * p_pctcpu is only for ps.
                 */
@@ -153,10 +196,9 @@ schedcpu()
                newcpu = (u_int) decay_cpu(loadfac, p->p_cpu) + p->p_nice;
                p->p_cpu = min(newcpu, UCHAR_MAX);
                setpri(p);
                newcpu = (u_int) decay_cpu(loadfac, p->p_cpu) + p->p_nice;
                p->p_cpu = min(newcpu, UCHAR_MAX);
                setpri(p);
-               s = splhigh();  /* prevent state changes */
                if (p->p_pri >= PUSER) {
 #define        PPQ     (128 / NQS)             /* priorities per queue */
                if (p->p_pri >= PUSER) {
 #define        PPQ     (128 / NQS)             /* priorities per queue */
-                       if ((p != curproc || noproc) &&
+                       if ((p != curproc) &&
                            p->p_stat == SRUN &&
                            (p->p_flag & SLOAD) &&
                            (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) {
                            p->p_stat == SRUN &&
                            (p->p_flag & SLOAD) &&
                            (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) {
@@ -171,7 +213,7 @@ schedcpu()
        vmmeter();
        if (bclnlist != NULL)
                wakeup((caddr_t)pageproc);
        vmmeter();
        if (bclnlist != NULL)
                wakeup((caddr_t)pageproc);
-       timeout(schedcpu, (caddr_t)0, hz);
+       timeout(schedcpu, (void *)0, hz);
 }
 
 /*
 }
 
 /*
@@ -179,11 +221,12 @@ schedcpu()
  * For all load averages >= 1 and max p_cpu of 255, sleeping for at least
  * six times the loadfactor will decay p_cpu to zero.
  */
  * For all load averages >= 1 and max p_cpu of 255, sleeping for at least
  * six times the loadfactor will decay p_cpu to zero.
  */
+void
 updatepri(p)
        register struct proc *p;
 {
        register unsigned int newcpu = p->p_cpu;
 updatepri(p)
        register struct proc *p;
 {
        register unsigned int newcpu = p->p_cpu;
-       register fixpt_t loadfac = loadfactor(averunnable[0]);
+       register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
 
        if (p->p_slptime > 5 * loadfac)
                p->p_cpu = 0;
 
        if (p->p_slptime > 5 * loadfac)
                p->p_cpu = 0;
@@ -227,19 +270,24 @@ int safepri;
  * if possible, and EINTR is returned if the system call should
  * be interrupted by the signal (return EINTR).
  */
  * if possible, and EINTR is returned if the system call should
  * be interrupted by the signal (return EINTR).
  */
+int
 tsleep(chan, pri, wmesg, timo)
 tsleep(chan, pri, wmesg, timo)
-       caddr_t chan;
+       void *chan;
        int pri;
        char *wmesg;
        int timo;
 {
        int pri;
        char *wmesg;
        int timo;
 {
-       register struct proc *p = curproc;              /* XXX */
+       register struct proc *p = curproc;
        register struct slpque *qp;
        register s;
        int sig, catch = pri & PCATCH;
        extern int cold;
        register struct slpque *qp;
        register s;
        int sig, catch = pri & PCATCH;
        extern int cold;
-       int endtsleep();
+       void endtsleep __P((void *));
 
 
+#ifdef KTRACE
+       if (KTRPOINT(p, KTR_CSW))
+               ktrcsw(p->p_tracep, 1, 0);
+#endif
        s = splhigh();
        if (cold || panicstr) {
                /*
        s = splhigh();
        if (cold || panicstr) {
                /*
@@ -253,7 +301,7 @@ tsleep(chan, pri, wmesg, timo)
                return (0);
        }
 #ifdef DIAGNOSTIC
                return (0);
        }
 #ifdef DIAGNOSTIC
-       if (chan == 0 || p->p_stat != SRUN || p->p_rlink)
+       if (chan == NULL || p->p_stat != SRUN || p->p_rlink)
                panic("tsleep");
 #endif
        p->p_wchan = chan;
                panic("tsleep");
 #endif
        p->p_wchan = chan;
@@ -267,7 +315,7 @@ tsleep(chan, pri, wmesg, timo)
                *qp->sq_tailp = p;
        *(qp->sq_tailp = &p->p_link) = 0;
        if (timo)
                *qp->sq_tailp = p;
        *(qp->sq_tailp = &p->p_link) = 0;
        if (timo)
-               timeout(endtsleep, (caddr_t)p, timo);
+               timeout(endtsleep, (void *)p, timo);
        /*
         * We put ourselves on the sleep queue and start our timeout
         * before calling CURSIG, as we could stop there, and a wakeup
        /*
         * We put ourselves on the sleep queue and start our timeout
         * before calling CURSIG, as we could stop there, and a wakeup
@@ -289,9 +337,9 @@ tsleep(chan, pri, wmesg, timo)
                        catch = 0;
                        goto resume;
                }
                        catch = 0;
                        goto resume;
                }
-       }
+       } else
+               sig = 0;
        p->p_stat = SSLEEP;
        p->p_stat = SSLEEP;
-       (void) spl0();
        p->p_stats->p_ru.ru_nvcsw++;
        swtch();
 resume:
        p->p_stats->p_ru.ru_nvcsw++;
        swtch();
 resume:
@@ -300,15 +348,28 @@ resume:
        p->p_flag &= ~SSINTR;
        if (p->p_flag & STIMO) {
                p->p_flag &= ~STIMO;
        p->p_flag &= ~SSINTR;
        if (p->p_flag & STIMO) {
                p->p_flag &= ~STIMO;
-               if (catch == 0 || sig == 0)
+               if (sig == 0) {
+#ifdef KTRACE
+                       if (KTRPOINT(p, KTR_CSW))
+                               ktrcsw(p->p_tracep, 0, 0);
+#endif
                        return (EWOULDBLOCK);
                        return (EWOULDBLOCK);
+               }
        } else if (timo)
        } else if (timo)
-               untimeout(endtsleep, (caddr_t)p);
+               untimeout(endtsleep, (void *)p);
        if (catch && (sig != 0 || (sig = CURSIG(p)))) {
        if (catch && (sig != 0 || (sig = CURSIG(p)))) {
+#ifdef KTRACE
+               if (KTRPOINT(p, KTR_CSW))
+                       ktrcsw(p->p_tracep, 0, 0);
+#endif
                if (p->p_sigacts->ps_sigintr & sigmask(sig))
                        return (EINTR);
                return (ERESTART);
        }
                if (p->p_sigacts->ps_sigintr & sigmask(sig))
                        return (EINTR);
                return (ERESTART);
        }
+#ifdef KTRACE
+       if (KTRPOINT(p, KTR_CSW))
+               ktrcsw(p->p_tracep, 0, 0);
+#endif
        return (0);
 }
 
        return (0);
 }
 
@@ -318,11 +379,15 @@ resume:
  * set timeout flag and undo the sleep.  If proc
  * is stopped, just unsleep so it will remain stopped.
  */
  * set timeout flag and undo the sleep.  If proc
  * is stopped, just unsleep so it will remain stopped.
  */
-endtsleep(p)
-       register struct proc *p;
+void
+endtsleep(arg)
+       void *arg;
 {
 {
-       int s = splhigh();
+       register struct proc *p;
+       int s;
 
 
+       p = (struct proc *)arg;
+       s = splhigh();
        if (p->p_wchan) {
                if (p->p_stat == SSLEEP)
                        setrun(p);
        if (p->p_wchan) {
                if (p->p_stat == SSLEEP)
                        setrun(p);
@@ -336,11 +401,12 @@ endtsleep(p)
 /*
  * Short-term, non-interruptable sleep.
  */
 /*
  * Short-term, non-interruptable sleep.
  */
+void
 sleep(chan, pri)
 sleep(chan, pri)
-       caddr_t chan;
+       void *chan;
        int pri;
 {
        int pri;
 {
-       register struct proc *p = curproc;              /* XXX */
+       register struct proc *p = curproc;
        register struct slpque *qp;
        register s;
        extern int cold;
        register struct slpque *qp;
        register s;
        extern int cold;
@@ -348,7 +414,7 @@ sleep(chan, pri)
 #ifdef DIAGNOSTIC
        if (pri > PZERO) {
                printf("sleep called with pri %d > PZERO, wchan: %x\n",
 #ifdef DIAGNOSTIC
        if (pri > PZERO) {
                printf("sleep called with pri %d > PZERO, wchan: %x\n",
-                       pri, chan);
+                   pri, chan);
                panic("old sleep");
        }
 #endif
                panic("old sleep");
        }
 #endif
@@ -365,7 +431,7 @@ sleep(chan, pri)
                return;
        }
 #ifdef DIAGNOSTIC
                return;
        }
 #ifdef DIAGNOSTIC
-       if (chan==0 || p->p_stat != SRUN || p->p_rlink)
+       if (chan == NULL || p->p_stat != SRUN || p->p_rlink)
                panic("sleep");
 #endif
        p->p_wchan = chan;
                panic("sleep");
 #endif
        p->p_wchan = chan;
@@ -379,9 +445,16 @@ sleep(chan, pri)
                *qp->sq_tailp = p;
        *(qp->sq_tailp = &p->p_link) = 0;
        p->p_stat = SSLEEP;
                *qp->sq_tailp = p;
        *(qp->sq_tailp = &p->p_link) = 0;
        p->p_stat = SSLEEP;
-       (void) spl0();
        p->p_stats->p_ru.ru_nvcsw++;
        p->p_stats->p_ru.ru_nvcsw++;
+#ifdef KTRACE
+       if (KTRPOINT(p, KTR_CSW))
+               ktrcsw(p->p_tracep, 1, 0);
+#endif
        swtch();
        swtch();
+#ifdef KTRACE
+       if (KTRPOINT(p, KTR_CSW))
+               ktrcsw(p->p_tracep, 0, 0);
+#endif
        curpri = p->p_usrpri;
        splx(s);
 }
        curpri = p->p_usrpri;
        splx(s);
 }
@@ -389,6 +462,7 @@ sleep(chan, pri)
 /*
  * Remove a process from its wait queue
  */
 /*
  * Remove a process from its wait queue
  */
+void
 unsleep(p)
        register struct proc *p;
 {
 unsleep(p)
        register struct proc *p;
 {
@@ -413,8 +487,9 @@ unsleep(p)
  * Wakeup on "chan"; set all processes
  * sleeping on chan to run state.
  */
  * Wakeup on "chan"; set all processes
  * sleeping on chan to run state.
  */
+void
 wakeup(chan)
 wakeup(chan)
-       register caddr_t chan;
+       register void *chan;
 {
        register struct slpque *qp;
        register struct proc *p, **q;
 {
        register struct slpque *qp;
        register struct proc *p, **q;
@@ -458,6 +533,63 @@ restart:
        splx(s);
 }
 
        splx(s);
 }
 
+/*
+ * The machine independent parts of swtch().
+ * Must be called at splstatclock() or higher.
+ */
+void
+swtch()
+{
+       register struct proc *p = curproc;      /* XXX */
+       register struct rlimit *rlim;
+       register long s, u;
+       struct timeval tv;
+
+       /*
+        * Compute the amount of time during which the current
+        * process was running, and add that to its total so far.
+        */
+       microtime(&tv);
+       u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec);
+       s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec);
+       if (u < 0) {
+               u += 1000000;
+               s--;
+       } else if (u >= 1000000) {
+               u -= 1000000;
+               s++;
+       }
+       p->p_rtime.tv_usec = u;
+       p->p_rtime.tv_sec = s;
+
+       /*
+        * Check if the process exceeds its cpu resource allocation.
+        * If over max, kill it.  In any case, if it has run for more
+        * than 10 minutes, reduce priority to give others a chance.
+        */
+       rlim = &p->p_rlimit[RLIMIT_CPU];
+       if (s >= rlim->rlim_cur) {
+               if (s >= rlim->rlim_max)
+                       psignal(p, SIGKILL);
+               else {
+                       psignal(p, SIGXCPU);
+                       if (rlim->rlim_cur < rlim->rlim_max)
+                               rlim->rlim_cur += 5;
+               }
+       }
+       if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) {
+               p->p_nice = NZERO + 4;
+               setpri(p);
+       }
+
+       /*
+        * Pick a new current process and record its start time.
+        */
+       cnt.v_swtch++;
+       cpu_swtch(p);
+       microtime(&runtime);
+}
+
 /*
  * Initialize the (doubly-linked) run queues
  * to be empty.
 /*
  * Initialize the (doubly-linked) run queues
  * to be empty.
@@ -475,6 +607,7 @@ rqinit()
  * placing it on the run queue if it is in memory,
  * and awakening the swapper if it isn't in memory.
  */
  * placing it on the run queue if it is in memory,
  * and awakening the swapper if it isn't in memory.
  */
+void
 setrun(p)
        register struct proc *p;
 {
 setrun(p)
        register struct proc *p;
 {
@@ -516,6 +649,7 @@ setrun(p)
  * Arrange to reschedule if the resulting priority
  * is better than that of the current process.
  */
  * Arrange to reschedule if the resulting priority
  * is better than that of the current process.
  */
+void
 setpri(p)
        register struct proc *p;
 {
 setpri(p)
        register struct proc *p;
 {