1 | /*
|
---|
2 | * Copyright (c) 2012 Adam Hraska
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <test.h>
|
---|
30 | #include <arch.h>
|
---|
31 | #include <atomic.h>
|
---|
32 | #include <print.h>
|
---|
33 | #include <proc/thread.h>
|
---|
34 | #include <macros.h>
|
---|
35 | #include <str.h>
|
---|
36 |
|
---|
37 | #include <synch/rcu.h>
|
---|
38 |
|
---|
39 | #include "abi/errno.h"
|
---|
40 | #include "time/delay.h"
|
---|
41 |
|
---|
42 | #define MAX_THREADS 32
|
---|
43 |
|
---|
44 | static int one_idx = 0;
|
---|
45 | static thread_t *thread[MAX_THREADS] = {0};
|
---|
46 |
|
---|
47 | typedef struct {
|
---|
48 | rcu_item_t rcu;
|
---|
49 | bool exited;
|
---|
50 | } exited_t;
|
---|
51 |
|
---|
52 | /* Callback raced with preexisting readers. */
|
---|
53 | #define ERACE 123
|
---|
54 | /* Waited for too long for the callback to exit; consider it lost. */
|
---|
55 | #define ECBLOST 432
|
---|
56 |
|
---|
57 | /*-------------------------------------------------------------------*/
|
---|
58 | static void wait_for_cb_exit(size_t secs, exited_t *p, int *presult)
|
---|
59 | {
|
---|
60 | size_t loops = 0;
|
---|
61 | /* 4 secs max */
|
---|
62 | size_t loop_ms_sec = 500;
|
---|
63 | size_t max_loops = ((secs * 1000 + loop_ms_sec - 1) / loop_ms_sec);
|
---|
64 |
|
---|
65 | while (loops < max_loops && !p->exited) {
|
---|
66 | ++loops;
|
---|
67 | thread_usleep(loop_ms_sec * 1000);
|
---|
68 | TPRINTF(".");
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (!p->exited) {
|
---|
72 | *presult = ECBLOST;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | static size_t get_thread_cnt(void)
|
---|
77 | {
|
---|
78 | return min(MAX_THREADS, config.cpu_active * 4);
|
---|
79 | }
|
---|
80 |
|
---|
81 | static void run_thread(size_t k, void (*func)(void*), void *arg)
|
---|
82 | {
|
---|
83 | ASSERT(thread[k] == NULL);
|
---|
84 |
|
---|
85 | thread[k] = thread_create(func, arg, TASK, THREAD_FLAG_NONE,
|
---|
86 | "test-rcu-thread");
|
---|
87 |
|
---|
88 | if(thread[k]) {
|
---|
89 | /* Distribute evenly. */
|
---|
90 | thread_wire(thread[k], &cpus[k % config.cpu_active]);
|
---|
91 | thread_ready(thread[k]);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | static void run_all(void (*func)(void*))
|
---|
96 | {
|
---|
97 | size_t thread_cnt = get_thread_cnt();
|
---|
98 |
|
---|
99 | one_idx = 0;
|
---|
100 |
|
---|
101 | for (size_t i = 0; i < thread_cnt; ++i) {
|
---|
102 | run_thread(i, func, 0);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void join_all(void)
|
---|
107 | {
|
---|
108 | size_t thread_cnt = get_thread_cnt();
|
---|
109 |
|
---|
110 | one_idx = 0;
|
---|
111 |
|
---|
112 | for (size_t i = 0; i < thread_cnt; ++i) {
|
---|
113 | if (thread[i]) {
|
---|
114 | bool joined = false;
|
---|
115 | do {
|
---|
116 | int ret = thread_join_timeout(thread[i], 5 * 1000 * 1000, 0);
|
---|
117 | joined = (ret != ESYNCH_TIMEOUT);
|
---|
118 |
|
---|
119 | if (ret == ESYNCH_OK_BLOCKED) {
|
---|
120 | TPRINTF("%zu threads remain\n", thread_cnt - i - 1);
|
---|
121 | }
|
---|
122 | } while (!joined);
|
---|
123 |
|
---|
124 | thread_detach(thread[i]);
|
---|
125 | thread[i] = 0;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | static void run_one(void (*func)(void*), void *arg)
|
---|
131 | {
|
---|
132 | ASSERT(one_idx < MAX_THREADS);
|
---|
133 | run_thread(one_idx, func, arg);
|
---|
134 | ++one_idx;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | static void join_one(void)
|
---|
139 | {
|
---|
140 | ASSERT(0 < one_idx && one_idx <= MAX_THREADS);
|
---|
141 |
|
---|
142 | --one_idx;
|
---|
143 |
|
---|
144 | if (thread[one_idx]) {
|
---|
145 | thread_join(thread[one_idx]);
|
---|
146 | thread_detach(thread[one_idx]);
|
---|
147 | thread[one_idx] = 0;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*-------------------------------------------------------------------*/
|
---|
152 |
|
---|
153 |
|
---|
154 | static void nop_reader(void *arg)
|
---|
155 | {
|
---|
156 | size_t nop_iters = (size_t)arg;
|
---|
157 |
|
---|
158 | TPRINTF("Enter nop-reader\n");
|
---|
159 |
|
---|
160 | for (size_t i = 0; i < nop_iters; ++i) {
|
---|
161 | rcu_read_lock();
|
---|
162 | rcu_read_unlock();
|
---|
163 | }
|
---|
164 |
|
---|
165 | TPRINTF("Exit nop-reader\n");
|
---|
166 | }
|
---|
167 |
|
---|
168 | static void get_seq(size_t from, size_t to, size_t steps, size_t *seq)
|
---|
169 | {
|
---|
170 | ASSERT(0 < steps && from <= to && 0 < to);
|
---|
171 | size_t inc = (to - from) / (steps - 1);
|
---|
172 |
|
---|
173 | for (size_t i = 0; i < steps - 1; ++i) {
|
---|
174 | seq[i] = i * inc + from;
|
---|
175 | }
|
---|
176 |
|
---|
177 | seq[steps - 1] = to;
|
---|
178 | }
|
---|
179 |
|
---|
180 | static bool do_nop_readers(void)
|
---|
181 | {
|
---|
182 | size_t seq[MAX_THREADS] = {0};
|
---|
183 | get_seq(100, 100000, get_thread_cnt(), seq);
|
---|
184 |
|
---|
185 | TPRINTF("\nRun %zu thr: repeat empty no-op reader sections\n", get_thread_cnt());
|
---|
186 |
|
---|
187 | for (size_t k = 0; k < get_thread_cnt(); ++k)
|
---|
188 | run_one(nop_reader, (void*)seq[k]);
|
---|
189 |
|
---|
190 | TPRINTF("\nJoining %zu no-op readers\n", get_thread_cnt());
|
---|
191 | join_all();
|
---|
192 |
|
---|
193 | return true;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*-------------------------------------------------------------------*/
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 | static void long_reader(void *arg)
|
---|
201 | {
|
---|
202 | const size_t iter_cnt = 100 * 1000 * 1000;
|
---|
203 | size_t nop_iters = (size_t)arg;
|
---|
204 | size_t outer_iters = iter_cnt / nop_iters;
|
---|
205 |
|
---|
206 | TPRINTF("Enter long-reader\n");
|
---|
207 |
|
---|
208 | for (size_t i = 0; i < outer_iters; ++i) {
|
---|
209 | rcu_read_lock();
|
---|
210 |
|
---|
211 | for (volatile size_t k = 0; k < nop_iters; ++k) {
|
---|
212 | /* nop, but increment volatile k */
|
---|
213 | }
|
---|
214 |
|
---|
215 | rcu_read_unlock();
|
---|
216 | }
|
---|
217 |
|
---|
218 | TPRINTF("Exit long-reader\n");
|
---|
219 | }
|
---|
220 |
|
---|
221 | static bool do_long_readers(void)
|
---|
222 | {
|
---|
223 | size_t seq[MAX_THREADS] = {0};
|
---|
224 | get_seq(10, 1000 * 1000, get_thread_cnt(), seq);
|
---|
225 |
|
---|
226 | TPRINTF("\nRun %zu thr: repeat long reader sections, will preempt, no cbs.\n",
|
---|
227 | get_thread_cnt());
|
---|
228 |
|
---|
229 | for (size_t k = 0; k < get_thread_cnt(); ++k)
|
---|
230 | run_one(long_reader, (void*)seq[k]);
|
---|
231 |
|
---|
232 | TPRINTF("\nJoining %zu readers with long reader sections.\n", get_thread_cnt());
|
---|
233 | join_all();
|
---|
234 |
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*-------------------------------------------------------------------*/
|
---|
239 |
|
---|
240 |
|
---|
241 | static atomic_t nop_callbacks_cnt = {0};
|
---|
242 | /* Must be even. */
|
---|
243 | static const int nop_updater_iters = 10000;
|
---|
244 |
|
---|
245 | static void count_cb(rcu_item_t *item)
|
---|
246 | {
|
---|
247 | atomic_inc(&nop_callbacks_cnt);
|
---|
248 | free(item);
|
---|
249 | }
|
---|
250 |
|
---|
251 | static void nop_updater(void *arg)
|
---|
252 | {
|
---|
253 | for (int i = 0; i < nop_updater_iters; i += 2){
|
---|
254 | rcu_item_t *a = malloc(sizeof(rcu_item_t), FRAME_ATOMIC);
|
---|
255 | rcu_item_t *b = malloc(sizeof(rcu_item_t), FRAME_ATOMIC);
|
---|
256 |
|
---|
257 | if (a && b) {
|
---|
258 | rcu_call(a, count_cb);
|
---|
259 | rcu_call(b, count_cb);
|
---|
260 | } else {
|
---|
261 | TPRINTF("[out-of-mem]\n");
|
---|
262 | free(a);
|
---|
263 | free(b);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | static bool do_nop_callbacks(void)
|
---|
270 | {
|
---|
271 | atomic_set(&nop_callbacks_cnt, 0);
|
---|
272 |
|
---|
273 | size_t exp_cnt = nop_updater_iters * get_thread_cnt();
|
---|
274 | size_t max_used_mem = sizeof(rcu_item_t) * exp_cnt;
|
---|
275 |
|
---|
276 | TPRINTF("\nRun %zu thr: post %zu no-op callbacks (%zu B used), no readers.\n",
|
---|
277 | get_thread_cnt(), exp_cnt, max_used_mem);
|
---|
278 |
|
---|
279 | run_all(nop_updater);
|
---|
280 | TPRINTF("\nJoining %zu no-op callback threads\n", get_thread_cnt());
|
---|
281 | join_all();
|
---|
282 |
|
---|
283 | size_t loop_cnt = 0, max_loops = 15;
|
---|
284 |
|
---|
285 | while (exp_cnt != atomic_get(&nop_callbacks_cnt) && loop_cnt < max_loops) {
|
---|
286 | ++loop_cnt;
|
---|
287 | TPRINTF(".");
|
---|
288 | thread_sleep(1);
|
---|
289 | }
|
---|
290 |
|
---|
291 | return loop_cnt < max_loops;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*-------------------------------------------------------------------*/
|
---|
295 |
|
---|
296 | typedef struct {
|
---|
297 | rcu_item_t rcu_item;
|
---|
298 | int cookie;
|
---|
299 | } item_w_cookie_t;
|
---|
300 |
|
---|
301 | const int magic_cookie = 0x01234567;
|
---|
302 | static int one_cb_is_done = 0;
|
---|
303 |
|
---|
304 | static void one_cb_done(rcu_item_t *item)
|
---|
305 | {
|
---|
306 | ASSERT( ((item_w_cookie_t *)item)->cookie == magic_cookie);
|
---|
307 | one_cb_is_done = 1;
|
---|
308 | TPRINTF("Callback()\n");
|
---|
309 | free(item);
|
---|
310 | }
|
---|
311 |
|
---|
312 | static void one_cb_reader(void *arg)
|
---|
313 | {
|
---|
314 | TPRINTF("Enter one-cb-reader\n");
|
---|
315 |
|
---|
316 | rcu_read_lock();
|
---|
317 |
|
---|
318 | item_w_cookie_t *item = malloc(sizeof(item_w_cookie_t), FRAME_ATOMIC);
|
---|
319 |
|
---|
320 | if (item) {
|
---|
321 | item->cookie = magic_cookie;
|
---|
322 | rcu_call(&item->rcu_item, one_cb_done);
|
---|
323 | } else {
|
---|
324 | TPRINTF("\n[out-of-mem]\n");
|
---|
325 | }
|
---|
326 |
|
---|
327 | thread_sleep(1);
|
---|
328 |
|
---|
329 | rcu_read_unlock();
|
---|
330 |
|
---|
331 | TPRINTF("Exit one-cb-reader\n");
|
---|
332 | }
|
---|
333 |
|
---|
334 | static bool do_one_cb(void)
|
---|
335 | {
|
---|
336 | one_cb_is_done = 0;
|
---|
337 |
|
---|
338 | TPRINTF("\nRun a single reader that posts one callback.\n");
|
---|
339 | run_one(one_cb_reader, 0);
|
---|
340 | join_one();
|
---|
341 |
|
---|
342 | TPRINTF("\nJoined one-cb reader, wait for callback.\n");
|
---|
343 | size_t loop_cnt = 0;
|
---|
344 | size_t max_loops = 4; /* 200 ms total */
|
---|
345 |
|
---|
346 | while (!one_cb_is_done && loop_cnt < max_loops) {
|
---|
347 | thread_usleep(50 * 1000);
|
---|
348 | ++loop_cnt;
|
---|
349 | }
|
---|
350 |
|
---|
351 | return one_cb_is_done;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*-------------------------------------------------------------------*/
|
---|
355 |
|
---|
356 | typedef struct {
|
---|
357 | size_t update_cnt;
|
---|
358 | size_t read_cnt;
|
---|
359 | size_t iters;
|
---|
360 | } seq_work_t;
|
---|
361 |
|
---|
362 | typedef struct {
|
---|
363 | rcu_item_t rcu;
|
---|
364 | atomic_count_t start_time;
|
---|
365 | } seq_item_t;
|
---|
366 |
|
---|
367 |
|
---|
368 | static int seq_test_result = EOK;
|
---|
369 |
|
---|
370 | static atomic_t cur_time = {1};
|
---|
371 | static atomic_count_t max_upd_done_time = {0};
|
---|
372 |
|
---|
373 | static void seq_cb(rcu_item_t *rcu_item)
|
---|
374 | {
|
---|
375 | seq_item_t *item = member_to_inst(rcu_item, seq_item_t, rcu);
|
---|
376 |
|
---|
377 | /* Racy but errs to the conservative side, so it is ok. */
|
---|
378 | if (max_upd_done_time < item->start_time) {
|
---|
379 | max_upd_done_time = item->start_time;
|
---|
380 |
|
---|
381 | /* Make updated time visible */
|
---|
382 | memory_barrier();
|
---|
383 | }
|
---|
384 |
|
---|
385 | free(item);
|
---|
386 | }
|
---|
387 |
|
---|
388 | static void seq_func(void *arg)
|
---|
389 | {
|
---|
390 | seq_work_t *work = (seq_work_t*)arg;
|
---|
391 |
|
---|
392 | /* Alternate between reader and updater roles. */
|
---|
393 | for (size_t k = 0; k < work->iters; ++k) {
|
---|
394 | /* Reader */
|
---|
395 | for (size_t i = 0; i < work->read_cnt; ++i) {
|
---|
396 | rcu_read_lock();
|
---|
397 | atomic_count_t start_time = atomic_postinc(&cur_time);
|
---|
398 |
|
---|
399 | for (volatile size_t d = 0; d < 10 * i; ++d ){
|
---|
400 | /* no-op */
|
---|
401 | }
|
---|
402 |
|
---|
403 | /* Get most recent max_upd_done_time. */
|
---|
404 | memory_barrier();
|
---|
405 |
|
---|
406 | if (start_time < max_upd_done_time) {
|
---|
407 | seq_test_result = ERACE;
|
---|
408 | }
|
---|
409 |
|
---|
410 | rcu_read_unlock();
|
---|
411 |
|
---|
412 | if (seq_test_result != EOK)
|
---|
413 | return;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* Updater */
|
---|
417 | for (size_t i = 0; i < work->update_cnt; ++i) {
|
---|
418 | seq_item_t *a = malloc(sizeof(seq_item_t), FRAME_ATOMIC);
|
---|
419 | seq_item_t *b = malloc(sizeof(seq_item_t), FRAME_ATOMIC);
|
---|
420 |
|
---|
421 | if (a && b) {
|
---|
422 | a->start_time = atomic_postinc(&cur_time);
|
---|
423 | rcu_call(&a->rcu, seq_cb);
|
---|
424 |
|
---|
425 | b->start_time = atomic_postinc(&cur_time);
|
---|
426 | rcu_call(&b->rcu, seq_cb);
|
---|
427 | } else {
|
---|
428 | TPRINTF("\n[out-of-mem]\n");
|
---|
429 | seq_test_result = ENOMEM;
|
---|
430 | free(a);
|
---|
431 | free(b);
|
---|
432 | return;
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | static bool do_seq_check(void)
|
---|
440 | {
|
---|
441 | seq_test_result = EOK;
|
---|
442 | max_upd_done_time = 0;
|
---|
443 | atomic_set(&cur_time, 1);
|
---|
444 |
|
---|
445 | const size_t iters = 100;
|
---|
446 | const size_t total_cnt = 1000;
|
---|
447 | size_t read_cnt[MAX_THREADS] = {0};
|
---|
448 | seq_work_t item[MAX_THREADS];
|
---|
449 |
|
---|
450 | size_t total_cbs = 0;
|
---|
451 | size_t max_used_mem = 0;
|
---|
452 |
|
---|
453 | get_seq(0, total_cnt, get_thread_cnt(), read_cnt);
|
---|
454 |
|
---|
455 |
|
---|
456 | for (size_t i = 0; i < get_thread_cnt(); ++i) {
|
---|
457 | item[i].update_cnt = total_cnt - read_cnt[i];
|
---|
458 | item[i].read_cnt = read_cnt[i];
|
---|
459 | item[i].iters = iters;
|
---|
460 |
|
---|
461 | total_cbs += 2 * iters * item[i].update_cnt;
|
---|
462 | }
|
---|
463 |
|
---|
464 | max_used_mem = total_cbs * sizeof(seq_item_t);
|
---|
465 |
|
---|
466 | const char *mem_suffix;
|
---|
467 | uint64_t mem_units;
|
---|
468 | bin_order_suffix(max_used_mem, &mem_units, &mem_suffix, false);
|
---|
469 |
|
---|
470 | TPRINTF("\nRun %zu th: check callback completion time in readers. "
|
---|
471 | "%zu callbacks total (max %" PRIu64 " %s used). Be patient.\n",
|
---|
472 | get_thread_cnt(), total_cbs, mem_units, mem_suffix);
|
---|
473 |
|
---|
474 | for (size_t i = 0; i < get_thread_cnt(); ++i) {
|
---|
475 | run_one(seq_func, &item[i]);
|
---|
476 | }
|
---|
477 |
|
---|
478 | TPRINTF("\nJoining %zu seq-threads\n", get_thread_cnt());
|
---|
479 | join_all();
|
---|
480 |
|
---|
481 | if (seq_test_result == ENOMEM) {
|
---|
482 | TPRINTF("\nErr: out-of mem\n");
|
---|
483 | } else if (seq_test_result == ERACE) {
|
---|
484 | TPRINTF("\nERROR: race detected!!\n");
|
---|
485 | }
|
---|
486 |
|
---|
487 | return seq_test_result == EOK;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /*-------------------------------------------------------------------*/
|
---|
491 |
|
---|
492 |
|
---|
493 | static void reader_unlocked(rcu_item_t *item)
|
---|
494 | {
|
---|
495 | exited_t *p = (exited_t*)item;
|
---|
496 | p->exited = true;
|
---|
497 | }
|
---|
498 |
|
---|
499 | static void reader_exit(void *arg)
|
---|
500 | {
|
---|
501 | rcu_read_lock();
|
---|
502 | rcu_read_lock();
|
---|
503 | rcu_read_lock();
|
---|
504 | rcu_read_unlock();
|
---|
505 |
|
---|
506 | rcu_call((rcu_item_t*)arg, reader_unlocked);
|
---|
507 |
|
---|
508 | rcu_read_lock();
|
---|
509 | rcu_read_lock();
|
---|
510 |
|
---|
511 | /* Exit without unlocking the rcu reader section. */
|
---|
512 | }
|
---|
513 |
|
---|
514 | static bool do_reader_exit(void)
|
---|
515 | {
|
---|
516 | TPRINTF("\nReader exits thread with rcu_lock\n");
|
---|
517 |
|
---|
518 | exited_t *p = malloc(sizeof(exited_t), FRAME_ATOMIC);
|
---|
519 | if (!p) {
|
---|
520 | TPRINTF("[out-of-mem]\n");
|
---|
521 | return false;
|
---|
522 | }
|
---|
523 |
|
---|
524 | p->exited = false;
|
---|
525 |
|
---|
526 | run_one(reader_exit, p);
|
---|
527 | join_one();
|
---|
528 |
|
---|
529 | int result = EOK;
|
---|
530 | wait_for_cb_exit(2 /* secs */, p, &result);
|
---|
531 |
|
---|
532 | if (result != EOK) {
|
---|
533 | TPRINTF("Err: RCU locked up after exiting from within a reader\n");
|
---|
534 | /* Leak the mem. */
|
---|
535 | } else {
|
---|
536 | free(p);
|
---|
537 | }
|
---|
538 |
|
---|
539 | return result == EOK;
|
---|
540 | }
|
---|
541 |
|
---|
542 | /*-------------------------------------------------------------------*/
|
---|
543 |
|
---|
544 | /*-------------------------------------------------------------------*/
|
---|
545 |
|
---|
546 | typedef struct preempt_struct {
|
---|
547 | exited_t e;
|
---|
548 | int result;
|
---|
549 | } preempt_t;
|
---|
550 |
|
---|
551 |
|
---|
552 | static void preempted_unlocked(rcu_item_t *item)
|
---|
553 | {
|
---|
554 | preempt_t *p = member_to_inst(item, preempt_t, e.rcu);
|
---|
555 | p->e.exited = true;
|
---|
556 | TPRINTF("Callback().\n");
|
---|
557 | }
|
---|
558 |
|
---|
559 | static void preempted_reader_prev(void *arg)
|
---|
560 | {
|
---|
561 | preempt_t *p = (preempt_t*)arg;
|
---|
562 | ASSERT(!p->e.exited);
|
---|
563 |
|
---|
564 | TPRINTF("reader_prev{ ");
|
---|
565 |
|
---|
566 | rcu_read_lock();
|
---|
567 | scheduler();
|
---|
568 | rcu_read_unlock();
|
---|
569 |
|
---|
570 | /*
|
---|
571 | * Start GP after exiting reader section w/ preemption.
|
---|
572 | * Just check that the callback does not lock up and is not lost.
|
---|
573 | */
|
---|
574 | rcu_call(&p->e.rcu, preempted_unlocked);
|
---|
575 |
|
---|
576 | TPRINTF("}reader_prev\n");
|
---|
577 | }
|
---|
578 |
|
---|
579 | static void preempted_reader_inside_cur(void *arg)
|
---|
580 | {
|
---|
581 | preempt_t *p = (preempt_t*)arg;
|
---|
582 | ASSERT(!p->e.exited);
|
---|
583 |
|
---|
584 | TPRINTF("reader_inside_cur{ ");
|
---|
585 | /*
|
---|
586 | * Start a GP and try to finish the reader before
|
---|
587 | * the GP ends (including preemption).
|
---|
588 | */
|
---|
589 | rcu_call(&p->e.rcu, preempted_unlocked);
|
---|
590 |
|
---|
591 | /* Give RCU threads a chance to start up. */
|
---|
592 | scheduler();
|
---|
593 | scheduler();
|
---|
594 |
|
---|
595 | rcu_read_lock();
|
---|
596 | /* Come back as soon as possible to complete before GP ends. */
|
---|
597 | thread_usleep(2);
|
---|
598 | rcu_read_unlock();
|
---|
599 |
|
---|
600 | TPRINTF("}reader_inside_cur\n");
|
---|
601 | }
|
---|
602 |
|
---|
603 |
|
---|
604 | static void preempted_reader_cur(void *arg)
|
---|
605 | {
|
---|
606 | preempt_t *p = (preempt_t*)arg;
|
---|
607 | ASSERT(!p->e.exited);
|
---|
608 |
|
---|
609 | TPRINTF("reader_cur{ ");
|
---|
610 | rcu_read_lock();
|
---|
611 |
|
---|
612 | /* Start GP. */
|
---|
613 | rcu_call(&p->e.rcu, preempted_unlocked);
|
---|
614 |
|
---|
615 | /* Preempt while cur GP detection is running */
|
---|
616 | thread_sleep(1);
|
---|
617 |
|
---|
618 | /* Err: exited before this reader completed. */
|
---|
619 | if (p->e.exited)
|
---|
620 | p->result = ERACE;
|
---|
621 |
|
---|
622 | rcu_read_unlock();
|
---|
623 | TPRINTF("}reader_cur\n");
|
---|
624 | }
|
---|
625 |
|
---|
626 | static void preempted_reader_next1(void *arg)
|
---|
627 | {
|
---|
628 | preempt_t *p = (preempt_t*)arg;
|
---|
629 | ASSERT(!p->e.exited);
|
---|
630 |
|
---|
631 | TPRINTF("reader_next1{ ");
|
---|
632 | rcu_read_lock();
|
---|
633 |
|
---|
634 | /* Preempt before cur GP detection starts. */
|
---|
635 | scheduler();
|
---|
636 |
|
---|
637 | /* Start GP. */
|
---|
638 | rcu_call(&p->e.rcu, preempted_unlocked);
|
---|
639 |
|
---|
640 | /* Err: exited before this reader completed. */
|
---|
641 | if (p->e.exited)
|
---|
642 | p->result = ERACE;
|
---|
643 |
|
---|
644 | rcu_read_unlock();
|
---|
645 | TPRINTF("}reader_next1\n");
|
---|
646 | }
|
---|
647 |
|
---|
648 | static void preempted_reader_next2(void *arg)
|
---|
649 | {
|
---|
650 | preempt_t *p = (preempt_t*)arg;
|
---|
651 | ASSERT(!p->e.exited);
|
---|
652 |
|
---|
653 | TPRINTF("reader_next2{ ");
|
---|
654 | rcu_read_lock();
|
---|
655 |
|
---|
656 | /* Preempt before cur GP detection starts. */
|
---|
657 | scheduler();
|
---|
658 |
|
---|
659 | /* Start GP. */
|
---|
660 | rcu_call(&p->e.rcu, preempted_unlocked);
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * Preempt twice while GP is running after we've been known
|
---|
664 | * to hold up the GP just to make sure multiple preemptions
|
---|
665 | * are properly tracked if a reader is delaying the cur GP.
|
---|
666 | */
|
---|
667 | thread_sleep(1);
|
---|
668 | thread_sleep(1);
|
---|
669 |
|
---|
670 | /* Err: exited before this reader completed. */
|
---|
671 | if (p->e.exited)
|
---|
672 | p->result = ERACE;
|
---|
673 |
|
---|
674 | rcu_read_unlock();
|
---|
675 | TPRINTF("}reader_next2\n");
|
---|
676 | }
|
---|
677 |
|
---|
678 |
|
---|
679 | static bool do_one_reader_preempt(void (*f)(void*), const char *err)
|
---|
680 | {
|
---|
681 | preempt_t *p = malloc(sizeof(preempt_t), FRAME_ATOMIC);
|
---|
682 | if (!p) {
|
---|
683 | TPRINTF("[out-of-mem]\n");
|
---|
684 | return false;
|
---|
685 | }
|
---|
686 |
|
---|
687 | p->e.exited = false;
|
---|
688 | p->result = EOK;
|
---|
689 |
|
---|
690 | run_one(f, p);
|
---|
691 | join_one();
|
---|
692 |
|
---|
693 | /* Wait at most 4 secs. */
|
---|
694 | wait_for_cb_exit(4, &p->e, &p->result);
|
---|
695 |
|
---|
696 | if (p->result == EOK) {
|
---|
697 | free(p);
|
---|
698 | return true;
|
---|
699 | } else {
|
---|
700 | TPRINTF(err);
|
---|
701 | /* Leak a bit of mem. */
|
---|
702 | return false;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | static bool do_reader_preempt(void)
|
---|
707 | {
|
---|
708 | TPRINTF("\nReaders will be preempted.\n");
|
---|
709 |
|
---|
710 | bool success = true;
|
---|
711 | bool ok = true;
|
---|
712 |
|
---|
713 | ok = do_one_reader_preempt(preempted_reader_prev,
|
---|
714 | "Err: preempted_reader_prev()\n");
|
---|
715 | success = success && ok;
|
---|
716 |
|
---|
717 | ok = do_one_reader_preempt(preempted_reader_inside_cur,
|
---|
718 | "Err: preempted_reader_inside_cur()\n");
|
---|
719 | success = success && ok;
|
---|
720 |
|
---|
721 | ok = do_one_reader_preempt(preempted_reader_cur,
|
---|
722 | "Err: preempted_reader_cur()\n");
|
---|
723 | success = success && ok;
|
---|
724 |
|
---|
725 | ok = do_one_reader_preempt(preempted_reader_next1,
|
---|
726 | "Err: preempted_reader_next1()\n");
|
---|
727 | success = success && ok;
|
---|
728 |
|
---|
729 | ok = do_one_reader_preempt(preempted_reader_next2,
|
---|
730 | "Err: preempted_reader_next2()\n");
|
---|
731 | success = success && ok;
|
---|
732 |
|
---|
733 | return success;
|
---|
734 | }
|
---|
735 |
|
---|
736 | /*-------------------------------------------------------------------*/
|
---|
737 | typedef struct {
|
---|
738 | bool reader_done;
|
---|
739 | bool reader_running;
|
---|
740 | bool synch_running;
|
---|
741 | } synch_t;
|
---|
742 |
|
---|
743 | static void synch_reader(void *arg)
|
---|
744 | {
|
---|
745 | synch_t *synch = (synch_t *) arg;
|
---|
746 |
|
---|
747 | rcu_read_lock();
|
---|
748 |
|
---|
749 | /* Order accesses of synch after the reader section begins. */
|
---|
750 | memory_barrier();
|
---|
751 |
|
---|
752 | synch->reader_running = true;
|
---|
753 |
|
---|
754 | while (!synch->synch_running) {
|
---|
755 | /* 0.5 sec */
|
---|
756 | delay(500 * 1000);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* Run for 1 sec */
|
---|
760 | delay(1000 * 1000);
|
---|
761 | /* thread_join() propagates done to do_synch() */
|
---|
762 | synch->reader_done = true;
|
---|
763 |
|
---|
764 | rcu_read_unlock();
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | static bool do_synch(void)
|
---|
769 | {
|
---|
770 | TPRINTF("\nSynchronize with long reader\n");
|
---|
771 |
|
---|
772 | synch_t *synch = malloc(sizeof(synch_t), FRAME_ATOMIC);
|
---|
773 |
|
---|
774 | if (!synch) {
|
---|
775 | TPRINTF("[out-of-mem]\n");
|
---|
776 | return false;
|
---|
777 | }
|
---|
778 |
|
---|
779 | synch->reader_done = false;
|
---|
780 | synch->reader_running = false;
|
---|
781 | synch->synch_running = false;
|
---|
782 |
|
---|
783 | run_one(synch_reader, synch);
|
---|
784 |
|
---|
785 | /* Wait for the reader to enter its critical section. */
|
---|
786 | scheduler();
|
---|
787 | while (!synch->reader_running) {
|
---|
788 | thread_usleep(500 * 1000);
|
---|
789 | }
|
---|
790 |
|
---|
791 | synch->synch_running = true;
|
---|
792 |
|
---|
793 | rcu_synchronize();
|
---|
794 | join_one();
|
---|
795 |
|
---|
796 |
|
---|
797 | if (synch->reader_done) {
|
---|
798 | free(synch);
|
---|
799 | return true;
|
---|
800 | } else {
|
---|
801 | TPRINTF("Err: synchronize() exited prematurely \n");
|
---|
802 | /* Leak some mem. */
|
---|
803 | return false;
|
---|
804 | }
|
---|
805 | }
|
---|
806 |
|
---|
807 | /*-------------------------------------------------------------------*/
|
---|
808 |
|
---|
809 | typedef struct {
|
---|
810 | size_t iters;
|
---|
811 | bool master;
|
---|
812 | } stress_t;
|
---|
813 |
|
---|
814 |
|
---|
815 | static void stress_reader(void *arg)
|
---|
816 | {
|
---|
817 | bool *done = (bool*) arg;
|
---|
818 |
|
---|
819 | while (!*done) {
|
---|
820 | rcu_read_lock();
|
---|
821 | rcu_read_unlock();
|
---|
822 |
|
---|
823 | /*
|
---|
824 | * Do some work outside of the reader section so we are not always
|
---|
825 | * preempted in the reader section.
|
---|
826 | */
|
---|
827 | delay(5);
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | static void stress_cb(rcu_item_t *item)
|
---|
832 | {
|
---|
833 | /* 5 us * 1000 * 1000 iters == 5 sec per updater thread */
|
---|
834 | delay(5);
|
---|
835 | free(item);
|
---|
836 | }
|
---|
837 |
|
---|
838 | static void stress_updater(void *arg)
|
---|
839 | {
|
---|
840 | stress_t *s = (stress_t *)arg;
|
---|
841 |
|
---|
842 | for (size_t i = 0; i < s->iters; ++i) {
|
---|
843 | rcu_item_t *item = malloc(sizeof(rcu_item_t), FRAME_ATOMIC);
|
---|
844 |
|
---|
845 | if (item) {
|
---|
846 | rcu_call(item, stress_cb);
|
---|
847 | } else {
|
---|
848 | TPRINTF("[out-of-mem]\n");
|
---|
849 | return;
|
---|
850 | }
|
---|
851 |
|
---|
852 | /* Print a dot if we make a progress of 1% */
|
---|
853 | if (s->master && 0 == (i % (s->iters/100)))
|
---|
854 | TPRINTF(".");
|
---|
855 | }
|
---|
856 | }
|
---|
857 |
|
---|
858 | static bool do_stress(void)
|
---|
859 | {
|
---|
860 | size_t cb_per_thread = 1000 * 1000;
|
---|
861 | bool done = false;
|
---|
862 | stress_t master = { .iters = cb_per_thread, .master = true };
|
---|
863 | stress_t worker = { .iters = cb_per_thread, .master = false };
|
---|
864 |
|
---|
865 | size_t thread_cnt = min(MAX_THREADS / 2, config.cpu_active);
|
---|
866 | /* Each cpu has one reader and one updater. */
|
---|
867 | size_t reader_cnt = thread_cnt;
|
---|
868 | size_t updater_cnt = thread_cnt;
|
---|
869 |
|
---|
870 | size_t exp_upd_calls = updater_cnt * cb_per_thread;
|
---|
871 | size_t max_used_mem = exp_upd_calls * sizeof(rcu_item_t);
|
---|
872 |
|
---|
873 | const char *mem_suffix;
|
---|
874 | uint64_t mem_units;
|
---|
875 | bin_order_suffix(max_used_mem, &mem_units, &mem_suffix, false);
|
---|
876 |
|
---|
877 | TPRINTF("\nStress: Run %zu nop-readers and %zu updaters. %zu callbacks"
|
---|
878 | " total (max %" PRIu64 " %s used). Be very patient.\n",
|
---|
879 | reader_cnt, updater_cnt, exp_upd_calls, mem_units, mem_suffix);
|
---|
880 |
|
---|
881 | for (size_t k = 0; k < reader_cnt; ++k) {
|
---|
882 | run_one(stress_reader, &done);
|
---|
883 | }
|
---|
884 |
|
---|
885 | for (size_t k = 0; k < updater_cnt; ++k) {
|
---|
886 | run_one(stress_updater, k > 0 ? &worker : &master);
|
---|
887 | }
|
---|
888 |
|
---|
889 | TPRINTF("\nJoining %zu stress updaters.\n", updater_cnt);
|
---|
890 |
|
---|
891 | for (size_t k = 0; k < updater_cnt; ++k) {
|
---|
892 | join_one();
|
---|
893 | }
|
---|
894 |
|
---|
895 | done = true;
|
---|
896 |
|
---|
897 | TPRINTF("\nJoining %zu stress nop-readers.\n", reader_cnt);
|
---|
898 |
|
---|
899 | join_all();
|
---|
900 | return true;
|
---|
901 | }
|
---|
902 | /*-------------------------------------------------------------------*/
|
---|
903 |
|
---|
904 | typedef struct {
|
---|
905 | rcu_item_t r;
|
---|
906 | size_t total_cnt;
|
---|
907 | size_t count_down;
|
---|
908 | bool expedite;
|
---|
909 | } expedite_t;
|
---|
910 |
|
---|
911 | static void expedite_cb(rcu_item_t *arg)
|
---|
912 | {
|
---|
913 | expedite_t *e = (expedite_t *)arg;
|
---|
914 |
|
---|
915 | if (1 < e->count_down) {
|
---|
916 | --e->count_down;
|
---|
917 |
|
---|
918 | if (0 == (e->count_down % (e->total_cnt/100))) {
|
---|
919 | TPRINTF("*");
|
---|
920 | }
|
---|
921 |
|
---|
922 | _rcu_call(e->expedite, &e->r, expedite_cb);
|
---|
923 | } else {
|
---|
924 | /* Do not touch any of e's mem after we declare we're done with it. */
|
---|
925 | memory_barrier();
|
---|
926 | e->count_down = 0;
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | static void run_expedite(bool exp, size_t cnt)
|
---|
931 | {
|
---|
932 | expedite_t e;
|
---|
933 | e.total_cnt = cnt;
|
---|
934 | e.count_down = cnt;
|
---|
935 | e.expedite = exp;
|
---|
936 |
|
---|
937 | _rcu_call(e.expedite, &e.r, expedite_cb);
|
---|
938 |
|
---|
939 | while (0 < e.count_down) {
|
---|
940 | thread_sleep(1);
|
---|
941 | TPRINTF(".");
|
---|
942 | }
|
---|
943 | }
|
---|
944 |
|
---|
945 | static bool do_expedite(void)
|
---|
946 | {
|
---|
947 | size_t exp_cnt = 1000 * 1000;
|
---|
948 | size_t normal_cnt = 1 * 1000;
|
---|
949 |
|
---|
950 | TPRINTF("Expedited: sequence of %zu rcu_calls\n", exp_cnt);
|
---|
951 | run_expedite(true, exp_cnt);
|
---|
952 | TPRINTF("Normal/non-expedited: sequence of %zu rcu_calls\n", normal_cnt);
|
---|
953 | run_expedite(false, normal_cnt);
|
---|
954 | return true;
|
---|
955 | }
|
---|
956 | /*-------------------------------------------------------------------*/
|
---|
957 |
|
---|
958 | struct test_func {
|
---|
959 | bool include;
|
---|
960 | bool (*func)(void);
|
---|
961 | const char *desc;
|
---|
962 | };
|
---|
963 |
|
---|
964 |
|
---|
965 | const char *test_rcu1(void)
|
---|
966 | {
|
---|
967 | struct test_func test_func[] = {
|
---|
968 | { 1, do_one_cb, "do_one_cb" },
|
---|
969 | { 1, do_reader_preempt, "do_reader_preempt" },
|
---|
970 | { 1, do_synch, "do_synch" },
|
---|
971 | { 1, do_reader_exit, "do_reader_exit" },
|
---|
972 | { 1, do_nop_readers, "do_nop_readers" },
|
---|
973 | { 1, do_seq_check, "do_seq_check" },
|
---|
974 | { 0, do_long_readers, "do_long_readers" },
|
---|
975 | { 1, do_nop_callbacks, "do_nop_callbacks" },
|
---|
976 | { 0, do_expedite, "do_expedite" },
|
---|
977 | { 1, do_stress, "do_stress" },
|
---|
978 | { 0, 0, 0 }
|
---|
979 | };
|
---|
980 |
|
---|
981 | bool success = true;
|
---|
982 | bool ok = true;
|
---|
983 | uint64_t completed_gps = rcu_completed_gps();
|
---|
984 | uint64_t delta_gps = 0;
|
---|
985 |
|
---|
986 | for (int i = 0; test_func[i].func != 0; ++i) {
|
---|
987 | if (!test_func[i].include) {
|
---|
988 | TPRINTF("\nSubtest %s() skipped.\n", test_func[i].desc);
|
---|
989 | continue;
|
---|
990 | } else {
|
---|
991 | TPRINTF("\nRunning subtest %s.\n", test_func[i].desc);
|
---|
992 | }
|
---|
993 |
|
---|
994 | ok = test_func[i].func();
|
---|
995 | success = success && ok;
|
---|
996 |
|
---|
997 | delta_gps = rcu_completed_gps() - completed_gps;
|
---|
998 | completed_gps += delta_gps;
|
---|
999 |
|
---|
1000 | if (ok) {
|
---|
1001 | TPRINTF("\nSubtest %s() ok (GPs: %" PRIu64 ").\n",
|
---|
1002 | test_func[i].desc, delta_gps);
|
---|
1003 | } else {
|
---|
1004 | TPRINTF("\nFailed: %s(). Pausing for 5 secs.\n", test_func[i].desc);
|
---|
1005 | thread_sleep(5);
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | if (success)
|
---|
1010 | return 0;
|
---|
1011 | else
|
---|
1012 | return "One of the tests failed.";
|
---|
1013 | }
|
---|