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