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.
28 #include <sys/types.h>
33 #include <sys/syscall.h>
38 #if defined(_syscall0)
39 _syscall0(pid_t
, gettid
)
40 #elif defined(__NR_gettid)
41 static inline pid_t
gettid(void)
43 return syscall(__NR_gettid
);
46 #warning "use pid as tid"
47 static inline pid_t
gettid(void)
53 #ifndef DYNAMIC_LINK_TEST
56 #define debug_yield_read()
64 static pthread_mutex_t lock
;
66 static volatile int test_go
, test_stop
;
70 static volatile struct test_array test_array
= { 8 };
72 static unsigned long duration
;
74 /* read-side C.S. duration, in us */
75 static unsigned long rduration
;
78 * returns 0 if test should end.
80 static int test_duration_write(void)
85 static int test_duration_read(void)
90 static unsigned long long __thread nr_writes
;
91 static unsigned long long __thread nr_reads
;
93 static unsigned long long __attribute__((aligned(128))) *tot_nr_writes
;
94 static unsigned long long __attribute__((aligned(128))) *tot_nr_reads
;
96 static unsigned int nr_readers
;
97 static unsigned int nr_writers
;
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");
122 void *thr_reader(void *data
)
124 unsigned long tidx
= (unsigned long)data
;
126 printf("thread_begin %s, thread id : %lx, tid %lu\n",
127 "reader", pthread_self(), (unsigned long)gettid());
134 pthread_mutex_lock(&lock
);
135 assert(test_array
.a
== 8);
136 if (unlikely(rduration
))
138 pthread_mutex_unlock(&lock
);
140 if (unlikely(!test_duration_read()))
144 tot_nr_reads
[tidx
] = nr_reads
;
145 printf("thread_end %s, thread id : %lx, tid %lu\n",
146 "reader", pthread_self(), (unsigned long)gettid());
151 void *thr_writer(void *data
)
153 unsigned long wtidx
= (unsigned long)data
;
155 printf("thread_begin %s, thread id : %lx, tid %lu\n",
156 "writer", pthread_self(), (unsigned long)gettid());
164 pthread_mutex_lock(&lock
);
167 pthread_mutex_unlock(&lock
);
169 if (unlikely(!test_duration_write()))
171 if (unlikely(wdelay
))
175 printf("thread_end %s, thread id : %lx, tid %lu\n",
176 "writer", pthread_self(), (unsigned long)gettid());
177 tot_nr_writes
[wtidx
] = nr_writes
;
181 void show_usage(int argc
, char **argv
)
183 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
185 printf(" [-r] [-w] (yield reader and/or writer)");
187 printf(" [-d delay] (writer period (us))");
188 printf(" [-c duration] (reader C.S. duration (us))");
189 printf(" [-a cpu#] [-a cpu#]... (affinity)");
195 int main(int argc
, char **argv
)
198 pthread_t
*tid_reader
, *tid_writer
;
200 unsigned long long *count_reader
, *count_writer
;
201 unsigned long long tot_reads
= 0, tot_writes
= 0;
203 int use_affinity
= 0;
206 show_usage(argc
, argv
);
211 err
= sscanf(argv
[1], "%u", &nr_readers
);
213 show_usage(argc
, argv
);
217 err
= sscanf(argv
[2], "%u", &nr_writers
);
219 show_usage(argc
, argv
);
223 err
= sscanf(argv
[3], "%lu", &duration
);
225 show_usage(argc
, argv
);
231 for (i
= 4; i
< argc
; i
++) {
232 if (argv
[i
][0] != '-')
234 switch (argv
[i
][1]) {
237 yield_active
|= YIELD_READ
;
240 yield_active
|= YIELD_WRITE
;
245 show_usage(argc
, argv
);
249 CPU_SET(a
, &affinity
);
251 printf("Adding CPU %d affinity\n", a
);
255 show_usage(argc
, argv
);
258 rduration
= atoi(argv
[++i
]);
262 show_usage(argc
, argv
);
265 wdelay
= atoi(argv
[++i
]);
270 printf("running test for %lu seconds, %u readers, %u writers.\n",
271 duration
, nr_readers
, nr_writers
);
272 printf("Writer delay : %u us.\n", wdelay
);
273 printf("thread %-6s, thread id : %lx, tid %lu\n",
274 "main", pthread_self(), (unsigned long)gettid());
277 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
278 perror("sched_setaffinity");
282 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
283 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
284 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
285 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
286 tot_nr_reads
= malloc(sizeof(*tot_nr_reads
) * nr_readers
);
287 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
289 for (i
= 0; i
< nr_readers
; i
++) {
290 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
295 for (i
= 0; i
< nr_writers
; i
++) {
296 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
310 for (i
= 0; i
< nr_readers
; i
++) {
311 err
= pthread_join(tid_reader
[i
], &tret
);
314 tot_reads
+= tot_nr_reads
[i
];
316 for (i
= 0; i
< nr_writers
; i
++) {
317 err
= pthread_join(tid_writer
[i
], &tret
);
320 tot_writes
+= tot_nr_writes
[i
];
323 printf("total number of reads : %llu, writes %llu\n", tot_reads
,