cf70709c9fe7e58beaab0f2e3920391d06f43d02
[urcu.git] / 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@polymtl.ca>
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 <sys/syscall.h>
33 #include <arch.h>
34
35 #if defined(_syscall0)
36 _syscall0(pid_t, gettid)
37 #elif defined(__NR_gettid)
38 static inline pid_t gettid(void)
39 {
40 return syscall(__NR_gettid);
41 }
42 #else
43 #warning "use pid as tid"
44 static inline pid_t gettid(void)
45 {
46 return getpid();
47 }
48 #endif
49
50 #define _LGPL_SOURCE
51 #include "urcu.h"
52
53 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
54
55 void rcu_copy_mutex_lock(void)
56 {
57 int ret;
58 ret = pthread_mutex_lock(&rcu_copy_mutex);
59 if (ret) {
60 perror("Error in pthread mutex lock");
61 exit(-1);
62 }
63 }
64
65 void rcu_copy_mutex_unlock(void)
66 {
67 int ret;
68
69 ret = pthread_mutex_unlock(&rcu_copy_mutex);
70 if (ret) {
71 perror("Error in pthread mutex unlock");
72 exit(-1);
73 }
74 }
75
76 struct test_array {
77 int a;
78 };
79
80 static struct test_array *test_rcu_pointer;
81
82 #define OUTER_READ_LOOP 2000U
83 #define INNER_READ_LOOP 100000U
84 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
85
86 #define WRITE_LOOP 2000U
87
88 #define NR_READ 10
89 #define NR_WRITE 9
90
91 static cycles_t reader_time[NR_READ] __attribute__((aligned(128)));
92
93 void *thr_reader(void *arg)
94 {
95 int i, j;
96 struct test_array *local_ptr;
97 cycles_t time1, time2;
98
99 printf("thread_begin %s, thread id : %lx, tid %lu\n",
100 "reader", pthread_self(), (unsigned long)gettid());
101 sleep(2);
102
103 rcu_register_thread();
104
105 time1 = get_cycles();
106 for (i = 0; i < OUTER_READ_LOOP; i++) {
107 for (j = 0; j < INNER_READ_LOOP; j++) {
108 rcu_read_lock();
109 local_ptr = rcu_dereference(test_rcu_pointer);
110 if (local_ptr) {
111 assert(local_ptr->a == 8);
112 }
113 rcu_read_unlock();
114 }
115 }
116 time2 = get_cycles();
117
118 rcu_unregister_thread();
119
120 reader_time[(unsigned long)arg] = time2 - time1;
121
122 sleep(2);
123 printf("thread_end %s, thread id : %lx, tid %lu\n",
124 "reader", pthread_self(), (unsigned long)gettid());
125 return ((void*)1);
126
127 }
128
129 void *thr_writer(void *arg)
130 {
131 int i;
132 struct test_array *new, *old;
133
134 printf("thread_begin %s, thread id : %lx, tid %lu\n",
135 "writer", pthread_self(), (unsigned long)gettid());
136 sleep(2);
137
138 for (i = 0; i < WRITE_LOOP; i++) {
139 new = malloc(sizeof(struct test_array));
140 rcu_copy_mutex_lock();
141 old = test_rcu_pointer;
142 if (old) {
143 assert(old->a == 8);
144 }
145 new->a = 8;
146 old = rcu_publish_content(&test_rcu_pointer, new);
147 rcu_copy_mutex_unlock();
148 /* can be done after unlock */
149 if (old) {
150 old->a = 0;
151 }
152 free(old);
153 usleep(1);
154 }
155
156 printf("thread_end %s, thread id : %lx, tid %lu\n",
157 "writer", pthread_self(), (unsigned long)gettid());
158 return ((void*)2);
159 }
160
161 int main()
162 {
163 int err;
164 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
165 void *tret;
166 int i;
167 cycles_t tot_time = 0;
168
169 printf("thread %-6s, thread id : %lx, tid %lu\n",
170 "main", pthread_self(), (unsigned long)gettid());
171
172 for (i = 0; i < NR_READ; i++) {
173 err = pthread_create(&tid_reader[i], NULL, thr_reader,
174 (void *)(long)i);
175 if (err != 0)
176 exit(1);
177 }
178 for (i = 0; i < NR_WRITE; i++) {
179 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
180 if (err != 0)
181 exit(1);
182 }
183
184 sleep(10);
185
186 for (i = 0; i < NR_READ; i++) {
187 err = pthread_join(tid_reader[i], &tret);
188 if (err != 0)
189 exit(1);
190 tot_time += reader_time[i];
191 }
192 for (i = 0; i < NR_WRITE; i++) {
193 err = pthread_join(tid_writer[i], &tret);
194 if (err != 0)
195 exit(1);
196 }
197 free(test_rcu_pointer);
198 printf("Time per read : %g cycles\n",
199 (double)tot_time / ((double)NR_READ * (double)READ_LOOP));
200
201 return 0;
202 }
This page took 0.032839 seconds and 4 git commands to generate.