Debian Codegraph @

regressions/ck_ec/validate/ck_ec_smoke_test.c

raw
1 #include <assert.h>
2 #include <ck_ec.h>
3 #include <ck_limits.h>
4 #include <ck_stdbool.h>
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <sys/time.h>
8 #include <unistd.h>
9
10 #define TIME_MAX ((time_t)((1ULL << ((sizeof(time_t) * CHAR_BIT) - 1)) - 1))
11
12 #ifndef __linux__
13 /* Zero-initialize to mark the ops as unavailable. */
14 static const struct ck_ec_ops test_ops;
15 #else
16 #include <linux/futex.h>
17 #include <sys/syscall.h>
18 #include <time.h>
19
20 static int gettime(const struct ck_ec_ops *, struct timespec *out);
21 static void wake32(const struct ck_ec_ops *, const uint32_t *);
22 static void wait32(const struct ck_ec_wait_state *, const uint32_t *,
23 uint32_t, const struct timespec *);
24 static void wake64(const struct ck_ec_ops *, const uint64_t *);
25 static void wait64(const struct ck_ec_wait_state *, const uint64_t *,
26 uint64_t, const struct timespec *);
27
28 static const struct ck_ec_ops test_ops = {
29 .gettime = gettime,
30 .wait32 = wait32,
31 .wait64 = wait64,
32 .wake32 = wake32,
33 .wake64 = wake64
34 };
35
36 static int gettime(const struct ck_ec_ops *ops, struct timespec *out)
37 {
38 assert(ops == &test_ops);
39 return clock_gettime(CLOCK_MONOTONIC, out);
40 }
41
42 static void wait32(const struct ck_ec_wait_state *state,
43 const uint32_t *address, uint32_t expected,
44 const struct timespec *deadline)
45 {
46 assert(state->ops == &test_ops);
47 syscall(SYS_futex, address,
48 FUTEX_WAIT_BITSET, expected, deadline,
49 NULL, FUTEX_BITSET_MATCH_ANY, 0);
50 return;
51 }
52
53 static void wait64(const struct ck_ec_wait_state *state,
54 const uint64_t *address, uint64_t expected,
55 const struct timespec *deadline)
56 {
57 const void *low_half;
58
59 assert(state->ops == &test_ops);
60
61 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
62 low_half = address;
63 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
64 low_half = (uintptr_t)address + sizeof(uint32_t);
65 #else
66 # error "__BYTE_ORDER__ must be defined."
67 #endif
68
69 syscall(SYS_futex, low_half,
70 FUTEX_WAIT_BITSET, (uint32_t)expected, deadline,
71 NULL, FUTEX_BITSET_MATCH_ANY, 0);
72 return;
73 }
74
75 static void wake32(const struct ck_ec_ops *ops, const uint32_t *address)
76 {
77 assert(ops == &test_ops);
78 syscall(SYS_futex, address,
79 FUTEX_WAKE, INT_MAX,
80 /* ignored arguments */NULL, NULL, 0);
81 return;
82 }
83
84 static void wake64(const struct ck_ec_ops *ops, const uint64_t *address)
85 {
86 const void *low_half;
87
88 assert(ops == &test_ops);
89
90 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
91 low_half = address;
92 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
93 low_half = (uintptr_t)address + sizeof(uint32_t);
94 #else
95 # error "__BYTE_ORDER__ must be defined."
96 #endif
97
98 syscall(SYS_futex, low_half,
99 FUTEX_WAKE, INT_MAX,
100 /* ignored arguments */NULL, NULL, 0);
101 return;
102 }
103 #endif /* __linux__ */
104
105 static const struct ck_ec_mode sp = {
106 .ops = &test_ops,
107 .single_producer = true
108 };
109
110 static const struct ck_ec_mode mp = {
111 .ops = &test_ops,
112 .single_producer = false
113 };
114
115 static void test_update_counter_32(const struct ck_ec_mode *mode)
116 {
117 struct ck_ec32 ec = CK_EC_INITIALIZER;
118
119 assert(ck_ec_value(&ec) == 0);
120
121 ck_ec_inc(&ec, mode);
122 assert(ck_ec_value(&ec) == 1);
123
124 uint32_t old = ck_ec_add(&ec, mode, 42);
125 assert(old == 1);
126 assert(ck_ec_value(&ec) == 43);
127 return;
128 }
129
130 #ifdef CK_F_EC64
131 static void test_update_counter_64(const struct ck_ec_mode *mode)
132 {
133 struct ck_ec64 ec = CK_EC_INITIALIZER;
134
135 assert(ck_ec_value(&ec) == 0);
136
137 ck_ec_inc(&ec, mode);
138 assert(ck_ec_value(&ec) == 1);
139
140 uint64_t old = ck_ec_add(&ec, mode, 42);
141 assert(old == 1);
142 assert(ck_ec_value(&ec) == 43);
143 return;
144 }
145 #endif
146
147 static void test_deadline(void)
148 {
149 struct timespec deadline;
150
151 assert(ck_ec_deadline(&deadline, &sp, NULL) == 0);
152 assert(deadline.tv_sec == TIME_MAX);
153
154 {
155 const struct timespec timeout = {
156 .tv_sec = 1,
157 .tv_nsec = 1000
158 };
159 const struct timespec no_timeout = {
160 .tv_sec = 0
161 };
162 struct timespec now;
163
164 assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
165 assert(ck_ec_deadline(&now, &sp, &no_timeout) == 0);
166
167 double now_sec = now.tv_sec + 1e-9 * now.tv_nsec;
168 double deadline_sec = deadline.tv_sec + 1e-9 * deadline.tv_nsec;
169 assert(now_sec < deadline_sec);
170 assert(deadline_sec <= now_sec + 1 + 1000e-9);
171 }
172
173 {
174 const struct timespec timeout = {
175 .tv_sec = TIME_MAX - 1,
176 .tv_nsec = 1000
177 };
178
179 assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
180 assert(deadline.tv_sec == TIME_MAX);
181 }
182
183 return;
184 }
185
186 static void test_wait_32(void)
187 {
188 struct timespec deadline = { .tv_sec = 0 };
189 struct ck_ec32 ec;
190
191 ck_ec_init(&ec, 1);
192 assert(ck_ec_value(&ec) == 1);
193 assert(ck_ec_wait(&ec, &sp, 2, NULL) == 0);
194 assert(ck_ec_wait(&ec, &sp, 1, &deadline) == -1);
195
196 {
197 const struct timespec timeout = { .tv_nsec = 1 };
198
199 assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
200 assert(ck_ec_wait(&ec, &sp, 1, &deadline) == -1);
201 assert(ck_ec_has_waiters(&ec));
202 }
203
204 return;
205 }
206
207 #ifdef CK_F_EC64
208 static void test_wait_64(void)
209 {
210 struct timespec deadline = { .tv_sec = 0 };
211 struct ck_ec64 ec;
212
213 ck_ec_init(&ec, 0);
214 assert(ck_ec_value(&ec) == 0);
215 assert(ck_ec_wait(&ec, &sp, 1, NULL) == 0);
216 assert(ck_ec_wait(&ec, &sp, 0, &deadline) == -1);
217
218 {
219 const struct timespec timeout = { .tv_nsec = 1 };
220
221 assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
222 assert(ck_ec_wait(&ec, &sp, 0, &deadline) == -1);
223 assert(ck_ec_has_waiters(&ec));
224 }
225
226 return;
227 }
228 #endif
229
230 static int pred(const struct ck_ec_wait_state *state,
231 struct timespec *deadline)
232 {
233 double initial_ts = state->start.tv_sec +
234 1e-9 * state->start.tv_nsec;
235 int *count = state->data;
236
237 printf("pred wait: %f\n",
238 deadline->tv_sec + 1e-9 * deadline->tv_nsec - initial_ts);
239
240 if ((*count)++ < 3) {
241 return 0;
242 }
243
244 return (*count)++;
245 }
246
247 /*
248 * Check that pred's return value is correctly bubbled up,
249 * and that the event count is marked as having waiters.
250 */
251 static void test_wait_pred_32(void)
252 {
253 struct ck_ec32 ec = CK_EC_INITIALIZER;
254 int count = 0;
255
256 assert(!ck_ec_has_waiters(&ec));
257 assert(ck_ec_wait_pred(&ec, &sp, 0, pred, &count, NULL) == 4);
258 assert(ck_ec_has_waiters(&ec));
259 assert(count == 5);
260 return;
261 }
262
263 #ifdef CK_F_EC64
264 static int pred2(const struct ck_ec_wait_state *state,
265 struct timespec *deadline)
266 {
267 double initial_ts = state->start.tv_sec +
268 1e-9 * state->start.tv_nsec;
269 int *count = state->data;
270
271 printf("pred2 wait: %f\n",
272 deadline->tv_sec + 1e-9 * deadline->tv_nsec - initial_ts);
273
274 *deadline = state->now;
275 deadline->tv_sec++;
276
277 (*count)++;
278 return 0;
279 }
280
281 /*
282 * wait_pred_64 is nearly identical to _32. Now check that deadline
283 * overriding works.
284 */
285 static void test_wait_pred_64(void)
286 {
287 const struct timespec timeout = { .tv_sec = 5 };
288 struct timespec deadline;
289 struct ck_ec64 ec = CK_EC_INITIALIZER;
290 int count = 0;
291
292 assert(!ck_ec_has_waiters(&ec));
293 assert(ck_ec_deadline(&deadline, &sp, &timeout) == 0);
294 assert(ck_ec_wait_pred(&ec, &sp, 0, pred2, &count, &deadline) == -1);
295 assert(ck_ec_has_waiters(&ec));
296 assert(count == 5);
297 return;
298 }
299 #endif
300
301 static int woken = 0;
302
303 static void *test_threaded_32_waiter(void *data)
304 {
305 struct ck_ec32 *ec = data;
306
307 ck_ec_wait(ec, &sp, 0, NULL);
308 ck_pr_store_int(&woken, 1);
309 return NULL;
310 }
311
312 static void test_threaded_inc_32(const struct ck_ec_mode *mode)
313 {
314 struct ck_ec32 ec = CK_EC_INITIALIZER;
315 pthread_t waiter;
316
317 ck_pr_store_int(&woken, 0);
318
319 pthread_create(&waiter, NULL, test_threaded_32_waiter, &ec);
320 usleep(10000);
321
322 assert(ck_pr_load_int(&woken) == 0);
323 ck_ec_inc(&ec, mode);
324
325 pthread_join(waiter, NULL);
326 assert(ck_pr_load_int(&woken) == 1);
327 return;
328 }
329
330 static void test_threaded_add_32(const struct ck_ec_mode *mode)
331 {
332 struct ck_ec32 ec = CK_EC_INITIALIZER;
333 pthread_t waiter;
334
335 ck_pr_store_int(&woken, 0);
336
337 pthread_create(&waiter, NULL, test_threaded_32_waiter, &ec);
338 usleep(10000);
339
340 assert(ck_pr_load_int(&woken) == 0);
341 ck_ec_add(&ec, mode, 4);
342
343 pthread_join(waiter, NULL);
344 assert(ck_pr_load_int(&woken) == 1);
345 return;
346 }
347
348 #ifdef CK_F_EC64
349 static void *test_threaded_64_waiter(void *data)
350 {
351 struct ck_ec64 *ec = data;
352
353 ck_ec_wait(ec, &sp, 0, NULL);
354 ck_pr_store_int(&woken, 1);
355 return NULL;
356 }
357
358 static void test_threaded_inc_64(const struct ck_ec_mode *mode)
359 {
360 struct ck_ec64 ec = CK_EC_INITIALIZER;
361 pthread_t waiter;
362
363 ck_pr_store_int(&woken, 0);
364
365 pthread_create(&waiter, NULL, test_threaded_64_waiter, &ec);
366 usleep(10000);
367
368 assert(ck_pr_load_int(&woken) == 0);
369 ck_ec_inc(&ec, mode);
370
371 pthread_join(waiter, NULL);
372 assert(ck_pr_load_int(&woken) == 1);
373 return;
374 }
375
376 static void test_threaded_add_64(const struct ck_ec_mode *mode)
377 {
378 struct ck_ec64 ec = CK_EC_INITIALIZER;
379 pthread_t waiter;
380
381 ck_pr_store_int(&woken, 0);
382
383 pthread_create(&waiter, NULL, test_threaded_64_waiter, &ec);
384 usleep(10000);
385
386 assert(ck_pr_load_int(&woken) == 0);
387 ck_ec_add(&ec, mode, 4);
388
389 pthread_join(waiter, NULL);
390 assert(ck_pr_load_int(&woken) == 1);
391 return;
392 }
393 #endif
394
395 int main(int argc, char **argv)
396 {
397 (void)argc;
398 (void)argv;
399
400 if (test_ops.gettime == NULL ||
401 test_ops.wake32 == NULL ||
402 test_ops.wait32 == NULL) {
403 printf("No ck_ec ops for this platform. Trivial success.\n");
404 return 0;
405 }
406
407 test_update_counter_32(&sp);
408 #ifdef CK_F_EC64
409 test_update_counter_64(&sp);
410 #endif
411 printf("test_update_counter SP passed.\n");
412
413 test_update_counter_32(&mp);
414 #ifdef CK_F_EC64
415 test_update_counter_64(&mp);
416 #endif
417 printf("test_update_counter MP passed.\n");
418
419 test_deadline();
420 printf("test_deadline passed.\n");
421
422 test_wait_32();
423 #ifdef CK_F_EC64
424 test_wait_64();
425 #endif
426 printf("test_wait passed.\n");
427
428 test_wait_pred_32();
429 #ifdef CK_F_EC64
430 test_wait_pred_64();
431 #endif
432 printf("test_wait_pred passed.\n");
433
434 test_threaded_inc_32(&sp);
435 test_threaded_add_32(&sp);
436 #ifdef CK_F_EC64
437 test_threaded_inc_64(&sp);
438 test_threaded_add_64(&sp);
439 #endif
440 printf("test_threaded SP passed.\n");
441
442 test_threaded_inc_32(&mp);
443 test_threaded_add_32(&mp);
444 #ifdef CK_F_EC64
445 test_threaded_inc_64(&mp);
446 test_threaded_add_64(&mp);
447 #endif
448 printf("test_threaded MP passed.\n");
449 return 0;
450 }