Commit | Line | Data |
---|---|---|
0ee8bb0a 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 | * 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. | |
12 | * | |
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. | |
17 | * | |
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. | |
21 | */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <pthread.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/wait.h> | |
29 | #include <unistd.h> | |
30 | #include <stdio.h> | |
31 | #include <assert.h> | |
32 | #include <sys/syscall.h> | |
33 | ||
34 | #include "arch.h" | |
35 | ||
36 | #if defined(_syscall0) | |
37 | _syscall0(pid_t, gettid) | |
38 | #elif defined(__NR_gettid) | |
39 | static inline pid_t gettid(void) | |
40 | { | |
41 | return syscall(__NR_gettid); | |
42 | } | |
43 | #else | |
44 | #warning "use pid as tid" | |
45 | static inline pid_t gettid(void) | |
46 | { | |
47 | return getpid(); | |
48 | } | |
49 | #endif | |
50 | ||
51 | #ifndef DYNAMIC_LINK_TEST | |
52 | #define _LGPL_SOURCE | |
53 | #else | |
54 | #define debug_yield_read() | |
55 | #endif | |
56 | #include "urcu.h" | |
57 | ||
58 | struct test_array { | |
59 | int a; | |
60 | }; | |
61 | ||
62 | struct per_thread_lock { | |
63 | pthread_mutex_t lock; | |
64 | } __attribute__((aligned(128))); /* cache-line aligned */ | |
65 | ||
66 | static struct per_thread_lock *per_thread_lock; | |
67 | ||
78efb485 | 68 | static volatile int test_go, test_stop; |
0ee8bb0a MD |
69 | |
70 | static int wdelay; | |
71 | ||
72 | static struct test_array test_array = { 8 }; | |
73 | ||
74 | static unsigned long duration; | |
0ee8bb0a MD |
75 | |
76 | /* | |
77 | * returns 0 if test should end. | |
78 | */ | |
79 | static int test_duration_write(void) | |
80 | { | |
78efb485 | 81 | return !test_stop; |
0ee8bb0a MD |
82 | } |
83 | ||
84 | static int test_duration_read(void) | |
85 | { | |
78efb485 | 86 | return !test_stop; |
0ee8bb0a MD |
87 | } |
88 | ||
89 | static unsigned long long __thread nr_writes; | |
90 | static unsigned long long __thread nr_reads; | |
91 | ||
92 | static unsigned long long __attribute__((aligned(128))) *tot_nr_writes; | |
93 | static unsigned long long __attribute__((aligned(128))) *tot_nr_reads; | |
94 | ||
95 | static unsigned int nr_readers; | |
96 | static unsigned int nr_writers; | |
97 | ||
98 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
99 | ||
100 | void rcu_copy_mutex_lock(void) | |
101 | { | |
102 | int ret; | |
103 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
104 | if (ret) { | |
105 | perror("Error in pthread mutex lock"); | |
106 | exit(-1); | |
107 | } | |
108 | } | |
109 | ||
110 | void rcu_copy_mutex_unlock(void) | |
111 | { | |
112 | int ret; | |
113 | ||
114 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
115 | if (ret) { | |
116 | perror("Error in pthread mutex unlock"); | |
117 | exit(-1); | |
118 | } | |
119 | } | |
120 | ||
121 | void *thr_reader(void *data) | |
122 | { | |
123 | unsigned long tidx = (unsigned long)data; | |
124 | ||
125 | printf("thread_begin %s, thread id : %lx, tid %lu\n", | |
126 | "reader", pthread_self(), (unsigned long)gettid()); | |
127 | ||
128 | while (!test_go) | |
129 | { | |
130 | } | |
131 | ||
132 | for (;;) { | |
133 | pthread_mutex_lock(&per_thread_lock[tidx].lock); | |
134 | assert(test_array.a == 8); | |
135 | pthread_mutex_unlock(&per_thread_lock[tidx].lock); | |
136 | nr_reads++; | |
137 | if (!test_duration_read()) | |
138 | break; | |
139 | } | |
140 | ||
141 | tot_nr_reads[tidx] = nr_reads; | |
142 | printf("thread_end %s, thread id : %lx, tid %lu\n", | |
143 | "reader", pthread_self(), (unsigned long)gettid()); | |
144 | return ((void*)1); | |
145 | ||
146 | } | |
147 | ||
148 | void *thr_writer(void *data) | |
149 | { | |
150 | unsigned long wtidx = (unsigned long)data; | |
151 | long tidx; | |
152 | ||
153 | printf("thread_begin %s, thread id : %lx, tid %lu\n", | |
154 | "writer", pthread_self(), (unsigned long)gettid()); | |
155 | ||
156 | while (!test_go) | |
157 | { | |
158 | } | |
159 | smp_mb(); | |
160 | ||
161 | for (;;) { | |
162 | for (tidx = 0; tidx < nr_readers; tidx++) { | |
163 | pthread_mutex_lock(&per_thread_lock[tidx].lock); | |
164 | } | |
165 | test_array.a = 8; | |
166 | for (tidx = nr_readers - 1; tidx >= 0; tidx--) { | |
167 | pthread_mutex_unlock(&per_thread_lock[tidx].lock); | |
168 | } | |
169 | nr_writes++; | |
170 | if (!test_duration_write()) | |
171 | break; | |
172 | if (wdelay) | |
173 | usleep(wdelay); | |
174 | } | |
175 | ||
176 | printf("thread_end %s, thread id : %lx, tid %lu\n", | |
177 | "writer", pthread_self(), (unsigned long)gettid()); | |
178 | tot_nr_writes[wtidx] = nr_writes; | |
179 | return ((void*)2); | |
180 | } | |
181 | ||
182 | void show_usage(int argc, char **argv) | |
183 | { | |
184 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
185 | #ifdef DEBUG_YIELD | |
186 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
187 | #endif | |
188 | printf(" [-d delay] (writer period (us))"); | |
189 | printf("\n"); | |
190 | } | |
191 | ||
192 | int main(int argc, char **argv) | |
193 | { | |
194 | int err; | |
195 | pthread_t *tid_reader, *tid_writer; | |
196 | void *tret; | |
197 | unsigned long long *count_reader, *count_writer; | |
198 | unsigned long long tot_reads = 0, tot_writes = 0; | |
199 | int i; | |
200 | ||
201 | if (argc < 4) { | |
202 | show_usage(argc, argv); | |
203 | return -1; | |
204 | } | |
205 | smp_mb(); | |
206 | ||
207 | err = sscanf(argv[1], "%u", &nr_readers); | |
208 | if (err != 1) { | |
209 | show_usage(argc, argv); | |
210 | return -1; | |
211 | } | |
212 | ||
213 | err = sscanf(argv[2], "%u", &nr_writers); | |
214 | if (err != 1) { | |
215 | show_usage(argc, argv); | |
216 | return -1; | |
217 | } | |
218 | ||
219 | err = sscanf(argv[3], "%lu", &duration); | |
220 | if (err != 1) { | |
221 | show_usage(argc, argv); | |
222 | return -1; | |
223 | } | |
224 | ||
225 | for (i = 4; i < argc; i++) { | |
226 | if (argv[i][0] != '-') | |
227 | continue; | |
228 | switch (argv[i][1]) { | |
229 | #ifdef DEBUG_YIELD | |
230 | case 'r': | |
231 | yield_active |= YIELD_READ; | |
232 | break; | |
233 | case 'w': | |
234 | yield_active |= YIELD_WRITE; | |
235 | break; | |
236 | #endif | |
237 | case 'd': | |
238 | if (argc < i + 2) { | |
239 | show_usage(argc, argv); | |
240 | return -1; | |
241 | } | |
242 | wdelay = atoi(argv[++i]); | |
243 | break; | |
244 | } | |
245 | } | |
246 | ||
247 | printf("running test for %lu seconds, %u readers, %u writers.\n", | |
248 | duration, nr_readers, nr_writers); | |
249 | printf("Writer delay : %u us.\n", wdelay); | |
250 | printf("thread %-6s, thread id : %lx, tid %lu\n", | |
251 | "main", pthread_self(), (unsigned long)gettid()); | |
252 | ||
253 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
254 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
255 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
256 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
257 | tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers); | |
258 | tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers); | |
259 | per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers); | |
260 | ||
261 | for (i = 0; i < nr_readers; i++) { | |
262 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
263 | (void *)(long)i); | |
264 | if (err != 0) | |
265 | exit(1); | |
266 | } | |
267 | for (i = 0; i < nr_writers; i++) { | |
268 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
269 | (void *)(long)i); | |
270 | if (err != 0) | |
271 | exit(1); | |
272 | } | |
273 | ||
0ee8bb0a MD |
274 | smp_mb(); |
275 | ||
78efb485 MD |
276 | test_go = 1; |
277 | ||
278 | sleep(duration); | |
279 | ||
280 | test_stop = 1; | |
281 | ||
0ee8bb0a MD |
282 | for (i = 0; i < nr_readers; i++) { |
283 | err = pthread_join(tid_reader[i], &tret); | |
284 | if (err != 0) | |
285 | exit(1); | |
286 | tot_reads += tot_nr_reads[i]; | |
287 | } | |
288 | for (i = 0; i < nr_writers; i++) { | |
289 | err = pthread_join(tid_writer[i], &tret); | |
290 | if (err != 0) | |
291 | exit(1); | |
292 | tot_writes += tot_nr_writes[i]; | |
293 | } | |
294 | ||
295 | printf("total number of reads : %llu, writes %llu\n", tot_reads, | |
296 | tot_writes); | |
297 | free(tid_reader); | |
298 | free(tid_writer); | |
299 | free(count_reader); | |
300 | free(count_writer); | |
301 | free(tot_nr_reads); | |
302 | free(tot_nr_writes); | |
303 | free(per_thread_lock); | |
304 | return 0; | |
305 | } |