meson: specify fuzz linker script as a project arg
[qemu/ar7.git] / tests / test-rcu-list.c
blob92be51ec505b2b231fd1d331f87cc3168539bd48
1 /*
2 * rcuq_test.c
4 * usage: rcuq_test <readers> <duration>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Copyright (c) 2013 Mike D. Day, IBM Corporation.
23 #include "qemu/osdep.h"
24 #include "qemu/atomic.h"
25 #include "qemu/rcu.h"
26 #include "qemu/thread.h"
27 #include "qemu/rcu_queue.h"
30 * Test variables.
33 static QemuMutex counts_mutex;
34 static long long n_reads = 0LL;
35 static long long n_updates = 0LL;
36 static int64_t n_reclaims;
37 static int64_t n_nodes_removed;
38 static long long n_nodes = 0LL;
39 static int g_test_in_charge = 0;
41 static int nthreadsrunning;
43 #define GOFLAG_INIT 0
44 #define GOFLAG_RUN 1
45 #define GOFLAG_STOP 2
47 static int goflag = GOFLAG_INIT;
49 #define RCU_READ_RUN 1000
50 #define RCU_UPDATE_RUN 10
51 #define NR_THREADS 100
52 #define RCU_Q_LEN 100
54 static QemuThread threads[NR_THREADS];
55 static struct rcu_reader_data *data[NR_THREADS];
56 static int n_threads;
58 static int select_random_el(int max)
60 return (rand() % max);
64 static void create_thread(void *(*func)(void *))
66 if (n_threads >= NR_THREADS) {
67 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
68 exit(-1);
70 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
71 QEMU_THREAD_JOINABLE);
72 n_threads++;
75 static void wait_all_threads(void)
77 int i;
79 for (i = 0; i < n_threads; i++) {
80 qemu_thread_join(&threads[i]);
82 n_threads = 0;
85 #ifndef TEST_LIST_TYPE
86 #define TEST_LIST_TYPE 1
87 #endif
89 struct list_element {
90 #if TEST_LIST_TYPE == 1
91 QLIST_ENTRY(list_element) entry;
92 #elif TEST_LIST_TYPE == 2
93 QSIMPLEQ_ENTRY(list_element) entry;
94 #elif TEST_LIST_TYPE == 3
95 QTAILQ_ENTRY(list_element) entry;
96 #elif TEST_LIST_TYPE == 4
97 QSLIST_ENTRY(list_element) entry;
98 #else
99 #error Invalid TEST_LIST_TYPE
100 #endif
101 struct rcu_head rcu;
104 static void reclaim_list_el(struct rcu_head *prcu)
106 struct list_element *el = container_of(prcu, struct list_element, rcu);
107 g_free(el);
108 /* Accessed only from call_rcu thread. */
109 atomic_set_i64(&n_reclaims, n_reclaims + 1);
112 #if TEST_LIST_TYPE == 1
113 static QLIST_HEAD(, list_element) Q_list_head;
115 #define TEST_NAME "qlist"
116 #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
117 #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
118 #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
119 #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
120 #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
122 #elif TEST_LIST_TYPE == 2
123 static QSIMPLEQ_HEAD(, list_element) Q_list_head =
124 QSIMPLEQ_HEAD_INITIALIZER(Q_list_head);
126 #define TEST_NAME "qsimpleq"
127 #define TEST_LIST_REMOVE_RCU(el, f) \
128 QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f)
130 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
131 QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
133 #define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU
134 #define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU
135 #define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU
137 #elif TEST_LIST_TYPE == 3
138 static QTAILQ_HEAD(, list_element) Q_list_head;
140 #define TEST_NAME "qtailq"
141 #define TEST_LIST_REMOVE_RCU(el, f) QTAILQ_REMOVE_RCU(&Q_list_head, el, f)
143 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
144 QTAILQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
146 #define TEST_LIST_INSERT_HEAD_RCU QTAILQ_INSERT_HEAD_RCU
147 #define TEST_LIST_FOREACH_RCU QTAILQ_FOREACH_RCU
148 #define TEST_LIST_FOREACH_SAFE_RCU QTAILQ_FOREACH_SAFE_RCU
150 #elif TEST_LIST_TYPE == 4
151 static QSLIST_HEAD(, list_element) Q_list_head;
153 #define TEST_NAME "qslist"
154 #define TEST_LIST_REMOVE_RCU(el, f) \
155 QSLIST_REMOVE_RCU(&Q_list_head, el, list_element, f)
157 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
158 QSLIST_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
160 #define TEST_LIST_INSERT_HEAD_RCU QSLIST_INSERT_HEAD_RCU
161 #define TEST_LIST_FOREACH_RCU QSLIST_FOREACH_RCU
162 #define TEST_LIST_FOREACH_SAFE_RCU QSLIST_FOREACH_SAFE_RCU
163 #else
164 #error Invalid TEST_LIST_TYPE
165 #endif
167 static void *rcu_q_reader(void *arg)
169 long long n_reads_local = 0;
170 struct list_element *el;
172 rcu_register_thread();
174 *(struct rcu_reader_data **)arg = &rcu_reader;
175 atomic_inc(&nthreadsrunning);
176 while (atomic_read(&goflag) == GOFLAG_INIT) {
177 g_usleep(1000);
180 while (atomic_read(&goflag) == GOFLAG_RUN) {
181 rcu_read_lock();
182 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
183 n_reads_local++;
184 if (atomic_read(&goflag) == GOFLAG_STOP) {
185 break;
188 rcu_read_unlock();
190 g_usleep(100);
192 qemu_mutex_lock(&counts_mutex);
193 n_reads += n_reads_local;
194 qemu_mutex_unlock(&counts_mutex);
196 rcu_unregister_thread();
197 return NULL;
201 static void *rcu_q_updater(void *arg)
203 int j, target_el;
204 long long n_nodes_local = 0;
205 long long n_updates_local = 0;
206 long long n_removed_local = 0;
207 struct list_element *el, *prev_el;
209 *(struct rcu_reader_data **)arg = &rcu_reader;
210 atomic_inc(&nthreadsrunning);
211 while (atomic_read(&goflag) == GOFLAG_INIT) {
212 g_usleep(1000);
215 while (atomic_read(&goflag) == GOFLAG_RUN) {
216 target_el = select_random_el(RCU_Q_LEN);
217 j = 0;
218 /* FOREACH_RCU could work here but let's use both macros */
219 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
220 j++;
221 if (target_el == j) {
222 TEST_LIST_REMOVE_RCU(prev_el, entry);
223 /* may be more than one updater in the future */
224 call_rcu1(&prev_el->rcu, reclaim_list_el);
225 n_removed_local++;
226 break;
229 if (atomic_read(&goflag) == GOFLAG_STOP) {
230 break;
232 target_el = select_random_el(RCU_Q_LEN);
233 j = 0;
234 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
235 j++;
236 if (target_el == j) {
237 struct list_element *new_el = g_new(struct list_element, 1);
238 n_nodes_local++;
239 TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
240 break;
244 n_updates_local += 2;
245 synchronize_rcu();
247 synchronize_rcu();
248 qemu_mutex_lock(&counts_mutex);
249 n_nodes += n_nodes_local;
250 n_updates += n_updates_local;
251 atomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
252 qemu_mutex_unlock(&counts_mutex);
253 return NULL;
256 static void rcu_qtest_init(void)
258 struct list_element *new_el;
259 int i;
260 nthreadsrunning = 0;
261 srand(time(0));
262 for (i = 0; i < RCU_Q_LEN; i++) {
263 new_el = g_new(struct list_element, 1);
264 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
266 qemu_mutex_lock(&counts_mutex);
267 n_nodes += RCU_Q_LEN;
268 qemu_mutex_unlock(&counts_mutex);
271 static void rcu_qtest_run(int duration, int nreaders)
273 int nthreads = nreaders + 1;
274 while (atomic_read(&nthreadsrunning) < nthreads) {
275 g_usleep(1000);
278 atomic_set(&goflag, GOFLAG_RUN);
279 sleep(duration);
280 atomic_set(&goflag, GOFLAG_STOP);
281 wait_all_threads();
285 static void rcu_qtest(const char *test, int duration, int nreaders)
287 int i;
288 long long n_removed_local = 0;
290 struct list_element *el, *prev_el;
292 rcu_qtest_init();
293 for (i = 0; i < nreaders; i++) {
294 create_thread(rcu_q_reader);
296 create_thread(rcu_q_updater);
297 rcu_qtest_run(duration, nreaders);
299 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
300 TEST_LIST_REMOVE_RCU(prev_el, entry);
301 call_rcu1(&prev_el->rcu, reclaim_list_el);
302 n_removed_local++;
304 qemu_mutex_lock(&counts_mutex);
305 atomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
306 qemu_mutex_unlock(&counts_mutex);
307 synchronize_rcu();
308 while (atomic_read_i64(&n_nodes_removed) > atomic_read_i64(&n_reclaims)) {
309 g_usleep(100);
310 synchronize_rcu();
312 if (g_test_in_charge) {
313 g_assert_cmpint(atomic_read_i64(&n_nodes_removed), ==,
314 atomic_read_i64(&n_reclaims));
315 } else {
316 printf("%s: %d readers; 1 updater; nodes read: " \
317 "%lld, nodes removed: %"PRIi64"; nodes reclaimed: %"PRIi64"\n",
318 test, nthreadsrunning - 1, n_reads,
319 atomic_read_i64(&n_nodes_removed), atomic_read_i64(&n_reclaims));
320 exit(0);
324 static void usage(int argc, char *argv[])
326 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
327 exit(-1);
330 static int gtest_seconds;
332 static void gtest_rcuq_one(void)
334 rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
337 static void gtest_rcuq_few(void)
339 rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
342 static void gtest_rcuq_many(void)
344 rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
348 int main(int argc, char *argv[])
350 int duration = 0, readers = 0;
352 qemu_mutex_init(&counts_mutex);
353 if (argc >= 2) {
354 if (argv[1][0] == '-') {
355 g_test_init(&argc, &argv, NULL);
356 if (g_test_quick()) {
357 gtest_seconds = 4;
358 } else {
359 gtest_seconds = 20;
361 g_test_add_func("/rcu/"TEST_NAME"/single-threaded", gtest_rcuq_one);
362 g_test_add_func("/rcu/"TEST_NAME"/short-few", gtest_rcuq_few);
363 g_test_add_func("/rcu/"TEST_NAME"/long-many", gtest_rcuq_many);
364 g_test_in_charge = 1;
365 return g_test_run();
367 duration = strtoul(argv[1], NULL, 0);
369 if (argc >= 3) {
370 readers = strtoul(argv[2], NULL, 0);
372 if (duration && readers) {
373 rcu_qtest(argv[0], duration, readers);
374 return 0;
377 usage(argc, argv);
378 return -1;