Skip to content

Commit

Permalink
rhashtable: Introduce max_size/min_size
Browse files Browse the repository at this point in the history
This patch adds the parameters max_size and min_size which are
meant to replace max_shift and min_shift.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Herbert Xu authored and David S. Miller committed Mar 18, 2015
1 parent 6aebd94 commit c2e213c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions include/linux/rhashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ struct rhashtable;
* @head_offset: Offset of rhash_head in struct to be hashed
* @max_shift: Maximum number of shifts while expanding
* @min_shift: Minimum number of shifts while shrinking
* @max_size: Maximum size while expanding
* @min_size: Minimum size while shrinking
* @nulls_base: Base value to generate nulls marker
* @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
* @hashfn: Function to hash key
Expand All @@ -97,6 +99,8 @@ struct rhashtable_params {
size_t head_offset;
size_t max_shift;
size_t min_shift;
unsigned int max_size;
unsigned int min_size;
u32 nulls_base;
size_t locks_mul;
rht_hashfn_t hashfn;
Expand Down
12 changes: 8 additions & 4 deletions lib/rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <linux/err.h>

#define HASH_DEFAULT_SIZE 64UL
#define HASH_MIN_SIZE 4UL
#define HASH_MIN_SIZE 4U
#define BUCKET_LOCKS_PER_CPU 128UL

/* Base bits plus 1 bit for nulls marker */
Expand Down Expand Up @@ -188,7 +188,8 @@ static bool rht_grow_above_75(const struct rhashtable *ht,
{
/* Expand table when exceeding 75% load */
return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
(!ht->p.max_shift || tbl->size < (1 << ht->p.max_shift));
(!ht->p.max_shift || tbl->size < (1 << ht->p.max_shift)) &&
(!ht->p.max_size || tbl->size < ht->p.max_size);
}

/**
Expand All @@ -201,7 +202,8 @@ static bool rht_shrink_below_30(const struct rhashtable *ht,
{
/* Shrink table beneath 30% load */
return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
tbl->size > (1 << ht->p.min_shift);
tbl->size > (1 << ht->p.min_shift) &&
tbl->size > ht->p.min_size;
}

static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Expand Down Expand Up @@ -873,7 +875,8 @@ EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
static size_t rounded_hashtable_size(struct rhashtable_params *params)
{
return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
1UL << params->min_shift);
max(1UL << params->min_shift,
(unsigned long)params->min_size));
}

/**
Expand Down Expand Up @@ -935,6 +938,7 @@ int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)

params->min_shift = max_t(size_t, params->min_shift,
ilog2(HASH_MIN_SIZE));
params->min_size = max(params->min_size, HASH_MIN_SIZE);

if (params->nelem_hint)
size = rounded_hashtable_size(params);
Expand Down

0 comments on commit c2e213c

Please sign in to comment.