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 "qemu/osdep.h"
24 #include "qemu/atomic.h"
26 #include "qemu/thread.h"
27 #include "qemu/rcu_queue.h"
33 static QemuMutex counts_mutex
;
34 static long long n_reads
= 0LL;
35 static long long n_updates
= 0LL;
36 static long long n_reclaims
= 0LL;
37 static long long n_nodes_removed
= 0LL;
38 static long long n_nodes
= 0LL;
39 static int g_test_in_charge
= 0;
41 static int nthreadsrunning
;
47 static int goflag
= GOFLAG_INIT
;
49 #define RCU_READ_RUN 1000
50 #define RCU_UPDATE_RUN 10
51 #define NR_THREADS 100
54 static QemuThread threads
[NR_THREADS
];
55 static struct rcu_reader_data
*data
[NR_THREADS
];
58 static int select_random_el(int max
)
60 return (rand() % max
);
64 static void create_thread(void *(*func
)(void *))
66 if (n_threads
>= NR_THREADS
) {
67 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
70 qemu_thread_create(&threads
[n_threads
], "test", func
, &data
[n_threads
],
71 QEMU_THREAD_JOINABLE
);
75 static void wait_all_threads(void)
79 for (i
= 0; i
< n_threads
; i
++) {
80 qemu_thread_join(&threads
[i
]);
85 #ifndef TEST_LIST_TYPE
86 #define TEST_LIST_TYPE 1
90 #if TEST_LIST_TYPE == 1
91 QLIST_ENTRY(list_element
) entry
;
93 #error Invalid TEST_LIST_TYPE
98 static void reclaim_list_el(struct rcu_head
*prcu
)
100 struct list_element
*el
= container_of(prcu
, struct list_element
, rcu
);
102 /* Accessed only from call_rcu thread. */
106 #if TEST_LIST_TYPE == 1
107 static QLIST_HEAD(q_list_head
, list_element
) Q_list_head
;
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
116 #error Invalid TEST_LIST_TYPE
119 static void *rcu_q_reader(void *arg
)
121 long long n_reads_local
= 0;
122 struct list_element
*el
;
124 rcu_register_thread();
126 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
127 atomic_inc(&nthreadsrunning
);
128 while (atomic_read(&goflag
) == GOFLAG_INIT
) {
132 while (atomic_read(&goflag
) == GOFLAG_RUN
) {
134 TEST_LIST_FOREACH_RCU(el
, &Q_list_head
, entry
) {
136 if (atomic_read(&goflag
) == GOFLAG_STOP
) {
144 qemu_mutex_lock(&counts_mutex
);
145 n_reads
+= n_reads_local
;
146 qemu_mutex_unlock(&counts_mutex
);
148 rcu_unregister_thread();
153 static void *rcu_q_updater(void *arg
)
156 long long n_nodes_local
= 0;
157 long long n_updates_local
= 0;
158 long long n_removed_local
= 0;
159 struct list_element
*el
, *prev_el
;
161 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
162 atomic_inc(&nthreadsrunning
);
163 while (atomic_read(&goflag
) == GOFLAG_INIT
) {
167 while (atomic_read(&goflag
) == GOFLAG_RUN
) {
168 target_el
= select_random_el(RCU_Q_LEN
);
170 /* FOREACH_RCU could work here but let's use both macros */
171 TEST_LIST_FOREACH_SAFE_RCU(prev_el
, &Q_list_head
, entry
, el
) {
173 if (target_el
== j
) {
174 TEST_LIST_REMOVE_RCU(prev_el
, entry
);
175 /* may be more than one updater in the future */
176 call_rcu1(&prev_el
->rcu
, reclaim_list_el
);
181 if (atomic_read(&goflag
) == GOFLAG_STOP
) {
184 target_el
= select_random_el(RCU_Q_LEN
);
186 TEST_LIST_FOREACH_RCU(el
, &Q_list_head
, entry
) {
188 if (target_el
== j
) {
189 struct list_element
*new_el
= g_new(struct list_element
, 1);
190 n_nodes
+= n_nodes_local
;
191 TEST_LIST_INSERT_AFTER_RCU(el
, new_el
, entry
);
196 n_updates_local
+= 2;
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
);
208 static void rcu_qtest_init(void)
210 struct list_element
*new_el
;
214 for (i
= 0; i
< RCU_Q_LEN
; i
++) {
215 new_el
= g_new(struct list_element
, 1);
216 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head
, new_el
, entry
);
218 qemu_mutex_lock(&counts_mutex
);
219 n_nodes
+= RCU_Q_LEN
;
220 qemu_mutex_unlock(&counts_mutex
);
223 static void rcu_qtest_run(int duration
, int nreaders
)
225 int nthreads
= nreaders
+ 1;
226 while (atomic_read(&nthreadsrunning
) < nthreads
) {
230 atomic_set(&goflag
, GOFLAG_RUN
);
232 atomic_set(&goflag
, GOFLAG_STOP
);
237 static void rcu_qtest(const char *test
, int duration
, int nreaders
)
240 long long n_removed_local
= 0;
242 struct list_element
*el
, *prev_el
;
245 for (i
= 0; i
< nreaders
; i
++) {
246 create_thread(rcu_q_reader
);
248 create_thread(rcu_q_updater
);
249 rcu_qtest_run(duration
, nreaders
);
251 TEST_LIST_FOREACH_SAFE_RCU(prev_el
, &Q_list_head
, entry
, el
) {
252 TEST_LIST_REMOVE_RCU(prev_el
, entry
);
253 call_rcu1(&prev_el
->rcu
, reclaim_list_el
);
256 qemu_mutex_lock(&counts_mutex
);
257 n_nodes_removed
+= n_removed_local
;
258 qemu_mutex_unlock(&counts_mutex
);
260 while (n_nodes_removed
> n_reclaims
) {
264 if (g_test_in_charge
) {
265 g_assert_cmpint(n_nodes_removed
, ==, n_reclaims
);
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
);
274 static void usage(int argc
, char *argv
[])
276 fprintf(stderr
, "Usage: %s duration nreaders\n", argv
[0]);
280 static int gtest_seconds
;
282 static void gtest_rcuq_one(void)
284 rcu_qtest("rcuqtest", gtest_seconds
/ 4, 1);
287 static void gtest_rcuq_few(void)
289 rcu_qtest("rcuqtest", gtest_seconds
/ 4, 5);
292 static void gtest_rcuq_many(void)
294 rcu_qtest("rcuqtest", gtest_seconds
/ 2, 20);
298 int main(int argc
, char *argv
[])
300 int duration
= 0, readers
= 0;
302 qemu_mutex_init(&counts_mutex
);
304 if (argv
[1][0] == '-') {
305 g_test_init(&argc
, &argv
, NULL
);
306 if (g_test_quick()) {
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
);
314 g_test_in_charge
= 1;
317 duration
= strtoul(argv
[1], NULL
, 0);
320 readers
= strtoul(argv
[2], NULL
, 0);
322 if (duration
&& readers
) {
323 rcu_qtest(argv
[0], duration
, readers
);