]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-rcu-list.c
tests/i440fx-test: Don't define ARRAY_SIZE locally
[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
23#include <glib.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include "qemu/atomic.h"
28#include "qemu/rcu.h"
29#include "qemu/compiler.h"
30#include "qemu/osdep.h"
31#include "qemu/thread.h"
32#include "qemu/rcu_queue.h"
33
34/*
35 * Test variables.
36 */
37
8a5956ad
PB
38static QemuMutex counts_mutex;
39static long long n_reads = 0LL;
40static long long n_updates = 0LL;
41static long long n_reclaims = 0LL;
42static long long n_nodes_removed = 0LL;
43static long long n_nodes = 0LL;
44static int g_test_in_charge = 0;
341774fe 45
8a5956ad 46static int nthreadsrunning;
341774fe
MD
47
48#define GOFLAG_INIT 0
49#define GOFLAG_RUN 1
50#define GOFLAG_STOP 2
51
52static volatile int goflag = GOFLAG_INIT;
53
54#define RCU_READ_RUN 1000
55#define RCU_UPDATE_RUN 10
56#define NR_THREADS 100
57#define RCU_Q_LEN 100
58
59static QemuThread threads[NR_THREADS];
60static struct rcu_reader_data *data[NR_THREADS];
61static int n_threads;
62
63static int select_random_el(int max)
64{
65 return (rand() % max);
66}
67
68
69static void create_thread(void *(*func)(void *))
70{
71 if (n_threads >= NR_THREADS) {
72 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
73 exit(-1);
74 }
75 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
76 QEMU_THREAD_JOINABLE);
77 n_threads++;
78}
79
80static void wait_all_threads(void)
81{
82 int i;
83
84 for (i = 0; i < n_threads; i++) {
85 qemu_thread_join(&threads[i]);
86 }
87 n_threads = 0;
88}
89
90
91struct list_element {
92 QLIST_ENTRY(list_element) entry;
93 struct rcu_head rcu;
341774fe
MD
94};
95
96static void reclaim_list_el(struct rcu_head *prcu)
97{
98 struct list_element *el = container_of(prcu, struct list_element, rcu);
99 g_free(el);
8a5956ad
PB
100 /* Accessed only from call_rcu thread. */
101 n_reclaims++;
341774fe
MD
102}
103
104static QLIST_HEAD(q_list_head, list_element) Q_list_head;
105
106static void *rcu_q_reader(void *arg)
107{
8a5956ad 108 long long n_reads_local = 0;
341774fe
MD
109 struct list_element *el;
110
ab28bd23
PB
111 rcu_register_thread();
112
341774fe
MD
113 *(struct rcu_reader_data **)arg = &rcu_reader;
114 atomic_inc(&nthreadsrunning);
115 while (goflag == GOFLAG_INIT) {
116 g_usleep(1000);
117 }
118
119 while (goflag == GOFLAG_RUN) {
120 rcu_read_lock();
121 QLIST_FOREACH_RCU(el, &Q_list_head, entry) {
341774fe
MD
122 n_reads_local++;
123 if (goflag == GOFLAG_STOP) {
124 break;
125 }
126 }
127 rcu_read_unlock();
128
129 g_usleep(100);
130 }
8a5956ad
PB
131 qemu_mutex_lock(&counts_mutex);
132 n_reads += n_reads_local;
133 qemu_mutex_unlock(&counts_mutex);
ab28bd23
PB
134
135 rcu_unregister_thread();
341774fe
MD
136 return NULL;
137}
138
139
140static void *rcu_q_updater(void *arg)
141{
142 int j, target_el;
8a5956ad 143 long long n_nodes_local = 0;
341774fe
MD
144 long long n_updates_local = 0;
145 long long n_removed_local = 0;
146 struct list_element *el, *prev_el;
147
148 *(struct rcu_reader_data **)arg = &rcu_reader;
149 atomic_inc(&nthreadsrunning);
150 while (goflag == GOFLAG_INIT) {
151 g_usleep(1000);
152 }
153
154 while (goflag == GOFLAG_RUN) {
155 target_el = select_random_el(RCU_Q_LEN);
156 j = 0;
157 /* FOREACH_RCU could work here but let's use both macros */
158 QLIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
159 j++;
160 if (target_el == j) {
161 QLIST_REMOVE_RCU(prev_el, entry);
162 /* may be more than one updater in the future */
163 call_rcu1(&prev_el->rcu, reclaim_list_el);
164 n_removed_local++;
165 break;
166 }
167 }
168 if (goflag == GOFLAG_STOP) {
169 break;
170 }
171 target_el = select_random_el(RCU_Q_LEN);
172 j = 0;
173 QLIST_FOREACH_RCU(el, &Q_list_head, entry) {
174 j++;
175 if (target_el == j) {
176 prev_el = g_new(struct list_element, 1);
8a5956ad 177 n_nodes += n_nodes_local;
341774fe
MD
178 QLIST_INSERT_BEFORE_RCU(el, prev_el, entry);
179 break;
180 }
181 }
182
183 n_updates_local += 2;
184 synchronize_rcu();
185 }
186 synchronize_rcu();
8a5956ad
PB
187 qemu_mutex_lock(&counts_mutex);
188 n_nodes += n_nodes_local;
189 n_updates += n_updates_local;
190 n_nodes_removed += n_removed_local;
191 qemu_mutex_unlock(&counts_mutex);
341774fe
MD
192 return NULL;
193}
194
195static void rcu_qtest_init(void)
196{
197 struct list_element *new_el;
198 int i;
199 nthreadsrunning = 0;
200 srand(time(0));
201 for (i = 0; i < RCU_Q_LEN; i++) {
202 new_el = g_new(struct list_element, 1);
341774fe
MD
203 QLIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
204 }
8a5956ad
PB
205 qemu_mutex_lock(&counts_mutex);
206 n_nodes += RCU_Q_LEN;
207 qemu_mutex_unlock(&counts_mutex);
341774fe
MD
208}
209
210static void rcu_qtest_run(int duration, int nreaders)
211{
212 int nthreads = nreaders + 1;
213 while (atomic_read(&nthreadsrunning) < nthreads) {
214 g_usleep(1000);
215 }
216
217 goflag = GOFLAG_RUN;
218 sleep(duration);
219 goflag = GOFLAG_STOP;
220 wait_all_threads();
221}
222
223
224static void rcu_qtest(const char *test, int duration, int nreaders)
225{
226 int i;
227 long long n_removed_local = 0;
228
229 struct list_element *el, *prev_el;
230
231 rcu_qtest_init();
232 for (i = 0; i < nreaders; i++) {
233 create_thread(rcu_q_reader);
234 }
235 create_thread(rcu_q_updater);
236 rcu_qtest_run(duration, nreaders);
237
238 QLIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
239 QLIST_REMOVE_RCU(prev_el, entry);
240 call_rcu1(&prev_el->rcu, reclaim_list_el);
241 n_removed_local++;
242 }
8a5956ad
PB
243 qemu_mutex_lock(&counts_mutex);
244 n_nodes_removed += n_removed_local;
245 qemu_mutex_unlock(&counts_mutex);
341774fe
MD
246 synchronize_rcu();
247 while (n_nodes_removed > n_reclaims) {
248 g_usleep(100);
249 synchronize_rcu();
250 }
251 if (g_test_in_charge) {
252 g_assert_cmpint(n_nodes_removed, ==, n_reclaims);
253 } else {
254 printf("%s: %d readers; 1 updater; nodes read: " \
255 "%lld, nodes removed: %lld; nodes reclaimed: %lld\n",
256 test, nthreadsrunning - 1, n_reads, n_nodes_removed, n_reclaims);
257 exit(0);
258 }
259}
260
261static void usage(int argc, char *argv[])
262{
263 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
264 exit(-1);
265}
266
267static int gtest_seconds;
268
269static void gtest_rcuq_one(void)
270{
271 rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
272}
273
274static void gtest_rcuq_few(void)
275{
276 rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
277}
278
279static void gtest_rcuq_many(void)
280{
281 rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
282}
283
284
285int main(int argc, char *argv[])
286{
287 int duration = 0, readers = 0;
288
8a5956ad 289 qemu_mutex_init(&counts_mutex);
341774fe
MD
290 if (argc >= 2) {
291 if (argv[1][0] == '-') {
292 g_test_init(&argc, &argv, NULL);
293 if (g_test_quick()) {
294 gtest_seconds = 4;
295 } else {
296 gtest_seconds = 20;
297 }
298 g_test_add_func("/rcu/qlist/single-threaded", gtest_rcuq_one);
299 g_test_add_func("/rcu/qlist/short-few", gtest_rcuq_few);
300 g_test_add_func("/rcu/qlist/long-many", gtest_rcuq_many);
301 g_test_in_charge = 1;
302 return g_test_run();
303 }
304 duration = strtoul(argv[1], NULL, 0);
305 }
306 if (argc >= 3) {
307 readers = strtoul(argv[2], NULL, 0);
308 }
309 if (duration && readers) {
310 rcu_qtest(argv[0], duration, readers);
311 return 0;
312 }
313
314 usage(argc, argv);
315 return -1;
316}