summaryrefslogtreecommitdiffstats
path: root/tests/lib/test_atomlist.c
blob: b50216cf929224a59e8fbcc9345001df0941c269 (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
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
// SPDX-License-Identifier: ISC
/*
 * Copyright (c) 2016-2018  David Lamparter, for NetDEF, Inc.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>

#include "atomlist.h"
#include "seqlock.h"
#include "monotime.h"
#include "printfrr.h"

/*
 * maybe test:
 * - alist_del_hint
 * - alist_next_safe
 * - asort_del_hint
 * - asort_next_safe
 */

static struct seqlock sqlo;

PREDECL_ATOMLIST(alist);
PREDECL_ATOMSORT_UNIQ(asort);
struct item {
	uint64_t val1;
	struct alist_item chain;
	struct asort_item sortc;
	uint64_t val2;
};
DECLARE_ATOMLIST(alist, struct item, chain);

static int icmp(const struct item *a, const struct item *b);
DECLARE_ATOMSORT_UNIQ(asort, struct item, sortc, icmp);

static int icmp(const struct item *a, const struct item *b)
{
	if (a->val1 > b->val1)
		return 1;
	if (a->val1 < b->val1)
		return -1;
	return 0;
}

#define NITEM 10000
struct item itm[NITEM];

static struct alist_head ahead;
static struct asort_head shead;

#define NTHREADS 4
static struct testthread {
	pthread_t pt;
	struct seqlock sqlo;
	size_t counter, nullops;
} thr[NTHREADS];

struct testrun {
	struct testrun *next;
	int lineno;
	const char *desc;
	ssize_t prefill;
	bool sorted;
	void (*func)(unsigned int offset);
};
struct testrun *runs = NULL;

#define NOCLEAR -1

#define deftestrun(name, _desc, _prefill, _sorted) \
static void trfunc_##name(unsigned int offset); \
struct testrun tr_##name = { \
	.desc = _desc, \
	.lineno = __LINE__, \
	.prefill = _prefill, \
	.func = &trfunc_##name, \
	.sorted = _sorted }; \
static void __attribute__((constructor)) trsetup_##name(void) \
{ \
	struct testrun **inspos = &runs; \
	while (*inspos && (*inspos)->lineno < tr_##name.lineno) \
		inspos = &(*inspos)->next; \
	tr_##name.next = *inspos; \
	*inspos = &tr_##name; \
} \
static void trfunc_##name(unsigned int offset) \
{ \
	size_t i = 0, n = 0;

#define endtestrun \
	thr[offset].counter = i; \
	thr[offset].nullops = n; \
}

deftestrun(add, "add vs. add", 0, false)
	for (; i < NITEM / NTHREADS; i++)
		alist_add_head(&ahead, &itm[i * NTHREADS + offset]);
endtestrun

deftestrun(del, "del vs. del", NOCLEAR, false)
	for (; i < NITEM / NTHREADS / 10; i++)
		alist_del(&ahead, &itm[i * NTHREADS + offset]);
endtestrun

deftestrun(addtail, "add_tail vs. add_tail", 0, false)
	for (; i < NITEM / NTHREADS; i++)
		alist_add_tail(&ahead, &itm[i * NTHREADS + offset]);
endtestrun

deftestrun(pop, "pop vs. pop", NOCLEAR, false)
	for (; i < NITEM / NTHREADS; )
		if (alist_pop(&ahead))
			i++;
		else
			n++;
endtestrun

deftestrun(headN_vs_pop1, "add_head(N) vs. pop(1)", 1, false);
	if (offset == 0) {
		struct item *dr = NULL;

		for (i = n = 0; i < NITEM; ) {
			dr = alist_pop(&ahead);
			if (dr)
				i++;
			else
				n++;
		}
	} else {
		for (i = offset; i < NITEM; i += NTHREADS)
			alist_add_head(&ahead, &itm[i]);
		i = 0;
	}
endtestrun

deftestrun(head1_vs_popN, "add_head(1) vs. pop(N)", 0, false);
	if (offset < NTHREADS - 1) {
		struct item *dr = NULL;

		for (i = n = 0; i < NITEM / NTHREADS; ) {
			dr = alist_pop(&ahead);
			if (dr)
				i++;
			else
				n++;
		}
	} else {
		for (i = 0; i < NITEM; i++)
			alist_add_head(&ahead, &itm[i]);
		i = 0;
	}
endtestrun

deftestrun(headN_vs_popN, "add_head(N) vs. pop(N)", NTHREADS / 2, false)
	if (offset < NTHREADS / 2) {
		struct item *dr = NULL;

		for (i = n = 0; i < NITEM * 2 / NTHREADS; ) {
			dr = alist_pop(&ahead);
			if (dr)
				i++;
			else
				n++;
		}
	} else {
		for (i = offset; i < NITEM; i += NTHREADS)
			alist_add_head(&ahead, &itm[i]);
		i = 0;
	}
endtestrun

deftestrun(tailN_vs_pop1, "add_tail(N) vs. pop(1)", 1, false)
	if (offset == 0) {
		struct item *dr = NULL;

		for (i = n = 0; i < NITEM - (NITEM / NTHREADS); ) {
			dr = alist_pop(&ahead);
			if (dr)
				i++;
			else
				n++;
		}
	} else {
		for (i = offset; i < NITEM; i += NTHREADS)
			alist_add_tail(&ahead, &itm[i]);
		i = 0;
	}
endtestrun

deftestrun(tail1_vs_popN, "add_tail(1) vs. pop(N)", 0, false)
	if (offset < NTHREADS - 1) {
		struct item *dr = NULL;

		for (i = n = 0; i < NITEM / NTHREADS; ) {
			dr = alist_pop(&ahead);
			if (dr)
				i++;
			else
				n++;
		}
	} else {
		for (i = 0; i < NITEM; i++)
			alist_add_tail(&ahead, &itm[i]);
		i = 0;
	}
endtestrun

deftestrun(sort_add, "add_sort vs. add_sort", 0, true)
	for (; i < NITEM / NTHREADS / 10; i++)
		asort_add(&shead, &itm[i * NTHREADS + offset]);
endtestrun

deftestrun(sort_del, "del_sort vs. del_sort", NOCLEAR, true)
	for (; i < NITEM / NTHREADS / 10; i++)
		asort_del(&shead, &itm[i * NTHREADS + offset]);
endtestrun

deftestrun(sort_add_del, "add_sort vs. del_sort", NTHREADS / 2, true)
	if (offset < NTHREADS / 2) {
		for (; i < NITEM / NTHREADS / 10; i++)
			asort_del(&shead, &itm[i * NTHREADS + offset]);
	} else {
		for (; i < NITEM / NTHREADS / 10; i++)
			asort_add(&shead, &itm[i * NTHREADS + offset]);
	}
endtestrun

static void *thr1func(void *arg)
{
	struct testthread *p = arg;
	unsigned int offset = (unsigned int)(p - &thr[0]);
	seqlock_val_t sv;
	struct testrun *tr;

	for (tr = runs; tr; tr = tr->next) {
		sv = seqlock_bump(&p->sqlo) - SEQLOCK_INCR;
		seqlock_wait(&sqlo, sv);

		tr->func(offset);
	}
	seqlock_bump(&p->sqlo);

	return NULL;
}

static void clear_list(size_t prefill)
{
	size_t i;

	memset(&ahead, 0, sizeof(ahead));
	memset(&shead, 0, sizeof(shead));
	memset(itm, 0, sizeof(itm));
	for (i = 0; i < NITEM; i++) {
		itm[i].val1 = itm[i].val2 = i;
		if ((i % NTHREADS) < prefill) {
			alist_add_tail(&ahead, &itm[i]);
			asort_add(&shead, &itm[i]);
		}
	}
}

static void run_tr(struct testrun *tr)
{
	const char *desc = tr->desc;
	struct timeval tv;
	int64_t delta;
	seqlock_val_t sv;
	size_t c = 0, s = 0, n = 0;
	struct item *item, *prev, dummy;

	printfrr("[%02u] %35s %s\n", seqlock_cur(&sqlo) >> 2, "", desc);
	fflush(stdout);

	if (tr->prefill != NOCLEAR)
		clear_list(tr->prefill);

	monotime(&tv);
	sv = seqlock_bump(&sqlo) - SEQLOCK_INCR;
	for (size_t i = 0; i < NTHREADS; i++) {
		seqlock_wait(&thr[i].sqlo, seqlock_cur(&sqlo));
		s += thr[i].counter;
		n += thr[i].nullops;
		thr[i].counter = 0;
		thr[i].nullops = 0;
	}

	delta = monotime_since(&tv, NULL);
	if (tr->sorted) {
		uint64_t prevval = 0;

		frr_each(asort, &shead, item) {
			assert(item->val1 >= prevval);
			prevval = item->val1;
			c++;
		}
		assert(c == asort_count(&shead));
	} else {
		prev = &dummy;
		frr_each(alist, &ahead, item) {
			assert(item != prev);
			prev = item;
			c++;
			assert(c <= NITEM);
		}
		assert(c == alist_count(&ahead));
	}
	printfrr("\033[1A[%02u] %9"PRId64"us c=%5zu s=%5zu n=%5zu %s\n",
		sv >> 2, delta, c, s, n, desc);
}

#ifdef BASIC_TESTS
static void dump(const char *lbl)
{
	struct item *item, *safe;
	size_t ctr = 0;

	printfrr("dumping %s:\n", lbl);
	frr_each_safe(alist, &ahead, item) {
		printfrr("%s %3zu %p %3"PRIu64" %3"PRIu64"\n", lbl, ctr++,
				(void *)item, item->val1, item->val2);
	}
}

static void basic_tests(void)
{
	size_t i;

	memset(&ahead, 0, sizeof(ahead));
	memset(itm, 0, sizeof(itm));
	for (i = 0; i < NITEM; i++)
		itm[i].val1 = itm[i].val2 = i;

	assert(alist_first(&ahead) == NULL);
	dump("");
	alist_add_head(&ahead, &itm[0]);
	dump("");
	alist_add_head(&ahead, &itm[1]);
	dump("");
	alist_add_tail(&ahead, &itm[2]);
	dump("");
	alist_add_tail(&ahead, &itm[3]);
	dump("");
	alist_del(&ahead, &itm[1]);
	dump("");
	printfrr("POP: %p\n", alist_pop(&ahead));
	dump("");
	printfrr("POP: %p\n", alist_pop(&ahead));
	printfrr("POP: %p\n", alist_pop(&ahead));
	printfrr("POP: %p\n", alist_pop(&ahead));
	printfrr("POP: %p\n", alist_pop(&ahead));
	dump("");
}
#else
#define basic_tests() do { } while (0)
#endif

int main(int argc, char **argv)
{
	size_t i;

	basic_tests();

	seqlock_init(&sqlo);
	seqlock_acquire_val(&sqlo, SEQLOCK_STARTVAL);

	for (i = 0; i < NTHREADS; i++) {
		seqlock_init(&thr[i].sqlo);
		seqlock_acquire(&thr[i].sqlo, &sqlo);
		thr[i].counter = 0;
		thr[i].nullops = 0;

		pthread_create(&thr[i].pt, NULL, thr1func, &thr[i]);
	}

	struct testrun *tr;

	for (tr = runs; tr; tr = tr->next)
		run_tr(tr);

	for (i = 0; i < NTHREADS; i++)
		pthread_join(thr[i].pt, NULL);

	return 0;
}