4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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>
33 #include <urcu/arch.h>
35 #include "thread-id.h"
40 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
42 void rcu_copy_mutex_lock(void)
45 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
47 perror("Error in pthread mutex lock");
52 void rcu_copy_mutex_unlock(void)
56 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
58 perror("Error in pthread mutex unlock");
67 static struct test_array
*test_rcu_pointer
;
69 #define OUTER_READ_LOOP 2000U
70 #define INNER_READ_LOOP 100000U
71 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
73 #define OUTER_WRITE_LOOP 10U
74 #define INNER_WRITE_LOOP 200U
75 #define WRITE_LOOP ((unsigned long long)OUTER_WRITE_LOOP * INNER_WRITE_LOOP)
80 #define NR_READ num_read
81 #define NR_WRITE num_write
83 static caa_cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *reader_time
;
84 static caa_cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *writer_time
;
86 void *thr_reader(void *arg
)
89 struct test_array
*local_ptr
;
90 caa_cycles_t time1
, time2
;
92 printf("thread_begin %s, tid %lu\n",
93 "reader", urcu_get_thread_id());
96 rcu_register_thread();
98 time1
= caa_get_cycles();
99 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
100 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
102 local_ptr
= rcu_dereference(test_rcu_pointer
);
104 assert(local_ptr
->a
== 8);
109 time2
= caa_get_cycles();
111 rcu_unregister_thread();
113 reader_time
[(unsigned long)arg
] = time2
- time1
;
116 printf("thread_end %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
122 void *thr_writer(void *arg
)
125 struct test_array
*new, *old
;
126 caa_cycles_t time1
, time2
;
128 printf("thread_begin %s, tid %lu\n",
129 "writer", urcu_get_thread_id());
132 for (i
= 0; i
< OUTER_WRITE_LOOP
; i
++) {
133 for (j
= 0; j
< INNER_WRITE_LOOP
; j
++) {
134 time1
= caa_get_cycles();
135 new = malloc(sizeof(struct test_array
));
136 rcu_copy_mutex_lock();
137 old
= test_rcu_pointer
;
142 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
143 rcu_copy_mutex_unlock();
145 /* can be done after unlock */
150 time2
= caa_get_cycles();
151 writer_time
[(unsigned long)arg
] += time2
- time1
;
156 printf("thread_end %s, tid %lu\n",
157 "writer", urcu_get_thread_id());
161 int main(int argc
, char **argv
)
164 pthread_t
*tid_reader
, *tid_writer
;
167 caa_cycles_t tot_rtime
= 0;
168 caa_cycles_t tot_wtime
= 0;
171 printf("Usage : %s nr_readers nr_writers\n", argv
[0]);
174 num_read
= atoi(argv
[1]);
175 num_write
= atoi(argv
[2]);
177 reader_time
= calloc(num_read
, sizeof(*reader_time
));
178 writer_time
= calloc(num_write
, sizeof(*writer_time
));
179 tid_reader
= calloc(num_read
, sizeof(*tid_reader
));
180 tid_writer
= calloc(num_write
, sizeof(*tid_writer
));
182 printf("thread %-6s, tid %lu\n",
183 "main", urcu_get_thread_id());
185 for (i
= 0; i
< NR_READ
; i
++) {
186 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
191 for (i
= 0; i
< NR_WRITE
; i
++) {
192 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
200 for (i
= 0; i
< NR_READ
; i
++) {
201 err
= pthread_join(tid_reader
[i
], &tret
);
204 tot_rtime
+= reader_time
[i
];
206 for (i
= 0; i
< NR_WRITE
; i
++) {
207 err
= pthread_join(tid_writer
[i
], &tret
);
210 tot_wtime
+= writer_time
[i
];
212 free(test_rcu_pointer
);
213 printf("Time per read : %g cycles\n",
214 (double)tot_rtime
/ ((double)NR_READ
* (double)READ_LOOP
));
215 printf("Time per write : %g cycles\n",
216 (double)tot_wtime
/ ((double)NR_WRITE
* (double)WRITE_LOOP
));