vm_pageout.c revision 3567
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.16 1994/10/09 01:52:15 phk 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#include <vm/swap_pager.h> 85 86extern vm_map_t kmem_map; 87int vm_pages_needed; /* Event on which pageout daemon sleeps */ 88int vm_pagescanner; /* Event on which pagescanner sleeps */ 89int vm_pageout_free_min = 0; /* Stop pageout to wait for pagers at this free level */ 90 91int vm_pageout_pages_needed = 0; /* flag saying that the pageout daemon needs pages */ 92int vm_page_pagesfreed; 93int vm_desired_cache_size; 94 95extern int npendingio; 96extern int hz; 97int vm_pageout_proc_limit; 98extern int nswiodone; 99extern int swap_pager_full; 100extern int swap_pager_ready(); 101 102#define MAXREF 32767 103 104#define MAXSCAN 512 /* maximum number of pages to scan in active queue */ 105 /* set the "clock" hands to be (MAXSCAN * 4096) Bytes */ 106#define ACT_DECLINE 1 107#define ACT_ADVANCE 3 108#define ACT_MAX 100 109 110#define LOWATER ((2048*1024)/NBPG) 111 112#define VM_PAGEOUT_PAGE_COUNT 8 113int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 114int vm_pageout_req_do_stats; 115 116int vm_page_max_wired = 0; /* XXX max # of wired pages system-wide */ 117 118 119/* 120 * vm_pageout_clean: 121 * cleans a vm_page 122 */ 123int 124vm_pageout_clean(m, sync) 125 register vm_page_t m; 126 int sync; 127{ 128 /* 129 * Clean the page and remove it from the 130 * laundry. 131 * 132 * We set the busy bit to cause 133 * potential page faults on this page to 134 * block. 135 * 136 * And we set pageout-in-progress to keep 137 * the object from disappearing during 138 * pageout. This guarantees that the 139 * page won't move from the inactive 140 * queue. (However, any other page on 141 * the inactive queue may move!) 142 */ 143 144 register vm_object_t object; 145 register vm_pager_t pager; 146 int pageout_status[VM_PAGEOUT_PAGE_COUNT]; 147 vm_page_t ms[VM_PAGEOUT_PAGE_COUNT]; 148 int pageout_count; 149 int anyok=0; 150 int i; 151 vm_offset_t offset = m->offset; 152 153 object = m->object; 154 if (!object) { 155 printf("pager: object missing\n"); 156 return 0; 157 } 158 159 /* 160 * Try to collapse the object before 161 * making a pager for it. We must 162 * unlock the page queues first. 163 * We try to defer the creation of a pager 164 * until all shadows are not paging. This 165 * allows vm_object_collapse to work better and 166 * helps control swap space size. 167 * (J. Dyson 11 Nov 93) 168 */ 169 170 if (!object->pager && 171 cnt.v_free_count < vm_pageout_free_min) 172 return 0; 173 174 if (!object->pager && 175 object->shadow && 176 object->shadow->paging_in_progress) 177 return 0; 178 179 if( !sync) { 180 if (object->shadow) { 181 vm_object_collapse(object); 182 if (!vm_page_lookup(object, offset)) 183 return 0; 184 } 185 186 if ((m->busy != 0) || 187 (m->flags & PG_BUSY) || (m->hold_count != 0)) { 188 return 0; 189 } 190 } 191 192 pageout_count = 1; 193 ms[0] = m; 194 195 pager = object->pager; 196 if (pager) { 197 for (i = 1; i < vm_pageout_page_count; i++) { 198 ms[i] = vm_page_lookup(object, offset+i*NBPG); 199 if (ms[i]) { 200 if (( ((ms[i]->flags & (PG_CLEAN|PG_INACTIVE|PG_BUSY)) == PG_INACTIVE) 201 || ( (ms[i]->flags & (PG_CLEAN|PG_BUSY)) == 0 && sync == VM_PAGEOUT_FORCE)) 202 && (ms[i]->wire_count == 0) 203 && (ms[i]->busy == 0) 204 && (ms[i]->hold_count == 0)) 205 pageout_count++; 206 else 207 break; 208 } else 209 break; 210 } 211 for(i=0;i<pageout_count;i++) { 212 ms[i]->flags |= PG_BUSY; 213 pmap_page_protect(VM_PAGE_TO_PHYS(ms[i]), VM_PROT_READ); 214 } 215 object->paging_in_progress += pageout_count; 216 cnt.v_pageouts++; 217 cnt.v_pgpgout += pageout_count; 218 } else { 219 220 m->flags |= PG_BUSY; 221 222 pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_READ); 223 224 cnt.v_pageouts++; 225 cnt.v_pgpgout++; 226 227 object->paging_in_progress++; 228 229 pager = vm_pager_allocate(PG_DFLT, (caddr_t)0, 230 object->size, VM_PROT_ALL, 0); 231 if (pager != NULL) { 232 vm_object_setpager(object, pager, 0, FALSE); 233 } 234 } 235 236 /* 237 * If there is no pager for the page, 238 * use the default pager. If there's 239 * no place to put the page at the 240 * moment, leave it in the laundry and 241 * hope that there will be paging space 242 * later. 243 */ 244 245 if ((pager && pager->pg_type == PG_SWAP) || 246 cnt.v_free_count >= vm_pageout_free_min) { 247 if( pageout_count == 1) { 248 pageout_status[0] = pager ? 249 vm_pager_put(pager, m, 250 ((sync || (object == kernel_object)) ? TRUE: FALSE)) : 251 VM_PAGER_FAIL; 252 } else { 253 if( !pager) { 254 for(i=0;i<pageout_count;i++) 255 pageout_status[i] = VM_PAGER_FAIL; 256 } else { 257 vm_pager_put_pages(pager, ms, pageout_count, 258 ((sync || (object == kernel_object)) ? TRUE : FALSE), 259 pageout_status); 260 } 261 } 262 263 } else { 264 for(i=0;i<pageout_count;i++) 265 pageout_status[i] = VM_PAGER_FAIL; 266 } 267 268 for(i=0;i<pageout_count;i++) { 269 switch (pageout_status[i]) { 270 case VM_PAGER_OK: 271 ms[i]->flags &= ~PG_LAUNDRY; 272 ++anyok; 273 break; 274 case VM_PAGER_PEND: 275 ms[i]->flags &= ~PG_LAUNDRY; 276 ++anyok; 277 break; 278 case VM_PAGER_BAD: 279 /* 280 * Page outside of range of object. 281 * Right now we essentially lose the 282 * changes by pretending it worked. 283 */ 284 ms[i]->flags &= ~PG_LAUNDRY; 285 ms[i]->flags |= PG_CLEAN; 286 pmap_clear_modify(VM_PAGE_TO_PHYS(ms[i])); 287 break; 288 case VM_PAGER_ERROR: 289 case VM_PAGER_FAIL: 290 /* 291 * If page couldn't be paged out, then 292 * reactivate the page so it doesn't 293 * clog the inactive list. (We will 294 * try paging out it again later). 295 */ 296 if (ms[i]->flags & PG_INACTIVE) 297 vm_page_activate(ms[i]); 298 break; 299 case VM_PAGER_AGAIN: 300 break; 301 } 302 303 304 /* 305 * If the operation is still going, leave 306 * the page busy to block all other accesses. 307 * Also, leave the paging in progress 308 * indicator set so that we don't attempt an 309 * object collapse. 310 */ 311 if (pageout_status[i] != VM_PAGER_PEND) { 312 PAGE_WAKEUP(ms[i]); 313 if (--object->paging_in_progress == 0) 314 wakeup((caddr_t) object); 315 if ((ms[i]->flags & PG_REFERENCED) || 316 pmap_is_referenced(VM_PAGE_TO_PHYS(ms[i]))) { 317 pmap_clear_reference(VM_PAGE_TO_PHYS(ms[i])); 318 ms[i]->flags &= ~PG_REFERENCED; 319 if( ms[i]->flags & PG_INACTIVE) 320 vm_page_activate(ms[i]); 321 } 322 } 323 } 324 return anyok; 325} 326 327/* 328 * vm_pageout_object_deactivate_pages 329 * 330 * deactivate enough pages to satisfy the inactive target 331 * requirements or if vm_page_proc_limit is set, then 332 * deactivate all of the pages in the object and its 333 * shadows. 334 * 335 * The object and map must be locked. 336 */ 337int 338vm_pageout_object_deactivate_pages(map, object, count) 339 vm_map_t map; 340 vm_object_t object; 341 int count; 342{ 343 register vm_page_t p, next; 344 int rcount; 345 int dcount; 346 347 dcount = 0; 348 if (count == 0) 349 count = 1; 350 351 if (object->shadow) { 352 int scount = count; 353 if( object->shadow->ref_count > 1) 354 scount /= object->shadow->ref_count; 355 if( scount) 356 dcount += vm_pageout_object_deactivate_pages(map, object->shadow, scount); 357 } 358 359 if (object->paging_in_progress) 360 return dcount; 361 362 /* 363 * scan the objects entire memory queue 364 */ 365 rcount = object->resident_page_count; 366 p = object->memq.tqh_first; 367 while (p && (rcount-- > 0)) { 368 next = p->listq.tqe_next; 369 vm_page_lock_queues(); 370 /* 371 * if a page is active, not wired and is in the processes pmap, 372 * then deactivate the page. 373 */ 374 if ((p->flags & (PG_ACTIVE|PG_BUSY)) == PG_ACTIVE && 375 p->wire_count == 0 && 376 p->hold_count == 0 && 377 p->busy == 0 && 378 pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 379 if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) && 380 (p->flags & PG_REFERENCED) == 0) { 381 p->act_count -= min(p->act_count, ACT_DECLINE); 382 /* 383 * if the page act_count is zero -- then we deactivate 384 */ 385 if (!p->act_count) { 386 vm_page_deactivate(p); 387 pmap_page_protect(VM_PAGE_TO_PHYS(p), 388 VM_PROT_NONE); 389 /* 390 * else if on the next go-around we will deactivate the page 391 * we need to place the page on the end of the queue to age 392 * the other pages in memory. 393 */ 394 } else { 395 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 396 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 397 TAILQ_REMOVE(&object->memq, p, listq); 398 TAILQ_INSERT_TAIL(&object->memq, p, listq); 399 } 400 /* 401 * see if we are done yet 402 */ 403 if (p->flags & PG_INACTIVE) { 404 --count; 405 ++dcount; 406 if (count <= 0 && 407 cnt.v_inactive_count > cnt.v_inactive_target) { 408 vm_page_unlock_queues(); 409 return dcount; 410 } 411 } 412 413 } else { 414 /* 415 * Move the page to the bottom of the queue. 416 */ 417 pmap_clear_reference(VM_PAGE_TO_PHYS(p)); 418 p->flags &= ~PG_REFERENCED; 419 if (p->act_count < ACT_MAX) 420 p->act_count += ACT_ADVANCE; 421 422 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 423 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 424 TAILQ_REMOVE(&object->memq, p, listq); 425 TAILQ_INSERT_TAIL(&object->memq, p, listq); 426 } 427 } 428 429 vm_page_unlock_queues(); 430 p = next; 431 } 432 return dcount; 433} 434 435 436/* 437 * deactivate some number of pages in a map, try to do it fairly, but 438 * that is really hard to do. 439 */ 440 441void 442vm_pageout_map_deactivate_pages(map, entry, count, freeer) 443 vm_map_t map; 444 vm_map_entry_t entry; 445 int *count; 446 int (*freeer)(vm_map_t, vm_object_t, int); 447{ 448 vm_map_t tmpm; 449 vm_map_entry_t tmpe; 450 vm_object_t obj; 451 if (*count <= 0) 452 return; 453 vm_map_reference(map); 454 if (!lock_try_read(&map->lock)) { 455 vm_map_deallocate(map); 456 return; 457 } 458 if (entry == 0) { 459 tmpe = map->header.next; 460 while (tmpe != &map->header && *count > 0) { 461 vm_pageout_map_deactivate_pages(map, tmpe, count, freeer); 462 tmpe = tmpe->next; 463 }; 464 } else if (entry->is_sub_map || entry->is_a_map) { 465 tmpm = entry->object.share_map; 466 tmpe = tmpm->header.next; 467 while (tmpe != &tmpm->header && *count > 0) { 468 vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer); 469 tmpe = tmpe->next; 470 }; 471 } else if ((obj = entry->object.vm_object) != 0) { 472 *count -= (*freeer)(map, obj, *count); 473 } 474 lock_read_done(&map->lock); 475 vm_map_deallocate(map); 476 return; 477} 478 479/* 480 * vm_pageout_scan does the dirty work for the pageout daemon. 481 */ 482int 483vm_pageout_scan() 484{ 485 vm_page_t m; 486 int page_shortage, maxscan, maxlaunder; 487 int pages_freed; 488 int desired_free; 489 vm_page_t next; 490 struct proc *p; 491 vm_object_t object; 492 int force_wakeup = 0; 493 int cache_size, orig_cache_size; 494 495 /* 496 * We manage the cached memory by attempting to keep it 497 * at about the desired level. 498 * We deactivate the pages for the oldest cached objects 499 * first. This keeps pages that are "cached" from hogging 500 * physical memory. 501 */ 502 orig_cache_size = 0; 503 object = vm_object_cached_list.tqh_first; 504 505 /* calculate the total cached size */ 506 507 while( object) { 508 orig_cache_size += object->resident_page_count; 509 object = object->cached_list.tqe_next; 510 } 511 512redeact: 513 cache_size = orig_cache_size; 514 object = vm_object_cached_list.tqh_first; 515 vm_object_cache_lock(); 516 while ( object && (cnt.v_inactive_count < cnt.v_inactive_target) && 517 (cache_size >= vm_desired_cache_size)) { 518 vm_object_cache_unlock(); 519 520 /* 521 * if there are no resident pages -- get rid of the object 522 */ 523 if( object->resident_page_count == 0) { 524 if (object != vm_object_lookup(object->pager)) 525 panic("vm_object_deactivate: I'm sooo confused."); 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