]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/vm/vm_pageq.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / sys / vm / vm_pageq.c
1 /*-
2  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  * 1. Redistributions of source code must retain the above copyright
7  *    notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright
9  *    notice, this list of conditions and the following disclaimer in the
10  *    documentation and/or other materials provided with the distribution.
11  * 4. Neither the name of the University nor the names of its contributors
12  *    may be used to endorse or promote products derived from this software
13  *    without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/vmmeter.h>
38 #include <sys/vnode.h>
39
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 #include <vm/vm_kern.h>
43 #include <vm/vm_object.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_pageout.h>
46 #include <vm/vm_pager.h>
47 #include <vm/vm_extern.h>
48
49 struct vpgqueues vm_page_queues[PQ_COUNT];
50
51 void
52 vm_pageq_init(void) 
53 {
54         int i;
55
56         for (i = 0; i < PQ_L2_SIZE; i++) {
57                 vm_page_queues[PQ_FREE+i].cnt = &cnt.v_free_count;
58         }
59         for (i = 0; i < PQ_L2_SIZE; i++) {
60                 vm_page_queues[PQ_CACHE+i].cnt = &cnt.v_cache_count;
61         }
62         vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
63         vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
64         vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
65
66         for (i = 0; i < PQ_COUNT; i++) {
67                 TAILQ_INIT(&vm_page_queues[i].pl);
68         }
69 }
70
71 void
72 vm_pageq_requeue(vm_page_t m)
73 {
74         int queue = m->queue;
75         struct vpgqueues *vpq;
76
77         if (queue != PQ_NONE) {
78                 vpq = &vm_page_queues[queue];
79                 TAILQ_REMOVE(&vpq->pl, m, pageq);
80                 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
81         }
82 }
83
84 /*
85  *      vm_pageq_enqueue:
86  */
87 void
88 vm_pageq_enqueue(int queue, vm_page_t m)
89 {
90         struct vpgqueues *vpq;
91
92         vpq = &vm_page_queues[queue];
93         m->queue = queue;
94         TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
95         ++*vpq->cnt;
96         ++vpq->lcnt;
97 }
98
99 /*
100  *      vm_add_new_page:
101  *
102  *      Add a new page to the freelist for use by the system.
103  */
104 vm_page_t
105 vm_pageq_add_new_page(vm_paddr_t pa)
106 {
107         vm_page_t m;
108
109         GIANT_REQUIRED;
110
111         ++cnt.v_page_count;
112         m = PHYS_TO_VM_PAGE(pa);
113         m->phys_addr = pa;
114         m->flags = 0;
115         m->pc = (pa >> PAGE_SHIFT) & PQ_L2_MASK;
116         pmap_page_init(m);
117         vm_pageq_enqueue(m->pc + PQ_FREE, m);
118         return (m);
119 }
120
121 /*
122  * vm_pageq_remove_nowakeup:
123  *
124  *      vm_page_unqueue() without any wakeup
125  *
126  *      The queue containing the given page must be locked.
127  *      This routine may not block.
128  */
129 void
130 vm_pageq_remove_nowakeup(vm_page_t m)
131 {
132         int queue = m->queue;
133         struct vpgqueues *pq;
134         if (queue != PQ_NONE) {
135                 pq = &vm_page_queues[queue];
136                 m->queue = PQ_NONE;
137                 TAILQ_REMOVE(&pq->pl, m, pageq);
138                 (*pq->cnt)--;
139                 pq->lcnt--;
140         }
141 }
142
143 /*
144  * vm_pageq_remove:
145  *
146  *      Remove a page from its queue.
147  *
148  *      The queue containing the given page must be locked.
149  *      This routine may not block.
150  */
151 void
152 vm_pageq_remove(vm_page_t m)
153 {
154         int queue = m->queue;
155         struct vpgqueues *pq;
156
157         if (queue != PQ_NONE) {
158                 m->queue = PQ_NONE;
159                 pq = &vm_page_queues[queue];
160                 TAILQ_REMOVE(&pq->pl, m, pageq);
161                 (*pq->cnt)--;
162                 pq->lcnt--;
163                 if ((queue - m->pc) == PQ_CACHE) {
164                         if (vm_paging_needed())
165                                 pagedaemon_wakeup();
166                 }
167         }
168 }
169
170 #if PQ_L2_SIZE > 1
171
172 /*
173  *      vm_pageq_find:
174  *
175  *      Find a page on the specified queue with color optimization.
176  *
177  *      The page coloring optimization attempts to locate a page
178  *      that does not overload other nearby pages in the object in
179  *      the cpu's L2 cache.  We need this optimization because cpu
180  *      caches tend to be physical caches, while object spaces tend 
181  *      to be virtual.
182  *
183  *      The specified queue must be locked.
184  *      This routine may not block.
185  *
186  *      This routine may only be called from the vm_pageq_find()
187  *      function in this file.
188  */
189 static __inline vm_page_t
190 _vm_pageq_find(int basequeue, int index)
191 {
192         int i;
193         vm_page_t m = NULL;
194         struct vpgqueues *pq;
195
196         pq = &vm_page_queues[basequeue];
197
198         /*
199          * Note that for the first loop, index+i and index-i wind up at the
200          * same place.  Even though this is not totally optimal, we've already
201          * blown it by missing the cache case so we do not care.
202          */
203         for (i = PQ_L2_SIZE / 2; i > 0; --i) {
204                 if ((m = TAILQ_FIRST(&pq[(index + i) & PQ_L2_MASK].pl)) != NULL)
205                         break;
206
207                 if ((m = TAILQ_FIRST(&pq[(index - i) & PQ_L2_MASK].pl)) != NULL)
208                         break;
209         }
210         return (m);
211 }
212 #endif          /* PQ_L2_SIZE > 1 */
213
214 vm_page_t
215 vm_pageq_find(int basequeue, int index, boolean_t prefer_zero)
216 {
217         vm_page_t m;
218
219 #if PQ_L2_SIZE > 1
220         if (prefer_zero) {
221                 m = TAILQ_LAST(&vm_page_queues[basequeue+index].pl, pglist);
222         } else {
223                 m = TAILQ_FIRST(&vm_page_queues[basequeue+index].pl);
224         }
225         if (m == NULL) {
226                 m = _vm_pageq_find(basequeue, index);
227         }
228 #else
229         if (prefer_zero) {
230                 m = TAILQ_LAST(&vm_page_queues[basequeue].pl, pglist);
231         } else {
232                 m = TAILQ_FIRST(&vm_page_queues[basequeue].pl);
233         }
234 #endif
235         return (m);
236 }
237