Commit | Line | Data |
---|---|---|
da7c5cc6 KM |
1 | /* |
2 | * Copyright (c) 1982 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
6 | * @(#)vm_meter.c 6.7 (Berkeley) %G% | |
7 | */ | |
e3bf9f41 | 8 | |
94368568 JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "seg.h" | |
12 | #include "dir.h" | |
13 | #include "user.h" | |
14 | #include "proc.h" | |
15 | #include "text.h" | |
16 | #include "vm.h" | |
17 | #include "cmap.h" | |
18 | #include "kernel.h" | |
e3bf9f41 | 19 | |
e3bf9f41 | 20 | int maxslp = MAXSLP; |
e3bf9f41 | 21 | int saferss = SAFERSS; |
27b135c8 BJ |
22 | |
23 | /* | |
24 | * The following parameters control operation of the page replacement | |
25 | * algorithm. They are initialized to 0, and then computed at boot time | |
26 | * based on the size of the system. If they are patched non-zero in | |
27 | * a loaded vmunix they are left alone and may thus be changed per system | |
28 | * using adb on the loaded system. | |
29 | */ | |
30 | int maxpgio = 0; | |
31 | int minfree = 0; | |
32 | int desfree = 0; | |
33 | int lotsfree = 0; | |
34 | int slowscan = 0; | |
35 | int fastscan = 0; | |
e3bf9f41 | 36 | int klin = KLIN; |
27b135c8 | 37 | int klseql = KLSEQL; |
4643e8eb | 38 | int klsdist = KLSDIST; |
27b135c8 | 39 | int kltxt = KLTXT; |
e3bf9f41 BJ |
40 | int klout = KLOUT; |
41 | int multprog = -1; /* so we don't count process 2 */ | |
42 | ||
43 | double avenrun[3]; /* load average, of runnable procs */ | |
44 | ||
45 | /* | |
46 | * The main loop of the scheduling (swapping) process. | |
47 | * | |
48 | * The basic idea is: | |
49 | * see if anyone wants to be swapped in; | |
50 | * swap out processes until there is room; | |
51 | * swap him in; | |
52 | * repeat. | |
53 | * If the paging rate is too high, or the average free memory | |
54 | * is very low, then we do not consider swapping anyone in, | |
55 | * but rather look for someone to swap out. | |
56 | * | |
57 | * The runout flag is set whenever someone is swapped out. | |
58 | * Sched sleeps on it awaiting work. | |
59 | * | |
60 | * Sched sleeps on runin whenever it cannot find enough | |
61 | * core (by swapping out or otherwise) to fit the | |
62 | * selected swapped process. It is awakened when the | |
63 | * core situation changes and in any case once per second. | |
64 | * | |
65 | * sched DOESN'T ACCOUNT FOR PAGE TABLE SIZE IN CALCULATIONS. | |
66 | */ | |
67 | ||
68 | #define swappable(p) \ | |
69 | (((p)->p_flag&(SSYS|SLOCK|SULOCK|SLOAD|SPAGE|SKEEP|SWEXIT|SPHYSIO))==SLOAD) | |
70 | ||
71 | /* insure non-zero */ | |
72 | #define nz(x) (x != 0 ? x : 1) | |
73 | ||
74 | #define NBIG 4 | |
75 | #define MAXNBIG 10 | |
76 | int nbig = NBIG; | |
77 | ||
78 | struct bigp { | |
79 | struct proc *bp_proc; | |
80 | int bp_pri; | |
81 | struct bigp *bp_link; | |
82 | } bigp[MAXNBIG], bplist; | |
83 | ||
84 | sched() | |
85 | { | |
86 | register struct proc *rp, *p, *inp; | |
87 | int outpri, inpri, rppri; | |
97afa637 | 88 | int sleeper, desperate, deservin, needs, divisor; |
e3bf9f41 BJ |
89 | register struct bigp *bp, *nbp; |
90 | int biggot, gives; | |
91 | ||
e3bf9f41 | 92 | loop: |
7eb2e67e | 93 | wantin = 0; |
e3bf9f41 BJ |
94 | deservin = 0; |
95 | sleeper = 0; | |
96 | p = 0; | |
27b135c8 | 97 | /* |
37324218 | 98 | * See if paging system is overloaded; if so swap someone out. |
27b135c8 BJ |
99 | * Conditions for hard outswap are: |
100 | * if need kernel map (mix it up). | |
101 | * or | |
102 | * 1. if there are at least 2 runnable processes (on the average) | |
103 | * and 2. the paging rate is excessive or memory is now VERY low. | |
104 | * and 3. the short (5-second) and longer (30-second) average | |
105 | * memory is less than desirable. | |
106 | */ | |
f3d75589 SL |
107 | if (kmapwnt || |
108 | (avenrun[0] >= 2 && imax(avefree, avefree30) < desfree && | |
e3bf9f41 | 109 | (rate.v_pgin + rate.v_pgout > maxpgio || avefree < minfree))) { |
97afa637 | 110 | desperate = 1; |
e3bf9f41 BJ |
111 | goto hardswap; |
112 | } | |
97afa637 | 113 | desperate = 0; |
e3bf9f41 | 114 | /* |
97afa637 | 115 | * Not desperate for core, |
e3bf9f41 BJ |
116 | * look for someone who deserves to be brought in. |
117 | */ | |
118 | outpri = -20000; | |
1d348849 | 119 | for (rp = allproc; rp != NULL; rp = rp->p_nxt) switch(rp->p_stat) { |
e3bf9f41 BJ |
120 | |
121 | case SRUN: | |
122 | if ((rp->p_flag&SLOAD) == 0) { | |
27b135c8 BJ |
123 | rppri = rp->p_time - |
124 | rp->p_swrss / nz((maxpgio/2) * (klin * CLSIZE)) + | |
293c7069 | 125 | rp->p_slptime - (rp->p_nice-NZERO)*8; |
e3bf9f41 BJ |
126 | if (rppri > outpri) { |
127 | if (rp->p_poip) | |
128 | continue; | |
129 | if (rp->p_textp && rp->p_textp->x_poip) | |
130 | continue; | |
131 | p = rp; | |
132 | outpri = rppri; | |
133 | } | |
134 | } | |
135 | continue; | |
136 | ||
137 | case SSLEEP: | |
138 | case SSTOP: | |
139 | if ((freemem < desfree || rp->p_rssize == 0) && | |
140 | rp->p_slptime > maxslp && | |
141 | (!rp->p_textp || (rp->p_textp->x_flag&XLOCK)==0) && | |
142 | swappable(rp)) { | |
143 | /* | |
144 | * Kick out deadwood. | |
e3bf9f41 BJ |
145 | */ |
146 | rp->p_flag &= ~SLOAD; | |
e650efcf | 147 | (void) swapout(rp, rp->p_dsize, rp->p_ssize); |
e3bf9f41 BJ |
148 | goto loop; |
149 | } | |
150 | continue; | |
151 | } | |
152 | ||
153 | /* | |
154 | * No one wants in, so nothing to do. | |
155 | */ | |
156 | if (outpri == -20000) { | |
7d1e298f | 157 | (void) splhigh(); |
7eb2e67e BJ |
158 | if (wantin) { |
159 | wantin = 0; | |
160 | sleep((caddr_t)&lbolt, PSWP); | |
161 | } else { | |
162 | runout++; | |
163 | sleep((caddr_t)&runout, PSWP); | |
164 | } | |
165 | (void) spl0(); | |
e3bf9f41 BJ |
166 | goto loop; |
167 | } | |
e3bf9f41 BJ |
168 | /* |
169 | * Decide how deserving this guy is. If he is deserving | |
170 | * we will be willing to work harder to bring him in. | |
171 | * Needs is an estimate of how much core he will need. | |
172 | * If he has been out for a while, then we will | |
173 | * bring him in with 1/2 the core he will need, otherwise | |
174 | * we are conservative. | |
175 | */ | |
176 | deservin = 0; | |
177 | divisor = 1; | |
178 | if (outpri > maxslp/2) { | |
179 | deservin = 1; | |
180 | divisor = 2; | |
181 | } | |
182 | needs = p->p_swrss; | |
183 | if (p->p_textp && p->p_textp->x_ccount == 0) | |
184 | needs += p->p_textp->x_swrss; | |
27b135c8 | 185 | needs = imin(needs, lotsfree); |
e3bf9f41 BJ |
186 | if (freemem - deficit > needs / divisor) { |
187 | deficit += needs; | |
188 | if (swapin(p)) | |
189 | goto loop; | |
190 | deficit -= imin(needs, deficit); | |
191 | } | |
192 | ||
193 | hardswap: | |
194 | /* | |
195 | * Need resources (kernel map or memory), swap someone out. | |
196 | * Select the nbig largest jobs, then the oldest of these | |
197 | * is ``most likely to get booted.'' | |
198 | */ | |
e3bf9f41 BJ |
199 | inp = p; |
200 | sleeper = 0; | |
201 | if (nbig > MAXNBIG) | |
202 | nbig = MAXNBIG; | |
203 | if (nbig < 1) | |
204 | nbig = 1; | |
205 | biggot = 0; | |
206 | bplist.bp_link = 0; | |
1d348849 | 207 | for (rp = allproc; rp != NULL; rp = rp->p_nxt) { |
e3bf9f41 BJ |
208 | if (!swappable(rp)) |
209 | continue; | |
e3bf9f41 BJ |
210 | if (rp == inp) |
211 | continue; | |
212 | if (rp->p_textp && rp->p_textp->x_flag&XLOCK) | |
213 | continue; | |
214 | if (rp->p_slptime > maxslp && | |
89b899a0 | 215 | (rp->p_stat==SSLEEP&&rp->p_pri>PZERO||rp->p_stat==SSTOP)) { |
e3bf9f41 BJ |
216 | if (sleeper < rp->p_slptime) { |
217 | p = rp; | |
218 | sleeper = rp->p_slptime; | |
219 | } | |
220 | } else if (!sleeper && (rp->p_stat==SRUN||rp->p_stat==SSLEEP)) { | |
221 | rppri = rp->p_rssize; | |
222 | if (rp->p_textp) | |
293c7069 | 223 | rppri += rp->p_textp->x_rssize/rp->p_textp->x_ccount; |
e3bf9f41 BJ |
224 | if (biggot < nbig) |
225 | nbp = &bigp[biggot++]; | |
226 | else { | |
227 | nbp = bplist.bp_link; | |
228 | if (nbp->bp_pri > rppri) | |
229 | continue; | |
230 | bplist.bp_link = nbp->bp_link; | |
231 | } | |
232 | for (bp = &bplist; bp->bp_link; bp = bp->bp_link) | |
233 | if (rppri < bp->bp_link->bp_pri) | |
234 | break; | |
235 | nbp->bp_link = bp->bp_link; | |
236 | bp->bp_link = nbp; | |
237 | nbp->bp_pri = rppri; | |
238 | nbp->bp_proc = rp; | |
239 | } | |
240 | } | |
241 | if (!sleeper) { | |
242 | p = NULL; | |
243 | inpri = -1000; | |
244 | for (bp = bplist.bp_link; bp; bp = bp->bp_link) { | |
245 | rp = bp->bp_proc; | |
246 | rppri = rp->p_time+rp->p_nice-NZERO; | |
247 | if (rppri >= inpri) { | |
248 | p = rp; | |
249 | inpri = rppri; | |
250 | } | |
251 | } | |
252 | } | |
253 | /* | |
97afa637 | 254 | * If we found a long-time sleeper, or we are desperate and |
e3bf9f41 BJ |
255 | * found anyone to swap out, or if someone deserves to come |
256 | * in and we didn't find a sleeper, but found someone who | |
257 | * has been in core for a reasonable length of time, then | |
258 | * we kick the poor luser out. | |
259 | */ | |
97afa637 | 260 | if (sleeper || desperate && p || deservin && inpri > maxslp) { |
7d1e298f | 261 | (void) splhigh(); |
e3bf9f41 BJ |
262 | p->p_flag &= ~SLOAD; |
263 | if (p->p_stat == SRUN) | |
264 | remrq(p); | |
7eb2e67e | 265 | (void) spl0(); |
97afa637 | 266 | if (desperate) { |
e3bf9f41 BJ |
267 | /* |
268 | * Want to give this space to the rest of | |
269 | * the processes in core so give them a chance | |
270 | * by increasing the deficit. | |
271 | */ | |
272 | gives = p->p_rssize; | |
273 | if (p->p_textp) | |
274 | gives += p->p_textp->x_rssize / p->p_textp->x_ccount; | |
9eec1cb0 | 275 | gives = imin(gives, lotsfree); |
e3bf9f41 BJ |
276 | deficit += gives; |
277 | } else | |
278 | gives = 0; /* someone else taketh away */ | |
279 | if (swapout(p, p->p_dsize, p->p_ssize) == 0) | |
280 | deficit -= imin(gives, deficit); | |
281 | goto loop; | |
282 | } | |
283 | /* | |
284 | * Want to swap someone in, but can't | |
285 | * so wait on runin. | |
286 | */ | |
7d1e298f | 287 | (void) splhigh(); |
e3bf9f41 BJ |
288 | runin++; |
289 | sleep((caddr_t)&runin, PSWP); | |
7eb2e67e | 290 | (void) spl0(); |
e3bf9f41 BJ |
291 | goto loop; |
292 | } | |
293 | ||
294 | vmmeter() | |
295 | { | |
296 | register unsigned *cp, *rp, *sp; | |
297 | ||
27b135c8 BJ |
298 | deficit -= imin(deficit, |
299 | imax(deficit / 10, ((klin * CLSIZE) / 2) * maxpgio / 2)); | |
e3bf9f41 | 300 | ave(avefree, freemem, 5); |
27b135c8 | 301 | ave(avefree30, freemem, 30); |
e3bf9f41 BJ |
302 | /* v_pgin is maintained by clock.c */ |
303 | cp = &cnt.v_first; rp = &rate.v_first; sp = &sum.v_first; | |
304 | while (cp <= &cnt.v_last) { | |
305 | ave(*rp, *cp, 5); | |
306 | *sp += *cp; | |
307 | *cp = 0; | |
308 | rp++, cp++, sp++; | |
309 | } | |
c4206698 | 310 | if (time.tv_sec % 5 == 0) { |
e3bf9f41 BJ |
311 | vmtotal(); |
312 | rate.v_swpin = cnt.v_swpin; | |
313 | sum.v_swpin += cnt.v_swpin; | |
314 | cnt.v_swpin = 0; | |
315 | rate.v_swpout = cnt.v_swpout; | |
316 | sum.v_swpout += cnt.v_swpout; | |
317 | cnt.v_swpout = 0; | |
318 | } | |
319 | if (avefree < minfree && runout || proc[0].p_slptime > maxslp/2) { | |
320 | runout = 0; | |
321 | runin = 0; | |
322 | wakeup((caddr_t)&runin); | |
323 | wakeup((caddr_t)&runout); | |
324 | } | |
325 | } | |
326 | ||
c4206698 BJ |
327 | /* |
328 | * Schedule rate for paging. | |
329 | * Rate is linear interpolation between | |
330 | * slowscan with lotsfree and fastscan when out of memory. | |
331 | */ | |
332 | schedpaging() | |
e3bf9f41 | 333 | { |
a2faae5d | 334 | register int vavail; |
e3bf9f41 | 335 | |
e3bf9f41 BJ |
336 | nscan = desscan = 0; |
337 | vavail = freemem - deficit; | |
338 | if (vavail < 0) | |
339 | vavail = 0; | |
e4d539fa | 340 | if (freemem < lotsfree) { |
a2faae5d KM |
341 | desscan = (slowscan * vavail + fastscan * (lotsfree - vavail)) / |
342 | nz(lotsfree) / RATETOSCHEDPAGING; | |
e4d539fa SL |
343 | wakeup((caddr_t)&proc[2]); |
344 | } | |
b32450f4 | 345 | timeout(schedpaging, (caddr_t)0, hz / RATETOSCHEDPAGING); |
e3bf9f41 BJ |
346 | } |
347 | ||
348 | vmtotal() | |
349 | { | |
350 | register struct proc *p; | |
351 | register struct text *xp; | |
352 | int nrun = 0; | |
353 | ||
354 | total.t_vmtxt = 0; | |
355 | total.t_avmtxt = 0; | |
356 | total.t_rmtxt = 0; | |
357 | total.t_armtxt = 0; | |
fcb2d36b | 358 | for (xp = text; xp < textNTEXT; xp++) |
e3bf9f41 BJ |
359 | if (xp->x_iptr) { |
360 | total.t_vmtxt += xp->x_size; | |
361 | total.t_rmtxt += xp->x_rssize; | |
362 | for (p = xp->x_caddr; p; p = p->p_xlink) | |
363 | switch (p->p_stat) { | |
364 | ||
365 | case SSTOP: | |
366 | case SSLEEP: | |
367 | if (p->p_slptime >= maxslp) | |
368 | continue; | |
369 | /* fall into... */ | |
370 | ||
371 | case SRUN: | |
372 | case SIDL: | |
373 | total.t_avmtxt += xp->x_size; | |
374 | total.t_armtxt += xp->x_rssize; | |
375 | goto next; | |
376 | } | |
377 | next: | |
378 | ; | |
379 | } | |
380 | total.t_vm = 0; | |
381 | total.t_avm = 0; | |
382 | total.t_rm = 0; | |
383 | total.t_arm = 0; | |
384 | total.t_rq = 0; | |
385 | total.t_dw = 0; | |
386 | total.t_pw = 0; | |
387 | total.t_sl = 0; | |
388 | total.t_sw = 0; | |
1d348849 | 389 | for (p = allproc; p != NULL; p = p->p_nxt) { |
e3bf9f41 BJ |
390 | if (p->p_flag & SSYS) |
391 | continue; | |
392 | if (p->p_stat) { | |
393 | total.t_vm += p->p_dsize + p->p_ssize; | |
394 | total.t_rm += p->p_rssize; | |
395 | switch (p->p_stat) { | |
396 | ||
397 | case SSLEEP: | |
398 | case SSTOP: | |
89b899a0 | 399 | if (p->p_pri <= PZERO) |
e3bf9f41 BJ |
400 | nrun++; |
401 | if (p->p_flag & SPAGE) | |
402 | total.t_pw++; | |
403 | else if (p->p_flag & SLOAD) { | |
89b899a0 | 404 | if (p->p_pri <= PZERO) |
e3bf9f41 BJ |
405 | total.t_dw++; |
406 | else if (p->p_slptime < maxslp) | |
407 | total.t_sl++; | |
408 | } else if (p->p_slptime < maxslp) | |
409 | total.t_sw++; | |
410 | if (p->p_slptime < maxslp) | |
411 | goto active; | |
412 | break; | |
413 | ||
414 | case SRUN: | |
415 | case SIDL: | |
416 | nrun++; | |
417 | if (p->p_flag & SLOAD) | |
418 | total.t_rq++; | |
419 | else | |
420 | total.t_sw++; | |
421 | active: | |
422 | total.t_avm += p->p_dsize + p->p_ssize; | |
423 | total.t_arm += p->p_rssize; | |
424 | break; | |
425 | } | |
426 | } | |
427 | } | |
428 | total.t_vm += total.t_vmtxt; | |
429 | total.t_avm += total.t_avmtxt; | |
430 | total.t_rm += total.t_rmtxt; | |
431 | total.t_arm += total.t_armtxt; | |
432 | total.t_free = avefree; | |
433 | loadav(avenrun, nrun); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Constants for averages over 1, 5, and 15 minutes | |
438 | * when sampling at 5 second intervals. | |
439 | */ | |
440 | double cexp[3] = { | |
441 | 0.9200444146293232, /* exp(-1/12) */ | |
442 | 0.9834714538216174, /* exp(-1/60) */ | |
443 | 0.9944598480048967, /* exp(-1/180) */ | |
444 | }; | |
445 | ||
446 | /* | |
447 | * Compute a tenex style load average of a quantity on | |
448 | * 1, 5 and 15 minute intervals. | |
449 | */ | |
450 | loadav(avg, n) | |
451 | register double *avg; | |
452 | int n; | |
453 | { | |
454 | register int i; | |
455 | ||
456 | for (i = 0; i < 3; i++) | |
457 | avg[i] = cexp[i] * avg[i] + n * (1.0 - cexp[i]); | |
458 | } |