fixes for fp context switching
[unix-history] / usr / src / sys / kern / kern_clock.c
index 1407207..d979a48 100644 (file)
@@ -1,27 +1,30 @@
-/*     kern_clock.c    6.1     83/07/29        */
-
-#include "../machine/reg.h"
-#include "../machine/psl.h"
+/*-
+ * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)kern_clock.c        7.16 (Berkeley) %G%
+ */
 
 
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/dk.h"
-#include "../h/callout.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/kernel.h"
-#include "../h/proc.h"
-#include "../h/vm.h"
-#include "../h/text.h"
+#include "param.h"
+#include "systm.h"
+#include "dkstat.h"
+#include "callout.h"
+#include "kernel.h"
+#include "proc.h"
+#include "resourcevar.h"
 
 
-#ifdef vax
-#include "../vax/mtpr.h"
-#endif
+#include "machine/cpu.h"
 
 #ifdef GPROF
 
 #ifdef GPROF
-#include "../h/gprof.h"
+#include "gprof.h"
 #endif
 
 #endif
 
+#define ADJTIME                /* For now... */
+#define        ADJ_TICK 1000
+int    adjtimedelta;
+
 /*
  * Clock handling routines.
  *
 /*
  * Clock handling routines.
  *
  *     allocate more timeout table slots when table overflows.
  */
 
  *     allocate more timeout table slots when table overflows.
  */
 
+/*
+ * Bump a timeval by a small number of usec's.
+ */
+#define BUMPTIME(t, usec) { \
+       register struct timeval *tp = (t); \
+ \
+       tp->tv_usec += (usec); \
+       if (tp->tv_usec >= 1000000) { \
+               tp->tv_usec -= 1000000; \
+               tp->tv_sec++; \
+       } \
+}
+
 /*
  * The hz hardware interval timer.
  * We update the events relating to real time.
  * If this timer is also being used to gather statistics,
  * we run through the statistics gathering routine as well.
  */
 /*
  * The hz hardware interval timer.
  * We update the events relating to real time.
  * If this timer is also being used to gather statistics,
  * we run through the statistics gathering routine as well.
  */
-/*ARGSUSED*/
-hardclock(pc, ps)
-       caddr_t pc;
-       int ps;
+hardclock(frame)
+       clockframe frame;
 {
        register struct callout *p1;
 {
        register struct callout *p1;
-       register struct proc *p;
-       register int s, cpstate;
-       int needsoft = 0;
+       register struct proc *p = curproc;
+       register struct pstats *pstats;
+       register int s;
 
        /*
         * Update real-time timeout queue.
 
        /*
         * Update real-time timeout queue.
@@ -73,53 +87,38 @@ hardclock(pc, ps)
        while (p1) {
                if (--p1->c_time > 0)
                        break;
        while (p1) {
                if (--p1->c_time > 0)
                        break;
-               needsoft = 1;
                if (p1->c_time == 0)
                        break;
                p1 = p1->c_next;
        }
 
                if (p1->c_time == 0)
                        break;
                p1 = p1->c_next;
        }
 
+       /*
+        * Curproc (now in p) is null if no process is running.
+        * We assume that curproc is set in user mode!
+        */
+       if (p)
+               pstats = p->p_stats;
        /*
         * Charge the time out based on the mode the cpu is in.
         * Here again we fudge for the lack of proper interval timers
         * assuming that the current state has been around at least
         * one tick.
         */
        /*
         * Charge the time out based on the mode the cpu is in.
         * Here again we fudge for the lack of proper interval timers
         * assuming that the current state has been around at least
         * one tick.
         */
-       if (USERMODE(ps)) {
-               if (u.u_prof.pr_scale)
-                       needsoft = 1;
                /*
                 * CPU was in user state.  Increment
                 * user time counter, and process process-virtual time
                 * interval timer. 
                 */
                /*
                 * CPU was in user state.  Increment
                 * user time counter, and process process-virtual time
                 * interval timer. 
                 */
-               bumptime(&u.u_ru.ru_utime, tick);
-               if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) &&
-                   itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0)
-                       psignal(u.u_procp, SIGVTALRM);
-               if (u.u_procp->p_nice > NZERO)
-                       cpstate = CP_NICE;
-               else
-                       cpstate = CP_USER;
+               BUMPTIME(&p->p_utime, tick);
+               if (timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
+                   itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
+                       psignal(p, SIGVTALRM);
        } else {
                /*
        } else {
                /*
-                * CPU was in system state.  If profiling kernel
-                * increment a counter.  If no process is running
-                * then this is a system tick if we were running
-                * at a non-zero IPL (in a driver).  If a process is running,
-                * then we charge it with system time even if we were
-                * at a non-zero IPL, since the system often runs
-                * this way during processing of system calls.
-                * This is approximate, but the lack of true interval
-                * timers makes doing anything else difficult.
+                * CPU was in system state.
                 */
                 */
-               cpstate = CP_SYS;
-               if (noproc) {
-                       if (BASEPRI(ps))
-                               cpstate = CP_IDLE;
-               } else {
-                       bumptime(&u.u_ru.ru_stime, tick);
-               }
+               if (p)
+                       BUMPTIME(&p->p_stime, tick);
        }
 
        /*
        }
 
        /*
@@ -130,50 +129,38 @@ hardclock(pc, ps)
         * This assumes that the current process has been running
         * the entire last tick.
         */
         * This assumes that the current process has been running
         * the entire last tick.
         */
-       if (noproc == 0 && cpstate != CP_IDLE) {
-               if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) >
-                   u.u_rlimit[RLIMIT_CPU].rlim_cur) {
-                       psignal(u.u_procp, SIGXCPU);
-                       if (u.u_rlimit[RLIMIT_CPU].rlim_cur <
-                           u.u_rlimit[RLIMIT_CPU].rlim_max)
-                               u.u_rlimit[RLIMIT_CPU].rlim_cur += 5;
-               }
-               if (timerisset(&u.u_timer[ITIMER_PROF].it_value) &&
-                   itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0)
-                       psignal(u.u_procp, SIGPROF);
-               s = u.u_procp->p_rssize;
-               u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0;     /* XXX */
-               if (u.u_procp->p_textp) {
-                       register int xrss = u.u_procp->p_textp->x_rssize;
-
-                       s += xrss;
-                       u.u_ru.ru_ixrss += xrss;
+       if (p) {
+               if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) >
+                   p->p_rlimit[RLIMIT_CPU].rlim_cur) {
+                       psignal(p, SIGXCPU);
+                       if (p->p_rlimit[RLIMIT_CPU].rlim_cur <
+                           p->p_rlimit[RLIMIT_CPU].rlim_max)
+                               p->p_rlimit[RLIMIT_CPU].rlim_cur += 5;
                }
                }
-               if (s > u.u_ru.ru_maxrss)
-                       u.u_ru.ru_maxrss = s;
-       }
+               if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
+                   itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
+                       psignal(p, SIGPROF);
 
 
-       /*
-        * We adjust the priority of the current process.
-        * The priority of a process gets worse as it accumulates
-        * CPU time.  The cpu usage estimator (p_cpu) is increased here
-        * and the formula for computing priorities (in kern_synch.c)
-        * will compute a different value each time the p_cpu increases
-        * by 4.  The cpu usage estimator ramps up quite quickly when
-        * the process is running (linearly), and decays away exponentially,
-        * at a rate which is proportionally slower when the system is
-        * busy.  The basic principal is that the system will 90% forget
-        * that a process used a lot of CPU time in 5*loadav seconds.
-        * This causes the system to favor processes which haven't run
-        * much recently, and to round-robin among other processes.
-        */
-       if (!noproc) {
-               p = u.u_procp;
+               /*
+                * We adjust the priority of the current process.
+                * The priority of a process gets worse as it accumulates
+                * CPU time.  The cpu usage estimator (p_cpu) is increased here
+                * and the formula for computing priorities (in kern_synch.c)
+                * will compute a different value each time the p_cpu increases
+                * by 4.  The cpu usage estimator ramps up quite quickly when
+                * the process is running (linearly), and decays away
+                * exponentially, * at a rate which is proportionally slower
+                * when the system is busy.  The basic principal is that the
+                * system will 90% forget that a process used a lot of CPU
+                * time in 5*loadav seconds.  This causes the system to favor
+                * processes which haven't run much recently, and to
+                * round-robin among other processes.
+                */
                p->p_cpticks++;
                if (++p->p_cpu == 0)
                        p->p_cpu--;
                if ((p->p_cpu&3) == 0) {
                p->p_cpticks++;
                if (++p->p_cpu == 0)
                        p->p_cpu--;
                if ((p->p_cpu&3) == 0) {
-                       (void) setpri(p);
+                       setpri(p);
                        if (p->p_pri >= PUSER)
                                p->p_pri = p->p_usrpri;
                }
                        if (p->p_pri >= PUSER)
                                p->p_pri = p->p_usrpri;
                }
@@ -184,7 +171,7 @@ hardclock(pc, ps)
         * we must gather the statistics.
         */
        if (phz == 0)
         * we must gather the statistics.
         */
        if (phz == 0)
-               gatherstats(pc, ps);
+               gatherstats(&frame);
 
        /*
         * Increment the time-of-day, and schedule
 
        /*
         * Increment the time-of-day, and schedule
@@ -192,11 +179,38 @@ hardclock(pc, ps)
         * so we don't keep the relatively high clock interrupt
         * priority any longer than necessary.
         */
         * so we don't keep the relatively high clock interrupt
         * priority any longer than necessary.
         */
-       bumptime(&time, tick);
-       if (needsoft)
-               setsoftclock();
+#ifdef ADJTIME
+       if (adjtimedelta == 0)
+               bumptime(&time, tick);
+       else {
+               if (adjtimedelta < 0) {
+                       bumptime(&time, tick-ADJ_TICK);
+                       adjtimedelta++;
+               } else {
+                       bumptime(&time, tick+ADJ_TICK);
+                       adjtimedelta--;
+               }
+       }
+#else
+       if (timedelta == 0)
+               BUMPTIME(&time, tick)
+       else {
+               register delta;
+
+               if (timedelta < 0) {
+                       delta = tick - tickdelta;
+                       timedelta += tickdelta;
+               } else {
+                       delta = tick + tickdelta;
+                       timedelta -= tickdelta;
+               }
+               BUMPTIME(&time, delta);
+       }
+#endif
+       setsoftclock();
 }
 
 }
 
+int    dk_ndrive = DK_NDRIVE;
 /*
  * Gather statistics on resource utilization.
  *
 /*
  * Gather statistics on resource utilization.
  *
@@ -205,34 +219,39 @@ hardclock(pc, ps)
  * or idle state) for the entire last time interval, and
  * update statistics accordingly.
  */
  * or idle state) for the entire last time interval, and
  * update statistics accordingly.
  */
-/*ARGSUSED*/
-gatherstats(pc, ps)
-       caddr_t pc;
-       int ps;
+gatherstats(framep)
+       clockframe *framep;
 {
 {
-       int cpstate, s;
+       register int cpstate, s;
 
        /*
         * Determine what state the cpu is in.
         */
 
        /*
         * Determine what state the cpu is in.
         */
-       if (USERMODE(ps)) {
+       if (CLKF_USERMODE(framep)) {
                /*
                 * CPU was in user state.
                 */
                /*
                 * CPU was in user state.
                 */
-               if (u.u_procp->p_nice > NZERO)
+               if (curproc->p_nice > NZERO)
                        cpstate = CP_NICE;
                else
                        cpstate = CP_USER;
        } else {
                /*
                 * CPU was in system state.  If profiling kernel
                        cpstate = CP_NICE;
                else
                        cpstate = CP_USER;
        } else {
                /*
                 * CPU was in system state.  If profiling kernel
-                * increment a counter.
+                * increment a counter.  If no process is running
+                * then this is a system tick if we were running
+                * at a non-zero IPL (in a driver).  If a process is running,
+                * then we charge it with system time even if we were
+                * at a non-zero IPL, since the system often runs
+                * this way during processing of system calls.
+                * This is approximate, but the lack of true interval
+                * timers makes doing anything else difficult.
                 */
                cpstate = CP_SYS;
                 */
                cpstate = CP_SYS;
-               if (noproc && BASEPRI(ps))
+               if (curproc == NULL && CLKF_BASEPRI(framep))
                        cpstate = CP_IDLE;
 #ifdef GPROF
                        cpstate = CP_IDLE;
 #ifdef GPROF
-               s = pc - s_lowpc;
+               s = CLKF_PC(framep) - s_lowpc;
                if (profiling < 2 && s < s_textsize)
                        kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
 #endif
                if (profiling < 2 && s < s_textsize)
                        kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
 #endif
@@ -253,9 +272,8 @@ gatherstats(pc, ps)
  * Run periodic events from timeout queue.
  */
 /*ARGSUSED*/
  * Run periodic events from timeout queue.
  */
 /*ARGSUSED*/
-softclock(pc, ps)
-       caddr_t pc;
-       int ps;
+softclock(frame)
+       clockframe frame;
 {
 
        for (;;) {
 {
 
        for (;;) {
@@ -264,7 +282,7 @@ softclock(pc, ps)
                register int (*func)();
                register int a, s;
 
                register int (*func)();
                register int a, s;
 
-               s = spl7();
+               s = splhigh();
                if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
                        splx(s);
                        break;
                if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
                        splx(s);
                        break;
@@ -280,61 +298,44 @@ softclock(pc, ps)
         * If trapped user-mode and profiling, give it
         * a profiling tick.
         */
         * If trapped user-mode and profiling, give it
         * a profiling tick.
         */
-       if (USERMODE(ps)) {
-               register struct proc *p = u.u_procp;
+       if (CLKF_USERMODE(&frame)) {
+               register struct proc *p = curproc;
 
 
-               if (u.u_prof.pr_scale) {
-                       p->p_flag |= SOWEUPC;
-                       aston();
-               }
+               if (p->p_stats->p_prof.pr_scale)
+                       profile_tick(p, &frame);
                /*
                 * Check to see if process has accumulated
                 * more than 10 minutes of user time.  If so
                 * reduce priority to give others a chance.
                 */
                /*
                 * Check to see if process has accumulated
                 * more than 10 minutes of user time.  If so
                 * reduce priority to give others a chance.
                 */
-               if (p->p_uid && p->p_nice == NZERO &&
-                   u.u_ru.ru_utime.tv_sec > 10 * 60) {
-                       p->p_nice = NZERO+4;
-                       (void) setpri(p);
+               if (p->p_ucred->cr_uid && p->p_nice == NZERO &&
+                   p->p_utime.tv_sec > 10 * 60) {
+                       p->p_nice = NZERO + 4;
+                       setpri(p);
                        p->p_pri = p->p_usrpri;
                }
        }
 }
 
 /*
                        p->p_pri = p->p_usrpri;
                }
        }
 }
 
 /*
- * Bump a timeval by a small number of usec's.
- */
-bumptime(tp, usec)
-       register struct timeval *tp;
-       int usec;
-{
-
-       tp->tv_usec += usec;
-       if (tp->tv_usec >= 1000000) {
-               tp->tv_usec -= 1000000;
-               tp->tv_sec++;
-       }
-}
-
-/*
- * Arrange that (*fun)(arg) is called in t/hz seconds.
+ * Arrange that (*func)(arg) is called in t/hz seconds.
  */
  */
-timeout(fun, arg, t)
-       int (*fun)();
+timeout(func, arg, t)
+       int (*func)();
        caddr_t arg;
        register int t;
 {
        register struct callout *p1, *p2, *pnew;
        caddr_t arg;
        register int t;
 {
        register struct callout *p1, *p2, *pnew;
-       register int s = spl7();
+       register int s = splhigh();
 
 
-       if (t == 0)
+       if (t <= 0)
                t = 1;
        pnew = callfree;
        if (pnew == NULL)
                panic("timeout table overflow");
        callfree = pnew->c_next;
        pnew->c_arg = arg;
                t = 1;
        pnew = callfree;
        if (pnew == NULL)
                panic("timeout table overflow");
        callfree = pnew->c_next;
        pnew->c_arg = arg;
-       pnew->c_func = fun;
+       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;
        for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
                if (p2->c_time > 0)
                        t -= p2->c_time;
@@ -350,16 +351,16 @@ timeout(fun, arg, t)
  * untimeout is called to remove a function timeout call
  * from the callout structure.
  */
  * untimeout is called to remove a function timeout call
  * from the callout structure.
  */
-untimeout(fun, arg)
-       int (*fun)();
+untimeout(func, arg)
+       int (*func)();
        caddr_t arg;
 {
        register struct callout *p1, *p2;
        register int s;
 
        caddr_t arg;
 {
        register struct callout *p1, *p2;
        register int s;
 
-       s = spl7();
+       s = splhigh();
        for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
        for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
-               if (p2->c_func == fun && p2->c_arg == arg) {
+               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;
                        if (p2->c_next && p2->c_time > 0)
                                p2->c_next->c_time += p2->c_time;
                        p1->c_next = p2->c_next;
@@ -381,7 +382,7 @@ hzto(tv)
 {
        register long ticks;
        register long sec;
 {
        register long ticks;
        register long sec;
-       int s = spl7();
+       int s = splhigh();
 
        /*
         * If number of milliseconds will fit in 32 bit arithmetic,
 
        /*
         * If number of milliseconds will fit in 32 bit arithmetic,
@@ -403,26 +404,3 @@ hzto(tv)
        splx(s);
        return (ticks);
 }
        splx(s);
        return (ticks);
 }
-
-profil()
-{
-       register struct a {
-               short   *bufbase;
-               unsigned bufsize;
-               unsigned pcoffset;
-               unsigned pcscale;
-       } *uap = (struct a *)u.u_ap;
-       register struct uprof *upp = &u.u_prof;
-
-       upp->pr_base = uap->bufbase;
-       upp->pr_size = uap->bufsize;
-       upp->pr_off = uap->pcoffset;
-       upp->pr_scale = uap->pcscale;
-}
-
-opause()
-{
-
-       for (;;)
-               sleep((caddr_t)&u, PSLEP);
-}