vm_zeroidle.c revision 118848
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$ 3279265Sdillon */ 3379265Sdillon 34116226Sobrien#include <sys/cdefs.h> 35116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/vm_zeroidle.c 118848 2003-08-12 23:24:05Z imp $"); 36116226Sobrien 3779265Sdillon#include <sys/param.h> 3879265Sdillon#include <sys/systm.h> 3982314Speter#include <sys/kernel.h> 4079265Sdillon#include <sys/proc.h> 4182314Speter#include <sys/resourcevar.h> 4279265Sdillon#include <sys/vmmeter.h> 4382314Speter#include <sys/lock.h> 4479265Sdillon#include <sys/mutex.h> 45104964Sjeff#include <sys/sched.h> 4679265Sdillon#include <sys/sysctl.h> 4782314Speter#include <sys/kthread.h> 4879265Sdillon 4979265Sdillon#include <vm/vm.h> 5079265Sdillon#include <vm/vm_page.h> 5179265Sdillon 5279265SdillonSYSCTL_DECL(_vm_stats_misc); 5379265Sdillon 5479265Sdillonstatic int cnt_prezero; 5579265SdillonSYSCTL_INT(_vm_stats_misc, OID_AUTO, 5679265Sdillon cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, ""); 5779265Sdillon 5899571Speterstatic int idlezero_enable = 1; 5982314SpeterSYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, ""); 6082314SpeterTUNABLE_INT("vm.idlezero_enable", &idlezero_enable); 6182314Speter 6282314Speterstatic int idlezero_maxrun = 16; 6382314SpeterSYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, ""); 6482314SpeterTUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun); 6582314Speter 6679265Sdillon/* 6779265Sdillon * Implement the pre-zeroed page mechanism. 6879265Sdillon */ 6979265Sdillon 7079265Sdillon#define ZIDLE_LO(v) ((v) * 2 / 3) 7179265Sdillon#define ZIDLE_HI(v) ((v) * 4 / 5) 7279265Sdillon 7382314Speterstatic int zero_state; 7482314Speter 7582314Speterstatic int 7682314Spetervm_page_zero_check(void) 7779265Sdillon{ 7879265Sdillon 7982314Speter if (!idlezero_enable) 8082314Speter return 0; 8179265Sdillon /* 8279265Sdillon * Attempt to maintain approximately 1/2 of our free pages in a 8379265Sdillon * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 8479265Sdillon * generally zeroing a page when the system is near steady-state. 8579265Sdillon * Otherwise we might get 'flutter' during disk I/O / IPC or 8679265Sdillon * fast sleeps. We also do not want to be continuously zeroing 8779265Sdillon * pages because doing so may flush our L1 and L2 caches too much. 8879265Sdillon */ 8979265Sdillon if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 9082314Speter return 0; 9179265Sdillon if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 9282314Speter return 0; 9382314Speter return 1; 9482314Speter} 9579265Sdillon 9682314Speterstatic int 9782314Spetervm_page_zero_idle(void) 9882314Speter{ 9982314Speter static int free_rover; 10082314Speter vm_page_t m; 10182314Speter 10299625Speter mtx_lock_spin(&vm_page_queue_free_mtx); 10382314Speter zero_state = 0; 10482314Speter m = vm_pageq_find(PQ_FREE, free_rover, FALSE); 10582314Speter if (m != NULL && (m->flags & PG_ZERO) == 0) { 106100193Salc vm_pageq_remove_nowakeup(m); 10799625Speter mtx_unlock_spin(&vm_page_queue_free_mtx); 10899571Speter pmap_zero_page_idle(m); 10999625Speter mtx_lock_spin(&vm_page_queue_free_mtx); 110100331Salc m->flags |= PG_ZERO; 111100193Salc vm_pageq_enqueue(PQ_FREE + m->pc, m); 11282314Speter ++vm_page_zero_count; 11382314Speter ++cnt_prezero; 11482314Speter if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 11582314Speter zero_state = 1; 11682314Speter } 11782314Speter free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK; 11899625Speter mtx_unlock_spin(&vm_page_queue_free_mtx); 11982314Speter return 1; 12082314Speter} 12182314Speter 12282314Speter 12382314Speter/* Called by vm_page_free to hint that a new page is available */ 12482314Spetervoid 12582314Spetervm_page_zero_idle_wakeup(void) 12682314Speter{ 12782314Speter 12899571Speter if (idlezero_enable && vm_page_zero_check()) 12982314Speter wakeup(&zero_state); 13082314Speter} 13182314Speter 13282314Speterstatic void 13382314Spetervm_pagezero(void) 13482314Speter{ 135100379Speter struct thread *td; 136100379Speter struct proc *p; 13782314Speter struct rtprio rtp; 13882314Speter int pages = 0; 13999571Speter int pri; 14082314Speter 141100379Speter td = curthread; 142100379Speter p = td->td_proc; 14382314Speter rtp.prio = RTP_PRIO_MAX; 14482314Speter rtp.type = RTP_PRIO_IDLE; 14582756Sjhb mtx_lock_spin(&sched_lock); 14690538Sjulian rtp_to_pri(&rtp, td->td_ksegrp); 14799571Speter pri = td->td_priority; 14882756Sjhb mtx_unlock_spin(&sched_lock); 149100379Speter PROC_LOCK(p); 150100379Speter p->p_flag |= P_NOLOAD; 151100379Speter PROC_UNLOCK(p); 15282314Speter 15382314Speter for (;;) { 15482314Speter if (vm_page_zero_check()) { 15582314Speter pages += vm_page_zero_idle(); 156104964Sjeff if (pages > idlezero_maxrun || sched_runnable()) { 15782314Speter mtx_lock_spin(&sched_lock); 15883366Sjulian td->td_proc->p_stats->p_ru.ru_nvcsw++; 15982314Speter mi_switch(); 16082314Speter mtx_unlock_spin(&sched_lock); 16182314Speter pages = 0; 16282314Speter } 16382314Speter } else { 16499571Speter tsleep(&zero_state, pri, "pgzero", hz * 300); 16582314Speter pages = 0; 16679265Sdillon } 16779265Sdillon } 16879265Sdillon} 16979265Sdillon 170113070Sdesstatic struct proc *pagezero_proc; 17182314Speterstatic struct kproc_desc pagezero_kp = { 17282314Speter "pagezero", 17382314Speter vm_pagezero, 174113070Sdes &pagezero_proc 17582314Speter}; 17682314SpeterSYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &pagezero_kp) 177