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