Add timing tests
[urcu.git] / test_urcu_timing.c
CommitLineData
20bf310a
MD
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
22#if defined(_syscall0)
23_syscall0(pid_t, gettid)
24#elif defined(__NR_gettid)
25static inline pid_t gettid(void)
26{
27 return syscall(__NR_gettid);
28}
29#else
30#warning "use pid as tid"
31static inline pid_t gettid(void)
32{
33 return getpid();
34}
35#endif
36
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); \
41} while(0)
42
43typedef unsigned long long cycles_t;
44
45static inline cycles_t get_cycles (void)
46{
47 unsigned long long ret = 0;
48
49 rdtscll(ret);
50 return ret;
51}
52
53#include "urcu.h"
54
55struct test_array {
56 int a;
57};
58
59static struct test_array *test_rcu_pointer;
60
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)
64
65#define WRITE_LOOP 2000U
66
67#define NR_READ 10
68#define NR_WRITE 9
69
70static cycles_t reader_time[NR_READ] __attribute__((aligned(128)));
71
72void *thr_reader(void *arg)
73{
74 int qparity, i, j;
75 struct test_array *local_ptr;
76 cycles_t time1, time2;
77
78 printf("thread_begin %s, thread id : %lx, tid %lu\n",
79 "reader", pthread_self(), (unsigned long)gettid());
80 sleep(2);
81
82 urcu_register_thread();
83
84 time1 = get_cycles();
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);
89 if (local_ptr) {
90 assert(local_ptr->a == 8);
91 }
92 rcu_read_unlock(qparity);
93 }
94 }
95 time2 = get_cycles();
96
97 urcu_unregister_thread();
98
99 reader_time[(unsigned long)arg] = time2 - time1;
100
101 sleep(2);
102 printf("thread_end %s, thread id : %lx, tid %lu\n",
103 "reader", pthread_self(), (unsigned long)gettid());
104 return ((void*)1);
105
106}
107
108void *thr_writer(void *arg)
109{
110 int i;
111 struct test_array *new, *old;
112
113 printf("thread_begin %s, thread id : %lx, tid %lu\n",
114 "writer", pthread_self(), (unsigned long)gettid());
115 sleep(2);
116
117 for (i = 0; i < WRITE_LOOP; i++) {
118 new = malloc(sizeof(struct test_array));
119 rcu_write_lock();
120 old = test_rcu_pointer;
121 if (old) {
122 assert(old->a == 8);
123 }
124 new->a = 8;
125 old = urcu_publish_content((void **)&test_rcu_pointer, new);
126 rcu_write_unlock();
127 /* can be done after unlock */
128 if (old) {
129 old->a = 0;
130 }
131 free(old);
132 usleep(1);
133 }
134
135 printf("thread_end %s, thread id : %lx, tid %lu\n",
136 "writer", pthread_self(), (unsigned long)gettid());
137 return ((void*)2);
138}
139
140int main()
141{
142 int err;
143 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
144 void *tret;
145 int i;
146 cycles_t tot_time = 0;
147
148 printf("thread %-6s, thread id : %lx, tid %lu\n",
149 "main", pthread_self(), (unsigned long)gettid());
150
151 for (i = 0; i < NR_READ; i++) {
152 err = pthread_create(&tid_reader[i], NULL, thr_reader,
153 (void *)(long)i);
154 if (err != 0)
155 exit(1);
156 }
157 for (i = 0; i < NR_WRITE; i++) {
158 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
159 if (err != 0)
160 exit(1);
161 }
162
163 sleep(10);
164
165 for (i = 0; i < NR_READ; i++) {
166 err = pthread_join(tid_reader[i], &tret);
167 if (err != 0)
168 exit(1);
169 tot_time += reader_time[i];
170 }
171 for (i = 0; i < NR_WRITE; i++) {
172 err = pthread_join(tid_writer[i], &tret);
173 if (err != 0)
174 exit(1);
175 }
176 free(test_rcu_pointer);
177 printf("Time per read : %g cycles\n",
178 (double)tot_time / ((double)NR_READ * (double)READ_LOOP));
179
180 return 0;
181}
This page took 0.027316 seconds and 4 git commands to generate.