BSD 4_4 release
[unix-history] / usr / src / sys / kern / kern_synch.c
index 9271372..609100b 100644 (file)
@@ -1,25 +1,52 @@
 /*-
 /*-
- * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
- * Copyright (c) 1991 The Regents of the University of California.
+ * Copyright (c) 1982, 1986, 1990, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * 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.
  *
  *
- *     @(#)kern_synch.c        7.23 (Berkeley) %G%
+ * 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        8.1 (Berkeley) 6/10/93
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "proc.h"
-#include "kernel.h"
-#include "buf.h"
-#include "signalvar.h"
-#include "resourcevar.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
 #ifdef KTRACE
-#include "ktrace.h"
+#include <sys/ktrace.h>
 #endif
 
 #endif
 
-#include "machine/cpu.h"
+#include <machine/cpu.h>
 
 u_char curpri;                 /* usrpri of curproc */
 int    lbolt;                  /* once a second sleep address */
 
 u_char curpri;                 /* usrpri of curproc */
 int    lbolt;                  /* once a second sleep address */
@@ -27,11 +54,14 @@ 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);
 }
 
 /*
 }
 
 /*
@@ -122,7 +152,10 @@ 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.ldavg[0]);
        register struct proc *p;
 {
        register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
        register struct proc *p;
@@ -130,7 +163,7 @@ schedcpu()
        register unsigned int newcpu;
 
        wakeup((caddr_t)&lbolt);
        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
@@ -146,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.
                 */
@@ -162,7 +196,6 @@ 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 != curproc) &&
                if (p->p_pri >= PUSER) {
 #define        PPQ     (128 / NQS)             /* priorities per queue */
                        if ((p != curproc) &&
@@ -180,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);
 }
 
 /*
 }
 
 /*
@@ -188,6 +221,7 @@ 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;
 {
 updatepri(p)
        register struct proc *p;
 {
@@ -236,6 +270,7 @@ 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)
        void *chan;
        int pri;
 tsleep(chan, pri, wmesg, timo)
        void *chan;
        int pri;
@@ -247,7 +282,7 @@ tsleep(chan, pri, wmesg, timo)
        register s;
        int sig, catch = pri & PCATCH;
        extern int cold;
        register s;
        int sig, catch = pri & PCATCH;
        extern int cold;
-       int endtsleep();
+       void endtsleep __P((void *));
 
 #ifdef KTRACE
        if (KTRPOINT(p, KTR_CSW))
 
 #ifdef KTRACE
        if (KTRPOINT(p, KTR_CSW))
@@ -280,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
@@ -321,7 +356,7 @@ resume:
                        return (EWOULDBLOCK);
                }
        } else if (timo)
                        return (EWOULDBLOCK);
                }
        } else if (timo)
-               untimeout(endtsleep, (caddr_t)p);
+               untimeout(endtsleep, (void *)p);
        if (catch && (sig != 0 || (sig = CURSIG(p)))) {
 #ifdef KTRACE
                if (KTRPOINT(p, KTR_CSW))
        if (catch && (sig != 0 || (sig = CURSIG(p)))) {
 #ifdef KTRACE
                if (KTRPOINT(p, KTR_CSW))
@@ -344,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);
@@ -362,6 +401,7 @@ endtsleep(p)
 /*
  * Short-term, non-interruptable sleep.
  */
 /*
  * Short-term, non-interruptable sleep.
  */
+void
 sleep(chan, pri)
        void *chan;
        int pri;
 sleep(chan, pri)
        void *chan;
        int pri;
@@ -422,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;
 {
@@ -446,6 +487,7 @@ 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)
        register void *chan;
 {
 wakeup(chan)
        register void *chan;
 {
@@ -491,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.
@@ -508,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;
 {
@@ -549,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;
 {