Use more standard flags
[urcu.git] / test_urcu.c
CommitLineData
b257a10b
MD
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
ac260fd9 11#include <stdio.h>
f69f195a
MD
12#include <pthread.h>
13#include <stdlib.h>
41718ff9 14#include <string.h>
f69f195a
MD
15#include <sys/types.h>
16#include <sys/wait.h>
17#include <unistd.h>
18#include <stdio.h>
41718ff9 19#include <assert.h>
87bd15cd
BW
20#include <sys/syscall.h>
21
22#if defined(_syscall0)
23_syscall0(pid_t, gettid)
24#elif defined(__NR_gettid)
25static inline pid_t gettid(void)
26{
27 return syscall(__NR_gettid);
28}
29#else
30#warning "use pid as tid"
31static inline pid_t gettid(void)
32{
33 return getpid();
34}
35#endif
36
ac260fd9
MD
37#include "urcu.h"
38
41718ff9
MD
39struct test_array {
40 int a;
41718ff9
MD
41};
42
bb488185
MD
43static int no_writer_delay;
44
41718ff9
MD
45static struct test_array *test_rcu_pointer;
46
cf380c2f
MD
47static unsigned long duration;
48static time_t start_time;
49static unsigned long __thread duration_interval;
50#define DURATION_TEST_DELAY 100
51
52/*
53 * returns 0 if test should end.
54 */
55static 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
b1b5ce8f 65#define NR_READ 10
8c8eed97 66#define NR_WRITE 9
f69f195a 67
1eec319e
MD
68static unsigned long long __thread nr_writes;
69static unsigned long long __thread nr_reads;
70
c265818b
MD
71pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
72
73void 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
83void 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}
f69f195a 93
bb488185
MD
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
101static int array_index;
102static struct test_array test_array[ARRAY_SIZE];
103
104static 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
121static 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
1eec319e 130void *thr_reader(void *_count)
f69f195a 131{
1eec319e 132 unsigned long long *count = _count;
41718ff9
MD
133 struct test_array *local_ptr;
134
cf380c2f 135 printf("thread_begin %s, thread id : %lx, tid %lu\n",
87bd15cd 136 "reader", pthread_self(), (unsigned long)gettid());
f69f195a
MD
137
138 urcu_register_thread();
139
cf380c2f 140 for (;;) {
1430ee0b 141 rcu_read_lock();
cf380c2f 142 local_ptr = rcu_dereference(test_rcu_pointer);
bb488185 143 debug_yield_read();
cf380c2f
MD
144 if (local_ptr)
145 assert(local_ptr->a == 8);
1430ee0b 146 rcu_read_unlock();
1eec319e 147 nr_reads++;
cf380c2f
MD
148 if (!test_duration())
149 break;
41718ff9 150 }
f69f195a
MD
151
152 urcu_unregister_thread();
41718ff9 153
1eec319e 154 *count = nr_reads;
cf380c2f
MD
155 printf("thread_end %s, thread id : %lx, tid %lu\n",
156 "reader", pthread_self(), (unsigned long)gettid());
f69f195a
MD
157 return ((void*)1);
158
159}
160
1eec319e 161void *thr_writer(void *_count)
f69f195a 162{
1eec319e 163 unsigned long long *count = _count;
41718ff9 164 struct test_array *new, *old;
f69f195a 165
cf380c2f 166 printf("thread_begin %s, thread id : %lx, tid %lu\n",
87bd15cd 167 "writer", pthread_self(), (unsigned long)gettid());
f69f195a 168
cf380c2f 169 for (;;) {
bb488185 170 new = test_array_alloc();
c265818b 171 rcu_copy_mutex_lock();
41718ff9 172 old = test_rcu_pointer;
cf380c2f 173 if (old)
41718ff9 174 assert(old->a == 8);
ad6ce6ae 175 new->a = 8;
f4a486ac 176 old = urcu_publish_content(&test_rcu_pointer, new);
c265818b 177 rcu_copy_mutex_unlock();
41718ff9 178 /* can be done after unlock */
cf380c2f 179 if (old)
8c8eed97 180 old->a = 0;
bb488185 181 test_array_free(old);
1eec319e 182 nr_writes++;
cf380c2f
MD
183 if (!test_duration())
184 break;
bb488185
MD
185 if (!no_writer_delay)
186 usleep(1);
f69f195a
MD
187 }
188
cf380c2f
MD
189 printf("thread_end %s, thread id : %lx, tid %lu\n",
190 "writer", pthread_self(), (unsigned long)gettid());
1eec319e 191 *count = nr_writes;
f69f195a
MD
192 return ((void*)2);
193}
ac260fd9 194
1430ee0b
MD
195void 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
bb488185 201 printf(" [-n] (disable writer delay)");
1430ee0b
MD
202 printf("\n");
203}
204
cf380c2f 205int main(int argc, char **argv)
ac260fd9 206{
f69f195a
MD
207 int err;
208 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
209 void *tret;
1eec319e
MD
210 unsigned long long count_reader[NR_READ], count_writer[NR_WRITE];
211 unsigned long long tot_reads = 0, tot_writes = 0;
f69f195a
MD
212 int i;
213
cf380c2f 214 if (argc < 2) {
1430ee0b 215 show_usage(argc, argv);
cf380c2f
MD
216 return -1;
217 }
218
219 err = sscanf(argv[1], "%lu", &duration);
220 if (err != 1) {
1430ee0b 221 show_usage(argc, argv);
cf380c2f
MD
222 return -1;
223 }
224
cf380c2f
MD
225 for (i = 2; i < argc; i++) {
226 if (argv[i][0] != '-')
227 continue;
228 switch (argv[i][1]) {
bb488185 229#ifdef DEBUG_YIELD
cf380c2f
MD
230 case 'r':
231 yield_active |= YIELD_READ;
232 break;
233 case 'w':
234 yield_active |= YIELD_WRITE;
235 break;
bb488185
MD
236#endif
237 case 'n':
238 no_writer_delay = 1;
239 break;
cf380c2f
MD
240 }
241 }
cf380c2f
MD
242
243 printf("running test for %lu seconds.\n", duration);
244 start_time = time(NULL);
87bd15cd
BW
245 printf("thread %-6s, thread id : %lx, tid %lu\n",
246 "main", pthread_self(), (unsigned long)gettid());
247
f69f195a 248 for (i = 0; i < NR_READ; i++) {
1eec319e
MD
249 err = pthread_create(&tid_reader[i], NULL, thr_reader,
250 &count_reader[i]);
f69f195a
MD
251 if (err != 0)
252 exit(1);
253 }
254 for (i = 0; i < NR_WRITE; i++) {
1eec319e
MD
255 err = pthread_create(&tid_writer[i], NULL, thr_writer,
256 &count_writer[i]);
f69f195a
MD
257 if (err != 0)
258 exit(1);
259 }
260
20bf310a 261 for (i = 0; i < NR_READ; i++) {
f69f195a
MD
262 err = pthread_join(tid_reader[i], &tret);
263 if (err != 0)
264 exit(1);
1eec319e 265 tot_reads += count_reader[i];
f69f195a
MD
266 }
267 for (i = 0; i < NR_WRITE; i++) {
268 err = pthread_join(tid_writer[i], &tret);
269 if (err != 0)
270 exit(1);
1eec319e 271 tot_writes += count_writer[i];
f69f195a 272 }
1eec319e
MD
273 printf("total number of reads : %llu, writes %llu\n", tot_reads,
274 tot_writes);
bb488185 275 test_array_free(test_rcu_pointer);
ac260fd9 276
f69f195a 277 return 0;
ac260fd9 278}
This page took 0.035381 seconds and 4 git commands to generate.