Implement DQ unit test
[userspace-rcu.git] / tests / benchmark / test_rwlock_timing.c
CommitLineData
20bf310a
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6982d6d7 6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
20bf310a 7 *
af02d47e
MD
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.
20bf310a
MD
21 */
22
d8540fc5 23#define _GNU_SOURCE
20bf310a
MD
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>
20bf310a 33#include <pthread.h>
94b343fd
PM
34#include <errno.h>
35
ec4e58a3 36#include <urcu/arch.h>
20bf310a 37
94df6318 38#include "thread-id.h"
20bf310a 39
ec4e58a3 40#include <urcu.h>
20bf310a
MD
41
42struct test_array {
43 int a;
44};
45
46pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
47
48static 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
102d1d23
MD
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)
20bf310a 57
2c9689fe
MD
58static int num_read;
59static int num_write;
20bf310a 60
2c9689fe
MD
61#define NR_READ num_read
62#define NR_WRITE num_write
63
06f22bdb
DG
64static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time;
65static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
20bf310a
MD
66
67void *thr_reader(void *arg)
68{
69 int i, j;
70 cycles_t time1, time2;
71
94df6318
MD
72 printf("thread_begin %s, tid %lu\n",
73 "reader", urcu_get_thread_id());
20bf310a
MD
74 sleep(2);
75
06f22bdb 76 time1 = caa_get_cycles();
20bf310a
MD
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 }
06f22bdb 84 time2 = caa_get_cycles();
20bf310a
MD
85
86 reader_time[(unsigned long)arg] = time2 - time1;
87
88 sleep(2);
94df6318
MD
89 printf("thread_end %s, tid %lu\n",
90 "reader", urcu_get_thread_id());
20bf310a
MD
91 return ((void*)1);
92
93}
94
95void *thr_writer(void *arg)
96{
102d1d23
MD
97 int i, j;
98 cycles_t time1, time2;
20bf310a 99
94df6318
MD
100 printf("thread_begin %s, tid %lu\n",
101 "writer", urcu_get_thread_id());
20bf310a
MD
102 sleep(2);
103
102d1d23
MD
104 for (i = 0; i < OUTER_WRITE_LOOP; i++) {
105 for (j = 0; j < INNER_WRITE_LOOP; j++) {
06f22bdb 106 time1 = caa_get_cycles();
102d1d23
MD
107 pthread_rwlock_wrlock(&lock);
108 test_array.a = 8;
109 pthread_rwlock_unlock(&lock);
06f22bdb 110 time2 = caa_get_cycles();
102d1d23
MD
111 writer_time[(unsigned long)arg] += time2 - time1;
112 usleep(1);
113 }
20bf310a
MD
114 }
115
94df6318
MD
116 printf("thread_end %s, tid %lu\n",
117 "writer", urcu_get_thread_id());
20bf310a
MD
118 return ((void*)2);
119}
120
2c9689fe 121int main(int argc, char **argv)
20bf310a
MD
122{
123 int err;
2c9689fe 124 pthread_t *tid_reader, *tid_writer;
20bf310a
MD
125 void *tret;
126 int i;
102d1d23
MD
127 cycles_t tot_rtime = 0;
128 cycles_t tot_wtime = 0;
20bf310a 129
2c9689fe
MD
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
9aa14175
MD
137 reader_time = calloc(num_read, sizeof(*reader_time));
138 writer_time = calloc(num_write, sizeof(*writer_time));
139 tid_reader = calloc(num_read, sizeof(*tid_reader));
140 tid_writer = calloc(num_write, sizeof(*tid_writer));
2c9689fe 141
94df6318
MD
142 printf("thread %-6s, tid %lu\n",
143 "main", urcu_get_thread_id());
20bf310a
MD
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++) {
102d1d23
MD
152 err = pthread_create(&tid_writer[i], NULL, thr_writer,
153 (void *)(long)i);
20bf310a
MD
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);
102d1d23 164 tot_rtime += reader_time[i];
20bf310a
MD
165 }
166 for (i = 0; i < NR_WRITE; i++) {
167 err = pthread_join(tid_writer[i], &tret);
168 if (err != 0)
169 exit(1);
102d1d23 170 tot_wtime += writer_time[i];
20bf310a
MD
171 }
172 printf("Time per read : %g cycles\n",
102d1d23
MD
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));
20bf310a 176
2c9689fe
MD
177 free(reader_time);
178 free(writer_time);
179 free(tid_reader);
180 free(tid_writer);
181
20bf310a
MD
182 return 0;
183}
This page took 0.040503 seconds and 4 git commands to generate.