vm_zeroidle.c revision 198854
179265Sdillon/*- 279265Sdillon * Copyright (c) 1994 John Dyson 379265Sdillon * Copyright (c) 2001 Matt Dillon 479265Sdillon * 5118848Simp * All Rights Reserved. 6118848Simp * Redistribution and use in source and binary forms, with or without 7118848Simp * modification, are permitted provided that the following conditions 8118848Simp * are met: 9118848Simp * 1. Redistributions of source code must retain the above copyright 10118848Simp * notice, this list of conditions and the following disclaimer. 11118848Simp * 2. Redistributions in binary form must reproduce the above copyright 12118848Simp * notice, this list of conditions and the following disclaimer in the 13118848Simp * documentation and/or other materials provided with the distribution. 14118848Simp * 4. Neither the name of the University nor the names of its contributors 15118848Simp * may be used to endorse or promote products derived from this software 16118848Simp * without specific prior written permission. 1779265Sdillon * 18118848Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 19118848Simp * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20118848Simp * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21118848Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 22118848Simp * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23118848Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 24118848Simp * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25118848Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26118848Simp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27118848Simp * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28118848Simp * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29118848Simp * 3079265Sdillon * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 3179265Sdillon * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 32126588Sbde * from: FreeBSD: .../i386/vm_machdep.c,v 1.165 2001/07/04 23:27:04 dillon 3379265Sdillon */ 3479265Sdillon 35116226Sobrien#include <sys/cdefs.h> 36116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/vm_zeroidle.c 198854 2009-11-03 16:46:52Z attilio $"); 37116226Sobrien 38134649Sscottl#include <opt_sched.h> 39134649Sscottl 4079265Sdillon#include <sys/param.h> 4179265Sdillon#include <sys/systm.h> 4282314Speter#include <sys/kernel.h> 4379265Sdillon#include <sys/proc.h> 4479265Sdillon#include <sys/vmmeter.h> 4582314Speter#include <sys/lock.h> 4679265Sdillon#include <sys/mutex.h> 47104964Sjeff#include <sys/sched.h> 4879265Sdillon#include <sys/sysctl.h> 4982314Speter#include <sys/kthread.h> 50125314Sjeff#include <sys/unistd.h> 5179265Sdillon 5279265Sdillon#include <vm/vm.h> 5379265Sdillon#include <vm/vm_page.h> 5479265Sdillon#include <vm/vm_phys.h> 5579265Sdillon 56126588Sbdestatic int idlezero_enable_default = 0; 57126588SbdeTUNABLE_INT("vm.idlezero_enable", &idlezero_enable_default); 5879265Sdillon/* Defer setting the enable flag until the kthread is running. */ 59134461Siedowsestatic int idlezero_enable = 0; 60134461SiedowseSYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, 61134461Siedowse "Allow the kernel to use idle cpu cycles to zero-out pages"); 62134461Siedowse/* 6382314Speter * Implement the pre-zeroed page mechanism. 6482314Speter */ 6582314Speter 6682314Speter#define ZIDLE_LO(v) ((v) * 2 / 3) 6782314Speter#define ZIDLE_HI(v) ((v) * 4 / 5) 6882314Speter 6979265Sdillonstatic boolean_t wakeup_needed = FALSE; 7079265Sdillonstatic int zero_state; 7179265Sdillon 7279265Sdillonstatic int 7379265Sdillonvm_page_zero_check(void) 7479265Sdillon{ 7579265Sdillon 76137104Salc if (!idlezero_enable) 7782314Speter return (0); 7882314Speter /* 7982314Speter * Attempt to maintain approximately 1/2 of our free pages in a 8082314Speter * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 8179265Sdillon * generally zeroing a page when the system is near steady-state. 8279265Sdillon * Otherwise we might get 'flutter' during disk I/O / IPC or 8382314Speter * fast sleeps. We also do not want to be continuously zeroing 84126588Sbde * pages because doing so may flush our L1 and L2 caches too much. 8579265Sdillon */ 8679265Sdillon if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 8779265Sdillon return (0); 8879265Sdillon if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 8979265Sdillon return (0); 9079265Sdillon return (1); 9179265Sdillon} 9279265Sdillon 93170170Sattiliostatic void 94126588Sbdevm_page_zero_idle(void) 95170170Sattilio{ 96126588Sbde 97126588Sbde mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 9882314Speter zero_state = 0; 9979265Sdillon if (vm_phys_zero_pages_idle()) { 100161489Salc if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 10182314Speter zero_state = 1; 10282314Speter } 10382314Speter} 10482314Speter 10582314Speter/* Called by vm_page_free to hint that a new page is available. */ 106166637Salcvoid 10782314Spetervm_page_zero_idle_wakeup(void) 10882314Speter{ 10982314Speter 110100193Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 111166508Salc if (wakeup_needed && vm_page_zero_check()) { 11299571Speter wakeup_needed = FALSE; 113166508Salc wakeup(&zero_state); 114100331Salc } 115100193Salc} 11682314Speter 11782314Speterstatic void 118170170Sattiliovm_pagezero(void __unused *arg) 11982314Speter{ 12082314Speter 121153940Snetchild idlezero_enable = idlezero_enable_default; 12282314Speter 12382314Speter mtx_lock(&vm_page_queue_free_mtx); 124126588Sbde for (;;) { 12582314Speter if (vm_page_zero_check()) { 12682314Speter vm_page_zero_idle(); 12782314Speter#ifndef PREEMPTION 12882314Speter if (sched_runnable()) { 129166637Salc thread_lock(curthread); 130137104Salc mi_switch(SW_VOL | SWT_IDLE, NULL); 131137104Salc thread_unlock(curthread); 13282314Speter } 133137104Salc#endif 13482314Speter } else { 13582314Speter wakeup_needed = TRUE; 13682314Speter msleep(&zero_state, &vm_page_queue_free_mtx, 0, 137125314Sjeff "pgzero", hz * 300); 13882314Speter } 13982314Speter } 140134461Siedowse} 14182314Speter 142166637Salcstatic void 14382314Speterpagezero_start(void __unused *arg) 14482314Speter{ 145137268Sjhb int error; 146131481Sjhb struct proc *p; 147137268Sjhb struct thread *td; 14882314Speter 149131473Sjhb error = kproc_create(vm_pagezero, NULL, &p, RFSTOPPED, 0, "pagezero"); 15082314Speter if (error) 15182314Speter panic("pagezero_start: error %d\n", error); 152131481Sjhb td = FIRST_THREAD_IN_PROC(p); 15382314Speter thread_lock(td); 154137104Salc 155166637Salc /* We're an idle task, don't count us in the load. */ 156166637Salc td->td_flags |= TDF_NOLOAD; 15779265Sdillon sched_class(td, PRI_IDLE); 15879265Sdillon sched_prio(td, PRI_MAX_IDLE); 15979265Sdillon sched_add(td, SRQ_BORING); 16079265Sdillon thread_unlock(td); 161113070Sdes} 162125314SjeffSYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL); 163125314Sjeff