summaryrefslogtreecommitdiffstats
path: root/regressions/ck_ec/validate/ck_ec_smoke_test.c
blob: 3aca1626bd20a5fd5bcaa8686fb404440241a11a (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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <assert.h>
#include <ck_ec.h>
#include <ck_limits.h>
#include <ck_stdbool.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

#define TIME_MAX ((time_t)((1ULL << ((sizeof(time_t) * CHAR_BIT) - 1)) - 1))

#ifndef __linux__
/* Zero-initialize to mark the ops as unavailable. */
static const struct ck_ec_ops test_ops;
#else
#include <linux/futex.h>
#include <sys/syscall.h>
#include <time.h>

static int gettime(const struct ck_ec_ops *, struct timespec *out);
static void wake32(const struct ck_ec_ops *, const uint32_t *);
static void wait32(const struct ck_ec_wait_state *, const uint32_t *,
		   uint32_t, const struct timespec *);
static void wake64(const struct ck_ec_ops *, const uint64_t *);
static void wait64(const struct ck_ec_wait_state *, const uint64_t *,
		   uint64_t, const struct timespec *);

static const struct ck_ec_ops test_ops = {
	.gettime = gettime,
	.wait32 = wait32,
	.wait64 = wait64,
	.wake32 = wake32,
	.wake64 = wake64
};

static int gettime(const struct ck_ec_ops *ops, struct timespec *out)
{
	assert(ops == &test_ops);
	return clock_gettime(CLOCK_MONOTONIC, out);
}

static void wait32(const struct ck_ec_wait_state *state,
		   const uint32_t *address, uint32_t expected,
		   const struct timespec *deadline)
{
	assert(state->ops == &test_ops);
	syscall(SYS_futex, address,
		FUTEX_WAIT_BITSET, expected, deadline,
		NULL, FUTEX_BITSET_MATCH_ANY, 0);
	return;
}

static void wait64(const struct ck_ec_wait_state *state,
		   const uint64_t *address, uint64_t expected,
		   const struct timespec *deadline)
{
	const void *low_half;

	assert(state->ops == &test_ops);

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	low_half = address;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
	low_half = (uintptr_t)address + sizeof(uint32_t);
#else
# error "__BYTE_ORDER__ must be defined."
#endif

	syscall(SYS_futex, low_half,
		FUTEX_WAIT_BITSET, (uint32_t)expected, deadline,
		NULL, FUTEX_BITSET_MATCH_ANY, 0);
	return;
}

static void wake32(const struct ck_ec_ops *ops, const uint32_t *address)
{
	assert(ops == &test_ops);
	syscall(SYS_futex, address,
		FUTEX_WAKE, INT_MAX,
		/* ignored arguments */NULL, NULL, 0);
	return;
}

static void wake64(const struct ck_ec_ops *ops, const uint64_t *address)
{
	const void *low_half;

	assert(ops == &test_ops);

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	low_half = address;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
	low_half = (uintptr_t)address + sizeof(uint32_t);
#else
# error "__BYTE_ORDER__ must be defined."
#endif

	syscall(SYS_futex, low_half,
		FUTEX_WAKE, INT_MAX,
		/* ignored arguments */NULL, NULL, 0);
	return;
}
#endif /* __linux__ */

static const struct ck_ec_mode sp = {
	.ops = &test_ops,
	.single_producer = true
};

static const struct ck_ec_mode mp = {
	.ops = &test_ops,
	.single_producer = false
};

static void test_update_counter_32(const struct ck_ec_mode *mode)
{
	struct ck_ec32 ec = CK_EC_INITIALIZER;

	assert(ck_ec_value(&ec) == 0);

	ck_ec_inc(&ec, mode);
	assert(ck_ec_value(&ec) == 1);

	uint32_t old = ck_ec_add(&ec, mode, 42);
	assert(old == 1);
	assert(ck_ec_value(&ec) == 43);
	return;
}

#ifdef CK_F_EC64
static void test_update_counter_64(const struct ck_ec_mode *mode)
{
	struct ck_ec64 ec = CK_EC_INITIALIZER;

	assert(ck_ec_value(&ec) == 0);

	ck_ec_inc(&ec, mode);
	assert(ck_ec_value(&ec) == 1);

	uint64_t old = ck_ec_add(&ec, mode, 42);
	assert(old == 1);
	assert(ck_ec_value(&ec) == 43);
	return;
}
#endif

static void test_deadline(void)
{
	struct timespec deadline;

	assert(ck_ec_deadline(&deadline, &sp, NULL) == 0);
	assert(deadline.tv_sec == TIME_MAX);

	{
		const struct timespec timeout = {
			.tv_sec = 1,
			.tv_nsec = 1000
		};
		const struct timespec no_timeout = {
			.tv_sec = 0
		};
		struct timespec now;

		assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
		assert(ck_ec_deadline(&now, &sp, &no_timeout) == 0);

		double now_sec = now.tv_sec + 1e-9 * now.tv_nsec;
		double deadline_sec = deadline.tv_sec + 1e-9 * deadline.tv_nsec;
		assert(now_sec < deadline_sec);
		assert(deadline_sec <= now_sec + 1 + 1000e-9);
	}

	{
		const struct timespec timeout = {
			.tv_sec = TIME_MAX - 1,
			.tv_nsec = 1000
		};

		assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
		assert(deadline.tv_sec == TIME_MAX);
	}

	return;
}

static void test_wait_32(void)
{
	struct timespec deadline = { .tv_sec = 0 };
	struct ck_ec32 ec;

	ck_ec_init(&ec, 1);
	assert(ck_ec_value(&ec) == 1);
	assert(ck_ec_wait(&ec, &sp, 2, NULL) == 0);
	assert(ck_ec_wait(&ec, &sp, 1, &deadline) == -1);

	{
		const struct timespec timeout = { .tv_nsec = 1 };

		assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
		assert(ck_ec_wait(&ec, &sp, 1, &deadline) == -1);
		assert(ck_ec_has_waiters(&ec));
	}

	return;
}

#ifdef CK_F_EC64
static void test_wait_64(void)
{
	struct timespec deadline = { .tv_sec = 0 };
	struct ck_ec64 ec;

	ck_ec_init(&ec, 0);
	assert(ck_ec_value(&ec) == 0);
	assert(ck_ec_wait(&ec, &sp, 1, NULL) == 0);
	assert(ck_ec_wait(&ec, &sp, 0, &deadline) == -1);

	{
		const struct timespec timeout = { .tv_nsec = 1 };

		assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
		assert(ck_ec_wait(&ec, &sp, 0, &deadline) == -1);
		assert(ck_ec_has_waiters(&ec));
	}

	return;
}
#endif

static int pred(const struct ck_ec_wait_state *state,
		struct timespec *deadline)
{
	double initial_ts = state->start.tv_sec +
	    1e-9 * state->start.tv_nsec;
	int *count = state->data;

	printf("pred wait: %f\n",
	       deadline->tv_sec + 1e-9 * deadline->tv_nsec - initial_ts);

	if ((*count)++ < 3) {
		return 0;
	}

	return (*count)++;
}

/*
 * Check that pred's return value is correctly bubbled up,
 * and that the event count is marked as having waiters.
 */
static void test_wait_pred_32(void)
{
	struct ck_ec32 ec = CK_EC_INITIALIZER;
	int count = 0;

	assert(!ck_ec_has_waiters(&ec));
	assert(ck_ec_wait_pred(&ec, &sp, 0, pred, &count, NULL) == 4);
	assert(ck_ec_has_waiters(&ec));
	assert(count == 5);
	return;
}

#ifdef CK_F_EC64
static int pred2(const struct ck_ec_wait_state *state,
		 struct timespec *deadline)
{
	double initial_ts = state->start.tv_sec +
	    1e-9 * state->start.tv_nsec;
	int *count = state->data;

	printf("pred2 wait: %f\n",
	       deadline->tv_sec + 1e-9 * deadline->tv_nsec - initial_ts);

	*deadline = state->now;
	deadline->tv_sec++;

	(*count)++;
	return 0;
}

/*
 * wait_pred_64 is nearly identical to _32. Now check that deadline
 * overriding works.
 */
static void test_wait_pred_64(void)
{
	const struct timespec timeout = { .tv_sec = 5 };
	struct timespec deadline;
	struct ck_ec64 ec = CK_EC_INITIALIZER;
	int count = 0;

	assert(!ck_ec_has_waiters(&ec));
	assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
	assert(ck_ec_wait_pred(&ec, &sp, 0, pred2, &count, &deadline) == -1);
	assert(ck_ec_has_waiters(&ec));
	assert(count == 5);
	return;
}
#endif

static int woken = 0;

static void *test_threaded_32_waiter(void *data)
{
	struct ck_ec32 *ec = data;

	ck_ec_wait(ec, &sp, 0, NULL);
	ck_pr_store_int(&woken, 1);
	return NULL;
}

static void test_threaded_inc_32(const struct ck_ec_mode *mode)
{
	struct ck_ec32 ec = CK_EC_INITIALIZER;
	pthread_t waiter;

	ck_pr_store_int(&woken, 0);

	pthread_create(&waiter, NULL, test_threaded_32_waiter, &ec);
	usleep(10000);

	assert(ck_pr_load_int(&woken) == 0);
	ck_ec_inc(&ec, mode);

	pthread_join(waiter, NULL);
	assert(ck_pr_load_int(&woken) == 1);
	return;
}

static void test_threaded_add_32(const struct ck_ec_mode *mode)
{
	struct ck_ec32 ec = CK_EC_INITIALIZER;
	pthread_t waiter;

	ck_pr_store_int(&woken, 0);

	pthread_create(&waiter, NULL, test_threaded_32_waiter, &ec);
	usleep(10000);

	assert(ck_pr_load_int(&woken) == 0);
	ck_ec_add(&ec, mode, 4);

	pthread_join(waiter, NULL);
	assert(ck_pr_load_int(&woken) == 1);
	return;
}

#ifdef CK_F_EC64
static void *test_threaded_64_waiter(void *data)
{
	struct ck_ec64 *ec = data;

	ck_ec_wait(ec, &sp, 0, NULL);
	ck_pr_store_int(&woken, 1);
	return NULL;
}

static void test_threaded_inc_64(const struct ck_ec_mode *mode)
{
	struct ck_ec64 ec = CK_EC_INITIALIZER;
	pthread_t waiter;

	ck_pr_store_int(&woken, 0);

	pthread_create(&waiter, NULL, test_threaded_64_waiter, &ec);
	usleep(10000);

	assert(ck_pr_load_int(&woken) == 0);
	ck_ec_inc(&ec, mode);

	pthread_join(waiter, NULL);
	assert(ck_pr_load_int(&woken) == 1);
	return;
}

static void test_threaded_add_64(const struct ck_ec_mode *mode)
{
	struct ck_ec64 ec = CK_EC_INITIALIZER;
	pthread_t waiter;

	ck_pr_store_int(&woken, 0);

	pthread_create(&waiter, NULL, test_threaded_64_waiter, &ec);
	usleep(10000);

	assert(ck_pr_load_int(&woken) == 0);
	ck_ec_add(&ec, mode, 4);

	pthread_join(waiter, NULL);
	assert(ck_pr_load_int(&woken) == 1);
	return;
}
#endif

int main(int argc, char **argv)
{
	(void)argc;
	(void)argv;

	if (test_ops.gettime == NULL ||
	    test_ops.wake32 == NULL ||
	    test_ops.wait32 == NULL) {
		printf("No ck_ec ops for this platform. Trivial success.\n");
		return 0;
	}

	test_update_counter_32(&sp);
#ifdef CK_F_EC64
	test_update_counter_64(&sp);
#endif
	printf("test_update_counter SP passed.\n");

	test_update_counter_32(&mp);
#ifdef CK_F_EC64
	test_update_counter_64(&mp);
#endif
	printf("test_update_counter MP passed.\n");

	test_deadline();
	printf("test_deadline passed.\n");

	test_wait_32();
#ifdef CK_F_EC64
	test_wait_64();
#endif
	printf("test_wait passed.\n");

	test_wait_pred_32();
#ifdef CK_F_EC64
	test_wait_pred_64();
#endif
	printf("test_wait_pred passed.\n");

	test_threaded_inc_32(&sp);
	test_threaded_add_32(&sp);
#ifdef CK_F_EC64
	test_threaded_inc_64(&sp);
	test_threaded_add_64(&sp);
#endif
	printf("test_threaded SP passed.\n");

	test_threaded_inc_32(&mp);
	test_threaded_add_32(&mp);
#ifdef CK_F_EC64
	test_threaded_inc_64(&mp);
	test_threaded_add_64(&mp);
#endif
	printf("test_threaded MP passed.\n");
	return 0;
}