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.
27 #include "qemu/atomic.h"
29 #include "qemu/compiler.h"
30 #include "qemu/osdep.h"
31 #include "qemu/thread.h"
32 #include "qemu/rcu_queue.h"
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;
53 static volatile int goflag = GOFLAG_INIT;
55 #define RCU_READ_RUN 1000
56 #define RCU_UPDATE_RUN 10
57 #define NR_THREADS 100
60 static QemuThread threads[NR_THREADS];
61 static struct rcu_reader_data *data[NR_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);
76 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
77 QEMU_THREAD_JOINABLE);
81 static void wait_all_threads(void)
85 for (i = 0; i < n_threads; i++) {
86 qemu_thread_join(&threads[i]);
93 QLIST_ENTRY(list_element) entry;
98 static void reclaim_list_el(struct rcu_head *prcu)
100 struct list_element *el = container_of(prcu, struct list_element, rcu);
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) {
118 while (goflag == GOFLAG_RUN) {
120 QLIST_FOREACH_RCU(el, &Q_list_head, entry) {
121 j = atomic_read(&el->val);
124 if (goflag == GOFLAG_STOP) {
132 atomic_add(&n_reads, n_reads_local);
137 static void *rcu_q_updater(void *arg)
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) {
150 while (goflag == GOFLAG_RUN) {
151 target_el = select_random_el(RCU_Q_LEN);
153 /* FOREACH_RCU could work here but let's use both macros */
154 QLIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
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);
164 if (goflag == GOFLAG_STOP) {
167 target_el = select_random_el(RCU_Q_LEN);
169 QLIST_FOREACH_RCU(el, &Q_list_head, entry) {
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);
180 n_updates_local += 2;
184 atomic_add(&n_updates, n_updates_local);
185 atomic_add(&n_nodes_removed, n_removed_local);
189 static void rcu_qtest_init(void)
191 struct list_element *new_el;
195 for (i = 0; i < RCU_Q_LEN; i++) {
196 new_el = g_new(struct list_element, 1);
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) {
212 goflag = GOFLAG_STOP;
217 static void rcu_qtest(const char *test, int duration, int nreaders)
220 long long n_removed_local = 0;
222 struct list_element *el, *prev_el;
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);
236 atomic_add(&n_nodes_removed, n_removed_local);
238 while (n_nodes_removed > n_reclaims) {
242 if (g_test_in_charge) {
243 g_assert_cmpint(n_nodes_removed, ==, n_reclaims);
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);
252 static void usage(int argc, char *argv[])
254 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
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;
281 if (argv[1][0] == '-') {
282 g_test_init(&argc, &argv, NULL);
283 if (g_test_quick()) {
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;
294 duration = strtoul(argv[1], NULL, 0);
297 readers = strtoul(argv[2], NULL, 0);
299 if (duration && readers) {
300 rcu_qtest(argv[0], duration, readers);