Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 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 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 23 | #include "qemu/osdep.h" |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 24 | #include "qemu/atomic.h" |
| 25 | #include "qemu/rcu.h" |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 26 | #include "qemu/thread.h" |
| 27 | #include "qemu/rcu_queue.h" |
| 28 | |
| 29 | /* |
| 30 | * Test variables. |
| 31 | */ |
| 32 | |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 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; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 40 | |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 41 | static int nthreadsrunning; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 42 | |
| 43 | #define GOFLAG_INIT 0 |
| 44 | #define GOFLAG_RUN 1 |
| 45 | #define GOFLAG_STOP 2 |
| 46 | |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 47 | static int goflag = GOFLAG_INIT; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 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 | |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 85 | #ifndef TEST_LIST_TYPE |
| 86 | #define TEST_LIST_TYPE 1 |
| 87 | #endif |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 88 | |
| 89 | struct list_element { |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 90 | #if TEST_LIST_TYPE == 1 |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 91 | QLIST_ENTRY(list_element) entry; |
Emilio G. Cota | 90487e4 | 2018-08-19 05:13:32 -0400 | [diff] [blame] | 92 | #elif TEST_LIST_TYPE == 2 |
| 93 | QSIMPLEQ_ENTRY(list_element) entry; |
Emilio G. Cota | dbf8862 | 2018-08-19 05:13:33 -0400 | [diff] [blame] | 94 | #elif TEST_LIST_TYPE == 3 |
| 95 | QTAILQ_ENTRY(list_element) entry; |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 96 | #else |
| 97 | #error Invalid TEST_LIST_TYPE |
| 98 | #endif |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 99 | struct rcu_head rcu; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static void reclaim_list_el(struct rcu_head *prcu) |
| 103 | { |
| 104 | struct list_element *el = container_of(prcu, struct list_element, rcu); |
| 105 | g_free(el); |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 106 | /* Accessed only from call_rcu thread. */ |
| 107 | n_reclaims++; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 110 | #if TEST_LIST_TYPE == 1 |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 111 | static QLIST_HEAD(q_list_head, list_element) Q_list_head; |
| 112 | |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 113 | #define TEST_NAME "qlist" |
| 114 | #define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU |
| 115 | #define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU |
| 116 | #define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU |
| 117 | #define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU |
| 118 | #define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU |
Emilio G. Cota | 90487e4 | 2018-08-19 05:13:32 -0400 | [diff] [blame] | 119 | |
| 120 | #elif TEST_LIST_TYPE == 2 |
| 121 | static QSIMPLEQ_HEAD(, list_element) Q_list_head = |
| 122 | QSIMPLEQ_HEAD_INITIALIZER(Q_list_head); |
| 123 | |
| 124 | #define TEST_NAME "qsimpleq" |
| 125 | #define TEST_LIST_REMOVE_RCU(el, f) \ |
| 126 | QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f) |
| 127 | |
| 128 | #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \ |
| 129 | QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f) |
| 130 | |
| 131 | #define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU |
| 132 | #define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU |
| 133 | #define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU |
Emilio G. Cota | dbf8862 | 2018-08-19 05:13:33 -0400 | [diff] [blame] | 134 | |
| 135 | #elif TEST_LIST_TYPE == 3 |
| 136 | static QTAILQ_HEAD(, list_element) Q_list_head; |
| 137 | |
| 138 | #define TEST_NAME "qtailq" |
| 139 | #define TEST_LIST_REMOVE_RCU(el, f) QTAILQ_REMOVE_RCU(&Q_list_head, el, f) |
| 140 | |
| 141 | #define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \ |
| 142 | QTAILQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f) |
| 143 | |
| 144 | #define TEST_LIST_INSERT_HEAD_RCU QTAILQ_INSERT_HEAD_RCU |
| 145 | #define TEST_LIST_FOREACH_RCU QTAILQ_FOREACH_RCU |
| 146 | #define TEST_LIST_FOREACH_SAFE_RCU QTAILQ_FOREACH_SAFE_RCU |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 147 | #else |
| 148 | #error Invalid TEST_LIST_TYPE |
| 149 | #endif |
| 150 | |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 151 | static void *rcu_q_reader(void *arg) |
| 152 | { |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 153 | long long n_reads_local = 0; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 154 | struct list_element *el; |
| 155 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 156 | rcu_register_thread(); |
| 157 | |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 158 | *(struct rcu_reader_data **)arg = &rcu_reader; |
| 159 | atomic_inc(&nthreadsrunning); |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 160 | while (atomic_read(&goflag) == GOFLAG_INIT) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 161 | g_usleep(1000); |
| 162 | } |
| 163 | |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 164 | while (atomic_read(&goflag) == GOFLAG_RUN) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 165 | rcu_read_lock(); |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 166 | TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 167 | n_reads_local++; |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 168 | if (atomic_read(&goflag) == GOFLAG_STOP) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | } |
| 172 | rcu_read_unlock(); |
| 173 | |
| 174 | g_usleep(100); |
| 175 | } |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 176 | qemu_mutex_lock(&counts_mutex); |
| 177 | n_reads += n_reads_local; |
| 178 | qemu_mutex_unlock(&counts_mutex); |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 179 | |
| 180 | rcu_unregister_thread(); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | static void *rcu_q_updater(void *arg) |
| 186 | { |
| 187 | int j, target_el; |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 188 | long long n_nodes_local = 0; |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 189 | long long n_updates_local = 0; |
| 190 | long long n_removed_local = 0; |
| 191 | struct list_element *el, *prev_el; |
| 192 | |
| 193 | *(struct rcu_reader_data **)arg = &rcu_reader; |
| 194 | atomic_inc(&nthreadsrunning); |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 195 | while (atomic_read(&goflag) == GOFLAG_INIT) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 196 | g_usleep(1000); |
| 197 | } |
| 198 | |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 199 | while (atomic_read(&goflag) == GOFLAG_RUN) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 200 | target_el = select_random_el(RCU_Q_LEN); |
| 201 | j = 0; |
| 202 | /* FOREACH_RCU could work here but let's use both macros */ |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 203 | TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 204 | j++; |
| 205 | if (target_el == j) { |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 206 | TEST_LIST_REMOVE_RCU(prev_el, entry); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 207 | /* may be more than one updater in the future */ |
| 208 | call_rcu1(&prev_el->rcu, reclaim_list_el); |
| 209 | n_removed_local++; |
| 210 | break; |
| 211 | } |
| 212 | } |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 213 | if (atomic_read(&goflag) == GOFLAG_STOP) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | target_el = select_random_el(RCU_Q_LEN); |
| 217 | j = 0; |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 218 | TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) { |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 219 | j++; |
| 220 | if (target_el == j) { |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 221 | struct list_element *new_el = g_new(struct list_element, 1); |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 222 | n_nodes += n_nodes_local; |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 223 | TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | n_updates_local += 2; |
| 229 | synchronize_rcu(); |
| 230 | } |
| 231 | synchronize_rcu(); |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 232 | qemu_mutex_lock(&counts_mutex); |
| 233 | n_nodes += n_nodes_local; |
| 234 | n_updates += n_updates_local; |
| 235 | n_nodes_removed += n_removed_local; |
| 236 | qemu_mutex_unlock(&counts_mutex); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | static void rcu_qtest_init(void) |
| 241 | { |
| 242 | struct list_element *new_el; |
| 243 | int i; |
| 244 | nthreadsrunning = 0; |
| 245 | srand(time(0)); |
| 246 | for (i = 0; i < RCU_Q_LEN; i++) { |
| 247 | new_el = g_new(struct list_element, 1); |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 248 | TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 249 | } |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 250 | qemu_mutex_lock(&counts_mutex); |
| 251 | n_nodes += RCU_Q_LEN; |
| 252 | qemu_mutex_unlock(&counts_mutex); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void rcu_qtest_run(int duration, int nreaders) |
| 256 | { |
| 257 | int nthreads = nreaders + 1; |
| 258 | while (atomic_read(&nthreadsrunning) < nthreads) { |
| 259 | g_usleep(1000); |
| 260 | } |
| 261 | |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 262 | atomic_set(&goflag, GOFLAG_RUN); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 263 | sleep(duration); |
Emilio G. Cota | 23311b8 | 2018-08-19 05:13:29 -0400 | [diff] [blame] | 264 | atomic_set(&goflag, GOFLAG_STOP); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 265 | wait_all_threads(); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static void rcu_qtest(const char *test, int duration, int nreaders) |
| 270 | { |
| 271 | int i; |
| 272 | long long n_removed_local = 0; |
| 273 | |
| 274 | struct list_element *el, *prev_el; |
| 275 | |
| 276 | rcu_qtest_init(); |
| 277 | for (i = 0; i < nreaders; i++) { |
| 278 | create_thread(rcu_q_reader); |
| 279 | } |
| 280 | create_thread(rcu_q_updater); |
| 281 | rcu_qtest_run(duration, nreaders); |
| 282 | |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 283 | TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) { |
| 284 | TEST_LIST_REMOVE_RCU(prev_el, entry); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 285 | call_rcu1(&prev_el->rcu, reclaim_list_el); |
| 286 | n_removed_local++; |
| 287 | } |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 288 | qemu_mutex_lock(&counts_mutex); |
| 289 | n_nodes_removed += n_removed_local; |
| 290 | qemu_mutex_unlock(&counts_mutex); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 291 | synchronize_rcu(); |
| 292 | while (n_nodes_removed > n_reclaims) { |
| 293 | g_usleep(100); |
| 294 | synchronize_rcu(); |
| 295 | } |
| 296 | if (g_test_in_charge) { |
| 297 | g_assert_cmpint(n_nodes_removed, ==, n_reclaims); |
| 298 | } else { |
| 299 | printf("%s: %d readers; 1 updater; nodes read: " \ |
| 300 | "%lld, nodes removed: %lld; nodes reclaimed: %lld\n", |
| 301 | test, nthreadsrunning - 1, n_reads, n_nodes_removed, n_reclaims); |
| 302 | exit(0); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void usage(int argc, char *argv[]) |
| 307 | { |
| 308 | fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]); |
| 309 | exit(-1); |
| 310 | } |
| 311 | |
| 312 | static int gtest_seconds; |
| 313 | |
| 314 | static void gtest_rcuq_one(void) |
| 315 | { |
| 316 | rcu_qtest("rcuqtest", gtest_seconds / 4, 1); |
| 317 | } |
| 318 | |
| 319 | static void gtest_rcuq_few(void) |
| 320 | { |
| 321 | rcu_qtest("rcuqtest", gtest_seconds / 4, 5); |
| 322 | } |
| 323 | |
| 324 | static void gtest_rcuq_many(void) |
| 325 | { |
| 326 | rcu_qtest("rcuqtest", gtest_seconds / 2, 20); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | int main(int argc, char *argv[]) |
| 331 | { |
| 332 | int duration = 0, readers = 0; |
| 333 | |
Paolo Bonzini | 8a5956a | 2015-03-21 16:32:05 +0100 | [diff] [blame] | 334 | qemu_mutex_init(&counts_mutex); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 335 | if (argc >= 2) { |
| 336 | if (argv[1][0] == '-') { |
| 337 | g_test_init(&argc, &argv, NULL); |
| 338 | if (g_test_quick()) { |
| 339 | gtest_seconds = 4; |
| 340 | } else { |
| 341 | gtest_seconds = 20; |
| 342 | } |
Emilio G. Cota | 685cc7c | 2018-08-19 05:13:31 -0400 | [diff] [blame] | 343 | g_test_add_func("/rcu/"TEST_NAME"/single-threaded", gtest_rcuq_one); |
| 344 | g_test_add_func("/rcu/"TEST_NAME"/short-few", gtest_rcuq_few); |
| 345 | g_test_add_func("/rcu/"TEST_NAME"/long-many", gtest_rcuq_many); |
Mike Day | 341774f | 2013-08-27 11:38:45 -0400 | [diff] [blame] | 346 | g_test_in_charge = 1; |
| 347 | return g_test_run(); |
| 348 | } |
| 349 | duration = strtoul(argv[1], NULL, 0); |
| 350 | } |
| 351 | if (argc >= 3) { |
| 352 | readers = strtoul(argv[2], NULL, 0); |
| 353 | } |
| 354 | if (duration && readers) { |
| 355 | rcu_qtest(argv[0], duration, readers); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | usage(argc, argv); |
| 360 | return -1; |
| 361 | } |