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