Add rcutorture with yield
[urcu.git] / rcutorture.h
1 /*
2 * rcutorture.h: simple user-level performance/stress test of RCU.
3 *
4 * Usage:
5 * ./rcu <nreaders> rperf [ <cpustride> ]
6 * Run a read-side performance test with the specified
7 * number of readers spaced by <cpustride>.
8 * Thus "./rcu 16 rperf 2" would run 16 readers on even-numbered
9 * CPUs from 0 to 30.
10 * ./rcu <nupdaters> uperf [ <cpustride> ]
11 * Run an update-side performance test with the specified
12 * number of updaters and specified CPU spacing.
13 * ./rcu <nreaders> perf [ <cpustride> ]
14 * Run a combined read/update performance test with the specified
15 * number of readers and one updater and specified CPU spacing.
16 * The readers run on the low-numbered CPUs and the updater
17 * of the highest-numbered CPU.
18 *
19 * The above tests produce output as follows:
20 *
21 * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1
22 * ns/read: 43.4707 ns/update: 6848.1
23 *
24 * The first line lists the total number of RCU reads and updates executed
25 * during the test, the number of reader threads, the number of updater
26 * threads, and the duration of the test in seconds. The second line
27 * lists the average duration of each type of operation in nanoseconds,
28 * or "nan" if the corresponding type of operation was not performed.
29 *
30 * ./rcu <nreaders> stress
31 * Run a stress test with the specified number of readers and
32 * one updater. None of the threads are affinitied to any
33 * particular CPU.
34 *
35 * This test produces output as follows:
36 *
37 * n_reads: 114633217 n_updates: 3903415 n_mberror: 0
38 * rcu_stress_count: 114618391 14826 0 0 0 0 0 0 0 0 0
39 *
40 * The first line lists the number of RCU read and update operations
41 * executed, followed by the number of memory-ordering violations
42 * (which will be zero in a correct RCU implementation). The second
43 * line lists the number of readers observing progressively more stale
44 * data. A correct RCU implementation will have all but the first two
45 * numbers non-zero.
46 *
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
50 * (at your option) any later version.
51 *
52 * This program is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 * GNU General Public License for more details.
56 *
57 * You should have received a copy of the GNU General Public License
58 * along with this program; if not, write to the Free Software
59 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
60 *
61 * Copyright (c) 2008 Paul E. McKenney, IBM Corporation.
62 */
63
64 /*
65 * Test variables.
66 */
67
68 DEFINE_PER_THREAD(long long, n_reads_pt);
69 DEFINE_PER_THREAD(long long, n_updates_pt);
70
71 long long n_reads = 0LL;
72 long n_updates = 0L;
73 atomic_t nthreadsrunning;
74 char argsbuf[64];
75
76 #define GOFLAG_INIT 0
77 #define GOFLAG_RUN 1
78 #define GOFLAG_STOP 2
79
80 int goflag __attribute__((__aligned__(CACHE_LINE_SIZE))) = GOFLAG_INIT;
81
82 #define RCU_READ_RUN 1000
83
84 //MD
85 #define RCU_READ_NESTABLE
86
87 #ifdef RCU_READ_NESTABLE
88 #define rcu_read_lock_nest() rcu_read_lock()
89 #define rcu_read_unlock_nest() rcu_read_unlock()
90 #else /* #ifdef RCU_READ_NESTABLE */
91 #define rcu_read_lock_nest()
92 #define rcu_read_unlock_nest()
93 #endif /* #else #ifdef RCU_READ_NESTABLE */
94
95 #ifndef mark_rcu_quiescent_state
96 #define mark_rcu_quiescent_state() do ; while (0)
97 #endif /* #ifdef mark_rcu_quiescent_state */
98
99 #ifndef put_thread_offline
100 #define put_thread_offline() do ; while (0)
101 #define put_thread_online() do ; while (0)
102 #define put_thread_online_delay() do ; while (0)
103 #else /* #ifndef put_thread_offline */
104 #define put_thread_online_delay() synchronize_rcu()
105 #endif /* #else #ifndef put_thread_offline */
106
107 /*
108 * Performance test.
109 */
110
111 void *rcu_read_perf_test(void *arg)
112 {
113 int i;
114 int me = (long)arg;
115 cpu_set_t mask;
116 long long n_reads_local = 0;
117
118 urcu_register_thread();
119 run_on(me);
120 __atomic_inc(&nthreadsrunning);
121 while (goflag == GOFLAG_INIT)
122 poll(NULL, 0, 1);
123 mark_rcu_quiescent_state();
124 while (goflag == GOFLAG_RUN) {
125 for (i = 0; i < RCU_READ_RUN; i++) {
126 rcu_read_lock();
127 /* rcu_read_lock_nest(); */
128 /* rcu_read_unlock_nest(); */
129 rcu_read_unlock();
130 }
131 n_reads_local += RCU_READ_RUN;
132 mark_rcu_quiescent_state();
133 }
134 __get_thread_var(n_reads_pt) += n_reads_local;
135 put_thread_offline();
136 urcu_unregister_thread();
137
138 return (NULL);
139 }
140
141 void *rcu_update_perf_test(void *arg)
142 {
143 long long n_updates_local = 0;
144
145 __atomic_inc(&nthreadsrunning);
146 while (goflag == GOFLAG_INIT)
147 poll(NULL, 0, 1);
148 while (goflag == GOFLAG_RUN) {
149 synchronize_rcu();
150 n_updates_local++;
151 }
152 __get_thread_var(n_updates_pt) += n_updates_local;
153 }
154
155 void perftestinit(void)
156 {
157 init_per_thread(n_reads_pt, 0LL);
158 init_per_thread(n_updates_pt, 0LL);
159 atomic_set(&nthreadsrunning, 0);
160 }
161
162 void perftestrun(int nthreads, int nreaders, int nupdaters)
163 {
164 int t;
165 int duration = 1;
166
167 smp_mb();
168 while (atomic_read(&nthreadsrunning) < nthreads)
169 poll(NULL, 0, 1);
170 goflag = GOFLAG_RUN;
171 smp_mb();
172 sleep(duration);
173 smp_mb();
174 goflag = GOFLAG_STOP;
175 smp_mb();
176 wait_all_threads();
177 for_each_thread(t) {
178 n_reads += per_thread(n_reads_pt, t);
179 n_updates += per_thread(n_updates_pt, t);
180 }
181 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
182 n_reads, n_updates, nreaders, nupdaters, duration);
183 printf("ns/read: %g ns/update: %g\n",
184 ((duration * 1000*1000*1000.*(double)nreaders) /
185 (double)n_reads),
186 ((duration * 1000*1000*1000.*(double)nupdaters) /
187 (double)n_updates));
188 exit(0);
189 }
190
191 void perftest(int nreaders, int cpustride)
192 {
193 int i;
194 long arg;
195
196 perftestinit();
197 for (i = 0; i < nreaders; i++) {
198 arg = (long)(i * cpustride);
199 create_thread(rcu_read_perf_test, (void *)arg);
200 }
201 arg = (long)(i * cpustride);
202 create_thread(rcu_update_perf_test, (void *)arg);
203 perftestrun(i + 1, nreaders, 1);
204 }
205
206 void rperftest(int nreaders, int cpustride)
207 {
208 int i;
209 long arg;
210
211 perftestinit();
212 init_per_thread(n_reads_pt, 0LL);
213 for (i = 0; i < nreaders; i++) {
214 arg = (long)(i * cpustride);
215 create_thread(rcu_read_perf_test, (void *)arg);
216 }
217 perftestrun(i, nreaders, 0);
218 }
219
220 void uperftest(int nupdaters, int cpustride)
221 {
222 int i;
223 long arg;
224
225 perftestinit();
226 init_per_thread(n_reads_pt, 0LL);
227 for (i = 0; i < nupdaters; i++) {
228 arg = (long)(i * cpustride);
229 create_thread(rcu_update_perf_test, (void *)arg);
230 }
231 perftestrun(i, 0, nupdaters);
232 }
233
234 /*
235 * Stress test.
236 */
237
238 #define RCU_STRESS_PIPE_LEN 10
239
240 struct rcu_stress {
241 int pipe_count;
242 int mbtest;
243 };
244
245 struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { 0 };
246 struct rcu_stress *rcu_stress_current;
247 int rcu_stress_idx = 0;
248
249 int n_mberror = 0;
250 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN + 1], rcu_stress_count);
251
252 int garbage = 0;
253
254 void *rcu_read_stress_test(void *arg)
255 {
256 int i;
257 int itercnt = 0;
258 struct rcu_stress *p;
259 int pc;
260
261 urcu_register_thread();
262 while (goflag == GOFLAG_INIT)
263 poll(NULL, 0, 1);
264 mark_rcu_quiescent_state();
265 while (goflag == GOFLAG_RUN) {
266 rcu_read_lock();
267 p = rcu_dereference(rcu_stress_current);
268 if (p->mbtest == 0)
269 n_mberror++;
270 rcu_read_lock_nest();
271 for (i = 0; i < 100; i++)
272 garbage++;
273 rcu_read_unlock_nest();
274 pc = p->pipe_count;
275 rcu_read_unlock();
276 if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0))
277 pc = RCU_STRESS_PIPE_LEN;
278 __get_thread_var(rcu_stress_count)[pc]++;
279 __get_thread_var(n_reads_pt)++;
280 mark_rcu_quiescent_state();
281 if ((++itercnt % 0x1000) == 0) {
282 put_thread_offline();
283 put_thread_online_delay();
284 put_thread_online();
285 }
286 }
287 put_thread_offline();
288 urcu_unregister_thread();
289
290 return (NULL);
291 }
292
293 void *rcu_update_stress_test(void *arg)
294 {
295 int i;
296 struct rcu_stress *p;
297
298 while (goflag == GOFLAG_INIT)
299 poll(NULL, 0, 1);
300 while (goflag == GOFLAG_RUN) {
301 i = rcu_stress_idx + 1;
302 if (i >= RCU_STRESS_PIPE_LEN)
303 i = 0;
304 p = &rcu_stress_array[i];
305 p->mbtest = 0;
306 smp_mb();
307 p->pipe_count = 0;
308 p->mbtest = 1;
309 rcu_assign_pointer(rcu_stress_current, p);
310 rcu_stress_idx = i;
311 for (i = 0; i < RCU_STRESS_PIPE_LEN; i++)
312 if (i != rcu_stress_idx)
313 rcu_stress_array[i].pipe_count++;
314 synchronize_rcu();
315 n_updates++;
316 }
317 }
318
319 void *rcu_fake_update_stress_test(void *arg)
320 {
321 int i;
322 struct rcu_stress *p;
323
324 while (goflag == GOFLAG_INIT)
325 poll(NULL, 0, 1);
326 while (goflag == GOFLAG_RUN) {
327 synchronize_rcu();
328 poll(NULL, 0, 1);
329 }
330 }
331
332 void stresstest(int nreaders)
333 {
334 int i;
335 int t;
336 long long *p;
337 long long sum;
338
339 init_per_thread(n_reads_pt, 0LL);
340 for_each_thread(t) {
341 p = &per_thread(rcu_stress_count,t)[0];
342 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++)
343 p[i] = 0LL;
344 }
345 rcu_stress_current = &rcu_stress_array[0];
346 rcu_stress_current->pipe_count = 0;
347 rcu_stress_current->mbtest = 1;
348 for (i = 0; i < nreaders; i++)
349 create_thread(rcu_read_stress_test, NULL);
350 create_thread(rcu_update_stress_test, NULL);
351 for (i = 0; i < 5; i++)
352 create_thread(rcu_fake_update_stress_test, NULL);
353 smp_mb();
354 goflag = GOFLAG_RUN;
355 smp_mb();
356 sleep(10);
357 smp_mb();
358 goflag = GOFLAG_STOP;
359 smp_mb();
360 wait_all_threads();
361 for_each_thread(t)
362 n_reads += per_thread(n_reads_pt, t);
363 printf("n_reads: %lld n_updates: %ld n_mberror: %ld\n",
364 n_reads, n_updates, n_mberror);
365 printf("rcu_stress_count:");
366 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
367 sum = 0LL;
368 for_each_thread(t) {
369 sum += per_thread(rcu_stress_count, t)[i];
370 }
371 printf(" %lld", sum);
372 }
373 printf("\n");
374 exit(0);
375 }
376
377 /*
378 * Mainprogram.
379 */
380
381 void usage(int argc, char *argv[])
382 {
383 fprintf(stderr, "Usage: %s [nreaders [ perf | stress ] ]\n", argv[0]);
384 exit(-1);
385 }
386
387 int main(int argc, char *argv[])
388 {
389 int nreaders = 1;
390 int cpustride = 1;
391
392 smp_init();
393 //rcu_init();
394
395 #ifdef DEBUG_YIELD
396 yield_active |= YIELD_READ;
397 yield_active |= YIELD_WRITE;
398 #endif
399
400 if (argc > 1) {
401 nreaders = strtoul(argv[1], NULL, 0);
402 if (argc == 2)
403 perftest(nreaders, cpustride);
404 if (argc > 3)
405 cpustride = strtoul(argv[3], NULL, 0);
406 if (strcmp(argv[2], "perf") == 0)
407 perftest(nreaders, cpustride);
408 else if (strcmp(argv[2], "rperf") == 0)
409 rperftest(nreaders, cpustride);
410 else if (strcmp(argv[2], "uperf") == 0)
411 uperftest(nreaders, cpustride);
412 else if (strcmp(argv[2], "stress") == 0)
413 stresstest(nreaders);
414 usage(argc, argv);
415 }
416 perftest(nreaders, cpustride);
417 }
This page took 0.037273 seconds and 4 git commands to generate.