Add scalability qsbr test
[urcu.git] / test_qsbr.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 #define _LGPL_SOURCE
50 #include "urcu-qsbr.h"
51
52 struct test_array {
53 int a;
54 };
55
56 static volatile int test_go;
57
58 static int wdelay;
59
60 static struct test_array *test_rcu_pointer;
61
62 static unsigned long duration;
63 static time_t start_time;
64 static unsigned long __thread duration_interval;
65 #define DURATION_TEST_DELAY_WRITE 4
66 #define DURATION_TEST_DELAY_READ 100
67
68 /*
69 * returns 0 if test should end.
70 */
71 static int test_duration_write(void)
72 {
73 if (duration_interval++ >= DURATION_TEST_DELAY_WRITE) {
74 duration_interval = 0;
75 if (time(NULL) - start_time >= duration)
76 return 0;
77 }
78 return 1;
79 }
80
81 static int test_duration_read(void)
82 {
83 if (duration_interval++ >= DURATION_TEST_DELAY_READ) {
84 duration_interval = 0;
85 if (time(NULL) - start_time >= duration)
86 return 0;
87 }
88 return 1;
89 }
90
91 static unsigned long long __thread nr_writes;
92 static unsigned long long __thread nr_reads;
93
94 static unsigned int nr_readers;
95 static unsigned int nr_writers;
96
97 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
98
99 void rcu_copy_mutex_lock(void)
100 {
101 int ret;
102 ret = pthread_mutex_lock(&rcu_copy_mutex);
103 if (ret) {
104 perror("Error in pthread mutex lock");
105 exit(-1);
106 }
107 }
108
109 void rcu_copy_mutex_unlock(void)
110 {
111 int ret;
112
113 ret = pthread_mutex_unlock(&rcu_copy_mutex);
114 if (ret) {
115 perror("Error in pthread mutex unlock");
116 exit(-1);
117 }
118 }
119
120 /*
121 * malloc/free are reusing memory areas too quickly, which does not let us
122 * test races appropriately. Use a large circular array for allocations.
123 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
124 */
125 #define ARRAY_SIZE (1048576 * nr_writers)
126 #define ARRAY_POISON 0xDEADBEEF
127 static int array_index;
128 static struct test_array *test_array;
129
130 static struct test_array *test_array_alloc(void)
131 {
132 struct test_array *ret;
133 int index;
134
135 rcu_copy_mutex_lock();
136 index = array_index % ARRAY_SIZE;
137 assert(test_array[index].a == ARRAY_POISON ||
138 test_array[index].a == 0);
139 ret = &test_array[index];
140 array_index++;
141 if (array_index == ARRAY_SIZE)
142 array_index = 0;
143 rcu_copy_mutex_unlock();
144 return ret;
145 }
146
147 static void test_array_free(struct test_array *ptr)
148 {
149 if (!ptr)
150 return;
151 rcu_copy_mutex_lock();
152 ptr->a = ARRAY_POISON;
153 rcu_copy_mutex_unlock();
154 }
155
156 void *thr_reader(void *_count)
157 {
158 unsigned long long *count = _count;
159 struct test_array *local_ptr;
160
161 printf("thread_begin %s, thread id : %lx, tid %lu\n",
162 "reader", pthread_self(), (unsigned long)gettid());
163
164 rcu_register_thread();
165
166 while (!test_go)
167 {
168 }
169
170 for (;;) {
171 _rcu_read_lock();
172 local_ptr = _rcu_dereference(test_rcu_pointer);
173 debug_yield_read();
174 if (local_ptr)
175 assert(local_ptr->a == 8);
176 _rcu_read_unlock();
177 nr_reads++;
178 if (duration_interval >= DURATION_TEST_DELAY_READ - 1)
179 _rcu_quiescent_state();
180 if (!test_duration_read())
181 break;
182 }
183
184 rcu_unregister_thread();
185
186 *count = nr_reads;
187 printf("thread_end %s, thread id : %lx, tid %lu\n",
188 "reader", pthread_self(), (unsigned long)gettid());
189 return ((void*)1);
190
191 }
192
193 void *thr_writer(void *_count)
194 {
195 unsigned long long *count = _count;
196 struct test_array *new, *old;
197
198 printf("thread_begin %s, thread id : %lx, tid %lu\n",
199 "writer", pthread_self(), (unsigned long)gettid());
200
201 while (!test_go)
202 {
203 }
204
205 for (;;) {
206 new = test_array_alloc();
207 rcu_copy_mutex_lock();
208 old = test_rcu_pointer;
209 if (old)
210 assert(old->a == 8);
211 new->a = 8;
212 old = _rcu_publish_content(&test_rcu_pointer, new);
213 rcu_copy_mutex_unlock();
214 /* can be done after unlock */
215 if (old)
216 old->a = 0;
217 test_array_free(old);
218 nr_writes++;
219 if (!test_duration_write())
220 break;
221 if (wdelay)
222 usleep(wdelay);
223 }
224
225 printf("thread_end %s, thread id : %lx, tid %lu\n",
226 "writer", pthread_self(), (unsigned long)gettid());
227 *count = nr_writes;
228 return ((void*)2);
229 }
230
231 void show_usage(int argc, char **argv)
232 {
233 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
234 #ifdef DEBUG_YIELD
235 printf(" [-r] [-w] (yield reader and/or writer)");
236 #endif
237 printf(" [-d delay] (writer period)");
238 printf("\n");
239 }
240
241 int main(int argc, char **argv)
242 {
243 int err;
244 pthread_t *tid_reader, *tid_writer;
245 void *tret;
246 unsigned long long *count_reader, *count_writer;
247 unsigned long long tot_reads = 0, tot_writes = 0;
248 int i;
249
250 if (argc < 4) {
251 show_usage(argc, argv);
252 return -1;
253 }
254
255 err = sscanf(argv[1], "%u", &nr_readers);
256 if (err != 1) {
257 show_usage(argc, argv);
258 return -1;
259 }
260
261 err = sscanf(argv[2], "%u", &nr_writers);
262 if (err != 1) {
263 show_usage(argc, argv);
264 return -1;
265 }
266
267 err = sscanf(argv[3], "%lu", &duration);
268 if (err != 1) {
269 show_usage(argc, argv);
270 return -1;
271 }
272
273 for (i = 4; i < argc; i++) {
274 if (argv[i][0] != '-')
275 continue;
276 switch (argv[i][1]) {
277 #ifdef DEBUG_YIELD
278 case 'r':
279 yield_active |= YIELD_READ;
280 break;
281 case 'w':
282 yield_active |= YIELD_WRITE;
283 break;
284 #endif
285 case 'd':
286 if (argc < i + 2) {
287 show_usage(argc, argv);
288 return -1;
289 }
290 wdelay = atoi(argv[++i]);
291 break;
292 }
293 }
294
295 printf("running test for %lu seconds, %u readers, %u writers.\n",
296 duration, nr_readers, nr_writers);
297 printf("Writer delay : %u us.\n", wdelay);
298 start_time = time(NULL);
299 printf("thread %-6s, thread id : %lx, tid %lu\n",
300 "main", pthread_self(), (unsigned long)gettid());
301
302 test_array = malloc(sizeof(*test_array) * ARRAY_SIZE);
303 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
304 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
305 count_reader = malloc(sizeof(*count_reader) * nr_readers);
306 count_writer = malloc(sizeof(*count_writer) * nr_writers);
307
308 for (i = 0; i < nr_readers; i++) {
309 err = pthread_create(&tid_reader[i], NULL, thr_reader,
310 &count_reader[i]);
311 if (err != 0)
312 exit(1);
313 }
314 for (i = 0; i < nr_writers; i++) {
315 err = pthread_create(&tid_writer[i], NULL, thr_writer,
316 &count_writer[i]);
317 if (err != 0)
318 exit(1);
319 }
320
321 test_go = 1;
322
323 for (i = 0; i < nr_readers; i++) {
324 err = pthread_join(tid_reader[i], &tret);
325 if (err != 0)
326 exit(1);
327 tot_reads += count_reader[i];
328 }
329 for (i = 0; i < nr_writers; i++) {
330 err = pthread_join(tid_writer[i], &tret);
331 if (err != 0)
332 exit(1);
333 tot_writes += count_writer[i];
334 }
335
336 printf("total number of reads : %llu, writes %llu\n", tot_reads,
337 tot_writes);
338 test_array_free(test_rcu_pointer);
339 free(test_array);
340 free(tid_reader);
341 free(tid_writer);
342 free(count_reader);
343 free(count_writer);
344 return 0;
345 }
This page took 0.051587 seconds and 4 git commands to generate.