fixes for fp context switching
[unix-history] / usr / src / sys / kern / kern_clock.c
index c8dfcce..d979a48 100644 (file)
@@ -1,28 +1,21 @@
-/*
- * Copyright (c) 1982, 1986 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, 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)kern_clock.c        7.5 (Berkeley) %G%
+ *     @(#)kern_clock.c        7.16 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
 #include "dkstat.h"
 #include "callout.h"
  */
 
 #include "param.h"
 #include "systm.h"
 #include "dkstat.h"
 #include "callout.h"
-#include "user.h"
 #include "kernel.h"
 #include "proc.h"
 #include "kernel.h"
 #include "proc.h"
-#include "vm.h"
-#include "text.h"
-
-#include "machine/reg.h"
-#include "machine/psl.h"
+#include "resourcevar.h"
 
 
-#if defined(vax) || defined(tahoe)
-#include "machine/mtpr.h"
-#include "machine/clock.h"
-#endif
+#include "machine/cpu.h"
 
 #ifdef GPROF
 #include "gprof.h"
 
 #ifdef GPROF
 #include "gprof.h"
@@ -72,13 +65,12 @@ int adjtimedelta;
  * If this timer is also being used to gather statistics,
  * we run through the statistics gathering routine as well.
  */
  * 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 struct proc *p = curproc;
+       register struct pstats *pstats;
        register int s;
 
        /*
        register int s;
 
        /*
@@ -100,28 +92,33 @@ hardclock(pc, ps)
                p1 = p1->c_next;
        }
 
                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)) {
                /*
                 * 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);
+               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 {
                /*
                 * CPU was in system state.
                 */
        } else {
                /*
                 * CPU was in system state.
                 */
-               if (!noproc)
-                       BUMPTIME(&u.u_ru.ru_stime, tick);
+               if (p)
+                       BUMPTIME(&p->p_stime, tick);
        }
 
        /*
        }
 
        /*
@@ -132,53 +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) {
-               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 (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 (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;
-#ifdef notdef
-               u.u_ru.ru_isrss += 0;           /* XXX (haven't got this) */
-#endif
-               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 (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;
                }
@@ -189,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
@@ -237,21 +219,19 @@ int       dk_ndrive = DK_NDRIVE;
  * 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;
 {
        register int cpstate, s;
 
        /*
         * Determine what state the cpu is in.
         */
 {
        register int cpstate, s;
 
        /*
         * 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;
                        cpstate = CP_NICE;
                else
                        cpstate = CP_USER;
@@ -268,10 +248,10 @@ gatherstats(pc, ps)
                 * timers makes doing anything else difficult.
                 */
                cpstate = CP_SYS;
                 * timers makes doing anything else difficult.
                 */
                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
@@ -292,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 (;;) {
@@ -319,32 +298,30 @@ 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;
                }
        }
 }
 
 /*
- * 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;
 {
        caddr_t arg;
        register int t;
 {
@@ -358,7 +335,7 @@ timeout(fun, arg, t)
                panic("timeout table overflow");
        callfree = pnew->c_next;
        pnew->c_arg = arg;
                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;
@@ -374,8 +351,8 @@ 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;
        caddr_t arg;
 {
        register struct callout *p1, *p2;
@@ -383,7 +360,7 @@ untimeout(fun, arg)
 
        s = splhigh();
        for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
 
        s = splhigh();
        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;
@@ -427,19 +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;
-}