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