149594Sbostic /*- 263176Sbostic * Copyright (c) 1982, 1986, 1990, 1991, 1993 363176Sbostic * The Regents of the University of California. All rights reserved. 449594Sbostic * All rights reserved. 523376Smckusick * 649594Sbostic * %sccs.include.redist.c% 749594Sbostic * 8*64533Sbostic * @(#)kern_synch.c 8.3 (Berkeley) 09/21/93 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 2564413Sbostic u_char curpriority; /* 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(); 3864413Sbostic timeout(roundrobin, NULL, 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 /* 12764413Sbostic * Recompute process priorities, every hz ticks. 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; 15656895Storek 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); 17264413Sbostic resetpriority(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; 181*64533Sbostic setrunqueue(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 } 21364413Sbostic resetpriority(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 /* 23564413Sbostic * General sleep call. Suspends the current process until a wakeup is 23664413Sbostic * performed on the specified identifier. The process will then be made 23764413Sbostic * runnable with the specified priority. Sleeps at most timo/hz seconds 23864413Sbostic * (0 means no timeout). If pri includes PCATCH flag, signals are checked 23964413Sbostic * before and after sleeping, else signals are not checked. Returns 0 if 24064413Sbostic * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 24164413Sbostic * signal needs to be delivered, ERESTART is returned if the current system 24264413Sbostic * call should be restarted if possible, and EINTR is returned if the system 24364413Sbostic * call should be interrupted by the signal (return EINTR). 24433Sbill */ 24554788Storek int 24664413Sbostic tsleep(ident, priority, wmesg, timo) 24764413Sbostic void *ident; 24864413Sbostic int priority, timo; 24940710Smarc char *wmesg; 25040710Smarc { 25149095Skarels register struct proc *p = curproc; 25240710Smarc register struct slpque *qp; 25340710Smarc register s; 25464413Sbostic int sig, catch = priority & PCATCH; 25540710Smarc extern int cold; 25654788Storek void endtsleep __P((void *)); 25740710Smarc 25852498Smarc #ifdef KTRACE 25952498Smarc if (KTRPOINT(p, KTR_CSW)) 26052498Smarc ktrcsw(p->p_tracep, 1, 0); 26152498Smarc #endif 26240710Smarc s = splhigh(); 26340710Smarc if (cold || panicstr) { 26440710Smarc /* 26540710Smarc * After a panic, or during autoconfiguration, 26640710Smarc * just give interrupts a chance, then just return; 26740710Smarc * don't run any other procs or panic below, 26840710Smarc * in case this is the idle process and already asleep. 26940710Smarc */ 27045671Skarels splx(safepri); 27140710Smarc splx(s); 27240710Smarc return (0); 27340710Smarc } 27440710Smarc #ifdef DIAGNOSTIC 27564413Sbostic if (ident == NULL || p->p_stat != SRUN || p->p_rlink) 27640711Skarels panic("tsleep"); 27740710Smarc #endif 27864413Sbostic p->p_wchan = ident; 27947544Skarels p->p_wmesg = wmesg; 28047544Skarels p->p_slptime = 0; 28164413Sbostic p->p_pri = priority & PRIMASK; 28264413Sbostic qp = &slpque[HASH(ident)]; 28340710Smarc if (qp->sq_head == 0) 28447544Skarels qp->sq_head = p; 28540710Smarc else 28647544Skarels *qp->sq_tailp = p; 28747544Skarels *(qp->sq_tailp = &p->p_link) = 0; 28845671Skarels if (timo) 28954788Storek timeout(endtsleep, (void *)p, timo); 29040710Smarc /* 29147544Skarels * We put ourselves on the sleep queue and start our timeout 29247544Skarels * before calling CURSIG, as we could stop there, and a wakeup 29347544Skarels * or a SIGCONT (or both) could occur while we were stopped. 29445671Skarels * A SIGCONT would cause us to be marked as SSLEEP 29545671Skarels * without resuming us, thus we must be ready for sleep 29645671Skarels * when CURSIG is called. If the wakeup happens while we're 29747544Skarels * stopped, p->p_wchan will be 0 upon return from CURSIG. 29840710Smarc */ 29940711Skarels if (catch) { 30047544Skarels p->p_flag |= SSINTR; 30147544Skarels if (sig = CURSIG(p)) { 30247544Skarels if (p->p_wchan) 30347544Skarels unsleep(p); 30447544Skarels p->p_stat = SRUN; 30545671Skarels goto resume; 30640711Skarels } 30747544Skarels if (p->p_wchan == 0) { 30845671Skarels catch = 0; 30945671Skarels goto resume; 31040711Skarels } 31152499Storek } else 31252499Storek sig = 0; 31347544Skarels p->p_stat = SSLEEP; 31447544Skarels p->p_stats->p_ru.ru_nvcsw++; 31540710Smarc swtch(); 31645671Skarels resume: 31764413Sbostic curpriority = p->p_usrpri; 31840710Smarc splx(s); 31947544Skarels p->p_flag &= ~SSINTR; 32047544Skarels if (p->p_flag & STIMO) { 32147544Skarels p->p_flag &= ~STIMO; 32252499Storek if (sig == 0) { 32352498Smarc #ifdef KTRACE 32452498Smarc if (KTRPOINT(p, KTR_CSW)) 32552498Smarc ktrcsw(p->p_tracep, 0, 0); 32652498Smarc #endif 32745671Skarels return (EWOULDBLOCK); 32852498Smarc } 32945671Skarels } else if (timo) 33054788Storek untimeout(endtsleep, (void *)p); 33147544Skarels if (catch && (sig != 0 || (sig = CURSIG(p)))) { 33252498Smarc #ifdef KTRACE 33352498Smarc if (KTRPOINT(p, KTR_CSW)) 33452498Smarc ktrcsw(p->p_tracep, 0, 0); 33552498Smarc #endif 33647544Skarels if (p->p_sigacts->ps_sigintr & sigmask(sig)) 33740711Skarels return (EINTR); 33840711Skarels return (ERESTART); 33940711Skarels } 34052498Smarc #ifdef KTRACE 34152498Smarc if (KTRPOINT(p, KTR_CSW)) 34252498Smarc ktrcsw(p->p_tracep, 0, 0); 34352498Smarc #endif 34440710Smarc return (0); 34540710Smarc } 34640710Smarc 34740710Smarc /* 34840710Smarc * Implement timeout for tsleep. 34940710Smarc * If process hasn't been awakened (wchan non-zero), 35040710Smarc * set timeout flag and undo the sleep. If proc 35140710Smarc * is stopped, just unsleep so it will remain stopped. 35240710Smarc */ 35354788Storek void 35454788Storek endtsleep(arg) 35554788Storek void *arg; 35654788Storek { 35740710Smarc register struct proc *p; 35854788Storek int s; 35940710Smarc 36054788Storek p = (struct proc *)arg; 36154788Storek s = splhigh(); 36240710Smarc if (p->p_wchan) { 36340710Smarc if (p->p_stat == SSLEEP) 364*64533Sbostic setrunnable(p); 36540710Smarc else 36640710Smarc unsleep(p); 36740710Smarc p->p_flag |= STIMO; 36840710Smarc } 36940710Smarc splx(s); 37040710Smarc } 37140710Smarc 37240711Skarels /* 37340711Skarels * Short-term, non-interruptable sleep. 37440711Skarels */ 37554788Storek void 37664413Sbostic sleep(ident, priority) 37764413Sbostic void *ident; 37864413Sbostic int priority; 37933Sbill { 38049095Skarels register struct proc *p = curproc; 38121099Smckusick register struct slpque *qp; 382207Sbill register s; 38330532Skarels extern int cold; 38433Sbill 38540711Skarels #ifdef DIAGNOSTIC 38664413Sbostic if (priority > PZERO) { 38764413Sbostic printf("sleep called with priority %d > PZERO, wchan: %x\n", 38864413Sbostic priority, ident); 38940711Skarels panic("old sleep"); 39040711Skarels } 39140711Skarels #endif 39217541Skarels s = splhigh(); 39330532Skarels if (cold || panicstr) { 39418363Skarels /* 39530532Skarels * After a panic, or during autoconfiguration, 39630532Skarels * just give interrupts a chance, then just return; 39730532Skarels * don't run any other procs or panic below, 39830532Skarels * in case this is the idle process and already asleep. 39918363Skarels */ 40045671Skarels splx(safepri); 40118363Skarels splx(s); 40218363Skarels return; 40318363Skarels } 40440710Smarc #ifdef DIAGNOSTIC 40564413Sbostic if (ident == NULL || p->p_stat != SRUN || p->p_rlink) 40633Sbill panic("sleep"); 40740710Smarc #endif 40864413Sbostic p->p_wchan = ident; 40947544Skarels p->p_wmesg = NULL; 41047544Skarels p->p_slptime = 0; 41164413Sbostic p->p_pri = priority; 41264413Sbostic qp = &slpque[HASH(ident)]; 41321099Smckusick if (qp->sq_head == 0) 41447544Skarels qp->sq_head = p; 41521099Smckusick else 41647544Skarels *qp->sq_tailp = p; 41747544Skarels *(qp->sq_tailp = &p->p_link) = 0; 41847544Skarels p->p_stat = SSLEEP; 41947544Skarels p->p_stats->p_ru.ru_nvcsw++; 42052498Smarc #ifdef KTRACE 42152498Smarc if (KTRPOINT(p, KTR_CSW)) 42252498Smarc ktrcsw(p->p_tracep, 1, 0); 42352498Smarc #endif 42440711Skarels swtch(); 42552498Smarc #ifdef KTRACE 42652498Smarc if (KTRPOINT(p, KTR_CSW)) 42752498Smarc ktrcsw(p->p_tracep, 0, 0); 42852498Smarc #endif 42964413Sbostic curpriority = p->p_usrpri; 43033Sbill splx(s); 43133Sbill } 43233Sbill 43333Sbill /* 434181Sbill * Remove a process from its wait queue 435181Sbill */ 43654788Storek void 437181Sbill unsleep(p) 4384826Swnj register struct proc *p; 439181Sbill { 44021099Smckusick register struct slpque *qp; 441181Sbill register struct proc **hp; 44221099Smckusick int s; 443181Sbill 44417541Skarels s = splhigh(); 445181Sbill if (p->p_wchan) { 44621099Smckusick hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head; 447181Sbill while (*hp != p) 448181Sbill hp = &(*hp)->p_link; 449181Sbill *hp = p->p_link; 45021099Smckusick if (qp->sq_tailp == &p->p_link) 45121099Smckusick qp->sq_tailp = hp; 452181Sbill p->p_wchan = 0; 453181Sbill } 454181Sbill splx(s); 455181Sbill } 456181Sbill 457181Sbill /* 45864413Sbostic * Make all processes sleeping on the specified identifier runnable. 45933Sbill */ 46054788Storek void 46164413Sbostic wakeup(ident) 46264413Sbostic register void *ident; 46333Sbill { 46421099Smckusick register struct slpque *qp; 46521099Smckusick register struct proc *p, **q; 46633Sbill int s; 46733Sbill 46817541Skarels s = splhigh(); 46964413Sbostic qp = &slpque[HASH(ident)]; 47033Sbill restart: 47121099Smckusick for (q = &qp->sq_head; p = *q; ) { 47240710Smarc #ifdef DIAGNOSTIC 473181Sbill if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 47433Sbill panic("wakeup"); 47540710Smarc #endif 47664413Sbostic if (p->p_wchan == ident) { 47733Sbill p->p_wchan = 0; 478187Sbill *q = p->p_link; 47921099Smckusick if (qp->sq_tailp == &p->p_link) 48021099Smckusick qp->sq_tailp = q; 481181Sbill if (p->p_stat == SSLEEP) { 482*64533Sbostic /* OPTIMIZED EXPANSION OF setrunnable(p); */ 48321763Skarels if (p->p_slptime > 1) 48421763Skarels updatepri(p); 48547544Skarels p->p_slptime = 0; 486181Sbill p->p_stat = SRUN; 4872702Swnj if (p->p_flag & SLOAD) 488*64533Sbostic setrunqueue(p); 48916795Skarels /* 49064413Sbostic * Since curpriority is a user priority, 49164413Sbostic * p->p_pri is always better than curpriority. 49216795Skarels */ 49347544Skarels if ((p->p_flag&SLOAD) == 0) 49447544Skarels wakeup((caddr_t)&proc0); 49547544Skarels else 49647544Skarels need_resched(); 497181Sbill /* END INLINE EXPANSION */ 498187Sbill goto restart; 49933Sbill } 500187Sbill } else 501187Sbill q = &p->p_link; 50233Sbill } 50333Sbill splx(s); 50433Sbill } 50533Sbill 50633Sbill /* 50754788Storek * The machine independent parts of swtch(). 50854788Storek * Must be called at splstatclock() or higher. 50954788Storek */ 51054788Storek void 51154788Storek swtch() 51254788Storek { 51354788Storek register struct proc *p = curproc; /* XXX */ 51454788Storek register struct rlimit *rlim; 51554788Storek register long s, u; 51654788Storek struct timeval tv; 51754788Storek 51854788Storek /* 51954788Storek * Compute the amount of time during which the current 52054788Storek * process was running, and add that to its total so far. 52154788Storek */ 52254788Storek microtime(&tv); 52354788Storek u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec); 52454788Storek s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec); 52554788Storek if (u < 0) { 52654788Storek u += 1000000; 52754788Storek s--; 52854788Storek } else if (u >= 1000000) { 52954788Storek u -= 1000000; 53054788Storek s++; 53154788Storek } 53254788Storek p->p_rtime.tv_usec = u; 53354788Storek p->p_rtime.tv_sec = s; 53454788Storek 53554788Storek /* 53654788Storek * Check if the process exceeds its cpu resource allocation. 53754788Storek * If over max, kill it. In any case, if it has run for more 53854788Storek * than 10 minutes, reduce priority to give others a chance. 53954788Storek */ 54054788Storek rlim = &p->p_rlimit[RLIMIT_CPU]; 54154788Storek if (s >= rlim->rlim_cur) { 54254788Storek if (s >= rlim->rlim_max) 54354788Storek psignal(p, SIGKILL); 54454788Storek else { 54554788Storek psignal(p, SIGXCPU); 54654788Storek if (rlim->rlim_cur < rlim->rlim_max) 54754788Storek rlim->rlim_cur += 5; 54854788Storek } 54954788Storek } 55054788Storek if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) { 55154788Storek p->p_nice = NZERO + 4; 55264413Sbostic resetpriority(p); 55354788Storek } 55454788Storek 55554788Storek /* 55654788Storek * Pick a new current process and record its start time. 55754788Storek */ 55854788Storek cnt.v_swtch++; 55954788Storek cpu_swtch(p); 56054788Storek microtime(&runtime); 56154788Storek } 56254788Storek 56354788Storek /* 56433Sbill * Initialize the (doubly-linked) run queues 56533Sbill * to be empty. 56633Sbill */ 56733Sbill rqinit() 56833Sbill { 56933Sbill register int i; 57033Sbill 57133Sbill for (i = 0; i < NQS; i++) 57233Sbill qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 57333Sbill } 57433Sbill 57533Sbill /* 57647544Skarels * Change process state to be runnable, 57747544Skarels * placing it on the run queue if it is in memory, 57847544Skarels * and awakening the swapper if it isn't in memory. 57933Sbill */ 58054788Storek void 581*64533Sbostic setrunnable(p) 5824826Swnj register struct proc *p; 58333Sbill { 5844826Swnj register int s; 58533Sbill 58617541Skarels s = splhigh(); 58733Sbill switch (p->p_stat) { 58833Sbill case 0: 58933Sbill case SRUN: 59033Sbill case SZOMB: 59133Sbill default: 592*64533Sbostic panic("setrunnable"); 593207Sbill case SSTOP: 59433Sbill case SSLEEP: 595181Sbill unsleep(p); /* e.g. when sending signals */ 59633Sbill break; 59733Sbill 59833Sbill case SIDL: 59933Sbill break; 60033Sbill } 60133Sbill p->p_stat = SRUN; 60233Sbill if (p->p_flag & SLOAD) 603*64533Sbostic setrunqueue(p); 60433Sbill splx(s); 60530232Skarels if (p->p_slptime > 1) 60630232Skarels updatepri(p); 60747544Skarels p->p_slptime = 0; 60847544Skarels if ((p->p_flag&SLOAD) == 0) 60947544Skarels wakeup((caddr_t)&proc0); 61064413Sbostic else if (p->p_pri < curpriority) 61147544Skarels need_resched(); 61233Sbill } 61333Sbill 61433Sbill /* 61564413Sbostic * Compute the priority of a process when running in user mode. 61664413Sbostic * Arrange to reschedule if the resulting priority is better 61764413Sbostic * than that of the current process. 61833Sbill */ 61954788Storek void 62064413Sbostic resetpriority(p) 62147544Skarels register struct proc *p; 62233Sbill { 62364413Sbostic register unsigned int newpriority; 62433Sbill 62564413Sbostic newpriority = PUSER + p->p_cpu / 4 + 2 * p->p_nice; 62664413Sbostic newpriority = min(newpriority, MAXPRI); 62764413Sbostic p->p_usrpri = newpriority; 62864413Sbostic if (newpriority < curpriority) 62947544Skarels need_resched(); 63033Sbill } 631