]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/test-rcu-list.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 "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
;
92 #elif TEST_LIST_TYPE == 2
93 QSIMPLEQ_ENTRY(list_element
) entry
;
95 #error Invalid TEST_LIST_TYPE
100 static void reclaim_list_el(struct rcu_head
*prcu
)
102 struct list_element
*el
= container_of(prcu
, struct list_element
, rcu
);
104 /* Accessed only from call_rcu thread. */
108 #if TEST_LIST_TYPE == 1
109 static QLIST_HEAD(q_list_head
, list_element
) Q_list_head
;
111 #define TEST_NAME "qlist"
112 #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
113 #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
114 #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
115 #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
116 #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
118 #elif TEST_LIST_TYPE == 2
119 static QSIMPLEQ_HEAD(, list_element
) Q_list_head
=
120 QSIMPLEQ_HEAD_INITIALIZER(Q_list_head
);
122 #define TEST_NAME "qsimpleq"
123 #define TEST_LIST_REMOVE_RCU(el, f) \
124 QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f)
126 #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
127 QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
129 #define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU
130 #define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU
131 #define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU
133 #error Invalid TEST_LIST_TYPE
136 static void *rcu_q_reader(void *arg
)
138 long long n_reads_local
= 0;
139 struct list_element
*el
;
141 rcu_register_thread();
143 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
144 atomic_inc(&nthreadsrunning
);
145 while (atomic_read(&goflag
) == GOFLAG_INIT
) {
149 while (atomic_read(&goflag
) == GOFLAG_RUN
) {
151 TEST_LIST_FOREACH_RCU(el
, &Q_list_head
, entry
) {
153 if (atomic_read(&goflag
) == GOFLAG_STOP
) {
161 qemu_mutex_lock(&counts_mutex
);
162 n_reads
+= n_reads_local
;
163 qemu_mutex_unlock(&counts_mutex
);
165 rcu_unregister_thread();
170 static void *rcu_q_updater(void *arg
)
173 long long n_nodes_local
= 0;
174 long long n_updates_local
= 0;
175 long long n_removed_local
= 0;
176 struct list_element
*el
, *prev_el
;
178 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
179 atomic_inc(&nthreadsrunning
);
180 while (atomic_read(&goflag
) == GOFLAG_INIT
) {
184 while (atomic_read(&goflag
) == GOFLAG_RUN
) {
185 target_el
= select_random_el(RCU_Q_LEN
);
187 /* FOREACH_RCU could work here but let's use both macros */
188 TEST_LIST_FOREACH_SAFE_RCU(prev_el
, &Q_list_head
, entry
, el
) {
190 if (target_el
== j
) {
191 TEST_LIST_REMOVE_RCU(prev_el
, entry
);
192 /* may be more than one updater in the future */
193 call_rcu1(&prev_el
->rcu
, reclaim_list_el
);
198 if (atomic_read(&goflag
) == GOFLAG_STOP
) {
201 target_el
= select_random_el(RCU_Q_LEN
);
203 TEST_LIST_FOREACH_RCU(el
, &Q_list_head
, entry
) {
205 if (target_el
== j
) {
206 struct list_element
*new_el
= g_new(struct list_element
, 1);
207 n_nodes
+= n_nodes_local
;
208 TEST_LIST_INSERT_AFTER_RCU(el
, new_el
, entry
);
213 n_updates_local
+= 2;
217 qemu_mutex_lock(&counts_mutex
);
218 n_nodes
+= n_nodes_local
;
219 n_updates
+= n_updates_local
;
220 n_nodes_removed
+= n_removed_local
;
221 qemu_mutex_unlock(&counts_mutex
);
225 static void rcu_qtest_init(void)
227 struct list_element
*new_el
;
231 for (i
= 0; i
< RCU_Q_LEN
; i
++) {
232 new_el
= g_new(struct list_element
, 1);
233 TEST_LIST_INSERT_HEAD_RCU(&Q_list_head
, new_el
, entry
);
235 qemu_mutex_lock(&counts_mutex
);
236 n_nodes
+= RCU_Q_LEN
;
237 qemu_mutex_unlock(&counts_mutex
);
240 static void rcu_qtest_run(int duration
, int nreaders
)
242 int nthreads
= nreaders
+ 1;
243 while (atomic_read(&nthreadsrunning
) < nthreads
) {
247 atomic_set(&goflag
, GOFLAG_RUN
);
249 atomic_set(&goflag
, GOFLAG_STOP
);
254 static void rcu_qtest(const char *test
, int duration
, int nreaders
)
257 long long n_removed_local
= 0;
259 struct list_element
*el
, *prev_el
;
262 for (i
= 0; i
< nreaders
; i
++) {
263 create_thread(rcu_q_reader
);
265 create_thread(rcu_q_updater
);
266 rcu_qtest_run(duration
, nreaders
);
268 TEST_LIST_FOREACH_SAFE_RCU(prev_el
, &Q_list_head
, entry
, el
) {
269 TEST_LIST_REMOVE_RCU(prev_el
, entry
);
270 call_rcu1(&prev_el
->rcu
, reclaim_list_el
);
273 qemu_mutex_lock(&counts_mutex
);
274 n_nodes_removed
+= n_removed_local
;
275 qemu_mutex_unlock(&counts_mutex
);
277 while (n_nodes_removed
> n_reclaims
) {
281 if (g_test_in_charge
) {
282 g_assert_cmpint(n_nodes_removed
, ==, n_reclaims
);
284 printf("%s: %d readers; 1 updater; nodes read: " \
285 "%lld, nodes removed: %lld; nodes reclaimed: %lld\n",
286 test
, nthreadsrunning
- 1, n_reads
, n_nodes_removed
, n_reclaims
);
291 static void usage(int argc
, char *argv
[])
293 fprintf(stderr
, "Usage: %s duration nreaders\n", argv
[0]);
297 static int gtest_seconds
;
299 static void gtest_rcuq_one(void)
301 rcu_qtest("rcuqtest", gtest_seconds
/ 4, 1);
304 static void gtest_rcuq_few(void)
306 rcu_qtest("rcuqtest", gtest_seconds
/ 4, 5);
309 static void gtest_rcuq_many(void)
311 rcu_qtest("rcuqtest", gtest_seconds
/ 2, 20);
315 int main(int argc
, char *argv
[])
317 int duration
= 0, readers
= 0;
319 qemu_mutex_init(&counts_mutex
);
321 if (argv
[1][0] == '-') {
322 g_test_init(&argc
, &argv
, NULL
);
323 if (g_test_quick()) {
328 g_test_add_func("/rcu/"TEST_NAME
"/single-threaded", gtest_rcuq_one
);
329 g_test_add_func("/rcu/"TEST_NAME
"/short-few", gtest_rcuq_few
);
330 g_test_add_func("/rcu/"TEST_NAME
"/long-many", gtest_rcuq_many
);
331 g_test_in_charge
= 1;
334 duration
= strtoul(argv
[1], NULL
, 0);
337 readers
= strtoul(argv
[2], NULL
, 0);
339 if (duration
&& readers
) {
340 rcu_qtest(argv
[0], duration
, readers
);