test_rhashtable.c 5.7 KB
Newer Older
1 2 3
/*
 * Resizable, Scalable, Concurrent Hash Table
 *
4
 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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>
23
#include <linux/sched.h>
24

25
#define MAX_ENTRIES	1000000
26
#define TEST_INSERT_FAIL INT_MAX
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

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)");
47 48 49 50 51 52

struct test_obj {
	int			value;
	struct rhash_head	node;
};

53 54
static struct test_obj array[MAX_ENTRIES];

55
static struct rhashtable_params test_rht_params = {
56 57 58 59 60 61 62
	.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),
};

63 64 65 66
static int __init test_rht_lookup(struct rhashtable *ht)
{
	unsigned int i;

67
	for (i = 0; i < entries * 2; i++) {
68 69 70 71
		struct test_obj *obj;
		bool expected = !(i % 2);
		u32 key = i;

72 73 74
		if (array[i / 2].value == TEST_INSERT_FAIL)
			expected = false;

75
		obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
76 77 78 79 80 81 82 83 84

		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) {
85 86 87
			if (obj->value != i) {
				pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
					obj->value, i);
88 89 90
				return -EINVAL;
			}
		}
91 92

		cond_resched_rcu();
93 94 95 96 97
	}

	return 0;
}

98
static void test_bucket_stats(struct rhashtable *ht)
99
{
100 101
	unsigned int err, total = 0, chain_len = 0;
	struct rhashtable_iter hti;
102 103
	struct rhash_head *pos;

104 105 106 107 108
	err = rhashtable_walk_init(ht, &hti);
	if (err) {
		pr_warn("Test failed: allocation error");
		return;
	}
109

110 111 112 113 114
	err = rhashtable_walk_start(&hti);
	if (err && err != -EAGAIN) {
		pr_warn("Test failed: iterator failed: %d\n", err);
		return;
	}
115

116 117 118 119 120 121 122 123 124
	while ((pos = rhashtable_walk_next(&hti))) {
		if (PTR_ERR(pos) == -EAGAIN) {
			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;
125 126
		}

127
		total++;
128 129
	}

130 131 132 133 134
	rhashtable_walk_stop(&hti);
	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);
135

136
	if (total != atomic_read(&ht->nelems) || total != entries)
137 138 139
		pr_warn("Test failed: Total count mismatch ^^^");
}

140
static s64 __init test_rhashtable(struct rhashtable *ht)
141 142 143
{
	struct test_obj *obj;
	int err;
144
	unsigned int i, insert_fails = 0;
145
	s64 start, end;
146 147 148

	/*
	 * Insertion Test:
149
	 * Insert entries into table with all keys even numbers
150
	 */
151 152 153
	pr_info("  Adding %d keys\n", entries);
	start = ktime_get_ns();
	for (i = 0; i < entries; i++) {
154
		struct test_obj *obj = &array[i];
155 156 157

		obj->value = i * 2;

158
		err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
159 160 161 162 163
		if (err == -ENOMEM || err == -EBUSY) {
			/* Mark failed inserts but continue */
			obj->value = TEST_INSERT_FAIL;
			insert_fails++;
		} else if (err) {
164
			return err;
165
		}
166 167

		cond_resched();
168 169
	}

170 171 172 173
	if (insert_fails)
		pr_info("  %u insertions failed due to memory pressure\n",
			insert_fails);

174
	test_bucket_stats(ht);
175 176 177 178
	rcu_read_lock();
	test_rht_lookup(ht);
	rcu_read_unlock();

179
	test_bucket_stats(ht);
180

181 182
	pr_info("  Deleting %d keys\n", entries);
	for (i = 0; i < entries; i++) {
183 184
		u32 key = i * 2;

185 186 187
		if (array[i].value != TEST_INSERT_FAIL) {
			obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
			BUG_ON(!obj);
188

189 190
			rhashtable_remove_fast(ht, &obj->node, test_rht_params);
		}
191 192

		cond_resched();
193 194
	}

195 196 197 198
	end = ktime_get_ns();
	pr_info("  Duration of test: %lld ns\n", end - start);

	return end - start;
199 200
}

201 202
static struct rhashtable ht;

203 204
static int __init test_rht_init(void)
{
205 206
	int i, err;
	u64 total_time = 0;
207

208
	entries = min(entries, MAX_ENTRIES);
209

210 211 212
	test_rht_params.automatic_shrinking = shrinking;
	test_rht_params.max_size = max_size;
	test_rht_params.nelem_hint = size;
213

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

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

220
		pr_info("Test %02d:\n", i);
221
		memset(&array, 0, sizeof(array));
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		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;
	}

239 240
	do_div(total_time, runs);
	pr_info("Average test time: %llu\n", total_time);
241 242

	return 0;
243 244
}

245 246 247 248
static void __exit test_rht_exit(void)
{
}

249
module_init(test_rht_init);
250
module_exit(test_rht_exit);
251 252

MODULE_LICENSE("GPL v2");