7619390e35f854df7020d8452e9dfd03420de540
[urcu.git] / tests / benchmark / test_urcu_qsbr_timing.c
1 /*
2 * test_qsbr_timing.c
3 *
4 * Userspace QSBR - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33
34 #include <urcu/arch.h>
35 #include "thread-id.h"
36
37 #define _LGPL_SOURCE
38 #include <urcu-qsbr.h>
39
40 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42 void rcu_copy_mutex_lock(void)
43 {
44 int ret;
45 ret = pthread_mutex_lock(&rcu_copy_mutex);
46 if (ret) {
47 perror("Error in pthread mutex lock");
48 exit(-1);
49 }
50 }
51
52 void rcu_copy_mutex_unlock(void)
53 {
54 int ret;
55
56 ret = pthread_mutex_unlock(&rcu_copy_mutex);
57 if (ret) {
58 perror("Error in pthread mutex unlock");
59 exit(-1);
60 }
61 }
62
63 struct test_array {
64 int a;
65 };
66
67 static struct test_array *test_rcu_pointer;
68
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)
72
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)
76
77 static int num_read;
78 static int num_write;
79
80 #define NR_READ num_read
81 #define NR_WRITE num_write
82
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;
85
86 void *thr_reader(void *arg)
87 {
88 int i, j;
89 struct test_array *local_ptr;
90 caa_cycles_t time1, time2;
91
92 printf("thread_begin %s, tid %lu\n",
93 "reader", urcu_get_thread_id());
94 sleep(2);
95
96 rcu_register_thread();
97
98 time1 = caa_get_cycles();
99 for (i = 0; i < OUTER_READ_LOOP; i++) {
100 for (j = 0; j < INNER_READ_LOOP; j++) {
101 _rcu_read_lock();
102 local_ptr = _rcu_dereference(test_rcu_pointer);
103 if (local_ptr) {
104 assert(local_ptr->a == 8);
105 }
106 _rcu_read_unlock();
107 }
108 _rcu_quiescent_state();
109 }
110 time2 = caa_get_cycles();
111
112 rcu_unregister_thread();
113
114 reader_time[(unsigned long)arg] = time2 - time1;
115
116 sleep(2);
117 printf("thread_end %s, tid %lu\n",
118 "reader", urcu_get_thread_id());
119 return ((void*)1);
120
121 }
122
123 void *thr_writer(void *arg)
124 {
125 int i, j;
126 struct test_array *new, *old;
127 caa_cycles_t time1, time2;
128
129 printf("thread_begin %s, tid %lu\n",
130 "writer", urcu_get_thread_id());
131 sleep(2);
132
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;
139 if (old) {
140 assert(old->a == 8);
141 }
142 new->a = 8;
143 old = rcu_xchg_pointer(&test_rcu_pointer, new);
144 rcu_copy_mutex_unlock();
145 synchronize_rcu();
146 /* can be done after unlock */
147 if (old) {
148 old->a = 0;
149 }
150 free(old);
151 time2 = caa_get_cycles();
152 writer_time[(unsigned long)arg] += time2 - time1;
153 usleep(1);
154 }
155 }
156
157 printf("thread_end %s, tid %lu\n",
158 "writer", urcu_get_thread_id());
159 return ((void*)2);
160 }
161
162 int main(int argc, char **argv)
163 {
164 int err;
165 pthread_t *tid_reader, *tid_writer;
166 void *tret;
167 int i;
168 caa_cycles_t tot_rtime = 0;
169 caa_cycles_t tot_wtime = 0;
170
171 if (argc < 2) {
172 printf("Usage : %s nr_readers nr_writers\n", argv[0]);
173 exit(-1);
174 }
175 num_read = atoi(argv[1]);
176 num_write = atoi(argv[2]);
177
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));
182
183 printf("thread %-6s, tid %lu\n",
184 "main", urcu_get_thread_id());
185
186 for (i = 0; i < NR_READ; i++) {
187 err = pthread_create(&tid_reader[i], NULL, thr_reader,
188 (void *)(long)i);
189 if (err != 0)
190 exit(1);
191 }
192 for (i = 0; i < NR_WRITE; i++) {
193 err = pthread_create(&tid_writer[i], NULL, thr_writer,
194 (void *)(long)i);
195 if (err != 0)
196 exit(1);
197 }
198
199 sleep(10);
200
201 for (i = 0; i < NR_READ; i++) {
202 err = pthread_join(tid_reader[i], &tret);
203 if (err != 0)
204 exit(1);
205 tot_rtime += reader_time[i];
206 }
207 for (i = 0; i < NR_WRITE; i++) {
208 err = pthread_join(tid_writer[i], &tret);
209 if (err != 0)
210 exit(1);
211 tot_wtime += writer_time[i];
212 }
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));
218
219 free(reader_time);
220 free(writer_time);
221 free(tid_reader);
222 free(tid_writer);
223
224 return 0;
225 }
This page took 0.046755 seconds and 3 git commands to generate.