vm_zeroidle.c revision 137104
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 137104 2004-10-31 19:32:57Z alc $"); 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> 4482314Speter#include <sys/resourcevar.h> 4579265Sdillon#include <sys/vmmeter.h> 4682314Speter#include <sys/lock.h> 4779265Sdillon#include <sys/mutex.h> 48104964Sjeff#include <sys/sched.h> 4979265Sdillon#include <sys/sysctl.h> 5082314Speter#include <sys/kthread.h> 51125314Sjeff#include <sys/unistd.h> 5279265Sdillon 5379265Sdillon#include <vm/vm.h> 5479265Sdillon#include <vm/vm_page.h> 5579265Sdillon 5679265SdillonSYSCTL_DECL(_vm_stats_misc); 5779265Sdillon 5879265Sdillonstatic int cnt_prezero; 59126588SbdeSYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD, 60126588Sbde &cnt_prezero, 0, ""); 6179265Sdillon 62134461Siedowsestatic int idlezero_enable_default = 1; 63134461SiedowseTUNABLE_INT("vm.idlezero_enable", &idlezero_enable_default); 64134461Siedowse/* Defer setting the enable flag until the kthread is running. */ 65134461Siedowsestatic int idlezero_enable = 0; 6682314SpeterSYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, ""); 6782314Speter 6882314Speterstatic int idlezero_maxrun = 16; 6982314SpeterSYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, ""); 7082314SpeterTUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun); 7182314Speter 7279265Sdillon/* 7379265Sdillon * Implement the pre-zeroed page mechanism. 7479265Sdillon */ 7579265Sdillon 7679265Sdillon#define ZIDLE_LO(v) ((v) * 2 / 3) 7779265Sdillon#define ZIDLE_HI(v) ((v) * 4 / 5) 7879265Sdillon 79137104Salcstatic boolean_t wakeup_needed = FALSE; 8082314Speterstatic int zero_state; 8182314Speter 8282314Speterstatic int 8382314Spetervm_page_zero_check(void) 8479265Sdillon{ 8579265Sdillon 8682314Speter if (!idlezero_enable) 87126588Sbde return (0); 8879265Sdillon /* 8979265Sdillon * Attempt to maintain approximately 1/2 of our free pages in a 9079265Sdillon * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 9179265Sdillon * generally zeroing a page when the system is near steady-state. 9279265Sdillon * Otherwise we might get 'flutter' during disk I/O / IPC or 9379265Sdillon * fast sleeps. We also do not want to be continuously zeroing 9479265Sdillon * pages because doing so may flush our L1 and L2 caches too much. 9579265Sdillon */ 9679265Sdillon if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 97126588Sbde return (0); 9879265Sdillon if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 99126588Sbde return (0); 100126588Sbde return (1); 10182314Speter} 10279265Sdillon 10382314Speterstatic int 10482314Spetervm_page_zero_idle(void) 10582314Speter{ 10682314Speter static int free_rover; 10782314Speter vm_page_t m; 10882314Speter 10999625Speter mtx_lock_spin(&vm_page_queue_free_mtx); 11082314Speter zero_state = 0; 11182314Speter m = vm_pageq_find(PQ_FREE, free_rover, FALSE); 11282314Speter if (m != NULL && (m->flags & PG_ZERO) == 0) { 113100193Salc vm_pageq_remove_nowakeup(m); 11499625Speter mtx_unlock_spin(&vm_page_queue_free_mtx); 11599571Speter pmap_zero_page_idle(m); 11699625Speter mtx_lock_spin(&vm_page_queue_free_mtx); 117100331Salc m->flags |= PG_ZERO; 118100193Salc vm_pageq_enqueue(PQ_FREE + m->pc, m); 11982314Speter ++vm_page_zero_count; 12082314Speter ++cnt_prezero; 12182314Speter if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 12282314Speter zero_state = 1; 12382314Speter } 12482314Speter free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK; 12599625Speter mtx_unlock_spin(&vm_page_queue_free_mtx); 126126588Sbde return (1); 12782314Speter} 12882314Speter 129126588Sbde/* Called by vm_page_free to hint that a new page is available. */ 13082314Spetervoid 13182314Spetervm_page_zero_idle_wakeup(void) 13282314Speter{ 13382314Speter 134137104Salc mtx_assert(&vm_page_queue_mtx, MA_OWNED); 135137104Salc if (wakeup_needed && vm_page_zero_check()) { 136137104Salc wakeup_needed = FALSE; 13782314Speter wakeup(&zero_state); 138137104Salc } 13982314Speter} 14082314Speter 14182314Speterstatic void 142125314Sjeffvm_pagezero(void __unused *arg) 14382314Speter{ 14482314Speter struct rtprio rtp; 145126588Sbde struct thread *td; 146126588Sbde int pages, pri; 14782314Speter 148100379Speter td = curthread; 14982314Speter rtp.prio = RTP_PRIO_MAX; 15082314Speter rtp.type = RTP_PRIO_IDLE; 151126588Sbde pages = 0; 15282756Sjhb mtx_lock_spin(&sched_lock); 15390538Sjulian rtp_to_pri(&rtp, td->td_ksegrp); 15499571Speter pri = td->td_priority; 15582756Sjhb mtx_unlock_spin(&sched_lock); 156134461Siedowse idlezero_enable = idlezero_enable_default; 15782314Speter 15882314Speter for (;;) { 15982314Speter if (vm_page_zero_check()) { 16082314Speter pages += vm_page_zero_idle(); 161131481Sjhb#ifndef PREEMPTION 162104964Sjeff if (pages > idlezero_maxrun || sched_runnable()) { 16382314Speter mtx_lock_spin(&sched_lock); 164131473Sjhb mi_switch(SW_VOL, NULL); 16582314Speter mtx_unlock_spin(&sched_lock); 16682314Speter pages = 0; 16782314Speter } 168131481Sjhb#endif 16982314Speter } else { 170137104Salc vm_page_lock_queues(); 171137104Salc wakeup_needed = TRUE; 172137104Salc msleep(&zero_state, &vm_page_queue_mtx, PDROP | pri, 173137104Salc "pgzero", hz * 300); 17482314Speter pages = 0; 17579265Sdillon } 17679265Sdillon } 17779265Sdillon} 17879265Sdillon 179113070Sdesstatic struct proc *pagezero_proc; 180125314Sjeff 181125314Sjeffstatic void 182125314Sjeffpagezero_start(void __unused *arg) 183125314Sjeff{ 184125314Sjeff int error; 185125314Sjeff 186125314Sjeff error = kthread_create(vm_pagezero, NULL, &pagezero_proc, RFSTOPPED, 0, 187125314Sjeff "pagezero"); 188125314Sjeff if (error) 189125314Sjeff panic("pagezero_start: error %d\n", error); 190125314Sjeff /* 191125314Sjeff * We're an idle task, don't count us in the load. 192125314Sjeff */ 193125314Sjeff PROC_LOCK(pagezero_proc); 194125314Sjeff pagezero_proc->p_flag |= P_NOLOAD; 195125314Sjeff PROC_UNLOCK(pagezero_proc); 196125314Sjeff mtx_lock_spin(&sched_lock); 197134586Sjulian setrunqueue(FIRST_THREAD_IN_PROC(pagezero_proc), SRQ_BORING); 198125314Sjeff mtx_unlock_spin(&sched_lock); 199125314Sjeff} 200125314SjeffSYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL) 201