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 100
53 * returns 0 if test should end.
55 static int test_duration(void)
57 if (duration_interval
++ >= DURATION_TEST_DELAY
) {
58 duration_interval
= 0;
59 if (time(NULL
) - start_time
>= duration
)
68 static unsigned long long __thread nr_writes
;
69 static unsigned long long __thread nr_reads
;
71 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
73 void rcu_copy_mutex_lock(void)
76 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
78 perror("Error in pthread mutex lock");
83 void rcu_copy_mutex_unlock(void)
87 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
89 perror("Error in pthread mutex unlock");
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.
99 #define ARRAY_SIZE (1048576 * NR_WRITE)
100 #define ARRAY_POISON 0xDEADBEEF
101 static int array_index
;
102 static struct test_array test_array
[ARRAY_SIZE
];
104 static struct test_array
*test_array_alloc(void)
106 struct test_array
*ret
;
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
];
115 if (array_index
== ARRAY_SIZE
)
117 rcu_copy_mutex_unlock();
121 static void test_array_free(struct test_array
*ptr
)
125 rcu_copy_mutex_lock();
126 ptr
->a
= ARRAY_POISON
;
127 rcu_copy_mutex_unlock();
130 void *thr_reader(void *_count
)
132 unsigned long long *count
= _count
;
133 struct test_array
*local_ptr
;
135 printf("thread_begin %s, thread id : %lx, tid %lu\n",
136 "reader", pthread_self(), (unsigned long)gettid());
138 urcu_register_thread();
142 local_ptr
= rcu_dereference(test_rcu_pointer
);
145 assert(local_ptr
->a
== 8);
148 if (!test_duration())
152 urcu_unregister_thread();
155 printf("thread_end %s, thread id : %lx, tid %lu\n",
156 "reader", pthread_self(), (unsigned long)gettid());
161 void *thr_writer(void *_count
)
163 unsigned long long *count
= _count
;
164 struct test_array
*new, *old
;
166 printf("thread_begin %s, thread id : %lx, tid %lu\n",
167 "writer", pthread_self(), (unsigned long)gettid());
170 new = test_array_alloc();
171 rcu_copy_mutex_lock();
172 old
= test_rcu_pointer
;
176 old
= urcu_publish_content(&test_rcu_pointer
, new);
177 rcu_copy_mutex_unlock();
178 /* can be done after unlock */
181 test_array_free(old
);
183 if (!test_duration())
185 if (!no_writer_delay
)
189 printf("thread_end %s, thread id : %lx, tid %lu\n",
190 "writer", pthread_self(), (unsigned long)gettid());
195 void show_usage(int argc
, char **argv
)
197 printf("Usage : %s duration (s)", argv
[0]);
199 printf(" [-r] [-w] (yield reader and/or writer)");
201 printf(" [-n] (disable writer delay)");
205 int main(int argc
, char **argv
)
208 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
210 unsigned long long count_reader
[NR_READ
], count_writer
[NR_WRITE
];
211 unsigned long long tot_reads
= 0, tot_writes
= 0;
215 show_usage(argc
, argv
);
219 err
= sscanf(argv
[1], "%lu", &duration
);
221 show_usage(argc
, argv
);
225 for (i
= 2; i
< argc
; i
++) {
226 if (argv
[i
][0] != '-')
228 switch (argv
[i
][1]) {
231 yield_active
|= YIELD_READ
;
234 yield_active
|= YIELD_WRITE
;
243 printf("running test for %lu seconds.\n", duration
);
244 start_time
= time(NULL
);
245 printf("thread %-6s, thread id : %lx, tid %lu\n",
246 "main", pthread_self(), (unsigned long)gettid());
248 for (i
= 0; i
< NR_READ
; i
++) {
249 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
254 for (i
= 0; i
< NR_WRITE
; i
++) {
255 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
261 for (i
= 0; i
< NR_READ
; i
++) {
262 err
= pthread_join(tid_reader
[i
], &tret
);
265 tot_reads
+= count_reader
[i
];
267 for (i
= 0; i
< NR_WRITE
; i
++) {
268 err
= pthread_join(tid_writer
[i
], &tret
);
271 tot_writes
+= count_writer
[i
];
273 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
275 test_array_free(test_rcu_pointer
);