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