2003-08-12 23:24:05 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2001-07-04 23:27:09 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 23:50:51 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-01-24 19:24:54 +00:00
|
|
|
#include "opt_vmpage.h"
|
|
|
|
|
2001-07-04 23:27:09 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2005-12-31 14:39:20 +00:00
|
|
|
#include <sys/linker_set.h>
|
2001-07-04 23:27:09 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
2005-12-31 14:39:20 +00:00
|
|
|
#include <sys/sysctl.h>
|
2001-07-04 23:27:09 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vmmeter.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/vm_kern.h>
|
|
|
|
#include <vm/vm_object.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
#include <vm/vm_pageout.h>
|
|
|
|
#include <vm/vm_pager.h>
|
|
|
|
#include <vm/vm_extern.h>
|
|
|
|
|
2005-12-31 14:39:20 +00:00
|
|
|
static void vm_coloring_init(void);
|
|
|
|
void setPQL2(int *const size, int *const ways);
|
|
|
|
|
|
|
|
struct vpgqueues vm_page_queues[PQ_MAXCOUNT];
|
|
|
|
struct pq_coloring page_queue_coloring;
|
|
|
|
|
|
|
|
static int pq_cachesize = 0; /* size of the cache in KB */
|
|
|
|
static int pq_cachenways = 0; /* associativity of the cache */
|
|
|
|
|
|
|
|
SYSCTL_DECL(_vm_stats);
|
|
|
|
SYSCTL_NODE(_vm_stats, OID_AUTO, pagequeue, CTLFLAG_RW, 0, "VM meter stats");
|
|
|
|
SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, page_colors, CTLFLAG_RD,
|
|
|
|
&(PQ_NUMCOLORS), 0, "Number of colors in the page queue");
|
|
|
|
SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, cachesize, CTLFLAG_RD,
|
|
|
|
&pq_cachesize, 0, "Size of the processor cache in KB");
|
|
|
|
SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, cachenways, CTLFLAG_RD,
|
|
|
|
&pq_cachenways, 0, "Associativity of the processor cache");
|
|
|
|
SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, prime1, CTLFLAG_RD,
|
|
|
|
&(PQ_PRIME1), 0, "Cache tuning value");
|
|
|
|
SYSCTL_INT(_vm_stats_pagequeue, OID_AUTO, prime2, CTLFLAG_RD,
|
|
|
|
&(PQ_PRIME2), 0, "Cache tuning value");
|
|
|
|
|
|
|
|
static void
|
|
|
|
vm_coloring_init(void)
|
|
|
|
{
|
|
|
|
#ifdef PQ_NOOPT
|
|
|
|
PQ_NUMCOLORS = PQ_PRIME1 = PQ_PRIME2 = 1;
|
|
|
|
#else
|
|
|
|
|
|
|
|
setPQL2(&pq_cachesize, &pq_cachenways);
|
|
|
|
|
2006-01-04 19:19:42 +00:00
|
|
|
CTASSERT(PAGE_SIZE/1024 > 0);
|
|
|
|
|
|
|
|
if (pq_cachesize > 0 && pq_cachenways > 0)
|
2005-12-31 14:39:20 +00:00
|
|
|
PQ_NUMCOLORS = pq_cachesize / (PAGE_SIZE/1024) / \
|
|
|
|
pq_cachenways;
|
|
|
|
else
|
|
|
|
PQ_NUMCOLORS = 32;
|
|
|
|
|
|
|
|
if (PQ_MAXCOLORS < PQ_NUMCOLORS) {
|
|
|
|
printf("VM-PQ color limit (PQ_MAXCOLORS=%u) exceeded (%u), see vm_page.h", PQ_MAXCOLORS, PQ_NUMCOLORS);
|
|
|
|
PQ_NUMCOLORS = PQ_MAXCOLORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PQ_NUMCOLORS >= 128) {
|
|
|
|
PQ_PRIME1 = 31;
|
|
|
|
PQ_PRIME2 = 23;
|
|
|
|
} else if (PQ_NUMCOLORS >= 64) {
|
|
|
|
PQ_PRIME1 = 13;
|
|
|
|
PQ_PRIME2 = 7;
|
|
|
|
} else if (PQ_NUMCOLORS >= 32) {
|
|
|
|
PQ_PRIME1 = 9;
|
|
|
|
PQ_PRIME2 = 5;
|
|
|
|
} else if (PQ_NUMCOLORS >= 16) {
|
|
|
|
PQ_PRIME1 = 5;
|
|
|
|
PQ_PRIME2 = 3;
|
|
|
|
} else
|
|
|
|
PQ_NUMCOLORS = PQ_PRIME1 = PQ_PRIME2 = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PQ_CACHE represents a
|
|
|
|
* PQ_NUMCOLORS consecutive queue.
|
|
|
|
*/
|
|
|
|
PQ_COLORMASK = PQ_NUMCOLORS - 1;
|
|
|
|
PQ_INACTIVE = 1 + PQ_NUMCOLORS;
|
|
|
|
PQ_ACTIVE = 2 + PQ_NUMCOLORS;
|
|
|
|
PQ_CACHE = 3 + PQ_NUMCOLORS;
|
|
|
|
PQ_HOLD = 3 + 2 * PQ_NUMCOLORS;
|
|
|
|
PQ_COUNT = 4 + 2 * PQ_NUMCOLORS;
|
|
|
|
PQ_MAXLENGTH = PQ_NUMCOLORS / 3 + PQ_PRIME1;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* XXX: is it possible to allocate vm_page_queues[PQ_COUNT] here? */
|
|
|
|
#error XXX: vm_page_queues = malloc(PQ_COUNT * sizeof(struct vpgqueues));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (bootverbose)
|
|
|
|
if (PQ_NUMCOLORS > 1)
|
|
|
|
printf("Using %d colors for the VM-PQ tuning (%d, %d)\n",
|
|
|
|
PQ_NUMCOLORS, pq_cachesize, pq_cachenways);
|
|
|
|
}
|
2001-07-04 23:27:09 +00:00
|
|
|
|
|
|
|
void
|
2005-12-31 14:39:20 +00:00
|
|
|
vm_pageq_init(void)
|
2001-07-04 23:27:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-12-31 14:39:20 +00:00
|
|
|
vm_coloring_init();
|
|
|
|
|
|
|
|
for (i = 0; i < PQ_NUMCOLORS; ++i) {
|
2001-07-04 23:27:09 +00:00
|
|
|
vm_page_queues[PQ_FREE+i].cnt = &cnt.v_free_count;
|
|
|
|
}
|
2005-12-31 14:39:20 +00:00
|
|
|
for (i = 0; i < PQ_NUMCOLORS; ++i) {
|
|
|
|
vm_page_queues[PQ_CACHE + i].cnt = &cnt.v_cache_count;
|
2001-07-04 23:27:09 +00:00
|
|
|
}
|
|
|
|
vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
|
|
|
|
vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
|
2002-02-19 23:19:30 +00:00
|
|
|
vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
|
2001-07-04 23:27:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < PQ_COUNT; i++) {
|
|
|
|
TAILQ_INIT(&vm_page_queues[i].pl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vm_pageq_requeue(vm_page_t m)
|
|
|
|
{
|
2005-12-31 14:39:20 +00:00
|
|
|
int queue = VM_PAGE_GETQUEUE(m);
|
2001-07-04 23:27:09 +00:00
|
|
|
struct vpgqueues *vpq;
|
|
|
|
|
2003-06-26 03:14:40 +00:00
|
|
|
if (queue != PQ_NONE) {
|
|
|
|
vpq = &vm_page_queues[queue];
|
|
|
|
TAILQ_REMOVE(&vpq->pl, m, pageq);
|
|
|
|
TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
|
|
|
|
}
|
2001-07-04 23:27:09 +00:00
|
|
|
}
|
|
|
|
|
2002-03-04 18:55:26 +00:00
|
|
|
/*
|
|
|
|
* vm_pageq_enqueue:
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vm_pageq_enqueue(int queue, vm_page_t m)
|
|
|
|
{
|
|
|
|
struct vpgqueues *vpq;
|
|
|
|
|
|
|
|
vpq = &vm_page_queues[queue];
|
2005-12-31 14:39:20 +00:00
|
|
|
VM_PAGE_SETQUEUE2(m, queue);
|
2002-03-04 18:55:26 +00:00
|
|
|
TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
|
|
|
|
++*vpq->cnt;
|
|
|
|
++vpq->lcnt;
|
|
|
|
}
|
|
|
|
|
2001-07-04 23:27:09 +00:00
|
|
|
/*
|
|
|
|
* vm_add_new_page:
|
|
|
|
*
|
|
|
|
* Add a new page to the freelist for use by the system.
|
|
|
|
*/
|
|
|
|
vm_page_t
|
2003-03-25 00:07:06 +00:00
|
|
|
vm_pageq_add_new_page(vm_paddr_t pa)
|
2001-07-04 23:27:09 +00:00
|
|
|
{
|
2005-04-15 21:45:02 +00:00
|
|
|
vm_paddr_t bad;
|
2001-07-04 23:27:09 +00:00
|
|
|
vm_page_t m;
|
2005-04-15 21:45:02 +00:00
|
|
|
char *cp, *list, *pos;
|
2001-07-04 23:27:09 +00:00
|
|
|
|
|
|
|
GIANT_REQUIRED;
|
|
|
|
|
2005-04-15 21:45:02 +00:00
|
|
|
/*
|
|
|
|
* See if a physical address in this page has been listed
|
|
|
|
* in the blacklist tunable. Entries in the tunable are
|
|
|
|
* separated by spaces or commas. If an invalid integer is
|
|
|
|
* encountered then the rest of the string is skipped.
|
|
|
|
*/
|
|
|
|
if (testenv("vm.blacklist")) {
|
|
|
|
list = getenv("vm.blacklist");
|
|
|
|
for (pos = list; *pos != '\0'; pos = cp) {
|
|
|
|
bad = strtoq(pos, &cp, 0);
|
|
|
|
if (*cp != '\0') {
|
|
|
|
if (*cp == ' ' || *cp == ',') {
|
|
|
|
cp++;
|
|
|
|
if (cp == pos)
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pa == trunc_page(bad)) {
|
2005-04-16 12:37:16 +00:00
|
|
|
printf("Skipping page with pa 0x%jx\n",
|
|
|
|
(uintmax_t)pa);
|
2005-04-15 21:45:02 +00:00
|
|
|
freeenv(list);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeenv(list);
|
|
|
|
}
|
|
|
|
|
2001-07-04 23:27:09 +00:00
|
|
|
++cnt.v_page_count;
|
|
|
|
m = PHYS_TO_VM_PAGE(pa);
|
|
|
|
m->phys_addr = pa;
|
|
|
|
m->flags = 0;
|
2005-12-31 14:39:20 +00:00
|
|
|
m->pc = (pa >> PAGE_SHIFT) & PQ_COLORMASK;
|
2005-06-10 03:33:36 +00:00
|
|
|
pmap_page_init(m);
|
2002-03-04 18:55:26 +00:00
|
|
|
vm_pageq_enqueue(m->pc + PQ_FREE, m);
|
2001-07-04 23:27:09 +00:00
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vm_pageq_remove_nowakeup:
|
|
|
|
*
|
|
|
|
* vm_page_unqueue() without any wakeup
|
|
|
|
*
|
2005-05-28 17:56:18 +00:00
|
|
|
* The queue containing the given page must be locked.
|
2001-07-04 23:27:09 +00:00
|
|
|
* This routine may not block.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vm_pageq_remove_nowakeup(vm_page_t m)
|
|
|
|
{
|
2005-12-31 14:39:20 +00:00
|
|
|
int queue = VM_PAGE_GETQUEUE(m);
|
2001-07-04 23:27:09 +00:00
|
|
|
struct vpgqueues *pq;
|
|
|
|
if (queue != PQ_NONE) {
|
|
|
|
pq = &vm_page_queues[queue];
|
2005-12-31 14:39:20 +00:00
|
|
|
VM_PAGE_SETQUEUE2(m, PQ_NONE);
|
2001-07-04 23:27:09 +00:00
|
|
|
TAILQ_REMOVE(&pq->pl, m, pageq);
|
|
|
|
(*pq->cnt)--;
|
|
|
|
pq->lcnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vm_pageq_remove:
|
|
|
|
*
|
|
|
|
* Remove a page from its queue.
|
|
|
|
*
|
2005-05-28 17:56:18 +00:00
|
|
|
* The queue containing the given page must be locked.
|
2001-07-04 23:27:09 +00:00
|
|
|
* This routine may not block.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vm_pageq_remove(vm_page_t m)
|
|
|
|
{
|
2005-12-31 14:39:20 +00:00
|
|
|
int queue = VM_PAGE_GETQUEUE(m);
|
2001-07-04 23:27:09 +00:00
|
|
|
struct vpgqueues *pq;
|
|
|
|
|
|
|
|
if (queue != PQ_NONE) {
|
2005-12-31 14:39:20 +00:00
|
|
|
VM_PAGE_SETQUEUE2(m, PQ_NONE);
|
2001-07-04 23:27:09 +00:00
|
|
|
pq = &vm_page_queues[queue];
|
|
|
|
TAILQ_REMOVE(&pq->pl, m, pageq);
|
|
|
|
(*pq->cnt)--;
|
|
|
|
pq->lcnt--;
|
2006-01-27 07:28:51 +00:00
|
|
|
if (VM_PAGE_RESOLVEQUEUE(m, queue) == PQ_CACHE) {
|
2001-07-04 23:27:09 +00:00
|
|
|
if (vm_paging_needed())
|
|
|
|
pagedaemon_wakeup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-31 14:39:20 +00:00
|
|
|
#ifndef PQ_NOOPT
|
2001-07-04 23:27:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* vm_pageq_find:
|
|
|
|
*
|
|
|
|
* Find a page on the specified queue with color optimization.
|
|
|
|
*
|
|
|
|
* The page coloring optimization attempts to locate a page
|
|
|
|
* that does not overload other nearby pages in the object in
|
2004-06-07 19:33:05 +00:00
|
|
|
* the cpu's L2 cache. We need this optimization because cpu
|
|
|
|
* caches tend to be physical caches, while object spaces tend
|
2001-07-04 23:27:09 +00:00
|
|
|
* to be virtual.
|
|
|
|
*
|
2005-05-28 17:56:18 +00:00
|
|
|
* The specified queue must be locked.
|
2001-07-04 23:27:09 +00:00
|
|
|
* This routine may not block.
|
|
|
|
*
|
2004-05-30 00:42:38 +00:00
|
|
|
* This routine may only be called from the vm_pageq_find()
|
|
|
|
* function in this file.
|
2001-07-04 23:27:09 +00:00
|
|
|
*/
|
|
|
|
static __inline vm_page_t
|
|
|
|
_vm_pageq_find(int basequeue, int index)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
vm_page_t m = NULL;
|
|
|
|
struct vpgqueues *pq;
|
|
|
|
|
|
|
|
pq = &vm_page_queues[basequeue];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that for the first loop, index+i and index-i wind up at the
|
|
|
|
* same place. Even though this is not totally optimal, we've already
|
|
|
|
* blown it by missing the cache case so we do not care.
|
|
|
|
*/
|
2005-12-31 14:39:20 +00:00
|
|
|
for (i = PQ_NUMCOLORS / 2; i > 0; --i) {
|
|
|
|
if ((m = TAILQ_FIRST(&pq[(index + i) & PQ_COLORMASK].pl)) \
|
|
|
|
!= NULL)
|
2001-07-04 23:27:09 +00:00
|
|
|
break;
|
|
|
|
|
2005-12-31 14:39:20 +00:00
|
|
|
if ((m = TAILQ_FIRST(&pq[(index - i) & PQ_COLORMASK].pl)) \
|
|
|
|
!= NULL)
|
2001-07-04 23:27:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-03-10 21:52:48 +00:00
|
|
|
return (m);
|
2001-07-04 23:27:09 +00:00
|
|
|
}
|
2005-12-31 14:39:20 +00:00
|
|
|
#endif /* PQ_NOOPT */
|
2001-07-04 23:27:09 +00:00
|
|
|
|
|
|
|
vm_page_t
|
|
|
|
vm_pageq_find(int basequeue, int index, boolean_t prefer_zero)
|
|
|
|
{
|
|
|
|
vm_page_t m;
|
|
|
|
|
2005-12-31 14:39:20 +00:00
|
|
|
#ifndef PQ_NOOPT
|
|
|
|
if (PQ_NUMCOLORS > 1) {
|
|
|
|
if (prefer_zero) {
|
|
|
|
m = TAILQ_LAST(&vm_page_queues[basequeue+index].pl, \
|
|
|
|
pglist);
|
|
|
|
} else {
|
|
|
|
m = TAILQ_FIRST(&vm_page_queues[basequeue+index].pl);
|
|
|
|
}
|
|
|
|
if (m == NULL) {
|
|
|
|
m = _vm_pageq_find(basequeue, index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
if (prefer_zero) {
|
|
|
|
m = TAILQ_LAST(&vm_page_queues[basequeue].pl, pglist);
|
|
|
|
} else {
|
|
|
|
m = TAILQ_FIRST(&vm_page_queues[basequeue].pl);
|
|
|
|
}
|
|
|
|
#ifndef PQ_NOOPT
|
2001-07-04 23:27:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-03-10 21:52:48 +00:00
|
|
|
return (m);
|
2001-07-04 23:27:09 +00:00
|
|
|
}
|
|
|
|
|