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