summaryrefslogtreecommitdiff
path: root/tests/test_hashtab_thrash.c
blob: ee77c92c0305ff1ebc0b4c9662686a430e410c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, David M. Lee, II
 *
 * David M. Lee, II <dlee@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*
 *! \file \brief Thrash a hash table, for fun and profit.
 *
 * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
 *
 * Inspired by the original hashtest.c by Steve Murphy <murf@digium.com>.  This test runs
 * several threads manipulatings a concurrent hastab to see if they maintain
 * consistency. While the tests attempt to check consistency and error normally, threading
 * errors often result in segfaults.
 * \ingroup tests
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <pthread.h>
#include "asterisk/hashtab.h"
#include "asterisk/lock.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/time.h"
#include "asterisk/utils.h"

#define MAX_HASH_ENTRIES 30000
#define MAX_TEST_SECONDS 60

struct hash_test {
	/*! Unit under test */
	struct ast_hashtab *to_be_thrashed;
	/*! Number of entries to insert in the grow thread. */
	int max_grow;
	/*! Number of enteries added by the grow thread. */
	int grow_count;
	/*! Entries preloaded into the hashtab; to be deleted by the shrink thread */
	int preload;
	/*! When to give up on the tests */
	struct timeval deadline;
	/*! The actual test object */
	struct ast_test *test;
};

static int is_timed_out(struct hash_test const *data) {
	struct timeval now = ast_tvnow();
	int val = ast_tvdiff_us(data->deadline, now) < 0;
	if (val) {
		/* tv_usec is suseconds_t, which could be int or long */
		ast_test_status_update(data->test, "Now: %ld.%06ld Deadline: %ld.%06ld\n",
			now.tv_sec, (long)now.tv_usec,
			data->deadline.tv_sec, (long)data->deadline.tv_usec);
	}
	return val;
}

/*! /brief Create test element */
static char *ht_new(int i)
{
	const int buflen = 12;
	char *keybuf = ast_malloc(buflen);
	int needed;
	if (keybuf == NULL) {
		return NULL;
	}
	needed = snprintf(keybuf, buflen, "key%08x", (unsigned)i);
	ast_assert(needed + 1 <= buflen);
	return keybuf;
}

/*! /brief Free test element */
static void ht_delete(void *obj)
{
	ast_free(obj);
}

/*! /brief Grow the hash data as specified */
static void *hash_test_grow(void *d)
{
	struct hash_test *data = d;
	int i;

	for (i = 0; i < data->max_grow; ++i) {
		char *obj;
		if (is_timed_out(data)) {
			return "Growth timed out";
		}
		obj = ht_new(i);
		if (obj == NULL) {
			return "Allocation failed";
		}
		ast_hashtab_insert_immediate(data->to_be_thrashed, obj);
		ast_atomic_fetchadd_int(&data->grow_count, 1);
	}
	return NULL;
}

/*! Randomly lookup data in the hash */
static void *hash_test_lookup(void *d)
{
	struct hash_test *data = d;
	int max;
	unsigned seed = time(NULL);

	/* ast_atomic_fetchadd_int provide a memory fence so that the optimizer doesn't
	 * optimize away reads.
	 */
	while ((max = ast_atomic_fetchadd_int(&data->grow_count, 0)) < data->max_grow) {
		int i;
		char *obj;
		int is_in_hashtab;

		if (is_timed_out(data)) {
			return "Lookup timed out";
		}

		if (max == 0) {
			/* No data yet; yield and try again */
			sched_yield();
			continue;
		}

		/* Randomly lookup one object from the hash */
		i = rand_r(&seed) % max;
		obj = ht_new(i);
		if (obj == NULL) {
			return "Allocation failed.";
		}
		is_in_hashtab = (ast_hashtab_lookup(data->to_be_thrashed, obj) != NULL);
		ht_delete(obj);
		if (!is_in_hashtab) {
			return "key unexpectedly missing";
		}
	}

	return NULL;
}

/*! Delete entries from the hash */
static void *hash_test_shrink(void *d)
{
	const struct hash_test *data = d;
	int i;

	for (i = 1; i < data->preload; ++i) {
		char *obj = ht_new(-i);
		char *from_hashtab;
		int deleted;

		if (obj == NULL) {
			return "Allocation failed";
		}
		from_hashtab = ast_hashtab_remove_object_via_lookup(data->to_be_thrashed, obj);
		deleted = from_hashtab != NULL;

		ht_delete(obj);
		ht_delete(from_hashtab);
		if (!deleted) {
			return "could not delete object";
		}
		if (is_timed_out(data)) {
			return "Shrink timed out";
		}
	}
	return NULL;
}

/*! Continuously iterate through all the entries in the hash */
static void *hash_test_count(void *d)
{
	const struct hash_test *data = d;
	int count = 0;
	int last_count = 0;

	while (count < data->max_grow) {
		struct ast_hashtab_iter *it = ast_hashtab_start_write_traversal(data->to_be_thrashed);
		char *ht = ast_hashtab_next(it);
		last_count = count;
		count = 0;
		while (ht) {
			/* only count keys added by grow thread */
			if (strncmp(ht, "key0", 4) == 0) {
				++count;
			}
			ht = ast_hashtab_next(it);
		}
		ast_hashtab_end_traversal(it);

		if (last_count == count) {
			/* Give other threads ample chance to run, note that using sched_yield here does not
			 * provide enough of a chance and can cause this thread to starve others.
			 */
			usleep(1);
		} else if (last_count > count) {
			/* Make sure the hashtable never shrinks */
			return "hashtab unexpectedly shrank";
		}

		if (is_timed_out(data)) {
			return "Count timed out";
		}
	}

	/* Successfully iterated over all of the expected elements */
	return NULL;
}

AST_TEST_DEFINE(hash_test)
{
	enum ast_test_result_state res = AST_TEST_PASS;
	struct hash_test data = {};
	pthread_t grow_thread, count_thread, lookup_thread, shrink_thread;
	void *thread_results;
	int i;

	switch (cmd) {
	case TEST_INIT:
		info->name = "thrash";
		info->category = "/main/hashtab/";
		info->summary = "Testing hashtab concurrency";
		info->description = "Test hashtab concurrency correctness.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	ast_test_status_update(test, "Executing hash concurrency test...\n");
	data.test = test;
	data.preload = MAX_HASH_ENTRIES / 2;
	data.max_grow = MAX_HASH_ENTRIES - data.preload;
	data.deadline = ast_tvadd(ast_tvnow(), ast_tv(MAX_TEST_SECONDS, 0));
	data.to_be_thrashed = ast_hashtab_create(MAX_HASH_ENTRIES / 100,
		ast_hashtab_compare_strings_nocase, ast_hashtab_resize_java,
		ast_hashtab_newsize_java, ast_hashtab_hash_string_nocase, 1);

	if (data.to_be_thrashed == NULL) {
		ast_test_status_update(test, "Allocation failed\n");
		/* Nothing needs to be freed; early return is fine */
		return AST_TEST_FAIL;
	}


	/* preload with data to delete */
	for (i = 1; i < data.preload; ++i) {
		char *obj = ht_new(-i);
		if (obj == NULL) {
			ast_test_status_update(test, "Allocation failed\n");
			ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
			return AST_TEST_FAIL;
		}
		ast_hashtab_insert_immediate(data.to_be_thrashed, obj);
	}

	/* add data.max_grow entries to the hashtab */
	ast_pthread_create(&grow_thread, NULL, hash_test_grow, &data);
	/* continually count the keys added by the grow thread */
	ast_pthread_create(&count_thread, NULL, hash_test_count, &data);
	/* continually lookup keys added by the grow thread */
	ast_pthread_create(&lookup_thread, NULL, hash_test_lookup, &data);
	/* delete all keys preloaded into the hashtab */
	ast_pthread_create(&shrink_thread, NULL, hash_test_shrink, &data);

	pthread_join(grow_thread, &thread_results);
	if (thread_results != NULL) {
		ast_test_status_update(test, "Growth thread failed: %s\n",
			(char *)thread_results);
		res = AST_TEST_FAIL;
	}

	pthread_join(count_thread, &thread_results);
	if (thread_results != NULL) {
		ast_test_status_update(test, "Count thread failed: %s\n",
			(char *)thread_results);
		res = AST_TEST_FAIL;
	}

	pthread_join(lookup_thread, &thread_results);
	if (thread_results != NULL) {
		ast_test_status_update(test, "Lookup thread failed: %s\n",
			(char *)thread_results);
		res = AST_TEST_FAIL;
	}

	pthread_join(shrink_thread, &thread_results);
	if (thread_results != NULL) {
		ast_test_status_update(test, "Shrink thread failed: %s\n",
			(char *)thread_results);
		res = AST_TEST_FAIL;
	}

	if (ast_hashtab_size(data.to_be_thrashed) != data.max_grow) {
		ast_test_status_update(test,
			"Invalid hashtab size. Expected: %d, Actual: %d\n",
			data.max_grow, ast_hashtab_size(data.to_be_thrashed));
		res = AST_TEST_FAIL;
	}

	ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(hash_test);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(hash_test);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hash test");