cleaned up and re-enable compaction
[unix-history] / usr / src / sys / kern / kern_clock.c
index 568f8f7..c94dc3c 100644 (file)
@@ -1,4 +1,4 @@
-/*     kern_clock.c    4.17    81/04/02        */
+/*     kern_clock.c    4.27    81/11/20        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -18,6 +18,7 @@
 #include "../h/clock.h"
 #include "../h/cpu.h"
 
 #include "../h/clock.h"
 #include "../h/cpu.h"
 
+#include "bk.h"
 #include "dh.h"
 #include "dz.h"
 
 #include "dh.h"
 #include "dz.h"
 
@@ -44,6 +45,9 @@
  * interrupts compressed into one (due to excessive interrupt load),
  * but that hardclock interrupts should never be lost.
  */
  * interrupts compressed into one (due to excessive interrupt load),
  * but that hardclock interrupts should never be lost.
  */
+#ifdef KPROF
+int    kcounts[20000];
+#endif
 
 /*ARGSUSED*/
 hardclock(pc, ps)
 
 /*ARGSUSED*/
 hardclock(pc, ps)
@@ -61,13 +65,10 @@ hardclock(pc, ps)
        /*
         * update callout times
         */
        /*
         * update callout times
         */
-       if (callout[0].c_func == NULL)
-               goto out;
-       p1 = &callout[0];
-       while (p1->c_time<=0 && p1->c_func!=NULL)
-               p1++;
-       p1->c_time--;
-out:
+       for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next)
+               ;
+       if (p1)
+               p1->c_time--;
 
        /*
         * Maintain iostat and per-process cpu statistics
 
        /*
         * Maintain iostat and per-process cpu statistics
@@ -99,6 +100,11 @@ out:
                else
                        cpstate = CP_USER;
        } else {
                else
                        cpstate = CP_USER;
        } else {
+#ifdef KPROF
+       int k = ((int)pc & 0x7fffffff) / 8;
+       if (k < 20000)
+               kcounts[k]++;
+#endif
                cpstate = CP_SYS;
                if (noproc)
                        cpstate = CP_IDLE;
                cpstate = CP_SYS;
                if (noproc)
                        cpstate = CP_IDLE;
@@ -117,7 +123,7 @@ out:
                pp->p_cpticks++;
                if(++pp->p_cpu == 0)
                        pp->p_cpu--;
                pp->p_cpticks++;
                if(++pp->p_cpu == 0)
                        pp->p_cpu--;
-               if(pp->p_cpu % 16 == 0) {
+               if(pp->p_cpu % 4 == 0) {
                        (void) setpri(pp);
                        if (pp->p_pri >= PUSER)
                                pp->p_pri = pp->p_usrpri;
                        (void) setpri(pp);
                        if (pp->p_pri >= PUSER)
                                pp->p_pri = pp->p_usrpri;
@@ -143,13 +149,26 @@ out:
 }
 
 /*
 }
 
 /*
- * SCHMAG is the constant in the digital decay cpu
- * usage priority assignment.  Each second we multiply
- * the previous cpu usage estimate by SCHMAG.  At 9/10
- * it tends to decay away all knowledge of previous activity
- * in about 10 seconds.
+ * The digital decay cpu usage priority assignment is scaled to run in
+ * time as expanded by the 1 minute load average.  Each second we
+ * multiply the the previous cpu usage estimate by
+ *             nrscale*avenrun[0]
+ * The following relates the load average to the period over which
+ * cpu usage is 90% forgotten:
+ *     loadav 1         5 seconds
+ *     loadav 5        24 seconds
+ *     loadav 10       47 seconds
+ *     loadav 20       93 seconds
+ * This is a great improvement on the previous algorithm which
+ * decayed the priorities by a constant, and decayed away all knowledge
+ * of previous activity in about 20 seconds.  Under heavy load,
+ * the previous algorithm degenerated to round-robin with poor response
+ * time when there was a high load average.
  */
  */
-#define        SCHMAG  9/10
+#undef ave
+#define        ave(a,b) ((int)(((int)(a*b))/(b+1)))
+int    nrscale = 2;
+double avenrun[];
 
 /*
  * Constant for decay filter for cpu usage field
 
 /*
  * Constant for decay filter for cpu usage field
@@ -165,25 +184,29 @@ double    ccpu = 0.95122942450071400909;          /* exp(-1/20) */
 softclock(pc, ps)
        caddr_t pc;
 {
 softclock(pc, ps)
        caddr_t pc;
 {
-       register struct callout *p1, *p2;
+       register struct callout *p1;
        register struct proc *pp;
        register int a, s;
        register struct proc *pp;
        register int a, s;
+       caddr_t arg;
+       int (*func)();
 
        /*
         * Perform callouts (but not after panic's!)
         */
 
        /*
         * Perform callouts (but not after panic's!)
         */
-       if (panicstr == 0 && callout[0].c_time <= 0) {
-               p1 = &callout[0];
-               while (p1->c_func != 0 && p1->c_time <= 0) {
-                       (*p1->c_func)(p1->c_arg);
-                       p1++;
-               }
-               p2 = &callout[0];
-               while (p2->c_func = p1->c_func) {
-                       p2->c_time = p1->c_time;
-                       p2->c_arg = p1->c_arg;
-                       p1++;
-                       p2++;
+       if (panicstr == 0) {
+               for (;;) {
+                       s = spl7();
+                       if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
+                               splx(s);
+                               break;
+                       }
+                       calltodo.c_next = p1->c_next;
+                       arg = p1->c_arg;
+                       func = p1->c_func;
+                       p1->c_next = callfree;
+                       callfree = p1;
+                       (void) splx(s);
+                       (*func)(arg);
                }
        }
 
                }
        }
 
@@ -207,10 +230,16 @@ softclock(pc, ps)
        }
 
        /*
        }
 
        /*
-        * Run paging daemon and reschedule every 1/4 sec.
+        * Run paging daemon every 1/4 sec.
         */
        if (lbolt % (hz/4) == 0) {
                vmpago();
         */
        if (lbolt % (hz/4) == 0) {
                vmpago();
+       }
+
+       /*
+        * Reschedule every 1/10 sec.
+        */
+       if (lbolt % (hz/10) == 0) {
                runrun++;
                aston();
        }
                runrun++;
                aston();
        }
@@ -307,15 +336,18 @@ softclock(pc, ps)
                         * is a weighted estimate of cpu time consumed.
                         * A process which consumes cpu time has this
                         * increase regularly.  We here decrease it by
                         * is a weighted estimate of cpu time consumed.
                         * A process which consumes cpu time has this
                         * increase regularly.  We here decrease it by
-                        * a fraction (SCHMAG is 90%), giving a digital
-                        * decay filter which damps out in about 10 seconds.
+                        * a fraction based on load average giving a digital
+                        * decay filter which damps out in about 5 seconds
+                        * when seconds are measured in time expanded by the
+                        * load average.
                         *
                         * If a process is niced, then the nice directly
                         * affects the new priority.  The final priority
                         * is in the range 0 to 255, to fit in a character.
                         */
                        pp->p_cpticks = 0;
                         *
                         * If a process is niced, then the nice directly
                         * affects the new priority.  The final priority
                         * is in the range 0 to 255, to fit in a character.
                         */
                        pp->p_cpticks = 0;
-                       a = (pp->p_cpu & 0377)*SCHMAG + pp->p_nice - NZERO;
+                       a = ave((pp->p_cpu & 0377), avenrun[0]*nrscale) +
+                            pp->p_nice - NZERO;
                        if (a < 0)
                                a = 0;
                        if (a > 255)
                        if (a < 0)
                                a = 0;
                        if (a > 255)
@@ -397,7 +429,7 @@ softclock(pc, ps)
 /*
  * Timeout is called to arrange that
  * fun(arg) is called in tim/hz seconds.
 /*
  * Timeout is called to arrange that
  * fun(arg) is called in tim/hz seconds.
- * An entry is sorted into the callout
+ * An entry is linked into the callout
  * structure.  The time in each structure
  * entry is the number of hz's more
  * than the previous entry.
  * structure.  The time in each structure
  * entry is the number of hz's more
  * than the previous entry.
@@ -412,7 +444,7 @@ timeout(fun, arg, tim)
        int (*fun)();
        caddr_t arg;
 {
        int (*fun)();
        caddr_t arg;
 {
-       register struct callout *p1, *p2, *p3;
+       register struct callout *p1, *p2, *pnew;
        register int t;
        int s;
 
        register int t;
        int s;
 
@@ -423,28 +455,19 @@ timeout(fun, arg, tim)
                panic("timeout ttrstr arg");
 /* END DEBUGGING CODE */
        t = tim;
                panic("timeout ttrstr arg");
 /* END DEBUGGING CODE */
        t = tim;
-       p1 = &callout[0];
        s = spl7();
        s = spl7();
-       while (p1->c_func != 0 && p1->c_time <= t) {
-               t -= p1->c_time;
-               p1++;
-       }
-       p1->c_time -= t;
-       p2 = p1;
-       p3 = callout+(ncallout-2);
-       while (p2->c_func != 0) {
-               if (p2 >= p3)
-                       panic("timeout");
-               p2++;
-       }
-       while (p2 >= p1) {
-               (p2+1)->c_time = p2->c_time;
-               (p2+1)->c_func = p2->c_func;
-               (p2+1)->c_arg = p2->c_arg;
-               p2--;
-       }
-       p1->c_time = t;
-       p1->c_func = fun;
-       p1->c_arg = arg;
+       pnew = callfree;
+       if (pnew == NULL)
+               panic("timeout table overflow");
+       callfree = pnew->c_next;
+       pnew->c_arg = arg;
+       pnew->c_func = fun;
+       for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
+               t -= p2->c_time;
+       p1->c_next = pnew;
+       pnew->c_next = p2;
+       pnew->c_time = t;
+       if (p2)
+               p2->c_time -= t;
        splx(s);
 }
        splx(s);
 }