put redundant stuff in subroutines; add WILLRELE support
[unix-history] / usr / src / sys / kern / kern_synch.c
index b93c93d..efc9f73 100644 (file)
@@ -1,9 +1,11 @@
-/*
- * Copyright (c) 1982, 1986, 1990, 1991 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 The Regents of the University of California.
+ * Copyright (c) 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)kern_synch.c        7.15 (Berkeley) %G%
+ *     @(#)kern_synch.c        7.24 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
 #include "buf.h"
 #include "signalvar.h"
 #include "resourcevar.h"
 #include "buf.h"
 #include "signalvar.h"
 #include "resourcevar.h"
+#include "vmmeter.h"
+#ifdef KTRACE
+#include "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);
 }
 
 /*
 }
 
 /*
@@ -114,15 +126,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
@@ -172,7 +187,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);
 }
 
 /*
 }
 
 /*
@@ -180,11 +195,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;
@@ -228,8 +244,9 @@ 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;
@@ -239,8 +256,12 @@ 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))
+               ktrcsw(p->p_tracep, 1, 0);
+#endif
        s = splhigh();
        if (cold || panicstr) {
                /*
        s = splhigh();
        if (cold || panicstr) {
                /*
@@ -254,7 +275,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;
@@ -268,7 +289,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
@@ -290,9 +311,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:
@@ -301,15 +322,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);
 }
 
@@ -319,11 +353,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);
@@ -337,8 +375,9 @@ 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;
 {
        register struct proc *p = curproc;
        int pri;
 {
        register struct proc *p = curproc;
@@ -349,7 +388,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
@@ -366,7 +405,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;
@@ -380,9 +419,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);
 }
@@ -390,6 +436,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;
 {
@@ -414,8 +461,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;
@@ -459,6 +507,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.
@@ -476,6 +581,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;
 {
@@ -517,6 +623,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;
 {