| 1 | /* |
| 2 | * test_urcu.c |
| 3 | * |
| 4 | * Userspace RCU library - test program |
| 5 | * |
| 6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <pthread.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <unistd.h> |
| 30 | #include <stdio.h> |
| 31 | #include <assert.h> |
| 32 | #include <sys/syscall.h> |
| 33 | |
| 34 | #include "arch.h" |
| 35 | |
| 36 | #if defined(_syscall0) |
| 37 | _syscall0(pid_t, gettid) |
| 38 | #elif defined(__NR_gettid) |
| 39 | static inline pid_t gettid(void) |
| 40 | { |
| 41 | return syscall(__NR_gettid); |
| 42 | } |
| 43 | #else |
| 44 | #warning "use pid as tid" |
| 45 | static inline pid_t gettid(void) |
| 46 | { |
| 47 | return getpid(); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | #define _LGPL_SOURCE |
| 52 | #include "urcu-qsbr.h" |
| 53 | |
| 54 | struct test_array { |
| 55 | int a; |
| 56 | }; |
| 57 | |
| 58 | static volatile int test_go; |
| 59 | |
| 60 | static int wdelay; |
| 61 | |
| 62 | static struct test_array *test_rcu_pointer; |
| 63 | |
| 64 | static unsigned long duration; |
| 65 | static time_t start_time; |
| 66 | static unsigned long __thread duration_interval; |
| 67 | #define DURATION_TEST_DELAY_WRITE 4 |
| 68 | #define DURATION_TEST_DELAY_READ 100 |
| 69 | |
| 70 | /* |
| 71 | * returns 0 if test should end. |
| 72 | */ |
| 73 | static int test_duration_write(void) |
| 74 | { |
| 75 | if (duration_interval++ >= DURATION_TEST_DELAY_WRITE) { |
| 76 | duration_interval = 0; |
| 77 | if (time(NULL) - start_time >= duration) |
| 78 | return 0; |
| 79 | } |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | static int test_duration_read(void) |
| 84 | { |
| 85 | if (duration_interval++ >= DURATION_TEST_DELAY_READ) { |
| 86 | duration_interval = 0; |
| 87 | if (time(NULL) - start_time >= duration) |
| 88 | return 0; |
| 89 | } |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | static unsigned long long __thread nr_writes; |
| 94 | static unsigned long long __thread nr_reads; |
| 95 | |
| 96 | static unsigned int nr_readers; |
| 97 | static unsigned int nr_writers; |
| 98 | |
| 99 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 100 | |
| 101 | void rcu_copy_mutex_lock(void) |
| 102 | { |
| 103 | int ret; |
| 104 | ret = pthread_mutex_lock(&rcu_copy_mutex); |
| 105 | if (ret) { |
| 106 | perror("Error in pthread mutex lock"); |
| 107 | exit(-1); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void rcu_copy_mutex_unlock(void) |
| 112 | { |
| 113 | int ret; |
| 114 | |
| 115 | ret = pthread_mutex_unlock(&rcu_copy_mutex); |
| 116 | if (ret) { |
| 117 | perror("Error in pthread mutex unlock"); |
| 118 | exit(-1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * malloc/free are reusing memory areas too quickly, which does not let us |
| 124 | * test races appropriately. Use a large circular array for allocations. |
| 125 | * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail. |
| 126 | */ |
| 127 | #define ARRAY_SIZE (1048576 * nr_writers) |
| 128 | #define ARRAY_POISON 0xDEADBEEF |
| 129 | static int array_index; |
| 130 | static struct test_array *test_array; |
| 131 | |
| 132 | static struct test_array *test_array_alloc(void) |
| 133 | { |
| 134 | struct test_array *ret; |
| 135 | int index; |
| 136 | |
| 137 | rcu_copy_mutex_lock(); |
| 138 | index = array_index % ARRAY_SIZE; |
| 139 | assert(test_array[index].a == ARRAY_POISON || |
| 140 | test_array[index].a == 0); |
| 141 | ret = &test_array[index]; |
| 142 | array_index++; |
| 143 | if (array_index == ARRAY_SIZE) |
| 144 | array_index = 0; |
| 145 | rcu_copy_mutex_unlock(); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static void test_array_free(struct test_array *ptr) |
| 150 | { |
| 151 | if (!ptr) |
| 152 | return; |
| 153 | rcu_copy_mutex_lock(); |
| 154 | ptr->a = ARRAY_POISON; |
| 155 | rcu_copy_mutex_unlock(); |
| 156 | } |
| 157 | |
| 158 | void *thr_reader(void *_count) |
| 159 | { |
| 160 | unsigned long long *count = _count; |
| 161 | struct test_array *local_ptr; |
| 162 | |
| 163 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
| 164 | "reader", pthread_self(), (unsigned long)gettid()); |
| 165 | |
| 166 | rcu_register_thread(); |
| 167 | |
| 168 | while (!test_go) |
| 169 | { |
| 170 | } |
| 171 | smp_mb(); |
| 172 | |
| 173 | for (;;) { |
| 174 | _rcu_read_lock(); |
| 175 | local_ptr = _rcu_dereference(test_rcu_pointer); |
| 176 | debug_yield_read(); |
| 177 | if (local_ptr) |
| 178 | assert(local_ptr->a == 8); |
| 179 | _rcu_read_unlock(); |
| 180 | nr_reads++; |
| 181 | if (duration_interval >= DURATION_TEST_DELAY_READ - 1) |
| 182 | _rcu_quiescent_state(); |
| 183 | if (!test_duration_read()) |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | rcu_unregister_thread(); |
| 188 | |
| 189 | *count = nr_reads; |
| 190 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
| 191 | "reader", pthread_self(), (unsigned long)gettid()); |
| 192 | return ((void*)1); |
| 193 | |
| 194 | } |
| 195 | |
| 196 | void *thr_writer(void *_count) |
| 197 | { |
| 198 | unsigned long long *count = _count; |
| 199 | struct test_array *new, *old; |
| 200 | |
| 201 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
| 202 | "writer", pthread_self(), (unsigned long)gettid()); |
| 203 | |
| 204 | while (!test_go) |
| 205 | { |
| 206 | } |
| 207 | smp_mb(); |
| 208 | |
| 209 | for (;;) { |
| 210 | new = test_array_alloc(); |
| 211 | rcu_copy_mutex_lock(); |
| 212 | old = test_rcu_pointer; |
| 213 | if (old) |
| 214 | assert(old->a == 8); |
| 215 | new->a = 8; |
| 216 | old = _rcu_publish_content(&test_rcu_pointer, new); |
| 217 | rcu_copy_mutex_unlock(); |
| 218 | /* can be done after unlock */ |
| 219 | if (old) |
| 220 | old->a = 0; |
| 221 | test_array_free(old); |
| 222 | nr_writes++; |
| 223 | if (!test_duration_write()) |
| 224 | break; |
| 225 | if (wdelay) |
| 226 | usleep(wdelay); |
| 227 | } |
| 228 | |
| 229 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
| 230 | "writer", pthread_self(), (unsigned long)gettid()); |
| 231 | *count = nr_writes; |
| 232 | return ((void*)2); |
| 233 | } |
| 234 | |
| 235 | void show_usage(int argc, char **argv) |
| 236 | { |
| 237 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
| 238 | #ifdef DEBUG_YIELD |
| 239 | printf(" [-r] [-w] (yield reader and/or writer)"); |
| 240 | #endif |
| 241 | printf(" [-d delay] (writer period (us))"); |
| 242 | printf("\n"); |
| 243 | } |
| 244 | |
| 245 | int main(int argc, char **argv) |
| 246 | { |
| 247 | int err; |
| 248 | pthread_t *tid_reader, *tid_writer; |
| 249 | void *tret; |
| 250 | unsigned long long *count_reader, *count_writer; |
| 251 | unsigned long long tot_reads = 0, tot_writes = 0; |
| 252 | int i; |
| 253 | |
| 254 | if (argc < 4) { |
| 255 | show_usage(argc, argv); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | err = sscanf(argv[1], "%u", &nr_readers); |
| 260 | if (err != 1) { |
| 261 | show_usage(argc, argv); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | err = sscanf(argv[2], "%u", &nr_writers); |
| 266 | if (err != 1) { |
| 267 | show_usage(argc, argv); |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | err = sscanf(argv[3], "%lu", &duration); |
| 272 | if (err != 1) { |
| 273 | show_usage(argc, argv); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | for (i = 4; i < argc; i++) { |
| 278 | if (argv[i][0] != '-') |
| 279 | continue; |
| 280 | switch (argv[i][1]) { |
| 281 | #ifdef DEBUG_YIELD |
| 282 | case 'r': |
| 283 | yield_active |= YIELD_READ; |
| 284 | break; |
| 285 | case 'w': |
| 286 | yield_active |= YIELD_WRITE; |
| 287 | break; |
| 288 | #endif |
| 289 | case 'd': |
| 290 | if (argc < i + 2) { |
| 291 | show_usage(argc, argv); |
| 292 | return -1; |
| 293 | } |
| 294 | wdelay = atoi(argv[++i]); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | printf("running test for %lu seconds, %u readers, %u writers.\n", |
| 300 | duration, nr_readers, nr_writers); |
| 301 | printf("Writer delay : %u us.\n", wdelay); |
| 302 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
| 303 | "main", pthread_self(), (unsigned long)gettid()); |
| 304 | |
| 305 | test_array = malloc(sizeof(*test_array) * ARRAY_SIZE); |
| 306 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
| 307 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); |
| 308 | count_reader = malloc(sizeof(*count_reader) * nr_readers); |
| 309 | count_writer = malloc(sizeof(*count_writer) * nr_writers); |
| 310 | |
| 311 | for (i = 0; i < nr_readers; i++) { |
| 312 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
| 313 | &count_reader[i]); |
| 314 | if (err != 0) |
| 315 | exit(1); |
| 316 | } |
| 317 | for (i = 0; i < nr_writers; i++) { |
| 318 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
| 319 | &count_writer[i]); |
| 320 | if (err != 0) |
| 321 | exit(1); |
| 322 | } |
| 323 | |
| 324 | start_time = time(NULL); |
| 325 | test_go = 1; |
| 326 | smp_mb(); |
| 327 | |
| 328 | for (i = 0; i < nr_readers; i++) { |
| 329 | err = pthread_join(tid_reader[i], &tret); |
| 330 | if (err != 0) |
| 331 | exit(1); |
| 332 | tot_reads += count_reader[i]; |
| 333 | } |
| 334 | for (i = 0; i < nr_writers; i++) { |
| 335 | err = pthread_join(tid_writer[i], &tret); |
| 336 | if (err != 0) |
| 337 | exit(1); |
| 338 | tot_writes += count_writer[i]; |
| 339 | } |
| 340 | |
| 341 | printf("total number of reads : %llu, writes %llu\n", tot_reads, |
| 342 | tot_writes); |
| 343 | test_array_free(test_rcu_pointer); |
| 344 | free(test_array); |
| 345 | free(tid_reader); |
| 346 | free(tid_writer); |
| 347 | free(count_reader); |
| 348 | free(count_writer); |
| 349 | return 0; |
| 350 | } |