2015-01-29 21:40:25 +07:00
|
|
|
/*
|
|
|
|
* Resizable, Scalable, Concurrent Hash Table
|
|
|
|
*
|
2015-05-01 05:37:41 +07:00
|
|
|
* Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
|
2015-01-29 21:40:25 +07:00
|
|
|
* Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
* Self Test
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/rcupdate.h>
|
|
|
|
#include <linux/rhashtable.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
#define MAX_ENTRIES 1000000
|
|
|
|
|
|
|
|
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)");
|
|
|
|
|
|
|
|
static int size = 8;
|
|
|
|
module_param(size, int, 0);
|
|
|
|
MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
|
2015-01-29 21:40:25 +07:00
|
|
|
|
|
|
|
struct test_obj {
|
|
|
|
int value;
|
|
|
|
struct rhash_head node;
|
|
|
|
};
|
|
|
|
|
2015-05-01 05:37:43 +07:00
|
|
|
static struct test_obj array[MAX_ENTRIES];
|
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
static struct rhashtable_params test_rht_params = {
|
2015-03-20 17:57:04 +07:00
|
|
|
.head_offset = offsetof(struct test_obj, node),
|
|
|
|
.key_offset = offsetof(struct test_obj, value),
|
|
|
|
.key_len = sizeof(int),
|
|
|
|
.hashfn = jhash,
|
|
|
|
.nulls_base = (3U << RHT_BASE_SHIFT),
|
|
|
|
};
|
|
|
|
|
2015-01-29 21:40:25 +07:00
|
|
|
static int __init test_rht_lookup(struct rhashtable *ht)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
for (i = 0; i < entries * 2; i++) {
|
2015-01-29 21:40:25 +07:00
|
|
|
struct test_obj *obj;
|
|
|
|
bool expected = !(i % 2);
|
|
|
|
u32 key = i;
|
|
|
|
|
2015-03-20 17:57:04 +07:00
|
|
|
obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
|
2015-01-29 21:40:25 +07:00
|
|
|
|
|
|
|
if (expected && !obj) {
|
|
|
|
pr_warn("Test failed: Could not find key %u\n", key);
|
|
|
|
return -ENOENT;
|
|
|
|
} else if (!expected && obj) {
|
|
|
|
pr_warn("Test failed: Unexpected entry found for key %u\n",
|
|
|
|
key);
|
|
|
|
return -EEXIST;
|
|
|
|
} else if (expected && obj) {
|
2015-05-01 05:37:42 +07:00
|
|
|
if (obj->value != i) {
|
|
|
|
pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
|
|
|
|
obj->value, i);
|
2015-01-29 21:40:25 +07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bucket_stats(struct rhashtable *ht, bool quiet)
|
|
|
|
{
|
|
|
|
unsigned int cnt, rcu_cnt, i, total = 0;
|
|
|
|
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)
|
2015-03-14 09:57:24 +07:00
|
|
|
pr_info(" [%#4x/%u]", i, tbl->size);
|
2015-01-29 21:40:25 +07:00
|
|
|
|
|
|
|
rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
|
|
|
|
cnt++;
|
|
|
|
total++;
|
|
|
|
if (!quiet)
|
|
|
|
pr_cont(" [%p],", obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
rht_for_each_entry_rcu(obj, pos, tbl, i, node)
|
|
|
|
rcu_cnt++;
|
|
|
|
|
|
|
|
if (rcu_cnt != cnt)
|
|
|
|
pr_warn("Test failed: Chain count mismach %d != %d",
|
|
|
|
cnt, rcu_cnt);
|
|
|
|
|
|
|
|
if (!quiet)
|
|
|
|
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",
|
2015-05-01 05:37:41 +07:00
|
|
|
total, atomic_read(&ht->nelems), entries);
|
2015-01-29 21:40:25 +07:00
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
if (total != atomic_read(&ht->nelems) || total != entries)
|
2015-01-29 21:40:25 +07:00
|
|
|
pr_warn("Test failed: Total count mismatch ^^^");
|
|
|
|
}
|
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
static s64 __init test_rhashtable(struct rhashtable *ht)
|
2015-01-29 21:40:25 +07:00
|
|
|
{
|
|
|
|
struct test_obj *obj;
|
|
|
|
int err;
|
|
|
|
unsigned int i;
|
2015-05-01 05:37:41 +07:00
|
|
|
s64 start, end;
|
2015-01-29 21:40:25 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Insertion Test:
|
2015-05-01 05:37:41 +07:00
|
|
|
* Insert entries into table with all keys even numbers
|
2015-01-29 21:40:25 +07:00
|
|
|
*/
|
2015-05-01 05:37:41 +07:00
|
|
|
pr_info(" Adding %d keys\n", entries);
|
|
|
|
start = ktime_get_ns();
|
|
|
|
for (i = 0; i < entries; i++) {
|
2015-05-01 05:37:43 +07:00
|
|
|
struct test_obj *obj = &array[i];
|
2015-01-29 21:40:25 +07:00
|
|
|
|
|
|
|
obj->value = i * 2;
|
|
|
|
|
2015-03-20 17:57:04 +07:00
|
|
|
err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
|
2015-05-01 05:37:43 +07:00
|
|
|
if (err)
|
|
|
|
return err;
|
2015-01-29 21:40:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
test_bucket_stats(ht, true);
|
|
|
|
test_rht_lookup(ht);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
test_bucket_stats(ht, true);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
pr_info(" Deleting %d keys\n", entries);
|
|
|
|
for (i = 0; i < entries; i++) {
|
2015-01-29 21:40:25 +07:00
|
|
|
u32 key = i * 2;
|
|
|
|
|
2015-03-20 17:57:04 +07:00
|
|
|
obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
|
2015-01-29 21:40:25 +07:00
|
|
|
BUG_ON(!obj);
|
|
|
|
|
2015-03-20 17:57:04 +07:00
|
|
|
rhashtable_remove_fast(ht, &obj->node, test_rht_params);
|
2015-01-29 21:40:25 +07:00
|
|
|
}
|
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
end = ktime_get_ns();
|
|
|
|
pr_info(" Duration of test: %lld ns\n", end - start);
|
|
|
|
|
|
|
|
return end - start;
|
2015-01-29 21:40:25 +07:00
|
|
|
}
|
|
|
|
|
rhashtable: don't allocate ht structure on stack in test_rht_init
With object runtime debugging enabled, the rhashtable test suite
will rightfully throw a warning "ODEBUG: object is on stack, but
not annotated" from rhashtable_init().
This is because run_work is (correctly) being initialized via
INIT_WORK(), and not annotated by INIT_WORK_ONSTACK(). Meaning,
rhashtable_init() is okay as is, we just need to move ht e.g.,
into global scope.
It never triggered anything, since test_rhashtable is rather a
controlled environment and effectively runs to completion, so
that stack memory is not vanishing underneath us, we shouldn't
confuse any testers with it though.
Fixes: 7e1e77636e36 ("lib: Resizable, Scalable, Concurrent Hash Table")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Thomas Graf <tgraf@suug.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-02-21 03:14:21 +07:00
|
|
|
static struct rhashtable ht;
|
|
|
|
|
2015-01-29 21:40:25 +07:00
|
|
|
static int __init test_rht_init(void)
|
|
|
|
{
|
2015-05-01 05:37:41 +07:00
|
|
|
int i, err;
|
|
|
|
u64 total_time = 0;
|
2015-01-29 21:40:25 +07:00
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
entries = min(entries, MAX_ENTRIES);
|
2015-01-29 21:40:25 +07:00
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
test_rht_params.automatic_shrinking = shrinking;
|
|
|
|
test_rht_params.max_size = max_size;
|
|
|
|
test_rht_params.nelem_hint = size;
|
2015-01-29 21:40:25 +07:00
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
|
|
|
|
size, max_size, shrinking);
|
2015-01-29 21:40:25 +07:00
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
for (i = 0; i < runs; i++) {
|
|
|
|
s64 time;
|
2015-01-29 21:40:25 +07:00
|
|
|
|
2015-05-01 05:37:41 +07:00
|
|
|
pr_info("Test %02d:\n", i);
|
2015-05-01 05:37:43 +07:00
|
|
|
memset(&array, 0, sizeof(array));
|
2015-05-01 05:37:41 +07:00
|
|
|
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);
|
|
|
|
rhashtable_destroy(&ht);
|
|
|
|
if (time < 0) {
|
|
|
|
pr_warn("Test failed: return code %lld\n", time);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_time += time;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("Average test time: %llu\n", total_time / runs);
|
|
|
|
|
|
|
|
return 0;
|
2015-01-29 21:40:25 +07:00
|
|
|
}
|
|
|
|
|
2015-02-20 06:53:39 +07:00
|
|
|
static void __exit test_rht_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-29 21:40:25 +07:00
|
|
|
module_init(test_rht_init);
|
2015-02-20 06:53:39 +07:00
|
|
|
module_exit(test_rht_exit);
|
2015-01-29 21:40:25 +07:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|