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