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 23 #include "qemu/osdep.h" 24 #include "qemu/atomic.h" 25 #include "qemu/rcu.h" 26 #include "qemu/thread.h" 27 #include "qemu/rcu_queue.h" 28 29 /* 30 * Test variables. 31 */ 32 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; 40 41 static int nthreadsrunning; 42 43 #define GOFLAG_INIT 0 44 #define GOFLAG_RUN 1 45 #define GOFLAG_STOP 2 46 47 static int goflag = GOFLAG_INIT; 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 85 #ifndef TEST_LIST_TYPE 86 #define TEST_LIST_TYPE 1 87 #endif 88 89 struct list_element { 90 #if TEST_LIST_TYPE == 1 91 QLIST_ENTRY(list_element) entry; 92 #else 93 #error Invalid TEST_LIST_TYPE 94 #endif 95 struct rcu_head rcu; 96 }; 97 98 static void reclaim_list_el(struct rcu_head *prcu) 99 { 100 struct list_element *el = container_of(prcu, struct list_element, rcu); 101 g_free(el); 102 /* Accessed only from call_rcu thread. */ 103 n_reclaims++; 104 } 105 106 #if TEST_LIST_TYPE == 1 107 static QLIST_HEAD(q_list_head, list_element) Q_list_head; 108 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 115 #else 116 #error Invalid TEST_LIST_TYPE 117 #endif 118 119 static void *rcu_q_reader(void *arg) 120 { 121 long long n_reads_local = 0; 122 struct list_element *el; 123 124 rcu_register_thread(); 125 126 *(struct rcu_reader_data **)arg = &rcu_reader; 127 atomic_inc(&nthreadsrunning); 128 while (atomic_read(&goflag) == GOFLAG_INIT) { 129 g_usleep(1000); 130 } 131 132 while (atomic_read(&goflag) == GOFLAG_RUN) { 133 rcu_read_lock(); 134 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) { 135 n_reads_local++; 136 if (atomic_read(&goflag) == GOFLAG_STOP) { 137 break; 138 } 139 } 140 rcu_read_unlock(); 141 142 g_usleep(100); 143 } 144 qemu_mutex_lock(&counts_mutex); 145 n_reads += n_reads_local; 146 qemu_mutex_unlock(&counts_mutex); 147 148 rcu_unregister_thread(); 149 return NULL; 150 } 151 152 153 static void *rcu_q_updater(void *arg) 154 { 155 int j, target_el; 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; 160 161 *(struct rcu_reader_data **)arg = &rcu_reader; 162 atomic_inc(&nthreadsrunning); 163 while (atomic_read(&goflag) == GOFLAG_INIT) { 164 g_usleep(1000); 165 } 166 167 while (atomic_read(&goflag) == GOFLAG_RUN) { 168 target_el = select_random_el(RCU_Q_LEN); 169 j = 0; 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) { 172 j++; 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); 177 n_removed_local++; 178 break; 179 } 180 } 181 if (atomic_read(&goflag) == GOFLAG_STOP) { 182 break; 183 } 184 target_el = select_random_el(RCU_Q_LEN); 185 j = 0; 186 TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) { 187 j++; 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); 192 break; 193 } 194 } 195 196 n_updates_local += 2; 197 synchronize_rcu(); 198 } 199 synchronize_rcu(); 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); 205 return NULL; 206 } 207 208 static void rcu_qtest_init(void) 209 { 210 struct list_element *new_el; 211 int i; 212 nthreadsrunning = 0; 213 srand(time(0)); 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); 217 } 218 qemu_mutex_lock(&counts_mutex); 219 n_nodes += RCU_Q_LEN; 220 qemu_mutex_unlock(&counts_mutex); 221 } 222 223 static void rcu_qtest_run(int duration, int nreaders) 224 { 225 int nthreads = nreaders + 1; 226 while (atomic_read(&nthreadsrunning) < nthreads) { 227 g_usleep(1000); 228 } 229 230 atomic_set(&goflag, GOFLAG_RUN); 231 sleep(duration); 232 atomic_set(&goflag, GOFLAG_STOP); 233 wait_all_threads(); 234 } 235 236 237 static void rcu_qtest(const char *test, int duration, int nreaders) 238 { 239 int i; 240 long long n_removed_local = 0; 241 242 struct list_element *el, *prev_el; 243 244 rcu_qtest_init(); 245 for (i = 0; i < nreaders; i++) { 246 create_thread(rcu_q_reader); 247 } 248 create_thread(rcu_q_updater); 249 rcu_qtest_run(duration, nreaders); 250 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); 254 n_removed_local++; 255 } 256 qemu_mutex_lock(&counts_mutex); 257 n_nodes_removed += n_removed_local; 258 qemu_mutex_unlock(&counts_mutex); 259 synchronize_rcu(); 260 while (n_nodes_removed > n_reclaims) { 261 g_usleep(100); 262 synchronize_rcu(); 263 } 264 if (g_test_in_charge) { 265 g_assert_cmpint(n_nodes_removed, ==, n_reclaims); 266 } else { 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); 270 exit(0); 271 } 272 } 273 274 static void usage(int argc, char *argv[]) 275 { 276 fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]); 277 exit(-1); 278 } 279 280 static int gtest_seconds; 281 282 static void gtest_rcuq_one(void) 283 { 284 rcu_qtest("rcuqtest", gtest_seconds / 4, 1); 285 } 286 287 static void gtest_rcuq_few(void) 288 { 289 rcu_qtest("rcuqtest", gtest_seconds / 4, 5); 290 } 291 292 static void gtest_rcuq_many(void) 293 { 294 rcu_qtest("rcuqtest", gtest_seconds / 2, 20); 295 } 296 297 298 int main(int argc, char *argv[]) 299 { 300 int duration = 0, readers = 0; 301 302 qemu_mutex_init(&counts_mutex); 303 if (argc >= 2) { 304 if (argv[1][0] == '-') { 305 g_test_init(&argc, &argv, NULL); 306 if (g_test_quick()) { 307 gtest_seconds = 4; 308 } else { 309 gtest_seconds = 20; 310 } 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; 315 return g_test_run(); 316 } 317 duration = strtoul(argv[1], NULL, 0); 318 } 319 if (argc >= 3) { 320 readers = strtoul(argv[2], NULL, 0); 321 } 322 if (duration && readers) { 323 rcu_qtest(argv[0], duration, readers); 324 return 0; 325 } 326 327 usage(argc, argv); 328 return -1; 329 } 330