BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / kern / kern_clock.c
index b1ee724..3d2a6ac 100644 (file)
@@ -1,35 +1,47 @@
-/*
- * 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  *
  *
- *     @(#)kern_clock.c        7.10 (Berkeley) 6/30/90
+ *     @(#)kern_clock.c        7.16 (Berkeley) 5/9/91
  */
 
 #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 "resourcevar.h"
 
 
-#include "machine/reg.h"
-#include "machine/psl.h"
-
-#if defined(vax) || defined(tahoe)
-#include "machine/mtpr.h"
-#include "machine/clock.h"
-#endif
-#if defined(hp300)
-#include "machine/mtpr.h"
-#endif
-#ifdef i386
-#include "machine/frame.h"
-#include "machine/segments.h"
-#endif
+#include "machine/cpu.h"
 
 #ifdef GPROF
 #include "gprof.h"
 
 #ifdef GPROF
 #include "gprof.h"
  * 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*/
-#ifndef i386
-hardclock(pc, ps)
-       caddr_t pc;
-       int ps;
-#else
 hardclock(frame)
 hardclock(frame)
-       struct intrframe frame;
-#define        pc      frame.if_eip
-#endif
+       clockframe frame;
 {
        register struct callout *p1;
 {
        register struct callout *p1;
-       register struct proc *p = u.u_procp;
+       register struct proc *p = curproc;
+       register struct pstats *pstats;
        register int s;
        int needsoft = 0;
        extern int tickdelta;
        register int s;
        int needsoft = 0;
        extern int tickdelta;
@@ -113,18 +118,20 @@ hardclock(frame)
                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.
         */
-#ifdef i386
-       if (ISPL(frame.if_cs) == SEL_UPL) {
-#else
-       if (USERMODE(ps)) {
-#endif
-               if (u.u_prof.pr_scale)
+       if (CLKF_USERMODE(&frame)) {
+               if (pstats->p_prof.pr_scale)
                        needsoft = 1;
                /*
                 * CPU was in user state.  Increment
                        needsoft = 1;
                /*
                 * CPU was in user state.  Increment
@@ -132,14 +139,14 @@ hardclock(frame)
                 * interval timer. 
                 */
                BUMPTIME(&p->p_utime, tick);
                 * interval timer. 
                 */
                BUMPTIME(&p->p_utime, tick);
-               if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) &&
-                   itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0)
+               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.
                 */
                        psignal(p, SIGVTALRM);
        } else {
                /*
                 * CPU was in system state.
                 */
-               if (!noproc)
+               if (p)
                        BUMPTIME(&p->p_stime, tick);
        }
 
                        BUMPTIME(&p->p_stime, tick);
        }
 
@@ -151,52 +158,38 @@ hardclock(frame)
         * 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 (p) {
                if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) >
                if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) >
-                   u.u_rlimit[RLIMIT_CPU].rlim_cur) {
+                   p->p_rlimit[RLIMIT_CPU].rlim_cur) {
                        psignal(p, SIGXCPU);
                        psignal(p, 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->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)
+               if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
+                   itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
                        psignal(p, SIGPROF);
                        psignal(p, SIGPROF);
-               s = p->p_rssize;
-               u.u_ru.ru_idrss += s;
-#ifdef notdef
-               u.u_ru.ru_isrss += 0;           /* XXX (haven't got this) */
-#endif
-               if (p->p_textp) {
-                       register int xrss = p->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;
-       }
-
-       /*
-        * 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) {
+               /*
+                * 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;
                }
@@ -207,11 +200,7 @@ hardclock(frame)
         * we must gather the statistics.
         */
        if (phz == 0)
         * we must gather the statistics.
         */
        if (phz == 0)
-#ifdef i386
-               gatherstats(pc, ISPL(frame.if_cs), frame.if_ppl);
-#else
-               gatherstats(pc, ps);
-#endif
+               gatherstats(&frame);
 
        /*
         * Increment the time-of-day, and schedule
 
        /*
         * Increment the time-of-day, and schedule
@@ -234,21 +223,13 @@ hardclock(frame)
                BUMPTIME(&time, delta);
        }
        if (needsoft) {
                BUMPTIME(&time, delta);
        }
        if (needsoft) {
-#ifdef i386
-               if (frame.if_ppl == 0) {
-#else
-               if (BASEPRI(ps)) {
-#endif
+               if (CLKF_BASEPRI(&frame)) {
                        /*
                         * Save the overhead of a software interrupt;
                         * it will happen as soon as we return, so do it now.
                         */
                        (void) splsoftclock();
                        /*
                         * Save the overhead of a software interrupt;
                         * it will happen as soon as we return, so do it now.
                         */
                        (void) splsoftclock();
-#ifdef i386
                        softclock(frame);
                        softclock(frame);
-#else
-                       softclock(pc, ps);
-#endif
                } else
                        setsoftclock();
        }
                } else
                        setsoftclock();
        }
@@ -263,30 +244,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*/
-#ifdef i386
-#undef pc
-gatherstats(pc, ps, ppl)
-#else
-gatherstats(pc, ps)
-#endif
-       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.
         */
-#ifdef i386
-       if (ps == SEL_UPL) {
-#else
-       if (USERMODE(ps)) {
-#endif
+       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;
@@ -303,14 +273,10 @@ gatherstats(pc, ps)
                 * timers makes doing anything else difficult.
                 */
                cpstate = CP_SYS;
                 * timers makes doing anything else difficult.
                 */
                cpstate = CP_SYS;
-#if defined(i386)
-               if (noproc && ps == 0)
-#else
-               if (noproc && BASEPRI(ps))
-#endif
+               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
@@ -331,15 +297,8 @@ gatherstats(pc, ps)
  * Run periodic events from timeout queue.
  */
 /*ARGSUSED*/
  * Run periodic events from timeout queue.
  */
 /*ARGSUSED*/
-#ifdef i386
 softclock(frame)
 softclock(frame)
-       struct  intrframe frame;
-#define        pc      frame.if_eip
-#else
-softclock(pc, ps)
-       caddr_t pc;
-       int ps;
-#endif
+       clockframe frame;
 {
 
        for (;;) {
 {
 
        for (;;) {
@@ -364,36 +323,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.
         */
-#ifdef i386
-       if (ISPL(frame.if_cs) == SEL_UPL) {
-#else
-       if (USERMODE(ps)) {
-#endif
-               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 &&
+               if (p->p_ucred->cr_uid && p->p_nice == NZERO &&
                    p->p_utime.tv_sec > 10 * 60) {
                    p->p_utime.tv_sec > 10 * 60) {
-                       p->p_nice = NZERO+4;
-                       (void) setpri(p);
+                       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;
 {
@@ -407,7 +360,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;
@@ -423,8 +376,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;
@@ -432,7 +385,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;
@@ -476,23 +429,3 @@ hzto(tv)
        splx(s);
        return (ticks);
 }
        splx(s);
        return (ticks);
 }
-
-/* ARGSUSED */
-profil(p, uap, retval)
-       struct proc *p;
-       register struct args {
-               short   *bufbase;
-               unsigned bufsize;
-               unsigned pcoffset;
-               unsigned pcscale;
-       } *uap;
-       int *retval;
-{
-       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;
-       return (0);
-}