Commit | Line | Data |
---|---|---|
b257a10b MD |
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 | ||
ac260fd9 | 11 | #include <stdio.h> |
f69f195a MD |
12 | #include <pthread.h> |
13 | #include <stdlib.h> | |
41718ff9 | 14 | #include <string.h> |
f69f195a MD |
15 | #include <sys/types.h> |
16 | #include <sys/wait.h> | |
17 | #include <unistd.h> | |
18 | #include <stdio.h> | |
41718ff9 | 19 | #include <assert.h> |
87bd15cd BW |
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 | ||
ac260fd9 MD |
37 | #include "urcu.h" |
38 | ||
41718ff9 MD |
39 | struct test_array { |
40 | int a; | |
41718ff9 MD |
41 | }; |
42 | ||
bb488185 MD |
43 | static int no_writer_delay; |
44 | ||
41718ff9 MD |
45 | static struct test_array *test_rcu_pointer; |
46 | ||
cf380c2f MD |
47 | static unsigned long duration; |
48 | static time_t start_time; | |
49 | static unsigned long __thread duration_interval; | |
5873cd17 MD |
50 | #define DURATION_TEST_DELAY_WRITE 4 |
51 | #define DURATION_TEST_DELAY_READ 100 | |
cf380c2f MD |
52 | |
53 | /* | |
54 | * returns 0 if test should end. | |
55 | */ | |
5873cd17 | 56 | static int test_duration_write(void) |
cf380c2f | 57 | { |
5873cd17 MD |
58 | if (duration_interval++ >= DURATION_TEST_DELAY_WRITE) { |
59 | duration_interval = 0; | |
60 | if (time(NULL) - start_time >= duration) | |
61 | return 0; | |
62 | } | |
63 | return 1; | |
64 | } | |
65 | ||
66 | static int test_duration_read(void) | |
67 | { | |
68 | if (duration_interval++ >= DURATION_TEST_DELAY_READ) { | |
cf380c2f MD |
69 | duration_interval = 0; |
70 | if (time(NULL) - start_time >= duration) | |
71 | return 0; | |
72 | } | |
73 | return 1; | |
74 | } | |
75 | ||
b1b5ce8f | 76 | #define NR_READ 10 |
8c8eed97 | 77 | #define NR_WRITE 9 |
f69f195a | 78 | |
1eec319e MD |
79 | static unsigned long long __thread nr_writes; |
80 | static unsigned long long __thread nr_reads; | |
81 | ||
c265818b MD |
82 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
83 | ||
84 | void rcu_copy_mutex_lock(void) | |
85 | { | |
86 | int ret; | |
87 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
88 | if (ret) { | |
89 | perror("Error in pthread mutex lock"); | |
90 | exit(-1); | |
91 | } | |
92 | } | |
93 | ||
94 | void rcu_copy_mutex_unlock(void) | |
95 | { | |
96 | int ret; | |
97 | ||
98 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
99 | if (ret) { | |
100 | perror("Error in pthread mutex unlock"); | |
101 | exit(-1); | |
102 | } | |
103 | } | |
f69f195a | 104 | |
bb488185 MD |
105 | /* |
106 | * malloc/free are reusing memory areas too quickly, which does not let us | |
107 | * test races appropriately. Use a large circular array for allocations. | |
108 | * ARRAY_SIZE is larger than NR_WRITE, which insures we never run over our tail. | |
109 | */ | |
110 | #define ARRAY_SIZE (1048576 * NR_WRITE) | |
111 | #define ARRAY_POISON 0xDEADBEEF | |
112 | static int array_index; | |
113 | static struct test_array test_array[ARRAY_SIZE]; | |
114 | ||
115 | static struct test_array *test_array_alloc(void) | |
116 | { | |
117 | struct test_array *ret; | |
118 | int index; | |
119 | ||
120 | rcu_copy_mutex_lock(); | |
121 | index = array_index % ARRAY_SIZE; | |
122 | assert(test_array[index].a == ARRAY_POISON || | |
123 | test_array[index].a == 0); | |
124 | ret = &test_array[index]; | |
125 | array_index++; | |
126 | if (array_index == ARRAY_SIZE) | |
127 | array_index = 0; | |
128 | rcu_copy_mutex_unlock(); | |
129 | return ret; | |
130 | } | |
131 | ||
132 | static void test_array_free(struct test_array *ptr) | |
133 | { | |
134 | if (!ptr) | |
135 | return; | |
136 | rcu_copy_mutex_lock(); | |
137 | ptr->a = ARRAY_POISON; | |
138 | rcu_copy_mutex_unlock(); | |
139 | } | |
140 | ||
1eec319e | 141 | void *thr_reader(void *_count) |
f69f195a | 142 | { |
1eec319e | 143 | unsigned long long *count = _count; |
41718ff9 MD |
144 | struct test_array *local_ptr; |
145 | ||
cf380c2f | 146 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 147 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a MD |
148 | |
149 | urcu_register_thread(); | |
150 | ||
cf380c2f | 151 | for (;;) { |
1430ee0b | 152 | rcu_read_lock(); |
cf380c2f | 153 | local_ptr = rcu_dereference(test_rcu_pointer); |
bb488185 | 154 | debug_yield_read(); |
cf380c2f MD |
155 | if (local_ptr) |
156 | assert(local_ptr->a == 8); | |
1430ee0b | 157 | rcu_read_unlock(); |
1eec319e | 158 | nr_reads++; |
5873cd17 | 159 | if (!test_duration_read()) |
cf380c2f | 160 | break; |
41718ff9 | 161 | } |
f69f195a MD |
162 | |
163 | urcu_unregister_thread(); | |
41718ff9 | 164 | |
1eec319e | 165 | *count = nr_reads; |
cf380c2f MD |
166 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
167 | "reader", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
168 | return ((void*)1); |
169 | ||
170 | } | |
171 | ||
1eec319e | 172 | void *thr_writer(void *_count) |
f69f195a | 173 | { |
1eec319e | 174 | unsigned long long *count = _count; |
41718ff9 | 175 | struct test_array *new, *old; |
f69f195a | 176 | |
cf380c2f | 177 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 178 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 179 | |
cf380c2f | 180 | for (;;) { |
bb488185 | 181 | new = test_array_alloc(); |
c265818b | 182 | rcu_copy_mutex_lock(); |
41718ff9 | 183 | old = test_rcu_pointer; |
cf380c2f | 184 | if (old) |
41718ff9 | 185 | assert(old->a == 8); |
ad6ce6ae | 186 | new->a = 8; |
f4a486ac | 187 | old = urcu_publish_content(&test_rcu_pointer, new); |
c265818b | 188 | rcu_copy_mutex_unlock(); |
41718ff9 | 189 | /* can be done after unlock */ |
cf380c2f | 190 | if (old) |
8c8eed97 | 191 | old->a = 0; |
bb488185 | 192 | test_array_free(old); |
1eec319e | 193 | nr_writes++; |
5873cd17 | 194 | if (!test_duration_write()) |
cf380c2f | 195 | break; |
bb488185 MD |
196 | if (!no_writer_delay) |
197 | usleep(1); | |
f69f195a MD |
198 | } |
199 | ||
cf380c2f MD |
200 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
201 | "writer", pthread_self(), (unsigned long)gettid()); | |
1eec319e | 202 | *count = nr_writes; |
f69f195a MD |
203 | return ((void*)2); |
204 | } | |
ac260fd9 | 205 | |
1430ee0b MD |
206 | void show_usage(int argc, char **argv) |
207 | { | |
208 | printf("Usage : %s duration (s)", argv[0]); | |
209 | #ifdef DEBUG_YIELD | |
210 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
211 | #endif | |
bb488185 | 212 | printf(" [-n] (disable writer delay)"); |
1430ee0b MD |
213 | printf("\n"); |
214 | } | |
215 | ||
cf380c2f | 216 | int main(int argc, char **argv) |
ac260fd9 | 217 | { |
f69f195a MD |
218 | int err; |
219 | pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE]; | |
220 | void *tret; | |
1eec319e MD |
221 | unsigned long long count_reader[NR_READ], count_writer[NR_WRITE]; |
222 | unsigned long long tot_reads = 0, tot_writes = 0; | |
f69f195a MD |
223 | int i; |
224 | ||
cf380c2f | 225 | if (argc < 2) { |
1430ee0b | 226 | show_usage(argc, argv); |
cf380c2f MD |
227 | return -1; |
228 | } | |
229 | ||
230 | err = sscanf(argv[1], "%lu", &duration); | |
231 | if (err != 1) { | |
1430ee0b | 232 | show_usage(argc, argv); |
cf380c2f MD |
233 | return -1; |
234 | } | |
235 | ||
cf380c2f MD |
236 | for (i = 2; i < argc; i++) { |
237 | if (argv[i][0] != '-') | |
238 | continue; | |
239 | switch (argv[i][1]) { | |
bb488185 | 240 | #ifdef DEBUG_YIELD |
cf380c2f MD |
241 | case 'r': |
242 | yield_active |= YIELD_READ; | |
243 | break; | |
244 | case 'w': | |
245 | yield_active |= YIELD_WRITE; | |
246 | break; | |
bb488185 MD |
247 | #endif |
248 | case 'n': | |
249 | no_writer_delay = 1; | |
250 | break; | |
cf380c2f MD |
251 | } |
252 | } | |
cf380c2f MD |
253 | |
254 | printf("running test for %lu seconds.\n", duration); | |
255 | start_time = time(NULL); | |
87bd15cd BW |
256 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
257 | "main", pthread_self(), (unsigned long)gettid()); | |
258 | ||
f69f195a | 259 | for (i = 0; i < NR_READ; i++) { |
1eec319e MD |
260 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
261 | &count_reader[i]); | |
f69f195a MD |
262 | if (err != 0) |
263 | exit(1); | |
264 | } | |
265 | for (i = 0; i < NR_WRITE; i++) { | |
1eec319e MD |
266 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
267 | &count_writer[i]); | |
f69f195a MD |
268 | if (err != 0) |
269 | exit(1); | |
270 | } | |
271 | ||
20bf310a | 272 | for (i = 0; i < NR_READ; i++) { |
f69f195a MD |
273 | err = pthread_join(tid_reader[i], &tret); |
274 | if (err != 0) | |
275 | exit(1); | |
1eec319e | 276 | tot_reads += count_reader[i]; |
f69f195a MD |
277 | } |
278 | for (i = 0; i < NR_WRITE; i++) { | |
279 | err = pthread_join(tid_writer[i], &tret); | |
280 | if (err != 0) | |
281 | exit(1); | |
1eec319e | 282 | tot_writes += count_writer[i]; |
f69f195a | 283 | } |
5873cd17 | 284 | |
1eec319e MD |
285 | printf("total number of reads : %llu, writes %llu\n", tot_reads, |
286 | tot_writes); | |
bb488185 | 287 | test_array_free(test_rcu_pointer); |
ac260fd9 | 288 | |
f69f195a | 289 | return 0; |
ac260fd9 | 290 | } |