Add QSBR RCU timing tests
[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 * 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.
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <assert.h>
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
49 #ifndef DYNAMIC_LINK_TEST
50 #define _LGPL_SOURCE
51 #else
52 #define debug_yield_read()
53 #endif
54 #include "urcu.h"
55
56 struct test_array {
57 int a;
58 };
59
60 static int no_writer_delay;
61
62 static struct test_array *test_rcu_pointer;
63
64 static unsigned long duration;
65 static time_t start_time;
66 static unsigned long __thread duration_interval;
67 #define DURATION_TEST_DELAY_WRITE 4
68 #define DURATION_TEST_DELAY_READ 100
69
70 /*
71 * returns 0 if test should end.
72 */
73 static int test_duration_write(void)
74 {
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) {
86 duration_interval = 0;
87 if (time(NULL) - start_time >= duration)
88 return 0;
89 }
90 return 1;
91 }
92
93 #define NR_READ 10
94 #define NR_WRITE 9
95
96 static unsigned long long __thread nr_writes;
97 static unsigned long long __thread nr_reads;
98
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 }
121
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
158 void *thr_reader(void *_count)
159 {
160 unsigned long long *count = _count;
161 struct test_array *local_ptr;
162
163 printf("thread_begin %s, thread id : %lx, tid %lu\n",
164 "reader", pthread_self(), (unsigned long)gettid());
165
166 rcu_register_thread();
167
168 for (;;) {
169 rcu_read_lock();
170 local_ptr = rcu_dereference(test_rcu_pointer);
171 debug_yield_read();
172 if (local_ptr)
173 assert(local_ptr->a == 8);
174 rcu_read_unlock();
175 nr_reads++;
176 if (!test_duration_read())
177 break;
178 }
179
180 rcu_unregister_thread();
181
182 *count = nr_reads;
183 printf("thread_end %s, thread id : %lx, tid %lu\n",
184 "reader", pthread_self(), (unsigned long)gettid());
185 return ((void*)1);
186
187 }
188
189 void *thr_writer(void *_count)
190 {
191 unsigned long long *count = _count;
192 struct test_array *new, *old;
193
194 printf("thread_begin %s, thread id : %lx, tid %lu\n",
195 "writer", pthread_self(), (unsigned long)gettid());
196
197 for (;;) {
198 new = test_array_alloc();
199 rcu_copy_mutex_lock();
200 old = test_rcu_pointer;
201 if (old)
202 assert(old->a == 8);
203 new->a = 8;
204 old = rcu_publish_content(&test_rcu_pointer, new);
205 rcu_copy_mutex_unlock();
206 /* can be done after unlock */
207 if (old)
208 old->a = 0;
209 test_array_free(old);
210 nr_writes++;
211 if (!test_duration_write())
212 break;
213 if (!no_writer_delay)
214 usleep(1);
215 }
216
217 printf("thread_end %s, thread id : %lx, tid %lu\n",
218 "writer", pthread_self(), (unsigned long)gettid());
219 *count = nr_writes;
220 return ((void*)2);
221 }
222
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
229 printf(" [-n] (disable writer delay)");
230 printf("\n");
231 }
232
233 int main(int argc, char **argv)
234 {
235 int err;
236 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
237 void *tret;
238 unsigned long long count_reader[NR_READ], count_writer[NR_WRITE];
239 unsigned long long tot_reads = 0, tot_writes = 0;
240 int i;
241
242 if (argc < 2) {
243 show_usage(argc, argv);
244 return -1;
245 }
246
247 err = sscanf(argv[1], "%lu", &duration);
248 if (err != 1) {
249 show_usage(argc, argv);
250 return -1;
251 }
252
253 for (i = 2; i < argc; i++) {
254 if (argv[i][0] != '-')
255 continue;
256 switch (argv[i][1]) {
257 #ifdef DEBUG_YIELD
258 case 'r':
259 yield_active |= YIELD_READ;
260 break;
261 case 'w':
262 yield_active |= YIELD_WRITE;
263 break;
264 #endif
265 case 'n':
266 no_writer_delay = 1;
267 break;
268 }
269 }
270
271 printf("running test for %lu seconds.\n", duration);
272 start_time = time(NULL);
273 printf("thread %-6s, thread id : %lx, tid %lu\n",
274 "main", pthread_self(), (unsigned long)gettid());
275
276 for (i = 0; i < NR_READ; i++) {
277 err = pthread_create(&tid_reader[i], NULL, thr_reader,
278 &count_reader[i]);
279 if (err != 0)
280 exit(1);
281 }
282 for (i = 0; i < NR_WRITE; i++) {
283 err = pthread_create(&tid_writer[i], NULL, thr_writer,
284 &count_writer[i]);
285 if (err != 0)
286 exit(1);
287 }
288
289 for (i = 0; i < NR_READ; i++) {
290 err = pthread_join(tid_reader[i], &tret);
291 if (err != 0)
292 exit(1);
293 tot_reads += count_reader[i];
294 }
295 for (i = 0; i < NR_WRITE; i++) {
296 err = pthread_join(tid_writer[i], &tret);
297 if (err != 0)
298 exit(1);
299 tot_writes += count_writer[i];
300 }
301
302 printf("total number of reads : %llu, writes %llu\n", tot_reads,
303 tot_writes);
304 test_array_free(test_rcu_pointer);
305
306 return 0;
307 }
This page took 0.037303 seconds and 4 git commands to generate.