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)__a) | (((unsigned long)__d)<<32); \
43 typedef unsigned long long cycles_t
;
45 static inline cycles_t
get_cycles (void)
47 unsigned long long ret
= 0;
59 static struct test_array
*test_rcu_pointer
;
61 #define OUTER_READ_LOOP 2000U
62 #define INNER_READ_LOOP 100000U
63 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
65 #define WRITE_LOOP 2000U
70 static cycles_t reader_time
[NR_READ
] __attribute__((aligned(128)));
72 void *thr_reader(void *arg
)
75 struct test_array
*local_ptr
;
76 cycles_t time1
, time2
;
78 printf("thread_begin %s, thread id : %lx, tid %lu\n",
79 "reader", pthread_self(), (unsigned long)gettid());
82 urcu_register_thread();
85 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
86 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
87 qparity
= rcu_read_lock();
88 local_ptr
= rcu_dereference(test_rcu_pointer
);
90 assert(local_ptr
->a
== 8);
92 rcu_read_unlock(qparity
);
97 urcu_unregister_thread();
99 reader_time
[(unsigned long)arg
] = time2
- time1
;
102 printf("thread_end %s, thread id : %lx, tid %lu\n",
103 "reader", pthread_self(), (unsigned long)gettid());
108 void *thr_writer(void *arg
)
111 struct test_array
*new, *old
;
113 printf("thread_begin %s, thread id : %lx, tid %lu\n",
114 "writer", pthread_self(), (unsigned long)gettid());
117 for (i
= 0; i
< WRITE_LOOP
; i
++) {
118 new = malloc(sizeof(struct test_array
));
120 old
= test_rcu_pointer
;
125 old
= urcu_publish_content((void **)&test_rcu_pointer
, new);
127 /* can be done after unlock */
135 printf("thread_end %s, thread id : %lx, tid %lu\n",
136 "writer", pthread_self(), (unsigned long)gettid());
143 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
146 cycles_t tot_time
= 0;
148 printf("thread %-6s, thread id : %lx, tid %lu\n",
149 "main", pthread_self(), (unsigned long)gettid());
151 for (i
= 0; i
< NR_READ
; i
++) {
152 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
157 for (i
= 0; i
< NR_WRITE
; i
++) {
158 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
165 for (i
= 0; i
< NR_READ
; i
++) {
166 err
= pthread_join(tid_reader
[i
], &tret
);
169 tot_time
+= reader_time
[i
];
171 for (i
= 0; i
< NR_WRITE
; i
++) {
172 err
= pthread_join(tid_writer
[i
], &tret
);
176 free(test_rcu_pointer
);
177 printf("Time per read : %g cycles\n",
178 (double)tot_time
/ ((double)NR_READ
* (double)READ_LOOP
));
This page took 0.039772 seconds and 5 git commands to generate.