vm_zeroidle.c revision 90538
1/*- 2 * Copyright (c) 1994 John Dyson 3 * Copyright (c) 2001 Matt Dillon 4 * 5 * All rights reserved. Terms for use and redistribution 6 * are covered by the BSD Copyright as found in /usr/src/COPYRIGHT. 7 * 8 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 9 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 10 * $FreeBSD: head/sys/vm/vm_zeroidle.c 90538 2002-02-11 20:37:54Z julian $ 11 */ 12 13#include <sys/param.h> 14#include <sys/systm.h> 15#include <sys/kernel.h> 16#include <sys/proc.h> 17#include <sys/resourcevar.h> 18#include <sys/vmmeter.h> 19#include <sys/lock.h> 20#include <sys/mutex.h> 21#include <sys/sysctl.h> 22#include <sys/kthread.h> 23 24#include <vm/vm.h> 25#include <vm/vm_page.h> 26 27SYSCTL_DECL(_vm_stats_misc); 28 29static int cnt_prezero; 30SYSCTL_INT(_vm_stats_misc, OID_AUTO, 31 cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, ""); 32 33static int idlezero_enable = 0; 34SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, ""); 35TUNABLE_INT("vm.idlezero_enable", &idlezero_enable); 36 37static int idlezero_maxrun = 16; 38SYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, ""); 39TUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun); 40 41/* 42 * Implement the pre-zeroed page mechanism. 43 */ 44 45#define ZIDLE_LO(v) ((v) * 2 / 3) 46#define ZIDLE_HI(v) ((v) * 4 / 5) 47 48static int zero_state; 49 50static int 51vm_page_zero_check(void) 52{ 53 54 if (!idlezero_enable) 55 return 0; 56 /* 57 * Attempt to maintain approximately 1/2 of our free pages in a 58 * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 59 * generally zeroing a page when the system is near steady-state. 60 * Otherwise we might get 'flutter' during disk I/O / IPC or 61 * fast sleeps. We also do not want to be continuously zeroing 62 * pages because doing so may flush our L1 and L2 caches too much. 63 */ 64 if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 65 return 0; 66 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 67 return 0; 68 return 1; 69} 70 71static int 72vm_page_zero_idle(void) 73{ 74 static int free_rover; 75 vm_page_t m; 76 77 mtx_lock(&Giant); 78 zero_state = 0; 79 m = vm_pageq_find(PQ_FREE, free_rover, FALSE); 80 if (m != NULL && (m->flags & PG_ZERO) == 0) { 81 vm_page_queues[m->queue].lcnt--; 82 TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq); 83 m->queue = PQ_NONE; 84 /* maybe drop out of Giant here */ 85 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 86 /* and return here */ 87 vm_page_flag_set(m, PG_ZERO); 88 m->queue = PQ_FREE + m->pc; 89 vm_page_queues[m->queue].lcnt++; 90 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, 91 pageq); 92 ++vm_page_zero_count; 93 ++cnt_prezero; 94 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 95 zero_state = 1; 96 } 97 free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK; 98 mtx_unlock(&Giant); 99 return 1; 100} 101 102 103/* Called by vm_page_free to hint that a new page is available */ 104void 105vm_page_zero_idle_wakeup(void) 106{ 107 108 if (vm_page_zero_check()) 109 wakeup(&zero_state); 110} 111 112static void 113vm_pagezero(void) 114{ 115 struct thread *td = curthread; 116 struct rtprio rtp; 117 int pages = 0; 118 119 rtp.prio = RTP_PRIO_MAX; 120 rtp.type = RTP_PRIO_IDLE; 121 mtx_lock_spin(&sched_lock); 122 rtp_to_pri(&rtp, td->td_ksegrp); 123 mtx_unlock_spin(&sched_lock); 124 125 for (;;) { 126 if (vm_page_zero_check()) { 127 pages += vm_page_zero_idle(); 128 if (pages > idlezero_maxrun) { 129 mtx_lock_spin(&sched_lock); 130 setrunqueue(td); 131 td->td_proc->p_stats->p_ru.ru_nvcsw++; 132 mi_switch(); 133 mtx_unlock_spin(&sched_lock); 134 pages = 0; 135 } 136 } else { 137 tsleep(&zero_state, PPAUSE, "pgzero", hz * 300); 138 pages = 0; 139 } 140 } 141} 142 143static struct proc *pagezero; 144static struct kproc_desc pagezero_kp = { 145 "pagezero", 146 vm_pagezero, 147 &pagezero 148}; 149SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &pagezero_kp) 150