Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Commit

Permalink
Add spl_kmem_cache_expire module option
Browse files Browse the repository at this point in the history
Cache aging was implemented because it was part of the default Solaris
kmem_cache behavior.  The idea is that per-cpu objects which haven't been
accessed in several seconds should be returned to the cache.  On the other
hand Linux slabs never move objects back to the slabs unless there is
memory pressure on the system.

This behavior is now configurable through the 'spl_kmem_cache_expire'
module option.  The value is a bit mask with the following meaning.

  0x1 - Solaris style cache aging eviction is enabled.
  0x2 - Linux style low memory eviction is enabled.

Both methods may be safely enabled simultaneously, but by default
both are disabled.  It has never been clear if the kmem cache aging
(which has been around from day one) actually does any good.  It has
however been the source of numerous bugs so I wouldn't mind retiring
it entirely.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes openzfs/zfs#1227
Closes #210
  • Loading branch information
behlendorf committed Jan 28, 2013
1 parent 84dd1f4 commit 0936c34
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
8 changes: 6 additions & 2 deletions include/sys/kmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,13 @@ typedef enum kmem_cbrc {
#define KMC_ALLOC (1 << KMC_BIT_ALLOC)
#define KMC_MAX (1 << KMC_BIT_MAX)

#define KMC_REAP_CHUNK INT_MAX
#define KMC_DEFAULT_SEEKS 1
#define KMC_REAP_CHUNK INT_MAX
#define KMC_DEFAULT_SEEKS 1

#define KMC_EXPIRE_AGE 0x1 /* Due to age */
#define KMC_EXPIRE_MEM 0x2 /* Due to low memory */

extern unsigned int spl_kmem_cache_expire;
extern struct list_head spl_kmem_cache_list;
extern struct rw_semaphore spl_kmem_cache_sem;

Expand Down
36 changes: 32 additions & 4 deletions module/spl/spl-kmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@

#define SS_DEBUG_SUBSYS SS_KMEM

/*
* Cache expiration was implemented because it was part of the default Solaris
* kmem_cache behavior. The idea is that per-cpu objects which haven't been
* accessed in several seconds should be returned to the cache. On the other
* hand Linux slabs never move objects back to the slabs unless there is
* memory pressure on the system. By default both methods are disabled, but
* may be enabled by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM.
*/
unsigned int spl_kmem_cache_expire = 0;
EXPORT_SYMBOL(spl_kmem_cache_expire);
module_param(spl_kmem_cache_expire, uint, 0644);
MODULE_PARM_DESC(spl_kmem_cache_expire, "By age (0x1) or low memory (0x2)");

/*
* The minimum amount of memory measured in pages to be free at all
* times on the system. This is similar to Linux's zone->pages_min
Expand Down Expand Up @@ -1317,6 +1330,10 @@ spl_cache_age(void *data)

ASSERT(skc->skc_magic == SKC_MAGIC);

/* Dynamically disabled at run time */
if (!(spl_kmem_cache_expire & KMC_EXPIRE_AGE))
return;

atomic_inc(&skc->skc_ref);
spl_on_each_cpu(spl_magazine_age, skc, 1);
spl_slab_reclaim(skc, skc->skc_reap, 0);
Expand Down Expand Up @@ -1609,9 +1626,10 @@ spl_kmem_cache_create(char *name, size_t size, size_t align,
if (rc)
SGOTO(out, rc);

skc->skc_taskqid = taskq_dispatch_delay(spl_kmem_cache_taskq,
spl_cache_age, skc, TQ_SLEEP,
ddi_get_lbolt() + skc->skc_delay / 3 * HZ);
if (spl_kmem_cache_expire & KMC_EXPIRE_AGE)
skc->skc_taskqid = taskq_dispatch_delay(spl_kmem_cache_taskq,
spl_cache_age, skc, TQ_SLEEP,
ddi_get_lbolt() + skc->skc_delay / 3 * HZ);

down_write(&spl_kmem_cache_sem);
list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
Expand Down Expand Up @@ -2168,7 +2186,17 @@ spl_kmem_cache_reap_now(spl_kmem_cache_t *skc, int count)
} while (do_reclaim);
}

/* Reclaim from the cache, ignoring it's age and delay. */
/* Reclaim from the magazine then the slabs ignoring age and delay. */
if (spl_kmem_cache_expire & KMC_EXPIRE_MEM) {
spl_kmem_magazine_t *skm;
int i;

for_each_online_cpu(i) {
skm = skc->skc_mag[i];
spl_cache_flush(skc, skm, skm->skm_avail);
}
}

spl_slab_reclaim(skc, count, 1);
clear_bit(KMC_BIT_REAPING, &skc->skc_flags);
smp_mb__after_clear_bit();
Expand Down
14 changes: 14 additions & 0 deletions module/splat/splat-kmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,13 @@ splat_kmem_test8(struct file *file, void *arg)
{
kmem_cache_priv_t *kcp;
kmem_cache_thread_t *kct;
unsigned int spl_kmem_cache_expire_old;
int i, rc = 0;

/* Enable cache aging just for this test if it is disabled */
spl_kmem_cache_expire_old = spl_kmem_cache_expire;
spl_kmem_cache_expire = KMC_EXPIRE_AGE;

kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST8_NAME,
256, 0, 0);
if (!kcp) {
Expand Down Expand Up @@ -882,6 +887,8 @@ splat_kmem_test8(struct file *file, void *arg)
out_kcp:
splat_kmem_cache_test_kcp_free(kcp);
out:
spl_kmem_cache_expire = spl_kmem_cache_expire_old;

return rc;
}

Expand All @@ -898,8 +905,13 @@ splat_kmem_test9(struct file *file, void *arg)
{
kmem_cache_priv_t *kcp;
kmem_cache_thread_t *kct;
unsigned int spl_kmem_cache_expire_old;
int i, rc = 0, count = SPLAT_KMEM_OBJ_COUNT * 128;

/* Enable cache aging just for this test if it is disabled */
spl_kmem_cache_expire_old = spl_kmem_cache_expire;
spl_kmem_cache_expire = KMC_EXPIRE_AGE;

kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST9_NAME,
256, 0, 0);
if (!kcp) {
Expand Down Expand Up @@ -968,6 +980,8 @@ splat_kmem_test9(struct file *file, void *arg)
out_kcp:
splat_kmem_cache_test_kcp_free(kcp);
out:
spl_kmem_cache_expire = spl_kmem_cache_expire_old;

return rc;
}

Expand Down

0 comments on commit 0936c34

Please sign in to comment.