test-rcu-list: abstract the list implementation
[qemu/ar7.git] / tests / test-rcu-list.c
blobf4f2a078b9696cca9f9cce5ce5d96dd20e1582b4
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 long long n_reclaims = 0LL;
37 static long long n_nodes_removed = 0LL;
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 #else
93 #error Invalid TEST_LIST_TYPE
94 #endif
95 struct rcu_head rcu;
98 static void reclaim_list_el(struct rcu_head *prcu)
100 struct list_element *el = container_of(prcu, struct list_element, rcu);
101 g_free(el);
102 /* Accessed only from call_rcu thread. */
103 n_reclaims++;
106 #if TEST_LIST_TYPE == 1
107 static QLIST_HEAD(q_list_head, list_element) Q_list_head;
109 #define TEST_NAME "qlist"
110 #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
111 #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
112 #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
113 #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
114 #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
115 #else
116 #error Invalid TEST_LIST_TYPE
117 #endif
119 static void *rcu_q_reader(void *arg)
121 long long n_reads_local = 0;
122 struct list_element *el;
124 rcu_register_thread();
126 *(struct rcu_reader_data **)arg = &rcu_reader;
127 atomic_inc(&nthreadsrunning);
128 while (atomic_read(&goflag) == GOFLAG_INIT) {
129 g_usleep(1000);
132 while (atomic_read(&goflag) == GOFLAG_RUN) {
133 rcu_read_lock();
134 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
135 n_reads_local++;
136 if (atomic_read(&goflag) == GOFLAG_STOP) {
137 break;
140 rcu_read_unlock();
142 g_usleep(100);
144 qemu_mutex_lock(&counts_mutex);
145 n_reads += n_reads_local;
146 qemu_mutex_unlock(&counts_mutex);
148 rcu_unregister_thread();
149 return NULL;
153 static void *rcu_q_updater(void *arg)
155 int j, target_el;
156 long long n_nodes_local = 0;
157 long long n_updates_local = 0;
158 long long n_removed_local = 0;
159 struct list_element *el, *prev_el;
161 *(struct rcu_reader_data **)arg = &rcu_reader;
162 atomic_inc(&nthreadsrunning);
163 while (atomic_read(&goflag) == GOFLAG_INIT) {
164 g_usleep(1000);
167 while (atomic_read(&goflag) == GOFLAG_RUN) {
168 target_el = select_random_el(RCU_Q_LEN);
169 j = 0;
170 /* FOREACH_RCU could work here but let's use both macros */
171 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
172 j++;
173 if (target_el == j) {
174 TEST_LIST_REMOVE_RCU(prev_el, entry);
175 /* may be more than one updater in the future */
176 call_rcu1(&prev_el->rcu, reclaim_list_el);
177 n_removed_local++;
178 break;
181 if (atomic_read(&goflag) == GOFLAG_STOP) {
182 break;
184 target_el = select_random_el(RCU_Q_LEN);
185 j = 0;
186 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
187 j++;
188 if (target_el == j) {
189 struct list_element *new_el = g_new(struct list_element, 1);
190 n_nodes += n_nodes_local;
191 TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
192 break;
196 n_updates_local += 2;
197 synchronize_rcu();
199 synchronize_rcu();
200 qemu_mutex_lock(&counts_mutex);
201 n_nodes += n_nodes_local;
202 n_updates += n_updates_local;
203 n_nodes_removed += n_removed_local;
204 qemu_mutex_unlock(&counts_mutex);
205 return NULL;
208 static void rcu_qtest_init(void)
210 struct list_element *new_el;
211 int i;
212 nthreadsrunning = 0;
213 srand(time(0));
214 for (i = 0; i < RCU_Q_LEN; i++) {
215 new_el = g_new(struct list_element, 1);
216 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
218 qemu_mutex_lock(&counts_mutex);
219 n_nodes += RCU_Q_LEN;
220 qemu_mutex_unlock(&counts_mutex);
223 static void rcu_qtest_run(int duration, int nreaders)
225 int nthreads = nreaders + 1;
226 while (atomic_read(&nthreadsrunning) < nthreads) {
227 g_usleep(1000);
230 atomic_set(&goflag, GOFLAG_RUN);
231 sleep(duration);
232 atomic_set(&goflag, GOFLAG_STOP);
233 wait_all_threads();
237 static void rcu_qtest(const char *test, int duration, int nreaders)
239 int i;
240 long long n_removed_local = 0;
242 struct list_element *el, *prev_el;
244 rcu_qtest_init();
245 for (i = 0; i < nreaders; i++) {
246 create_thread(rcu_q_reader);
248 create_thread(rcu_q_updater);
249 rcu_qtest_run(duration, nreaders);
251 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
252 TEST_LIST_REMOVE_RCU(prev_el, entry);
253 call_rcu1(&prev_el->rcu, reclaim_list_el);
254 n_removed_local++;
256 qemu_mutex_lock(&counts_mutex);
257 n_nodes_removed += n_removed_local;
258 qemu_mutex_unlock(&counts_mutex);
259 synchronize_rcu();
260 while (n_nodes_removed > n_reclaims) {
261 g_usleep(100);
262 synchronize_rcu();
264 if (g_test_in_charge) {
265 g_assert_cmpint(n_nodes_removed, ==, n_reclaims);
266 } else {
267 printf("%s: %d readers; 1 updater; nodes read: " \
268 "%lld, nodes removed: %lld; nodes reclaimed: %lld\n",
269 test, nthreadsrunning - 1, n_reads, n_nodes_removed, n_reclaims);
270 exit(0);
274 static void usage(int argc, char *argv[])
276 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
277 exit(-1);
280 static int gtest_seconds;
282 static void gtest_rcuq_one(void)
284 rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
287 static void gtest_rcuq_few(void)
289 rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
292 static void gtest_rcuq_many(void)
294 rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
298 int main(int argc, char *argv[])
300 int duration = 0, readers = 0;
302 qemu_mutex_init(&counts_mutex);
303 if (argc >= 2) {
304 if (argv[1][0] == '-') {
305 g_test_init(&argc, &argv, NULL);
306 if (g_test_quick()) {
307 gtest_seconds = 4;
308 } else {
309 gtest_seconds = 20;
311 g_test_add_func("/rcu/"TEST_NAME"/single-threaded", gtest_rcuq_one);
312 g_test_add_func("/rcu/"TEST_NAME"/short-few", gtest_rcuq_few);
313 g_test_add_func("/rcu/"TEST_NAME"/long-many", gtest_rcuq_many);
314 g_test_in_charge = 1;
315 return g_test_run();
317 duration = strtoul(argv[1], NULL, 0);
319 if (argc >= 3) {
320 readers = strtoul(argv[2], NULL, 0);
322 if (duration && readers) {
323 rcu_qtest(argv[0], duration, readers);
324 return 0;
327 usage(argc, argv);
328 return -1;