rculist: implement cds_list_add_tail_rcu
[urcu.git] / tests / test_rwlock_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 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <pthread.h>
34 #include <errno.h>
35
36 #include <urcu/arch.h>
37
38 #include "thread-id.h"
39
40 #include <urcu.h>
41
42 struct test_array {
43 int a;
44 };
45
46 pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
47
48 static struct test_array test_array = { 8 };
49
50 #define OUTER_READ_LOOP 200U
51 #define INNER_READ_LOOP 100000U
52 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
53
54 #define OUTER_WRITE_LOOP 10U
55 #define INNER_WRITE_LOOP 200U
56 #define WRITE_LOOP ((unsigned long long)OUTER_WRITE_LOOP * INNER_WRITE_LOOP)
57
58 static int num_read;
59 static int num_write;
60
61 #define NR_READ num_read
62 #define NR_WRITE num_write
63
64 static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time;
65 static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
66
67 void *thr_reader(void *arg)
68 {
69 int i, j;
70 cycles_t time1, time2;
71
72 printf("thread_begin %s, tid %lu\n",
73 "reader", urcu_get_thread_id());
74 sleep(2);
75
76 time1 = caa_get_cycles();
77 for (i = 0; i < OUTER_READ_LOOP; i++) {
78 for (j = 0; j < INNER_READ_LOOP; j++) {
79 pthread_rwlock_rdlock(&lock);
80 assert(test_array.a == 8);
81 pthread_rwlock_unlock(&lock);
82 }
83 }
84 time2 = caa_get_cycles();
85
86 reader_time[(unsigned long)arg] = time2 - time1;
87
88 sleep(2);
89 printf("thread_end %s, tid %lu\n",
90 "reader", urcu_get_thread_id());
91 return ((void*)1);
92
93 }
94
95 void *thr_writer(void *arg)
96 {
97 int i, j;
98 cycles_t time1, time2;
99
100 printf("thread_begin %s, tid %lu\n",
101 "writer", urcu_get_thread_id());
102 sleep(2);
103
104 for (i = 0; i < OUTER_WRITE_LOOP; i++) {
105 for (j = 0; j < INNER_WRITE_LOOP; j++) {
106 time1 = caa_get_cycles();
107 pthread_rwlock_wrlock(&lock);
108 test_array.a = 8;
109 pthread_rwlock_unlock(&lock);
110 time2 = caa_get_cycles();
111 writer_time[(unsigned long)arg] += time2 - time1;
112 usleep(1);
113 }
114 }
115
116 printf("thread_end %s, tid %lu\n",
117 "writer", urcu_get_thread_id());
118 return ((void*)2);
119 }
120
121 int main(int argc, char **argv)
122 {
123 int err;
124 pthread_t *tid_reader, *tid_writer;
125 void *tret;
126 int i;
127 cycles_t tot_rtime = 0;
128 cycles_t tot_wtime = 0;
129
130 if (argc < 2) {
131 printf("Usage : %s nr_readers nr_writers\n", argv[0]);
132 exit(-1);
133 }
134 num_read = atoi(argv[1]);
135 num_write = atoi(argv[2]);
136
137 reader_time = malloc(sizeof(*reader_time) * num_read);
138 writer_time = malloc(sizeof(*writer_time) * num_write);
139 tid_reader = malloc(sizeof(*tid_reader) * num_read);
140 tid_writer = malloc(sizeof(*tid_writer) * num_write);
141
142 printf("thread %-6s, tid %lu\n",
143 "main", urcu_get_thread_id());
144
145 for (i = 0; i < NR_READ; i++) {
146 err = pthread_create(&tid_reader[i], NULL, thr_reader,
147 (void *)(long)i);
148 if (err != 0)
149 exit(1);
150 }
151 for (i = 0; i < NR_WRITE; i++) {
152 err = pthread_create(&tid_writer[i], NULL, thr_writer,
153 (void *)(long)i);
154 if (err != 0)
155 exit(1);
156 }
157
158 sleep(10);
159
160 for (i = 0; i < NR_READ; i++) {
161 err = pthread_join(tid_reader[i], &tret);
162 if (err != 0)
163 exit(1);
164 tot_rtime += reader_time[i];
165 }
166 for (i = 0; i < NR_WRITE; i++) {
167 err = pthread_join(tid_writer[i], &tret);
168 if (err != 0)
169 exit(1);
170 tot_wtime += writer_time[i];
171 }
172 printf("Time per read : %g cycles\n",
173 (double)tot_rtime / ((double)NR_READ * (double)READ_LOOP));
174 printf("Time per write : %g cycles\n",
175 (double)tot_wtime / ((double)NR_WRITE * (double)WRITE_LOOP));
176
177 free(reader_time);
178 free(writer_time);
179 free(tid_reader);
180 free(tid_writer);
181
182 return 0;
183 }
This page took 0.047377 seconds and 4 git commands to generate.