1 /* 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 41 * 42 * 43 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 44 * All rights reserved. 45 * 46 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 * 68 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $ 69 * $DragonFly: src/sys/vm/vm_pageout.c,v 1.2 2003/06/17 04:29:00 dillon Exp $ 70 */ 71 72 /* 73 * The proverbial page-out daemon. 74 */ 75 76 #include "opt_vm.h" 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/kernel.h> 80 #include <sys/proc.h> 81 #include <sys/kthread.h> 82 #include <sys/resourcevar.h> 83 #include <sys/signalvar.h> 84 #include <sys/vnode.h> 85 #include <sys/vmmeter.h> 86 #include <sys/sysctl.h> 87 88 #include <vm/vm.h> 89 #include <vm/vm_param.h> 90 #include <sys/lock.h> 91 #include <vm/vm_object.h> 92 #include <vm/vm_page.h> 93 #include <vm/vm_map.h> 94 #include <vm/vm_pageout.h> 95 #include <vm/vm_pager.h> 96 #include <vm/swap_pager.h> 97 #include <vm/vm_extern.h> 98 99 /* 100 * System initialization 101 */ 102 103 /* the kernel process "vm_pageout"*/ 104 static void vm_pageout __P((void)); 105 static int vm_pageout_clean __P((vm_page_t)); 106 static void vm_pageout_scan __P((int pass)); 107 static int vm_pageout_free_page_calc __P((vm_size_t count)); 108 struct proc *pageproc; 109 110 static struct kproc_desc page_kp = { 111 "pagedaemon", 112 vm_pageout, 113 &pageproc 114 }; 115 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 116 117 #if !defined(NO_SWAPPING) 118 /* the kernel process "vm_daemon"*/ 119 static void vm_daemon __P((void)); 120 static struct proc *vmproc; 121 122 static struct kproc_desc vm_kp = { 123 "vmdaemon", 124 vm_daemon, 125 &vmproc 126 }; 127 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 128 #endif 129 130 131 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 132 int vm_pageout_deficit=0; /* Estimated number of pages deficit */ 133 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 134 135 #if !defined(NO_SWAPPING) 136 static int vm_pageout_req_swapout; /* XXX */ 137 static int vm_daemon_needed; 138 #endif 139 extern int vm_swap_size; 140 static int vm_max_launder = 32; 141 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 142 static int vm_pageout_full_stats_interval = 0; 143 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; 144 static int defer_swap_pageouts=0; 145 static int disable_swap_pageouts=0; 146 147 #if defined(NO_SWAPPING) 148 static int vm_swap_enabled=0; 149 static int vm_swap_idle_enabled=0; 150 #else 151 static int vm_swap_enabled=1; 152 static int vm_swap_idle_enabled=0; 153 #endif 154 155 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 156 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 157 158 SYSCTL_INT(_vm, OID_AUTO, max_launder, 159 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 160 161 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 162 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 163 164 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 165 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 166 167 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 168 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 169 170 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 171 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 172 173 #if defined(NO_SWAPPING) 174 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 175 CTLFLAG_RD, &vm_swap_enabled, 0, ""); 176 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 177 CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 178 #else 179 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 180 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 181 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 182 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 183 #endif 184 185 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 186 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 187 188 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 189 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 190 191 static int pageout_lock_miss; 192 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 193 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 194 195 #define VM_PAGEOUT_PAGE_COUNT 16 196 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 197 198 int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 199 200 #if !defined(NO_SWAPPING) 201 typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 202 static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 203 static freeer_fcn_t vm_pageout_object_deactivate_pages; 204 static void vm_req_vmdaemon __P((void)); 205 #endif 206 static void vm_pageout_page_stats(void); 207 208 /* 209 * vm_pageout_clean: 210 * 211 * Clean the page and remove it from the laundry. 212 * 213 * We set the busy bit to cause potential page faults on this page to 214 * block. Note the careful timing, however, the busy bit isn't set till 215 * late and we cannot do anything that will mess with the page. 216 */ 217 218 static int 219 vm_pageout_clean(m) 220 vm_page_t m; 221 { 222 register vm_object_t object; 223 vm_page_t mc[2*vm_pageout_page_count]; 224 int pageout_count; 225 int ib, is, page_base; 226 vm_pindex_t pindex = m->pindex; 227 228 object = m->object; 229 230 /* 231 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 232 * with the new swapper, but we could have serious problems paging 233 * out other object types if there is insufficient memory. 234 * 235 * Unfortunately, checking free memory here is far too late, so the 236 * check has been moved up a procedural level. 237 */ 238 239 /* 240 * Don't mess with the page if it's busy, held, or special 241 */ 242 if ((m->hold_count != 0) || 243 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 244 return 0; 245 } 246 247 mc[vm_pageout_page_count] = m; 248 pageout_count = 1; 249 page_base = vm_pageout_page_count; 250 ib = 1; 251 is = 1; 252 253 /* 254 * Scan object for clusterable pages. 255 * 256 * We can cluster ONLY if: ->> the page is NOT 257 * clean, wired, busy, held, or mapped into a 258 * buffer, and one of the following: 259 * 1) The page is inactive, or a seldom used 260 * active page. 261 * -or- 262 * 2) we force the issue. 263 * 264 * During heavy mmap/modification loads the pageout 265 * daemon can really fragment the underlying file 266 * due to flushing pages out of order and not trying 267 * align the clusters (which leave sporatic out-of-order 268 * holes). To solve this problem we do the reverse scan 269 * first and attempt to align our cluster, then do a 270 * forward scan if room remains. 271 */ 272 273 more: 274 while (ib && pageout_count < vm_pageout_page_count) { 275 vm_page_t p; 276 277 if (ib > pindex) { 278 ib = 0; 279 break; 280 } 281 282 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 283 ib = 0; 284 break; 285 } 286 if (((p->queue - p->pc) == PQ_CACHE) || 287 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 288 ib = 0; 289 break; 290 } 291 vm_page_test_dirty(p); 292 if ((p->dirty & p->valid) == 0 || 293 p->queue != PQ_INACTIVE || 294 p->wire_count != 0 || /* may be held by buf cache */ 295 p->hold_count != 0) { /* may be undergoing I/O */ 296 ib = 0; 297 break; 298 } 299 mc[--page_base] = p; 300 ++pageout_count; 301 ++ib; 302 /* 303 * alignment boundry, stop here and switch directions. Do 304 * not clear ib. 305 */ 306 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 307 break; 308 } 309 310 while (pageout_count < vm_pageout_page_count && 311 pindex + is < object->size) { 312 vm_page_t p; 313 314 if ((p = vm_page_lookup(object, pindex + is)) == NULL) 315 break; 316 if (((p->queue - p->pc) == PQ_CACHE) || 317 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 318 break; 319 } 320 vm_page_test_dirty(p); 321 if ((p->dirty & p->valid) == 0 || 322 p->queue != PQ_INACTIVE || 323 p->wire_count != 0 || /* may be held by buf cache */ 324 p->hold_count != 0) { /* may be undergoing I/O */ 325 break; 326 } 327 mc[page_base + pageout_count] = p; 328 ++pageout_count; 329 ++is; 330 } 331 332 /* 333 * If we exhausted our forward scan, continue with the reverse scan 334 * when possible, even past a page boundry. This catches boundry 335 * conditions. 336 */ 337 if (ib && pageout_count < vm_pageout_page_count) 338 goto more; 339 340 /* 341 * we allow reads during pageouts... 342 */ 343 return vm_pageout_flush(&mc[page_base], pageout_count, 0); 344 } 345 346 /* 347 * vm_pageout_flush() - launder the given pages 348 * 349 * The given pages are laundered. Note that we setup for the start of 350 * I/O ( i.e. busy the page ), mark it read-only, and bump the object 351 * reference count all in here rather then in the parent. If we want 352 * the parent to do more sophisticated things we may have to change 353 * the ordering. 354 */ 355 356 int 357 vm_pageout_flush(mc, count, flags) 358 vm_page_t *mc; 359 int count; 360 int flags; 361 { 362 register vm_object_t object; 363 int pageout_status[count]; 364 int numpagedout = 0; 365 int i; 366 367 /* 368 * Initiate I/O. Bump the vm_page_t->busy counter and 369 * mark the pages read-only. 370 * 371 * We do not have to fixup the clean/dirty bits here... we can 372 * allow the pager to do it after the I/O completes. 373 */ 374 375 for (i = 0; i < count; i++) { 376 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); 377 vm_page_io_start(mc[i]); 378 vm_page_protect(mc[i], VM_PROT_READ); 379 } 380 381 object = mc[0]->object; 382 vm_object_pip_add(object, count); 383 384 vm_pager_put_pages(object, mc, count, 385 (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)), 386 pageout_status); 387 388 for (i = 0; i < count; i++) { 389 vm_page_t mt = mc[i]; 390 391 switch (pageout_status[i]) { 392 case VM_PAGER_OK: 393 numpagedout++; 394 break; 395 case VM_PAGER_PEND: 396 numpagedout++; 397 break; 398 case VM_PAGER_BAD: 399 /* 400 * Page outside of range of object. Right now we 401 * essentially lose the changes by pretending it 402 * worked. 403 */ 404 pmap_clear_modify(mt); 405 vm_page_undirty(mt); 406 break; 407 case VM_PAGER_ERROR: 408 case VM_PAGER_FAIL: 409 /* 410 * If page couldn't be paged out, then reactivate the 411 * page so it doesn't clog the inactive list. (We 412 * will try paging out it again later). 413 */ 414 vm_page_activate(mt); 415 break; 416 case VM_PAGER_AGAIN: 417 break; 418 } 419 420 /* 421 * If the operation is still going, leave the page busy to 422 * block all other accesses. Also, leave the paging in 423 * progress indicator set so that we don't attempt an object 424 * collapse. 425 */ 426 if (pageout_status[i] != VM_PAGER_PEND) { 427 vm_object_pip_wakeup(object); 428 vm_page_io_finish(mt); 429 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) 430 vm_page_protect(mt, VM_PROT_READ); 431 } 432 } 433 return numpagedout; 434 } 435 436 #if !defined(NO_SWAPPING) 437 /* 438 * vm_pageout_object_deactivate_pages 439 * 440 * deactivate enough pages to satisfy the inactive target 441 * requirements or if vm_page_proc_limit is set, then 442 * deactivate all of the pages in the object and its 443 * backing_objects. 444 * 445 * The object and map must be locked. 446 */ 447 static void 448 vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 449 vm_map_t map; 450 vm_object_t object; 451 vm_pindex_t desired; 452 int map_remove_only; 453 { 454 register vm_page_t p, next; 455 int rcount; 456 int remove_mode; 457 int s; 458 459 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) 460 return; 461 462 while (object) { 463 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 464 return; 465 if (object->paging_in_progress) 466 return; 467 468 remove_mode = map_remove_only; 469 if (object->shadow_count > 1) 470 remove_mode = 1; 471 /* 472 * scan the objects entire memory queue 473 */ 474 rcount = object->resident_page_count; 475 p = TAILQ_FIRST(&object->memq); 476 while (p && (rcount-- > 0)) { 477 int actcount; 478 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 479 return; 480 next = TAILQ_NEXT(p, listq); 481 cnt.v_pdpages++; 482 if (p->wire_count != 0 || 483 p->hold_count != 0 || 484 p->busy != 0 || 485 (p->flags & (PG_BUSY|PG_UNMANAGED)) || 486 !pmap_page_exists_quick(vm_map_pmap(map), p)) { 487 p = next; 488 continue; 489 } 490 491 actcount = pmap_ts_referenced(p); 492 if (actcount) { 493 vm_page_flag_set(p, PG_REFERENCED); 494 } else if (p->flags & PG_REFERENCED) { 495 actcount = 1; 496 } 497 498 if ((p->queue != PQ_ACTIVE) && 499 (p->flags & PG_REFERENCED)) { 500 vm_page_activate(p); 501 p->act_count += actcount; 502 vm_page_flag_clear(p, PG_REFERENCED); 503 } else if (p->queue == PQ_ACTIVE) { 504 if ((p->flags & PG_REFERENCED) == 0) { 505 p->act_count -= min(p->act_count, ACT_DECLINE); 506 if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) { 507 vm_page_protect(p, VM_PROT_NONE); 508 vm_page_deactivate(p); 509 } else { 510 s = splvm(); 511 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 512 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 513 splx(s); 514 } 515 } else { 516 vm_page_activate(p); 517 vm_page_flag_clear(p, PG_REFERENCED); 518 if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 519 p->act_count += ACT_ADVANCE; 520 s = splvm(); 521 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 522 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 523 splx(s); 524 } 525 } else if (p->queue == PQ_INACTIVE) { 526 vm_page_protect(p, VM_PROT_NONE); 527 } 528 p = next; 529 } 530 object = object->backing_object; 531 } 532 return; 533 } 534 535 /* 536 * deactivate some number of pages in a map, try to do it fairly, but 537 * that is really hard to do. 538 */ 539 static void 540 vm_pageout_map_deactivate_pages(map, desired) 541 vm_map_t map; 542 vm_pindex_t desired; 543 { 544 vm_map_entry_t tmpe; 545 vm_object_t obj, bigobj; 546 int nothingwired; 547 548 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) { 549 return; 550 } 551 552 bigobj = NULL; 553 nothingwired = TRUE; 554 555 /* 556 * first, search out the biggest object, and try to free pages from 557 * that. 558 */ 559 tmpe = map->header.next; 560 while (tmpe != &map->header) { 561 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 562 obj = tmpe->object.vm_object; 563 if ((obj != NULL) && (obj->shadow_count <= 1) && 564 ((bigobj == NULL) || 565 (bigobj->resident_page_count < obj->resident_page_count))) { 566 bigobj = obj; 567 } 568 } 569 if (tmpe->wired_count > 0) 570 nothingwired = FALSE; 571 tmpe = tmpe->next; 572 } 573 574 if (bigobj) 575 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 576 577 /* 578 * Next, hunt around for other pages to deactivate. We actually 579 * do this search sort of wrong -- .text first is not the best idea. 580 */ 581 tmpe = map->header.next; 582 while (tmpe != &map->header) { 583 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 584 break; 585 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 586 obj = tmpe->object.vm_object; 587 if (obj) 588 vm_pageout_object_deactivate_pages(map, obj, desired, 0); 589 } 590 tmpe = tmpe->next; 591 }; 592 593 /* 594 * Remove all mappings if a process is swapped out, this will free page 595 * table pages. 596 */ 597 if (desired == 0 && nothingwired) 598 pmap_remove(vm_map_pmap(map), 599 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 600 vm_map_unlock(map); 601 return; 602 } 603 #endif 604 605 /* 606 * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore 607 * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects 608 * which we know can be trivially freed. 609 */ 610 611 void 612 vm_pageout_page_free(vm_page_t m) { 613 vm_object_t object = m->object; 614 int type = object->type; 615 616 if (type == OBJT_SWAP || type == OBJT_DEFAULT) 617 vm_object_reference(object); 618 vm_page_busy(m); 619 vm_page_protect(m, VM_PROT_NONE); 620 vm_page_free(m); 621 if (type == OBJT_SWAP || type == OBJT_DEFAULT) 622 vm_object_deallocate(object); 623 } 624 625 /* 626 * vm_pageout_scan does the dirty work for the pageout daemon. 627 */ 628 static void 629 vm_pageout_scan(int pass) 630 { 631 vm_page_t m, next; 632 struct vm_page marker; 633 int page_shortage, maxscan, pcount; 634 int addl_page_shortage, addl_page_shortage_init; 635 struct proc *p, *bigproc; 636 vm_offset_t size, bigsize; 637 vm_object_t object; 638 int actcount; 639 int vnodes_skipped = 0; 640 int maxlaunder; 641 int s; 642 643 /* 644 * Do whatever cleanup that the pmap code can. 645 */ 646 pmap_collect(); 647 648 addl_page_shortage_init = vm_pageout_deficit; 649 vm_pageout_deficit = 0; 650 651 /* 652 * Calculate the number of pages we want to either free or move 653 * to the cache. 654 */ 655 page_shortage = vm_paging_target() + addl_page_shortage_init; 656 657 /* 658 * Initialize our marker 659 */ 660 bzero(&marker, sizeof(marker)); 661 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 662 marker.queue = PQ_INACTIVE; 663 marker.wire_count = 1; 664 665 /* 666 * Start scanning the inactive queue for pages we can move to the 667 * cache or free. The scan will stop when the target is reached or 668 * we have scanned the entire inactive queue. Note that m->act_count 669 * is not used to form decisions for the inactive queue, only for the 670 * active queue. 671 * 672 * maxlaunder limits the number of dirty pages we flush per scan. 673 * For most systems a smaller value (16 or 32) is more robust under 674 * extreme memory and disk pressure because any unnecessary writes 675 * to disk can result in extreme performance degredation. However, 676 * systems with excessive dirty pages (especially when MAP_NOSYNC is 677 * used) will die horribly with limited laundering. If the pageout 678 * daemon cannot clean enough pages in the first pass, we let it go 679 * all out in succeeding passes. 680 */ 681 if ((maxlaunder = vm_max_launder) <= 1) 682 maxlaunder = 1; 683 if (pass) 684 maxlaunder = 10000; 685 686 rescan0: 687 addl_page_shortage = addl_page_shortage_init; 688 maxscan = cnt.v_inactive_count; 689 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 690 m != NULL && maxscan-- > 0 && page_shortage > 0; 691 m = next) { 692 693 cnt.v_pdpages++; 694 695 if (m->queue != PQ_INACTIVE) { 696 goto rescan0; 697 } 698 699 next = TAILQ_NEXT(m, pageq); 700 701 /* 702 * skip marker pages 703 */ 704 if (m->flags & PG_MARKER) 705 continue; 706 707 /* 708 * A held page may be undergoing I/O, so skip it. 709 */ 710 if (m->hold_count) { 711 s = splvm(); 712 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 713 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 714 splx(s); 715 addl_page_shortage++; 716 continue; 717 } 718 /* 719 * Dont mess with busy pages, keep in the front of the 720 * queue, most likely are being paged out. 721 */ 722 if (m->busy || (m->flags & PG_BUSY)) { 723 addl_page_shortage++; 724 continue; 725 } 726 727 /* 728 * If the object is not being used, we ignore previous 729 * references. 730 */ 731 if (m->object->ref_count == 0) { 732 vm_page_flag_clear(m, PG_REFERENCED); 733 pmap_clear_reference(m); 734 735 /* 736 * Otherwise, if the page has been referenced while in the 737 * inactive queue, we bump the "activation count" upwards, 738 * making it less likely that the page will be added back to 739 * the inactive queue prematurely again. Here we check the 740 * page tables (or emulated bits, if any), given the upper 741 * level VM system not knowing anything about existing 742 * references. 743 */ 744 } else if (((m->flags & PG_REFERENCED) == 0) && 745 (actcount = pmap_ts_referenced(m))) { 746 vm_page_activate(m); 747 m->act_count += (actcount + ACT_ADVANCE); 748 continue; 749 } 750 751 /* 752 * If the upper level VM system knows about any page 753 * references, we activate the page. We also set the 754 * "activation count" higher than normal so that we will less 755 * likely place pages back onto the inactive queue again. 756 */ 757 if ((m->flags & PG_REFERENCED) != 0) { 758 vm_page_flag_clear(m, PG_REFERENCED); 759 actcount = pmap_ts_referenced(m); 760 vm_page_activate(m); 761 m->act_count += (actcount + ACT_ADVANCE + 1); 762 continue; 763 } 764 765 /* 766 * If the upper level VM system doesn't know anything about 767 * the page being dirty, we have to check for it again. As 768 * far as the VM code knows, any partially dirty pages are 769 * fully dirty. 770 */ 771 if (m->dirty == 0) { 772 vm_page_test_dirty(m); 773 } else { 774 vm_page_dirty(m); 775 } 776 777 /* 778 * Invalid pages can be easily freed 779 */ 780 if (m->valid == 0) { 781 vm_pageout_page_free(m); 782 cnt.v_dfree++; 783 --page_shortage; 784 785 /* 786 * Clean pages can be placed onto the cache queue. This 787 * effectively frees them. 788 */ 789 } else if (m->dirty == 0) { 790 /* 791 * Clean pages can be immediately freed to the cache. 792 */ 793 vm_page_cache(m); 794 --page_shortage; 795 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 796 /* 797 * Dirty pages need to be paged out, but flushing 798 * a page is extremely expensive verses freeing 799 * a clean page. Rather then artificially limiting 800 * the number of pages we can flush, we instead give 801 * dirty pages extra priority on the inactive queue 802 * by forcing them to be cycled through the queue 803 * twice before being flushed, after which the 804 * (now clean) page will cycle through once more 805 * before being freed. This significantly extends 806 * the thrash point for a heavily loaded machine. 807 */ 808 s = splvm(); 809 vm_page_flag_set(m, PG_WINATCFLS); 810 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 811 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 812 splx(s); 813 } else if (maxlaunder > 0) { 814 /* 815 * We always want to try to flush some dirty pages if 816 * we encounter them, to keep the system stable. 817 * Normally this number is small, but under extreme 818 * pressure where there are insufficient clean pages 819 * on the inactive queue, we may have to go all out. 820 */ 821 int swap_pageouts_ok; 822 struct vnode *vp = NULL; 823 824 object = m->object; 825 826 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 827 swap_pageouts_ok = 1; 828 } else { 829 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 830 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 831 vm_page_count_min()); 832 833 } 834 835 /* 836 * We don't bother paging objects that are "dead". 837 * Those objects are in a "rundown" state. 838 */ 839 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 840 s = splvm(); 841 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 842 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 843 splx(s); 844 continue; 845 } 846 847 /* 848 * The object is already known NOT to be dead. It 849 * is possible for the vget() to block the whole 850 * pageout daemon, but the new low-memory handling 851 * code should prevent it. 852 * 853 * The previous code skipped locked vnodes and, worse, 854 * reordered pages in the queue. This results in 855 * completely non-deterministic operation because, 856 * quite often, a vm_fault has initiated an I/O and 857 * is holding a locked vnode at just the point where 858 * the pageout daemon is woken up. 859 * 860 * We can't wait forever for the vnode lock, we might 861 * deadlock due to a vn_read() getting stuck in 862 * vm_wait while holding this vnode. We skip the 863 * vnode if we can't get it in a reasonable amount 864 * of time. 865 */ 866 867 if (object->type == OBJT_VNODE) { 868 vp = object->handle; 869 870 if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ|LK_TIMELOCK, curproc)) { 871 ++pageout_lock_miss; 872 if (object->flags & OBJ_MIGHTBEDIRTY) 873 vnodes_skipped++; 874 continue; 875 } 876 877 /* 878 * The page might have been moved to another 879 * queue during potential blocking in vget() 880 * above. The page might have been freed and 881 * reused for another vnode. The object might 882 * have been reused for another vnode. 883 */ 884 if (m->queue != PQ_INACTIVE || 885 m->object != object || 886 object->handle != vp) { 887 if (object->flags & OBJ_MIGHTBEDIRTY) 888 vnodes_skipped++; 889 vput(vp); 890 continue; 891 } 892 893 /* 894 * The page may have been busied during the 895 * blocking in vput(); We don't move the 896 * page back onto the end of the queue so that 897 * statistics are more correct if we don't. 898 */ 899 if (m->busy || (m->flags & PG_BUSY)) { 900 vput(vp); 901 continue; 902 } 903 904 /* 905 * If the page has become held it might 906 * be undergoing I/O, so skip it 907 */ 908 if (m->hold_count) { 909 s = splvm(); 910 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 911 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 912 splx(s); 913 if (object->flags & OBJ_MIGHTBEDIRTY) 914 vnodes_skipped++; 915 vput(vp); 916 continue; 917 } 918 } 919 920 /* 921 * If a page is dirty, then it is either being washed 922 * (but not yet cleaned) or it is still in the 923 * laundry. If it is still in the laundry, then we 924 * start the cleaning operation. 925 * 926 * This operation may cluster, invalidating the 'next' 927 * pointer. To prevent an inordinate number of 928 * restarts we use our marker to remember our place. 929 * 930 * decrement page_shortage on success to account for 931 * the (future) cleaned page. Otherwise we could wind 932 * up laundering or cleaning too many pages. 933 */ 934 s = splvm(); 935 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 936 splx(s); 937 if (vm_pageout_clean(m) != 0) { 938 --page_shortage; 939 --maxlaunder; 940 } 941 s = splvm(); 942 next = TAILQ_NEXT(&marker, pageq); 943 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 944 splx(s); 945 if (vp != NULL) 946 vput(vp); 947 } 948 } 949 950 /* 951 * Compute the number of pages we want to try to move from the 952 * active queue to the inactive queue. 953 */ 954 page_shortage = vm_paging_target() + 955 cnt.v_inactive_target - cnt.v_inactive_count; 956 page_shortage += addl_page_shortage; 957 958 /* 959 * Scan the active queue for things we can deactivate. We nominally 960 * track the per-page activity counter and use it to locate 961 * deactivation candidates. 962 */ 963 964 pcount = cnt.v_active_count; 965 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 966 967 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 968 969 /* 970 * This is a consistency check, and should likely be a panic 971 * or warning. 972 */ 973 if (m->queue != PQ_ACTIVE) { 974 break; 975 } 976 977 next = TAILQ_NEXT(m, pageq); 978 /* 979 * Don't deactivate pages that are busy. 980 */ 981 if ((m->busy != 0) || 982 (m->flags & PG_BUSY) || 983 (m->hold_count != 0)) { 984 s = splvm(); 985 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 986 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 987 splx(s); 988 m = next; 989 continue; 990 } 991 992 /* 993 * The count for pagedaemon pages is done after checking the 994 * page for eligibility... 995 */ 996 cnt.v_pdpages++; 997 998 /* 999 * Check to see "how much" the page has been used. 1000 */ 1001 actcount = 0; 1002 if (m->object->ref_count != 0) { 1003 if (m->flags & PG_REFERENCED) { 1004 actcount += 1; 1005 } 1006 actcount += pmap_ts_referenced(m); 1007 if (actcount) { 1008 m->act_count += ACT_ADVANCE + actcount; 1009 if (m->act_count > ACT_MAX) 1010 m->act_count = ACT_MAX; 1011 } 1012 } 1013 1014 /* 1015 * Since we have "tested" this bit, we need to clear it now. 1016 */ 1017 vm_page_flag_clear(m, PG_REFERENCED); 1018 1019 /* 1020 * Only if an object is currently being used, do we use the 1021 * page activation count stats. 1022 */ 1023 if (actcount && (m->object->ref_count != 0)) { 1024 s = splvm(); 1025 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1026 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1027 splx(s); 1028 } else { 1029 m->act_count -= min(m->act_count, ACT_DECLINE); 1030 if (vm_pageout_algorithm || 1031 m->object->ref_count == 0 || 1032 m->act_count == 0) { 1033 page_shortage--; 1034 if (m->object->ref_count == 0) { 1035 vm_page_protect(m, VM_PROT_NONE); 1036 if (m->dirty == 0) 1037 vm_page_cache(m); 1038 else 1039 vm_page_deactivate(m); 1040 } else { 1041 vm_page_deactivate(m); 1042 } 1043 } else { 1044 s = splvm(); 1045 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1046 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1047 splx(s); 1048 } 1049 } 1050 m = next; 1051 } 1052 1053 s = splvm(); 1054 1055 /* 1056 * We try to maintain some *really* free pages, this allows interrupt 1057 * code to be guaranteed space. Since both cache and free queues 1058 * are considered basically 'free', moving pages from cache to free 1059 * does not effect other calculations. 1060 */ 1061 1062 while (cnt.v_free_count < cnt.v_free_reserved) { 1063 static int cache_rover = 0; 1064 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 1065 if (!m) 1066 break; 1067 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || 1068 m->busy || 1069 m->hold_count || 1070 m->wire_count) { 1071 #ifdef INVARIANTS 1072 printf("Warning: busy page %p found in cache\n", m); 1073 #endif 1074 vm_page_deactivate(m); 1075 continue; 1076 } 1077 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1078 vm_pageout_page_free(m); 1079 cnt.v_dfree++; 1080 } 1081 splx(s); 1082 1083 #if !defined(NO_SWAPPING) 1084 /* 1085 * Idle process swapout -- run once per second. 1086 */ 1087 if (vm_swap_idle_enabled) { 1088 static long lsec; 1089 if (time_second != lsec) { 1090 vm_pageout_req_swapout |= VM_SWAP_IDLE; 1091 vm_req_vmdaemon(); 1092 lsec = time_second; 1093 } 1094 } 1095 #endif 1096 1097 /* 1098 * If we didn't get enough free pages, and we have skipped a vnode 1099 * in a writeable object, wakeup the sync daemon. And kick swapout 1100 * if we did not get enough free pages. 1101 */ 1102 if (vm_paging_target() > 0) { 1103 if (vnodes_skipped && vm_page_count_min()) 1104 (void) speedup_syncer(); 1105 #if !defined(NO_SWAPPING) 1106 if (vm_swap_enabled && vm_page_count_target()) { 1107 vm_req_vmdaemon(); 1108 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1109 } 1110 #endif 1111 } 1112 1113 /* 1114 * If we are out of swap and were not able to reach our paging 1115 * target, kill the largest process. 1116 */ 1117 if ((vm_swap_size < 64 && vm_page_count_min()) || 1118 (swap_pager_full && vm_paging_target() > 0)) { 1119 #if 0 1120 if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) { 1121 #endif 1122 bigproc = NULL; 1123 bigsize = 0; 1124 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1125 /* 1126 * if this is a system process, skip it 1127 */ 1128 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 1129 ((p->p_pid < 48) && (vm_swap_size != 0))) { 1130 continue; 1131 } 1132 /* 1133 * if the process is in a non-running type state, 1134 * don't touch it. 1135 */ 1136 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1137 continue; 1138 } 1139 /* 1140 * get the process size 1141 */ 1142 size = vmspace_resident_count(p->p_vmspace) + 1143 vmspace_swap_count(p->p_vmspace); 1144 /* 1145 * if the this process is bigger than the biggest one 1146 * remember it. 1147 */ 1148 if (size > bigsize) { 1149 bigproc = p; 1150 bigsize = size; 1151 } 1152 } 1153 if (bigproc != NULL) { 1154 killproc(bigproc, "out of swap space"); 1155 bigproc->p_estcpu = 0; 1156 bigproc->p_nice = PRIO_MIN; 1157 resetpriority(bigproc); 1158 wakeup(&cnt.v_free_count); 1159 } 1160 } 1161 } 1162 1163 /* 1164 * This routine tries to maintain the pseudo LRU active queue, 1165 * so that during long periods of time where there is no paging, 1166 * that some statistic accumulation still occurs. This code 1167 * helps the situation where paging just starts to occur. 1168 */ 1169 static void 1170 vm_pageout_page_stats() 1171 { 1172 int s; 1173 vm_page_t m,next; 1174 int pcount,tpcount; /* Number of pages to check */ 1175 static int fullintervalcount = 0; 1176 int page_shortage; 1177 int s0; 1178 1179 page_shortage = 1180 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1181 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1182 1183 if (page_shortage <= 0) 1184 return; 1185 1186 s0 = splvm(); 1187 1188 pcount = cnt.v_active_count; 1189 fullintervalcount += vm_pageout_stats_interval; 1190 if (fullintervalcount < vm_pageout_full_stats_interval) { 1191 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1192 if (pcount > tpcount) 1193 pcount = tpcount; 1194 } else { 1195 fullintervalcount = 0; 1196 } 1197 1198 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1199 while ((m != NULL) && (pcount-- > 0)) { 1200 int actcount; 1201 1202 if (m->queue != PQ_ACTIVE) { 1203 break; 1204 } 1205 1206 next = TAILQ_NEXT(m, pageq); 1207 /* 1208 * Don't deactivate pages that are busy. 1209 */ 1210 if ((m->busy != 0) || 1211 (m->flags & PG_BUSY) || 1212 (m->hold_count != 0)) { 1213 s = splvm(); 1214 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1215 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1216 splx(s); 1217 m = next; 1218 continue; 1219 } 1220 1221 actcount = 0; 1222 if (m->flags & PG_REFERENCED) { 1223 vm_page_flag_clear(m, PG_REFERENCED); 1224 actcount += 1; 1225 } 1226 1227 actcount += pmap_ts_referenced(m); 1228 if (actcount) { 1229 m->act_count += ACT_ADVANCE + actcount; 1230 if (m->act_count > ACT_MAX) 1231 m->act_count = ACT_MAX; 1232 s = splvm(); 1233 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1234 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1235 splx(s); 1236 } else { 1237 if (m->act_count == 0) { 1238 /* 1239 * We turn off page access, so that we have 1240 * more accurate RSS stats. We don't do this 1241 * in the normal page deactivation when the 1242 * system is loaded VM wise, because the 1243 * cost of the large number of page protect 1244 * operations would be higher than the value 1245 * of doing the operation. 1246 */ 1247 vm_page_protect(m, VM_PROT_NONE); 1248 vm_page_deactivate(m); 1249 } else { 1250 m->act_count -= min(m->act_count, ACT_DECLINE); 1251 s = splvm(); 1252 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1253 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1254 splx(s); 1255 } 1256 } 1257 1258 m = next; 1259 } 1260 splx(s0); 1261 } 1262 1263 static int 1264 vm_pageout_free_page_calc(count) 1265 vm_size_t count; 1266 { 1267 if (count < cnt.v_page_count) 1268 return 0; 1269 /* 1270 * free_reserved needs to include enough for the largest swap pager 1271 * structures plus enough for any pv_entry structs when paging. 1272 */ 1273 if (cnt.v_page_count > 1024) 1274 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1275 else 1276 cnt.v_free_min = 4; 1277 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1278 cnt.v_interrupt_free_min; 1279 cnt.v_free_reserved = vm_pageout_page_count + 1280 cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 1281 cnt.v_free_severe = cnt.v_free_min / 2; 1282 cnt.v_free_min += cnt.v_free_reserved; 1283 cnt.v_free_severe += cnt.v_free_reserved; 1284 return 1; 1285 } 1286 1287 1288 /* 1289 * vm_pageout is the high level pageout daemon. 1290 */ 1291 static void 1292 vm_pageout() 1293 { 1294 int pass; 1295 1296 /* 1297 * Initialize some paging parameters. 1298 */ 1299 1300 cnt.v_interrupt_free_min = 2; 1301 if (cnt.v_page_count < 2000) 1302 vm_pageout_page_count = 8; 1303 1304 vm_pageout_free_page_calc(cnt.v_page_count); 1305 /* 1306 * v_free_target and v_cache_min control pageout hysteresis. Note 1307 * that these are more a measure of the VM cache queue hysteresis 1308 * then the VM free queue. Specifically, v_free_target is the 1309 * high water mark (free+cache pages). 1310 * 1311 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 1312 * low water mark, while v_free_min is the stop. v_cache_min must 1313 * be big enough to handle memory needs while the pageout daemon 1314 * is signalled and run to free more pages. 1315 */ 1316 if (cnt.v_free_count > 6144) 1317 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1318 else 1319 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1320 1321 if (cnt.v_free_count > 2048) { 1322 cnt.v_cache_min = cnt.v_free_target; 1323 cnt.v_cache_max = 2 * cnt.v_cache_min; 1324 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1325 } else { 1326 cnt.v_cache_min = 0; 1327 cnt.v_cache_max = 0; 1328 cnt.v_inactive_target = cnt.v_free_count / 4; 1329 } 1330 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1331 cnt.v_inactive_target = cnt.v_free_count / 3; 1332 1333 /* XXX does not really belong here */ 1334 if (vm_page_max_wired == 0) 1335 vm_page_max_wired = cnt.v_free_count / 3; 1336 1337 if (vm_pageout_stats_max == 0) 1338 vm_pageout_stats_max = cnt.v_free_target; 1339 1340 /* 1341 * Set interval in seconds for stats scan. 1342 */ 1343 if (vm_pageout_stats_interval == 0) 1344 vm_pageout_stats_interval = 5; 1345 if (vm_pageout_full_stats_interval == 0) 1346 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1347 1348 1349 /* 1350 * Set maximum free per pass 1351 */ 1352 if (vm_pageout_stats_free_max == 0) 1353 vm_pageout_stats_free_max = 5; 1354 1355 swap_pager_swap_init(); 1356 pass = 0; 1357 /* 1358 * The pageout daemon is never done, so loop forever. 1359 */ 1360 while (TRUE) { 1361 int error; 1362 int s = splvm(); 1363 1364 /* 1365 * If we have enough free memory, wakeup waiters. Do 1366 * not clear vm_pages_needed until we reach our target, 1367 * otherwise we may be woken up over and over again and 1368 * waste a lot of cpu. 1369 */ 1370 if (vm_pages_needed && !vm_page_count_min()) { 1371 if (vm_paging_needed() <= 0) 1372 vm_pages_needed = 0; 1373 wakeup(&cnt.v_free_count); 1374 } 1375 if (vm_pages_needed) { 1376 /* 1377 * Still not done, take a second pass without waiting 1378 * (unlimited dirty cleaning), otherwise sleep a bit 1379 * and try again. 1380 */ 1381 ++pass; 1382 if (pass > 1) 1383 tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 1384 } else { 1385 /* 1386 * Good enough, sleep & handle stats. Prime the pass 1387 * for the next run. 1388 */ 1389 if (pass > 1) 1390 pass = 1; 1391 else 1392 pass = 0; 1393 error = tsleep(&vm_pages_needed, 1394 PVM, "psleep", vm_pageout_stats_interval * hz); 1395 if (error && !vm_pages_needed) { 1396 splx(s); 1397 pass = 0; 1398 vm_pageout_page_stats(); 1399 continue; 1400 } 1401 } 1402 1403 if (vm_pages_needed) 1404 cnt.v_pdwakeups++; 1405 splx(s); 1406 vm_pageout_scan(pass); 1407 vm_pageout_deficit = 0; 1408 } 1409 } 1410 1411 void 1412 pagedaemon_wakeup() 1413 { 1414 if (!vm_pages_needed && curproc != pageproc) { 1415 vm_pages_needed++; 1416 wakeup(&vm_pages_needed); 1417 } 1418 } 1419 1420 #if !defined(NO_SWAPPING) 1421 static void 1422 vm_req_vmdaemon() 1423 { 1424 static int lastrun = 0; 1425 1426 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1427 wakeup(&vm_daemon_needed); 1428 lastrun = ticks; 1429 } 1430 } 1431 1432 static void 1433 vm_daemon() 1434 { 1435 struct proc *p; 1436 1437 while (TRUE) { 1438 tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 1439 if (vm_pageout_req_swapout) { 1440 swapout_procs(vm_pageout_req_swapout); 1441 vm_pageout_req_swapout = 0; 1442 } 1443 /* 1444 * scan the processes for exceeding their rlimits or if 1445 * process is swapped out -- deactivate pages 1446 */ 1447 1448 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1449 vm_pindex_t limit, size; 1450 1451 /* 1452 * if this is a system process or if we have already 1453 * looked at this process, skip it. 1454 */ 1455 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1456 continue; 1457 } 1458 /* 1459 * if the process is in a non-running type state, 1460 * don't touch it. 1461 */ 1462 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1463 continue; 1464 } 1465 /* 1466 * get a limit 1467 */ 1468 limit = OFF_TO_IDX( 1469 qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1470 p->p_rlimit[RLIMIT_RSS].rlim_max)); 1471 1472 /* 1473 * let processes that are swapped out really be 1474 * swapped out set the limit to nothing (will force a 1475 * swap-out.) 1476 */ 1477 if ((p->p_flag & P_INMEM) == 0) 1478 limit = 0; /* XXX */ 1479 1480 size = vmspace_resident_count(p->p_vmspace); 1481 if (limit >= 0 && size >= limit) { 1482 vm_pageout_map_deactivate_pages( 1483 &p->p_vmspace->vm_map, limit); 1484 } 1485 } 1486 } 1487 } 1488 #endif 1489