1 /* kern_synch.c 4.24 82/10/21 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/dir.h" 6 #include "../h/user.h" 7 #include "../h/proc.h" 8 #include "../h/file.h" 9 #include "../h/inode.h" 10 #include "../h/vm.h" 11 #include "../h/pte.h" 12 #ifdef MUSH 13 #include "../h/quota.h" 14 #include "../h/share.h" 15 #endif 16 #include "../h/kernel.h" 17 #include "../h/buf.h" 18 #include "../vax/mtpr.h" /* XXX */ 19 20 /* 21 * Force switch among equal priority processes every 100ms. 22 */ 23 roundrobin() 24 { 25 26 runrun++; 27 aston(); 28 timeout(roundrobin, (caddr_t)0, hz / 10); 29 } 30 31 /* constants to digital decay and forget 90% of usage in 5*loadav time */ 32 #undef ave 33 #define ave(a,b) ((int)(((int)(a*b))/(b+1))) 34 int nrscale = 2; 35 double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 36 37 /* 38 * Recompute process priorities, once a second 39 */ 40 schedcpu() 41 { 42 register struct proc *p; 43 register int s, a; 44 45 wakeup((caddr_t)&lbolt); 46 for (p = proc; p < procNPROC; p++) if (p->p_stat && p->p_stat!=SZOMB) { 47 #ifdef MUSH 48 if (p->p_quota->q_uid) 49 p->p_quota->q_cost += 50 shconsts.sc_click * p->p_rssize; 51 #endif 52 if (p->p_time != 127) 53 p->p_time++; 54 if (p->p_stat==SSLEEP || p->p_stat==SSTOP) 55 if (p->p_slptime != 127) 56 p->p_slptime++; 57 if (p->p_flag&SLOAD) 58 p->p_pctcpu = ccpu * p->p_pctcpu + 59 (1.0 - ccpu) * (p->p_cpticks/(float)hz); 60 p->p_cpticks = 0; 61 #ifdef MUSH 62 a = ave((p->p_cpu & 0377), avenrun[0]*nrscale) + 63 p->p_nice - NZERO + p->p_quota->q_nice; 64 #else 65 a = ave((p->p_cpu & 0377), avenrun[0]*nrscale) + 66 p->p_nice - NZERO; 67 #endif 68 if (a < 0) 69 a = 0; 70 if (a > 255) 71 a = 255; 72 p->p_cpu = a; 73 (void) setpri(p); 74 s = spl6(); /* prevent state changes */ 75 if (p->p_pri >= PUSER) { 76 if ((p != u.u_procp || noproc) && 77 p->p_stat == SRUN && 78 (p->p_flag & SLOAD) && 79 p->p_pri != p->p_usrpri) { 80 remrq(p); 81 p->p_pri = p->p_usrpri; 82 setrq(p); 83 } else 84 p->p_pri = p->p_usrpri; 85 } 86 splx(s); 87 } 88 vmmeter(); 89 if (runin!=0) { 90 runin = 0; 91 wakeup((caddr_t)&runin); 92 } 93 if (bclnlist != NULL) 94 wakeup((caddr_t)&proc[2]); 95 timeout(schedcpu, (caddr_t)0, hz); 96 } 97 98 #define SQSIZE 0100 /* Must be power of 2 */ 99 #define HASH(x) (( (int) x >> 5) & (SQSIZE-1)) 100 struct proc *slpque[SQSIZE]; 101 102 /* 103 * Give up the processor till a wakeup occurs 104 * on chan, at which time the process 105 * enters the scheduling queue at priority pri. 106 * The most important effect of pri is that when 107 * pri<=PZERO a signal cannot disturb the sleep; 108 * if pri>PZERO signals will be processed. 109 * Callers of this routine must be prepared for 110 * premature return, and check that the reason for 111 * sleeping has gone away. 112 */ 113 sleep(chan, pri) 114 caddr_t chan; 115 int pri; 116 { 117 register struct proc *rp, **hp; 118 register s; 119 120 rp = u.u_procp; 121 s = spl6(); 122 if (chan==0 || rp->p_stat != SRUN || rp->p_rlink) 123 panic("sleep"); 124 rp->p_wchan = chan; 125 rp->p_slptime = 0; 126 rp->p_pri = pri; 127 hp = &slpque[HASH(chan)]; 128 rp->p_link = *hp; 129 *hp = rp; 130 if (pri > PZERO) { 131 if (ISSIG(rp)) { 132 if (rp->p_wchan) 133 unsleep(rp); 134 rp->p_stat = SRUN; 135 (void) spl0(); 136 goto psig; 137 } 138 if (rp->p_wchan == 0) 139 goto out; 140 rp->p_stat = SSLEEP; 141 (void) spl0(); 142 u.u_ru.ru_nvcsw++; 143 swtch(); 144 if (ISSIG(rp)) 145 goto psig; 146 } else { 147 rp->p_stat = SSLEEP; 148 (void) spl0(); 149 u.u_ru.ru_nvcsw++; 150 swtch(); 151 } 152 out: 153 splx(s); 154 return; 155 156 /* 157 * If priority was low (>PZERO) and 158 * there has been a signal, execute non-local goto through 159 * u.u_qsave, aborting the system call in progress (see trap.c) 160 * (or finishing a tsleep, see below) 161 */ 162 psig: 163 longjmp(&u.u_qsave); 164 /*NOTREACHED*/ 165 } 166 167 /* 168 * Remove a process from its wait queue 169 */ 170 unsleep(p) 171 register struct proc *p; 172 { 173 register struct proc **hp; 174 register s; 175 176 s = spl6(); 177 if (p->p_wchan) { 178 hp = &slpque[HASH(p->p_wchan)]; 179 while (*hp != p) 180 hp = &(*hp)->p_link; 181 *hp = p->p_link; 182 p->p_wchan = 0; 183 } 184 splx(s); 185 } 186 187 /* 188 * Wake up all processes sleeping on chan. 189 */ 190 wakeup(chan) 191 register caddr_t chan; 192 { 193 register struct proc *p, **q, **h; 194 int s; 195 196 s = spl6(); 197 h = &slpque[HASH(chan)]; 198 restart: 199 for (q = h; p = *q; ) { 200 if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 201 panic("wakeup"); 202 if (p->p_wchan==chan) { 203 p->p_wchan = 0; 204 *q = p->p_link; 205 p->p_slptime = 0; 206 if (p->p_stat == SSLEEP) { 207 /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 208 p->p_stat = SRUN; 209 if (p->p_flag & SLOAD) 210 setrq(p); 211 if (p->p_pri < curpri) { 212 runrun++; 213 aston(); 214 } 215 if ((p->p_flag&SLOAD) == 0) { 216 if (runout != 0) { 217 runout = 0; 218 wakeup((caddr_t)&runout); 219 } 220 wantin++; 221 } 222 /* END INLINE EXPANSION */ 223 goto restart; 224 } 225 } else 226 q = &p->p_link; 227 } 228 splx(s); 229 } 230 231 /* 232 * Initialize the (doubly-linked) run queues 233 * to be empty. 234 */ 235 rqinit() 236 { 237 register int i; 238 239 for (i = 0; i < NQS; i++) 240 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 241 } 242 243 /* 244 * Set the process running; 245 * arrange for it to be swapped in if necessary. 246 */ 247 setrun(p) 248 register struct proc *p; 249 { 250 register int s; 251 252 s = spl6(); 253 switch (p->p_stat) { 254 255 case 0: 256 case SWAIT: 257 case SRUN: 258 case SZOMB: 259 default: 260 panic("setrun"); 261 262 case SSTOP: 263 case SSLEEP: 264 unsleep(p); /* e.g. when sending signals */ 265 break; 266 267 case SIDL: 268 break; 269 } 270 p->p_stat = SRUN; 271 if (p->p_flag & SLOAD) 272 setrq(p); 273 splx(s); 274 if (p->p_pri < curpri) { 275 runrun++; 276 aston(); 277 } 278 if ((p->p_flag&SLOAD) == 0) { 279 if (runout != 0) { 280 runout = 0; 281 wakeup((caddr_t)&runout); 282 } 283 wantin++; 284 } 285 } 286 287 /* 288 * Set user priority. 289 * The rescheduling flag (runrun) 290 * is set if the priority is better 291 * than the currently running process. 292 */ 293 setpri(pp) 294 register struct proc *pp; 295 { 296 register int p; 297 298 p = (pp->p_cpu & 0377)/4; 299 p += PUSER + 2*(pp->p_nice - NZERO); 300 if (pp->p_rssize > pp->p_maxrss && freemem < desfree) 301 p += 2*4; /* effectively, nice(4) */ 302 if (p > 127) 303 p = 127; 304 if (p < curpri) { 305 runrun++; 306 aston(); 307 } 308 pp->p_usrpri = p; 309 return (p); 310 } 311