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)
37 #ifndef DYNAMIC_LINK_TEST
40 #define debug_yield_read()
48 static int no_writer_delay
;
50 static struct test_array
*test_rcu_pointer
;
52 static unsigned long duration
;
53 static time_t start_time
;
54 static unsigned long __thread duration_interval
;
55 #define DURATION_TEST_DELAY_WRITE 4
56 #define DURATION_TEST_DELAY_READ 100
59 * returns 0 if test should end.
61 static int test_duration_write(void)
63 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
64 duration_interval
= 0;
65 if (time(NULL
) - start_time
>= duration
)
71 static int test_duration_read(void)
73 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
74 duration_interval
= 0;
75 if (time(NULL
) - start_time
>= duration
)
84 static unsigned long long __thread nr_writes
;
85 static unsigned long long __thread nr_reads
;
87 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
89 void rcu_copy_mutex_lock(void)
92 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
94 perror("Error in pthread mutex lock");
99 void rcu_copy_mutex_unlock(void)
103 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
105 perror("Error in pthread mutex unlock");
111 * malloc/free are reusing memory areas too quickly, which does not let us
112 * test races appropriately. Use a large circular array for allocations.
113 * ARRAY_SIZE is larger than NR_WRITE, which insures we never run over our tail.
115 #define ARRAY_SIZE (1048576 * NR_WRITE)
116 #define ARRAY_POISON 0xDEADBEEF
117 static int array_index
;
118 static struct test_array test_array
[ARRAY_SIZE
];
120 static struct test_array
*test_array_alloc(void)
122 struct test_array
*ret
;
125 rcu_copy_mutex_lock();
126 index
= array_index
% ARRAY_SIZE
;
127 assert(test_array
[index
].a
== ARRAY_POISON
||
128 test_array
[index
].a
== 0);
129 ret
= &test_array
[index
];
131 if (array_index
== ARRAY_SIZE
)
133 rcu_copy_mutex_unlock();
137 static void test_array_free(struct test_array
*ptr
)
141 rcu_copy_mutex_lock();
142 ptr
->a
= ARRAY_POISON
;
143 rcu_copy_mutex_unlock();
146 void *thr_reader(void *_count
)
148 unsigned long long *count
= _count
;
149 struct test_array
*local_ptr
;
151 printf("thread_begin %s, thread id : %lx, tid %lu\n",
152 "reader", pthread_self(), (unsigned long)gettid());
154 rcu_register_thread();
158 local_ptr
= rcu_dereference(test_rcu_pointer
);
161 assert(local_ptr
->a
== 8);
164 if (!test_duration_read())
168 rcu_unregister_thread();
171 printf("thread_end %s, thread id : %lx, tid %lu\n",
172 "reader", pthread_self(), (unsigned long)gettid());
177 void *thr_writer(void *_count
)
179 unsigned long long *count
= _count
;
180 struct test_array
*new, *old
;
182 printf("thread_begin %s, thread id : %lx, tid %lu\n",
183 "writer", pthread_self(), (unsigned long)gettid());
186 new = test_array_alloc();
187 rcu_copy_mutex_lock();
188 old
= test_rcu_pointer
;
192 old
= rcu_publish_content(&test_rcu_pointer
, new);
193 rcu_copy_mutex_unlock();
194 /* can be done after unlock */
197 test_array_free(old
);
199 if (!test_duration_write())
201 if (!no_writer_delay
)
205 printf("thread_end %s, thread id : %lx, tid %lu\n",
206 "writer", pthread_self(), (unsigned long)gettid());
211 void show_usage(int argc
, char **argv
)
213 printf("Usage : %s duration (s)", argv
[0]);
215 printf(" [-r] [-w] (yield reader and/or writer)");
217 printf(" [-n] (disable writer delay)");
221 int main(int argc
, char **argv
)
224 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
226 unsigned long long count_reader
[NR_READ
], count_writer
[NR_WRITE
];
227 unsigned long long tot_reads
= 0, tot_writes
= 0;
231 show_usage(argc
, argv
);
235 err
= sscanf(argv
[1], "%lu", &duration
);
237 show_usage(argc
, argv
);
241 for (i
= 2; i
< argc
; i
++) {
242 if (argv
[i
][0] != '-')
244 switch (argv
[i
][1]) {
247 yield_active
|= YIELD_READ
;
250 yield_active
|= YIELD_WRITE
;
259 printf("running test for %lu seconds.\n", duration
);
260 start_time
= time(NULL
);
261 printf("thread %-6s, thread id : %lx, tid %lu\n",
262 "main", pthread_self(), (unsigned long)gettid());
264 for (i
= 0; i
< NR_READ
; i
++) {
265 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
270 for (i
= 0; i
< NR_WRITE
; i
++) {
271 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
277 for (i
= 0; i
< NR_READ
; i
++) {
278 err
= pthread_join(tid_reader
[i
], &tret
);
281 tot_reads
+= count_reader
[i
];
283 for (i
= 0; i
< NR_WRITE
; i
++) {
284 err
= pthread_join(tid_writer
[i
], &tret
);
287 tot_writes
+= count_writer
[i
];
290 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
292 test_array_free(test_rcu_pointer
);