1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Userspace RCU library - test program
13 #include <sys/types.h>
19 #include <urcu/arch.h>
20 #include <urcu/assert.h>
22 #include "thread-id.h"
27 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
30 void rcu_copy_mutex_lock(void)
33 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
35 perror("Error in pthread mutex lock");
41 void rcu_copy_mutex_unlock(void)
45 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
47 perror("Error in pthread mutex unlock");
56 static struct test_array
*test_rcu_pointer
;
58 #define OUTER_READ_LOOP 2000U
59 #define INNER_READ_LOOP 100000U
60 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
62 #define OUTER_WRITE_LOOP 10U
63 #define INNER_WRITE_LOOP 200U
64 #define WRITE_LOOP ((unsigned long long)OUTER_WRITE_LOOP * INNER_WRITE_LOOP)
69 #define NR_READ num_read
70 #define NR_WRITE num_write
72 static caa_cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *reader_time
;
73 static caa_cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *writer_time
;
76 void *thr_reader(void *arg
)
79 struct test_array
*local_ptr
;
80 caa_cycles_t time1
, time2
;
82 printf("thread_begin %s, tid %lu\n",
83 "reader", urcu_get_thread_id());
86 rcu_register_thread();
88 time1
= caa_get_cycles();
89 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
90 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
92 local_ptr
= rcu_dereference(test_rcu_pointer
);
94 urcu_posix_assert(local_ptr
->a
== 8);
99 time2
= caa_get_cycles();
101 rcu_unregister_thread();
103 reader_time
[(unsigned long)arg
] = time2
- time1
;
106 printf("thread_end %s, tid %lu\n",
107 "reader", urcu_get_thread_id());
113 void *thr_writer(void *arg
)
116 struct test_array
*new, *old
;
117 caa_cycles_t time1
, time2
;
119 printf("thread_begin %s, tid %lu\n",
120 "writer", urcu_get_thread_id());
123 for (i
= 0; i
< OUTER_WRITE_LOOP
; i
++) {
124 for (j
= 0; j
< INNER_WRITE_LOOP
; j
++) {
125 time1
= caa_get_cycles();
126 new = malloc(sizeof(struct test_array
));
127 rcu_copy_mutex_lock();
128 old
= test_rcu_pointer
;
130 urcu_posix_assert(old
->a
== 8);
133 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
134 rcu_copy_mutex_unlock();
136 /* can be done after unlock */
141 time2
= caa_get_cycles();
142 writer_time
[(unsigned long)arg
] += time2
- time1
;
147 printf("thread_end %s, tid %lu\n",
148 "writer", urcu_get_thread_id());
152 int main(int argc
, char **argv
)
155 pthread_t
*tid_reader
, *tid_writer
;
158 caa_cycles_t tot_rtime
= 0;
159 caa_cycles_t tot_wtime
= 0;
162 printf("Usage : %s nr_readers nr_writers\n", argv
[0]);
165 num_read
= atoi(argv
[1]);
166 num_write
= atoi(argv
[2]);
168 reader_time
= calloc(num_read
, sizeof(*reader_time
));
169 writer_time
= calloc(num_write
, sizeof(*writer_time
));
170 tid_reader
= calloc(num_read
, sizeof(*tid_reader
));
171 tid_writer
= calloc(num_write
, sizeof(*tid_writer
));
173 printf("thread %-6s, tid %lu\n",
174 "main", urcu_get_thread_id());
176 for (i
= 0; i
< NR_READ
; i
++) {
177 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
182 for (i
= 0; i
< NR_WRITE
; i
++) {
183 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
191 for (i
= 0; i
< NR_READ
; i
++) {
192 err
= pthread_join(tid_reader
[i
], &tret
);
195 tot_rtime
+= reader_time
[i
];
197 for (i
= 0; i
< NR_WRITE
; i
++) {
198 err
= pthread_join(tid_writer
[i
], &tret
);
201 tot_wtime
+= writer_time
[i
];
203 free(test_rcu_pointer
);
204 printf("Time per read : %g cycles\n",
205 (double)tot_rtime
/ ((double)NR_READ
* (double)READ_LOOP
));
206 printf("Time per write : %g cycles\n",
207 (double)tot_wtime
/ ((double)NR_WRITE
* (double)WRITE_LOOP
));