uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / benchmark / test_urcu_timing.c
CommitLineData
ce29b371
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: GPL-2.0-or-later
4
20bf310a 5/*
20bf310a 6 * Userspace RCU library - test program
20bf310a
MD
7 */
8
9#include <stdio.h>
10#include <pthread.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/wait.h>
15#include <unistd.h>
16#include <stdio.h>
94b343fd 17#include <errno.h>
01477510 18
ec4e58a3 19#include <urcu/arch.h>
01477510 20#include <urcu/assert.h>
20bf310a 21
94df6318 22#include "thread-id.h"
20bf310a 23
121a5d44 24#define _LGPL_SOURCE
ec4e58a3 25#include <urcu.h>
20bf310a 26
c265818b
MD
27pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
28
61c3fb60 29static
c265818b
MD
30void rcu_copy_mutex_lock(void)
31{
32 int ret;
33 ret = pthread_mutex_lock(&rcu_copy_mutex);
34 if (ret) {
35 perror("Error in pthread mutex lock");
36 exit(-1);
37 }
38}
39
61c3fb60 40static
c265818b
MD
41void rcu_copy_mutex_unlock(void)
42{
43 int ret;
44
45 ret = pthread_mutex_unlock(&rcu_copy_mutex);
46 if (ret) {
47 perror("Error in pthread mutex unlock");
48 exit(-1);
49 }
50}
51
20bf310a
MD
52struct test_array {
53 int a;
54};
55
56static struct test_array *test_rcu_pointer;
57
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)
61
102d1d23
MD
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)
20bf310a 65
2c9689fe
MD
66static int num_read;
67static int num_write;
20bf310a 68
2c9689fe
MD
69#define NR_READ num_read
70#define NR_WRITE num_write
71
3fa18286
MD
72static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time;
73static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
20bf310a 74
61c3fb60 75static
20bf310a
MD
76void *thr_reader(void *arg)
77{
83e334d0 78 unsigned int i, j;
20bf310a 79 struct test_array *local_ptr;
3fa18286 80 caa_cycles_t time1, time2;
20bf310a 81
94df6318
MD
82 printf("thread_begin %s, tid %lu\n",
83 "reader", urcu_get_thread_id());
20bf310a
MD
84 sleep(2);
85
121a5d44 86 rcu_register_thread();
20bf310a 87
06f22bdb 88 time1 = caa_get_cycles();
20bf310a
MD
89 for (i = 0; i < OUTER_READ_LOOP; i++) {
90 for (j = 0; j < INNER_READ_LOOP; j++) {
1430ee0b 91 rcu_read_lock();
20bf310a
MD
92 local_ptr = rcu_dereference(test_rcu_pointer);
93 if (local_ptr) {
01477510 94 urcu_posix_assert(local_ptr->a == 8);
20bf310a 95 }
1430ee0b 96 rcu_read_unlock();
20bf310a
MD
97 }
98 }
06f22bdb 99 time2 = caa_get_cycles();
20bf310a 100
121a5d44 101 rcu_unregister_thread();
20bf310a
MD
102
103 reader_time[(unsigned long)arg] = time2 - time1;
104
105 sleep(2);
94df6318
MD
106 printf("thread_end %s, tid %lu\n",
107 "reader", urcu_get_thread_id());
20bf310a
MD
108 return ((void*)1);
109
110}
111
61c3fb60 112static
20bf310a
MD
113void *thr_writer(void *arg)
114{
83e334d0 115 unsigned int i, j;
20bf310a 116 struct test_array *new, *old;
3fa18286 117 caa_cycles_t time1, time2;
20bf310a 118
94df6318
MD
119 printf("thread_begin %s, tid %lu\n",
120 "writer", urcu_get_thread_id());
20bf310a
MD
121 sleep(2);
122
102d1d23 123 for (i = 0; i < OUTER_WRITE_LOOP; i++) {
102d1d23 124 for (j = 0; j < INNER_WRITE_LOOP; j++) {
06f22bdb 125 time1 = caa_get_cycles();
102d1d23
MD
126 new = malloc(sizeof(struct test_array));
127 rcu_copy_mutex_lock();
128 old = test_rcu_pointer;
129 if (old) {
01477510 130 urcu_posix_assert(old->a == 8);
102d1d23
MD
131 }
132 new->a = 8;
d2835e6f 133 old = rcu_xchg_pointer(&test_rcu_pointer, new);
102d1d23 134 rcu_copy_mutex_unlock();
d2835e6f 135 synchronize_rcu();
102d1d23
MD
136 /* can be done after unlock */
137 if (old) {
138 old->a = 0;
139 }
140 free(old);
06f22bdb 141 time2 = caa_get_cycles();
fc606a74
MD
142 writer_time[(unsigned long)arg] += time2 - time1;
143 usleep(1);
20bf310a 144 }
20bf310a
MD
145 }
146
94df6318
MD
147 printf("thread_end %s, tid %lu\n",
148 "writer", urcu_get_thread_id());
20bf310a
MD
149 return ((void*)2);
150}
151
2c9689fe 152int main(int argc, char **argv)
20bf310a
MD
153{
154 int err;
2c9689fe 155 pthread_t *tid_reader, *tid_writer;
20bf310a
MD
156 void *tret;
157 int i;
3fa18286
MD
158 caa_cycles_t tot_rtime = 0;
159 caa_cycles_t tot_wtime = 0;
20bf310a 160
2c9689fe
MD
161 if (argc < 2) {
162 printf("Usage : %s nr_readers nr_writers\n", argv[0]);
163 exit(-1);
164 }
165 num_read = atoi(argv[1]);
166 num_write = atoi(argv[2]);
167
9aa14175
MD
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));
2c9689fe 172
94df6318
MD
173 printf("thread %-6s, tid %lu\n",
174 "main", urcu_get_thread_id());
20bf310a
MD
175
176 for (i = 0; i < NR_READ; i++) {
177 err = pthread_create(&tid_reader[i], NULL, thr_reader,
178 (void *)(long)i);
179 if (err != 0)
180 exit(1);
181 }
182 for (i = 0; i < NR_WRITE; i++) {
102d1d23
MD
183 err = pthread_create(&tid_writer[i], NULL, thr_writer,
184 (void *)(long)i);
20bf310a
MD
185 if (err != 0)
186 exit(1);
187 }
188
189 sleep(10);
190
191 for (i = 0; i < NR_READ; i++) {
192 err = pthread_join(tid_reader[i], &tret);
193 if (err != 0)
194 exit(1);
102d1d23 195 tot_rtime += reader_time[i];
20bf310a
MD
196 }
197 for (i = 0; i < NR_WRITE; i++) {
198 err = pthread_join(tid_writer[i], &tret);
199 if (err != 0)
200 exit(1);
102d1d23 201 tot_wtime += writer_time[i];
20bf310a
MD
202 }
203 free(test_rcu_pointer);
204 printf("Time per read : %g cycles\n",
102d1d23
MD
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));
20bf310a 208
2c9689fe
MD
209 free(reader_time);
210 free(writer_time);
211 free(tid_reader);
212 free(tid_writer);
213
20bf310a
MD
214 return 0;
215}
This page took 0.058483 seconds and 5 git commands to generate.