| 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.36 2008/07/01 02:02:56 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 | #include <sys/thread2.h> |
| 100 | #include <vm/vm_page2.h> |
| 101 | |
| 102 | /* |
| 103 | * System initialization |
| 104 | */ |
| 105 | |
| 106 | /* the kernel process "vm_pageout"*/ |
| 107 | static void vm_pageout (void); |
| 108 | static int vm_pageout_clean (vm_page_t); |
| 109 | static int vm_pageout_scan (int pass); |
| 110 | static int vm_pageout_free_page_calc (vm_size_t count); |
| 111 | struct thread *pagethread; |
| 112 | |
| 113 | static struct kproc_desc page_kp = { |
| 114 | "pagedaemon", |
| 115 | vm_pageout, |
| 116 | &pagethread |
| 117 | }; |
| 118 | SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) |
| 119 | |
| 120 | #if !defined(NO_SWAPPING) |
| 121 | /* the kernel process "vm_daemon"*/ |
| 122 | static void vm_daemon (void); |
| 123 | static struct thread *vmthread; |
| 124 | |
| 125 | static struct kproc_desc vm_kp = { |
| 126 | "vmdaemon", |
| 127 | vm_daemon, |
| 128 | &vmthread |
| 129 | }; |
| 130 | SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) |
| 131 | #endif |
| 132 | |
| 133 | |
| 134 | int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ |
| 135 | int vm_pageout_deficit=0; /* Estimated number of pages deficit */ |
| 136 | int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ |
| 137 | |
| 138 | #if !defined(NO_SWAPPING) |
| 139 | static int vm_pageout_req_swapout; /* XXX */ |
| 140 | static int vm_daemon_needed; |
| 141 | #endif |
| 142 | static int vm_max_launder = 32; |
| 143 | static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; |
| 144 | static int vm_pageout_full_stats_interval = 0; |
| 145 | static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; |
| 146 | static int defer_swap_pageouts=0; |
| 147 | static int disable_swap_pageouts=0; |
| 148 | |
| 149 | #if defined(NO_SWAPPING) |
| 150 | static int vm_swap_enabled=0; |
| 151 | static int vm_swap_idle_enabled=0; |
| 152 | #else |
| 153 | static int vm_swap_enabled=1; |
| 154 | static int vm_swap_idle_enabled=0; |
| 155 | #endif |
| 156 | |
| 157 | SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, |
| 158 | CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); |
| 159 | |
| 160 | SYSCTL_INT(_vm, OID_AUTO, max_launder, |
| 161 | CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); |
| 162 | |
| 163 | SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, |
| 164 | CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); |
| 165 | |
| 166 | SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, |
| 167 | CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); |
| 168 | |
| 169 | SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, |
| 170 | CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); |
| 171 | |
| 172 | SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, |
| 173 | CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); |
| 174 | |
| 175 | #if defined(NO_SWAPPING) |
| 176 | SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, |
| 177 | CTLFLAG_RD, &vm_swap_enabled, 0, ""); |
| 178 | SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, |
| 179 | CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); |
| 180 | #else |
| 181 | SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, |
| 182 | CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); |
| 183 | SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, |
| 184 | CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); |
| 185 | #endif |
| 186 | |
| 187 | SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, |
| 188 | CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); |
| 189 | |
| 190 | SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, |
| 191 | CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); |
| 192 | |
| 193 | static int pageout_lock_miss; |
| 194 | SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, |
| 195 | CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); |
| 196 | |
| 197 | int vm_load; |
| 198 | SYSCTL_INT(_vm, OID_AUTO, vm_load, |
| 199 | CTLFLAG_RD, &vm_load, 0, "load on the VM system"); |
| 200 | int vm_load_enable = 1; |
| 201 | SYSCTL_INT(_vm, OID_AUTO, vm_load_enable, |
| 202 | CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting"); |
| 203 | #ifdef INVARIANTS |
| 204 | int vm_load_debug; |
| 205 | SYSCTL_INT(_vm, OID_AUTO, vm_load_debug, |
| 206 | CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load"); |
| 207 | #endif |
| 208 | |
| 209 | #define VM_PAGEOUT_PAGE_COUNT 16 |
| 210 | int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; |
| 211 | |
| 212 | int vm_page_max_wired; /* XXX max # of wired pages system-wide */ |
| 213 | |
| 214 | #if !defined(NO_SWAPPING) |
| 215 | typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int); |
| 216 | static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t); |
| 217 | static freeer_fcn_t vm_pageout_object_deactivate_pages; |
| 218 | static void vm_req_vmdaemon (void); |
| 219 | #endif |
| 220 | static void vm_pageout_page_stats(void); |
| 221 | |
| 222 | /* |
| 223 | * Update vm_load to slow down faulting processes. |
| 224 | */ |
| 225 | void |
| 226 | vm_fault_ratecheck(void) |
| 227 | { |
| 228 | if (vm_pages_needed) { |
| 229 | if (vm_load < 1000) |
| 230 | ++vm_load; |
| 231 | } else { |
| 232 | if (vm_load > 0) |
| 233 | --vm_load; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * vm_pageout_clean: |
| 239 | * |
| 240 | * Clean the page and remove it from the laundry. The page must not be |
| 241 | * busy on-call. |
| 242 | * |
| 243 | * We set the busy bit to cause potential page faults on this page to |
| 244 | * block. Note the careful timing, however, the busy bit isn't set till |
| 245 | * late and we cannot do anything that will mess with the page. |
| 246 | */ |
| 247 | |
| 248 | static int |
| 249 | vm_pageout_clean(vm_page_t m) |
| 250 | { |
| 251 | vm_object_t object; |
| 252 | vm_page_t mc[2*vm_pageout_page_count]; |
| 253 | int pageout_count; |
| 254 | int ib, is, page_base; |
| 255 | vm_pindex_t pindex = m->pindex; |
| 256 | |
| 257 | object = m->object; |
| 258 | |
| 259 | /* |
| 260 | * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP |
| 261 | * with the new swapper, but we could have serious problems paging |
| 262 | * out other object types if there is insufficient memory. |
| 263 | * |
| 264 | * Unfortunately, checking free memory here is far too late, so the |
| 265 | * check has been moved up a procedural level. |
| 266 | */ |
| 267 | |
| 268 | /* |
| 269 | * Don't mess with the page if it's busy, held, or special |
| 270 | */ |
| 271 | if ((m->hold_count != 0) || |
| 272 | ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | mc[vm_pageout_page_count] = m; |
| 277 | pageout_count = 1; |
| 278 | page_base = vm_pageout_page_count; |
| 279 | ib = 1; |
| 280 | is = 1; |
| 281 | |
| 282 | /* |
| 283 | * Scan object for clusterable pages. |
| 284 | * |
| 285 | * We can cluster ONLY if: ->> the page is NOT |
| 286 | * clean, wired, busy, held, or mapped into a |
| 287 | * buffer, and one of the following: |
| 288 | * 1) The page is inactive, or a seldom used |
| 289 | * active page. |
| 290 | * -or- |
| 291 | * 2) we force the issue. |
| 292 | * |
| 293 | * During heavy mmap/modification loads the pageout |
| 294 | * daemon can really fragment the underlying file |
| 295 | * due to flushing pages out of order and not trying |
| 296 | * align the clusters (which leave sporatic out-of-order |
| 297 | * holes). To solve this problem we do the reverse scan |
| 298 | * first and attempt to align our cluster, then do a |
| 299 | * forward scan if room remains. |
| 300 | */ |
| 301 | |
| 302 | more: |
| 303 | while (ib && pageout_count < vm_pageout_page_count) { |
| 304 | vm_page_t p; |
| 305 | |
| 306 | if (ib > pindex) { |
| 307 | ib = 0; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { |
| 312 | ib = 0; |
| 313 | break; |
| 314 | } |
| 315 | if (((p->queue - p->pc) == PQ_CACHE) || |
| 316 | (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { |
| 317 | ib = 0; |
| 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 | ib = 0; |
| 326 | break; |
| 327 | } |
| 328 | mc[--page_base] = p; |
| 329 | ++pageout_count; |
| 330 | ++ib; |
| 331 | /* |
| 332 | * alignment boundry, stop here and switch directions. Do |
| 333 | * not clear ib. |
| 334 | */ |
| 335 | if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | while (pageout_count < vm_pageout_page_count && |
| 340 | pindex + is < object->size) { |
| 341 | vm_page_t p; |
| 342 | |
| 343 | if ((p = vm_page_lookup(object, pindex + is)) == NULL) |
| 344 | break; |
| 345 | if (((p->queue - p->pc) == PQ_CACHE) || |
| 346 | (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { |
| 347 | break; |
| 348 | } |
| 349 | vm_page_test_dirty(p); |
| 350 | if ((p->dirty & p->valid) == 0 || |
| 351 | p->queue != PQ_INACTIVE || |
| 352 | p->wire_count != 0 || /* may be held by buf cache */ |
| 353 | p->hold_count != 0) { /* may be undergoing I/O */ |
| 354 | break; |
| 355 | } |
| 356 | mc[page_base + pageout_count] = p; |
| 357 | ++pageout_count; |
| 358 | ++is; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * If we exhausted our forward scan, continue with the reverse scan |
| 363 | * when possible, even past a page boundry. This catches boundry |
| 364 | * conditions. |
| 365 | */ |
| 366 | if (ib && pageout_count < vm_pageout_page_count) |
| 367 | goto more; |
| 368 | |
| 369 | /* |
| 370 | * we allow reads during pageouts... |
| 371 | */ |
| 372 | return vm_pageout_flush(&mc[page_base], pageout_count, 0); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * vm_pageout_flush() - launder the given pages |
| 377 | * |
| 378 | * The given pages are laundered. Note that we setup for the start of |
| 379 | * I/O ( i.e. busy the page ), mark it read-only, and bump the object |
| 380 | * reference count all in here rather then in the parent. If we want |
| 381 | * the parent to do more sophisticated things we may have to change |
| 382 | * the ordering. |
| 383 | */ |
| 384 | int |
| 385 | vm_pageout_flush(vm_page_t *mc, int count, int flags) |
| 386 | { |
| 387 | vm_object_t object; |
| 388 | int pageout_status[count]; |
| 389 | int numpagedout = 0; |
| 390 | int i; |
| 391 | |
| 392 | /* |
| 393 | * Initiate I/O. Bump the vm_page_t->busy counter. |
| 394 | */ |
| 395 | for (i = 0; i < count; i++) { |
| 396 | KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); |
| 397 | vm_page_io_start(mc[i]); |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * We must make the pages read-only. This will also force the |
| 402 | * modified bit in the related pmaps to be cleared. The pager |
| 403 | * cannot clear the bit for us since the I/O completion code |
| 404 | * typically runs from an interrupt. The act of making the page |
| 405 | * read-only handles the case for us. |
| 406 | */ |
| 407 | for (i = 0; i < count; i++) { |
| 408 | vm_page_protect(mc[i], VM_PROT_READ); |
| 409 | } |
| 410 | |
| 411 | object = mc[0]->object; |
| 412 | vm_object_pip_add(object, count); |
| 413 | |
| 414 | vm_pager_put_pages(object, mc, count, |
| 415 | (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)), |
| 416 | pageout_status); |
| 417 | |
| 418 | for (i = 0; i < count; i++) { |
| 419 | vm_page_t mt = mc[i]; |
| 420 | |
| 421 | switch (pageout_status[i]) { |
| 422 | case VM_PAGER_OK: |
| 423 | numpagedout++; |
| 424 | break; |
| 425 | case VM_PAGER_PEND: |
| 426 | numpagedout++; |
| 427 | break; |
| 428 | case VM_PAGER_BAD: |
| 429 | /* |
| 430 | * Page outside of range of object. Right now we |
| 431 | * essentially lose the changes by pretending it |
| 432 | * worked. |
| 433 | */ |
| 434 | pmap_clear_modify(mt); |
| 435 | vm_page_undirty(mt); |
| 436 | break; |
| 437 | case VM_PAGER_ERROR: |
| 438 | case VM_PAGER_FAIL: |
| 439 | /* |
| 440 | * A page typically cannot be paged out when we |
| 441 | * have run out of swap. We leave the page |
| 442 | * marked inactive and will try to page it out |
| 443 | * again later. |
| 444 | * |
| 445 | * Starvation of the active page list is used to |
| 446 | * determine when the system is massively memory |
| 447 | * starved. |
| 448 | */ |
| 449 | break; |
| 450 | case VM_PAGER_AGAIN: |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * If the operation is still going, leave the page busy to |
| 456 | * block all other accesses. Also, leave the paging in |
| 457 | * progress indicator set so that we don't attempt an object |
| 458 | * collapse. |
| 459 | * |
| 460 | * For any pages which have completed synchronously, |
| 461 | * deactivate the page if we are under a severe deficit. |
| 462 | * Do not try to enter them into the cache, though, they |
| 463 | * might still be read-heavy. |
| 464 | */ |
| 465 | if (pageout_status[i] != VM_PAGER_PEND) { |
| 466 | vm_object_pip_wakeup(object); |
| 467 | vm_page_io_finish(mt); |
| 468 | if (vm_page_count_severe()) |
| 469 | vm_page_deactivate(mt); |
| 470 | #if 0 |
| 471 | if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) |
| 472 | vm_page_protect(mt, VM_PROT_READ); |
| 473 | #endif |
| 474 | } |
| 475 | } |
| 476 | return numpagedout; |
| 477 | } |
| 478 | |
| 479 | #if !defined(NO_SWAPPING) |
| 480 | /* |
| 481 | * vm_pageout_object_deactivate_pages |
| 482 | * |
| 483 | * deactivate enough pages to satisfy the inactive target |
| 484 | * requirements or if vm_page_proc_limit is set, then |
| 485 | * deactivate all of the pages in the object and its |
| 486 | * backing_objects. |
| 487 | * |
| 488 | * The object and map must be locked. |
| 489 | */ |
| 490 | static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *); |
| 491 | |
| 492 | static void |
| 493 | vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object, |
| 494 | vm_pindex_t desired, int map_remove_only) |
| 495 | { |
| 496 | struct rb_vm_page_scan_info info; |
| 497 | int remove_mode; |
| 498 | |
| 499 | if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) |
| 500 | return; |
| 501 | |
| 502 | while (object) { |
| 503 | if (pmap_resident_count(vm_map_pmap(map)) <= desired) |
| 504 | return; |
| 505 | if (object->paging_in_progress) |
| 506 | return; |
| 507 | |
| 508 | remove_mode = map_remove_only; |
| 509 | if (object->shadow_count > 1) |
| 510 | remove_mode = 1; |
| 511 | |
| 512 | /* |
| 513 | * scan the objects entire memory queue. spl protection is |
| 514 | * required to avoid an interrupt unbusy/free race against |
| 515 | * our busy check. |
| 516 | */ |
| 517 | crit_enter(); |
| 518 | info.limit = remove_mode; |
| 519 | info.map = map; |
| 520 | info.desired = desired; |
| 521 | vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, |
| 522 | vm_pageout_object_deactivate_pages_callback, |
| 523 | &info |
| 524 | ); |
| 525 | crit_exit(); |
| 526 | object = object->backing_object; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static int |
| 531 | vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data) |
| 532 | { |
| 533 | struct rb_vm_page_scan_info *info = data; |
| 534 | int actcount; |
| 535 | |
| 536 | if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) { |
| 537 | return(-1); |
| 538 | } |
| 539 | mycpu->gd_cnt.v_pdpages++; |
| 540 | if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 || |
| 541 | (p->flags & (PG_BUSY|PG_UNMANAGED)) || |
| 542 | !pmap_page_exists_quick(vm_map_pmap(info->map), p)) { |
| 543 | return(0); |
| 544 | } |
| 545 | |
| 546 | actcount = pmap_ts_referenced(p); |
| 547 | if (actcount) { |
| 548 | vm_page_flag_set(p, PG_REFERENCED); |
| 549 | } else if (p->flags & PG_REFERENCED) { |
| 550 | actcount = 1; |
| 551 | } |
| 552 | |
| 553 | if ((p->queue != PQ_ACTIVE) && |
| 554 | (p->flags & PG_REFERENCED)) { |
| 555 | vm_page_activate(p); |
| 556 | p->act_count += actcount; |
| 557 | vm_page_flag_clear(p, PG_REFERENCED); |
| 558 | } else if (p->queue == PQ_ACTIVE) { |
| 559 | if ((p->flags & PG_REFERENCED) == 0) { |
| 560 | p->act_count -= min(p->act_count, ACT_DECLINE); |
| 561 | if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) { |
| 562 | vm_page_busy(p); |
| 563 | vm_page_protect(p, VM_PROT_NONE); |
| 564 | vm_page_wakeup(p); |
| 565 | vm_page_deactivate(p); |
| 566 | } else { |
| 567 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); |
| 568 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); |
| 569 | } |
| 570 | } else { |
| 571 | vm_page_activate(p); |
| 572 | vm_page_flag_clear(p, PG_REFERENCED); |
| 573 | if (p->act_count < (ACT_MAX - ACT_ADVANCE)) |
| 574 | p->act_count += ACT_ADVANCE; |
| 575 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); |
| 576 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); |
| 577 | } |
| 578 | } else if (p->queue == PQ_INACTIVE) { |
| 579 | vm_page_busy(p); |
| 580 | vm_page_protect(p, VM_PROT_NONE); |
| 581 | vm_page_wakeup(p); |
| 582 | } |
| 583 | return(0); |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * deactivate some number of pages in a map, try to do it fairly, but |
| 588 | * that is really hard to do. |
| 589 | */ |
| 590 | static void |
| 591 | vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired) |
| 592 | { |
| 593 | vm_map_entry_t tmpe; |
| 594 | vm_object_t obj, bigobj; |
| 595 | int nothingwired; |
| 596 | |
| 597 | if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) { |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | bigobj = NULL; |
| 602 | nothingwired = TRUE; |
| 603 | |
| 604 | /* |
| 605 | * first, search out the biggest object, and try to free pages from |
| 606 | * that. |
| 607 | */ |
| 608 | tmpe = map->header.next; |
| 609 | while (tmpe != &map->header) { |
| 610 | switch(tmpe->maptype) { |
| 611 | case VM_MAPTYPE_NORMAL: |
| 612 | case VM_MAPTYPE_VPAGETABLE: |
| 613 | obj = tmpe->object.vm_object; |
| 614 | if ((obj != NULL) && (obj->shadow_count <= 1) && |
| 615 | ((bigobj == NULL) || |
| 616 | (bigobj->resident_page_count < obj->resident_page_count))) { |
| 617 | bigobj = obj; |
| 618 | } |
| 619 | break; |
| 620 | default: |
| 621 | break; |
| 622 | } |
| 623 | if (tmpe->wired_count > 0) |
| 624 | nothingwired = FALSE; |
| 625 | tmpe = tmpe->next; |
| 626 | } |
| 627 | |
| 628 | if (bigobj) |
| 629 | vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); |
| 630 | |
| 631 | /* |
| 632 | * Next, hunt around for other pages to deactivate. We actually |
| 633 | * do this search sort of wrong -- .text first is not the best idea. |
| 634 | */ |
| 635 | tmpe = map->header.next; |
| 636 | while (tmpe != &map->header) { |
| 637 | if (pmap_resident_count(vm_map_pmap(map)) <= desired) |
| 638 | break; |
| 639 | switch(tmpe->maptype) { |
| 640 | case VM_MAPTYPE_NORMAL: |
| 641 | case VM_MAPTYPE_VPAGETABLE: |
| 642 | obj = tmpe->object.vm_object; |
| 643 | if (obj) |
| 644 | vm_pageout_object_deactivate_pages(map, obj, desired, 0); |
| 645 | break; |
| 646 | default: |
| 647 | break; |
| 648 | } |
| 649 | tmpe = tmpe->next; |
| 650 | }; |
| 651 | |
| 652 | /* |
| 653 | * Remove all mappings if a process is swapped out, this will free page |
| 654 | * table pages. |
| 655 | */ |
| 656 | if (desired == 0 && nothingwired) |
| 657 | pmap_remove(vm_map_pmap(map), |
| 658 | VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS); |
| 659 | vm_map_unlock(map); |
| 660 | } |
| 661 | #endif |
| 662 | |
| 663 | /* |
| 664 | * Don't try to be fancy - being fancy can lead to vnode deadlocks. We |
| 665 | * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can |
| 666 | * be trivially freed. |
| 667 | */ |
| 668 | void |
| 669 | vm_pageout_page_free(vm_page_t m) |
| 670 | { |
| 671 | vm_object_t object = m->object; |
| 672 | int type = object->type; |
| 673 | |
| 674 | if (type == OBJT_SWAP || type == OBJT_DEFAULT) |
| 675 | vm_object_reference(object); |
| 676 | vm_page_busy(m); |
| 677 | vm_page_protect(m, VM_PROT_NONE); |
| 678 | vm_page_free(m); |
| 679 | if (type == OBJT_SWAP || type == OBJT_DEFAULT) |
| 680 | vm_object_deallocate(object); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * vm_pageout_scan does the dirty work for the pageout daemon. |
| 685 | */ |
| 686 | struct vm_pageout_scan_info { |
| 687 | struct proc *bigproc; |
| 688 | vm_offset_t bigsize; |
| 689 | }; |
| 690 | |
| 691 | static int vm_pageout_scan_callback(struct proc *p, void *data); |
| 692 | |
| 693 | static int |
| 694 | vm_pageout_scan(int pass) |
| 695 | { |
| 696 | struct vm_pageout_scan_info info; |
| 697 | vm_page_t m, next; |
| 698 | struct vm_page marker; |
| 699 | int maxscan, pcount; |
| 700 | int recycle_count; |
| 701 | int inactive_shortage, active_shortage; |
| 702 | int inactive_original_shortage; |
| 703 | vm_object_t object; |
| 704 | int actcount; |
| 705 | int vnodes_skipped = 0; |
| 706 | int maxlaunder; |
| 707 | |
| 708 | /* |
| 709 | * Do whatever cleanup that the pmap code can. |
| 710 | */ |
| 711 | pmap_collect(); |
| 712 | |
| 713 | /* |
| 714 | * Calculate our target for the number of free+cache pages we |
| 715 | * want to get to. This is higher then the number that causes |
| 716 | * allocations to stall (severe) in order to provide hysteresis, |
| 717 | * and if we don't make it all the way but get to the minimum |
| 718 | * we're happy. |
| 719 | */ |
| 720 | inactive_shortage = vm_paging_target() + vm_pageout_deficit; |
| 721 | inactive_original_shortage = inactive_shortage; |
| 722 | vm_pageout_deficit = 0; |
| 723 | |
| 724 | /* |
| 725 | * Initialize our marker |
| 726 | */ |
| 727 | bzero(&marker, sizeof(marker)); |
| 728 | marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; |
| 729 | marker.queue = PQ_INACTIVE; |
| 730 | marker.wire_count = 1; |
| 731 | |
| 732 | /* |
| 733 | * Start scanning the inactive queue for pages we can move to the |
| 734 | * cache or free. The scan will stop when the target is reached or |
| 735 | * we have scanned the entire inactive queue. Note that m->act_count |
| 736 | * is not used to form decisions for the inactive queue, only for the |
| 737 | * active queue. |
| 738 | * |
| 739 | * maxlaunder limits the number of dirty pages we flush per scan. |
| 740 | * For most systems a smaller value (16 or 32) is more robust under |
| 741 | * extreme memory and disk pressure because any unnecessary writes |
| 742 | * to disk can result in extreme performance degredation. However, |
| 743 | * systems with excessive dirty pages (especially when MAP_NOSYNC is |
| 744 | * used) will die horribly with limited laundering. If the pageout |
| 745 | * daemon cannot clean enough pages in the first pass, we let it go |
| 746 | * all out in succeeding passes. |
| 747 | */ |
| 748 | if ((maxlaunder = vm_max_launder) <= 1) |
| 749 | maxlaunder = 1; |
| 750 | if (pass) |
| 751 | maxlaunder = 10000; |
| 752 | |
| 753 | /* |
| 754 | * We will generally be in a critical section throughout the |
| 755 | * scan, but we can release it temporarily when we are sitting on a |
| 756 | * non-busy page without fear. this is required to prevent an |
| 757 | * interrupt from unbusying or freeing a page prior to our busy |
| 758 | * check, leaving us on the wrong queue or checking the wrong |
| 759 | * page. |
| 760 | */ |
| 761 | crit_enter(); |
| 762 | rescan0: |
| 763 | maxscan = vmstats.v_inactive_count; |
| 764 | for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); |
| 765 | m != NULL && maxscan-- > 0 && inactive_shortage > 0; |
| 766 | m = next |
| 767 | ) { |
| 768 | mycpu->gd_cnt.v_pdpages++; |
| 769 | |
| 770 | /* |
| 771 | * Give interrupts a chance |
| 772 | */ |
| 773 | crit_exit(); |
| 774 | crit_enter(); |
| 775 | |
| 776 | /* |
| 777 | * It's easier for some of the conditions below to just loop |
| 778 | * and catch queue changes here rather then check everywhere |
| 779 | * else. |
| 780 | */ |
| 781 | if (m->queue != PQ_INACTIVE) |
| 782 | goto rescan0; |
| 783 | next = TAILQ_NEXT(m, pageq); |
| 784 | |
| 785 | /* |
| 786 | * skip marker pages |
| 787 | */ |
| 788 | if (m->flags & PG_MARKER) |
| 789 | continue; |
| 790 | |
| 791 | /* |
| 792 | * A held page may be undergoing I/O, so skip it. |
| 793 | */ |
| 794 | if (m->hold_count) { |
| 795 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 796 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 797 | continue; |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Dont mess with busy pages, keep in the front of the |
| 802 | * queue, most likely are being paged out. |
| 803 | */ |
| 804 | if (m->busy || (m->flags & PG_BUSY)) { |
| 805 | continue; |
| 806 | } |
| 807 | |
| 808 | if (m->object->ref_count == 0) { |
| 809 | /* |
| 810 | * If the object is not being used, we ignore previous |
| 811 | * references. |
| 812 | */ |
| 813 | vm_page_flag_clear(m, PG_REFERENCED); |
| 814 | pmap_clear_reference(m); |
| 815 | |
| 816 | } else if (((m->flags & PG_REFERENCED) == 0) && |
| 817 | (actcount = pmap_ts_referenced(m))) { |
| 818 | /* |
| 819 | * Otherwise, if the page has been referenced while |
| 820 | * in the inactive queue, we bump the "activation |
| 821 | * count" upwards, making it less likely that the |
| 822 | * page will be added back to the inactive queue |
| 823 | * prematurely again. Here we check the page tables |
| 824 | * (or emulated bits, if any), given the upper level |
| 825 | * VM system not knowing anything about existing |
| 826 | * references. |
| 827 | */ |
| 828 | vm_page_activate(m); |
| 829 | m->act_count += (actcount + ACT_ADVANCE); |
| 830 | continue; |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * If the upper level VM system knows about any page |
| 835 | * references, we activate the page. We also set the |
| 836 | * "activation count" higher than normal so that we will less |
| 837 | * likely place pages back onto the inactive queue again. |
| 838 | */ |
| 839 | if ((m->flags & PG_REFERENCED) != 0) { |
| 840 | vm_page_flag_clear(m, PG_REFERENCED); |
| 841 | actcount = pmap_ts_referenced(m); |
| 842 | vm_page_activate(m); |
| 843 | m->act_count += (actcount + ACT_ADVANCE + 1); |
| 844 | continue; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * If the upper level VM system doesn't know anything about |
| 849 | * the page being dirty, we have to check for it again. As |
| 850 | * far as the VM code knows, any partially dirty pages are |
| 851 | * fully dirty. |
| 852 | * |
| 853 | * Pages marked PG_WRITEABLE may be mapped into the user |
| 854 | * address space of a process running on another cpu. A |
| 855 | * user process (without holding the MP lock) running on |
| 856 | * another cpu may be able to touch the page while we are |
| 857 | * trying to remove it. vm_page_cache() will handle this |
| 858 | * case for us. |
| 859 | */ |
| 860 | if (m->dirty == 0) { |
| 861 | vm_page_test_dirty(m); |
| 862 | } else { |
| 863 | vm_page_dirty(m); |
| 864 | } |
| 865 | |
| 866 | if (m->valid == 0) { |
| 867 | /* |
| 868 | * Invalid pages can be easily freed |
| 869 | */ |
| 870 | vm_pageout_page_free(m); |
| 871 | mycpu->gd_cnt.v_dfree++; |
| 872 | --inactive_shortage; |
| 873 | } else if (m->dirty == 0) { |
| 874 | /* |
| 875 | * Clean pages can be placed onto the cache queue. |
| 876 | * This effectively frees them. |
| 877 | */ |
| 878 | vm_page_cache(m); |
| 879 | --inactive_shortage; |
| 880 | } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { |
| 881 | /* |
| 882 | * Dirty pages need to be paged out, but flushing |
| 883 | * a page is extremely expensive verses freeing |
| 884 | * a clean page. Rather then artificially limiting |
| 885 | * the number of pages we can flush, we instead give |
| 886 | * dirty pages extra priority on the inactive queue |
| 887 | * by forcing them to be cycled through the queue |
| 888 | * twice before being flushed, after which the |
| 889 | * (now clean) page will cycle through once more |
| 890 | * before being freed. This significantly extends |
| 891 | * the thrash point for a heavily loaded machine. |
| 892 | */ |
| 893 | vm_page_flag_set(m, PG_WINATCFLS); |
| 894 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 895 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 896 | } else if (maxlaunder > 0) { |
| 897 | /* |
| 898 | * We always want to try to flush some dirty pages if |
| 899 | * we encounter them, to keep the system stable. |
| 900 | * Normally this number is small, but under extreme |
| 901 | * pressure where there are insufficient clean pages |
| 902 | * on the inactive queue, we may have to go all out. |
| 903 | */ |
| 904 | int swap_pageouts_ok; |
| 905 | struct vnode *vp = NULL; |
| 906 | |
| 907 | object = m->object; |
| 908 | |
| 909 | if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { |
| 910 | swap_pageouts_ok = 1; |
| 911 | } else { |
| 912 | swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); |
| 913 | swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && |
| 914 | vm_page_count_min(0)); |
| 915 | |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * We don't bother paging objects that are "dead". |
| 920 | * Those objects are in a "rundown" state. |
| 921 | */ |
| 922 | if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { |
| 923 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 924 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 925 | continue; |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * The object is already known NOT to be dead. It |
| 930 | * is possible for the vget() to block the whole |
| 931 | * pageout daemon, but the new low-memory handling |
| 932 | * code should prevent it. |
| 933 | * |
| 934 | * The previous code skipped locked vnodes and, worse, |
| 935 | * reordered pages in the queue. This results in |
| 936 | * completely non-deterministic operation because, |
| 937 | * quite often, a vm_fault has initiated an I/O and |
| 938 | * is holding a locked vnode at just the point where |
| 939 | * the pageout daemon is woken up. |
| 940 | * |
| 941 | * We can't wait forever for the vnode lock, we might |
| 942 | * deadlock due to a vn_read() getting stuck in |
| 943 | * vm_wait while holding this vnode. We skip the |
| 944 | * vnode if we can't get it in a reasonable amount |
| 945 | * of time. |
| 946 | */ |
| 947 | |
| 948 | if (object->type == OBJT_VNODE) { |
| 949 | vp = object->handle; |
| 950 | |
| 951 | if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ|LK_TIMELOCK)) { |
| 952 | ++pageout_lock_miss; |
| 953 | if (object->flags & OBJ_MIGHTBEDIRTY) |
| 954 | vnodes_skipped++; |
| 955 | continue; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * The page might have been moved to another |
| 960 | * queue during potential blocking in vget() |
| 961 | * above. The page might have been freed and |
| 962 | * reused for another vnode. The object might |
| 963 | * have been reused for another vnode. |
| 964 | */ |
| 965 | if (m->queue != PQ_INACTIVE || |
| 966 | m->object != object || |
| 967 | object->handle != vp) { |
| 968 | if (object->flags & OBJ_MIGHTBEDIRTY) |
| 969 | vnodes_skipped++; |
| 970 | vput(vp); |
| 971 | continue; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * The page may have been busied during the |
| 976 | * blocking in vput(); We don't move the |
| 977 | * page back onto the end of the queue so that |
| 978 | * statistics are more correct if we don't. |
| 979 | */ |
| 980 | if (m->busy || (m->flags & PG_BUSY)) { |
| 981 | vput(vp); |
| 982 | continue; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * If the page has become held it might |
| 987 | * be undergoing I/O, so skip it |
| 988 | */ |
| 989 | if (m->hold_count) { |
| 990 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 991 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 992 | if (object->flags & OBJ_MIGHTBEDIRTY) |
| 993 | vnodes_skipped++; |
| 994 | vput(vp); |
| 995 | continue; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * If a page is dirty, then it is either being washed |
| 1001 | * (but not yet cleaned) or it is still in the |
| 1002 | * laundry. If it is still in the laundry, then we |
| 1003 | * start the cleaning operation. |
| 1004 | * |
| 1005 | * This operation may cluster, invalidating the 'next' |
| 1006 | * pointer. To prevent an inordinate number of |
| 1007 | * restarts we use our marker to remember our place. |
| 1008 | * |
| 1009 | * decrement inactive_shortage on success to account |
| 1010 | * for the (future) cleaned page. Otherwise we |
| 1011 | * could wind up laundering or cleaning too many |
| 1012 | * pages. |
| 1013 | */ |
| 1014 | TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); |
| 1015 | if (vm_pageout_clean(m) != 0) { |
| 1016 | --inactive_shortage; |
| 1017 | --maxlaunder; |
| 1018 | } |
| 1019 | next = TAILQ_NEXT(&marker, pageq); |
| 1020 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); |
| 1021 | if (vp != NULL) |
| 1022 | vput(vp); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * We want to move pages from the active queue to the inactive |
| 1028 | * queue to get the inactive queue to the inactive target. If |
| 1029 | * we still have a page shortage from above we try to directly free |
| 1030 | * clean pages instead of moving them. |
| 1031 | * |
| 1032 | * If we do still have a shortage we keep track of the number of |
| 1033 | * pages we free or cache (recycle_count) as a measure of thrashing |
| 1034 | * between the active and inactive queues. |
| 1035 | * |
| 1036 | * If we were able to completely satisfy the free+cache targets |
| 1037 | * from the inactive pool we limit the number of pages we move |
| 1038 | * from the active pool to the inactive pool to 2x the pages we |
| 1039 | * had removed from the inactive pool. If we were not able to |
| 1040 | * completel satisfy the free+cache targets we go for the whole |
| 1041 | * target aggressively. |
| 1042 | * |
| 1043 | * NOTE: Both variables can end up negative. |
| 1044 | * NOTE: We are still in a critical section. |
| 1045 | */ |
| 1046 | active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count; |
| 1047 | if (inactive_shortage <= 0 && |
| 1048 | active_shortage > inactive_original_shortage * 2) { |
| 1049 | active_shortage = inactive_original_shortage * 2; |
| 1050 | } |
| 1051 | |
| 1052 | pcount = vmstats.v_active_count; |
| 1053 | recycle_count = 0; |
| 1054 | m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); |
| 1055 | |
| 1056 | while ((m != NULL) && (pcount-- > 0) && |
| 1057 | (inactive_shortage > 0 || active_shortage > 0) |
| 1058 | ) { |
| 1059 | /* |
| 1060 | * Give interrupts a chance. |
| 1061 | */ |
| 1062 | crit_exit(); |
| 1063 | crit_enter(); |
| 1064 | |
| 1065 | /* |
| 1066 | * If the page was ripped out from under us, just stop. |
| 1067 | */ |
| 1068 | if (m->queue != PQ_ACTIVE) |
| 1069 | break; |
| 1070 | next = TAILQ_NEXT(m, pageq); |
| 1071 | |
| 1072 | /* |
| 1073 | * Don't deactivate pages that are busy. |
| 1074 | */ |
| 1075 | if ((m->busy != 0) || |
| 1076 | (m->flags & PG_BUSY) || |
| 1077 | (m->hold_count != 0)) { |
| 1078 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1079 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1080 | m = next; |
| 1081 | continue; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * The count for pagedaemon pages is done after checking the |
| 1086 | * page for eligibility... |
| 1087 | */ |
| 1088 | mycpu->gd_cnt.v_pdpages++; |
| 1089 | |
| 1090 | /* |
| 1091 | * Check to see "how much" the page has been used and clear |
| 1092 | * the tracking access bits. If the object has no references |
| 1093 | * don't bother paying the expense. |
| 1094 | */ |
| 1095 | actcount = 0; |
| 1096 | if (m->object->ref_count != 0) { |
| 1097 | if (m->flags & PG_REFERENCED) |
| 1098 | ++actcount; |
| 1099 | actcount += pmap_ts_referenced(m); |
| 1100 | if (actcount) { |
| 1101 | m->act_count += ACT_ADVANCE + actcount; |
| 1102 | if (m->act_count > ACT_MAX) |
| 1103 | m->act_count = ACT_MAX; |
| 1104 | } |
| 1105 | } |
| 1106 | vm_page_flag_clear(m, PG_REFERENCED); |
| 1107 | |
| 1108 | /* |
| 1109 | * actcount is only valid if the object ref_count is non-zero. |
| 1110 | */ |
| 1111 | if (actcount && m->object->ref_count != 0) { |
| 1112 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1113 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1114 | } else { |
| 1115 | m->act_count -= min(m->act_count, ACT_DECLINE); |
| 1116 | if (vm_pageout_algorithm || |
| 1117 | m->object->ref_count == 0 || |
| 1118 | m->act_count < pass + 1 |
| 1119 | ) { |
| 1120 | /* |
| 1121 | * Deactivate the page. If we had a |
| 1122 | * shortage from our inactive scan try to |
| 1123 | * free (cache) the page instead. |
| 1124 | */ |
| 1125 | --active_shortage; |
| 1126 | if (inactive_shortage > 0 || |
| 1127 | m->object->ref_count == 0) { |
| 1128 | if (inactive_shortage > 0) |
| 1129 | ++recycle_count; |
| 1130 | vm_page_busy(m); |
| 1131 | vm_page_protect(m, VM_PROT_NONE); |
| 1132 | vm_page_wakeup(m); |
| 1133 | if (m->dirty == 0) { |
| 1134 | --inactive_shortage; |
| 1135 | vm_page_cache(m); |
| 1136 | } else { |
| 1137 | vm_page_deactivate(m); |
| 1138 | } |
| 1139 | } else { |
| 1140 | vm_page_deactivate(m); |
| 1141 | } |
| 1142 | } else { |
| 1143 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1144 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1145 | } |
| 1146 | } |
| 1147 | m = next; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * We try to maintain some *really* free pages, this allows interrupt |
| 1152 | * code to be guaranteed space. Since both cache and free queues |
| 1153 | * are considered basically 'free', moving pages from cache to free |
| 1154 | * does not effect other calculations. |
| 1155 | * |
| 1156 | * NOTE: we are still in a critical section. |
| 1157 | * |
| 1158 | * Pages moved from PQ_CACHE to totally free are not counted in the |
| 1159 | * pages_freed counter. |
| 1160 | */ |
| 1161 | while (vmstats.v_free_count < vmstats.v_free_reserved) { |
| 1162 | static int cache_rover = 0; |
| 1163 | m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); |
| 1164 | if (m == NULL) |
| 1165 | break; |
| 1166 | if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || |
| 1167 | m->busy || |
| 1168 | m->hold_count || |
| 1169 | m->wire_count) { |
| 1170 | #ifdef INVARIANTS |
| 1171 | kprintf("Warning: busy page %p found in cache\n", m); |
| 1172 | #endif |
| 1173 | vm_page_deactivate(m); |
| 1174 | continue; |
| 1175 | } |
| 1176 | KKASSERT((m->flags & PG_MAPPED) == 0); |
| 1177 | KKASSERT(m->dirty == 0); |
| 1178 | cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; |
| 1179 | vm_pageout_page_free(m); |
| 1180 | mycpu->gd_cnt.v_dfree++; |
| 1181 | } |
| 1182 | |
| 1183 | crit_exit(); |
| 1184 | |
| 1185 | #if !defined(NO_SWAPPING) |
| 1186 | /* |
| 1187 | * Idle process swapout -- run once per second. |
| 1188 | */ |
| 1189 | if (vm_swap_idle_enabled) { |
| 1190 | static long lsec; |
| 1191 | if (time_second != lsec) { |
| 1192 | vm_pageout_req_swapout |= VM_SWAP_IDLE; |
| 1193 | vm_req_vmdaemon(); |
| 1194 | lsec = time_second; |
| 1195 | } |
| 1196 | } |
| 1197 | #endif |
| 1198 | |
| 1199 | /* |
| 1200 | * If we didn't get enough free pages, and we have skipped a vnode |
| 1201 | * in a writeable object, wakeup the sync daemon. And kick swapout |
| 1202 | * if we did not get enough free pages. |
| 1203 | */ |
| 1204 | if (vm_paging_target() > 0) { |
| 1205 | if (vnodes_skipped && vm_page_count_min(0)) |
| 1206 | speedup_syncer(); |
| 1207 | #if !defined(NO_SWAPPING) |
| 1208 | if (vm_swap_enabled && vm_page_count_target()) { |
| 1209 | vm_req_vmdaemon(); |
| 1210 | vm_pageout_req_swapout |= VM_SWAP_NORMAL; |
| 1211 | } |
| 1212 | #endif |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Handle catastrophic conditions. Under good conditions we should |
| 1217 | * be at the target, well beyond our minimum. If we could not even |
| 1218 | * reach our minimum the system is under heavy stress. |
| 1219 | * |
| 1220 | * Determine whether we have run out of memory. This occurs when |
| 1221 | * swap_pager_full is TRUE and the only pages left in the page |
| 1222 | * queues are dirty. We will still likely have page shortages. |
| 1223 | * |
| 1224 | * - swap_pager_full is set if insufficient swap was |
| 1225 | * available to satisfy a requested pageout. |
| 1226 | * |
| 1227 | * - the inactive queue is bloated (4 x size of active queue), |
| 1228 | * meaning it is unable to get rid of dirty pages and. |
| 1229 | * |
| 1230 | * - vm_page_count_min() without counting pages recycled from the |
| 1231 | * active queue (recycle_count) means we could not recover |
| 1232 | * enough pages to meet bare minimum needs. This test only |
| 1233 | * works if the inactive queue is bloated. |
| 1234 | * |
| 1235 | * - due to a positive inactive_shortage we shifted the remaining |
| 1236 | * dirty pages from the active queue to the inactive queue |
| 1237 | * trying to find clean ones to free. |
| 1238 | */ |
| 1239 | if (swap_pager_full && vm_page_count_min(recycle_count)) |
| 1240 | kprintf("Warning: system low on memory+swap!\n"); |
| 1241 | if (swap_pager_full && vm_page_count_min(recycle_count) && |
| 1242 | vmstats.v_inactive_count > vmstats.v_active_count * 4 && |
| 1243 | inactive_shortage > 0) { |
| 1244 | /* |
| 1245 | * Kill something. |
| 1246 | */ |
| 1247 | info.bigproc = NULL; |
| 1248 | info.bigsize = 0; |
| 1249 | allproc_scan(vm_pageout_scan_callback, &info); |
| 1250 | if (info.bigproc != NULL) { |
| 1251 | killproc(info.bigproc, "out of swap space"); |
| 1252 | info.bigproc->p_nice = PRIO_MIN; |
| 1253 | info.bigproc->p_usched->resetpriority( |
| 1254 | FIRST_LWP_IN_PROC(info.bigproc)); |
| 1255 | wakeup(&vmstats.v_free_count); |
| 1256 | PRELE(info.bigproc); |
| 1257 | } |
| 1258 | } |
| 1259 | return(inactive_shortage); |
| 1260 | } |
| 1261 | |
| 1262 | static int |
| 1263 | vm_pageout_scan_callback(struct proc *p, void *data) |
| 1264 | { |
| 1265 | struct vm_pageout_scan_info *info = data; |
| 1266 | vm_offset_t size; |
| 1267 | |
| 1268 | /* |
| 1269 | * Never kill system processes or init. If we have configured swap |
| 1270 | * then try to avoid killing low-numbered pids. |
| 1271 | */ |
| 1272 | if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || |
| 1273 | ((p->p_pid < 48) && (vm_swap_size != 0))) { |
| 1274 | return (0); |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * if the process is in a non-running type state, |
| 1279 | * don't touch it. |
| 1280 | */ |
| 1281 | if (p->p_stat != SACTIVE && p->p_stat != SSTOP) |
| 1282 | return (0); |
| 1283 | |
| 1284 | /* |
| 1285 | * Get the approximate process size. Note that anonymous pages |
| 1286 | * with backing swap will be counted twice, but there should not |
| 1287 | * be too many such pages due to the stress the VM system is |
| 1288 | * under at this point. |
| 1289 | */ |
| 1290 | size = vmspace_anonymous_count(p->p_vmspace) + |
| 1291 | vmspace_swap_count(p->p_vmspace); |
| 1292 | |
| 1293 | /* |
| 1294 | * If the this process is bigger than the biggest one |
| 1295 | * remember it. |
| 1296 | */ |
| 1297 | if (info->bigsize < size) { |
| 1298 | if (info->bigproc) |
| 1299 | PRELE(info->bigproc); |
| 1300 | PHOLD(p); |
| 1301 | info->bigproc = p; |
| 1302 | info->bigsize = size; |
| 1303 | } |
| 1304 | return(0); |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * This routine tries to maintain the pseudo LRU active queue, |
| 1309 | * so that during long periods of time where there is no paging, |
| 1310 | * that some statistic accumulation still occurs. This code |
| 1311 | * helps the situation where paging just starts to occur. |
| 1312 | */ |
| 1313 | static void |
| 1314 | vm_pageout_page_stats(void) |
| 1315 | { |
| 1316 | vm_page_t m,next; |
| 1317 | int pcount,tpcount; /* Number of pages to check */ |
| 1318 | static int fullintervalcount = 0; |
| 1319 | int page_shortage; |
| 1320 | |
| 1321 | page_shortage = |
| 1322 | (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) - |
| 1323 | (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count); |
| 1324 | |
| 1325 | if (page_shortage <= 0) |
| 1326 | return; |
| 1327 | |
| 1328 | crit_enter(); |
| 1329 | |
| 1330 | pcount = vmstats.v_active_count; |
| 1331 | fullintervalcount += vm_pageout_stats_interval; |
| 1332 | if (fullintervalcount < vm_pageout_full_stats_interval) { |
| 1333 | tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count; |
| 1334 | if (pcount > tpcount) |
| 1335 | pcount = tpcount; |
| 1336 | } else { |
| 1337 | fullintervalcount = 0; |
| 1338 | } |
| 1339 | |
| 1340 | m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); |
| 1341 | while ((m != NULL) && (pcount-- > 0)) { |
| 1342 | int actcount; |
| 1343 | |
| 1344 | if (m->queue != PQ_ACTIVE) { |
| 1345 | break; |
| 1346 | } |
| 1347 | |
| 1348 | next = TAILQ_NEXT(m, pageq); |
| 1349 | /* |
| 1350 | * Don't deactivate pages that are busy. |
| 1351 | */ |
| 1352 | if ((m->busy != 0) || |
| 1353 | (m->flags & PG_BUSY) || |
| 1354 | (m->hold_count != 0)) { |
| 1355 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1356 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1357 | m = next; |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | actcount = 0; |
| 1362 | if (m->flags & PG_REFERENCED) { |
| 1363 | vm_page_flag_clear(m, PG_REFERENCED); |
| 1364 | actcount += 1; |
| 1365 | } |
| 1366 | |
| 1367 | actcount += pmap_ts_referenced(m); |
| 1368 | if (actcount) { |
| 1369 | m->act_count += ACT_ADVANCE + actcount; |
| 1370 | if (m->act_count > ACT_MAX) |
| 1371 | m->act_count = ACT_MAX; |
| 1372 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1373 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1374 | } else { |
| 1375 | if (m->act_count == 0) { |
| 1376 | /* |
| 1377 | * We turn off page access, so that we have |
| 1378 | * more accurate RSS stats. We don't do this |
| 1379 | * in the normal page deactivation when the |
| 1380 | * system is loaded VM wise, because the |
| 1381 | * cost of the large number of page protect |
| 1382 | * operations would be higher than the value |
| 1383 | * of doing the operation. |
| 1384 | */ |
| 1385 | vm_page_busy(m); |
| 1386 | vm_page_protect(m, VM_PROT_NONE); |
| 1387 | vm_page_wakeup(m); |
| 1388 | vm_page_deactivate(m); |
| 1389 | } else { |
| 1390 | m->act_count -= min(m->act_count, ACT_DECLINE); |
| 1391 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1392 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | m = next; |
| 1397 | } |
| 1398 | crit_exit(); |
| 1399 | } |
| 1400 | |
| 1401 | static int |
| 1402 | vm_pageout_free_page_calc(vm_size_t count) |
| 1403 | { |
| 1404 | if (count < vmstats.v_page_count) |
| 1405 | return 0; |
| 1406 | /* |
| 1407 | * free_reserved needs to include enough for the largest swap pager |
| 1408 | * structures plus enough for any pv_entry structs when paging. |
| 1409 | */ |
| 1410 | if (vmstats.v_page_count > 1024) |
| 1411 | vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200; |
| 1412 | else |
| 1413 | vmstats.v_free_min = 4; |
| 1414 | vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + |
| 1415 | vmstats.v_interrupt_free_min; |
| 1416 | vmstats.v_free_reserved = vm_pageout_page_count + |
| 1417 | vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; |
| 1418 | vmstats.v_free_severe = vmstats.v_free_min / 2; |
| 1419 | vmstats.v_free_min += vmstats.v_free_reserved; |
| 1420 | vmstats.v_free_severe += vmstats.v_free_reserved; |
| 1421 | return 1; |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | /* |
| 1426 | * vm_pageout is the high level pageout daemon. |
| 1427 | */ |
| 1428 | static void |
| 1429 | vm_pageout(void) |
| 1430 | { |
| 1431 | int pass; |
| 1432 | int inactive_shortage; |
| 1433 | |
| 1434 | /* |
| 1435 | * Initialize some paging parameters. |
| 1436 | */ |
| 1437 | curthread->td_flags |= TDF_SYSTHREAD; |
| 1438 | |
| 1439 | vmstats.v_interrupt_free_min = 2; |
| 1440 | if (vmstats.v_page_count < 2000) |
| 1441 | vm_pageout_page_count = 8; |
| 1442 | |
| 1443 | vm_pageout_free_page_calc(vmstats.v_page_count); |
| 1444 | |
| 1445 | /* |
| 1446 | * v_free_target and v_cache_min control pageout hysteresis. Note |
| 1447 | * that these are more a measure of the VM cache queue hysteresis |
| 1448 | * then the VM free queue. Specifically, v_free_target is the |
| 1449 | * high water mark (free+cache pages). |
| 1450 | * |
| 1451 | * v_free_reserved + v_cache_min (mostly means v_cache_min) is the |
| 1452 | * low water mark, while v_free_min is the stop. v_cache_min must |
| 1453 | * be big enough to handle memory needs while the pageout daemon |
| 1454 | * is signalled and run to free more pages. |
| 1455 | */ |
| 1456 | if (vmstats.v_free_count > 6144) |
| 1457 | vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved; |
| 1458 | else |
| 1459 | vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved; |
| 1460 | |
| 1461 | /* |
| 1462 | * NOTE: With the new buffer cache b_act_count we want the default |
| 1463 | * inactive target to be a percentage of available memory. |
| 1464 | * |
| 1465 | * The inactive target essentially determines the minimum |
| 1466 | * number of 'temporary' pages capable of caching one-time-use |
| 1467 | * files when the VM system is otherwise full of pages |
| 1468 | * belonging to multi-time-use files or active program data. |
| 1469 | * |
| 1470 | * NOTE: The inactive target is aggressively persued only if the |
| 1471 | * inactive queue becomes too small. If the inactive queue |
| 1472 | * is large enough to satisfy page movement to free+cache |
| 1473 | * then it is repopulated more slowly from the active queue. |
| 1474 | * This allows a generate inactive_target default to be set. |
| 1475 | * |
| 1476 | * There is an issue here for processes which sit mostly idle |
| 1477 | * 'overnight', such as sshd, tcsh, and X. Any movement from |
| 1478 | * the active queue will eventually cause such pages to |
| 1479 | * recycle eventually causing a lot of paging in the morning. |
| 1480 | * To reduce the incidence of this pages cycled out of the |
| 1481 | * buffer cache are moved directly to the inactive queue if |
| 1482 | * they were only used once or twice. The vfs.vm_cycle_point |
| 1483 | * sysctl can be used to adjust this. |
| 1484 | */ |
| 1485 | if (vmstats.v_free_count > 2048) { |
| 1486 | vmstats.v_cache_min = vmstats.v_free_target; |
| 1487 | vmstats.v_cache_max = 2 * vmstats.v_cache_min; |
| 1488 | } else { |
| 1489 | vmstats.v_cache_min = 0; |
| 1490 | vmstats.v_cache_max = 0; |
| 1491 | } |
| 1492 | vmstats.v_inactive_target = vmstats.v_free_count / 2; |
| 1493 | |
| 1494 | /* XXX does not really belong here */ |
| 1495 | if (vm_page_max_wired == 0) |
| 1496 | vm_page_max_wired = vmstats.v_free_count / 3; |
| 1497 | |
| 1498 | if (vm_pageout_stats_max == 0) |
| 1499 | vm_pageout_stats_max = vmstats.v_free_target; |
| 1500 | |
| 1501 | /* |
| 1502 | * Set interval in seconds for stats scan. |
| 1503 | */ |
| 1504 | if (vm_pageout_stats_interval == 0) |
| 1505 | vm_pageout_stats_interval = 5; |
| 1506 | if (vm_pageout_full_stats_interval == 0) |
| 1507 | vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; |
| 1508 | |
| 1509 | |
| 1510 | /* |
| 1511 | * Set maximum free per pass |
| 1512 | */ |
| 1513 | if (vm_pageout_stats_free_max == 0) |
| 1514 | vm_pageout_stats_free_max = 5; |
| 1515 | |
| 1516 | swap_pager_swap_init(); |
| 1517 | pass = 0; |
| 1518 | |
| 1519 | /* |
| 1520 | * The pageout daemon is never done, so loop forever. |
| 1521 | */ |
| 1522 | while (TRUE) { |
| 1523 | int error; |
| 1524 | |
| 1525 | /* |
| 1526 | * Wait for an action request |
| 1527 | */ |
| 1528 | crit_enter(); |
| 1529 | if (vm_pages_needed == 0) { |
| 1530 | error = tsleep(&vm_pages_needed, |
| 1531 | 0, "psleep", |
| 1532 | vm_pageout_stats_interval * hz); |
| 1533 | if (error && vm_pages_needed == 0) { |
| 1534 | vm_pageout_page_stats(); |
| 1535 | continue; |
| 1536 | } |
| 1537 | vm_pages_needed = 1; |
| 1538 | } |
| 1539 | crit_exit(); |
| 1540 | |
| 1541 | /* |
| 1542 | * If we have enough free memory, wakeup waiters. |
| 1543 | * (This is optional here) |
| 1544 | */ |
| 1545 | crit_enter(); |
| 1546 | if (!vm_page_count_min(0)) |
| 1547 | wakeup(&vmstats.v_free_count); |
| 1548 | mycpu->gd_cnt.v_pdwakeups++; |
| 1549 | crit_exit(); |
| 1550 | |
| 1551 | /* |
| 1552 | * Scan for pageout. Try to avoid thrashing the system |
| 1553 | * with activity. |
| 1554 | */ |
| 1555 | inactive_shortage = vm_pageout_scan(pass); |
| 1556 | if (inactive_shortage > 0) { |
| 1557 | ++pass; |
| 1558 | if (swap_pager_full) { |
| 1559 | /* |
| 1560 | * Running out of memory, catastrophic back-off |
| 1561 | * to one-second intervals. |
| 1562 | */ |
| 1563 | tsleep(&vm_pages_needed, 0, "pdelay", hz); |
| 1564 | } else if (pass < 10 && vm_pages_needed > 1) { |
| 1565 | /* |
| 1566 | * Normal operation, additional processes |
| 1567 | * have already kicked us. Retry immediately. |
| 1568 | */ |
| 1569 | } else if (pass < 10) { |
| 1570 | /* |
| 1571 | * Normal operation, fewer processes. Delay |
| 1572 | * a bit but allow wakeups. |
| 1573 | */ |
| 1574 | vm_pages_needed = 0; |
| 1575 | tsleep(&vm_pages_needed, 0, "pdelay", hz / 10); |
| 1576 | vm_pages_needed = 1; |
| 1577 | } else { |
| 1578 | /* |
| 1579 | * We've taken too many passes, forced delay. |
| 1580 | */ |
| 1581 | tsleep(&vm_pages_needed, 0, "pdelay", hz / 10); |
| 1582 | } |
| 1583 | } else { |
| 1584 | /* |
| 1585 | * Interlocked wakeup of waiters (non-optional) |
| 1586 | */ |
| 1587 | pass = 0; |
| 1588 | if (vm_pages_needed && !vm_page_count_min(0)) { |
| 1589 | wakeup(&vmstats.v_free_count); |
| 1590 | vm_pages_needed = 0; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * Called after allocating a page out of the cache or free queue |
| 1598 | * to possibly wake the pagedaemon up to replentish our supply. |
| 1599 | * |
| 1600 | * We try to generate some hysteresis by waking the pagedaemon up |
| 1601 | * when our free+cache pages go below the severe level. The pagedaemon |
| 1602 | * tries to get the count back up to at least the minimum, and through |
| 1603 | * to the target level if possible. |
| 1604 | * |
| 1605 | * If the pagedaemon is already active bump vm_pages_needed as a hint |
| 1606 | * that there are even more requests pending. |
| 1607 | */ |
| 1608 | void |
| 1609 | pagedaemon_wakeup(void) |
| 1610 | { |
| 1611 | if (vm_page_count_severe() && curthread != pagethread) { |
| 1612 | if (vm_pages_needed == 0) { |
| 1613 | vm_pages_needed = 1; |
| 1614 | wakeup(&vm_pages_needed); |
| 1615 | } else if (vm_page_count_min(0)) { |
| 1616 | ++vm_pages_needed; |
| 1617 | } |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | #if !defined(NO_SWAPPING) |
| 1622 | static void |
| 1623 | vm_req_vmdaemon(void) |
| 1624 | { |
| 1625 | static int lastrun = 0; |
| 1626 | |
| 1627 | if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { |
| 1628 | wakeup(&vm_daemon_needed); |
| 1629 | lastrun = ticks; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | static int vm_daemon_callback(struct proc *p, void *data __unused); |
| 1634 | |
| 1635 | static void |
| 1636 | vm_daemon(void) |
| 1637 | { |
| 1638 | while (TRUE) { |
| 1639 | tsleep(&vm_daemon_needed, 0, "psleep", 0); |
| 1640 | if (vm_pageout_req_swapout) { |
| 1641 | swapout_procs(vm_pageout_req_swapout); |
| 1642 | vm_pageout_req_swapout = 0; |
| 1643 | } |
| 1644 | /* |
| 1645 | * scan the processes for exceeding their rlimits or if |
| 1646 | * process is swapped out -- deactivate pages |
| 1647 | */ |
| 1648 | allproc_scan(vm_daemon_callback, NULL); |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | static int |
| 1653 | vm_daemon_callback(struct proc *p, void *data __unused) |
| 1654 | { |
| 1655 | vm_pindex_t limit, size; |
| 1656 | |
| 1657 | /* |
| 1658 | * if this is a system process or if we have already |
| 1659 | * looked at this process, skip it. |
| 1660 | */ |
| 1661 | if (p->p_flag & (P_SYSTEM | P_WEXIT)) |
| 1662 | return (0); |
| 1663 | |
| 1664 | /* |
| 1665 | * if the process is in a non-running type state, |
| 1666 | * don't touch it. |
| 1667 | */ |
| 1668 | if (p->p_stat != SACTIVE && p->p_stat != SSTOP) |
| 1669 | return (0); |
| 1670 | |
| 1671 | /* |
| 1672 | * get a limit |
| 1673 | */ |
| 1674 | limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, |
| 1675 | p->p_rlimit[RLIMIT_RSS].rlim_max)); |
| 1676 | |
| 1677 | /* |
| 1678 | * let processes that are swapped out really be |
| 1679 | * swapped out. Set the limit to nothing to get as |
| 1680 | * many pages out to swap as possible. |
| 1681 | */ |
| 1682 | if (p->p_flag & P_SWAPPEDOUT) |
| 1683 | limit = 0; |
| 1684 | |
| 1685 | size = vmspace_resident_count(p->p_vmspace); |
| 1686 | if (limit >= 0 && size >= limit) { |
| 1687 | vm_pageout_map_deactivate_pages( |
| 1688 | &p->p_vmspace->vm_map, limit); |
| 1689 | } |
| 1690 | return (0); |
| 1691 | } |
| 1692 | |
| 1693 | #endif |