version 1.137.2.5, 2007/12/26 21:39:42 |
version 1.160.2.1, 2008/06/23 04:31:51 |
|
|
/* $NetBSD$ */ |
/* $NetBSD$ */ |
|
|
/*- |
/*- |
* Copyright (c) 1997, 1999, 2000, 2002, 2007 The NetBSD Foundation, Inc. |
* Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008 The NetBSD Foundation, Inc. |
* All rights reserved. |
* All rights reserved. |
* |
* |
* This code is derived from software contributed to The NetBSD Foundation |
* This code is derived from software contributed to The NetBSD Foundation |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
* 2. Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
* 3. All advertising materials mentioning features or use of this software |
|
* must display the following acknowledgement: |
|
* This product includes software developed by the NetBSD |
|
* Foundation, Inc. and its contributors. |
|
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
Line 52 __KERNEL_RCSID(0, "$NetBSD$"); |
|
Line 45 __KERNEL_RCSID(0, "$NetBSD$"); |
|
#include <sys/errno.h> |
#include <sys/errno.h> |
#include <sys/kernel.h> |
#include <sys/kernel.h> |
#include <sys/malloc.h> |
#include <sys/malloc.h> |
#include <sys/lock.h> |
|
#include <sys/pool.h> |
#include <sys/pool.h> |
#include <sys/syslog.h> |
#include <sys/syslog.h> |
#include <sys/debug.h> |
#include <sys/debug.h> |
Line 123 struct pool_item_header { |
|
Line 115 struct pool_item_header { |
|
SPLAY_ENTRY(pool_item_header) |
SPLAY_ENTRY(pool_item_header) |
ph_node; /* Off-page page headers */ |
ph_node; /* Off-page page headers */ |
void * ph_page; /* this page's address */ |
void * ph_page; /* this page's address */ |
struct timeval ph_time; /* last referenced */ |
uint32_t ph_time; /* last referenced */ |
uint16_t ph_nmissing; /* # of chunks in use */ |
uint16_t ph_nmissing; /* # of chunks in use */ |
uint16_t ph_off; /* start offset in page */ |
uint16_t ph_off; /* start offset in page */ |
union { |
union { |
Line 613 pool_subsystem_init(void) |
|
Line 605 pool_subsystem_init(void) |
|
pa_reclaim_register(pa); |
pa_reclaim_register(pa); |
} |
} |
|
|
pool_init(&cache_pool, sizeof(struct pool_cache), CACHE_LINE_SIZE, |
pool_init(&cache_pool, sizeof(struct pool_cache), coherency_unit, |
0, 0, "pcache", &pool_allocator_nointr, IPL_NONE); |
0, 0, "pcache", &pool_allocator_nointr, IPL_NONE); |
|
|
pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), CACHE_LINE_SIZE, |
pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), coherency_unit, |
0, 0, "pcachecpu", &pool_allocator_nointr, IPL_NONE); |
0, 0, "pcachecpu", &pool_allocator_nointr, IPL_NONE); |
} |
} |
|
|
Line 815 pool_init(struct pool *pp, size_t size, |
|
Line 807 pool_init(struct pool *pp, size_t size, |
|
pp->pr_entered_file = NULL; |
pp->pr_entered_file = NULL; |
pp->pr_entered_line = 0; |
pp->pr_entered_line = 0; |
|
|
/* |
mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl); |
* XXXAD hack to prevent IP input processing from blocking. |
|
*/ |
|
if (ipl == IPL_SOFTNET) { |
|
mutex_init(&pp->pr_lock, MUTEX_DEFAULT, IPL_VM); |
|
} else { |
|
mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl); |
|
} |
|
cv_init(&pp->pr_cv, wchan); |
cv_init(&pp->pr_cv, wchan); |
pp->pr_ipl = ipl; |
pp->pr_ipl = ipl; |
|
|
Line 856 pool_init(struct pool *pp, size_t size, |
|
Line 841 pool_init(struct pool *pp, size_t size, |
|
|
|
size = sizeof(pcg_t) + |
size = sizeof(pcg_t) + |
(PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t); |
(PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t); |
pool_init(&pcg_normal_pool, size, CACHE_LINE_SIZE, 0, 0, |
pool_init(&pcg_normal_pool, size, coherency_unit, 0, 0, |
"pcgnormal", &pool_allocator_meta, IPL_VM); |
"pcgnormal", &pool_allocator_meta, IPL_VM); |
|
|
size = sizeof(pcg_t) + |
size = sizeof(pcg_t) + |
(PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t); |
(PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t); |
pool_init(&pcg_large_pool, size, CACHE_LINE_SIZE, 0, 0, |
pool_init(&pcg_large_pool, size, coherency_unit, 0, 0, |
"pcglarge", &pool_allocator_meta, IPL_VM); |
"pcglarge", &pool_allocator_meta, IPL_VM); |
} |
} |
|
|
Line 996 pool_get(struct pool *pp, int flags) |
|
Line 981 pool_get(struct pool *pp, int flags) |
|
|
|
#endif /* DIAGNOSTIC */ |
#endif /* DIAGNOSTIC */ |
#ifdef LOCKDEBUG |
#ifdef LOCKDEBUG |
if (flags & PR_WAITOK) |
if (flags & PR_WAITOK) { |
ASSERT_SLEEPABLE(NULL, "pool_get(PR_WAITOK)"); |
ASSERT_SLEEPABLE(); |
|
} |
#endif |
#endif |
|
|
mutex_enter(&pp->pr_lock); |
mutex_enter(&pp->pr_lock); |
Line 1289 pool_do_put(struct pool *pp, void *v, st |
|
Line 1275 pool_do_put(struct pool *pp, void *v, st |
|
if (ph->ph_nmissing == 0) { |
if (ph->ph_nmissing == 0) { |
pp->pr_nidle++; |
pp->pr_nidle++; |
if (pp->pr_npages > pp->pr_minpages && |
if (pp->pr_npages > pp->pr_minpages && |
(pp->pr_npages > pp->pr_maxpages || |
pp->pr_npages > pp->pr_maxpages) { |
pa_starved_p(pp->pr_alloc))) { |
|
pr_rmpage(pp, ph, pq); |
pr_rmpage(pp, ph, pq); |
} else { |
} else { |
LIST_REMOVE(ph, ph_pagelist); |
LIST_REMOVE(ph, ph_pagelist); |
Line 1301 pool_do_put(struct pool *pp, void *v, st |
|
Line 1286 pool_do_put(struct pool *pp, void *v, st |
|
* be idle for some period of time before it can |
* be idle for some period of time before it can |
* be reclaimed by the pagedaemon. This minimizes |
* be reclaimed by the pagedaemon. This minimizes |
* ping-pong'ing for memory. |
* ping-pong'ing for memory. |
|
* |
|
* note for 64-bit time_t: truncating to 32-bit is not |
|
* a problem for our usage. |
*/ |
*/ |
getmicrotime(&ph->ph_time); |
ph->ph_time = time_uptime; |
} |
} |
pool_update_curpage(pp); |
pool_update_curpage(pp); |
} |
} |
Line 1454 pool_prime_page(struct pool *pp, void *s |
|
Line 1442 pool_prime_page(struct pool *pp, void *s |
|
LIST_INIT(&ph->ph_itemlist); |
LIST_INIT(&ph->ph_itemlist); |
ph->ph_page = storage; |
ph->ph_page = storage; |
ph->ph_nmissing = 0; |
ph->ph_nmissing = 0; |
getmicrotime(&ph->ph_time); |
ph->ph_time = time_uptime; |
if ((pp->pr_roflags & PR_PHINPAGE) == 0) |
if ((pp->pr_roflags & PR_PHINPAGE) == 0) |
SPLAY_INSERT(phtree, &pp->pr_phtree, ph); |
SPLAY_INSERT(phtree, &pp->pr_phtree, ph); |
|
|
Line 1615 pool_reclaim(struct pool *pp) |
|
Line 1603 pool_reclaim(struct pool *pp) |
|
{ |
{ |
struct pool_item_header *ph, *phnext; |
struct pool_item_header *ph, *phnext; |
struct pool_pagelist pq; |
struct pool_pagelist pq; |
struct timeval curtime, diff; |
uint32_t curtime; |
bool klock; |
bool klock; |
int rv; |
int rv; |
|
|
Line 1627 pool_reclaim(struct pool *pp) |
|
Line 1615 pool_reclaim(struct pool *pp) |
|
} |
} |
|
|
/* |
/* |
* XXXSMP Because mutexes at IPL_SOFTXXX are still spinlocks, |
* XXXSMP Because we do not want to cause non-MPSAFE code |
* and we are called from the pagedaemon without kernel_lock. |
* to block. |
* Does not apply to IPL_SOFTBIO. |
|
*/ |
*/ |
if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK || |
if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK || |
pp->pr_ipl == IPL_SOFTSERIAL) { |
pp->pr_ipl == IPL_SOFTSERIAL) { |
Line 1652 pool_reclaim(struct pool *pp) |
|
Line 1639 pool_reclaim(struct pool *pp) |
|
|
|
LIST_INIT(&pq); |
LIST_INIT(&pq); |
|
|
getmicrotime(&curtime); |
curtime = time_uptime; |
|
|
for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) { |
for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) { |
phnext = LIST_NEXT(ph, ph_pagelist); |
phnext = LIST_NEXT(ph, ph_pagelist); |
Line 1662 pool_reclaim(struct pool *pp) |
|
Line 1649 pool_reclaim(struct pool *pp) |
|
break; |
break; |
|
|
KASSERT(ph->ph_nmissing == 0); |
KASSERT(ph->ph_nmissing == 0); |
timersub(&curtime, &ph->ph_time, &diff); |
if (curtime - ph->ph_time < pool_inactive_time |
if (diff.tv_sec < pool_inactive_time |
|
&& !pa_starved_p(pp->pr_alloc)) |
&& !pa_starved_p(pp->pr_alloc)) |
continue; |
continue; |
|
|
Line 1804 pool_print_pagelist(struct pool *pp, str |
|
Line 1790 pool_print_pagelist(struct pool *pp, str |
|
#endif |
#endif |
|
|
LIST_FOREACH(ph, pl, ph_pagelist) { |
LIST_FOREACH(ph, pl, ph_pagelist) { |
(*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n", |
(*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n", |
ph->ph_page, ph->ph_nmissing, |
ph->ph_page, ph->ph_nmissing, ph->ph_time); |
(u_long)ph->ph_time.tv_sec, |
|
(u_long)ph->ph_time.tv_usec); |
|
#ifdef DIAGNOSTIC |
#ifdef DIAGNOSTIC |
if (!(pp->pr_roflags & PR_NOTOUCH)) { |
if (!(pp->pr_roflags & PR_NOTOUCH)) { |
LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) { |
LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) { |
Line 2075 pool_cache_bootstrap(pool_cache_t pc, si |
|
Line 2059 pool_cache_bootstrap(pool_cache_t pc, si |
|
if (palloc == NULL && ipl == IPL_NONE) |
if (palloc == NULL && ipl == IPL_NONE) |
palloc = &pool_allocator_nointr; |
palloc = &pool_allocator_nointr; |
pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl); |
pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl); |
|
mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl); |
/* |
|
* XXXAD hack to prevent IP input processing from blocking. |
|
*/ |
|
if (ipl == IPL_SOFTNET) { |
|
mutex_init(&pc->pc_lock, MUTEX_DEFAULT, IPL_VM); |
|
} else { |
|
mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl); |
|
} |
|
|
|
if (ctor == NULL) { |
if (ctor == NULL) { |
ctor = (int (*)(void *, void *, int))nullop; |
ctor = (int (*)(void *, void *, int))nullop; |
Line 2207 pool_cache_cpu_init1(struct cpu_info *ci |
|
Line 2183 pool_cache_cpu_init1(struct cpu_info *ci |
|
index = ci->ci_index; |
index = ci->ci_index; |
|
|
KASSERT(index < MAXCPUS); |
KASSERT(index < MAXCPUS); |
KASSERT(((uintptr_t)pc->pc_cpus & (CACHE_LINE_SIZE - 1)) == 0); |
|
|
|
if ((cc = pc->pc_cpus[index]) != NULL) { |
if ((cc = pc->pc_cpus[index]) != NULL) { |
KASSERT(cc->cc_cpuindex == index); |
KASSERT(cc->cc_cpuindex == index); |
Line 2394 pool_cache_cpu_enter(pool_cache_t pc, in |
|
Line 2369 pool_cache_cpu_enter(pool_cache_t pc, in |
|
* CPU-local data. To avoid touching shared state, we |
* CPU-local data. To avoid touching shared state, we |
* pull the neccessary information from CPU local data. |
* pull the neccessary information from CPU local data. |
*/ |
*/ |
crit_enter(); |
KPREEMPT_DISABLE(curlwp); |
cc = pc->pc_cpus[curcpu()->ci_index]; |
cc = pc->pc_cpus[curcpu()->ci_index]; |
KASSERT(cc->cc_cache == pc); |
KASSERT(cc->cc_cache == pc); |
if (cc->cc_ipl != IPL_NONE) { |
if (cc->cc_ipl != IPL_NONE) { |
*s = splraiseipl(cc->cc_iplcookie); |
*s = splraiseipl(cc->cc_iplcookie); |
} |
} |
KASSERT(((uintptr_t)cc & (CACHE_LINE_SIZE - 1)) == 0); |
|
|
|
return cc; |
return cc; |
} |
} |
Line 2413 pool_cache_cpu_exit(pool_cache_cpu_t *cc |
|
Line 2387 pool_cache_cpu_exit(pool_cache_cpu_t *cc |
|
if (cc->cc_ipl != IPL_NONE) { |
if (cc->cc_ipl != IPL_NONE) { |
splx(*s); |
splx(*s); |
} |
} |
crit_exit(); |
KPREEMPT_ENABLE(curlwp); |
} |
} |
|
|
#if __GNUC_PREREQ__(3, 0) |
pool_cache_cpu_t * __noinline |
__attribute ((noinline)) |
|
#endif |
|
pool_cache_cpu_t * |
|
pool_cache_get_slow(pool_cache_cpu_t *cc, int *s, void **objectp, |
pool_cache_get_slow(pool_cache_cpu_t *cc, int *s, void **objectp, |
paddr_t *pap, int flags) |
paddr_t *pap, int flags) |
{ |
{ |
Line 2523 pool_cache_get_paddr(pool_cache_t pc, in |
|
Line 2494 pool_cache_get_paddr(pool_cache_t pc, in |
|
int s; |
int s; |
|
|
#ifdef LOCKDEBUG |
#ifdef LOCKDEBUG |
if (flags & PR_WAITOK) |
if (flags & PR_WAITOK) { |
ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)"); |
ASSERT_SLEEPABLE(); |
|
} |
#endif |
#endif |
|
|
cc = pool_cache_cpu_enter(pc, &s); |
cc = pool_cache_cpu_enter(pc, &s); |
Line 2535 pool_cache_get_paddr(pool_cache_t pc, in |
|
Line 2507 pool_cache_get_paddr(pool_cache_t pc, in |
|
object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va; |
object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va; |
if (pap != NULL) |
if (pap != NULL) |
*pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa; |
*pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa; |
|
#if defined(DIAGNOSTIC) |
pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL; |
pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL; |
|
#endif /* defined(DIAGNOSTIC) */ |
KASSERT(pcg->pcg_avail <= pcg->pcg_size); |
KASSERT(pcg->pcg_avail <= pcg->pcg_size); |
KASSERT(object != NULL); |
KASSERT(object != NULL); |
cc->cc_hits++; |
cc->cc_hits++; |
Line 2567 pool_cache_get_paddr(pool_cache_t pc, in |
|
Line 2541 pool_cache_get_paddr(pool_cache_t pc, in |
|
return object; |
return object; |
} |
} |
|
|
#if __GNUC_PREREQ__(3, 0) |
pool_cache_cpu_t * __noinline |
__attribute ((noinline)) |
|
#endif |
|
pool_cache_cpu_t * |
|
pool_cache_put_slow(pool_cache_cpu_t *cc, int *s, void *object, paddr_t pa) |
pool_cache_put_slow(pool_cache_cpu_t *cc, int *s, void *object, paddr_t pa) |
{ |
{ |
pcg_t *pcg, *cur; |
pcg_t *pcg, *cur; |
Line 2606 pool_cache_put_slow(pool_cache_cpu_t *cc |
|
Line 2577 pool_cache_put_slow(pool_cache_cpu_t *cc |
|
/* |
/* |
* If there's a empty group, release our full |
* If there's a empty group, release our full |
* group back to the cache. Install the empty |
* group back to the cache. Install the empty |
* group as cc_current and return. |
* group and return. |
*/ |
*/ |
if ((cur = cc->cc_current) != NULL) { |
|
KASSERT(cur->pcg_avail == pcg->pcg_size); |
|
cur->pcg_next = pc->pc_fullgroups; |
|
pc->pc_fullgroups = cur; |
|
pc->pc_nfull++; |
|
} |
|
KASSERT(pcg->pcg_avail == 0); |
KASSERT(pcg->pcg_avail == 0); |
cc->cc_current = pcg; |
|
pc->pc_emptygroups = pcg->pcg_next; |
pc->pc_emptygroups = pcg->pcg_next; |
|
if (cc->cc_previous == NULL) { |
|
cc->cc_previous = pcg; |
|
} else { |
|
if ((cur = cc->cc_current) != NULL) { |
|
KASSERT(cur->pcg_avail == pcg->pcg_size); |
|
cur->pcg_next = pc->pc_fullgroups; |
|
pc->pc_fullgroups = cur; |
|
pc->pc_nfull++; |
|
} |
|
cc->cc_current = pcg; |
|
} |
pc->pc_hits++; |
pc->pc_hits++; |
pc->pc_nempty--; |
pc->pc_nempty--; |
mutex_exit(&pc->pc_lock); |
mutex_exit(&pc->pc_lock); |
Line 2637 pool_cache_put_slow(pool_cache_cpu_t *cc |
|
Line 2612 pool_cache_put_slow(pool_cache_cpu_t *cc |
|
* object away. |
* object away. |
*/ |
*/ |
nobj = pc->pc_pcgsize; |
nobj = pc->pc_pcgsize; |
if (nobj == PCG_NOBJECTS_LARGE) { |
if (pool_cache_disable) { |
|
pcg = NULL; |
|
} else if (nobj == PCG_NOBJECTS_LARGE) { |
pcg = pool_get(&pcg_large_pool, PR_NOWAIT); |
pcg = pool_get(&pcg_large_pool, PR_NOWAIT); |
} else { |
} else { |
pcg = pool_get(&pcg_normal_pool, PR_NOWAIT); |
pcg = pool_get(&pcg_normal_pool, PR_NOWAIT); |
|
|
snprintf(cpucachestr, |
snprintf(cpucachestr, |
sizeof(cpucachestr), |
sizeof(cpucachestr), |
"cached by CPU %u", |
"cached by CPU %u", |
(u_int)ci->ci_cpuid); |
ci->ci_index); |
goto print; |
goto print; |
} |
} |
} |
} |