]>
Commit | Line | Data |
---|---|---|
341774fe MD |
1 | /* |
2 | * rcuq_test.c | |
3 | * | |
4 | * usage: rcuq_test <readers> <duration> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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. | |
19 | * | |
20 | * Copyright (c) 2013 Mike D. Day, IBM Corporation. | |
21 | */ | |
22 | ||
681c28a3 | 23 | #include "qemu/osdep.h" |
341774fe MD |
24 | #include "qemu/atomic.h" |
25 | #include "qemu/rcu.h" | |
341774fe MD |
26 | #include "qemu/thread.h" |
27 | #include "qemu/rcu_queue.h" | |
28 | ||
29 | /* | |
30 | * Test variables. | |
31 | */ | |
32 | ||
8a5956ad PB |
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; | |
341774fe | 40 | |
8a5956ad | 41 | static int nthreadsrunning; |
341774fe MD |
42 | |
43 | #define GOFLAG_INIT 0 | |
44 | #define GOFLAG_RUN 1 | |
45 | #define GOFLAG_STOP 2 | |
46 | ||
23311b81 | 47 | static int goflag = GOFLAG_INIT; |
341774fe MD |
48 | |
49 | #define RCU_READ_RUN 1000 | |
50 | #define RCU_UPDATE_RUN 10 | |
51 | #define NR_THREADS 100 | |
52 | #define RCU_Q_LEN 100 | |
53 | ||
54 | static QemuThread threads[NR_THREADS]; | |
55 | static struct rcu_reader_data *data[NR_THREADS]; | |
56 | static int n_threads; | |
57 | ||
58 | static int select_random_el(int max) | |
59 | { | |
60 | return (rand() % max); | |
61 | } | |
62 | ||
63 | ||
64 | static void create_thread(void *(*func)(void *)) | |
65 | { | |
66 | if (n_threads >= NR_THREADS) { | |
67 | fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS); | |
68 | exit(-1); | |
69 | } | |
70 | qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads], | |
71 | QEMU_THREAD_JOINABLE); | |
72 | n_threads++; | |
73 | } | |
74 | ||
75 | static void wait_all_threads(void) | |
76 | { | |
77 | int i; | |
78 | ||
79 | for (i = 0; i < n_threads; i++) { | |
80 | qemu_thread_join(&threads[i]); | |
81 | } | |
82 | n_threads = 0; | |
83 | } | |
84 | ||
685cc7c0 EC |
85 | #ifndef TEST_LIST_TYPE |
86 | #define TEST_LIST_TYPE 1 | |
87 | #endif | |
341774fe MD |
88 | |
89 | struct list_element { | |
685cc7c0 | 90 | #if TEST_LIST_TYPE == 1 |
341774fe | 91 | QLIST_ENTRY(list_element) entry; |
90487e45 EC |
92 | #elif TEST_LIST_TYPE == 2 |
93 | QSIMPLEQ_ENTRY(list_element) entry; | |
685cc7c0 EC |
94 | #else |
95 | #error Invalid TEST_LIST_TYPE | |
96 | #endif | |
341774fe | 97 | struct rcu_head rcu; |
341774fe MD |
98 | }; |
99 | ||
100 | static void reclaim_list_el(struct rcu_head *prcu) | |
101 | { | |
102 | struct list_element *el = container_of(prcu, struct list_element, rcu); | |
103 | g_free(el); | |
8a5956ad PB |
104 | /* Accessed only from call_rcu thread. */ |
105 | n_reclaims++; | |
341774fe MD |
106 | } |
107 | ||
685cc7c0 | 108 | #if TEST_LIST_TYPE == 1 |
341774fe MD |
109 | static QLIST_HEAD(q_list_head, list_element) Q_list_head; |
110 | ||
685cc7c0 EC |
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 | |
90487e45 EC |
117 | |
118 | #elif TEST_LIST_TYPE == 2 | |
119 | static QSIMPLEQ_HEAD(, list_element) Q_list_head = | |
120 | QSIMPLEQ_HEAD_INITIALIZER(Q_list_head); | |
121 | ||
122 | #define TEST_NAME "qsimpleq" | |
123 | #define TEST_LIST_REMOVE_RCU(el, f) \ | |
124 | QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f) | |
125 | ||
126 | #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \ | |
127 | QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f) | |
128 | ||
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 | |
685cc7c0 EC |
132 | #else |
133 | #error Invalid TEST_LIST_TYPE | |
134 | #endif | |
135 | ||
341774fe MD |
136 | static void *rcu_q_reader(void *arg) |
137 | { | |
8a5956ad | 138 | long long n_reads_local = 0; |
341774fe MD |
139 | struct list_element *el; |
140 | ||
ab28bd23 PB |
141 | rcu_register_thread(); |
142 | ||
341774fe MD |
143 | *(struct rcu_reader_data **)arg = &rcu_reader; |
144 | atomic_inc(&nthreadsrunning); | |
23311b81 | 145 | while (atomic_read(&goflag) == GOFLAG_INIT) { |
341774fe MD |
146 | g_usleep(1000); |
147 | } | |
148 | ||
23311b81 | 149 | while (atomic_read(&goflag) == GOFLAG_RUN) { |
341774fe | 150 | rcu_read_lock(); |
685cc7c0 | 151 | TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) { |
341774fe | 152 | n_reads_local++; |
23311b81 | 153 | if (atomic_read(&goflag) == GOFLAG_STOP) { |
341774fe MD |
154 | break; |
155 | } | |
156 | } | |
157 | rcu_read_unlock(); | |
158 | ||
159 | g_usleep(100); | |
160 | } | |
8a5956ad PB |
161 | qemu_mutex_lock(&counts_mutex); |
162 | n_reads += n_reads_local; | |
163 | qemu_mutex_unlock(&counts_mutex); | |
ab28bd23 PB |
164 | |
165 | rcu_unregister_thread(); | |
341774fe MD |
166 | return NULL; |
167 | } | |
168 | ||
169 | ||
170 | static void *rcu_q_updater(void *arg) | |
171 | { | |
172 | int j, target_el; | |
8a5956ad | 173 | long long n_nodes_local = 0; |
341774fe MD |
174 | long long n_updates_local = 0; |
175 | long long n_removed_local = 0; | |
176 | struct list_element *el, *prev_el; | |
177 | ||
178 | *(struct rcu_reader_data **)arg = &rcu_reader; | |
179 | atomic_inc(&nthreadsrunning); | |
23311b81 | 180 | while (atomic_read(&goflag) == GOFLAG_INIT) { |
341774fe MD |
181 | g_usleep(1000); |
182 | } | |
183 | ||
23311b81 | 184 | while (atomic_read(&goflag) == GOFLAG_RUN) { |
341774fe MD |
185 | target_el = select_random_el(RCU_Q_LEN); |
186 | j = 0; | |
187 | /* FOREACH_RCU could work here but let's use both macros */ | |
685cc7c0 | 188 | TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) { |
341774fe MD |
189 | j++; |
190 | if (target_el == j) { | |
685cc7c0 | 191 | TEST_LIST_REMOVE_RCU(prev_el, entry); |
341774fe MD |
192 | /* may be more than one updater in the future */ |
193 | call_rcu1(&prev_el->rcu, reclaim_list_el); | |
194 | n_removed_local++; | |
195 | break; | |
196 | } | |
197 | } | |
23311b81 | 198 | if (atomic_read(&goflag) == GOFLAG_STOP) { |
341774fe MD |
199 | break; |
200 | } | |
201 | target_el = select_random_el(RCU_Q_LEN); | |
202 | j = 0; | |
685cc7c0 | 203 | TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) { |
341774fe MD |
204 | j++; |
205 | if (target_el == j) { | |
685cc7c0 | 206 | struct list_element *new_el = g_new(struct list_element, 1); |
8a5956ad | 207 | n_nodes += n_nodes_local; |
685cc7c0 | 208 | TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry); |
341774fe MD |
209 | break; |
210 | } | |
211 | } | |
212 | ||
213 | n_updates_local += 2; | |
214 | synchronize_rcu(); | |
215 | } | |
216 | synchronize_rcu(); | |
8a5956ad PB |
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); | |
341774fe MD |
222 | return NULL; |
223 | } | |
224 | ||
225 | static void rcu_qtest_init(void) | |
226 | { | |
227 | struct list_element *new_el; | |
228 | int i; | |
229 | nthreadsrunning = 0; | |
230 | srand(time(0)); | |
231 | for (i = 0; i < RCU_Q_LEN; i++) { | |
232 | new_el = g_new(struct list_element, 1); | |
685cc7c0 | 233 | TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry); |
341774fe | 234 | } |
8a5956ad PB |
235 | qemu_mutex_lock(&counts_mutex); |
236 | n_nodes += RCU_Q_LEN; | |
237 | qemu_mutex_unlock(&counts_mutex); | |
341774fe MD |
238 | } |
239 | ||
240 | static void rcu_qtest_run(int duration, int nreaders) | |
241 | { | |
242 | int nthreads = nreaders + 1; | |
243 | while (atomic_read(&nthreadsrunning) < nthreads) { | |
244 | g_usleep(1000); | |
245 | } | |
246 | ||
23311b81 | 247 | atomic_set(&goflag, GOFLAG_RUN); |
341774fe | 248 | sleep(duration); |
23311b81 | 249 | atomic_set(&goflag, GOFLAG_STOP); |
341774fe MD |
250 | wait_all_threads(); |
251 | } | |
252 | ||
253 | ||
254 | static void rcu_qtest(const char *test, int duration, int nreaders) | |
255 | { | |
256 | int i; | |
257 | long long n_removed_local = 0; | |
258 | ||
259 | struct list_element *el, *prev_el; | |
260 | ||
261 | rcu_qtest_init(); | |
262 | for (i = 0; i < nreaders; i++) { | |
263 | create_thread(rcu_q_reader); | |
264 | } | |
265 | create_thread(rcu_q_updater); | |
266 | rcu_qtest_run(duration, nreaders); | |
267 | ||
685cc7c0 EC |
268 | TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) { |
269 | TEST_LIST_REMOVE_RCU(prev_el, entry); | |
341774fe MD |
270 | call_rcu1(&prev_el->rcu, reclaim_list_el); |
271 | n_removed_local++; | |
272 | } | |
8a5956ad PB |
273 | qemu_mutex_lock(&counts_mutex); |
274 | n_nodes_removed += n_removed_local; | |
275 | qemu_mutex_unlock(&counts_mutex); | |
341774fe MD |
276 | synchronize_rcu(); |
277 | while (n_nodes_removed > n_reclaims) { | |
278 | g_usleep(100); | |
279 | synchronize_rcu(); | |
280 | } | |
281 | if (g_test_in_charge) { | |
282 | g_assert_cmpint(n_nodes_removed, ==, n_reclaims); | |
283 | } else { | |
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); | |
287 | exit(0); | |
288 | } | |
289 | } | |
290 | ||
291 | static void usage(int argc, char *argv[]) | |
292 | { | |
293 | fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]); | |
294 | exit(-1); | |
295 | } | |
296 | ||
297 | static int gtest_seconds; | |
298 | ||
299 | static void gtest_rcuq_one(void) | |
300 | { | |
301 | rcu_qtest("rcuqtest", gtest_seconds / 4, 1); | |
302 | } | |
303 | ||
304 | static void gtest_rcuq_few(void) | |
305 | { | |
306 | rcu_qtest("rcuqtest", gtest_seconds / 4, 5); | |
307 | } | |
308 | ||
309 | static void gtest_rcuq_many(void) | |
310 | { | |
311 | rcu_qtest("rcuqtest", gtest_seconds / 2, 20); | |
312 | } | |
313 | ||
314 | ||
315 | int main(int argc, char *argv[]) | |
316 | { | |
317 | int duration = 0, readers = 0; | |
318 | ||
8a5956ad | 319 | qemu_mutex_init(&counts_mutex); |
341774fe MD |
320 | if (argc >= 2) { |
321 | if (argv[1][0] == '-') { | |
322 | g_test_init(&argc, &argv, NULL); | |
323 | if (g_test_quick()) { | |
324 | gtest_seconds = 4; | |
325 | } else { | |
326 | gtest_seconds = 20; | |
327 | } | |
685cc7c0 EC |
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); | |
341774fe MD |
331 | g_test_in_charge = 1; |
332 | return g_test_run(); | |
333 | } | |
334 | duration = strtoul(argv[1], NULL, 0); | |
335 | } | |
336 | if (argc >= 3) { | |
337 | readers = strtoul(argv[2], NULL, 0); | |
338 | } | |
339 | if (duration && readers) { | |
340 | rcu_qtest(argv[0], duration, readers); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | usage(argc, argv); | |
345 | return -1; | |
346 | } |