test_rhashtable.c 16.4 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
 * 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>
19
#include <linux/kthread.h>
20 21 22
#include <linux/module.h>
#include <linux/rcupdate.h>
#include <linux/rhashtable.h>
23
#include <linux/semaphore.h>
24
#include <linux/slab.h>
25
#include <linux/sched.h>
26
#include <linux/random.h>
27
#include <linux/vmalloc.h>
28

29
#define MAX_ENTRIES	1000000
30
#define TEST_INSERT_FAIL INT_MAX
31

32 33 34
static int parm_entries = 50000;
module_param(parm_entries, int, 0);
MODULE_PARM_DESC(parm_entries, "Number of entries to add (default: 50000)");
35 36 37 38 39

static int runs = 4;
module_param(runs, int, 0);
MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");

40
static int max_size = 0;
41
module_param(max_size, int, 0);
42
MODULE_PARM_DESC(max_size, "Maximum table size (default: calculated)");
43 44 45 46 47 48 49 50

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)");
51

52 53 54 55
static int tcount = 10;
module_param(tcount, int, 0);
MODULE_PARM_DESC(tcount, "Number of threads to spawn (default: 10)");

56 57 58 59
static bool enomem_retry = false;
module_param(enomem_retry, bool, 0);
MODULE_PARM_DESC(enomem_retry, "Retry insert even if -ENOMEM was returned (default: off)");

60 61 62 63 64
struct test_obj_val {
	int	id;
	int	tid;
};

65
struct test_obj {
66
	struct test_obj_val	value;
67 68 69
	struct rhash_head	node;
};

70 71 72 73 74
struct test_obj_rhl {
	struct test_obj_val	value;
	struct rhlist_head	list_node;
};

75
struct thread_data {
76
	unsigned int entries;
77 78 79 80 81
	int id;
	struct task_struct *task;
	struct test_obj *objs;
};

82
static struct rhashtable_params test_rht_params = {
83 84
	.head_offset = offsetof(struct test_obj, node),
	.key_offset = offsetof(struct test_obj, value),
85
	.key_len = sizeof(struct test_obj_val),
86 87 88 89
	.hashfn = jhash,
	.nulls_base = (3U << RHT_BASE_SHIFT),
};

90 91 92
static struct semaphore prestart_sem;
static struct semaphore startup_sem = __SEMAPHORE_INITIALIZER(startup_sem, 0);

93
static int insert_retry(struct rhashtable *ht, struct test_obj *obj,
94 95
                        const struct rhashtable_params params)
{
96
	int err, retries = -1, enomem_retries = 0;
97 98 99 100

	do {
		retries++;
		cond_resched();
101
		err = rhashtable_insert_fast(ht, &obj->node, params);
102 103 104 105
		if (err == -ENOMEM && enomem_retry) {
			enomem_retries++;
			err = -EBUSY;
		}
106 107
	} while (err == -EBUSY);

108 109 110 111
	if (enomem_retries)
		pr_info(" %u insertions retried after -ENOMEM\n",
			enomem_retries);

112 113 114
	return err ? : retries;
}

115 116
static int __init test_rht_lookup(struct rhashtable *ht, struct test_obj *array,
				  unsigned int entries)
117 118 119
{
	unsigned int i;

120
	for (i = 0; i < entries; i++) {
121 122
		struct test_obj *obj;
		bool expected = !(i % 2);
123 124 125
		struct test_obj_val key = {
			.id = i,
		};
126

127
		if (array[i / 2].value.id == TEST_INSERT_FAIL)
128 129
			expected = false;

130
		obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
131 132

		if (expected && !obj) {
133
			pr_warn("Test failed: Could not find key %u\n", key.id);
134 135 136
			return -ENOENT;
		} else if (!expected && obj) {
			pr_warn("Test failed: Unexpected entry found for key %u\n",
137
				key.id);
138 139
			return -EEXIST;
		} else if (expected && obj) {
140
			if (obj->value.id != i) {
141
				pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
142
					obj->value.id, i);
143 144 145
				return -EINVAL;
			}
		}
146 147

		cond_resched_rcu();
148 149 150 151 152
	}

	return 0;
}

153
static void test_bucket_stats(struct rhashtable *ht, unsigned int entries)
154
{
155 156
	unsigned int err, total = 0, chain_len = 0;
	struct rhashtable_iter hti;
157 158
	struct rhash_head *pos;

159
	err = rhashtable_walk_init(ht, &hti, GFP_KERNEL);
160 161 162 163
	if (err) {
		pr_warn("Test failed: allocation error");
		return;
	}
164

165
	rhashtable_walk_start(&hti);
166

167 168 169 170 171 172 173 174 175
	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;
176 177
		}

178
		total++;
179 180
	}

181 182 183 184 185
	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);
186

187
	if (total != atomic_read(&ht->nelems) || total != entries)
188 189 190
		pr_warn("Test failed: Total count mismatch ^^^");
}

191 192
static s64 __init test_rhashtable(struct rhashtable *ht, struct test_obj *array,
				  unsigned int entries)
193 194 195
{
	struct test_obj *obj;
	int err;
196
	unsigned int i, insert_retries = 0;
197
	s64 start, end;
198 199 200

	/*
	 * Insertion Test:
201
	 * Insert entries into table with all keys even numbers
202
	 */
203 204 205
	pr_info("  Adding %d keys\n", entries);
	start = ktime_get_ns();
	for (i = 0; i < entries; i++) {
206
		struct test_obj *obj = &array[i];
207

208
		obj->value.id = i * 2;
209
		err = insert_retry(ht, obj, test_rht_params);
210 211 212
		if (err > 0)
			insert_retries += err;
		else if (err)
213
			return err;
214 215
	}

216 217 218
	if (insert_retries)
		pr_info("  %u insertions retried due to memory pressure\n",
			insert_retries);
219

220
	test_bucket_stats(ht, entries);
221
	rcu_read_lock();
222
	test_rht_lookup(ht, array, entries);
223 224
	rcu_read_unlock();

225
	test_bucket_stats(ht, entries);
226

227 228
	pr_info("  Deleting %d keys\n", entries);
	for (i = 0; i < entries; i++) {
229 230 231
		struct test_obj_val key = {
			.id = i * 2,
		};
232

233
		if (array[i].value.id != TEST_INSERT_FAIL) {
234 235
			obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
			BUG_ON(!obj);
236

237 238
			rhashtable_remove_fast(ht, &obj->node, test_rht_params);
		}
239 240

		cond_resched();
241 242
	}

243 244 245 246
	end = ktime_get_ns();
	pr_info("  Duration of test: %lld ns\n", end - start);

	return end - start;
247 248
}

249
static struct rhashtable ht;
250
static struct rhltable rhlt;
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

static int __init test_rhltable(unsigned int entries)
{
	struct test_obj_rhl *rhl_test_objects;
	unsigned long *obj_in_table;
	unsigned int i, j, k;
	int ret, err;

	if (entries == 0)
		entries = 1;

	rhl_test_objects = vzalloc(sizeof(*rhl_test_objects) * entries);
	if (!rhl_test_objects)
		return -ENOMEM;

	ret = -ENOMEM;
	obj_in_table = vzalloc(BITS_TO_LONGS(entries) * sizeof(unsigned long));
	if (!obj_in_table)
		goto out_free;

	/* nulls_base not supported in rhlist interface */
	test_rht_params.nulls_base = 0;
	err = rhltable_init(&rhlt, &test_rht_params);
	if (WARN_ON(err))
		goto out_free;

	k = prandom_u32();
	ret = 0;
	for (i = 0; i < entries; i++) {
		rhl_test_objects[i].value.id = k;
		err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node,
				      test_rht_params);
		if (WARN(err, "error %d on element %d\n", err, i))
			break;
		if (err == 0)
			set_bit(i, obj_in_table);
	}

	if (err)
		ret = err;

	pr_info("test %d add/delete pairs into rhlist\n", entries);
	for (i = 0; i < entries; i++) {
		struct rhlist_head *h, *pos;
		struct test_obj_rhl *obj;
		struct test_obj_val key = {
			.id = k,
		};
		bool found;

		rcu_read_lock();
		h = rhltable_lookup(&rhlt, &key, test_rht_params);
		if (WARN(!h, "key not found during iteration %d of %d", i, entries)) {
			rcu_read_unlock();
			break;
		}

		if (i) {
			j = i - 1;
			rhl_for_each_entry_rcu(obj, pos, h, list_node) {
				if (WARN(pos == &rhl_test_objects[j].list_node, "old element found, should be gone"))
					break;
			}
		}

		cond_resched_rcu();

		found = false;

		rhl_for_each_entry_rcu(obj, pos, h, list_node) {
			if (pos == &rhl_test_objects[i].list_node) {
				found = true;
				break;
			}
		}

		rcu_read_unlock();

		if (WARN(!found, "element %d not found", i))
			break;

		err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
		WARN(err, "rhltable_remove: err %d for iteration %d\n", err, i);
		if (err == 0)
			clear_bit(i, obj_in_table);
	}

	if (ret == 0 && err)
		ret = err;

	for (i = 0; i < entries; i++) {
		WARN(test_bit(i, obj_in_table), "elem %d allegedly still present", i);

		err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node,
				      test_rht_params);
		if (WARN(err, "error %d on element %d\n", err, i))
			break;
		if (err == 0)
			set_bit(i, obj_in_table);
	}

	pr_info("test %d random rhlist add/delete operations\n", entries);
	for (j = 0; j < entries; j++) {
		u32 i = prandom_u32_max(entries);
		u32 prand = prandom_u32();

		cond_resched();

		if (prand == 0)
			prand = prandom_u32();

		if (prand & 1) {
			prand >>= 1;
			continue;
		}

		err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
		if (test_bit(i, obj_in_table)) {
			clear_bit(i, obj_in_table);
			if (WARN(err, "cannot remove element at slot %d", i))
				continue;
		} else {
			if (WARN(err != -ENOENT, "removed non-existant element %d, error %d not %d",
			     i, err, -ENOENT))
				continue;
		}

		if (prand & 1) {
			prand >>= 1;
			continue;
		}

		err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
		if (err == 0) {
			if (WARN(test_and_set_bit(i, obj_in_table), "succeeded to insert same object %d", i))
				continue;
		} else {
			if (WARN(!test_bit(i, obj_in_table), "failed to insert object %d", i))
				continue;
		}

		if (prand & 1) {
			prand >>= 1;
			continue;
		}

		i = prandom_u32_max(entries);
		if (test_bit(i, obj_in_table)) {
			err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
			WARN(err, "cannot remove element at slot %d", i);
			if (err == 0)
				clear_bit(i, obj_in_table);
		} else {
			err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
			WARN(err, "failed to insert object %d", i);
			if (err == 0)
				set_bit(i, obj_in_table);
		}
	}

	for (i = 0; i < entries; i++) {
		cond_resched();
		err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
		if (test_bit(i, obj_in_table)) {
			if (WARN(err, "cannot remove element at slot %d", i))
				continue;
		} else {
			if (WARN(err != -ENOENT, "removed non-existant element, error %d not %d",
				 err, -ENOENT))
			continue;
		}
	}

	rhltable_destroy(&rhlt);
out_free:
	vfree(rhl_test_objects);
	vfree(obj_in_table);
	return ret;
}
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static int __init test_rhashtable_max(struct test_obj *array,
				      unsigned int entries)
{
	unsigned int i, insert_retries = 0;
	int err;

	test_rht_params.max_size = roundup_pow_of_two(entries / 8);
	err = rhashtable_init(&ht, &test_rht_params);
	if (err)
		return err;

	for (i = 0; i < ht.max_elems; i++) {
		struct test_obj *obj = &array[i];

		obj->value.id = i * 2;
		err = insert_retry(&ht, obj, test_rht_params);
		if (err > 0)
			insert_retries += err;
		else if (err)
			return err;
	}

	err = insert_retry(&ht, &array[ht.max_elems], test_rht_params);
	if (err == -E2BIG) {
		err = 0;
	} else {
		pr_info("insert element %u should have failed with %d, got %d\n",
				ht.max_elems, -E2BIG, err);
		if (err == 0)
			err = -1;
	}

	rhashtable_destroy(&ht);

	return err;
}

468 469
static int thread_lookup_test(struct thread_data *tdata)
{
470
	unsigned int entries = tdata->entries;
471 472 473 474
	int i, err = 0;

	for (i = 0; i < entries; i++) {
		struct test_obj *obj;
475 476 477 478
		struct test_obj_val key = {
			.id = i,
			.tid = tdata->id,
		};
479 480

		obj = rhashtable_lookup_fast(&ht, &key, test_rht_params);
481 482
		if (obj && (tdata->objs[i].value.id == TEST_INSERT_FAIL)) {
			pr_err("  found unexpected object %d-%d\n", key.tid, key.id);
483
			err++;
484 485
		} else if (!obj && (tdata->objs[i].value.id != TEST_INSERT_FAIL)) {
			pr_err("  object %d-%d not found!\n", key.tid, key.id);
486
			err++;
487 488 489
		} else if (obj && memcmp(&obj->value, &key, sizeof(key))) {
			pr_err("  wrong object returned (got %d-%d, expected %d-%d)\n",
			       obj->value.tid, obj->value.id, key.tid, key.id);
490 491
			err++;
		}
492 493

		cond_resched();
494 495 496 497 498 499
	}
	return err;
}

static int threadfunc(void *data)
{
500
	int i, step, err = 0, insert_retries = 0;
501 502 503 504 505 506
	struct thread_data *tdata = data;

	up(&prestart_sem);
	if (down_interruptible(&startup_sem))
		pr_err("  thread[%d]: down_interruptible failed\n", tdata->id);

507
	for (i = 0; i < tdata->entries; i++) {
508 509
		tdata->objs[i].value.id = i;
		tdata->objs[i].value.tid = tdata->id;
510
		err = insert_retry(&ht, &tdata->objs[i], test_rht_params);
511 512
		if (err > 0) {
			insert_retries += err;
513 514 515 516 517 518
		} else if (err) {
			pr_err("  thread[%d]: rhashtable_insert_fast failed\n",
			       tdata->id);
			goto out;
		}
	}
519 520 521
	if (insert_retries)
		pr_info("  thread[%d]: %u insertions retried due to memory pressure\n",
			tdata->id, insert_retries);
522 523 524 525 526 527 528 529 530

	err = thread_lookup_test(tdata);
	if (err) {
		pr_err("  thread[%d]: rhashtable_lookup_test failed\n",
		       tdata->id);
		goto out;
	}

	for (step = 10; step > 0; step--) {
531
		for (i = 0; i < tdata->entries; i += step) {
532
			if (tdata->objs[i].value.id == TEST_INSERT_FAIL)
533 534 535 536 537 538 539 540
				continue;
			err = rhashtable_remove_fast(&ht, &tdata->objs[i].node,
			                             test_rht_params);
			if (err) {
				pr_err("  thread[%d]: rhashtable_remove_fast failed\n",
				       tdata->id);
				goto out;
			}
541
			tdata->objs[i].value.id = TEST_INSERT_FAIL;
542 543

			cond_resched();
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
		}
		err = thread_lookup_test(tdata);
		if (err) {
			pr_err("  thread[%d]: rhashtable_lookup_test (2) failed\n",
			       tdata->id);
			goto out;
		}
	}
out:
	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule();
	}
	return err;
}

560 561
static int __init test_rht_init(void)
{
562
	unsigned int entries;
563
	int i, err, started_threads = 0, failed_threads = 0;
564
	u64 total_time = 0;
565 566
	struct thread_data *tdata;
	struct test_obj *objs;
567

568 569 570 571
	if (parm_entries < 0)
		parm_entries = 1;

	entries = min(parm_entries, MAX_ENTRIES);
572

573
	test_rht_params.automatic_shrinking = shrinking;
574
	test_rht_params.max_size = max_size ? : roundup_pow_of_two(entries);
575
	test_rht_params.nelem_hint = size;
576

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

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

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

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

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

597
		time = test_rhashtable(&ht, objs, entries);
598 599
		rhashtable_destroy(&ht);
		if (time < 0) {
600
			vfree(objs);
601 602 603 604 605 606 607
			pr_warn("Test failed: return code %lld\n", time);
			return -EINVAL;
		}

		total_time += time;
	}

608 609 610
	pr_info("test if its possible to exceed max_size %d: %s\n",
			test_rht_params.max_size, test_rhashtable_max(objs, entries) == 0 ?
			"no, ok" : "YES, failed");
611
	vfree(objs);
612

613 614
	do_div(total_time, runs);
	pr_info("Average test time: %llu\n", total_time);
615

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	if (!tcount)
		return 0;

	pr_info("Testing concurrent rhashtable access from %d threads\n",
	        tcount);
	sema_init(&prestart_sem, 1 - tcount);
	tdata = vzalloc(tcount * sizeof(struct thread_data));
	if (!tdata)
		return -ENOMEM;
	objs  = vzalloc(tcount * entries * sizeof(struct test_obj));
	if (!objs) {
		vfree(tdata);
		return -ENOMEM;
	}

631 632
	test_rht_params.max_size = max_size ? :
	                           roundup_pow_of_two(tcount * entries);
633 634 635 636 637 638 639 640 641 642
	err = rhashtable_init(&ht, &test_rht_params);
	if (err < 0) {
		pr_warn("Test failed: Unable to initialize hashtable: %d\n",
			err);
		vfree(tdata);
		vfree(objs);
		return -EINVAL;
	}
	for (i = 0; i < tcount; i++) {
		tdata[i].id = i;
643
		tdata[i].entries = entries;
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
		tdata[i].objs = objs + i * entries;
		tdata[i].task = kthread_run(threadfunc, &tdata[i],
		                            "rhashtable_thrad[%d]", i);
		if (IS_ERR(tdata[i].task))
			pr_err(" kthread_run failed for thread %d\n", i);
		else
			started_threads++;
	}
	if (down_interruptible(&prestart_sem))
		pr_err("  down interruptible failed\n");
	for (i = 0; i < tcount; i++)
		up(&startup_sem);
	for (i = 0; i < tcount; i++) {
		if (IS_ERR(tdata[i].task))
			continue;
		if ((err = kthread_stop(tdata[i].task))) {
			pr_warn("Test failed: thread %d returned: %d\n",
			        i, err);
			failed_threads++;
		}
	}
	rhashtable_destroy(&ht);
	vfree(tdata);
	vfree(objs);
668 669 670 671 672 673 674 675

	/*
	 * rhltable_remove is very expensive, default values can cause test
	 * to run for 2 minutes or more,  use a smaller number instead.
	 */
	err = test_rhltable(entries / 16);
	pr_info("Started %d threads, %d failed, rhltable test returns %d\n",
	        started_threads, failed_threads, err);
676
	return 0;
677 678
}

679 680 681 682
static void __exit test_rht_exit(void)
{
}

683
module_init(test_rht_init);
684
module_exit(test_rht_exit);
685 686

MODULE_LICENSE("GPL v2");