LGPLv2.1 relicensing
[urcu.git] / test_urcu.c
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 #ifndef DYNAMIC_LINK_TEST
38 #define _LGPL_SOURCE
39 #else
40 #define debug_yield_read()
41 #endif
42 #include "urcu.h"
43
44 struct test_array {
45 int a;
46 };
47
48 static int no_writer_delay;
49
50 static struct test_array *test_rcu_pointer;
51
52 static unsigned long duration;
53 static time_t start_time;
54 static unsigned long __thread duration_interval;
55 #define DURATION_TEST_DELAY_WRITE 4
56 #define DURATION_TEST_DELAY_READ 100
57
58 /*
59 * returns 0 if test should end.
60 */
61 static int test_duration_write(void)
62 {
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) {
74 duration_interval = 0;
75 if (time(NULL) - start_time >= duration)
76 return 0;
77 }
78 return 1;
79 }
80
81 #define NR_READ 10
82 #define NR_WRITE 9
83
84 static unsigned long long __thread nr_writes;
85 static unsigned long long __thread nr_reads;
86
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 }
109
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
146 void *thr_reader(void *_count)
147 {
148 unsigned long long *count = _count;
149 struct test_array *local_ptr;
150
151 printf("thread_begin %s, thread id : %lx, tid %lu\n",
152 "reader", pthread_self(), (unsigned long)gettid());
153
154 rcu_register_thread();
155
156 for (;;) {
157 rcu_read_lock();
158 local_ptr = rcu_dereference(test_rcu_pointer);
159 debug_yield_read();
160 if (local_ptr)
161 assert(local_ptr->a == 8);
162 rcu_read_unlock();
163 nr_reads++;
164 if (!test_duration_read())
165 break;
166 }
167
168 rcu_unregister_thread();
169
170 *count = nr_reads;
171 printf("thread_end %s, thread id : %lx, tid %lu\n",
172 "reader", pthread_self(), (unsigned long)gettid());
173 return ((void*)1);
174
175 }
176
177 void *thr_writer(void *_count)
178 {
179 unsigned long long *count = _count;
180 struct test_array *new, *old;
181
182 printf("thread_begin %s, thread id : %lx, tid %lu\n",
183 "writer", pthread_self(), (unsigned long)gettid());
184
185 for (;;) {
186 new = test_array_alloc();
187 rcu_copy_mutex_lock();
188 old = test_rcu_pointer;
189 if (old)
190 assert(old->a == 8);
191 new->a = 8;
192 old = rcu_publish_content(&test_rcu_pointer, new);
193 rcu_copy_mutex_unlock();
194 /* can be done after unlock */
195 if (old)
196 old->a = 0;
197 test_array_free(old);
198 nr_writes++;
199 if (!test_duration_write())
200 break;
201 if (!no_writer_delay)
202 usleep(1);
203 }
204
205 printf("thread_end %s, thread id : %lx, tid %lu\n",
206 "writer", pthread_self(), (unsigned long)gettid());
207 *count = nr_writes;
208 return ((void*)2);
209 }
210
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
217 printf(" [-n] (disable writer delay)");
218 printf("\n");
219 }
220
221 int main(int argc, char **argv)
222 {
223 int err;
224 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
225 void *tret;
226 unsigned long long count_reader[NR_READ], count_writer[NR_WRITE];
227 unsigned long long tot_reads = 0, tot_writes = 0;
228 int i;
229
230 if (argc < 2) {
231 show_usage(argc, argv);
232 return -1;
233 }
234
235 err = sscanf(argv[1], "%lu", &duration);
236 if (err != 1) {
237 show_usage(argc, argv);
238 return -1;
239 }
240
241 for (i = 2; i < argc; i++) {
242 if (argv[i][0] != '-')
243 continue;
244 switch (argv[i][1]) {
245 #ifdef DEBUG_YIELD
246 case 'r':
247 yield_active |= YIELD_READ;
248 break;
249 case 'w':
250 yield_active |= YIELD_WRITE;
251 break;
252 #endif
253 case 'n':
254 no_writer_delay = 1;
255 break;
256 }
257 }
258
259 printf("running test for %lu seconds.\n", duration);
260 start_time = time(NULL);
261 printf("thread %-6s, thread id : %lx, tid %lu\n",
262 "main", pthread_self(), (unsigned long)gettid());
263
264 for (i = 0; i < NR_READ; i++) {
265 err = pthread_create(&tid_reader[i], NULL, thr_reader,
266 &count_reader[i]);
267 if (err != 0)
268 exit(1);
269 }
270 for (i = 0; i < NR_WRITE; i++) {
271 err = pthread_create(&tid_writer[i], NULL, thr_writer,
272 &count_writer[i]);
273 if (err != 0)
274 exit(1);
275 }
276
277 for (i = 0; i < NR_READ; i++) {
278 err = pthread_join(tid_reader[i], &tret);
279 if (err != 0)
280 exit(1);
281 tot_reads += count_reader[i];
282 }
283 for (i = 0; i < NR_WRITE; i++) {
284 err = pthread_join(tid_writer[i], &tret);
285 if (err != 0)
286 exit(1);
287 tot_writes += count_writer[i];
288 }
289
290 printf("total number of reads : %llu, writes %llu\n", tot_reads,
291 tot_writes);
292 test_array_free(test_rcu_pointer);
293
294 return 0;
295 }
This page took 0.033863 seconds and 4 git commands to generate.