Commit 10308220 authored by David S. Miller's avatar David S. Miller

Merge branch 'rhashtable-test'

Thomas Graf says:

====================
rhashtable self-test improvements

This series improves the rhashtable self-test to:
  * Avoid allocation of test objects
  * Measure the time of test runs
  * Use the iterator to walk the table for consistency
  * Account for failed insertions due to memory pressure or
    utilization pressure
  * Ignore failed insertions when checking for consistency
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 7a852021 67b7cbf4
/* /*
* Resizable, Scalable, Concurrent Hash Table * Resizable, Scalable, Concurrent Hash Table
* *
* Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
* Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
* *
* Based on the following paper:
* https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
*
* Code partially derived from nft_hash
*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as * it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. * published by the Free Software Foundation.
...@@ -26,20 +21,37 @@ ...@@ -26,20 +21,37 @@
#include <linux/rhashtable.h> #include <linux/rhashtable.h>
#include <linux/slab.h> #include <linux/slab.h>
#define MAX_ENTRIES 1000000
#define TEST_INSERT_FAIL INT_MAX
static int entries = 50000;
module_param(entries, int, 0);
MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
static int runs = 4;
module_param(runs, int, 0);
MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
static int max_size = 65536;
module_param(max_size, int, 0);
MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
static bool shrinking = false;
module_param(shrinking, bool, 0);
MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
#define TEST_HT_SIZE 8 static int size = 8;
#define TEST_ENTRIES 2048 module_param(size, int, 0);
#define TEST_PTR ((void *) 0xdeadbeef) MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
#define TEST_NEXPANDS 4
struct test_obj { struct test_obj {
void *ptr;
int value; int value;
struct rhash_head node; struct rhash_head node;
}; };
static const struct rhashtable_params test_rht_params = { static struct test_obj array[MAX_ENTRIES];
.nelem_hint = TEST_HT_SIZE,
static struct rhashtable_params test_rht_params = {
.head_offset = offsetof(struct test_obj, node), .head_offset = offsetof(struct test_obj, node),
.key_offset = offsetof(struct test_obj, value), .key_offset = offsetof(struct test_obj, value),
.key_len = sizeof(int), .key_len = sizeof(int),
...@@ -51,11 +63,14 @@ static int __init test_rht_lookup(struct rhashtable *ht) ...@@ -51,11 +63,14 @@ static int __init test_rht_lookup(struct rhashtable *ht)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < TEST_ENTRIES * 2; i++) { for (i = 0; i < entries * 2; i++) {
struct test_obj *obj; struct test_obj *obj;
bool expected = !(i % 2); bool expected = !(i % 2);
u32 key = i; u32 key = i;
if (array[i / 2].value == TEST_INSERT_FAIL)
expected = false;
obj = rhashtable_lookup_fast(ht, &key, test_rht_params); obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
if (expected && !obj) { if (expected && !obj) {
...@@ -66,9 +81,9 @@ static int __init test_rht_lookup(struct rhashtable *ht) ...@@ -66,9 +81,9 @@ static int __init test_rht_lookup(struct rhashtable *ht)
key); key);
return -EEXIST; return -EEXIST;
} else if (expected && obj) { } else if (expected && obj) {
if (obj->ptr != TEST_PTR || obj->value != i) { if (obj->value != i) {
pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n", pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
obj->ptr, TEST_PTR, obj->value, i); obj->value, i);
return -EINVAL; return -EINVAL;
} }
} }
...@@ -77,129 +92,146 @@ static int __init test_rht_lookup(struct rhashtable *ht) ...@@ -77,129 +92,146 @@ static int __init test_rht_lookup(struct rhashtable *ht)
return 0; return 0;
} }
static void test_bucket_stats(struct rhashtable *ht, bool quiet) static void test_bucket_stats(struct rhashtable *ht)
{ {
unsigned int cnt, rcu_cnt, i, total = 0; unsigned int err, total = 0, chain_len = 0;
struct rhashtable_iter hti;
struct rhash_head *pos; struct rhash_head *pos;
struct test_obj *obj;
struct bucket_table *tbl;
tbl = rht_dereference_rcu(ht->tbl, ht);
for (i = 0; i < tbl->size; i++) {
rcu_cnt = cnt = 0;
if (!quiet)
pr_info(" [%#4x/%u]", i, tbl->size);
rht_for_each_entry_rcu(obj, pos, tbl, i, node) { err = rhashtable_walk_init(ht, &hti);
cnt++; if (err) {
total++; pr_warn("Test failed: allocation error");
if (!quiet) return;
pr_cont(" [%p],", obj);
} }
rht_for_each_entry_rcu(obj, pos, tbl, i, node) err = rhashtable_walk_start(&hti);
rcu_cnt++; if (err && err != -EAGAIN) {
pr_warn("Test failed: iterator failed: %d\n", err);
return;
}
if (rcu_cnt != cnt) while ((pos = rhashtable_walk_next(&hti))) {
pr_warn("Test failed: Chain count mismach %d != %d", if (PTR_ERR(pos) == -EAGAIN) {
cnt, rcu_cnt); pr_info("Info: encountered resize\n");
chain_len++;
continue;
} else if (IS_ERR(pos)) {
pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
PTR_ERR(pos));
break;
}
if (!quiet) total++;
pr_cont("\n [%#x] first element: %p, chain length: %u\n",
i, tbl->buckets[i], cnt);
} }
pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n", rhashtable_walk_stop(&hti);
total, atomic_read(&ht->nelems), TEST_ENTRIES); rhashtable_walk_exit(&hti);
pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
total, atomic_read(&ht->nelems), entries, chain_len);
if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES) if (total != atomic_read(&ht->nelems) || total != entries)
pr_warn("Test failed: Total count mismatch ^^^"); pr_warn("Test failed: Total count mismatch ^^^");
} }
static int __init test_rhashtable(struct rhashtable *ht) static s64 __init test_rhashtable(struct rhashtable *ht)
{ {
struct bucket_table *tbl;
struct test_obj *obj; struct test_obj *obj;
struct rhash_head *pos, *next;
int err; int err;
unsigned int i; unsigned int i, insert_fails = 0;
s64 start, end;
/* /*
* Insertion Test: * Insertion Test:
* Insert TEST_ENTRIES into table with all keys even numbers * Insert entries into table with all keys even numbers
*/ */
pr_info(" Adding %d keys\n", TEST_ENTRIES); pr_info(" Adding %d keys\n", entries);
for (i = 0; i < TEST_ENTRIES; i++) { start = ktime_get_ns();
struct test_obj *obj; for (i = 0; i < entries; i++) {
struct test_obj *obj = &array[i];
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
if (!obj) {
err = -ENOMEM;
goto error;
}
obj->ptr = TEST_PTR;
obj->value = i * 2; obj->value = i * 2;
err = rhashtable_insert_fast(ht, &obj->node, test_rht_params); err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
if (err) { if (err == -ENOMEM || err == -EBUSY) {
kfree(obj); /* Mark failed inserts but continue */
goto error; obj->value = TEST_INSERT_FAIL;
insert_fails++;
} else if (err) {
return err;
} }
} }
if (insert_fails)
pr_info(" %u insertions failed due to memory pressure\n",
insert_fails);
test_bucket_stats(ht);
rcu_read_lock(); rcu_read_lock();
test_bucket_stats(ht, true);
test_rht_lookup(ht); test_rht_lookup(ht);
rcu_read_unlock(); rcu_read_unlock();
rcu_read_lock(); test_bucket_stats(ht);
test_bucket_stats(ht, true);
rcu_read_unlock();
pr_info(" Deleting %d keys\n", TEST_ENTRIES); pr_info(" Deleting %d keys\n", entries);
for (i = 0; i < TEST_ENTRIES; i++) { for (i = 0; i < entries; i++) {
u32 key = i * 2; u32 key = i * 2;
if (array[i].value != TEST_INSERT_FAIL) {
obj = rhashtable_lookup_fast(ht, &key, test_rht_params); obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
BUG_ON(!obj); BUG_ON(!obj);
rhashtable_remove_fast(ht, &obj->node, test_rht_params); rhashtable_remove_fast(ht, &obj->node, test_rht_params);
kfree(obj); }
} }
return 0; end = ktime_get_ns();
pr_info(" Duration of test: %lld ns\n", end - start);
error:
tbl = rht_dereference_rcu(ht->tbl, ht);
for (i = 0; i < tbl->size; i++)
rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
kfree(obj);
return err; return end - start;
} }
static struct rhashtable ht; static struct rhashtable ht;
static int __init test_rht_init(void) static int __init test_rht_init(void)
{ {
int err; int i, err;
u64 total_time = 0;
entries = min(entries, MAX_ENTRIES);
test_rht_params.automatic_shrinking = shrinking;
test_rht_params.max_size = max_size;
test_rht_params.nelem_hint = size;
pr_info("Running resizable hashtable tests...\n"); 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));
err = rhashtable_init(&ht, &test_rht_params); err = rhashtable_init(&ht, &test_rht_params);
if (err < 0) { if (err < 0) {
pr_warn("Test failed: Unable to initialize hashtable: %d\n", pr_warn("Test failed: Unable to initialize hashtable: %d\n",
err); err);
return err; continue;
} }
err = test_rhashtable(&ht); time = test_rhashtable(&ht);
rhashtable_destroy(&ht); rhashtable_destroy(&ht);
if (time < 0) {
pr_warn("Test failed: return code %lld\n", time);
return -EINVAL;
}
return err; total_time += time;
}
pr_info("Average test time: %llu\n", total_time / runs);
return 0;
} }
static void __exit test_rht_exit(void) static void __exit test_rht_exit(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment