4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
27 #include <sys/types.h>
32 #include <sys/syscall.h>
34 #if defined(_syscall0)
35 _syscall0(pid_t
, gettid
)
36 #elif defined(__NR_gettid)
37 static inline pid_t
gettid(void)
39 return syscall(__NR_gettid
);
42 #warning "use pid as tid"
43 static inline pid_t
gettid(void)
49 #ifndef DYNAMIC_LINK_TEST
52 #define debug_yield_read()
60 static int no_writer_delay
;
62 static struct test_array
*test_rcu_pointer
;
64 static unsigned long duration
;
65 static time_t start_time
;
66 static unsigned long __thread duration_interval
;
67 #define DURATION_TEST_DELAY_WRITE 4
68 #define DURATION_TEST_DELAY_READ 100
71 * returns 0 if test should end.
73 static int test_duration_write(void)
75 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
76 duration_interval
= 0;
77 if (time(NULL
) - start_time
>= duration
)
83 static int test_duration_read(void)
85 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
86 duration_interval
= 0;
87 if (time(NULL
) - start_time
>= duration
)
96 static unsigned long long __thread nr_writes
;
97 static unsigned long long __thread nr_reads
;
99 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
101 void rcu_copy_mutex_lock(void)
104 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
106 perror("Error in pthread mutex lock");
111 void rcu_copy_mutex_unlock(void)
115 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
117 perror("Error in pthread mutex unlock");
123 * malloc/free are reusing memory areas too quickly, which does not let us
124 * test races appropriately. Use a large circular array for allocations.
125 * ARRAY_SIZE is larger than NR_WRITE, which insures we never run over our tail.
127 #define ARRAY_SIZE (1048576 * NR_WRITE)
128 #define ARRAY_POISON 0xDEADBEEF
129 static int array_index
;
130 static struct test_array test_array
[ARRAY_SIZE
];
132 static struct test_array
*test_array_alloc(void)
134 struct test_array
*ret
;
137 rcu_copy_mutex_lock();
138 index
= array_index
% ARRAY_SIZE
;
139 assert(test_array
[index
].a
== ARRAY_POISON
||
140 test_array
[index
].a
== 0);
141 ret
= &test_array
[index
];
143 if (array_index
== ARRAY_SIZE
)
145 rcu_copy_mutex_unlock();
149 static void test_array_free(struct test_array
*ptr
)
153 rcu_copy_mutex_lock();
154 ptr
->a
= ARRAY_POISON
;
155 rcu_copy_mutex_unlock();
158 void *thr_reader(void *_count
)
160 unsigned long long *count
= _count
;
161 struct test_array
*local_ptr
;
163 printf("thread_begin %s, thread id : %lx, tid %lu\n",
164 "reader", pthread_self(), (unsigned long)gettid());
166 rcu_register_thread();
170 local_ptr
= rcu_dereference(test_rcu_pointer
);
173 assert(local_ptr
->a
== 8);
176 if (!test_duration_read())
180 rcu_unregister_thread();
183 printf("thread_end %s, thread id : %lx, tid %lu\n",
184 "reader", pthread_self(), (unsigned long)gettid());
189 void *thr_writer(void *_count
)
191 unsigned long long *count
= _count
;
192 struct test_array
*new, *old
;
194 printf("thread_begin %s, thread id : %lx, tid %lu\n",
195 "writer", pthread_self(), (unsigned long)gettid());
198 new = test_array_alloc();
199 rcu_copy_mutex_lock();
200 old
= test_rcu_pointer
;
204 old
= rcu_publish_content(&test_rcu_pointer
, new);
205 rcu_copy_mutex_unlock();
206 /* can be done after unlock */
209 test_array_free(old
);
211 if (!test_duration_write())
213 if (!no_writer_delay
)
217 printf("thread_end %s, thread id : %lx, tid %lu\n",
218 "writer", pthread_self(), (unsigned long)gettid());
223 void show_usage(int argc
, char **argv
)
225 printf("Usage : %s duration (s)", argv
[0]);
227 printf(" [-r] [-w] (yield reader and/or writer)");
229 printf(" [-n] (disable writer delay)");
233 int main(int argc
, char **argv
)
236 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
238 unsigned long long count_reader
[NR_READ
], count_writer
[NR_WRITE
];
239 unsigned long long tot_reads
= 0, tot_writes
= 0;
243 show_usage(argc
, argv
);
247 err
= sscanf(argv
[1], "%lu", &duration
);
249 show_usage(argc
, argv
);
253 for (i
= 2; i
< argc
; i
++) {
254 if (argv
[i
][0] != '-')
256 switch (argv
[i
][1]) {
259 yield_active
|= YIELD_READ
;
262 yield_active
|= YIELD_WRITE
;
271 printf("running test for %lu seconds.\n", duration
);
272 start_time
= time(NULL
);
273 printf("thread %-6s, thread id : %lx, tid %lu\n",
274 "main", pthread_self(), (unsigned long)gettid());
276 for (i
= 0; i
< NR_READ
; i
++) {
277 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
282 for (i
= 0; i
< NR_WRITE
; i
++) {
283 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
289 for (i
= 0; i
< NR_READ
; i
++) {
290 err
= pthread_join(tid_reader
[i
], &tret
);
293 tot_reads
+= count_reader
[i
];
295 for (i
= 0; i
< NR_WRITE
; i
++) {
296 err
= pthread_join(tid_writer
[i
], &tret
);
299 tot_writes
+= count_writer
[i
];
302 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
304 test_array_free(test_rcu_pointer
);