source: mainline/kernel/test/synch/rcu1.c@ 0a597d7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0a597d7 was 0a597d7, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

rcu test: Fixed includes.

  • Property mode set to 100644
File size: 22.4 KB
RevLine 
[5b6c033]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>
[2e16033]35#include <str.h>
[0a597d7]36#include <errno.h>
37#include <time/delay.h>
[5b6c033]38
39#include <synch/rcu.h>
40
41
42#define MAX_THREADS 32
43
44static int one_idx = 0;
[205832b]45static thread_t *thread[MAX_THREADS] = { NULL };
[5b6c033]46
47typedef struct {
48 rcu_item_t rcu;
49 bool exited;
50} exited_t;
51
[1c1da4b]52/* Callback raced with preexisting readers. */
[5b6c033]53#define ERACE 123
[1c1da4b]54/* Waited for too long for the callback to exit; consider it lost. */
[5b6c033]55#define ECBLOST 432
56
57/*-------------------------------------------------------------------*/
58static 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
76static size_t get_thread_cnt(void)
77{
78 return min(MAX_THREADS, config.cpu_active * 4);
79}
80
81static 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]) {
[1c1da4b]89 /* Distribute evenly. */
90 thread_wire(thread[k], &cpus[k % config.cpu_active]);
[5b6c033]91 thread_ready(thread[k]);
92 }
93}
94
95static 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) {
[205832b]102 run_thread(i, func, NULL);
[5b6c033]103 }
104}
105
106static 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]);
[205832b]125 thread[i] = NULL;
[5b6c033]126 }
127 }
128}
129
130static 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
138static 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]);
[205832b]147 thread[one_idx] = NULL;
[5b6c033]148 }
149}
150
151/*-------------------------------------------------------------------*/
152
153
154static 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
168static 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
180static 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
200static 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) {
[1c1da4b]212 /* nop, but increment volatile k */
[5b6c033]213 }
214
215 rcu_read_unlock();
216 }
217
218 TPRINTF("Exit long-reader\n");
219}
220
221static 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
241static atomic_t nop_callbacks_cnt = {0};
[2e16033]242/* Must be even. */
[5b6c033]243static const int nop_updater_iters = 10000;
244
245static void count_cb(rcu_item_t *item)
246{
247 atomic_inc(&nop_callbacks_cnt);
248 free(item);
249}
250
251static void nop_updater(void *arg)
252{
[2e16033]253 for (int i = 0; i < nop_updater_iters; i += 2){
[0594c7ea]254 rcu_item_t *a = malloc(sizeof(rcu_item_t), FRAME_ATOMIC);
255 rcu_item_t *b = malloc(sizeof(rcu_item_t), FRAME_ATOMIC);
[5b6c033]256
257 if (a && b) {
258 rcu_call(a, count_cb);
259 rcu_call(b, count_cb);
260 } else {
[0594c7ea]261 TPRINTF("[out-of-mem]\n");
[5b6c033]262 free(a);
263 free(b);
[0594c7ea]264 return;
[5b6c033]265 }
266 }
267}
268
269static bool do_nop_callbacks(void)
270{
271 atomic_set(&nop_callbacks_cnt, 0);
[2e16033]272
273 size_t exp_cnt = nop_updater_iters * get_thread_cnt();
274 size_t max_used_mem = sizeof(rcu_item_t) * exp_cnt;
[5b6c033]275
[2e16033]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);
[5b6c033]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
296typedef struct {
297 rcu_item_t rcu_item;
298 int cookie;
299} item_w_cookie_t;
300
301const int magic_cookie = 0x01234567;
302static int one_cb_is_done = 0;
303
304static 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
312static void one_cb_reader(void *arg)
313{
314 TPRINTF("Enter one-cb-reader\n");
315
316 rcu_read_lock();
317
[0594c7ea]318 item_w_cookie_t *item = malloc(sizeof(item_w_cookie_t), FRAME_ATOMIC);
[5b6c033]319
[2e16033]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 }
[5b6c033]326
327 thread_sleep(1);
328
329 rcu_read_unlock();
330
331 TPRINTF("Exit one-cb-reader\n");
332}
333
334static bool do_one_cb(void)
335{
336 one_cb_is_done = 0;
337
338 TPRINTF("\nRun a single reader that posts one callback.\n");
[205832b]339 run_one(one_cb_reader, NULL);
[5b6c033]340 join_one();
341
[1c1da4b]342 TPRINTF("\nJoined one-cb reader, wait for callback.\n");
[5b6c033]343 size_t loop_cnt = 0;
[1c1da4b]344 size_t max_loops = 4; /* 200 ms total */
[5b6c033]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
356typedef struct {
357 size_t update_cnt;
358 size_t read_cnt;
359 size_t iters;
360} seq_work_t;
361
362typedef struct {
363 rcu_item_t rcu;
364 atomic_count_t start_time;
365} seq_item_t;
366
367
368static int seq_test_result = EOK;
369
370static atomic_t cur_time = {1};
371static atomic_count_t max_upd_done_time = {0};
372
373static 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
388static 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
[2e16033]399 for (volatile size_t d = 0; d < 10 * i; ++d ){
[5b6c033]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 */
[2e16033]417 for (size_t i = 0; i < work->update_cnt; ++i) {
[0594c7ea]418 seq_item_t *a = malloc(sizeof(seq_item_t), FRAME_ATOMIC);
419 seq_item_t *b = malloc(sizeof(seq_item_t), FRAME_ATOMIC);
[5b6c033]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;
[2e16033]430 free(a);
431 free(b);
[5b6c033]432 return;
433 }
434 }
435
436 }
437}
438
439static 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
[2e16033]450 size_t total_cbs = 0;
451 size_t max_used_mem = 0;
452
[5b6c033]453 get_seq(0, total_cnt, get_thread_cnt(), read_cnt);
454
[2e16033]455
[5b6c033]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;
[2e16033]460
461 total_cbs += 2 * iters * item[i].update_cnt;
[5b6c033]462 }
463
[2e16033]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);
[5b6c033]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
493static void reader_unlocked(rcu_item_t *item)
494{
495 exited_t *p = (exited_t*)item;
496 p->exited = true;
497}
498
499static 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
514static bool do_reader_exit(void)
515{
516 TPRINTF("\nReader exits thread with rcu_lock\n");
517
[0594c7ea]518 exited_t *p = malloc(sizeof(exited_t), FRAME_ATOMIC);
519 if (!p) {
520 TPRINTF("[out-of-mem]\n");
521 return false;
522 }
523
[5b6c033]524 p->exited = false;
525
526 run_one(reader_exit, p);
527 join_one();
528
529 int result = EOK;
[1c1da4b]530 wait_for_cb_exit(2 /* secs */, p, &result);
[5b6c033]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
546typedef struct preempt_struct {
547 exited_t e;
548 int result;
549} preempt_t;
550
551
552static 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
559static 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
579static 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
604static 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
626static 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
648static 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
679static bool do_one_reader_preempt(void (*f)(void*), const char *err)
680{
[0594c7ea]681 preempt_t *p = malloc(sizeof(preempt_t), FRAME_ATOMIC);
682 if (!p) {
683 TPRINTF("[out-of-mem]\n");
684 return false;
685 }
686
[5b6c033]687 p->e.exited = false;
688 p->result = EOK;
689
690 run_one(f, p);
691 join_one();
692
[1c1da4b]693 /* Wait at most 4 secs. */
[5b6c033]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
706static bool do_reader_preempt(void)
707{
[1c1da4b]708 TPRINTF("\nReaders will be preempted.\n");
[5b6c033]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/*-------------------------------------------------------------------*/
737typedef struct {
738 bool reader_done;
739 bool reader_running;
740 bool synch_running;
741} synch_t;
742
743static void synch_reader(void *arg)
744{
745 synch_t *synch = (synch_t *) arg;
746
747 rcu_read_lock();
748
[1c1da4b]749 /* Order accesses of synch after the reader section begins. */
[5b6c033]750 memory_barrier();
751
752 synch->reader_running = true;
753
754 while (!synch->synch_running) {
[1c1da4b]755 /* 0.5 sec */
[5b6c033]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
768static bool do_synch(void)
769{
770 TPRINTF("\nSynchronize with long reader\n");
771
[0594c7ea]772 synch_t *synch = malloc(sizeof(synch_t), FRAME_ATOMIC);
[5b6c033]773
774 if (!synch) {
[0594c7ea]775 TPRINTF("[out-of-mem]\n");
[5b6c033]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
[4ec9ea41]807/*-------------------------------------------------------------------*/
808typedef struct {
809 rcu_item_t rcu_item;
810 atomic_t done;
811} barrier_t;
812
813static void barrier_callback(rcu_item_t *item)
814{
815 barrier_t *b = member_to_inst(item, barrier_t, rcu_item);
816 atomic_set(&b->done, 1);
817}
818
819static bool do_barrier(void)
820{
821 TPRINTF("\nrcu_barrier: Wait for outstanding rcu callbacks to complete\n");
822
823 barrier_t *barrier = malloc(sizeof(barrier_t), FRAME_ATOMIC);
824
825 if (!barrier) {
826 TPRINTF("[out-of-mem]\n");
827 return false;
828 }
829
830 atomic_set(&barrier->done, 0);
831
832 rcu_call(&barrier->rcu_item, barrier_callback);
833 rcu_barrier();
834
835 if (1 == atomic_get(&barrier->done)) {
836 free(barrier);
837 return true;
838 } else {
839 TPRINTF("rcu_barrier() exited prematurely.\n");
840 /* Leak some mem. */
841 return false;
842 }
843}
844
[5b6c033]845/*-------------------------------------------------------------------*/
846
847typedef struct {
848 size_t iters;
849 bool master;
850} stress_t;
851
852
853static void stress_reader(void *arg)
854{
855 bool *done = (bool*) arg;
856
857 while (!*done) {
858 rcu_read_lock();
859 rcu_read_unlock();
860
861 /*
862 * Do some work outside of the reader section so we are not always
863 * preempted in the reader section.
864 */
865 delay(5);
866 }
867}
868
869static void stress_cb(rcu_item_t *item)
870{
871 /* 5 us * 1000 * 1000 iters == 5 sec per updater thread */
[1c1da4b]872 delay(5);
[2e16033]873 free(item);
[5b6c033]874}
875
876static void stress_updater(void *arg)
877{
878 stress_t *s = (stress_t *)arg;
879
880 for (size_t i = 0; i < s->iters; ++i) {
[0594c7ea]881 rcu_item_t *item = malloc(sizeof(rcu_item_t), FRAME_ATOMIC);
[5b6c033]882
[0594c7ea]883 if (item) {
[5b6c033]884 rcu_call(item, stress_cb);
[0594c7ea]885 } else {
886 TPRINTF("[out-of-mem]\n");
887 return;
888 }
[5b6c033]889
[1c1da4b]890 /* Print a dot if we make a progress of 1% */
891 if (s->master && 0 == (i % (s->iters/100)))
[5b6c033]892 TPRINTF(".");
893 }
894}
895
896static bool do_stress(void)
897{
898 size_t cb_per_thread = 1000 * 1000;
899 bool done = false;
900 stress_t master = { .iters = cb_per_thread, .master = true };
901 stress_t worker = { .iters = cb_per_thread, .master = false };
902
[1c1da4b]903 size_t thread_cnt = min(MAX_THREADS / 2, config.cpu_active);
[5b6c033]904 /* Each cpu has one reader and one updater. */
905 size_t reader_cnt = thread_cnt;
906 size_t updater_cnt = thread_cnt;
[2e16033]907
908 size_t exp_upd_calls = updater_cnt * cb_per_thread;
909 size_t max_used_mem = exp_upd_calls * sizeof(rcu_item_t);
910
911 const char *mem_suffix;
912 uint64_t mem_units;
913 bin_order_suffix(max_used_mem, &mem_units, &mem_suffix, false);
[5b6c033]914
[1c1da4b]915 TPRINTF("\nStress: Run %zu nop-readers and %zu updaters. %zu callbacks"
[2e16033]916 " total (max %" PRIu64 " %s used). Be very patient.\n",
917 reader_cnt, updater_cnt, exp_upd_calls, mem_units, mem_suffix);
[5b6c033]918
919 for (size_t k = 0; k < reader_cnt; ++k) {
920 run_one(stress_reader, &done);
921 }
922
923 for (size_t k = 0; k < updater_cnt; ++k) {
924 run_one(stress_updater, k > 0 ? &worker : &master);
925 }
926
927 TPRINTF("\nJoining %zu stress updaters.\n", updater_cnt);
928
929 for (size_t k = 0; k < updater_cnt; ++k) {
930 join_one();
931 }
932
933 done = true;
934
935 TPRINTF("\nJoining %zu stress nop-readers.\n", reader_cnt);
936
937 join_all();
938 return true;
939}
940/*-------------------------------------------------------------------*/
941
942typedef struct {
943 rcu_item_t r;
944 size_t total_cnt;
945 size_t count_down;
946 bool expedite;
947} expedite_t;
948
949static void expedite_cb(rcu_item_t *arg)
950{
951 expedite_t *e = (expedite_t *)arg;
952
953 if (1 < e->count_down) {
954 --e->count_down;
955
[1c1da4b]956 if (0 == (e->count_down % (e->total_cnt/100))) {
[5b6c033]957 TPRINTF("*");
958 }
959
960 _rcu_call(e->expedite, &e->r, expedite_cb);
961 } else {
[2e16033]962 /* Do not touch any of e's mem after we declare we're done with it. */
[5b6c033]963 memory_barrier();
964 e->count_down = 0;
965 }
966}
967
968static void run_expedite(bool exp, size_t cnt)
969{
970 expedite_t e;
971 e.total_cnt = cnt;
972 e.count_down = cnt;
973 e.expedite = exp;
974
975 _rcu_call(e.expedite, &e.r, expedite_cb);
976
977 while (0 < e.count_down) {
978 thread_sleep(1);
979 TPRINTF(".");
980 }
981}
982
983static bool do_expedite(void)
984{
985 size_t exp_cnt = 1000 * 1000;
986 size_t normal_cnt = 1 * 1000;
987
988 TPRINTF("Expedited: sequence of %zu rcu_calls\n", exp_cnt);
989 run_expedite(true, exp_cnt);
990 TPRINTF("Normal/non-expedited: sequence of %zu rcu_calls\n", normal_cnt);
991 run_expedite(false, normal_cnt);
992 return true;
993}
994/*-------------------------------------------------------------------*/
995
996struct test_func {
997 bool include;
998 bool (*func)(void);
999 const char *desc;
1000};
1001
1002
1003const char *test_rcu1(void)
1004{
1005 struct test_func test_func[] = {
1006 { 1, do_one_cb, "do_one_cb" },
1007 { 1, do_reader_preempt, "do_reader_preempt" },
1008 { 1, do_synch, "do_synch" },
[4ec9ea41]1009 { 1, do_barrier, "do_barrier" },
[5b6c033]1010 { 1, do_reader_exit, "do_reader_exit" },
1011 { 1, do_nop_readers, "do_nop_readers" },
1012 { 1, do_seq_check, "do_seq_check" },
1013 { 0, do_long_readers, "do_long_readers" },
1014 { 1, do_nop_callbacks, "do_nop_callbacks" },
1015 { 0, do_expedite, "do_expedite" },
1016 { 1, do_stress, "do_stress" },
[205832b]1017 { 0, NULL, NULL }
[5b6c033]1018 };
1019
1020 bool success = true;
1021 bool ok = true;
1022 uint64_t completed_gps = rcu_completed_gps();
1023 uint64_t delta_gps = 0;
1024
[205832b]1025 for (int i = 0; test_func[i].func; ++i) {
[5b6c033]1026 if (!test_func[i].include) {
1027 TPRINTF("\nSubtest %s() skipped.\n", test_func[i].desc);
1028 continue;
[2e16033]1029 } else {
1030 TPRINTF("\nRunning subtest %s.\n", test_func[i].desc);
[5b6c033]1031 }
1032
1033 ok = test_func[i].func();
1034 success = success && ok;
1035
1036 delta_gps = rcu_completed_gps() - completed_gps;
1037 completed_gps += delta_gps;
1038
1039 if (ok) {
1040 TPRINTF("\nSubtest %s() ok (GPs: %" PRIu64 ").\n",
1041 test_func[i].desc, delta_gps);
1042 } else {
1043 TPRINTF("\nFailed: %s(). Pausing for 5 secs.\n", test_func[i].desc);
1044 thread_sleep(5);
1045 }
1046 }
1047
1048 if (success)
[205832b]1049 return NULL;
[5b6c033]1050 else
1051 return "One of the tests failed.";
1052}
Note: See TracBrowser for help on using the repository browser.