acpi: add aml_string() term
[qemu/ar7.git] / tests / test-rcu-list.c
blob46b5e263e581bda9ea3b7eb160f16f2fdbd27b80
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 <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"
35 * Test variables.
38 long long n_reads = 0LL;
39 long long n_updates = 0LL;
40 long long n_reclaims = 0LL;
41 long long n_nodes_removed = 0LL;
42 long long n_nodes = 0LL;
43 int g_test_in_charge = 0;
45 int nthreadsrunning;
47 char argsbuf[64];
49 #define GOFLAG_INIT 0
50 #define GOFLAG_RUN 1
51 #define GOFLAG_STOP 2
53 static volatile int goflag = GOFLAG_INIT;
55 #define RCU_READ_RUN 1000
56 #define RCU_UPDATE_RUN 10
57 #define NR_THREADS 100
58 #define RCU_Q_LEN 100
60 static QemuThread threads[NR_THREADS];
61 static struct rcu_reader_data *data[NR_THREADS];
62 static int n_threads;
64 static int select_random_el(int max)
66 return (rand() % max);
70 static void create_thread(void *(*func)(void *))
72 if (n_threads >= NR_THREADS) {
73 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
74 exit(-1);
76 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
77 QEMU_THREAD_JOINABLE);
78 n_threads++;
81 static void wait_all_threads(void)
83 int i;
85 for (i = 0; i < n_threads; i++) {
86 qemu_thread_join(&threads[i]);
88 n_threads = 0;
92 struct list_element {
93 QLIST_ENTRY(list_element) entry;
94 struct rcu_head rcu;
95 long long val;
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 atomic_add(&n_reclaims, 1);
105 static QLIST_HEAD(q_list_head, list_element) Q_list_head;
107 static void *rcu_q_reader(void *arg)
109 long long j, n_reads_local = 0;
110 struct list_element *el;
112 *(struct rcu_reader_data **)arg = &rcu_reader;
113 atomic_inc(&nthreadsrunning);
114 while (goflag == GOFLAG_INIT) {
115 g_usleep(1000);
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;
128 rcu_read_unlock();
130 g_usleep(100);
132 atomic_add(&n_reads, n_reads_local);
133 return NULL;
137 static void *rcu_q_updater(void *arg)
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;
144 *(struct rcu_reader_data **)arg = &rcu_reader;
145 atomic_inc(&nthreadsrunning);
146 while (goflag == GOFLAG_INIT) {
147 g_usleep(1000);
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;
164 if (goflag == GOFLAG_STOP) {
165 break;
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;
180 n_updates_local += 2;
181 synchronize_rcu();
183 synchronize_rcu();
184 atomic_add(&n_updates, n_updates_local);
185 atomic_add(&n_nodes_removed, n_removed_local);
186 return NULL;
189 static void rcu_qtest_init(void)
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);
200 atomic_add(&n_nodes, RCU_Q_LEN);
203 static void rcu_qtest_run(int duration, int nreaders)
205 int nthreads = nreaders + 1;
206 while (atomic_read(&nthreadsrunning) < nthreads) {
207 g_usleep(1000);
210 goflag = GOFLAG_RUN;
211 sleep(duration);
212 goflag = GOFLAG_STOP;
213 wait_all_threads();
217 static void rcu_qtest(const char *test, int duration, int nreaders)
219 int i;
220 long long n_removed_local = 0;
222 struct list_element *el, *prev_el;
224 rcu_qtest_init();
225 for (i = 0; i < nreaders; i++) {
226 create_thread(rcu_q_reader);
228 create_thread(rcu_q_updater);
229 rcu_qtest_run(duration, nreaders);
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++;
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();
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);
252 static void usage(int argc, char *argv[])
254 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
255 exit(-1);
258 static int gtest_seconds;
260 static void gtest_rcuq_one(void)
262 rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
265 static void gtest_rcuq_few(void)
267 rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
270 static void gtest_rcuq_many(void)
272 rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
276 int main(int argc, char *argv[])
278 int duration = 0, readers = 0;
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;
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();
294 duration = strtoul(argv[1], NULL, 0);
296 if (argc >= 3) {
297 readers = strtoul(argv[2], NULL, 0);
299 if (duration && readers) {
300 rcu_qtest(argv[0], duration, readers);
301 return 0;
304 usage(argc, argv);
305 return -1;