Commit | Line | Data |
---|---|---|
ab7d5fc6 MD |
1 | /* |
2 | * test_ht.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 | #define _GNU_SOURCE | |
24 | #include <stdio.h> | |
25 | #include <pthread.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/wait.h> | |
30 | #include <unistd.h> | |
31 | #include <stdio.h> | |
32 | #include <assert.h> | |
33 | #include <sys/syscall.h> | |
34 | #include <sched.h> | |
35 | #include "../urcu-ht.h" | |
36 | ||
37 | #include "../arch.h" | |
38 | ||
39 | /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */ | |
40 | #define CACHE_LINE_SIZE 4096 | |
41 | ||
42 | /* hardcoded number of CPUs */ | |
43 | #define NR_CPUS 16384 | |
44 | ||
45 | #if defined(_syscall0) | |
46 | _syscall0(pid_t, gettid) | |
47 | #elif defined(__NR_gettid) | |
48 | static inline pid_t gettid(void) | |
49 | { | |
50 | return syscall(__NR_gettid); | |
51 | } | |
52 | #else | |
53 | #warning "use pid as tid" | |
54 | static inline pid_t gettid(void) | |
55 | { | |
56 | return getpid(); | |
57 | } | |
58 | #endif | |
59 | ||
60 | #ifndef DYNAMIC_LINK_TEST | |
61 | #define _LGPL_SOURCE | |
62 | #else | |
63 | #define debug_yield_read() | |
64 | #endif | |
65 | #include "../urcu.h" | |
66 | ||
67 | static struct rcu_ht *test_ht; | |
68 | ||
69 | struct test_array { | |
70 | int a; | |
71 | }; | |
72 | ||
73 | static volatile int test_go, test_stop; | |
74 | ||
75 | static unsigned long wdelay; | |
76 | ||
77 | static struct test_array *test_rcu_pointer; | |
78 | ||
79 | static unsigned long duration; | |
80 | ||
81 | /* read-side C.S. duration, in loops */ | |
82 | static unsigned long rduration; | |
83 | ||
84 | static inline void loop_sleep(unsigned long l) | |
85 | { | |
86 | while(l-- != 0) | |
87 | cpu_relax(); | |
88 | } | |
89 | ||
90 | static int verbose_mode; | |
91 | ||
92 | #define printf_verbose(fmt, args...) \ | |
93 | do { \ | |
94 | if (verbose_mode) \ | |
95 | printf(fmt, args); \ | |
96 | } while (0) | |
97 | ||
98 | static unsigned int cpu_affinities[NR_CPUS]; | |
99 | static unsigned int next_aff = 0; | |
100 | static int use_affinity = 0; | |
101 | ||
102 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
103 | ||
104 | static void set_affinity(void) | |
105 | { | |
106 | cpu_set_t mask; | |
107 | int cpu; | |
108 | int ret; | |
109 | ||
110 | if (!use_affinity) | |
111 | return; | |
112 | ||
113 | ret = pthread_mutex_lock(&affinity_mutex); | |
114 | if (ret) { | |
115 | perror("Error in pthread mutex lock"); | |
116 | exit(-1); | |
117 | } | |
118 | cpu = cpu_affinities[next_aff++]; | |
119 | ret = pthread_mutex_unlock(&affinity_mutex); | |
120 | if (ret) { | |
121 | perror("Error in pthread mutex unlock"); | |
122 | exit(-1); | |
123 | } | |
124 | CPU_ZERO(&mask); | |
125 | CPU_SET(cpu, &mask); | |
126 | sched_setaffinity(0, sizeof(mask), &mask); | |
127 | } | |
128 | ||
129 | /* | |
130 | * returns 0 if test should end. | |
131 | */ | |
132 | static int test_duration_write(void) | |
133 | { | |
134 | return !test_stop; | |
135 | } | |
136 | ||
137 | static int test_duration_read(void) | |
138 | { | |
139 | return !test_stop; | |
140 | } | |
141 | ||
142 | static unsigned long long __thread nr_writes; | |
143 | static unsigned long long __thread nr_reads; | |
144 | ||
145 | static unsigned int nr_readers; | |
146 | static unsigned int nr_writers; | |
147 | ||
148 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
149 | ||
150 | void rcu_copy_mutex_lock(void) | |
151 | { | |
152 | int ret; | |
153 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
154 | if (ret) { | |
155 | perror("Error in pthread mutex lock"); | |
156 | exit(-1); | |
157 | } | |
158 | } | |
159 | ||
160 | void rcu_copy_mutex_unlock(void) | |
161 | { | |
162 | int ret; | |
163 | ||
164 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
165 | if (ret) { | |
166 | perror("Error in pthread mutex unlock"); | |
167 | exit(-1); | |
168 | } | |
169 | } | |
170 | ||
171 | /* | |
172 | * malloc/free are reusing memory areas too quickly, which does not let us | |
173 | * test races appropriately. Use a large circular array for allocations. | |
174 | * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail. | |
175 | */ | |
176 | #define ARRAY_SIZE (1048576 * nr_writers) | |
177 | #define ARRAY_POISON 0xDEADBEEF | |
178 | static int array_index; | |
179 | static struct test_array *test_array; | |
180 | ||
181 | static struct test_array *test_array_alloc(void) | |
182 | { | |
183 | struct test_array *ret; | |
184 | int index; | |
185 | ||
186 | rcu_copy_mutex_lock(); | |
187 | index = array_index % ARRAY_SIZE; | |
188 | assert(test_array[index].a == ARRAY_POISON || | |
189 | test_array[index].a == 0); | |
190 | ret = &test_array[index]; | |
191 | array_index++; | |
192 | if (array_index == ARRAY_SIZE) | |
193 | array_index = 0; | |
194 | rcu_copy_mutex_unlock(); | |
195 | return ret; | |
196 | } | |
197 | ||
198 | static void test_array_free(struct test_array *ptr) | |
199 | { | |
200 | if (!ptr) | |
201 | return; | |
202 | rcu_copy_mutex_lock(); | |
203 | ptr->a = ARRAY_POISON; | |
204 | rcu_copy_mutex_unlock(); | |
205 | } | |
206 | ||
207 | void *thr_reader(void *_count) | |
208 | { | |
209 | unsigned long long *count = _count; | |
210 | struct test_array *local_ptr; | |
211 | ||
212 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
213 | "reader", pthread_self(), (unsigned long)gettid()); | |
214 | ||
215 | set_affinity(); | |
216 | ||
217 | rcu_register_thread(); | |
218 | ||
219 | while (!test_go) | |
220 | { | |
221 | } | |
222 | smp_mb(); | |
223 | ||
224 | for (;;) { | |
225 | rcu_read_lock(); | |
226 | local_ptr = rcu_dereference(test_rcu_pointer); | |
227 | debug_yield_read(); | |
228 | if (local_ptr) | |
229 | assert(local_ptr->a == 8); | |
230 | if (unlikely(rduration)) | |
231 | loop_sleep(rduration); | |
232 | rcu_read_unlock(); | |
233 | nr_reads++; | |
234 | if (unlikely(!test_duration_read())) | |
235 | break; | |
236 | } | |
237 | ||
238 | rcu_unregister_thread(); | |
239 | ||
240 | *count = nr_reads; | |
241 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
242 | "reader", pthread_self(), (unsigned long)gettid()); | |
243 | return ((void*)1); | |
244 | ||
245 | } | |
246 | ||
247 | void *thr_writer(void *_count) | |
248 | { | |
249 | unsigned long long *count = _count; | |
250 | struct test_array *new, *old; | |
251 | ||
252 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
253 | "writer", pthread_self(), (unsigned long)gettid()); | |
254 | ||
255 | set_affinity(); | |
256 | ||
257 | while (!test_go) | |
258 | { | |
259 | } | |
260 | smp_mb(); | |
261 | ||
262 | for (;;) { | |
263 | new = test_array_alloc(); | |
264 | new->a = 8; | |
265 | old = rcu_publish_content(&test_rcu_pointer, new); | |
266 | if (old) | |
267 | old->a = 0; | |
268 | test_array_free(old); | |
269 | nr_writes++; | |
270 | if (unlikely(!test_duration_write())) | |
271 | break; | |
272 | if (unlikely(wdelay)) | |
273 | loop_sleep(wdelay); | |
274 | } | |
275 | ||
276 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
277 | "writer", pthread_self(), (unsigned long)gettid()); | |
278 | *count = nr_writes; | |
279 | return ((void*)2); | |
280 | } | |
281 | ||
282 | void show_usage(int argc, char **argv) | |
283 | { | |
284 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
285 | #ifdef DEBUG_YIELD | |
286 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
287 | #endif | |
288 | printf(" [-d delay] (writer period (us))"); | |
289 | printf(" [-c duration] (reader C.S. duration (in loops))"); | |
290 | printf(" [-v] (verbose output)"); | |
291 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); | |
292 | printf("\n"); | |
293 | } | |
294 | ||
295 | int main(int argc, char **argv) | |
296 | { | |
297 | int err; | |
298 | pthread_t *tid_reader, *tid_writer; | |
299 | void *tret; | |
300 | unsigned long long *count_reader, *count_writer; | |
301 | unsigned long long tot_reads = 0, tot_writes = 0; | |
302 | int i, a; | |
303 | ||
304 | if (argc < 4) { | |
305 | show_usage(argc, argv); | |
306 | return -1; | |
307 | } | |
308 | ||
309 | err = sscanf(argv[1], "%u", &nr_readers); | |
310 | if (err != 1) { | |
311 | show_usage(argc, argv); | |
312 | return -1; | |
313 | } | |
314 | ||
315 | err = sscanf(argv[2], "%u", &nr_writers); | |
316 | if (err != 1) { | |
317 | show_usage(argc, argv); | |
318 | return -1; | |
319 | } | |
320 | ||
321 | err = sscanf(argv[3], "%lu", &duration); | |
322 | if (err != 1) { | |
323 | show_usage(argc, argv); | |
324 | return -1; | |
325 | } | |
326 | ||
327 | for (i = 4; i < argc; i++) { | |
328 | if (argv[i][0] != '-') | |
329 | continue; | |
330 | switch (argv[i][1]) { | |
331 | #ifdef DEBUG_YIELD | |
332 | case 'r': | |
333 | yield_active |= YIELD_READ; | |
334 | break; | |
335 | case 'w': | |
336 | yield_active |= YIELD_WRITE; | |
337 | break; | |
338 | #endif | |
339 | case 'a': | |
340 | if (argc < i + 2) { | |
341 | show_usage(argc, argv); | |
342 | return -1; | |
343 | } | |
344 | a = atoi(argv[++i]); | |
345 | cpu_affinities[next_aff++] = a; | |
346 | use_affinity = 1; | |
347 | printf_verbose("Adding CPU %d affinity\n", a); | |
348 | break; | |
349 | case 'c': | |
350 | if (argc < i + 2) { | |
351 | show_usage(argc, argv); | |
352 | return -1; | |
353 | } | |
354 | rduration = atol(argv[++i]); | |
355 | break; | |
356 | case 'd': | |
357 | if (argc < i + 2) { | |
358 | show_usage(argc, argv); | |
359 | return -1; | |
360 | } | |
361 | wdelay = atol(argv[++i]); | |
362 | break; | |
363 | case 'v': | |
364 | verbose_mode = 1; | |
365 | break; | |
366 | } | |
367 | } | |
368 | ||
369 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
370 | duration, nr_readers, nr_writers); | |
371 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
372 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
373 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
374 | "main", pthread_self(), (unsigned long)gettid()); | |
375 | ||
376 | test_array = malloc(sizeof(*test_array) * ARRAY_SIZE); | |
377 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
378 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
379 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
380 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
381 | test_ht = ht_new(stupid_hash, free); | |
382 | next_aff = 0; | |
383 | ||
384 | for (i = 0; i < nr_readers; i++) { | |
385 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
386 | &count_reader[i]); | |
387 | if (err != 0) | |
388 | exit(1); | |
389 | } | |
390 | for (i = 0; i < nr_writers; i++) { | |
391 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
392 | &count_writer[i]); | |
393 | if (err != 0) | |
394 | exit(1); | |
395 | } | |
396 | ||
397 | smp_mb(); | |
398 | ||
399 | test_go = 1; | |
400 | ||
401 | sleep(duration); | |
402 | ||
403 | test_stop = 1; | |
404 | ||
405 | for (i = 0; i < nr_readers; i++) { | |
406 | err = pthread_join(tid_reader[i], &tret); | |
407 | if (err != 0) | |
408 | exit(1); | |
409 | tot_reads += count_reader[i]; | |
410 | } | |
411 | for (i = 0; i < nr_writers; i++) { | |
412 | err = pthread_join(tid_writer[i], &tret); | |
413 | if (err != 0) | |
414 | exit(1); | |
415 | tot_writes += count_writer[i]; | |
416 | } | |
417 | ht_destroy(test_ht); | |
418 | ||
419 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, | |
420 | tot_writes); | |
421 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " | |
422 | "nr_writers %3u " | |
423 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", | |
424 | argv[0], duration, nr_readers, rduration, | |
425 | nr_writers, wdelay, tot_reads, tot_writes, | |
426 | tot_reads + tot_writes); | |
427 | test_array_free(test_rcu_pointer); | |
428 | free(test_array); | |
429 | free(tid_reader); | |
430 | free(tid_writer); | |
431 | free(count_reader); | |
432 | free(count_writer); | |
433 | return 0; | |
434 | } |