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