Fix RCU_GP_CTR_BIT
[urcu.git] / 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@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 <pthread.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 rdtscll(val) do { \
39 unsigned int __a,__d; \
40 asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
41 (val) = ((unsigned long long)__a) | (((unsigned long long)__d)<<32); \
42 } while(0)
43
44 typedef unsigned long long cycles_t;
45
46 static inline cycles_t get_cycles (void)
47 {
48 unsigned long long ret = 0;
49
50 rdtscll(ret);
51 return ret;
52 }
53
54 #include "urcu.h"
55
56 struct test_array {
57 int a;
58 };
59
60 pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
61
62 static struct test_array test_array = { 8 };
63
64 #define OUTER_READ_LOOP 200U
65 #define INNER_READ_LOOP 100000U
66 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
67
68 #define WRITE_LOOP 2000U
69
70 #define NR_READ 10
71 #define NR_WRITE 9
72
73 static cycles_t reader_time[NR_READ] __attribute__((aligned(128)));
74
75 void *thr_reader(void *arg)
76 {
77 int i, j;
78 cycles_t time1, time2;
79
80 printf("thread_begin %s, thread id : %lx, tid %lu\n",
81 "reader", pthread_self(), (unsigned long)gettid());
82 sleep(2);
83
84 time1 = get_cycles();
85 for (i = 0; i < OUTER_READ_LOOP; i++) {
86 for (j = 0; j < INNER_READ_LOOP; j++) {
87 pthread_rwlock_rdlock(&lock);
88 assert(test_array.a == 8);
89 pthread_rwlock_unlock(&lock);
90 }
91 }
92 time2 = get_cycles();
93
94 reader_time[(unsigned long)arg] = time2 - time1;
95
96 sleep(2);
97 printf("thread_end %s, thread id : %lx, tid %lu\n",
98 "reader", pthread_self(), (unsigned long)gettid());
99 return ((void*)1);
100
101 }
102
103 void *thr_writer(void *arg)
104 {
105 int i;
106
107 printf("thread_begin %s, thread id : %lx, tid %lu\n",
108 "writer", pthread_self(), (unsigned long)gettid());
109 sleep(2);
110
111 for (i = 0; i < WRITE_LOOP; i++) {
112 pthread_rwlock_wrlock(&lock);
113 test_array.a = 8;
114 pthread_rwlock_unlock(&lock);
115 usleep(1);
116 }
117
118 printf("thread_end %s, thread id : %lx, tid %lu\n",
119 "writer", pthread_self(), (unsigned long)gettid());
120 return ((void*)2);
121 }
122
123 int main()
124 {
125 int err;
126 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
127 void *tret;
128 int i;
129 cycles_t tot_time = 0;
130
131 printf("thread %-6s, thread id : %lx, tid %lu\n",
132 "main", pthread_self(), (unsigned long)gettid());
133
134 for (i = 0; i < NR_READ; i++) {
135 err = pthread_create(&tid_reader[i], NULL, thr_reader,
136 (void *)(long)i);
137 if (err != 0)
138 exit(1);
139 }
140 for (i = 0; i < NR_WRITE; i++) {
141 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
142 if (err != 0)
143 exit(1);
144 }
145
146 sleep(10);
147
148 for (i = 0; i < NR_READ; i++) {
149 err = pthread_join(tid_reader[i], &tret);
150 if (err != 0)
151 exit(1);
152 tot_time += reader_time[i];
153 }
154 for (i = 0; i < NR_WRITE; i++) {
155 err = pthread_join(tid_writer[i], &tret);
156 if (err != 0)
157 exit(1);
158 }
159 printf("Time per read : %g cycles\n",
160 (double)tot_time / ((double)NR_READ * (double)READ_LOOP));
161
162 return 0;
163 }
This page took 0.031922 seconds and 4 git commands to generate.