| 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 | * Distributed under GPLv2 |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <pthread.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/wait.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdio.h> |
| 19 | #include <assert.h> |
| 20 | #include <sys/syscall.h> |
| 21 | |
| 22 | #if defined(_syscall0) |
| 23 | _syscall0(pid_t, gettid) |
| 24 | #elif defined(__NR_gettid) |
| 25 | static inline pid_t gettid(void) |
| 26 | { |
| 27 | return syscall(__NR_gettid); |
| 28 | } |
| 29 | #else |
| 30 | #warning "use pid as tid" |
| 31 | static inline pid_t gettid(void) |
| 32 | { |
| 33 | return getpid(); |
| 34 | } |
| 35 | #endif |
| 36 | |
| 37 | #define rdtscll(val) do { \ |
| 38 | unsigned int __a,__d; \ |
| 39 | asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \ |
| 40 | (val) = ((unsigned long long)__a) | (((unsigned long long)__d)<<32); \ |
| 41 | } while(0) |
| 42 | |
| 43 | typedef unsigned long long cycles_t; |
| 44 | |
| 45 | static inline cycles_t get_cycles (void) |
| 46 | { |
| 47 | unsigned long long ret = 0; |
| 48 | |
| 49 | rdtscll(ret); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | #include "urcu.h" |
| 54 | |
| 55 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 56 | |
| 57 | void rcu_copy_mutex_lock(void) |
| 58 | { |
| 59 | int ret; |
| 60 | ret = pthread_mutex_lock(&rcu_copy_mutex); |
| 61 | if (ret) { |
| 62 | perror("Error in pthread mutex lock"); |
| 63 | exit(-1); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void rcu_copy_mutex_unlock(void) |
| 68 | { |
| 69 | int ret; |
| 70 | |
| 71 | ret = pthread_mutex_unlock(&rcu_copy_mutex); |
| 72 | if (ret) { |
| 73 | perror("Error in pthread mutex unlock"); |
| 74 | exit(-1); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | struct test_array { |
| 79 | int a; |
| 80 | }; |
| 81 | |
| 82 | static struct test_array *test_rcu_pointer; |
| 83 | |
| 84 | #define OUTER_READ_LOOP 2000U |
| 85 | #define INNER_READ_LOOP 100000U |
| 86 | #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP) |
| 87 | |
| 88 | #define WRITE_LOOP 2000U |
| 89 | |
| 90 | #define NR_READ 10 |
| 91 | #define NR_WRITE 9 |
| 92 | |
| 93 | static cycles_t reader_time[NR_READ] __attribute__((aligned(128))); |
| 94 | |
| 95 | void *thr_reader(void *arg) |
| 96 | { |
| 97 | int i, j; |
| 98 | struct test_array *local_ptr; |
| 99 | cycles_t time1, time2; |
| 100 | |
| 101 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
| 102 | "reader", pthread_self(), (unsigned long)gettid()); |
| 103 | sleep(2); |
| 104 | |
| 105 | urcu_register_thread(); |
| 106 | |
| 107 | time1 = get_cycles(); |
| 108 | for (i = 0; i < OUTER_READ_LOOP; i++) { |
| 109 | for (j = 0; j < INNER_READ_LOOP; j++) { |
| 110 | rcu_read_lock(); |
| 111 | local_ptr = rcu_dereference(test_rcu_pointer); |
| 112 | if (local_ptr) { |
| 113 | assert(local_ptr->a == 8); |
| 114 | } |
| 115 | rcu_read_unlock(); |
| 116 | } |
| 117 | } |
| 118 | time2 = get_cycles(); |
| 119 | |
| 120 | urcu_unregister_thread(); |
| 121 | |
| 122 | reader_time[(unsigned long)arg] = time2 - time1; |
| 123 | |
| 124 | sleep(2); |
| 125 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
| 126 | "reader", pthread_self(), (unsigned long)gettid()); |
| 127 | return ((void*)1); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | void *thr_writer(void *arg) |
| 132 | { |
| 133 | int i; |
| 134 | struct test_array *new, *old; |
| 135 | |
| 136 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
| 137 | "writer", pthread_self(), (unsigned long)gettid()); |
| 138 | sleep(2); |
| 139 | |
| 140 | for (i = 0; i < WRITE_LOOP; i++) { |
| 141 | new = malloc(sizeof(struct test_array)); |
| 142 | rcu_copy_mutex_lock(); |
| 143 | old = test_rcu_pointer; |
| 144 | if (old) { |
| 145 | assert(old->a == 8); |
| 146 | } |
| 147 | new->a = 8; |
| 148 | old = urcu_publish_content(&test_rcu_pointer, new); |
| 149 | rcu_copy_mutex_unlock(); |
| 150 | /* can be done after unlock */ |
| 151 | if (old) { |
| 152 | old->a = 0; |
| 153 | } |
| 154 | free(old); |
| 155 | usleep(1); |
| 156 | } |
| 157 | |
| 158 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
| 159 | "writer", pthread_self(), (unsigned long)gettid()); |
| 160 | return ((void*)2); |
| 161 | } |
| 162 | |
| 163 | int main() |
| 164 | { |
| 165 | int err; |
| 166 | pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE]; |
| 167 | void *tret; |
| 168 | int i; |
| 169 | cycles_t tot_time = 0; |
| 170 | |
| 171 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
| 172 | "main", pthread_self(), (unsigned long)gettid()); |
| 173 | |
| 174 | for (i = 0; i < NR_READ; i++) { |
| 175 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
| 176 | (void *)(long)i); |
| 177 | if (err != 0) |
| 178 | exit(1); |
| 179 | } |
| 180 | for (i = 0; i < NR_WRITE; i++) { |
| 181 | err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL); |
| 182 | if (err != 0) |
| 183 | exit(1); |
| 184 | } |
| 185 | |
| 186 | sleep(10); |
| 187 | |
| 188 | for (i = 0; i < NR_READ; i++) { |
| 189 | err = pthread_join(tid_reader[i], &tret); |
| 190 | if (err != 0) |
| 191 | exit(1); |
| 192 | tot_time += reader_time[i]; |
| 193 | } |
| 194 | for (i = 0; i < NR_WRITE; i++) { |
| 195 | err = pthread_join(tid_writer[i], &tret); |
| 196 | if (err != 0) |
| 197 | exit(1); |
| 198 | } |
| 199 | free(test_rcu_pointer); |
| 200 | printf("Time per read : %g cycles\n", |
| 201 | (double)tot_time / ((double)NR_READ * (double)READ_LOOP)); |
| 202 | |
| 203 | return 0; |
| 204 | } |