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