qapi/trace: Tidy up trace-event-get-state, -set-state documentation
[qemu/armbru.git] / tests / unit / test-rcu-list.c
blob8f0adb8b00a3ae4dfd576a82f1b376947b323b1d
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, see <https://www.gnu.org/licenses/>.
19 * Copyright (c) 2013 Mike D. Day, IBM Corporation.
22 #include "qemu/osdep.h"
23 #include "qemu/atomic.h"
24 #include "qemu/rcu.h"
25 #include "qemu/thread.h"
26 #include "qemu/rcu_queue.h"
29 * Test variables.
32 static QemuMutex counts_mutex;
33 static long long n_reads = 0LL;
34 static long long n_updates = 0LL;
35 static int64_t n_reclaims;
36 static int64_t n_nodes_removed;
37 static long long n_nodes = 0LL;
38 static int g_test_in_charge = 0;
40 static int nthreadsrunning;
42 #define GOFLAG_INIT 0
43 #define GOFLAG_RUN 1
44 #define GOFLAG_STOP 2
46 static int goflag = GOFLAG_INIT;
48 #define RCU_READ_RUN 1000
49 #define RCU_UPDATE_RUN 10
50 #define NR_THREADS 100
51 #define RCU_Q_LEN 100
53 static QemuThread threads[NR_THREADS];
54 static struct rcu_reader_data *data[NR_THREADS];
55 static int n_threads;
57 static int select_random_el(int max)
59 return (rand() % max);
63 static void create_thread(void *(*func)(void *))
65 if (n_threads >= NR_THREADS) {
66 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
67 exit(-1);
69 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
70 QEMU_THREAD_JOINABLE);
71 n_threads++;
74 static void wait_all_threads(void)
76 int i;
78 for (i = 0; i < n_threads; i++) {
79 qemu_thread_join(&threads[i]);
81 n_threads = 0;
84 #ifndef TEST_LIST_TYPE
85 #define TEST_LIST_TYPE 1
86 #endif
88 struct list_element {
89 #if TEST_LIST_TYPE == 1
90 QLIST_ENTRY(list_element) entry;
91 #elif TEST_LIST_TYPE == 2
92 QSIMPLEQ_ENTRY(list_element) entry;
93 #elif TEST_LIST_TYPE == 3
94 QTAILQ_ENTRY(list_element) entry;
95 #elif TEST_LIST_TYPE == 4
96 QSLIST_ENTRY(list_element) entry;
97 #else
98 #error Invalid TEST_LIST_TYPE
99 #endif
100 struct rcu_head rcu;
103 static void reclaim_list_el(struct rcu_head *prcu)
105 struct list_element *el = container_of(prcu, struct list_element, rcu);
106 g_free(el);
107 /* Accessed only from call_rcu thread. */
108 qatomic_set_i64(&n_reclaims, n_reclaims + 1);
111 #if TEST_LIST_TYPE == 1
112 static QLIST_HEAD(, list_element) Q_list_head;
114 #define TEST_NAME "qlist"
115 #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
116 #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
117 #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
118 #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
119 #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
121 #elif TEST_LIST_TYPE == 2
122 static QSIMPLEQ_HEAD(, list_element) Q_list_head =
123 QSIMPLEQ_HEAD_INITIALIZER(Q_list_head);
125 #define TEST_NAME "qsimpleq"
126 #define TEST_LIST_REMOVE_RCU(el, f) \
127 QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f)
129 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
130 QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
132 #define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU
133 #define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU
134 #define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU
136 #elif TEST_LIST_TYPE == 3
137 static QTAILQ_HEAD(, list_element) Q_list_head;
139 #define TEST_NAME "qtailq"
140 #define TEST_LIST_REMOVE_RCU(el, f) QTAILQ_REMOVE_RCU(&Q_list_head, el, f)
142 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
143 QTAILQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
145 #define TEST_LIST_INSERT_HEAD_RCU QTAILQ_INSERT_HEAD_RCU
146 #define TEST_LIST_FOREACH_RCU QTAILQ_FOREACH_RCU
147 #define TEST_LIST_FOREACH_SAFE_RCU QTAILQ_FOREACH_SAFE_RCU
149 #elif TEST_LIST_TYPE == 4
150 static QSLIST_HEAD(, list_element) Q_list_head;
152 #define TEST_NAME "qslist"
153 #define TEST_LIST_REMOVE_RCU(el, f) \
154 QSLIST_REMOVE_RCU(&Q_list_head, el, list_element, f)
156 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
157 QSLIST_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
159 #define TEST_LIST_INSERT_HEAD_RCU QSLIST_INSERT_HEAD_RCU
160 #define TEST_LIST_FOREACH_RCU QSLIST_FOREACH_RCU
161 #define TEST_LIST_FOREACH_SAFE_RCU QSLIST_FOREACH_SAFE_RCU
162 #else
163 #error Invalid TEST_LIST_TYPE
164 #endif
166 static void *rcu_q_reader(void *arg)
168 long long n_reads_local = 0;
169 struct list_element *el;
171 rcu_register_thread();
173 *(struct rcu_reader_data **)arg = get_ptr_rcu_reader();
174 qatomic_inc(&nthreadsrunning);
175 while (qatomic_read(&goflag) == GOFLAG_INIT) {
176 g_usleep(1000);
179 while (qatomic_read(&goflag) == GOFLAG_RUN) {
180 rcu_read_lock();
181 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
182 n_reads_local++;
183 if (qatomic_read(&goflag) == GOFLAG_STOP) {
184 break;
187 rcu_read_unlock();
189 g_usleep(100);
191 qemu_mutex_lock(&counts_mutex);
192 n_reads += n_reads_local;
193 qemu_mutex_unlock(&counts_mutex);
195 rcu_unregister_thread();
196 return NULL;
200 static void *rcu_q_updater(void *arg)
202 int j, target_el;
203 long long n_nodes_local = 0;
204 long long n_updates_local = 0;
205 long long n_removed_local = 0;
206 struct list_element *el, *prev_el;
208 *(struct rcu_reader_data **)arg = get_ptr_rcu_reader();
209 qatomic_inc(&nthreadsrunning);
210 while (qatomic_read(&goflag) == GOFLAG_INIT) {
211 g_usleep(1000);
214 while (qatomic_read(&goflag) == GOFLAG_RUN) {
215 target_el = select_random_el(RCU_Q_LEN);
216 j = 0;
217 /* FOREACH_RCU could work here but let's use both macros */
218 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
219 j++;
220 if (target_el == j) {
221 TEST_LIST_REMOVE_RCU(prev_el, entry);
222 /* may be more than one updater in the future */
223 call_rcu1(&prev_el->rcu, reclaim_list_el);
224 n_removed_local++;
225 break;
228 if (qatomic_read(&goflag) == GOFLAG_STOP) {
229 break;
231 target_el = select_random_el(RCU_Q_LEN);
232 j = 0;
233 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
234 j++;
235 if (target_el == j) {
236 struct list_element *new_el = g_new(struct list_element, 1);
237 n_nodes_local++;
238 TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
239 break;
243 n_updates_local += 2;
244 synchronize_rcu();
246 synchronize_rcu();
247 qemu_mutex_lock(&counts_mutex);
248 n_nodes += n_nodes_local;
249 n_updates += n_updates_local;
250 qatomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
251 qemu_mutex_unlock(&counts_mutex);
252 return NULL;
255 static void rcu_qtest_init(void)
257 struct list_element *new_el;
258 int i;
259 nthreadsrunning = 0;
260 srand(time(0));
261 for (i = 0; i < RCU_Q_LEN; i++) {
262 new_el = g_new(struct list_element, 1);
263 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
265 qemu_mutex_lock(&counts_mutex);
266 n_nodes += RCU_Q_LEN;
267 qemu_mutex_unlock(&counts_mutex);
270 static void rcu_qtest_run(int duration, int nreaders)
272 int nthreads = nreaders + 1;
273 while (qatomic_read(&nthreadsrunning) < nthreads) {
274 g_usleep(1000);
277 qatomic_set(&goflag, GOFLAG_RUN);
278 sleep(duration);
279 qatomic_set(&goflag, GOFLAG_STOP);
280 wait_all_threads();
284 static void rcu_qtest(const char *test, int duration, int nreaders)
286 int i;
287 long long n_removed_local = 0;
289 struct list_element *el, *prev_el;
291 rcu_qtest_init();
292 for (i = 0; i < nreaders; i++) {
293 create_thread(rcu_q_reader);
295 create_thread(rcu_q_updater);
296 rcu_qtest_run(duration, nreaders);
298 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
299 TEST_LIST_REMOVE_RCU(prev_el, entry);
300 call_rcu1(&prev_el->rcu, reclaim_list_el);
301 n_removed_local++;
303 qemu_mutex_lock(&counts_mutex);
304 qatomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
305 qemu_mutex_unlock(&counts_mutex);
306 synchronize_rcu();
307 while (qatomic_read_i64(&n_nodes_removed) >
308 qatomic_read_i64(&n_reclaims)) {
309 g_usleep(100);
310 synchronize_rcu();
312 if (g_test_in_charge) {
313 g_assert_cmpint(qatomic_read_i64(&n_nodes_removed), ==,
314 qatomic_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 qatomic_read_i64(&n_nodes_removed),
320 qatomic_read_i64(&n_reclaims));
321 exit(0);
325 static void usage(int argc, char *argv[])
327 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
328 exit(-1);
331 static int gtest_seconds;
333 static void gtest_rcuq_one(void)
335 rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
338 static void gtest_rcuq_few(void)
340 rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
343 static void gtest_rcuq_many(void)
345 rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
349 int main(int argc, char *argv[])
351 int duration = 0, readers = 0;
353 qemu_mutex_init(&counts_mutex);
354 if (argc >= 2) {
355 if (argv[1][0] == '-') {
356 g_test_init(&argc, &argv, NULL);
357 if (g_test_quick()) {
358 gtest_seconds = 4;
359 } else {
360 gtest_seconds = 20;
362 g_test_add_func("/rcu/"TEST_NAME"/single-threaded", gtest_rcuq_one);
363 g_test_add_func("/rcu/"TEST_NAME"/short-few", gtest_rcuq_few);
364 g_test_add_func("/rcu/"TEST_NAME"/long-many", gtest_rcuq_many);
365 g_test_in_charge = 1;
366 return g_test_run();
368 duration = strtoul(argv[1], NULL, 0);
370 if (argc >= 3) {
371 readers = strtoul(argv[2], NULL, 0);
373 if (duration && readers) {
374 rcu_qtest(argv[0], duration, readers);
375 return 0;
378 usage(argc, argv);
379 return -1;