vm_pageout.c revision 3407
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 * $Id: vm_pageout.c,v 1.14 1994/10/05 09:02:53 davidg Exp $ 69 */ 70 71/* 72 * The proverbial page-out daemon. 73 */ 74 75#include <sys/param.h> 76#include <sys/systm.h> 77#include <sys/proc.h> 78#include <sys/resourcevar.h> 79#include <sys/malloc.h> 80 81#include <vm/vm.h> 82#include <vm/vm_page.h> 83#include <vm/vm_pageout.h> 84 85extern vm_map_t kmem_map; 86int vm_pages_needed; /* Event on which pageout daemon sleeps */ 87int vm_pagescanner; /* Event on which pagescanner sleeps */ 88int vm_pageout_free_min = 0; /* Stop pageout to wait for pagers at this free level */ 89 90int vm_pageout_pages_needed = 0; /* flag saying that the pageout daemon needs pages */ 91int vm_page_pagesfreed; 92int vm_desired_cache_size; 93 94extern int npendingio; 95extern int hz; 96int vm_pageout_proc_limit; 97extern int nswiodone; 98extern int swap_pager_full; 99extern int swap_pager_ready(); 100 101#define MAXREF 32767 102 103#define MAXSCAN 512 /* maximum number of pages to scan in active queue */ 104 /* set the "clock" hands to be (MAXSCAN * 4096) Bytes */ 105#define ACT_DECLINE 1 106#define ACT_ADVANCE 3 107#define ACT_MAX 100 108 109#define LOWATER ((2048*1024)/NBPG) 110 111#define VM_PAGEOUT_PAGE_COUNT 8 112int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 113int vm_pageout_req_do_stats; 114 115int vm_page_max_wired = 0; /* XXX max # of wired pages system-wide */ 116 117 118/* 119 * vm_pageout_clean: 120 * cleans a vm_page 121 */ 122int 123vm_pageout_clean(m, sync) 124 register vm_page_t m; 125 int sync; 126{ 127 /* 128 * Clean the page and remove it from the 129 * laundry. 130 * 131 * We set the busy bit to cause 132 * potential page faults on this page to 133 * block. 134 * 135 * And we set pageout-in-progress to keep 136 * the object from disappearing during 137 * pageout. This guarantees that the 138 * page won't move from the inactive 139 * queue. (However, any other page on 140 * the inactive queue may move!) 141 */ 142 143 register vm_object_t object; 144 register vm_pager_t pager; 145 int pageout_status[VM_PAGEOUT_PAGE_COUNT]; 146 vm_page_t ms[VM_PAGEOUT_PAGE_COUNT]; 147 int pageout_count; 148 int anyok=0; 149 int i; 150 vm_offset_t offset = m->offset; 151 152 object = m->object; 153 if (!object) { 154 printf("pager: object missing\n"); 155 return 0; 156 } 157 158 /* 159 * Try to collapse the object before 160 * making a pager for it. We must 161 * unlock the page queues first. 162 * We try to defer the creation of a pager 163 * until all shadows are not paging. This 164 * allows vm_object_collapse to work better and 165 * helps control swap space size. 166 * (J. Dyson 11 Nov 93) 167 */ 168 169 if (!object->pager && 170 cnt.v_free_count < vm_pageout_free_min) 171 return 0; 172 173 if (!object->pager && 174 object->shadow && 175 object->shadow->paging_in_progress) 176 return 0; 177 178 if( !sync) { 179 if (object->shadow) { 180 vm_object_collapse(object); 181 if (!vm_page_lookup(object, offset)) 182 return 0; 183 } 184 185 if ((m->busy != 0) || 186 (m->flags & PG_BUSY) || (m->hold_count != 0)) { 187 return 0; 188 } 189 } 190 191 pageout_count = 1; 192 ms[0] = m; 193 194 pager = object->pager; 195 if (pager) { 196 for (i = 1; i < vm_pageout_page_count; i++) { 197 ms[i] = vm_page_lookup(object, offset+i*NBPG); 198 if (ms[i]) { 199 if (( ((ms[i]->flags & (PG_CLEAN|PG_INACTIVE|PG_BUSY)) == PG_INACTIVE) 200 || ( (ms[i]->flags & (PG_CLEAN|PG_BUSY)) == 0 && sync == VM_PAGEOUT_FORCE)) 201 && (ms[i]->wire_count == 0) 202 && (ms[i]->busy == 0) 203 && (ms[i]->hold_count == 0)) 204 pageout_count++; 205 else 206 break; 207 } else 208 break; 209 } 210 for(i=0;i<pageout_count;i++) { 211 ms[i]->flags |= PG_BUSY; 212 pmap_page_protect(VM_PAGE_TO_PHYS(ms[i]), VM_PROT_READ); 213 } 214 object->paging_in_progress += pageout_count; 215 cnt.v_pageouts++; 216 cnt.v_pgpgout += pageout_count; 217 } else { 218 219 m->flags |= PG_BUSY; 220 221 pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_READ); 222 223 cnt.v_pageouts++; 224 cnt.v_pgpgout++; 225 226 object->paging_in_progress++; 227 228 pager = vm_pager_allocate(PG_DFLT, (caddr_t)0, 229 object->size, VM_PROT_ALL, 0); 230 if (pager != NULL) { 231 vm_object_setpager(object, pager, 0, FALSE); 232 } 233 } 234 235 /* 236 * If there is no pager for the page, 237 * use the default pager. If there's 238 * no place to put the page at the 239 * moment, leave it in the laundry and 240 * hope that there will be paging space 241 * later. 242 */ 243 244 if ((pager && pager->pg_type == PG_SWAP) || 245 cnt.v_free_count >= vm_pageout_free_min) { 246 if( pageout_count == 1) { 247 pageout_status[0] = pager ? 248 vm_pager_put(pager, m, 249 ((sync || (object == kernel_object)) ? TRUE: FALSE)) : 250 VM_PAGER_FAIL; 251 } else { 252 if( !pager) { 253 for(i=0;i<pageout_count;i++) 254 pageout_status[i] = VM_PAGER_FAIL; 255 } else { 256 vm_pager_put_pages(pager, ms, pageout_count, 257 ((sync || (object == kernel_object)) ? TRUE : FALSE), 258 pageout_status); 259 } 260 } 261 262 } else { 263 for(i=0;i<pageout_count;i++) 264 pageout_status[i] = VM_PAGER_FAIL; 265 } 266 267 for(i=0;i<pageout_count;i++) { 268 switch (pageout_status[i]) { 269 case VM_PAGER_OK: 270 ms[i]->flags &= ~PG_LAUNDRY; 271 ++anyok; 272 break; 273 case VM_PAGER_PEND: 274 ms[i]->flags &= ~PG_LAUNDRY; 275 ++anyok; 276 break; 277 case VM_PAGER_BAD: 278 /* 279 * Page outside of range of object. 280 * Right now we essentially lose the 281 * changes by pretending it worked. 282 */ 283 ms[i]->flags &= ~PG_LAUNDRY; 284 ms[i]->flags |= PG_CLEAN; 285 pmap_clear_modify(VM_PAGE_TO_PHYS(ms[i])); 286 break; 287 case VM_PAGER_ERROR: 288 case VM_PAGER_FAIL: 289 /* 290 * If page couldn't be paged out, then 291 * reactivate the page so it doesn't 292 * clog the inactive list. (We will 293 * try paging out it again later). 294 */ 295 if (ms[i]->flags & PG_INACTIVE) 296 vm_page_activate(ms[i]); 297 break; 298 case VM_PAGER_AGAIN: 299 break; 300 } 301 302 303 /* 304 * If the operation is still going, leave 305 * the page busy to block all other accesses. 306 * Also, leave the paging in progress 307 * indicator set so that we don't attempt an 308 * object collapse. 309 */ 310 if (pageout_status[i] != VM_PAGER_PEND) { 311 PAGE_WAKEUP(ms[i]); 312 if (--object->paging_in_progress == 0) 313 wakeup((caddr_t) object); 314 if ((ms[i]->flags & PG_REFERENCED) || 315 pmap_is_referenced(VM_PAGE_TO_PHYS(ms[i]))) { 316 pmap_clear_reference(VM_PAGE_TO_PHYS(ms[i])); 317 ms[i]->flags &= ~PG_REFERENCED; 318 if( ms[i]->flags & PG_INACTIVE) 319 vm_page_activate(ms[i]); 320 } 321 } 322 } 323 return anyok; 324} 325 326/* 327 * vm_pageout_object_deactivate_pages 328 * 329 * deactivate enough pages to satisfy the inactive target 330 * requirements or if vm_page_proc_limit is set, then 331 * deactivate all of the pages in the object and its 332 * shadows. 333 * 334 * The object and map must be locked. 335 */ 336int 337vm_pageout_object_deactivate_pages(map, object, count) 338 vm_map_t map; 339 vm_object_t object; 340 int count; 341{ 342 register vm_page_t p, next; 343 int rcount; 344 int dcount; 345 346 dcount = 0; 347 if (count == 0) 348 count = 1; 349 350 if (object->shadow) { 351 int scount = count; 352 if( object->shadow->ref_count > 1) 353 scount /= object->shadow->ref_count; 354 if( scount) 355 dcount += vm_pageout_object_deactivate_pages(map, object->shadow, scount); 356 } 357 358 if (object->paging_in_progress) 359 return dcount; 360 361 /* 362 * scan the objects entire memory queue 363 */ 364 rcount = object->resident_page_count; 365 p = object->memq.tqh_first; 366 while (p && (rcount-- > 0)) { 367 next = p->listq.tqe_next; 368 vm_page_lock_queues(); 369 /* 370 * if a page is active, not wired and is in the processes pmap, 371 * then deactivate the page. 372 */ 373 if ((p->flags & (PG_ACTIVE|PG_BUSY)) == PG_ACTIVE && 374 p->wire_count == 0 && 375 p->hold_count == 0 && 376 p->busy == 0 && 377 pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 378 if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) && 379 (p->flags & PG_REFERENCED) == 0) { 380 p->act_count -= min(p->act_count, ACT_DECLINE); 381 /* 382 * if the page act_count is zero -- then we deactivate 383 */ 384 if (!p->act_count) { 385 vm_page_deactivate(p); 386 pmap_page_protect(VM_PAGE_TO_PHYS(p), 387 VM_PROT_NONE); 388 /* 389 * else if on the next go-around we will deactivate the page 390 * we need to place the page on the end of the queue to age 391 * the other pages in memory. 392 */ 393 } else { 394 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 395 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 396 TAILQ_REMOVE(&object->memq, p, listq); 397 TAILQ_INSERT_TAIL(&object->memq, p, listq); 398 } 399 /* 400 * see if we are done yet 401 */ 402 if (p->flags & PG_INACTIVE) { 403 --count; 404 ++dcount; 405 if (count <= 0 && 406 cnt.v_inactive_count > cnt.v_inactive_target) { 407 vm_page_unlock_queues(); 408 return dcount; 409 } 410 } 411 412 } else { 413 /* 414 * Move the page to the bottom of the queue. 415 */ 416 pmap_clear_reference(VM_PAGE_TO_PHYS(p)); 417 p->flags &= ~PG_REFERENCED; 418 if (p->act_count < ACT_MAX) 419 p->act_count += ACT_ADVANCE; 420 421 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 422 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 423 TAILQ_REMOVE(&object->memq, p, listq); 424 TAILQ_INSERT_TAIL(&object->memq, p, listq); 425 } 426 } 427 428 vm_page_unlock_queues(); 429 p = next; 430 } 431 return dcount; 432} 433 434 435/* 436 * deactivate some number of pages in a map, try to do it fairly, but 437 * that is really hard to do. 438 */ 439 440void 441vm_pageout_map_deactivate_pages(map, entry, count, freeer) 442 vm_map_t map; 443 vm_map_entry_t entry; 444 int *count; 445 int (*freeer)(vm_map_t, vm_object_t, int); 446{ 447 vm_map_t tmpm; 448 vm_map_entry_t tmpe; 449 vm_object_t obj; 450 if (*count <= 0) 451 return; 452 vm_map_reference(map); 453 if (!lock_try_read(&map->lock)) { 454 vm_map_deallocate(map); 455 return; 456 } 457 if (entry == 0) { 458 tmpe = map->header.next; 459 while (tmpe != &map->header && *count > 0) { 460 vm_pageout_map_deactivate_pages(map, tmpe, count, freeer); 461 tmpe = tmpe->next; 462 }; 463 } else if (entry->is_sub_map || entry->is_a_map) { 464 tmpm = entry->object.share_map; 465 tmpe = tmpm->header.next; 466 while (tmpe != &tmpm->header && *count > 0) { 467 vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer); 468 tmpe = tmpe->next; 469 }; 470 } else if ((obj = entry->object.vm_object) != 0) { 471 *count -= (*freeer)(map, obj, *count); 472 } 473 lock_read_done(&map->lock); 474 vm_map_deallocate(map); 475 return; 476} 477 478/* 479 * vm_pageout_scan does the dirty work for the pageout daemon. 480 */ 481int 482vm_pageout_scan() 483{ 484 vm_page_t m; 485 int page_shortage, maxscan, maxlaunder; 486 int pages_freed; 487 int desired_free; 488 vm_page_t next; 489 struct proc *p; 490 vm_object_t object; 491 int force_wakeup = 0; 492 int cache_size, orig_cache_size; 493 494 /* 495 * We manage the cached memory by attempting to keep it 496 * at about the desired level. 497 * We deactivate the pages for the oldest cached objects 498 * first. This keeps pages that are "cached" from hogging 499 * physical memory. 500 */ 501 orig_cache_size = 0; 502 object = vm_object_cached_list.tqh_first; 503 504 /* calculate the total cached size */ 505 506 while( object) { 507 orig_cache_size += object->resident_page_count; 508 object = object->cached_list.tqe_next; 509 } 510 511redeact: 512 cache_size = orig_cache_size; 513 object = vm_object_cached_list.tqh_first; 514 vm_object_cache_lock(); 515 while ( object && (cnt.v_inactive_count < cnt.v_inactive_target) && 516 (cache_size >= vm_desired_cache_size)) { 517 vm_object_cache_unlock(); 518 519 if (object != vm_object_lookup(object->pager)) 520 panic("vm_object_deactivate: I'm sooo confused."); 521 522 /* 523 * if there are no resident pages -- get rid of the object 524 */ 525 if( object->resident_page_count == 0) { 526 pager_cache(object, FALSE); 527 goto redeact; 528 } else { 529 /* 530 * if there are resident pages -- deactivate them 531 */ 532 vm_object_deactivate_pages(object); 533 cache_size -= object->resident_page_count; 534 object = object->cached_list.tqe_next; 535 } 536 537 vm_object_cache_lock(); 538 } 539 vm_object_cache_unlock(); 540 541morefree: 542 /* 543 * now swap processes out if we are in low memory conditions 544 */ 545 if (cnt.v_free_count <= cnt.v_free_min) { 546 /* 547 * swap out inactive processes 548 */ 549 swapout_threads(); 550 } 551 552 /* 553 * scan the processes for exceeding their rlimits or if process 554 * is swapped out -- deactivate pages 555 */ 556 557 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 558 vm_offset_t size; 559 int overage; 560 quad_t limit; 561 562 /* 563 * if this is a system process or if we have already 564 * looked at this process, skip it. 565 */ 566 if (p->p_flag & (P_SYSTEM|P_WEXIT)) { 567 continue; 568 } 569 570 /* 571 * if the process is in a non-running type state, 572 * don't touch it. 573 */ 574 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 575 continue; 576 } 577 578 /* 579 * get a limit 580 */ 581 limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 582 p->p_rlimit[RLIMIT_RSS].rlim_max); 583 584 /* 585 * let processes that are swapped out really be swapped out 586 * set the limit to nothing (will force a swap-out.) 587 */ 588 if ((p->p_flag & P_INMEM) == 0) 589 limit = 0; 590 591 size = p->p_vmspace->vm_pmap.pm_stats.resident_count * NBPG; 592 if (limit >= 0 && size >= limit) { 593 overage = (size - limit) / NBPG; 594 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, 595 (vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages); 596 } 597 } 598 599 if (((cnt.v_free_count + cnt.v_inactive_count) >= 600 (cnt.v_inactive_target + cnt.v_free_target)) && 601 (cnt.v_free_count >= cnt.v_free_target)) 602 return force_wakeup; 603 604 pages_freed = 0; 605 desired_free = cnt.v_free_target; 606 607 /* 608 * Start scanning the inactive queue for pages we can free. 609 * We keep scanning until we have enough free pages or 610 * we have scanned through the entire queue. If we 611 * encounter dirty pages, we start cleaning them. 612 */ 613 614 maxlaunder = (cnt.v_free_target - cnt.v_free_count); 615 maxscan = cnt.v_inactive_count; 616rescan1: 617 m = vm_page_queue_inactive.tqh_first; 618 while (m && (maxscan-- > 0) && 619 (cnt.v_free_count < desired_free) ) { 620 vm_page_t next; 621 622 next = m->pageq.tqe_next; 623 624 if( (m->flags & PG_INACTIVE) == 0) { 625 printf("vm_pageout_scan: page not inactive?"); 626 continue; 627 } 628 629 /* 630 * activate held pages 631 */ 632 if (m->hold_count != 0) { 633 vm_page_activate(m); 634 m = next; 635 continue; 636 } 637 638 /* 639 * dont mess with busy pages 640 */ 641 if (m->busy || (m->flags & PG_BUSY)) { 642 m = next; 643 continue; 644 } 645 646 /* 647 * NOTE: PG_CLEAN doesn't guarantee that the page is clean. 648 */ 649 if (m->flags & PG_CLEAN) { 650 /* 651 * If we're not low on memory and the page has been reference, 652 * or if the page has been modified, then reactivate the page. 653 */ 654 if (((cnt.v_free_count > vm_pageout_free_min) && 655 (pmap_is_referenced(VM_PAGE_TO_PHYS(m)) || ((m->flags & PG_REFERENCED) != 0))) || 656 pmap_is_modified(VM_PAGE_TO_PHYS(m))) { 657 m->flags &= ~PG_REFERENCED; 658 vm_page_activate(m); 659 } else if (!m->act_count) { 660 pmap_page_protect(VM_PAGE_TO_PHYS(m), 661 VM_PROT_NONE); 662 vm_page_free(m); 663 ++cnt.v_dfree; 664 ++pages_freed; 665 } else { 666 m->act_count -= min(m->act_count, ACT_DECLINE); 667 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 668 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 669 } 670 } else if ((m->flags & PG_LAUNDRY) && maxlaunder > 0) { 671 int written; 672 if (pmap_is_referenced(VM_PAGE_TO_PHYS(m)) || 673 ((m->flags & PG_REFERENCED) != 0)) { 674 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 675 vm_page_activate(m); 676 m->flags &= ~PG_REFERENCED; 677 m = next; 678 continue; 679 } 680 /* 681 * If a page is dirty, then it is either 682 * being washed (but not yet cleaned) 683 * or it is still in the laundry. If it is 684 * still in the laundry, then we start the 685 * cleaning operation. 686 */ 687 688 written = vm_pageout_clean(m,0); 689 if (written) 690 maxlaunder -= written; 691 692 if (!next) 693 break; 694 /* 695 * if the next page has been re-activated, start scanning again 696 */ 697 if ((next->flags & PG_INACTIVE) == 0) 698 goto rescan1; 699 } else if ((m->flags & PG_REFERENCED) || 700 pmap_is_referenced(VM_PAGE_TO_PHYS(m))) { 701 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 702 m->flags &= ~PG_REFERENCED; 703 vm_page_activate(m); 704 } 705 m = next; 706 } 707 708 /* 709 * Compute the page shortage. If we are still very low on memory 710 * be sure that we will move a minimal amount of pages from active 711 * to inactive. 712 */ 713 714 page_shortage = cnt.v_inactive_target - 715 (cnt.v_free_count + cnt.v_inactive_count); 716 717 if (page_shortage <= 0) { 718 if (pages_freed == 0) { 719 if( cnt.v_free_count < cnt.v_free_min) { 720 page_shortage = cnt.v_free_min - cnt.v_free_count; 721 } else if(((cnt.v_free_count + cnt.v_inactive_count) < 722 (cnt.v_free_min + cnt.v_inactive_target))) { 723 page_shortage = 1; 724 } else { 725 page_shortage = 0; 726 } 727 } 728 729 } 730 731 maxscan = cnt.v_active_count; 732 m = vm_page_queue_active.tqh_first; 733 while (m && maxscan-- && (page_shortage > 0)) { 734 735 next = m->pageq.tqe_next; 736 737 /* 738 * Don't deactivate pages that are busy. 739 */ 740 if ((m->busy != 0) || 741 (m->flags & PG_BUSY) || (m->hold_count != 0)) { 742 m = next; 743 continue; 744 } 745 746 if ((m->flags & PG_REFERENCED) || 747 pmap_is_referenced(VM_PAGE_TO_PHYS(m))) { 748 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 749 m->flags &= ~PG_REFERENCED; 750 if (m->act_count < ACT_MAX) 751 m->act_count += ACT_ADVANCE; 752 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 753 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 754 TAILQ_REMOVE(&m->object->memq, m, listq); 755 TAILQ_INSERT_TAIL(&m->object->memq, m, listq); 756 } else { 757 m->act_count -= min(m->act_count, ACT_DECLINE); 758 759 /* 760 * if the page act_count is zero -- then we deactivate 761 */ 762 if (!m->act_count) { 763 vm_page_deactivate(m); 764 --page_shortage; 765 /* 766 * else if on the next go-around we will deactivate the page 767 * we need to place the page on the end of the queue to age 768 * the other pages in memory. 769 */ 770 } else { 771 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 772 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 773 TAILQ_REMOVE(&m->object->memq, m, listq); 774 TAILQ_INSERT_TAIL(&m->object->memq, m, listq); 775 } 776 } 777 m = next; 778 } 779 780 /* 781 * if we have not freed any pages and we are desparate for memory 782 * then we keep trying until we get some (any) memory. 783 */ 784 785 if( !force_wakeup && (swap_pager_full || !force_wakeup || 786 (pages_freed == 0 && (cnt.v_free_count < cnt.v_free_min)))){ 787 vm_pager_sync(); 788 force_wakeup = 1; 789 goto morefree; 790 } 791 vm_page_pagesfreed += pages_freed; 792 return force_wakeup; 793} 794 795/* 796 * vm_pageout is the high level pageout daemon. 797 */ 798void 799vm_pageout() 800{ 801 (void) spl0(); 802 803 /* 804 * Initialize some paging parameters. 805 */ 806 807 cnt.v_free_min = 12; 808 /* 809 * free_reserved needs to include enough for the largest 810 * swap pager structures plus enough for any pv_entry 811 * structs when paging. 812 */ 813 cnt.v_free_reserved = 4 + cnt.v_page_count / 1024; 814 if (cnt.v_free_min < 8) 815 cnt.v_free_min = 8; 816 if (cnt.v_free_min > 32) 817 cnt.v_free_min = 32; 818 vm_pageout_free_min = cnt.v_free_reserved; 819 cnt.v_free_target = 2*cnt.v_free_min + cnt.v_free_reserved; 820 cnt.v_inactive_target = cnt.v_free_count / 12; 821 cnt.v_free_min += cnt.v_free_reserved; 822 vm_desired_cache_size = cnt.v_page_count / 3; 823 824 /* XXX does not really belong here */ 825 if (vm_page_max_wired == 0) 826 vm_page_max_wired = cnt.v_free_count / 3; 827 828 829 (void) swap_pager_alloc(0, 0, 0, 0); 830 831 /* 832 * The pageout daemon is never done, so loop 833 * forever. 834 */ 835 while (TRUE) { 836 int force_wakeup; 837/* 838 cnt.v_free_min = 12 + averunnable.ldavg[0] / 1024; 839 cnt.v_free_target = 2*cnt.v_free_min + cnt.v_free_reserved; 840 cnt.v_inactive_target = cnt.v_free_target*2; 841*/ 842 843 tsleep((caddr_t) &vm_pages_needed, PVM, "psleep", 0); 844 845 vm_pager_sync(); 846 /* 847 * The force wakeup hack added to eliminate delays and potiential 848 * deadlock. It was possible for the page daemon to indefintely 849 * postpone waking up a process that it might be waiting for memory 850 * on. The putmulti stuff seems to have aggravated the situation. 851 */ 852 force_wakeup = vm_pageout_scan(); 853 vm_pager_sync(); 854 if( force_wakeup) 855 wakeup( (caddr_t) &cnt.v_free_count); 856 cnt.v_scan++; 857 wakeup((caddr_t) kmem_map); 858 } 859} 860 861