Cleanups for 4.4BSD-Lite
[unix-history] / usr / src / sys / kern / kern_clock.c
index 6031678..237cc30 100644 (file)
@@ -1,25 +1,24 @@
 /*-
 /*-
- * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1982, 1986, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)kern_clock.c        7.23 (Berkeley) %G%
+ *     @(#)kern_clock.c        8.2 (Berkeley) %G%
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "dkstat.h"
-#include "callout.h"
-#include "kernel.h"
-#include "proc.h"
-#include "resourcevar.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/dkstat.h>
+#include <sys/callout.h>
+#include <sys/kernel.h>
+#include <sys/proc.h>
+#include <sys/resourcevar.h>
 
 
-#include "machine/cpu.h"
+#include <machine/cpu.h>
 
 #ifdef GPROF
 
 #ifdef GPROF
-#include "gmon.h"
-extern u_short *kcount;
+#include <sys/gmon.h>
 #endif
 
 #define ADJTIME                /* For now... */
 #endif
 
 #define ADJTIME                /* For now... */
@@ -72,7 +71,9 @@ int   adjtimedelta;
 int    stathz;
 int    profhz;
 int    profprocs;
 int    stathz;
 int    profhz;
 int    profprocs;
-static int psratio, psdiv, pscnt;      /* prof => stat divider */
+int    ticks;
+static int psdiv, pscnt;       /* prof => stat divider */
+int    psratio;                /* ratio: prof / stat */
 
 volatile struct        timeval time;
 volatile struct        timeval mono_time;
 
 volatile struct        timeval time;
 volatile struct        timeval mono_time;
@@ -148,8 +149,10 @@ hardclock(frame)
                statclock(frame);
 
        /*
                statclock(frame);
 
        /*
-        * Increment the time-of-day.
+        * Increment the time-of-day.  The increment is just ``tick'' unless
+        * we are still adjusting the clock; see adjtime().
         */
         */
+       ticks++;
 #ifdef ADJTIME
        if (adjtimedelta == 0)
                bumptime(&time, tick);
 #ifdef ADJTIME
        if (adjtimedelta == 0)
                bumptime(&time, tick);
@@ -163,22 +166,14 @@ hardclock(frame)
                }
        }
 #else
                }
        }
 #else
-       if (timedelta == 0) {
-               BUMPTIME(&time, tick);
-               BUMPTIME(&mono_time, tick);
-       } else {
-               register int delta;
-
-               if (timedelta < 0) {
-                       delta = tick - tickdelta;
-                       timedelta += tickdelta;
-               } else {
-                       delta = tick + tickdelta;
-                       timedelta -= tickdelta;
-               }
-               BUMPTIME(&time, delta);
-               BUMPTIME(&mono_time, delta);
+       if (timedelta == 0)
+               delta = tick;
+       else {
+               delta = tick + tickdelta;
+               timedelta -= tickdelta;
        }
        }
+       BUMPTIME(&time, delta);
+       BUMPTIME(&mono_time, delta);
 
        /*
         * Process callouts at a very low cpu priority, so we don't keep the
 
        /*
         * Process callouts at a very low cpu priority, so we don't keep the
@@ -216,67 +211,87 @@ softclock()
 }
 
 /*
 }
 
 /*
- * Arrange that (*func)(arg) is called in t/hz seconds.
+ * timeout --
+ *     Execute a function after a specified length of time.
+ *
+ * untimeout --
+ *     Cancel previous timeout function call.
+ *
+ *     See AT&T BCI Driver Reference Manual for specification.  This
+ *     implementation differs from that one in that no identification
+ *     value is returned from timeout, rather, the original arguments
+ *     to timeout are used to identify entries for untimeout.
  */
 void
  */
 void
-timeout(func, arg, t)
-       void (*func) __P((void *));
+timeout(ftn, arg, ticks)
+       void (*ftn) __P((void *));
        void *arg;
        void *arg;
-       register int t;
+       register int ticks;
 {
 {
-       register struct callout *p1, *p2, *pnew;
+       register struct callout *new, *p, *t;
        register int s;
 
        register int s;
 
+       if (ticks <= 0)
+               ticks = 1;
+
+       /* Lock out the clock. */
        s = splhigh();
        s = splhigh();
-       if (t <= 0)
-               t = 1;
-       pnew = callfree;
-       if (pnew == NULL)
-               panic("timeout table overflow");
-       callfree = pnew->c_next;
-       pnew->c_arg = arg;
-       pnew->c_func = func;
-       for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
-               if (p2->c_time > 0)
-                       t -= p2->c_time;
-       p1->c_next = pnew;
-       pnew->c_next = p2;
-       pnew->c_time = t;
-       if (p2)
-               p2->c_time -= t;
+
+       /* Fill in the next free callout structure. */
+       if (callfree == NULL)
+               panic("timeout table full");
+       new = callfree;
+       callfree = new->c_next;
+       new->c_arg = arg;
+       new->c_func = ftn;
+
+       /*
+        * The time for each event is stored as a difference from the time
+        * of the previous event on the queue.  Walk the queue, correcting
+        * the ticks argument for queue entries passed.  Correct the ticks
+        * value for the queue entry immediately after the insertion point
+        * as well.
+        */
+       for (p = &calltodo;
+           (t = p->c_next) != NULL && ticks > t->c_time; p = t)
+               ticks -= t->c_time;
+       new->c_time = ticks;
+       if (t != NULL)
+               t->c_time -= ticks;
+
+       /* Insert the new entry into the queue. */
+       p->c_next = new;
+       new->c_next = t;
        splx(s);
 }
 
        splx(s);
 }
 
-/*
- * untimeout is called to remove a function timeout call
- * from the callout structure.
- */
 void
 void
-untimeout(func, arg)
-       void (*func) __P((void *));
+untimeout(ftn, arg)
+       void (*ftn) __P((void *));
        void *arg;
 {
        void *arg;
 {
-       register struct callout *p1, *p2;
+       register struct callout *p, *t;
        register int s;
 
        s = splhigh();
        register int s;
 
        s = splhigh();
-       for (p1 = &calltodo; (p2 = p1->c_next) != NULL; p1 = p2) {
-               if (p2->c_func == func && p2->c_arg == arg) {
-                       if (p2->c_next && p2->c_time > 0)
-                               p2->c_next->c_time += p2->c_time;
-                       p1->c_next = p2->c_next;
-                       p2->c_next = callfree;
-                       callfree = p2;
+       for (p = &calltodo; (t = p->c_next) != NULL; p = t)
+               if (t->c_func == ftn && t->c_arg == arg) {
+                       /* Increment next entry's tick count. */
+                       if (t->c_next && t->c_time > 0)
+                               t->c_next->c_time += t->c_time;
+
+                       /* Move entry from callout queue to callfree queue. */
+                       p->c_next = t->c_next;
+                       t->c_next = callfree;
+                       callfree = t;
                        break;
                }
                        break;
                }
-       }
        splx(s);
 }
 
 /*
        splx(s);
 }
 
 /*
- * Compute number of hz until specified time.
- * Used to compute third argument to timeout() from an
- * absolute time.
+ * Compute number of hz until specified time.  Used to
+ * compute third argument to timeout() from an absolute time.
  */
 int
 hzto(tv)
  */
 int
 hzto(tv)
@@ -389,8 +404,10 @@ statclock(frame)
                g = &_gmonparam;
                if (g->state == GMON_PROF_ON) {
                        i = CLKF_PC(frame) - g->lowpc;
                g = &_gmonparam;
                if (g->state == GMON_PROF_ON) {
                        i = CLKF_PC(frame) - g->lowpc;
-                       if (i < g->textsize)
-                               kcount[i / (HISTFRACTION * sizeof(*kcount))]++;
+                       if (i < g->textsize) {
+                               i /= HISTFRACTION * sizeof(*g->kcount);
+                               g->kcount[i]++;
+                       }
                }
 #endif
                if (--pscnt > 0)
                }
 #endif
                if (--pscnt > 0)
@@ -452,7 +469,7 @@ statclock(frame)
                if (++p->p_cpu == 0)
                        p->p_cpu--;
                if ((p->p_cpu & 3) == 0) {
                if (++p->p_cpu == 0)
                        p->p_cpu--;
                if ((p->p_cpu & 3) == 0) {
-                       setpri(p);
+                       resetpriority(p);
                        if (p->p_pri >= PUSER)
                                p->p_pri = p->p_usrpri;
                }
                        if (p->p_pri >= PUSER)
                                p->p_pri = p->p_usrpri;
                }
@@ -462,35 +479,18 @@ statclock(frame)
 /*
  * Return information about system clocks.
  */
 /*
  * Return information about system clocks.
  */
-/* ARGSUSED */
-kinfo_clockrate(op, where, acopysize, arg, aneeded)
-       int op;
+sysctl_clockrate(where, sizep)
        register char *where;
        register char *where;
-       int *acopysize, arg, *aneeded;
+       size_t *sizep;
 {
 {
-       int buflen, error;
-       struct clockinfo clockinfo;
+       struct clockinfo clkinfo;
 
 
-       *aneeded = sizeof(clockinfo);
-       if (where == NULL)
-               return (0);
-       /*
-        * Check for enough buffering.
-        */
-       buflen = *acopysize;
-       if (buflen < sizeof(clockinfo)) {
-               *acopysize = 0;
-               return (0);
-       }
        /*
        /*
-        * Copyout clockinfo structure.
+        * Construct clockinfo structure.
         */
         */
-       clockinfo.hz = hz;
-       clockinfo.tick = tick;
-       clockinfo.profhz = profhz;
-       clockinfo.stathz = stathz ? stathz : hz;
-       if (error = copyout((caddr_t)&clockinfo, where, sizeof(clockinfo)))
-               return (error);
-       *acopysize = sizeof(clockinfo);
-       return (0);
+       clkinfo.hz = hz;
+       clkinfo.tick = tick;
+       clkinfo.profhz = profhz;
+       clkinfo.stathz = stathz ? stathz : hz;
+       return (sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo)));
 }
 }