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