]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-rcu-list.c
test-rcu-list: abstract the list implementation
[mirror_qemu.git] / tests / test-rcu-list.c
CommitLineData
341774fe
MD
1/*
2 * rcuq_test.c
3 *
4 * usage: rcuq_test <readers> <duration>
5 *
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.
10 *
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.
15 *
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.
19 *
20 * Copyright (c) 2013 Mike D. Day, IBM Corporation.
21 */
22
681c28a3 23#include "qemu/osdep.h"
341774fe
MD
24#include "qemu/atomic.h"
25#include "qemu/rcu.h"
341774fe
MD
26#include "qemu/thread.h"
27#include "qemu/rcu_queue.h"
28
29/*
30 * Test variables.
31 */
32
8a5956ad
PB
33static QemuMutex counts_mutex;
34static long long n_reads = 0LL;
35static long long n_updates = 0LL;
36static long long n_reclaims = 0LL;
37static long long n_nodes_removed = 0LL;
38static long long n_nodes = 0LL;
39static int g_test_in_charge = 0;
341774fe 40
8a5956ad 41static int nthreadsrunning;
341774fe
MD
42
43#define GOFLAG_INIT 0
44#define GOFLAG_RUN 1
45#define GOFLAG_STOP 2
46
23311b81 47static int goflag = GOFLAG_INIT;
341774fe
MD
48
49#define RCU_READ_RUN 1000
50#define RCU_UPDATE_RUN 10
51#define NR_THREADS 100
52#define RCU_Q_LEN 100
53
54static QemuThread threads[NR_THREADS];
55static struct rcu_reader_data *data[NR_THREADS];
56static int n_threads;
57
58static int select_random_el(int max)
59{
60 return (rand() % max);
61}
62
63
64static void create_thread(void *(*func)(void *))
65{
66 if (n_threads >= NR_THREADS) {
67 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
68 exit(-1);
69 }
70 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
71 QEMU_THREAD_JOINABLE);
72 n_threads++;
73}
74
75static void wait_all_threads(void)
76{
77 int i;
78
79 for (i = 0; i < n_threads; i++) {
80 qemu_thread_join(&threads[i]);
81 }
82 n_threads = 0;
83}
84
685cc7c0
EC
85#ifndef TEST_LIST_TYPE
86#define TEST_LIST_TYPE 1
87#endif
341774fe
MD
88
89struct list_element {
685cc7c0 90#if TEST_LIST_TYPE == 1
341774fe 91 QLIST_ENTRY(list_element) entry;
685cc7c0
EC
92#else
93#error Invalid TEST_LIST_TYPE
94#endif
341774fe 95 struct rcu_head rcu;
341774fe
MD
96};
97
98static void reclaim_list_el(struct rcu_head *prcu)
99{
100 struct list_element *el = container_of(prcu, struct list_element, rcu);
101 g_free(el);
8a5956ad
PB
102 /* Accessed only from call_rcu thread. */
103 n_reclaims++;
341774fe
MD
104}
105
685cc7c0 106#if TEST_LIST_TYPE == 1
341774fe
MD
107static QLIST_HEAD(q_list_head, list_element) Q_list_head;
108
685cc7c0
EC
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
118
341774fe
MD
119static void *rcu_q_reader(void *arg)
120{
8a5956ad 121 long long n_reads_local = 0;
341774fe
MD
122 struct list_element *el;
123
ab28bd23
PB
124 rcu_register_thread();
125
341774fe
MD
126 *(struct rcu_reader_data **)arg = &rcu_reader;
127 atomic_inc(&nthreadsrunning);
23311b81 128 while (atomic_read(&goflag) == GOFLAG_INIT) {
341774fe
MD
129 g_usleep(1000);
130 }
131
23311b81 132 while (atomic_read(&goflag) == GOFLAG_RUN) {
341774fe 133 rcu_read_lock();
685cc7c0 134 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
341774fe 135 n_reads_local++;
23311b81 136 if (atomic_read(&goflag) == GOFLAG_STOP) {
341774fe
MD
137 break;
138 }
139 }
140 rcu_read_unlock();
141
142 g_usleep(100);
143 }
8a5956ad
PB
144 qemu_mutex_lock(&counts_mutex);
145 n_reads += n_reads_local;
146 qemu_mutex_unlock(&counts_mutex);
ab28bd23
PB
147
148 rcu_unregister_thread();
341774fe
MD
149 return NULL;
150}
151
152
153static void *rcu_q_updater(void *arg)
154{
155 int j, target_el;
8a5956ad 156 long long n_nodes_local = 0;
341774fe
MD
157 long long n_updates_local = 0;
158 long long n_removed_local = 0;
159 struct list_element *el, *prev_el;
160
161 *(struct rcu_reader_data **)arg = &rcu_reader;
162 atomic_inc(&nthreadsrunning);
23311b81 163 while (atomic_read(&goflag) == GOFLAG_INIT) {
341774fe
MD
164 g_usleep(1000);
165 }
166
23311b81 167 while (atomic_read(&goflag) == GOFLAG_RUN) {
341774fe
MD
168 target_el = select_random_el(RCU_Q_LEN);
169 j = 0;
170 /* FOREACH_RCU could work here but let's use both macros */
685cc7c0 171 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
341774fe
MD
172 j++;
173 if (target_el == j) {
685cc7c0 174 TEST_LIST_REMOVE_RCU(prev_el, entry);
341774fe
MD
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;
179 }
180 }
23311b81 181 if (atomic_read(&goflag) == GOFLAG_STOP) {
341774fe
MD
182 break;
183 }
184 target_el = select_random_el(RCU_Q_LEN);
185 j = 0;
685cc7c0 186 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
341774fe
MD
187 j++;
188 if (target_el == j) {
685cc7c0 189 struct list_element *new_el = g_new(struct list_element, 1);
8a5956ad 190 n_nodes += n_nodes_local;
685cc7c0 191 TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
341774fe
MD
192 break;
193 }
194 }
195
196 n_updates_local += 2;
197 synchronize_rcu();
198 }
199 synchronize_rcu();
8a5956ad
PB
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);
341774fe
MD
205 return NULL;
206}
207
208static void rcu_qtest_init(void)
209{
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);
685cc7c0 216 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
341774fe 217 }
8a5956ad
PB
218 qemu_mutex_lock(&counts_mutex);
219 n_nodes += RCU_Q_LEN;
220 qemu_mutex_unlock(&counts_mutex);
341774fe
MD
221}
222
223static void rcu_qtest_run(int duration, int nreaders)
224{
225 int nthreads = nreaders + 1;
226 while (atomic_read(&nthreadsrunning) < nthreads) {
227 g_usleep(1000);
228 }
229
23311b81 230 atomic_set(&goflag, GOFLAG_RUN);
341774fe 231 sleep(duration);
23311b81 232 atomic_set(&goflag, GOFLAG_STOP);
341774fe
MD
233 wait_all_threads();
234}
235
236
237static void rcu_qtest(const char *test, int duration, int nreaders)
238{
239 int i;
240 long long n_removed_local = 0;
241
242 struct list_element *el, *prev_el;
243
244 rcu_qtest_init();
245 for (i = 0; i < nreaders; i++) {
246 create_thread(rcu_q_reader);
247 }
248 create_thread(rcu_q_updater);
249 rcu_qtest_run(duration, nreaders);
250
685cc7c0
EC
251 TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
252 TEST_LIST_REMOVE_RCU(prev_el, entry);
341774fe
MD
253 call_rcu1(&prev_el->rcu, reclaim_list_el);
254 n_removed_local++;
255 }
8a5956ad
PB
256 qemu_mutex_lock(&counts_mutex);
257 n_nodes_removed += n_removed_local;
258 qemu_mutex_unlock(&counts_mutex);
341774fe
MD
259 synchronize_rcu();
260 while (n_nodes_removed > n_reclaims) {
261 g_usleep(100);
262 synchronize_rcu();
263 }
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);
271 }
272}
273
274static void usage(int argc, char *argv[])
275{
276 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
277 exit(-1);
278}
279
280static int gtest_seconds;
281
282static void gtest_rcuq_one(void)
283{
284 rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
285}
286
287static void gtest_rcuq_few(void)
288{
289 rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
290}
291
292static void gtest_rcuq_many(void)
293{
294 rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
295}
296
297
298int main(int argc, char *argv[])
299{
300 int duration = 0, readers = 0;
301
8a5956ad 302 qemu_mutex_init(&counts_mutex);
341774fe
MD
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;
310 }
685cc7c0
EC
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);
341774fe
MD
314 g_test_in_charge = 1;
315 return g_test_run();
316 }
317 duration = strtoul(argv[1], NULL, 0);
318 }
319 if (argc >= 3) {
320 readers = strtoul(argv[2], NULL, 0);
321 }
322 if (duration && readers) {
323 rcu_qtest(argv[0], duration, readers);
324 return 0;
325 }
326
327 usage(argc, argv);
328 return -1;
329}