Fix tests: use of uninitialized variables
[urcu.git] / tests / test_urcu_timing.c
1 /*
2 * test_urcu.c
3 *
4 * Userspace RCU library - 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 #include <urcu/arch.h>
34
35 #include "thread-id.h"
36
37 #define _LGPL_SOURCE
38 #include <urcu.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 cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time;
84 static 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 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 }
109 time2 = caa_get_cycles();
110
111 rcu_unregister_thread();
112
113 reader_time[(unsigned long)arg] = time2 - time1;
114
115 sleep(2);
116 printf("thread_end %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
118 return ((void*)1);
119
120 }
121
122 void *thr_writer(void *arg)
123 {
124 int i, j;
125 struct test_array *new, *old;
126 cycles_t time1, time2;
127
128 printf("thread_begin %s, tid %lu\n",
129 "writer", urcu_get_thread_id());
130 sleep(2);
131
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;
138 if (old) {
139 assert(old->a == 8);
140 }
141 new->a = 8;
142 old = rcu_xchg_pointer(&test_rcu_pointer, new);
143 rcu_copy_mutex_unlock();
144 synchronize_rcu();
145 /* can be done after unlock */
146 if (old) {
147 old->a = 0;
148 }
149 free(old);
150 time2 = caa_get_cycles();
151 writer_time[(unsigned long)arg] += time2 - time1;
152 usleep(1);
153 }
154 }
155
156 printf("thread_end %s, tid %lu\n",
157 "writer", urcu_get_thread_id());
158 return ((void*)2);
159 }
160
161 int main(int argc, char **argv)
162 {
163 int err;
164 pthread_t *tid_reader, *tid_writer;
165 void *tret;
166 int i;
167 cycles_t tot_rtime = 0;
168 cycles_t tot_wtime = 0;
169
170 if (argc < 2) {
171 printf("Usage : %s nr_readers nr_writers\n", argv[0]);
172 exit(-1);
173 }
174 num_read = atoi(argv[1]);
175 num_write = atoi(argv[2]);
176
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));
181
182 printf("thread %-6s, tid %lu\n",
183 "main", urcu_get_thread_id());
184
185 for (i = 0; i < NR_READ; i++) {
186 err = pthread_create(&tid_reader[i], NULL, thr_reader,
187 (void *)(long)i);
188 if (err != 0)
189 exit(1);
190 }
191 for (i = 0; i < NR_WRITE; i++) {
192 err = pthread_create(&tid_writer[i], NULL, thr_writer,
193 (void *)(long)i);
194 if (err != 0)
195 exit(1);
196 }
197
198 sleep(10);
199
200 for (i = 0; i < NR_READ; i++) {
201 err = pthread_join(tid_reader[i], &tret);
202 if (err != 0)
203 exit(1);
204 tot_rtime += reader_time[i];
205 }
206 for (i = 0; i < NR_WRITE; i++) {
207 err = pthread_join(tid_writer[i], &tret);
208 if (err != 0)
209 exit(1);
210 tot_wtime += writer_time[i];
211 }
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));
217
218 free(reader_time);
219 free(writer_time);
220 free(tid_reader);
221 free(tid_writer);
222
223 return 0;
224 }
This page took 0.032819 seconds and 4 git commands to generate.