The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_zeroidle.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1994 John Dyson
    3  * Copyright (c) 2001 Matt Dillon
    4  *
    5  * All Rights Reserved.
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 4. Neither the name of the University nor the names of its contributors
   15  *    may be used to endorse or promote products derived from this software
   16  *    without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   29  *
   30  *      from: @(#)vm_machdep.c  7.3 (Berkeley) 5/13/91
   31  *      Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
   32  * from: FreeBSD: .../i386/vm_machdep.c,v 1.165 2001/07/04 23:27:04 dillon
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/5.3/sys/vm/vm_zeroidle.c 135018 2004-09-10 00:04:18Z scottl $");
   37 
   38 #include <opt_sched.h>
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <sys/proc.h>
   44 #include <sys/resourcevar.h>
   45 #include <sys/vmmeter.h>
   46 #include <sys/lock.h>
   47 #include <sys/mutex.h>
   48 #include <sys/sched.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/kthread.h>
   51 #include <sys/unistd.h>
   52 
   53 #include <vm/vm.h>
   54 #include <vm/vm_page.h>
   55 
   56 SYSCTL_DECL(_vm_stats_misc);
   57 
   58 static int cnt_prezero;
   59 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
   60     &cnt_prezero, 0, "");
   61 
   62 static int idlezero_enable_default = 1;
   63 TUNABLE_INT("vm.idlezero_enable", &idlezero_enable_default);
   64 /* Defer setting the enable flag until the kthread is running. */
   65 static int idlezero_enable = 0;
   66 SYSCTL_INT(_vm, OID_AUTO, idlezero_enable, CTLFLAG_RW, &idlezero_enable, 0, "");
   67 
   68 static int idlezero_maxrun = 16;
   69 SYSCTL_INT(_vm, OID_AUTO, idlezero_maxrun, CTLFLAG_RW, &idlezero_maxrun, 0, "");
   70 TUNABLE_INT("vm.idlezero_maxrun", &idlezero_maxrun);
   71 
   72 /*
   73  * Implement the pre-zeroed page mechanism.
   74  */
   75 
   76 #define ZIDLE_LO(v)     ((v) * 2 / 3)
   77 #define ZIDLE_HI(v)     ((v) * 4 / 5)
   78 
   79 static int zero_state;
   80 
   81 static int
   82 vm_page_zero_check(void)
   83 {
   84 
   85         if (!idlezero_enable)
   86                 return (0);
   87         /*
   88          * Attempt to maintain approximately 1/2 of our free pages in a
   89          * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
   90          * generally zeroing a page when the system is near steady-state.
   91          * Otherwise we might get 'flutter' during disk I/O / IPC or 
   92          * fast sleeps.  We also do not want to be continuously zeroing
   93          * pages because doing so may flush our L1 and L2 caches too much.
   94          */
   95         if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
   96                 return (0);
   97         if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
   98                 return (0);
   99         return (1);
  100 }
  101 
  102 static int
  103 vm_page_zero_idle(void)
  104 {
  105         static int free_rover;
  106         vm_page_t m;
  107 
  108         mtx_lock_spin(&vm_page_queue_free_mtx);
  109         zero_state = 0;
  110         m = vm_pageq_find(PQ_FREE, free_rover, FALSE);
  111         if (m != NULL && (m->flags & PG_ZERO) == 0) {
  112                 vm_pageq_remove_nowakeup(m);
  113                 mtx_unlock_spin(&vm_page_queue_free_mtx);
  114                 pmap_zero_page_idle(m);
  115                 mtx_lock_spin(&vm_page_queue_free_mtx);
  116                 m->flags |= PG_ZERO;
  117                 vm_pageq_enqueue(PQ_FREE + m->pc, m);
  118                 ++vm_page_zero_count;
  119                 ++cnt_prezero;
  120                 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
  121                         zero_state = 1;
  122         }
  123         free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
  124         mtx_unlock_spin(&vm_page_queue_free_mtx);
  125         return (1);
  126 }
  127 
  128 /* Called by vm_page_free to hint that a new page is available. */
  129 void
  130 vm_page_zero_idle_wakeup(void)
  131 {
  132 
  133         if (idlezero_enable && vm_page_zero_check())
  134                 wakeup(&zero_state);
  135 }
  136 
  137 static void
  138 vm_pagezero(void __unused *arg)
  139 {
  140         struct proc *p;
  141         struct rtprio rtp;
  142         struct thread *td;
  143         int pages, pri;
  144 
  145         td = curthread;
  146         p = td->td_proc;
  147         rtp.prio = RTP_PRIO_MAX;
  148         rtp.type = RTP_PRIO_IDLE;
  149         pages = 0;
  150         mtx_lock_spin(&sched_lock);
  151         rtp_to_pri(&rtp, td->td_ksegrp);
  152         pri = td->td_priority;
  153         mtx_unlock_spin(&sched_lock);
  154         idlezero_enable = idlezero_enable_default;
  155 
  156         for (;;) {
  157                 if (vm_page_zero_check()) {
  158                         pages += vm_page_zero_idle();
  159 #ifndef PREEMPTION
  160                         if (pages > idlezero_maxrun || sched_runnable()) {
  161                                 mtx_lock_spin(&sched_lock);
  162                                 mi_switch(SW_VOL, NULL);
  163                                 mtx_unlock_spin(&sched_lock);
  164                                 pages = 0;
  165                         }
  166 #endif
  167                 } else {
  168                         tsleep(&zero_state, pri, "pgzero", hz * 300);
  169                         pages = 0;
  170                 }
  171         }
  172 }
  173 
  174 static struct proc *pagezero_proc;
  175 
  176 static void
  177 pagezero_start(void __unused *arg)
  178 {
  179         int error;
  180 
  181         error = kthread_create(vm_pagezero, NULL, &pagezero_proc, RFSTOPPED, 0,
  182             "pagezero");
  183         if (error)
  184                 panic("pagezero_start: error %d\n", error);
  185         /*
  186          * We're an idle task, don't count us in the load.
  187          */
  188         PROC_LOCK(pagezero_proc);
  189         pagezero_proc->p_flag |= P_NOLOAD;
  190         PROC_UNLOCK(pagezero_proc);
  191         mtx_lock_spin(&sched_lock);
  192         setrunqueue(FIRST_THREAD_IN_PROC(pagezero_proc), SRQ_BORING);
  193         mtx_unlock_spin(&sched_lock);
  194 }
  195 SYSINIT(pagezero, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, pagezero_start, NULL)

Cache object: 0bee0f4fb07474d0c6f1e9d89c452702


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.