4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
15 #include <sys/types.h>
20 #include <sys/syscall.h>
22 #if defined(_syscall0)
23 _syscall0(pid_t
, gettid
)
24 #elif defined(__NR_gettid)
25 static inline pid_t
gettid(void)
27 return syscall(__NR_gettid
);
30 #warning "use pid as tid"
31 static inline pid_t
gettid(void)
43 static int no_writer_delay
;
45 static struct test_array
*test_rcu_pointer
;
47 static unsigned long duration
;
48 static time_t start_time
;
49 static unsigned long __thread duration_interval
;
50 #define DURATION_TEST_DELAY_WRITE 4
51 #define DURATION_TEST_DELAY_READ 100
54 * returns 0 if test should end.
56 static int test_duration_write(void)
58 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
59 duration_interval
= 0;
60 if (time(NULL
) - start_time
>= duration
)
66 static int test_duration_read(void)
68 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
69 duration_interval
= 0;
70 if (time(NULL
) - start_time
>= duration
)
79 static unsigned long long __thread nr_writes
;
80 static unsigned long long __thread nr_reads
;
82 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
84 void rcu_copy_mutex_lock(void)
87 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
89 perror("Error in pthread mutex lock");
94 void rcu_copy_mutex_unlock(void)
98 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
100 perror("Error in pthread mutex unlock");
106 * malloc/free are reusing memory areas too quickly, which does not let us
107 * test races appropriately. Use a large circular array for allocations.
108 * ARRAY_SIZE is larger than NR_WRITE, which insures we never run over our tail.
110 #define ARRAY_SIZE (1048576 * NR_WRITE)
111 #define ARRAY_POISON 0xDEADBEEF
112 static int array_index
;
113 static struct test_array test_array
[ARRAY_SIZE
];
115 static struct test_array
*test_array_alloc(void)
117 struct test_array
*ret
;
120 rcu_copy_mutex_lock();
121 index
= array_index
% ARRAY_SIZE
;
122 assert(test_array
[index
].a
== ARRAY_POISON
||
123 test_array
[index
].a
== 0);
124 ret
= &test_array
[index
];
126 if (array_index
== ARRAY_SIZE
)
128 rcu_copy_mutex_unlock();
132 static void test_array_free(struct test_array
*ptr
)
136 rcu_copy_mutex_lock();
137 ptr
->a
= ARRAY_POISON
;
138 rcu_copy_mutex_unlock();
141 void *thr_reader(void *_count
)
143 unsigned long long *count
= _count
;
144 struct test_array
*local_ptr
;
146 printf("thread_begin %s, thread id : %lx, tid %lu\n",
147 "reader", pthread_self(), (unsigned long)gettid());
149 urcu_register_thread();
153 local_ptr
= rcu_dereference(test_rcu_pointer
);
156 assert(local_ptr
->a
== 8);
159 if (!test_duration_read())
163 urcu_unregister_thread();
166 printf("thread_end %s, thread id : %lx, tid %lu\n",
167 "reader", pthread_self(), (unsigned long)gettid());
172 void *thr_writer(void *_count
)
174 unsigned long long *count
= _count
;
175 struct test_array
*new, *old
;
177 printf("thread_begin %s, thread id : %lx, tid %lu\n",
178 "writer", pthread_self(), (unsigned long)gettid());
181 new = test_array_alloc();
182 rcu_copy_mutex_lock();
183 old
= test_rcu_pointer
;
187 old
= urcu_publish_content(&test_rcu_pointer
, new);
188 rcu_copy_mutex_unlock();
189 /* can be done after unlock */
192 test_array_free(old
);
194 if (!test_duration_write())
196 if (!no_writer_delay
)
200 printf("thread_end %s, thread id : %lx, tid %lu\n",
201 "writer", pthread_self(), (unsigned long)gettid());
206 void show_usage(int argc
, char **argv
)
208 printf("Usage : %s duration (s)", argv
[0]);
210 printf(" [-r] [-w] (yield reader and/or writer)");
212 printf(" [-n] (disable writer delay)");
216 int main(int argc
, char **argv
)
219 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
221 unsigned long long count_reader
[NR_READ
], count_writer
[NR_WRITE
];
222 unsigned long long tot_reads
= 0, tot_writes
= 0;
226 show_usage(argc
, argv
);
230 err
= sscanf(argv
[1], "%lu", &duration
);
232 show_usage(argc
, argv
);
236 for (i
= 2; i
< argc
; i
++) {
237 if (argv
[i
][0] != '-')
239 switch (argv
[i
][1]) {
242 yield_active
|= YIELD_READ
;
245 yield_active
|= YIELD_WRITE
;
254 printf("running test for %lu seconds.\n", duration
);
255 start_time
= time(NULL
);
256 printf("thread %-6s, thread id : %lx, tid %lu\n",
257 "main", pthread_self(), (unsigned long)gettid());
259 for (i
= 0; i
< NR_READ
; i
++) {
260 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
265 for (i
= 0; i
< NR_WRITE
; i
++) {
266 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
272 for (i
= 0; i
< NR_READ
; i
++) {
273 err
= pthread_join(tid_reader
[i
], &tret
);
276 tot_reads
+= count_reader
[i
];
278 for (i
= 0; i
< NR_WRITE
; i
++) {
279 err
= pthread_join(tid_writer
[i
], &tret
);
282 tot_writes
+= count_writer
[i
];
285 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
287 test_array_free(test_rcu_pointer
);