4 * Userspace QSBR - 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>
34 #include <urcu/arch.h>
35 #include "thread-id.h"
38 #include <urcu-qsbr.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 cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *reader_time
;
84 static cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *writer_time
;
86 void *thr_reader(void *arg
)
89 struct test_array
*local_ptr
;
90 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);
108 _rcu_quiescent_state();
110 time2
= caa_get_cycles();
112 rcu_unregister_thread();
114 reader_time
[(unsigned long)arg
] = time2
- time1
;
117 printf("thread_end %s, tid %lu\n",
118 "reader", urcu_get_thread_id());
123 void *thr_writer(void *arg
)
126 struct test_array
*new, *old
;
127 cycles_t time1
, time2
;
129 printf("thread_begin %s, tid %lu\n",
130 "writer", urcu_get_thread_id());
133 for (i
= 0; i
< OUTER_WRITE_LOOP
; i
++) {
134 for (j
= 0; j
< INNER_WRITE_LOOP
; j
++) {
135 time1
= caa_get_cycles();
136 new = malloc(sizeof(struct test_array
));
137 rcu_copy_mutex_lock();
138 old
= test_rcu_pointer
;
143 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
144 rcu_copy_mutex_unlock();
146 /* can be done after unlock */
151 time2
= caa_get_cycles();
152 writer_time
[(unsigned long)arg
] += time2
- time1
;
157 printf("thread_end %s, tid %lu\n",
158 "writer", urcu_get_thread_id());
162 int main(int argc
, char **argv
)
165 pthread_t
*tid_reader
, *tid_writer
;
168 cycles_t tot_rtime
= 0;
169 cycles_t tot_wtime
= 0;
172 printf("Usage : %s nr_readers nr_writers\n", argv
[0]);
175 num_read
= atoi(argv
[1]);
176 num_write
= atoi(argv
[2]);
178 reader_time
= calloc(num_read
, sizeof(*reader_time
));
179 writer_time
= calloc(num_write
, sizeof(*writer_time
));
180 tid_reader
= calloc(num_read
, sizeof(*tid_reader
));
181 tid_writer
= calloc(num_write
, sizeof(*tid_writer
));
183 printf("thread %-6s, tid %lu\n",
184 "main", urcu_get_thread_id());
186 for (i
= 0; i
< NR_READ
; i
++) {
187 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
192 for (i
= 0; i
< NR_WRITE
; i
++) {
193 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
201 for (i
= 0; i
< NR_READ
; i
++) {
202 err
= pthread_join(tid_reader
[i
], &tret
);
205 tot_rtime
+= reader_time
[i
];
207 for (i
= 0; i
< NR_WRITE
; i
++) {
208 err
= pthread_join(tid_writer
[i
], &tret
);
211 tot_wtime
+= writer_time
[i
];
213 free(test_rcu_pointer
);
214 printf("Time per read : %g cycles\n",
215 (double)tot_rtime
/ ((double)NR_READ
* (double)READ_LOOP
));
216 printf("Time per write : %g cycles\n",
217 (double)tot_wtime
/ ((double)NR_WRITE
* (double)WRITE_LOOP
));