Skip to content

Commit

Permalink
test_rhashtable: don't allocate huge static array
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Florian Westphal authored and David S. Miller committed Sep 19, 2017
1 parent 3d5cc72 commit 7e936bd
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/test_rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ struct thread_data {
struct test_obj *objs;
};

static struct test_obj array[MAX_ENTRIES];

static struct rhashtable_params test_rht_params = {
.head_offset = offsetof(struct test_obj, node),
.key_offset = offsetof(struct test_obj, value),
Expand All @@ -85,15 +83,15 @@ static struct rhashtable_params test_rht_params = {
static struct semaphore prestart_sem;
static struct semaphore startup_sem = __SEMAPHORE_INITIALIZER(startup_sem, 0);

static int insert_retry(struct rhashtable *ht, struct rhash_head *obj,
static int insert_retry(struct rhashtable *ht, struct test_obj *obj,
const struct rhashtable_params params)
{
int err, retries = -1, enomem_retries = 0;

do {
retries++;
cond_resched();
err = rhashtable_insert_fast(ht, obj, params);
err = rhashtable_insert_fast(ht, &obj->node, params);
if (err == -ENOMEM && enomem_retry) {
enomem_retries++;
err = -EBUSY;
Expand All @@ -107,7 +105,7 @@ static int insert_retry(struct rhashtable *ht, struct rhash_head *obj,
return err ? : retries;
}

static int __init test_rht_lookup(struct rhashtable *ht)
static int __init test_rht_lookup(struct rhashtable *ht, struct test_obj *array)
{
unsigned int i;

Expand Down Expand Up @@ -186,7 +184,7 @@ static void test_bucket_stats(struct rhashtable *ht)
pr_warn("Test failed: Total count mismatch ^^^");
}

static s64 __init test_rhashtable(struct rhashtable *ht)
static s64 __init test_rhashtable(struct rhashtable *ht, struct test_obj *array)
{
struct test_obj *obj;
int err;
Expand All @@ -203,7 +201,7 @@ static s64 __init test_rhashtable(struct rhashtable *ht)
struct test_obj *obj = &array[i];

obj->value.id = i * 2;
err = insert_retry(ht, &obj->node, test_rht_params);
err = insert_retry(ht, obj, test_rht_params);
if (err > 0)
insert_retries += err;
else if (err)
Expand All @@ -216,7 +214,7 @@ static s64 __init test_rhashtable(struct rhashtable *ht)

test_bucket_stats(ht);
rcu_read_lock();
test_rht_lookup(ht);
test_rht_lookup(ht, array);
rcu_read_unlock();

test_bucket_stats(ht);
Expand Down Expand Up @@ -286,7 +284,7 @@ static int threadfunc(void *data)
for (i = 0; i < entries; i++) {
tdata->objs[i].value.id = i;
tdata->objs[i].value.tid = tdata->id;
err = insert_retry(&ht, &tdata->objs[i].node, test_rht_params);
err = insert_retry(&ht, &tdata->objs[i], test_rht_params);
if (err > 0) {
insert_retries += err;
} else if (err) {
Expand Down Expand Up @@ -349,31 +347,38 @@ static int __init test_rht_init(void)
test_rht_params.max_size = max_size ? : roundup_pow_of_two(entries);
test_rht_params.nelem_hint = size;

objs = vzalloc((test_rht_params.max_size + 1) * sizeof(struct test_obj));
if (!objs)
return -ENOMEM;

pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
size, max_size, shrinking);

for (i = 0; i < runs; i++) {
s64 time;

pr_info("Test %02d:\n", i);
memset(&array, 0, sizeof(array));
memset(objs, 0, test_rht_params.max_size * sizeof(struct test_obj));

err = rhashtable_init(&ht, &test_rht_params);
if (err < 0) {
pr_warn("Test failed: Unable to initialize hashtable: %d\n",
err);
continue;
}

time = test_rhashtable(&ht);
time = test_rhashtable(&ht, objs);
rhashtable_destroy(&ht);
if (time < 0) {
vfree(objs);
pr_warn("Test failed: return code %lld\n", time);
return -EINVAL;
}

total_time += time;
}

vfree(objs);
do_div(total_time, runs);
pr_info("Average test time: %llu\n", total_time);

Expand Down

0 comments on commit 7e936bd

Please sign in to comment.