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 int64_t n_reclaims
;
37 static int64_t n_nodes_removed
;
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
;
92 #elif TEST_LIST_TYPE == 2
93 QSIMPLEQ_ENTRY(list_element
) entry
;
94 #elif TEST_LIST_TYPE == 3
95 QTAILQ_ENTRY(list_element
) entry
;
96 #elif TEST_LIST_TYPE == 4
97 QSLIST_ENTRY(list_element
) entry
;
99 #error Invalid TEST_LIST_TYPE
104 static void reclaim_list_el(struct rcu_head
*prcu
)
106 struct list_element
*el
= container_of(prcu
, struct list_element
, rcu
);
108 /* Accessed only from call_rcu thread. */
109 atomic_set_i64(&n_reclaims
, n_reclaims
+ 1);
112 #if TEST_LIST_TYPE == 1
113 static QLIST_HEAD(, list_element
) Q_list_head
;
115 #define TEST_NAME "qlist"
116 #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
117 #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
118 #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
119 #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
120 #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
122 #elif TEST_LIST_TYPE == 2
123 static QSIMPLEQ_HEAD(, list_element
) Q_list_head
=
124 QSIMPLEQ_HEAD_INITIALIZER(Q_list_head
);
126 #define TEST_NAME "qsimpleq"
127 #define TEST_LIST_REMOVE_RCU(el, f) \
128 QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f)
130 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
131 QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
133 #define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU
134 #define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU
135 #define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU
137 #elif TEST_LIST_TYPE == 3
138 static QTAILQ_HEAD(, list_element
) Q_list_head
;
140 #define TEST_NAME "qtailq"
141 #define TEST_LIST_REMOVE_RCU(el, f) QTAILQ_REMOVE_RCU(&Q_list_head, el, f)
143 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
144 QTAILQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
146 #define TEST_LIST_INSERT_HEAD_RCU QTAILQ_INSERT_HEAD_RCU
147 #define TEST_LIST_FOREACH_RCU QTAILQ_FOREACH_RCU
148 #define TEST_LIST_FOREACH_SAFE_RCU QTAILQ_FOREACH_SAFE_RCU
150 #elif TEST_LIST_TYPE == 4
151 static QSLIST_HEAD(, list_element
) Q_list_head
;
153 #define TEST_NAME "qslist"
154 #define TEST_LIST_REMOVE_RCU(el, f) \
155 QSLIST_REMOVE_RCU(&Q_list_head, el, list_element, f)
157 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
158 QSLIST_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
160 #define TEST_LIST_INSERT_HEAD_RCU QSLIST_INSERT_HEAD_RCU
161 #define TEST_LIST_FOREACH_RCU QSLIST_FOREACH_RCU
162 #define TEST_LIST_FOREACH_SAFE_RCU QSLIST_FOREACH_SAFE_RCU
164 #error Invalid TEST_LIST_TYPE
167 static void *rcu_q_reader(void *arg
)
169 long long n_reads_local
= 0;
170 struct list_element
*el
;
172 rcu_register_thread();
174 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
175 atomic_inc(&nthreadsrunning
);
176 while (atomic_read(&goflag
) == GOFLAG_INIT
) {
180 while (atomic_read(&goflag
) == GOFLAG_RUN
) {
182 TEST_LIST_FOREACH_RCU(el
, &Q_list_head
, entry
) {
184 if (atomic_read(&goflag
) == GOFLAG_STOP
) {
192 qemu_mutex_lock(&counts_mutex
);
193 n_reads
+= n_reads_local
;
194 qemu_mutex_unlock(&counts_mutex
);
196 rcu_unregister_thread();
201 static void *rcu_q_updater(void *arg
)
204 long long n_nodes_local
= 0;
205 long long n_updates_local
= 0;
206 long long n_removed_local
= 0;
207 struct list_element
*el
, *prev_el
;
209 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
210 atomic_inc(&nthreadsrunning
);
211 while (atomic_read(&goflag
) == GOFLAG_INIT
) {
215 while (atomic_read(&goflag
) == GOFLAG_RUN
) {
216 target_el
= select_random_el(RCU_Q_LEN
);
218 /* FOREACH_RCU could work here but let's use both macros */
219 TEST_LIST_FOREACH_SAFE_RCU(prev_el
, &Q_list_head
, entry
, el
) {
221 if (target_el
== j
) {
222 TEST_LIST_REMOVE_RCU(prev_el
, entry
);
223 /* may be more than one updater in the future */
224 call_rcu1(&prev_el
->rcu
, reclaim_list_el
);
229 if (atomic_read(&goflag
) == GOFLAG_STOP
) {
232 target_el
= select_random_el(RCU_Q_LEN
);
234 TEST_LIST_FOREACH_RCU(el
, &Q_list_head
, entry
) {
236 if (target_el
== j
) {
237 struct list_element
*new_el
= g_new(struct list_element
, 1);
239 TEST_LIST_INSERT_AFTER_RCU(el
, new_el
, entry
);
244 n_updates_local
+= 2;
248 qemu_mutex_lock(&counts_mutex
);
249 n_nodes
+= n_nodes_local
;
250 n_updates
+= n_updates_local
;
251 atomic_set_i64(&n_nodes_removed
, n_nodes_removed
+ n_removed_local
);
252 qemu_mutex_unlock(&counts_mutex
);
256 static void rcu_qtest_init(void)
258 struct list_element
*new_el
;
262 for (i
= 0; i
< RCU_Q_LEN
; i
++) {
263 new_el
= g_new(struct list_element
, 1);
264 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head
, new_el
, entry
);
266 qemu_mutex_lock(&counts_mutex
);
267 n_nodes
+= RCU_Q_LEN
;
268 qemu_mutex_unlock(&counts_mutex
);
271 static void rcu_qtest_run(int duration
, int nreaders
)
273 int nthreads
= nreaders
+ 1;
274 while (atomic_read(&nthreadsrunning
) < nthreads
) {
278 atomic_set(&goflag
, GOFLAG_RUN
);
280 atomic_set(&goflag
, GOFLAG_STOP
);
285 static void rcu_qtest(const char *test
, int duration
, int nreaders
)
288 long long n_removed_local
= 0;
290 struct list_element
*el
, *prev_el
;
293 for (i
= 0; i
< nreaders
; i
++) {
294 create_thread(rcu_q_reader
);
296 create_thread(rcu_q_updater
);
297 rcu_qtest_run(duration
, nreaders
);
299 TEST_LIST_FOREACH_SAFE_RCU(prev_el
, &Q_list_head
, entry
, el
) {
300 TEST_LIST_REMOVE_RCU(prev_el
, entry
);
301 call_rcu1(&prev_el
->rcu
, reclaim_list_el
);
304 qemu_mutex_lock(&counts_mutex
);
305 atomic_set_i64(&n_nodes_removed
, n_nodes_removed
+ n_removed_local
);
306 qemu_mutex_unlock(&counts_mutex
);
308 while (atomic_read_i64(&n_nodes_removed
) > atomic_read_i64(&n_reclaims
)) {
312 if (g_test_in_charge
) {
313 g_assert_cmpint(atomic_read_i64(&n_nodes_removed
), ==,
314 atomic_read_i64(&n_reclaims
));
316 printf("%s: %d readers; 1 updater; nodes read: " \
317 "%lld, nodes removed: %"PRIi64
"; nodes reclaimed: %"PRIi64
"\n",
318 test
, nthreadsrunning
- 1, n_reads
,
319 atomic_read_i64(&n_nodes_removed
), atomic_read_i64(&n_reclaims
));
324 static void usage(int argc
, char *argv
[])
326 fprintf(stderr
, "Usage: %s duration nreaders\n", argv
[0]);
330 static int gtest_seconds
;
332 static void gtest_rcuq_one(void)
334 rcu_qtest("rcuqtest", gtest_seconds
/ 4, 1);
337 static void gtest_rcuq_few(void)
339 rcu_qtest("rcuqtest", gtest_seconds
/ 4, 5);
342 static void gtest_rcuq_many(void)
344 rcu_qtest("rcuqtest", gtest_seconds
/ 2, 20);
348 int main(int argc
, char *argv
[])
350 int duration
= 0, readers
= 0;
352 qemu_mutex_init(&counts_mutex
);
354 if (argv
[1][0] == '-') {
355 g_test_init(&argc
, &argv
, NULL
);
356 if (g_test_quick()) {
361 g_test_add_func("/rcu/"TEST_NAME
"/single-threaded", gtest_rcuq_one
);
362 g_test_add_func("/rcu/"TEST_NAME
"/short-few", gtest_rcuq_few
);
363 g_test_add_func("/rcu/"TEST_NAME
"/long-many", gtest_rcuq_many
);
364 g_test_in_charge
= 1;
367 duration
= strtoul(argv
[1], NULL
, 0);
370 readers
= strtoul(argv
[2], NULL
, 0);
372 if (duration
&& readers
) {
373 rcu_qtest(argv
[0], duration
, readers
);