149594Sbostic /*- 249594Sbostic * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 349594Sbostic * Copyright (c) 1991 The Regents of the University of California. 449594Sbostic * All rights reserved. 523376Smckusick * 649594Sbostic * %sccs.include.redist.c% 749594Sbostic * 8*56895Storek * @(#)kern_synch.c 7.26 (Berkeley) 11/18/92 923376Smckusick */ 1033Sbill 1156517Sbostic #include <sys/param.h> 1256517Sbostic #include <sys/systm.h> 1356517Sbostic #include <sys/proc.h> 1456517Sbostic #include <sys/kernel.h> 1556517Sbostic #include <sys/buf.h> 1656517Sbostic #include <sys/signalvar.h> 1756517Sbostic #include <sys/resourcevar.h> 1856517Sbostic #include <sys/vmmeter.h> 1952498Smarc #ifdef KTRACE 2056517Sbostic #include <sys/ktrace.h> 2152498Smarc #endif 229756Ssam 2356517Sbostic #include <machine/cpu.h> 2445742Smckusick 2549226Skarels u_char curpri; /* usrpri of curproc */ 2652686Ssklower int lbolt; /* once a second sleep address */ 2749226Skarels 288102Sroot /* 298102Sroot * Force switch among equal priority processes every 100ms. 308102Sroot */ 3154788Storek /* ARGSUSED */ 3254788Storek void 3354788Storek roundrobin(arg) 3454788Storek void *arg; 358102Sroot { 368102Sroot 3747544Skarels need_resched(); 3854788Storek timeout(roundrobin, (void *)0, hz / 10); 398102Sroot } 408102Sroot 4132908Smckusick /* 4232908Smckusick * constants for digital decay and forget 4332908Smckusick * 90% of (p_cpu) usage in 5*loadav time 4432908Smckusick * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 4532908Smckusick * Note that, as ps(1) mentions, this can let percentages 4632908Smckusick * total over 100% (I've seen 137.9% for 3 processes). 4732908Smckusick * 4832908Smckusick * Note that hardclock updates p_cpu and p_cpticks independently. 4932908Smckusick * 5032908Smckusick * We wish to decay away 90% of p_cpu in (5 * loadavg) seconds. 5132908Smckusick * That is, the system wants to compute a value of decay such 5232908Smckusick * that the following for loop: 5332908Smckusick * for (i = 0; i < (5 * loadavg); i++) 5432908Smckusick * p_cpu *= decay; 5532908Smckusick * will compute 5632908Smckusick * p_cpu *= 0.1; 5732908Smckusick * for all values of loadavg: 5832908Smckusick * 5932908Smckusick * Mathematically this loop can be expressed by saying: 6032908Smckusick * decay ** (5 * loadavg) ~= .1 6132908Smckusick * 6232908Smckusick * The system computes decay as: 6332908Smckusick * decay = (2 * loadavg) / (2 * loadavg + 1) 6432908Smckusick * 6532908Smckusick * We wish to prove that the system's computation of decay 6632908Smckusick * will always fulfill the equation: 6732908Smckusick * decay ** (5 * loadavg) ~= .1 6832908Smckusick * 6932908Smckusick * If we compute b as: 7032908Smckusick * b = 2 * loadavg 7132908Smckusick * then 7232908Smckusick * decay = b / (b + 1) 7332908Smckusick * 7432908Smckusick * We now need to prove two things: 7532908Smckusick * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 7632908Smckusick * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 7732908Smckusick * 7832908Smckusick * Facts: 7932908Smckusick * For x close to zero, exp(x) =~ 1 + x, since 8032908Smckusick * exp(x) = 0! + x**1/1! + x**2/2! + ... . 8132908Smckusick * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 8232908Smckusick * For x close to zero, ln(1+x) =~ x, since 8332908Smckusick * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 8432908Smckusick * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 8532908Smckusick * ln(.1) =~ -2.30 8632908Smckusick * 8732908Smckusick * Proof of (1): 8832908Smckusick * Solve (factor)**(power) =~ .1 given power (5*loadav): 8932908Smckusick * solving for factor, 9032908Smckusick * ln(factor) =~ (-2.30/5*loadav), or 9147544Skarels * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 9232908Smckusick * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 9332908Smckusick * 9432908Smckusick * Proof of (2): 9532908Smckusick * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 9632908Smckusick * solving for power, 9732908Smckusick * power*ln(b/(b+1)) =~ -2.30, or 9832908Smckusick * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 9932908Smckusick * 10032908Smckusick * Actual power values for the implemented algorithm are as follows: 10132908Smckusick * loadav: 1 2 3 4 10232908Smckusick * power: 5.68 10.32 14.94 19.55 10332908Smckusick */ 10417541Skarels 10538164Smckusick /* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 10647544Skarels #define loadfactor(loadav) (2 * (loadav)) 10747544Skarels #define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 1088102Sroot 10938164Smckusick /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 11038164Smckusick fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 11138164Smckusick 1128102Sroot /* 11338164Smckusick * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 11438164Smckusick * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 11538164Smckusick * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 11638164Smckusick * 11738164Smckusick * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 11838164Smckusick * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 11938164Smckusick * 12038164Smckusick * If you dont want to bother with the faster/more-accurate formula, you 12138164Smckusick * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 12238164Smckusick * (more general) method of calculating the %age of CPU used by a process. 12338164Smckusick */ 12438164Smckusick #define CCPU_SHIFT 11 12538164Smckusick 12638164Smckusick /* 1278102Sroot * Recompute process priorities, once a second 1288102Sroot */ 12954788Storek /* ARGSUSED */ 13054788Storek void 13154788Storek schedcpu(arg) 13254788Storek void *arg; 1338102Sroot { 13452667Smckusick register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 1358102Sroot register struct proc *p; 13647544Skarels register int s; 13747544Skarels register unsigned int newcpu; 1388102Sroot 1398102Sroot wakeup((caddr_t)&lbolt); 14054788Storek for (p = (struct proc *)allproc; p != NULL; p = p->p_nxt) { 14147544Skarels /* 14247544Skarels * Increment time in/out of memory and sleep time 14347544Skarels * (if sleeping). We ignore overflow; with 16-bit int's 14447544Skarels * (remember them?) overflow takes 45 days. 14547544Skarels */ 14647544Skarels p->p_time++; 14747544Skarels if (p->p_stat == SSLEEP || p->p_stat == SSTOP) 14847544Skarels p->p_slptime++; 14938164Smckusick p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT; 15017541Skarels /* 15117541Skarels * If the process has slept the entire second, 15217541Skarels * stop recalculating its priority until it wakes up. 15317541Skarels */ 15438164Smckusick if (p->p_slptime > 1) 15517541Skarels continue; 156*56895Storek s = splstatclock(); /* prevent state changes */ 15717541Skarels /* 15817541Skarels * p_pctcpu is only for ps. 15917541Skarels */ 16038164Smckusick #if (FSHIFT >= CCPU_SHIFT) 16138164Smckusick p->p_pctcpu += (hz == 100)? 16238164Smckusick ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT): 16338164Smckusick 100 * (((fixpt_t) p->p_cpticks) 16438164Smckusick << (FSHIFT - CCPU_SHIFT)) / hz; 16538164Smckusick #else 16638164Smckusick p->p_pctcpu += ((FSCALE - ccpu) * 16738164Smckusick (p->p_cpticks * FSCALE / hz)) >> FSHIFT; 16838164Smckusick #endif 1698102Sroot p->p_cpticks = 0; 17047544Skarels newcpu = (u_int) decay_cpu(loadfac, p->p_cpu) + p->p_nice; 17147544Skarels p->p_cpu = min(newcpu, UCHAR_MAX); 17247544Skarels setpri(p); 1738102Sroot if (p->p_pri >= PUSER) { 17447544Skarels #define PPQ (128 / NQS) /* priorities per queue */ 17549095Skarels if ((p != curproc) && 1768102Sroot p->p_stat == SRUN && 1778102Sroot (p->p_flag & SLOAD) && 17816795Skarels (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) { 1798102Sroot remrq(p); 1808102Sroot p->p_pri = p->p_usrpri; 1818102Sroot setrq(p); 1828102Sroot } else 1838102Sroot p->p_pri = p->p_usrpri; 1848102Sroot } 1858102Sroot splx(s); 1868102Sroot } 1878102Sroot vmmeter(); 1888102Sroot if (bclnlist != NULL) 18947544Skarels wakeup((caddr_t)pageproc); 19054788Storek timeout(schedcpu, (void *)0, hz); 1918102Sroot } 1928102Sroot 19317541Skarels /* 19417541Skarels * Recalculate the priority of a process after it has slept for a while. 19547544Skarels * For all load averages >= 1 and max p_cpu of 255, sleeping for at least 19647544Skarels * six times the loadfactor will decay p_cpu to zero. 19717541Skarels */ 19854788Storek void 19917541Skarels updatepri(p) 20017541Skarels register struct proc *p; 20117541Skarels { 20247544Skarels register unsigned int newcpu = p->p_cpu; 20352667Smckusick register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 20417541Skarels 20547544Skarels if (p->p_slptime > 5 * loadfac) 20647544Skarels p->p_cpu = 0; 20747544Skarels else { 20847544Skarels p->p_slptime--; /* the first time was done in schedcpu */ 20947544Skarels while (newcpu && --p->p_slptime) 21047544Skarels newcpu = (int) decay_cpu(loadfac, newcpu); 21147544Skarels p->p_cpu = min(newcpu, UCHAR_MAX); 21247544Skarels } 21347544Skarels setpri(p); 21417541Skarels } 21517541Skarels 21633Sbill #define SQSIZE 0100 /* Must be power of 2 */ 21733Sbill #define HASH(x) (( (int) x >> 5) & (SQSIZE-1)) 21821099Smckusick struct slpque { 21921099Smckusick struct proc *sq_head; 22021099Smckusick struct proc **sq_tailp; 22121099Smckusick } slpque[SQSIZE]; 22233Sbill 22333Sbill /* 22445671Skarels * During autoconfiguration or after a panic, a sleep will simply 22545671Skarels * lower the priority briefly to allow interrupts, then return. 22645671Skarels * The priority to be used (safepri) is machine-dependent, thus this 22745671Skarels * value is initialized and maintained in the machine-dependent layers. 22845671Skarels * This priority will typically be 0, or the lowest priority 22945671Skarels * that is safe for use on the interrupt stack; it can be made 23045671Skarels * higher to block network software interrupts after panics. 23145671Skarels */ 23245671Skarels int safepri; 23345671Skarels 23445671Skarels /* 23540711Skarels * General sleep call. 23640711Skarels * Suspends current process until a wakeup is made on chan. 23740711Skarels * The process will then be made runnable with priority pri. 23840711Skarels * Sleeps at most timo/hz seconds (0 means no timeout). 23940711Skarels * If pri includes PCATCH flag, signals are checked 24040711Skarels * before and after sleeping, else signals are not checked. 24140711Skarels * Returns 0 if awakened, EWOULDBLOCK if the timeout expires. 24240711Skarels * If PCATCH is set and a signal needs to be delivered, 24340711Skarels * ERESTART is returned if the current system call should be restarted 24440711Skarels * if possible, and EINTR is returned if the system call should 24540711Skarels * be interrupted by the signal (return EINTR). 24633Sbill */ 24754788Storek int 24840711Skarels tsleep(chan, pri, wmesg, timo) 24952689Sbostic void *chan; 25040710Smarc int pri; 25140710Smarc char *wmesg; 25240710Smarc int timo; 25340710Smarc { 25449095Skarels register struct proc *p = curproc; 25540710Smarc register struct slpque *qp; 25640710Smarc register s; 25740711Skarels int sig, catch = pri & PCATCH; 25840710Smarc extern int cold; 25954788Storek void endtsleep __P((void *)); 26040710Smarc 26152498Smarc #ifdef KTRACE 26252498Smarc if (KTRPOINT(p, KTR_CSW)) 26352498Smarc ktrcsw(p->p_tracep, 1, 0); 26452498Smarc #endif 26540710Smarc s = splhigh(); 26640710Smarc if (cold || panicstr) { 26740710Smarc /* 26840710Smarc * After a panic, or during autoconfiguration, 26940710Smarc * just give interrupts a chance, then just return; 27040710Smarc * don't run any other procs or panic below, 27140710Smarc * in case this is the idle process and already asleep. 27240710Smarc */ 27345671Skarels splx(safepri); 27440710Smarc splx(s); 27540710Smarc return (0); 27640710Smarc } 27740710Smarc #ifdef DIAGNOSTIC 27852689Sbostic if (chan == NULL || p->p_stat != SRUN || p->p_rlink) 27940711Skarels panic("tsleep"); 28040710Smarc #endif 28147544Skarels p->p_wchan = chan; 28247544Skarels p->p_wmesg = wmesg; 28347544Skarels p->p_slptime = 0; 28447544Skarels p->p_pri = pri & PRIMASK; 28540710Smarc qp = &slpque[HASH(chan)]; 28640710Smarc if (qp->sq_head == 0) 28747544Skarels qp->sq_head = p; 28840710Smarc else 28947544Skarels *qp->sq_tailp = p; 29047544Skarels *(qp->sq_tailp = &p->p_link) = 0; 29145671Skarels if (timo) 29254788Storek timeout(endtsleep, (void *)p, timo); 29340710Smarc /* 29447544Skarels * We put ourselves on the sleep queue and start our timeout 29547544Skarels * before calling CURSIG, as we could stop there, and a wakeup 29647544Skarels * or a SIGCONT (or both) could occur while we were stopped. 29745671Skarels * A SIGCONT would cause us to be marked as SSLEEP 29845671Skarels * without resuming us, thus we must be ready for sleep 29945671Skarels * when CURSIG is called. If the wakeup happens while we're 30047544Skarels * stopped, p->p_wchan will be 0 upon return from CURSIG. 30140710Smarc */ 30240711Skarels if (catch) { 30347544Skarels p->p_flag |= SSINTR; 30447544Skarels if (sig = CURSIG(p)) { 30547544Skarels if (p->p_wchan) 30647544Skarels unsleep(p); 30747544Skarels p->p_stat = SRUN; 30845671Skarels goto resume; 30940711Skarels } 31047544Skarels if (p->p_wchan == 0) { 31145671Skarels catch = 0; 31245671Skarels goto resume; 31340711Skarels } 31452499Storek } else 31552499Storek sig = 0; 31647544Skarels p->p_stat = SSLEEP; 31747544Skarels p->p_stats->p_ru.ru_nvcsw++; 31840710Smarc swtch(); 31945671Skarels resume: 32047544Skarels curpri = p->p_usrpri; 32140710Smarc splx(s); 32247544Skarels p->p_flag &= ~SSINTR; 32347544Skarels if (p->p_flag & STIMO) { 32447544Skarels p->p_flag &= ~STIMO; 32552499Storek if (sig == 0) { 32652498Smarc #ifdef KTRACE 32752498Smarc if (KTRPOINT(p, KTR_CSW)) 32852498Smarc ktrcsw(p->p_tracep, 0, 0); 32952498Smarc #endif 33045671Skarels return (EWOULDBLOCK); 33152498Smarc } 33245671Skarels } else if (timo) 33354788Storek untimeout(endtsleep, (void *)p); 33447544Skarels if (catch && (sig != 0 || (sig = CURSIG(p)))) { 33552498Smarc #ifdef KTRACE 33652498Smarc if (KTRPOINT(p, KTR_CSW)) 33752498Smarc ktrcsw(p->p_tracep, 0, 0); 33852498Smarc #endif 33947544Skarels if (p->p_sigacts->ps_sigintr & sigmask(sig)) 34040711Skarels return (EINTR); 34140711Skarels return (ERESTART); 34240711Skarels } 34352498Smarc #ifdef KTRACE 34452498Smarc if (KTRPOINT(p, KTR_CSW)) 34552498Smarc ktrcsw(p->p_tracep, 0, 0); 34652498Smarc #endif 34740710Smarc return (0); 34840710Smarc } 34940710Smarc 35040710Smarc /* 35140710Smarc * Implement timeout for tsleep. 35240710Smarc * If process hasn't been awakened (wchan non-zero), 35340710Smarc * set timeout flag and undo the sleep. If proc 35440710Smarc * is stopped, just unsleep so it will remain stopped. 35540710Smarc */ 35654788Storek void 35754788Storek endtsleep(arg) 35854788Storek void *arg; 35954788Storek { 36040710Smarc register struct proc *p; 36154788Storek int s; 36240710Smarc 36354788Storek p = (struct proc *)arg; 36454788Storek s = splhigh(); 36540710Smarc if (p->p_wchan) { 36640710Smarc if (p->p_stat == SSLEEP) 36740710Smarc setrun(p); 36840710Smarc else 36940710Smarc unsleep(p); 37040710Smarc p->p_flag |= STIMO; 37140710Smarc } 37240710Smarc splx(s); 37340710Smarc } 37440710Smarc 37540711Skarels /* 37640711Skarels * Short-term, non-interruptable sleep. 37740711Skarels */ 37854788Storek void 37933Sbill sleep(chan, pri) 38052689Sbostic void *chan; 3818033Sroot int pri; 38233Sbill { 38349095Skarels register struct proc *p = curproc; 38421099Smckusick register struct slpque *qp; 385207Sbill register s; 38630532Skarels extern int cold; 38733Sbill 38840711Skarels #ifdef DIAGNOSTIC 38940711Skarels if (pri > PZERO) { 39040711Skarels printf("sleep called with pri %d > PZERO, wchan: %x\n", 39152689Sbostic pri, chan); 39240711Skarels panic("old sleep"); 39340711Skarels } 39440711Skarels #endif 39517541Skarels s = splhigh(); 39630532Skarels if (cold || panicstr) { 39718363Skarels /* 39830532Skarels * After a panic, or during autoconfiguration, 39930532Skarels * just give interrupts a chance, then just return; 40030532Skarels * don't run any other procs or panic below, 40130532Skarels * in case this is the idle process and already asleep. 40218363Skarels */ 40345671Skarels splx(safepri); 40418363Skarels splx(s); 40518363Skarels return; 40618363Skarels } 40740710Smarc #ifdef DIAGNOSTIC 40852689Sbostic if (chan == NULL || p->p_stat != SRUN || p->p_rlink) 40933Sbill panic("sleep"); 41040710Smarc #endif 41147544Skarels p->p_wchan = chan; 41247544Skarels p->p_wmesg = NULL; 41347544Skarels p->p_slptime = 0; 41447544Skarels p->p_pri = pri; 41521099Smckusick qp = &slpque[HASH(chan)]; 41621099Smckusick if (qp->sq_head == 0) 41747544Skarels qp->sq_head = p; 41821099Smckusick else 41947544Skarels *qp->sq_tailp = p; 42047544Skarels *(qp->sq_tailp = &p->p_link) = 0; 42147544Skarels p->p_stat = SSLEEP; 42247544Skarels p->p_stats->p_ru.ru_nvcsw++; 42352498Smarc #ifdef KTRACE 42452498Smarc if (KTRPOINT(p, KTR_CSW)) 42552498Smarc ktrcsw(p->p_tracep, 1, 0); 42652498Smarc #endif 42740711Skarels swtch(); 42852498Smarc #ifdef KTRACE 42952498Smarc if (KTRPOINT(p, KTR_CSW)) 43052498Smarc ktrcsw(p->p_tracep, 0, 0); 43152498Smarc #endif 43247544Skarels curpri = p->p_usrpri; 43333Sbill splx(s); 43433Sbill } 43533Sbill 43633Sbill /* 437181Sbill * Remove a process from its wait queue 438181Sbill */ 43954788Storek void 440181Sbill unsleep(p) 4414826Swnj register struct proc *p; 442181Sbill { 44321099Smckusick register struct slpque *qp; 444181Sbill register struct proc **hp; 44521099Smckusick int s; 446181Sbill 44717541Skarels s = splhigh(); 448181Sbill if (p->p_wchan) { 44921099Smckusick hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head; 450181Sbill while (*hp != p) 451181Sbill hp = &(*hp)->p_link; 452181Sbill *hp = p->p_link; 45321099Smckusick if (qp->sq_tailp == &p->p_link) 45421099Smckusick qp->sq_tailp = hp; 455181Sbill p->p_wchan = 0; 456181Sbill } 457181Sbill splx(s); 458181Sbill } 459181Sbill 460181Sbill /* 46147544Skarels * Wakeup on "chan"; set all processes 46247544Skarels * sleeping on chan to run state. 46333Sbill */ 46454788Storek void 46533Sbill wakeup(chan) 46652689Sbostic register void *chan; 46733Sbill { 46821099Smckusick register struct slpque *qp; 46921099Smckusick register struct proc *p, **q; 47033Sbill int s; 47133Sbill 47217541Skarels s = splhigh(); 47321099Smckusick qp = &slpque[HASH(chan)]; 47433Sbill restart: 47521099Smckusick for (q = &qp->sq_head; p = *q; ) { 47640710Smarc #ifdef DIAGNOSTIC 477181Sbill if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 47833Sbill panic("wakeup"); 47940710Smarc #endif 48047544Skarels if (p->p_wchan == chan) { 48133Sbill p->p_wchan = 0; 482187Sbill *q = p->p_link; 48321099Smckusick if (qp->sq_tailp == &p->p_link) 48421099Smckusick qp->sq_tailp = q; 485181Sbill if (p->p_stat == SSLEEP) { 486181Sbill /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 48721763Skarels if (p->p_slptime > 1) 48821763Skarels updatepri(p); 48947544Skarels p->p_slptime = 0; 490181Sbill p->p_stat = SRUN; 4912702Swnj if (p->p_flag & SLOAD) 492181Sbill setrq(p); 49316795Skarels /* 49416795Skarels * Since curpri is a usrpri, 49516795Skarels * p->p_pri is always better than curpri. 49616795Skarels */ 49747544Skarels if ((p->p_flag&SLOAD) == 0) 49847544Skarels wakeup((caddr_t)&proc0); 49947544Skarels else 50047544Skarels need_resched(); 501181Sbill /* END INLINE EXPANSION */ 502187Sbill goto restart; 50333Sbill } 504187Sbill } else 505187Sbill q = &p->p_link; 50633Sbill } 50733Sbill splx(s); 50833Sbill } 50933Sbill 51033Sbill /* 51154788Storek * The machine independent parts of swtch(). 51254788Storek * Must be called at splstatclock() or higher. 51354788Storek */ 51454788Storek void 51554788Storek swtch() 51654788Storek { 51754788Storek register struct proc *p = curproc; /* XXX */ 51854788Storek register struct rlimit *rlim; 51954788Storek register long s, u; 52054788Storek struct timeval tv; 52154788Storek 52254788Storek /* 52354788Storek * Compute the amount of time during which the current 52454788Storek * process was running, and add that to its total so far. 52554788Storek */ 52654788Storek microtime(&tv); 52754788Storek u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec); 52854788Storek s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec); 52954788Storek if (u < 0) { 53054788Storek u += 1000000; 53154788Storek s--; 53254788Storek } else if (u >= 1000000) { 53354788Storek u -= 1000000; 53454788Storek s++; 53554788Storek } 53654788Storek p->p_rtime.tv_usec = u; 53754788Storek p->p_rtime.tv_sec = s; 53854788Storek 53954788Storek /* 54054788Storek * Check if the process exceeds its cpu resource allocation. 54154788Storek * If over max, kill it. In any case, if it has run for more 54254788Storek * than 10 minutes, reduce priority to give others a chance. 54354788Storek */ 54454788Storek rlim = &p->p_rlimit[RLIMIT_CPU]; 54554788Storek if (s >= rlim->rlim_cur) { 54654788Storek if (s >= rlim->rlim_max) 54754788Storek psignal(p, SIGKILL); 54854788Storek else { 54954788Storek psignal(p, SIGXCPU); 55054788Storek if (rlim->rlim_cur < rlim->rlim_max) 55154788Storek rlim->rlim_cur += 5; 55254788Storek } 55354788Storek } 55454788Storek if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) { 55554788Storek p->p_nice = NZERO + 4; 55654788Storek setpri(p); 55754788Storek } 55854788Storek 55954788Storek /* 56054788Storek * Pick a new current process and record its start time. 56154788Storek */ 56254788Storek cnt.v_swtch++; 56354788Storek cpu_swtch(p); 56454788Storek microtime(&runtime); 56554788Storek } 56654788Storek 56754788Storek /* 56833Sbill * Initialize the (doubly-linked) run queues 56933Sbill * to be empty. 57033Sbill */ 57133Sbill rqinit() 57233Sbill { 57333Sbill register int i; 57433Sbill 57533Sbill for (i = 0; i < NQS; i++) 57633Sbill qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 57733Sbill } 57833Sbill 57933Sbill /* 58047544Skarels * Change process state to be runnable, 58147544Skarels * placing it on the run queue if it is in memory, 58247544Skarels * and awakening the swapper if it isn't in memory. 58333Sbill */ 58454788Storek void 58533Sbill setrun(p) 5864826Swnj register struct proc *p; 58733Sbill { 5884826Swnj register int s; 58933Sbill 59017541Skarels s = splhigh(); 59133Sbill switch (p->p_stat) { 59233Sbill 59333Sbill case 0: 59433Sbill case SWAIT: 59533Sbill case SRUN: 59633Sbill case SZOMB: 59733Sbill default: 59833Sbill panic("setrun"); 59933Sbill 600207Sbill case SSTOP: 60133Sbill case SSLEEP: 602181Sbill unsleep(p); /* e.g. when sending signals */ 60333Sbill break; 60433Sbill 60533Sbill case SIDL: 60633Sbill break; 60733Sbill } 60833Sbill p->p_stat = SRUN; 60933Sbill if (p->p_flag & SLOAD) 61033Sbill setrq(p); 61133Sbill splx(s); 61230232Skarels if (p->p_slptime > 1) 61330232Skarels updatepri(p); 61447544Skarels p->p_slptime = 0; 61547544Skarels if ((p->p_flag&SLOAD) == 0) 61647544Skarels wakeup((caddr_t)&proc0); 61747544Skarels else if (p->p_pri < curpri) 61847544Skarels need_resched(); 61933Sbill } 62033Sbill 62133Sbill /* 62247544Skarels * Compute priority of process when running in user mode. 62347544Skarels * Arrange to reschedule if the resulting priority 62447544Skarels * is better than that of the current process. 62533Sbill */ 62654788Storek void 62747544Skarels setpri(p) 62847544Skarels register struct proc *p; 62933Sbill { 63047544Skarels register unsigned int newpri; 63133Sbill 63247544Skarels newpri = PUSER + p->p_cpu / 4 + 2 * p->p_nice; 63347544Skarels newpri = min(newpri, MAXPRI); 63447544Skarels p->p_usrpri = newpri; 63547544Skarels if (newpri < curpri) 63647544Skarels need_resched(); 63733Sbill } 638