4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
15 #include <sys/types.h>
20 #include <sys/syscall.h>
22 #if defined(_syscall0)
23 _syscall0(pid_t
, gettid
)
24 #elif defined(__NR_gettid)
25 static inline pid_t
gettid(void)
27 return syscall(__NR_gettid
);
30 #warning "use pid as tid"
31 static inline pid_t
gettid(void)
37 #define rdtscll(val) do { \
38 unsigned int __a,__d; \
39 asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
40 (val) = ((unsigned long long)__a) | (((unsigned long long)__d)<<32); \
43 typedef unsigned long long cycles_t
;
45 static inline cycles_t
get_cycles (void)
47 unsigned long long ret
= 0;
55 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
57 void rcu_copy_mutex_lock(void)
60 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
62 perror("Error in pthread mutex lock");
67 void rcu_copy_mutex_unlock(void)
71 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
73 perror("Error in pthread mutex unlock");
82 static struct test_array
*test_rcu_pointer
;
84 #define OUTER_READ_LOOP 2000U
85 #define INNER_READ_LOOP 100000U
86 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
88 #define WRITE_LOOP 2000U
93 static cycles_t reader_time
[NR_READ
] __attribute__((aligned(128)));
95 void *thr_reader(void *arg
)
98 struct test_array
*local_ptr
;
99 cycles_t time1
, time2
;
101 printf("thread_begin %s, thread id : %lx, tid %lu\n",
102 "reader", pthread_self(), (unsigned long)gettid());
105 urcu_register_thread();
107 time1
= get_cycles();
108 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
109 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
111 local_ptr
= rcu_dereference(test_rcu_pointer
);
113 assert(local_ptr
->a
== 8);
118 time2
= get_cycles();
120 urcu_unregister_thread();
122 reader_time
[(unsigned long)arg
] = time2
- time1
;
125 printf("thread_end %s, thread id : %lx, tid %lu\n",
126 "reader", pthread_self(), (unsigned long)gettid());
131 void *thr_writer(void *arg
)
134 struct test_array
*new, *old
;
136 printf("thread_begin %s, thread id : %lx, tid %lu\n",
137 "writer", pthread_self(), (unsigned long)gettid());
140 for (i
= 0; i
< WRITE_LOOP
; i
++) {
141 new = malloc(sizeof(struct test_array
));
142 rcu_copy_mutex_lock();
143 old
= test_rcu_pointer
;
148 old
= urcu_publish_content(&test_rcu_pointer
, new);
149 rcu_copy_mutex_unlock();
150 /* can be done after unlock */
158 printf("thread_end %s, thread id : %lx, tid %lu\n",
159 "writer", pthread_self(), (unsigned long)gettid());
166 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
169 cycles_t tot_time
= 0;
171 printf("thread %-6s, thread id : %lx, tid %lu\n",
172 "main", pthread_self(), (unsigned long)gettid());
174 for (i
= 0; i
< NR_READ
; i
++) {
175 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
180 for (i
= 0; i
< NR_WRITE
; i
++) {
181 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
188 for (i
= 0; i
< NR_READ
; i
++) {
189 err
= pthread_join(tid_reader
[i
], &tret
);
192 tot_time
+= reader_time
[i
];
194 for (i
= 0; i
< NR_WRITE
; i
++) {
195 err
= pthread_join(tid_writer
[i
], &tret
);
199 free(test_rcu_pointer
);
200 printf("Time per read : %g cycles\n",
201 (double)tot_time
/ ((double)NR_READ
* (double)READ_LOOP
));