vm_zeroidle.c revision 126588
179265Sdillon/*- 279265Sdillon * Copyright (c) 1994 John Dyson 379265Sdillon * Copyright (c) 2001 Matt Dillon 479265Sdillon * 579265Sdillon * All Rights Reserved. 679265Sdillon * Redistribution and use in source and binary forms, with or without 779265Sdillon * modification, are permitted provided that the following conditions 879265Sdillon * are met: 979265Sdillon * 1. Redistributions of source code must retain the above copyright 1079265Sdillon * notice, this list of conditions and the following disclaimer. 1179265Sdillon * 2. Redistributions in binary form must reproduce the above copyright 12116226Sobrien * notice, this list of conditions and the following disclaimer in the 13116226Sobrien * documentation and/or other materials provided with the distribution. 14116226Sobrien * 4. Neither the name of the University nor the names of its contributors 1579265Sdillon * may be used to endorse or promote products derived from this software 1679265Sdillon * without specific prior written permission. 1782314Speter * 1879265Sdillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 1982314Speter * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 2079265Sdillon * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2182314Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 2279265Sdillon * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23104964Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 2479265Sdillon * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2582314Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2679265Sdillon * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2779265Sdillon * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 2879265Sdillon * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2979265Sdillon * 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 * from: FreeBSD: .../i386/vm_machdep.c,v 1.165 2001/07/04 23:27:04 dillon 3379265Sdillon */ 3479265Sdillon 3579265Sdillon#include <sys/cdefs.h> 3699571Speter__FBSDID("$FreeBSD: head/sys/vm/vm_zeroidle.c 126588 2004-03-04 10:18:17Z bde $"); 3782314Speter 3882314Speter#include <sys/param.h> 3982314Speter#include <sys/systm.h> 4082314Speter#include <sys/kernel.h> 4182314Speter#include <sys/proc.h> 4282314Speter#include <sys/resourcevar.h> 4382314Speter#include <sys/vmmeter.h> 4479265Sdillon#include <sys/lock.h> 4579265Sdillon#include <sys/mutex.h> 4679265Sdillon#include <sys/sched.h> 4779265Sdillon#include <sys/sysctl.h> 4879265Sdillon#include <sys/kthread.h> 4979265Sdillon#include <sys/unistd.h> 5079265Sdillon 5182314Speter#include <vm/vm.h> 5282314Speter#include <vm/vm_page.h> 5382314Speter 5482314SpeterSYSCTL_DECL(_vm_stats_misc); 5579265Sdillon 5679265Sdillonstatic int cnt_prezero; 5782314SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD, 5882314Speter &cnt_prezero, 0, ""); 5979265Sdillon 6079265Sdillonstatic int idlezero_enable = 1; 6179265SdillonSYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, ""); 6279265SdillonTUNABLE_INT("vm.idlezero_enable", &idlezero_enable); 6379265Sdillon 6479265Sdillonstatic int idlezero_maxrun = 16; 6579265SdillonSYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, ""); 6679265SdillonTUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun); 6779265Sdillon 6882314Speter/* 6979265Sdillon * Implement the pre-zeroed page mechanism. 7082314Speter */ 7182314Speter 7282314Speter#define ZIDLE_LO(v) ((v) * 2 / 3) 7379265Sdillon#define ZIDLE_HI(v) ((v) * 4 / 5) 7482314Speter 7582314Speterstatic int zero_state; 7682314Speter 7782314Speterstatic int 7882314Spetervm_page_zero_check(void) 7982314Speter{ 8099625Speter 8182314Speter if (!idlezero_enable) 8282314Speter return (0); 8382314Speter /* 84100193Salc * Attempt to maintain approximately 1/2 of our free pages in a 8599625Speter * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 8699571Speter * generally zeroing a page when the system is near steady-state. 8799625Speter * Otherwise we might get 'flutter' during disk I/O / IPC or 88100331Salc * fast sleeps. We also do not want to be continuously zeroing 89100193Salc * pages because doing so may flush our L1 and L2 caches too much. 9082314Speter */ 9182314Speter if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 9282314Speter return (0); 9382314Speter if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 9482314Speter return (0); 9582314Speter return (1); 9699625Speter} 9782314Speter 9882314Speterstatic int 9982314Spetervm_page_zero_idle(void) 10082314Speter{ 10182314Speter static int free_rover; 10282314Speter vm_page_t m; 10382314Speter 10482314Speter mtx_lock_spin(&vm_page_queue_free_mtx); 10582314Speter zero_state = 0; 10699571Speter m = vm_pageq_find(PQ_FREE, free_rover, FALSE); 10782314Speter if (m != NULL && (m->flags & PG_ZERO) == 0) { 10882314Speter vm_pageq_remove_nowakeup(m); 10982314Speter mtx_unlock_spin(&vm_page_queue_free_mtx); 11082314Speter pmap_zero_page_idle(m); 11182314Speter mtx_lock_spin(&vm_page_queue_free_mtx); 11282314Speter m->flags |= PG_ZERO; 113100379Speter vm_pageq_enqueue(PQ_FREE + m->pc, m); 114100379Speter ++vm_page_zero_count; 11582314Speter ++cnt_prezero; 11682314Speter if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 11799571Speter zero_state = 1; 11882314Speter } 119100379Speter free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK; 120100379Speter mtx_unlock_spin(&vm_page_queue_free_mtx); 12182314Speter return (1); 12282314Speter} 12382756Sjhb 12490538Sjulian/* Called by vm_page_free to hint that a new page is available. */ 12599571Spetervoid 12682756Sjhbvm_page_zero_idle_wakeup(void) 127100379Speter{ 128100379Speter 129100379Speter if (idlezero_enable && vm_page_zero_check()) 13082314Speter wakeup(&zero_state); 13182314Speter} 13282314Speter 13382314Speterstatic void 134104964Sjeffvm_pagezero(void __unused *arg) 13582314Speter{ 13683366Sjulian struct proc *p; 13782314Speter struct rtprio rtp; 13882314Speter struct thread *td; 13982314Speter int pages, pri; 14082314Speter 14182314Speter td = curthread; 14299571Speter p = td->td_proc; 14382314Speter rtp.prio = RTP_PRIO_MAX; 14479265Sdillon rtp.type = RTP_PRIO_IDLE; 14579265Sdillon pages = 0; 14679265Sdillon mtx_lock_spin(&sched_lock); 14779265Sdillon rtp_to_pri(&rtp, td->td_ksegrp); 148113070Sdes pri = td->td_priority; 14982314Speter mtx_unlock_spin(&sched_lock); 15082314Speter 15182314Speter for (;;) { 152113070Sdes if (vm_page_zero_check()) { 15382314Speter pages += vm_page_zero_idle(); 15482314Speter if (pages > idlezero_maxrun || sched_runnable()) { 155 mtx_lock_spin(&sched_lock); 156 mi_switch(SW_VOL); 157 mtx_unlock_spin(&sched_lock); 158 pages = 0; 159 } 160 } else { 161 tsleep(&zero_state, pri, "pgzero", hz * 300); 162 pages = 0; 163 } 164 } 165} 166 167static struct proc *pagezero_proc; 168 169static void 170pagezero_start(void __unused *arg) 171{ 172 int error; 173 174 error = kthread_create(vm_pagezero, NULL, &pagezero_proc, RFSTOPPED, 0, 175 "pagezero"); 176 if (error) 177 panic("pagezero_start: error %d\n", error); 178 /* 179 * We're an idle task, don't count us in the load. 180 */ 181 PROC_LOCK(pagezero_proc); 182 pagezero_proc->p_flag |= P_NOLOAD; 183 PROC_UNLOCK(pagezero_proc); 184 mtx_lock_spin(&sched_lock); 185 setrunqueue(FIRST_THREAD_IN_PROC(pagezero_proc)); 186 mtx_unlock_spin(&sched_lock); 187} 188SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL) 189