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>
23 #if defined(_syscall0)
24 _syscall0(pid_t
, gettid
)
25 #elif defined(__NR_gettid)
26 static inline pid_t
gettid(void)
28 return syscall(__NR_gettid
);
31 #warning "use pid as tid"
32 static inline pid_t
gettid(void)
41 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
43 void rcu_copy_mutex_lock(void)
46 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
48 perror("Error in pthread mutex lock");
53 void rcu_copy_mutex_unlock(void)
57 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
59 perror("Error in pthread mutex unlock");
68 static struct test_array
*test_rcu_pointer
;
70 #define OUTER_READ_LOOP 2000U
71 #define INNER_READ_LOOP 100000U
72 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
74 #define WRITE_LOOP 2000U
79 static cycles_t reader_time
[NR_READ
] __attribute__((aligned(128)));
81 void *thr_reader(void *arg
)
84 struct test_array
*local_ptr
;
85 cycles_t time1
, time2
;
87 printf("thread_begin %s, thread id : %lx, tid %lu\n",
88 "reader", pthread_self(), (unsigned long)gettid());
91 rcu_register_thread();
94 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
95 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
97 local_ptr
= rcu_dereference(test_rcu_pointer
);
99 assert(local_ptr
->a
== 8);
104 time2
= get_cycles();
106 rcu_unregister_thread();
108 reader_time
[(unsigned long)arg
] = time2
- time1
;
111 printf("thread_end %s, thread id : %lx, tid %lu\n",
112 "reader", pthread_self(), (unsigned long)gettid());
117 void *thr_writer(void *arg
)
120 struct test_array
*new, *old
;
122 printf("thread_begin %s, thread id : %lx, tid %lu\n",
123 "writer", pthread_self(), (unsigned long)gettid());
126 for (i
= 0; i
< WRITE_LOOP
; i
++) {
127 new = malloc(sizeof(struct test_array
));
128 rcu_copy_mutex_lock();
129 old
= test_rcu_pointer
;
134 old
= rcu_publish_content(&test_rcu_pointer
, new);
135 rcu_copy_mutex_unlock();
136 /* can be done after unlock */
144 printf("thread_end %s, thread id : %lx, tid %lu\n",
145 "writer", pthread_self(), (unsigned long)gettid());
152 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
155 cycles_t tot_time
= 0;
157 printf("thread %-6s, thread id : %lx, tid %lu\n",
158 "main", pthread_self(), (unsigned long)gettid());
160 for (i
= 0; i
< NR_READ
; i
++) {
161 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
166 for (i
= 0; i
< NR_WRITE
; i
++) {
167 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
174 for (i
= 0; i
< NR_READ
; i
++) {
175 err
= pthread_join(tid_reader
[i
], &tret
);
178 tot_time
+= reader_time
[i
];
180 for (i
= 0; i
< NR_WRITE
; i
++) {
181 err
= pthread_join(tid_writer
[i
], &tret
);
185 free(test_rcu_pointer
);
186 printf("Time per read : %g cycles\n",
187 (double)tot_time
/ ((double)NR_READ
* (double)READ_LOOP
));