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 | * | |
af02d47e MD |
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. | |
b257a10b MD |
21 | */ |
22 | ||
ac260fd9 | 23 | #include <stdio.h> |
f69f195a MD |
24 | #include <pthread.h> |
25 | #include <stdlib.h> | |
41718ff9 | 26 | #include <string.h> |
f69f195a MD |
27 | #include <sys/types.h> |
28 | #include <sys/wait.h> | |
29 | #include <unistd.h> | |
30 | #include <stdio.h> | |
41718ff9 | 31 | #include <assert.h> |
87bd15cd BW |
32 | #include <sys/syscall.h> |
33 | ||
34 | #if defined(_syscall0) | |
35 | _syscall0(pid_t, gettid) | |
36 | #elif defined(__NR_gettid) | |
37 | static inline pid_t gettid(void) | |
38 | { | |
39 | return syscall(__NR_gettid); | |
40 | } | |
41 | #else | |
42 | #warning "use pid as tid" | |
43 | static inline pid_t gettid(void) | |
44 | { | |
45 | return getpid(); | |
46 | } | |
47 | #endif | |
48 | ||
121a5d44 MD |
49 | #ifndef DYNAMIC_LINK_TEST |
50 | #define _LGPL_SOURCE | |
51 | #else | |
52 | #define debug_yield_read() | |
53 | #endif | |
ac260fd9 MD |
54 | #include "urcu.h" |
55 | ||
41718ff9 MD |
56 | struct test_array { |
57 | int a; | |
41718ff9 MD |
58 | }; |
59 | ||
bb488185 MD |
60 | static int no_writer_delay; |
61 | ||
41718ff9 MD |
62 | static struct test_array *test_rcu_pointer; |
63 | ||
cf380c2f MD |
64 | static unsigned long duration; |
65 | static time_t start_time; | |
66 | static unsigned long __thread duration_interval; | |
5873cd17 MD |
67 | #define DURATION_TEST_DELAY_WRITE 4 |
68 | #define DURATION_TEST_DELAY_READ 100 | |
cf380c2f MD |
69 | |
70 | /* | |
71 | * returns 0 if test should end. | |
72 | */ | |
5873cd17 | 73 | static int test_duration_write(void) |
cf380c2f | 74 | { |
5873cd17 MD |
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) { | |
cf380c2f MD |
86 | duration_interval = 0; |
87 | if (time(NULL) - start_time >= duration) | |
88 | return 0; | |
89 | } | |
90 | return 1; | |
91 | } | |
92 | ||
b1b5ce8f | 93 | #define NR_READ 10 |
8c8eed97 | 94 | #define NR_WRITE 9 |
f69f195a | 95 | |
1eec319e MD |
96 | static unsigned long long __thread nr_writes; |
97 | static unsigned long long __thread nr_reads; | |
98 | ||
c265818b MD |
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 | } | |
f69f195a | 121 | |
bb488185 MD |
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_WRITE, which insures we never run over our tail. | |
126 | */ | |
127 | #define ARRAY_SIZE (1048576 * NR_WRITE) | |
128 | #define ARRAY_POISON 0xDEADBEEF | |
129 | static int array_index; | |
130 | static struct test_array test_array[ARRAY_SIZE]; | |
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 | ||
1eec319e | 158 | void *thr_reader(void *_count) |
f69f195a | 159 | { |
1eec319e | 160 | unsigned long long *count = _count; |
41718ff9 MD |
161 | struct test_array *local_ptr; |
162 | ||
cf380c2f | 163 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 164 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a | 165 | |
121a5d44 | 166 | rcu_register_thread(); |
f69f195a | 167 | |
cf380c2f | 168 | for (;;) { |
1430ee0b | 169 | rcu_read_lock(); |
cf380c2f | 170 | local_ptr = rcu_dereference(test_rcu_pointer); |
bb488185 | 171 | debug_yield_read(); |
cf380c2f MD |
172 | if (local_ptr) |
173 | assert(local_ptr->a == 8); | |
1430ee0b | 174 | rcu_read_unlock(); |
1eec319e | 175 | nr_reads++; |
5873cd17 | 176 | if (!test_duration_read()) |
cf380c2f | 177 | break; |
41718ff9 | 178 | } |
f69f195a | 179 | |
121a5d44 | 180 | rcu_unregister_thread(); |
41718ff9 | 181 | |
1eec319e | 182 | *count = nr_reads; |
cf380c2f MD |
183 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
184 | "reader", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
185 | return ((void*)1); |
186 | ||
187 | } | |
188 | ||
1eec319e | 189 | void *thr_writer(void *_count) |
f69f195a | 190 | { |
1eec319e | 191 | unsigned long long *count = _count; |
41718ff9 | 192 | struct test_array *new, *old; |
f69f195a | 193 | |
cf380c2f | 194 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 195 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 196 | |
cf380c2f | 197 | for (;;) { |
bb488185 | 198 | new = test_array_alloc(); |
c265818b | 199 | rcu_copy_mutex_lock(); |
41718ff9 | 200 | old = test_rcu_pointer; |
cf380c2f | 201 | if (old) |
41718ff9 | 202 | assert(old->a == 8); |
ad6ce6ae | 203 | new->a = 8; |
121a5d44 | 204 | old = rcu_publish_content(&test_rcu_pointer, new); |
c265818b | 205 | rcu_copy_mutex_unlock(); |
41718ff9 | 206 | /* can be done after unlock */ |
cf380c2f | 207 | if (old) |
8c8eed97 | 208 | old->a = 0; |
bb488185 | 209 | test_array_free(old); |
1eec319e | 210 | nr_writes++; |
5873cd17 | 211 | if (!test_duration_write()) |
cf380c2f | 212 | break; |
bb488185 MD |
213 | if (!no_writer_delay) |
214 | usleep(1); | |
f69f195a MD |
215 | } |
216 | ||
cf380c2f MD |
217 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
218 | "writer", pthread_self(), (unsigned long)gettid()); | |
1eec319e | 219 | *count = nr_writes; |
f69f195a MD |
220 | return ((void*)2); |
221 | } | |
ac260fd9 | 222 | |
1430ee0b MD |
223 | void show_usage(int argc, char **argv) |
224 | { | |
225 | printf("Usage : %s duration (s)", argv[0]); | |
226 | #ifdef DEBUG_YIELD | |
227 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
228 | #endif | |
bb488185 | 229 | printf(" [-n] (disable writer delay)"); |
1430ee0b MD |
230 | printf("\n"); |
231 | } | |
232 | ||
cf380c2f | 233 | int main(int argc, char **argv) |
ac260fd9 | 234 | { |
f69f195a MD |
235 | int err; |
236 | pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE]; | |
237 | void *tret; | |
1eec319e MD |
238 | unsigned long long count_reader[NR_READ], count_writer[NR_WRITE]; |
239 | unsigned long long tot_reads = 0, tot_writes = 0; | |
f69f195a MD |
240 | int i; |
241 | ||
cf380c2f | 242 | if (argc < 2) { |
1430ee0b | 243 | show_usage(argc, argv); |
cf380c2f MD |
244 | return -1; |
245 | } | |
246 | ||
247 | err = sscanf(argv[1], "%lu", &duration); | |
248 | if (err != 1) { | |
1430ee0b | 249 | show_usage(argc, argv); |
cf380c2f MD |
250 | return -1; |
251 | } | |
252 | ||
cf380c2f MD |
253 | for (i = 2; i < argc; i++) { |
254 | if (argv[i][0] != '-') | |
255 | continue; | |
256 | switch (argv[i][1]) { | |
bb488185 | 257 | #ifdef DEBUG_YIELD |
cf380c2f MD |
258 | case 'r': |
259 | yield_active |= YIELD_READ; | |
260 | break; | |
261 | case 'w': | |
262 | yield_active |= YIELD_WRITE; | |
263 | break; | |
bb488185 MD |
264 | #endif |
265 | case 'n': | |
266 | no_writer_delay = 1; | |
267 | break; | |
cf380c2f MD |
268 | } |
269 | } | |
cf380c2f MD |
270 | |
271 | printf("running test for %lu seconds.\n", duration); | |
272 | start_time = time(NULL); | |
87bd15cd BW |
273 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
274 | "main", pthread_self(), (unsigned long)gettid()); | |
275 | ||
f69f195a | 276 | for (i = 0; i < NR_READ; i++) { |
1eec319e MD |
277 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
278 | &count_reader[i]); | |
f69f195a MD |
279 | if (err != 0) |
280 | exit(1); | |
281 | } | |
282 | for (i = 0; i < NR_WRITE; i++) { | |
1eec319e MD |
283 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
284 | &count_writer[i]); | |
f69f195a MD |
285 | if (err != 0) |
286 | exit(1); | |
287 | } | |
288 | ||
20bf310a | 289 | for (i = 0; i < NR_READ; i++) { |
f69f195a MD |
290 | err = pthread_join(tid_reader[i], &tret); |
291 | if (err != 0) | |
292 | exit(1); | |
1eec319e | 293 | tot_reads += count_reader[i]; |
f69f195a MD |
294 | } |
295 | for (i = 0; i < NR_WRITE; i++) { | |
296 | err = pthread_join(tid_writer[i], &tret); | |
297 | if (err != 0) | |
298 | exit(1); | |
1eec319e | 299 | tot_writes += count_writer[i]; |
f69f195a | 300 | } |
5873cd17 | 301 | |
1eec319e MD |
302 | printf("total number of reads : %llu, writes %llu\n", tot_reads, |
303 | tot_writes); | |
bb488185 | 304 | test_array_free(test_rcu_pointer); |
ac260fd9 | 305 | |
f69f195a | 306 | return 0; |
ac260fd9 | 307 | } |