|
|
1.1 root 1: /*
2: * Mach Operating System
3: * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University.
4: * Copyright (c) 1993,1994 The University of Utah and
5: * the Computer Systems Laboratory (CSL).
6: * All rights reserved.
7: *
8: * Permission to use, copy, modify and distribute this software and its
9: * documentation is hereby granted, provided that both the copyright
10: * notice and this permission notice appear in all copies of the
11: * software, derivative works or modified versions, and any portions
12: * thereof, and that both notices appear in supporting documentation.
13: *
14: * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
15: * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
16: * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
17: * THIS SOFTWARE.
18: *
19: * Carnegie Mellon requests users of this software to return to
20: *
21: * Software Distribution Coordinator or [email protected]
22: * School of Computer Science
23: * Carnegie Mellon University
24: * Pittsburgh PA 15213-3890
25: *
26: * any improvements or extensions that they make and grant Carnegie Mellon
27: * the rights to redistribute these changes.
28: */
29: /*
30: * File: vm/vm_pageout.c
31: * Author: Avadis Tevanian, Jr., Michael Wayne Young
32: * Date: 1985
33: *
34: * The proverbial page-out daemon.
35: */
36:
1.1.1.3 root 37: #include <device/net_io.h>
1.1 root 38: #include <mach/mach_types.h>
39: #include <mach/memory_object.h>
1.1.1.3 root 40: #include <vm/memory_object_default.user.h>
41: #include <vm/memory_object_user.user.h>
1.1 root 42: #include <mach/vm_param.h>
43: #include <mach/vm_statistics.h>
44: #include <kern/counters.h>
1.1.1.3 root 45: #include <kern/debug.h>
46: #include <kern/slab.h>
47: #include <kern/task.h>
1.1 root 48: #include <kern/thread.h>
49: #include <vm/pmap.h>
50: #include <vm/vm_map.h>
51: #include <vm/vm_object.h>
52: #include <vm/vm_page.h>
53: #include <vm/vm_pageout.h>
1.1.1.3 root 54: #include <machine/locore.h>
1.1 root 55:
56:
57:
58: #ifndef VM_PAGEOUT_BURST_MAX
59: #define VM_PAGEOUT_BURST_MAX 10 /* number of pages */
1.1.1.2 root 60: #endif /* VM_PAGEOUT_BURST_MAX */
1.1 root 61:
62: #ifndef VM_PAGEOUT_BURST_MIN
63: #define VM_PAGEOUT_BURST_MIN 5 /* number of pages */
1.1.1.2 root 64: #endif /* VM_PAGEOUT_BURST_MIN */
1.1 root 65:
66: #ifndef VM_PAGEOUT_BURST_WAIT
1.1.1.2 root 67: #define VM_PAGEOUT_BURST_WAIT 10 /* milliseconds per page */
68: #endif /* VM_PAGEOUT_BURST_WAIT */
1.1 root 69:
70: #ifndef VM_PAGEOUT_EMPTY_WAIT
1.1.1.2 root 71: #define VM_PAGEOUT_EMPTY_WAIT 75 /* milliseconds */
72: #endif /* VM_PAGEOUT_EMPTY_WAIT */
1.1 root 73:
74: #ifndef VM_PAGEOUT_PAUSE_MAX
75: #define VM_PAGEOUT_PAUSE_MAX 10 /* number of pauses */
1.1.1.2 root 76: #endif /* VM_PAGEOUT_PAUSE_MAX */
1.1 root 77:
78: /*
79: * To obtain a reasonable LRU approximation, the inactive queue
80: * needs to be large enough to give pages on it a chance to be
81: * referenced a second time. This macro defines the fraction
82: * of active+inactive pages that should be inactive.
83: * The pageout daemon uses it to update vm_page_inactive_target.
84: *
85: * If vm_page_free_count falls below vm_page_free_target and
86: * vm_page_inactive_count is below vm_page_inactive_target,
87: * then the pageout daemon starts running.
88: */
89:
90: #ifndef VM_PAGE_INACTIVE_TARGET
91: #define VM_PAGE_INACTIVE_TARGET(avail) ((avail) * 2 / 3)
1.1.1.2 root 92: #endif /* VM_PAGE_INACTIVE_TARGET */
1.1 root 93:
94: /*
95: * Once the pageout daemon starts running, it keeps going
96: * until vm_page_free_count meets or exceeds vm_page_free_target.
97: */
98:
99: #ifndef VM_PAGE_FREE_TARGET
1.1.1.4 root 100: #define VM_PAGE_FREE_TARGET(free) (150 + (free) * 10 / 100)
1.1.1.2 root 101: #endif /* VM_PAGE_FREE_TARGET */
1.1 root 102:
103: /*
104: * The pageout daemon always starts running once vm_page_free_count
105: * falls below vm_page_free_min.
106: */
107:
108: #ifndef VM_PAGE_FREE_MIN
1.1.1.4 root 109: #define VM_PAGE_FREE_MIN(free) (100 + (free) * 8 / 100)
1.1.1.2 root 110: #endif /* VM_PAGE_FREE_MIN */
111:
112: /* When vm_page_external_count exceeds vm_page_external_limit,
113: * allocations of externally paged pages stops.
114: */
115:
116: #ifndef VM_PAGE_EXTERNAL_LIMIT
117: #define VM_PAGE_EXTERNAL_LIMIT(free) ((free) / 2)
118: #endif /* VM_PAGE_EXTERNAL_LIMIT */
119:
120: /* Attempt to keep the number of externally paged pages less
121: * than vm_pages_external_target.
122: */
123: #ifndef VM_PAGE_EXTERNAL_TARGET
124: #define VM_PAGE_EXTERNAL_TARGET(free) ((free) / 4)
125: #endif /* VM_PAGE_EXTERNAL_TARGET */
1.1 root 126:
127: /*
128: * When vm_page_free_count falls below vm_page_free_reserved,
129: * only vm-privileged threads can allocate pages. vm-privilege
130: * allows the pageout daemon and default pager (and any other
131: * associated threads needed for default pageout) to continue
1.1.1.2 root 132: * operation by dipping into the reserved pool of pages. */
1.1 root 133:
134: #ifndef VM_PAGE_FREE_RESERVED
1.1.1.4 root 135: #define VM_PAGE_FREE_RESERVED 500
1.1.1.2 root 136: #endif /* VM_PAGE_FREE_RESERVED */
1.1 root 137:
138: /*
139: * When vm_page_free_count falls below vm_pageout_reserved_internal,
140: * the pageout daemon no longer trusts external pagers to clean pages.
141: * External pagers are probably all wedged waiting for a free page.
142: * It forcibly double-pages dirty pages belonging to external objects,
143: * getting the pages to the default pager to clean.
144: */
145:
146: #ifndef VM_PAGEOUT_RESERVED_INTERNAL
1.1.1.4 root 147: #define VM_PAGEOUT_RESERVED_INTERNAL(reserve) ((reserve) - 250)
1.1.1.2 root 148: #endif /* VM_PAGEOUT_RESERVED_INTERNAL */
1.1 root 149:
150: /*
151: * When vm_page_free_count falls below vm_pageout_reserved_really,
152: * the pageout daemon stops work entirely to let the default pager
153: * catch up (assuming the default pager has pages to clean).
154: * Beyond this point, it is too dangerous to consume memory
155: * even for memory_object_data_write messages to the default pager.
156: */
157:
158: #ifndef VM_PAGEOUT_RESERVED_REALLY
1.1.1.4 root 159: #define VM_PAGEOUT_RESERVED_REALLY(reserve) ((reserve) - 400)
1.1.1.2 root 160: #endif /* VM_PAGEOUT_RESERVED_REALLY */
1.1 root 161:
162: unsigned int vm_pageout_reserved_internal = 0;
163: unsigned int vm_pageout_reserved_really = 0;
164:
1.1.1.2 root 165: unsigned int vm_page_external_target = 0;
166:
1.1 root 167: unsigned int vm_pageout_burst_max = 0;
168: unsigned int vm_pageout_burst_min = 0;
169: unsigned int vm_pageout_burst_wait = 0; /* milliseconds per page */
170: unsigned int vm_pageout_empty_wait = 0; /* milliseconds */
171: unsigned int vm_pageout_pause_count = 0;
172: unsigned int vm_pageout_pause_max = 0;
173:
174: /*
175: * These variables record the pageout daemon's actions:
176: * how many pages it looks at and what happens to those pages.
177: * No locking needed because only one thread modifies the variables.
178: */
179:
180: unsigned int vm_pageout_active = 0; /* debugging */
181: unsigned int vm_pageout_inactive = 0; /* debugging */
182: unsigned int vm_pageout_inactive_nolock = 0; /* debugging */
183: unsigned int vm_pageout_inactive_busy = 0; /* debugging */
184: unsigned int vm_pageout_inactive_absent = 0; /* debugging */
185: unsigned int vm_pageout_inactive_used = 0; /* debugging */
186: unsigned int vm_pageout_inactive_clean = 0; /* debugging */
187: unsigned int vm_pageout_inactive_dirty = 0; /* debugging */
188: unsigned int vm_pageout_inactive_double = 0; /* debugging */
1.1.1.2 root 189: unsigned int vm_pageout_inactive_cleaned_external = 0;
1.1 root 190:
191: /*
192: * Routine: vm_pageout_setup
193: * Purpose:
194: * Set up a page for pageout.
195: *
196: * Move or copy the page to a new object, as part
197: * of which it will be sent to its memory manager
198: * in a memory_object_data_write or memory_object_initialize
199: * message.
200: *
201: * The "paging_offset" argument specifies the offset
202: * of the page within its external memory object.
203: *
204: * The "new_object" and "new_offset" arguments
205: * indicate where the page should be moved.
206: *
207: * The "flush" argument specifies whether the page
208: * should be flushed from its object. If not, a
209: * copy of the page is moved to the new object.
210: *
211: * In/Out conditions:
212: * The page in question must not be on any pageout queues,
213: * and must be busy. The object to which it belongs
214: * must be unlocked, and the caller must hold a paging
215: * reference to it. The new_object must not be locked.
216: *
217: * If the page is flushed from its original object,
218: * this routine returns a pointer to a place-holder page,
219: * inserted at the same offset, to block out-of-order
220: * requests for the page. The place-holder page must
221: * be freed after the data_write or initialize message
222: * has been sent. If the page is copied,
223: * the holding page is VM_PAGE_NULL.
224: *
225: * The original page is put on a paging queue and marked
226: * not busy on exit.
227: */
228: vm_page_t
1.1.1.4 root 229: vm_pageout_setup(
230: vm_page_t m,
231: vm_offset_t paging_offset,
232: vm_object_t new_object,
233: vm_offset_t new_offset,
234: boolean_t flush)
1.1 root 235: {
1.1.1.4 root 236: vm_object_t old_object = m->object;
237: vm_page_t holding_page = 0; /*'=0'to quiet gcc warnings*/
238: vm_page_t new_m;
1.1 root 239:
240: assert(m->busy && !m->absent && !m->fictitious);
241:
242: /*
243: * If we are not flushing the page, allocate a
244: * page in the object. If we cannot get the
245: * page, flush instead.
246: */
247: if (!flush) {
248: vm_object_lock(new_object);
249: new_m = vm_page_alloc(new_object, new_offset);
250: if (new_m == VM_PAGE_NULL)
251: flush = TRUE;
252: vm_object_unlock(new_object);
253: }
254:
255: if (flush) {
256: /*
257: * Create a place-holder page where the old one was,
258: * to prevent anyone from attempting to page in this
259: * page while we`re unlocked.
260: */
261: while ((holding_page = vm_page_grab_fictitious())
262: == VM_PAGE_NULL)
263: vm_page_more_fictitious();
264:
265: vm_object_lock(old_object);
266: vm_page_lock_queues();
267: vm_page_remove(m);
268: vm_page_unlock_queues();
269: PAGE_WAKEUP_DONE(m);
270:
271: vm_page_lock_queues();
272: vm_page_insert(holding_page, old_object, m->offset);
273: vm_page_unlock_queues();
274:
275: /*
276: * Record that this page has been written out
277: */
278: #if MACH_PAGEMAP
279: vm_external_state_set(old_object->existence_info,
280: paging_offset,
281: VM_EXTERNAL_STATE_EXISTS);
1.1.1.2 root 282: #endif /* MACH_PAGEMAP */
1.1 root 283:
284: vm_object_unlock(old_object);
285:
286: vm_object_lock(new_object);
287:
288: /*
289: * Move this page into the new object
290: */
291:
292: vm_page_lock_queues();
293: vm_page_insert(m, new_object, new_offset);
294: vm_page_unlock_queues();
295:
296: m->dirty = TRUE;
297: m->precious = FALSE;
298: m->page_lock = VM_PROT_NONE;
299: m->unlock_request = VM_PROT_NONE;
300: }
301: else {
302: /*
303: * Copy the data into the new page,
304: * and mark the new page as clean.
305: */
306: vm_page_copy(m, new_m);
307:
308: vm_object_lock(old_object);
309: m->dirty = FALSE;
310: pmap_clear_modify(m->phys_addr);
311:
312: /*
313: * Deactivate old page.
314: */
315: vm_page_lock_queues();
316: vm_page_deactivate(m);
317: vm_page_unlock_queues();
318:
319: PAGE_WAKEUP_DONE(m);
320:
321: /*
322: * Record that this page has been written out
323: */
324:
325: #if MACH_PAGEMAP
326: vm_external_state_set(old_object->existence_info,
327: paging_offset,
328: VM_EXTERNAL_STATE_EXISTS);
1.1.1.2 root 329: #endif /* MACH_PAGEMAP */
1.1 root 330:
331: vm_object_unlock(old_object);
332:
333: vm_object_lock(new_object);
334:
335: /*
336: * Use the new page below.
337: */
338: m = new_m;
339: m->dirty = TRUE;
340: assert(!m->precious);
341: PAGE_WAKEUP_DONE(m);
342: }
343:
344: /*
345: * Make the old page eligible for replacement again; if a
346: * user-supplied memory manager fails to release the page,
347: * it will be paged out again to the default memory manager.
348: *
349: * Note that pages written to the default memory manager
350: * must be wired down -- in return, it guarantees to free
351: * this page, rather than reusing it.
352: */
353:
354: vm_page_lock_queues();
355: vm_stat.pageouts++;
356: if (m->laundry) {
357: /*
358: * vm_pageout_scan is telling us to put this page
359: * at the front of the inactive queue, so it will
360: * be immediately paged out to the default pager.
361: */
362:
363: assert(!old_object->internal);
364: m->laundry = FALSE;
365:
366: queue_enter_first(&vm_page_queue_inactive, m,
367: vm_page_t, pageq);
368: m->inactive = TRUE;
369: vm_page_inactive_count++;
370: } else if (old_object->internal) {
371: m->laundry = TRUE;
372: vm_page_laundry_count++;
373:
374: vm_page_wire(m);
375: } else
376: vm_page_activate(m);
377: vm_page_unlock_queues();
378:
379: /*
380: * Since IPC operations may block, we drop locks now.
381: * [The placeholder page is busy, and we still have
382: * paging_in_progress incremented.]
383: */
384:
385: vm_object_unlock(new_object);
386:
387: /*
388: * Return the placeholder page to simplify cleanup.
389: */
390: return (flush ? holding_page : VM_PAGE_NULL);
391: }
392:
393: /*
394: * Routine: vm_pageout_page
395: * Purpose:
396: * Causes the specified page to be written back to
397: * the appropriate memory object.
398: *
399: * The "initial" argument specifies whether this
400: * data is an initialization only, and should use
401: * memory_object_data_initialize instead of
402: * memory_object_data_write.
403: *
404: * The "flush" argument specifies whether the page
405: * should be flushed from the object. If not, a
406: * copy of the data is sent to the memory object.
407: *
408: * In/out conditions:
409: * The page in question must not be on any pageout queues.
410: * The object to which it belongs must be locked.
411: * Implementation:
412: * Move this page to a completely new object, if flushing;
413: * copy to a new page in a new object, if not.
414: */
415: void
1.1.1.4 root 416: vm_pageout_page(
417: vm_page_t m,
418: boolean_t initial,
419: boolean_t flush)
1.1 root 420: {
421: vm_map_copy_t copy;
1.1.1.4 root 422: vm_object_t old_object;
423: vm_object_t new_object;
424: vm_page_t holding_page;
1.1 root 425: vm_offset_t paging_offset;
426: kern_return_t rc;
427: boolean_t precious_clean;
428:
429: assert(m->busy);
430:
431: /*
432: * Cleaning but not flushing a clean precious page is a
433: * no-op. Remember whether page is clean and precious now
434: * because vm_pageout_setup will mark it dirty and not precious.
435: *
436: * XXX Check if precious_clean && !flush can really happen.
437: */
438: precious_clean = (!m->dirty) && m->precious;
439: if (precious_clean && !flush) {
440: PAGE_WAKEUP_DONE(m);
441: return;
442: }
443:
444: /*
445: * Verify that we really want to clean this page.
446: */
447: if (m->absent || m->error || (!m->dirty && !m->precious)) {
448: VM_PAGE_FREE(m);
449: return;
450: }
451:
452: /*
453: * Create a paging reference to let us play with the object.
454: */
455: old_object = m->object;
456: paging_offset = m->offset + old_object->paging_offset;
457: vm_object_paging_begin(old_object);
458: vm_object_unlock(old_object);
459:
460: /*
461: * Allocate a new object into which we can put the page.
462: */
463: new_object = vm_object_allocate(PAGE_SIZE);
464:
465: /*
466: * Move the page into the new object.
467: */
468: holding_page = vm_pageout_setup(m,
469: paging_offset,
470: new_object,
471: 0, /* new offset */
472: flush); /* flush */
473:
474: rc = vm_map_copyin_object(new_object, 0, PAGE_SIZE, ©);
475: assert(rc == KERN_SUCCESS);
476:
477: if (initial || old_object->use_old_pageout) {
478: rc = (*(initial ? memory_object_data_initialize
479: : memory_object_data_write))
480: (old_object->pager,
481: old_object->pager_request,
482: paging_offset, (pointer_t) copy, PAGE_SIZE);
483: }
484: else {
485: rc = memory_object_data_return(
486: old_object->pager,
487: old_object->pager_request,
488: paging_offset, (pointer_t) copy, PAGE_SIZE,
489: !precious_clean, !flush);
490: }
491:
492: if (rc != KERN_SUCCESS)
493: vm_map_copy_discard(copy);
494:
495: /*
496: * Clean up.
497: */
498: vm_object_lock(old_object);
499: if (holding_page != VM_PAGE_NULL)
500: VM_PAGE_FREE(holding_page);
501: vm_object_paging_end(old_object);
502: }
503:
504: /*
505: * vm_pageout_scan does the dirty work for the pageout daemon.
506: * It returns with vm_page_queue_free_lock held and
507: * vm_page_free_wanted == 0.
508: */
509:
1.1.1.4 root 510: void vm_pageout_scan(void)
1.1 root 511: {
512: unsigned int burst_count;
1.1.1.2 root 513: unsigned int want_pages;
1.1 root 514:
515: /*
516: * We want to gradually dribble pages from the active queue
517: * to the inactive queue. If we let the inactive queue get
518: * very small, and then suddenly dump many pages into it,
519: * those pages won't get a sufficient chance to be referenced
520: * before we start taking them from the inactive queue.
521: *
522: * We must limit the rate at which we send pages to the pagers.
523: * data_write messages consume memory, for message buffers and
524: * for map-copy objects. If we get too far ahead of the pagers,
525: * we can potentially run out of memory.
526: *
527: * We can use the laundry count to limit directly the number
528: * of pages outstanding to the default pager. A similar
529: * strategy for external pagers doesn't work, because
530: * external pagers don't have to deallocate the pages sent them,
531: * and because we might have to send pages to external pagers
532: * even if they aren't processing writes. So we also
533: * use a burst count to limit writes to external pagers.
534: *
535: * When memory is very tight, we can't rely on external pagers to
536: * clean pages. They probably aren't running, because they
537: * aren't vm-privileged. If we kept sending dirty pages to them,
538: * we could exhaust the free list. However, we can't just ignore
539: * pages belonging to external objects, because there might be no
540: * pages belonging to internal objects. Hence, we get the page
541: * into an internal object and then immediately double-page it,
542: * sending it to the default pager.
543: *
1.1.1.3 root 544: * slab_collect should be last, because the other operations
545: * might return memory to caches. When we pause we use
1.1 root 546: * vm_pageout_scan_continue as our continuation, so we will
547: * reenter vm_pageout_scan periodically and attempt to reclaim
548: * internal memory even if we never reach vm_page_free_target.
549: */
550:
551: stack_collect();
552: net_kmsg_collect();
553: consider_task_collect();
1.1.1.4 root 554: if (0) /* XXX: pcb_collect doesn't do anything yet, so it is
555: pointless to call consider_thread_collect. */
1.1 root 556: consider_thread_collect();
1.1.1.3 root 557: slab_collect();
1.1 root 558:
559: for (burst_count = 0;;) {
1.1.1.4 root 560: vm_page_t m;
561: vm_object_t object;
1.1 root 562: unsigned int free_count;
563:
564: /*
565: * Recalculate vm_page_inactivate_target.
566: */
567:
568: vm_page_lock_queues();
569: vm_page_inactive_target =
570: VM_PAGE_INACTIVE_TARGET(vm_page_active_count +
571: vm_page_inactive_count);
572:
573: /*
574: * Move pages from active to inactive.
575: */
576:
577: while ((vm_page_inactive_count < vm_page_inactive_target) &&
578: !queue_empty(&vm_page_queue_active)) {
1.1.1.4 root 579: vm_object_t obj;
1.1 root 580:
581: vm_pageout_active++;
582: m = (vm_page_t) queue_first(&vm_page_queue_active);
583: assert(m->active && !m->inactive);
584:
585: obj = m->object;
586: if (!vm_object_lock_try(obj)) {
587: /*
588: * Move page to end and continue.
589: */
590:
591: queue_remove(&vm_page_queue_active, m,
592: vm_page_t, pageq);
593: queue_enter(&vm_page_queue_active, m,
594: vm_page_t, pageq);
595: vm_page_unlock_queues();
596: vm_page_lock_queues();
597: continue;
598: }
599:
600: /*
601: * If the page is busy, then we pull it
602: * off the active queue and leave it alone.
603: */
604:
605: if (m->busy) {
606: vm_object_unlock(obj);
607: queue_remove(&vm_page_queue_active, m,
608: vm_page_t, pageq);
609: m->active = FALSE;
610: vm_page_active_count--;
611: continue;
612: }
613:
614: /*
615: * Deactivate the page while holding the object
616: * locked, so we know the page is still not busy.
617: * This should prevent races between pmap_enter
618: * and pmap_clear_reference. The page might be
619: * absent or fictitious, but vm_page_deactivate
620: * can handle that.
621: */
622:
623: vm_page_deactivate(m);
624: vm_object_unlock(obj);
625: }
626:
627: /*
1.1.1.2 root 628: * We are done if we have met our targets *and*
1.1 root 629: * nobody is still waiting for a page.
630: */
631:
632: simple_lock(&vm_page_queue_free_lock);
633: free_count = vm_page_free_count;
1.1.1.2 root 634: if ((free_count >= vm_page_free_target) &&
635: (vm_page_external_count <= vm_page_external_target) &&
1.1 root 636: (vm_page_free_wanted == 0)) {
637: vm_page_unlock_queues();
638: break;
639: }
1.1.1.2 root 640: want_pages = ((free_count < vm_page_free_target) ||
641: vm_page_free_wanted);
1.1 root 642: simple_unlock(&vm_page_queue_free_lock);
643:
644: /*
645: * Sometimes we have to pause:
646: * 1) No inactive pages - nothing to do.
647: * 2) Flow control - wait for pagers to catch up.
648: * 3) Extremely low memory - sending out dirty pages
649: * consumes memory. We don't take the risk of doing
650: * this if the default pager already has work to do.
651: */
1.1.1.2 root 652: pause:
1.1 root 653: if (queue_empty(&vm_page_queue_inactive) ||
654: (burst_count >= vm_pageout_burst_max) ||
655: (vm_page_laundry_count >= vm_pageout_burst_max) ||
656: ((free_count < vm_pageout_reserved_really) &&
657: (vm_page_laundry_count > 0))) {
658: unsigned int pages, msecs;
659:
660: /*
661: * vm_pageout_burst_wait is msecs/page.
662: * If there is nothing for us to do, we wait
663: * at least vm_pageout_empty_wait msecs.
664: */
665:
666: if (vm_page_laundry_count > burst_count)
667: pages = vm_page_laundry_count;
668: else
669: pages = burst_count;
670: msecs = pages * vm_pageout_burst_wait;
671:
672: if (queue_empty(&vm_page_queue_inactive) &&
673: (msecs < vm_pageout_empty_wait))
674: msecs = vm_pageout_empty_wait;
675: vm_page_unlock_queues();
676:
677: thread_will_wait_with_timeout(current_thread(), msecs);
678: counter(c_vm_pageout_scan_block++);
679: thread_block(vm_pageout_scan_continue);
680: call_continuation(vm_pageout_scan_continue);
681: /*NOTREACHED*/
682: }
683:
684: vm_pageout_inactive++;
1.1.1.2 root 685:
686: /* Find a page we are interested in paging out. If we
687: need pages, then we'll page anything out; otherwise
688: we only page out external pages. */
689: m = (vm_page_t) queue_first (&vm_page_queue_inactive);
690: while (1)
691: {
692: assert (!m->active && m->inactive);
693: if (want_pages || m->external)
694: break;
695:
1.1.1.5 ! root 696: m = (vm_page_t) queue_next (&m->pageq);
1.1.1.2 root 697: if (!m)
698: goto pause;
699: }
700:
1.1 root 701: object = m->object;
702:
703: /*
704: * Try to lock object; since we've got the
705: * page queues lock, we can only try for this one.
706: */
707:
708: if (!vm_object_lock_try(object)) {
709: /*
710: * Move page to end and continue.
711: */
712:
713: queue_remove(&vm_page_queue_inactive, m,
714: vm_page_t, pageq);
715: queue_enter(&vm_page_queue_inactive, m,
716: vm_page_t, pageq);
717: vm_page_unlock_queues();
718: vm_pageout_inactive_nolock++;
719: continue;
720: }
721:
722: /*
723: * Remove the page from the inactive list.
724: */
725:
726: queue_remove(&vm_page_queue_inactive, m, vm_page_t, pageq);
727: vm_page_inactive_count--;
728: m->inactive = FALSE;
729:
730: if (m->busy || !object->alive) {
731: /*
732: * Somebody is already playing with this page.
733: * Leave it off the pageout queues.
734: */
735:
736: vm_page_unlock_queues();
737: vm_object_unlock(object);
738: vm_pageout_inactive_busy++;
739: continue;
740: }
741:
742: /*
743: * If it's absent, we can reclaim the page.
744: */
745:
1.1.1.2 root 746: if (want_pages && m->absent) {
1.1 root 747: vm_pageout_inactive_absent++;
748: reclaim_page:
749: vm_page_free(m);
750: vm_page_unlock_queues();
751: vm_object_unlock(object);
752: continue;
753: }
754:
755: /*
756: * If it's being used, reactivate.
757: * (Fictitious pages are either busy or absent.)
758: */
759:
760: assert(!m->fictitious);
761: if (m->reference || pmap_is_referenced(m->phys_addr)) {
762: vm_object_unlock(object);
763: vm_page_activate(m);
764: vm_stat.reactivations++;
1.1.1.3 root 765: current_task()->reactivations++;
1.1 root 766: vm_page_unlock_queues();
767: vm_pageout_inactive_used++;
768: continue;
769: }
770:
771: /*
772: * Eliminate all mappings.
773: */
774:
775: m->busy = TRUE;
776: pmap_page_protect(m->phys_addr, VM_PROT_NONE);
777: if (!m->dirty)
778: m->dirty = pmap_is_modified(m->phys_addr);
779:
1.1.1.2 root 780: if (m->external) {
781: /* Figure out if we still care about this
782: page in the limit of externally managed pages.
783: Clean pages don't actually cause system hosage,
784: so it's ok to stop considering them as
785: "consumers" of memory. */
786: if (m->dirty && !m->extcounted) {
787: m->extcounted = TRUE;
788: vm_page_external_count++;
789: } else if (!m->dirty && m->extcounted) {
790: m->extcounted = FALSE;
791: vm_page_external_count--;
792: }
793: }
794:
795: /* If we don't actually need more memory, and the page
796: is not dirty, put it on the tail of the inactive queue
797: and move on to the next page. */
798: if (!want_pages && !m->dirty) {
799: queue_remove (&vm_page_queue_inactive, m,
800: vm_page_t, pageq);
801: queue_enter (&vm_page_queue_inactive, m,
802: vm_page_t, pageq);
803: vm_page_unlock_queues();
804: vm_pageout_inactive_cleaned_external++;
805: continue;
806: }
807:
1.1 root 808: /*
809: * If it's clean and not precious, we can free the page.
810: */
811:
812: if (!m->dirty && !m->precious) {
813: vm_pageout_inactive_clean++;
814: goto reclaim_page;
815: }
816:
817: /*
818: * If we are very low on memory, then we can't
819: * rely on an external pager to clean a dirty page,
820: * because external pagers are not vm-privileged.
821: *
822: * The laundry bit tells vm_pageout_setup to
823: * put the page back at the front of the inactive
824: * queue instead of activating the page. Hence,
825: * we will pick the page up again immediately and
826: * resend it to the default pager.
827: */
828:
829: assert(!m->laundry);
830: if ((free_count < vm_pageout_reserved_internal) &&
831: !object->internal) {
832: m->laundry = TRUE;
833: vm_pageout_inactive_double++;
834: }
835: vm_page_unlock_queues();
836:
837: /*
838: * If there is no memory object for the page, create
839: * one and hand it to the default pager.
840: * [First try to collapse, so we don't create
841: * one unnecessarily.]
842: */
843:
844: if (!object->pager_initialized)
845: vm_object_collapse(object);
846: if (!object->pager_initialized)
847: vm_object_pager_create(object);
848: if (!object->pager_initialized)
849: panic("vm_pageout_scan");
850:
851: vm_pageout_inactive_dirty++;
852: vm_pageout_page(m, FALSE, TRUE); /* flush it */
853: vm_object_unlock(object);
854: burst_count++;
855: }
856: }
857:
1.1.1.4 root 858: void vm_pageout_scan_continue(void)
1.1 root 859: {
860: /*
861: * We just paused to let the pagers catch up.
862: * If vm_page_laundry_count is still high,
863: * then we aren't waiting long enough.
864: * If we have paused some vm_pageout_pause_max times without
865: * adjusting vm_pageout_burst_wait, it might be too big,
866: * so we decrease it.
867: */
868:
869: vm_page_lock_queues();
870: if (vm_page_laundry_count > vm_pageout_burst_min) {
871: vm_pageout_burst_wait++;
872: vm_pageout_pause_count = 0;
873: } else if (++vm_pageout_pause_count > vm_pageout_pause_max) {
874: vm_pageout_burst_wait = (vm_pageout_burst_wait * 3) / 4;
875: if (vm_pageout_burst_wait < 1)
876: vm_pageout_burst_wait = 1;
877: vm_pageout_pause_count = 0;
878: }
879: vm_page_unlock_queues();
880:
881: vm_pageout_continue();
882: /*NOTREACHED*/
883: }
884:
885: /*
886: * vm_pageout is the high level pageout daemon.
887: */
888:
1.1.1.4 root 889: void vm_pageout_continue(void)
1.1 root 890: {
891: /*
892: * The pageout daemon is never done, so loop forever.
893: * We should call vm_pageout_scan at least once each
894: * time we are woken, even if vm_page_free_wanted is
895: * zero, to check vm_page_free_target and
896: * vm_page_inactive_target.
897: */
898:
899: for (;;) {
900: vm_pageout_scan();
901: /* we hold vm_page_queue_free_lock now */
902: assert(vm_page_free_wanted == 0);
903:
904: assert_wait(&vm_page_free_wanted, FALSE);
905: simple_unlock(&vm_page_queue_free_lock);
906: counter(c_vm_pageout_block++);
907: thread_block(vm_pageout_continue);
908: }
909: }
910:
1.1.1.4 root 911: void vm_pageout(void)
1.1 root 912: {
913: int free_after_reserve;
914:
915: current_thread()->vm_privilege = TRUE;
916: stack_privilege(current_thread());
1.1.1.4 root 917: thread_set_own_priority(0);
1.1 root 918:
919: /*
920: * Initialize some paging parameters.
921: */
922:
923: if (vm_pageout_burst_max == 0)
924: vm_pageout_burst_max = VM_PAGEOUT_BURST_MAX;
925:
926: if (vm_pageout_burst_min == 0)
927: vm_pageout_burst_min = VM_PAGEOUT_BURST_MIN;
928:
929: if (vm_pageout_burst_wait == 0)
930: vm_pageout_burst_wait = VM_PAGEOUT_BURST_WAIT;
931:
932: if (vm_pageout_empty_wait == 0)
933: vm_pageout_empty_wait = VM_PAGEOUT_EMPTY_WAIT;
934:
935: if (vm_page_free_reserved == 0)
936: vm_page_free_reserved = VM_PAGE_FREE_RESERVED;
937:
938: if (vm_pageout_pause_max == 0)
939: vm_pageout_pause_max = VM_PAGEOUT_PAUSE_MAX;
940:
941: if (vm_pageout_reserved_internal == 0)
942: vm_pageout_reserved_internal =
943: VM_PAGEOUT_RESERVED_INTERNAL(vm_page_free_reserved);
944:
945: if (vm_pageout_reserved_really == 0)
946: vm_pageout_reserved_really =
947: VM_PAGEOUT_RESERVED_REALLY(vm_page_free_reserved);
948:
949: free_after_reserve = vm_page_free_count - vm_page_free_reserved;
950:
1.1.1.2 root 951: if (vm_page_external_limit == 0)
952: vm_page_external_limit =
953: VM_PAGE_EXTERNAL_LIMIT (free_after_reserve);
954:
955: if (vm_page_external_target == 0)
956: vm_page_external_target =
957: VM_PAGE_EXTERNAL_TARGET (free_after_reserve);
958:
1.1 root 959: if (vm_page_free_min == 0)
960: vm_page_free_min = vm_page_free_reserved +
961: VM_PAGE_FREE_MIN(free_after_reserve);
962:
963: if (vm_page_free_target == 0)
964: vm_page_free_target = vm_page_free_reserved +
965: VM_PAGE_FREE_TARGET(free_after_reserve);
966:
967: if (vm_page_free_target < vm_page_free_min + 5)
968: vm_page_free_target = vm_page_free_min + 5;
969:
970: /*
971: * vm_pageout_scan will set vm_page_inactive_target.
972: */
973:
974: vm_pageout_continue();
975: /*NOTREACHED*/
976: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.