Rename all memory primitives with prefix cmm_
[urcu.git] / tests / 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 int 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 #ifdef TORTURE_QSBR
96 #define mark_rcu_quiescent_state rcu_quiescent_state
97 #define put_thread_offline rcu_thread_offline
98 #define put_thread_online rcu_thread_online
99 #endif
100
101 #ifndef mark_rcu_quiescent_state
102 #define mark_rcu_quiescent_state() do ; while (0)
103 #endif /* #ifdef mark_rcu_quiescent_state */
104
105 #ifndef put_thread_offline
106 #define put_thread_offline() do ; while (0)
107 #define put_thread_online() do ; while (0)
108 #define put_thread_online_delay() do ; while (0)
109 #else /* #ifndef put_thread_offline */
110 #define put_thread_online_delay() synchronize_rcu()
111 #endif /* #else #ifndef put_thread_offline */
112
113 /*
114 * Performance test.
115 */
116
117 void *rcu_read_perf_test(void *arg)
118 {
119 int i;
120 int me = (long)arg;
121 long long n_reads_local = 0;
122
123 rcu_register_thread();
124 run_on(me);
125 uatomic_inc(&nthreadsrunning);
126 while (goflag == GOFLAG_INIT)
127 poll(NULL, 0, 1);
128 mark_rcu_quiescent_state();
129 while (goflag == GOFLAG_RUN) {
130 for (i = 0; i < RCU_READ_RUN; i++) {
131 rcu_read_lock();
132 /* rcu_read_lock_nest(); */
133 /* rcu_read_unlock_nest(); */
134 rcu_read_unlock();
135 }
136 n_reads_local += RCU_READ_RUN;
137 mark_rcu_quiescent_state();
138 }
139 __get_thread_var(n_reads_pt) += n_reads_local;
140 put_thread_offline();
141 rcu_unregister_thread();
142
143 return (NULL);
144 }
145
146 void *rcu_update_perf_test(void *arg)
147 {
148 long long n_updates_local = 0;
149
150 uatomic_inc(&nthreadsrunning);
151 while (goflag == GOFLAG_INIT)
152 poll(NULL, 0, 1);
153 while (goflag == GOFLAG_RUN) {
154 synchronize_rcu();
155 n_updates_local++;
156 }
157 __get_thread_var(n_updates_pt) += n_updates_local;
158 return NULL;
159 }
160
161 void perftestinit(void)
162 {
163 init_per_thread(n_reads_pt, 0LL);
164 init_per_thread(n_updates_pt, 0LL);
165 uatomic_set(&nthreadsrunning, 0);
166 }
167
168 void perftestrun(int nthreads, int nreaders, int nupdaters)
169 {
170 int t;
171 int duration = 1;
172
173 cmm_smp_mb();
174 while (uatomic_read(&nthreadsrunning) < nthreads)
175 poll(NULL, 0, 1);
176 goflag = GOFLAG_RUN;
177 cmm_smp_mb();
178 sleep(duration);
179 cmm_smp_mb();
180 goflag = GOFLAG_STOP;
181 cmm_smp_mb();
182 wait_all_threads();
183 for_each_thread(t) {
184 n_reads += per_thread(n_reads_pt, t);
185 n_updates += per_thread(n_updates_pt, t);
186 }
187 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
188 n_reads, n_updates, nreaders, nupdaters, duration);
189 printf("ns/read: %g ns/update: %g\n",
190 ((duration * 1000*1000*1000.*(double)nreaders) /
191 (double)n_reads),
192 ((duration * 1000*1000*1000.*(double)nupdaters) /
193 (double)n_updates));
194 exit(0);
195 }
196
197 void perftest(int nreaders, int cpustride)
198 {
199 int i;
200 long arg;
201
202 perftestinit();
203 for (i = 0; i < nreaders; i++) {
204 arg = (long)(i * cpustride);
205 create_thread(rcu_read_perf_test, (void *)arg);
206 }
207 arg = (long)(i * cpustride);
208 create_thread(rcu_update_perf_test, (void *)arg);
209 perftestrun(i + 1, nreaders, 1);
210 }
211
212 void rperftest(int nreaders, int cpustride)
213 {
214 int i;
215 long arg;
216
217 perftestinit();
218 init_per_thread(n_reads_pt, 0LL);
219 for (i = 0; i < nreaders; i++) {
220 arg = (long)(i * cpustride);
221 create_thread(rcu_read_perf_test, (void *)arg);
222 }
223 perftestrun(i, nreaders, 0);
224 }
225
226 void uperftest(int nupdaters, int cpustride)
227 {
228 int i;
229 long arg;
230
231 perftestinit();
232 init_per_thread(n_reads_pt, 0LL);
233 for (i = 0; i < nupdaters; i++) {
234 arg = (long)(i * cpustride);
235 create_thread(rcu_update_perf_test, (void *)arg);
236 }
237 perftestrun(i, 0, nupdaters);
238 }
239
240 /*
241 * Stress test.
242 */
243
244 #define RCU_STRESS_PIPE_LEN 10
245
246 struct rcu_stress {
247 int pipe_count;
248 int mbtest;
249 };
250
251 struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0 } };
252 struct rcu_stress *rcu_stress_current;
253 int rcu_stress_idx = 0;
254
255 int n_mberror = 0;
256 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN + 1], rcu_stress_count);
257
258 int garbage = 0;
259
260 void *rcu_read_stress_test(void *arg)
261 {
262 int i;
263 int itercnt = 0;
264 struct rcu_stress *p;
265 int pc;
266
267 rcu_register_thread();
268 while (goflag == GOFLAG_INIT)
269 poll(NULL, 0, 1);
270 mark_rcu_quiescent_state();
271 while (goflag == GOFLAG_RUN) {
272 rcu_read_lock();
273 p = rcu_dereference(rcu_stress_current);
274 if (p->mbtest == 0)
275 n_mberror++;
276 rcu_read_lock_nest();
277 for (i = 0; i < 100; i++)
278 garbage++;
279 rcu_read_unlock_nest();
280 pc = p->pipe_count;
281 rcu_read_unlock();
282 if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0))
283 pc = RCU_STRESS_PIPE_LEN;
284 __get_thread_var(rcu_stress_count)[pc]++;
285 __get_thread_var(n_reads_pt)++;
286 mark_rcu_quiescent_state();
287 if ((++itercnt % 0x1000) == 0) {
288 put_thread_offline();
289 put_thread_online_delay();
290 put_thread_online();
291 }
292 }
293 put_thread_offline();
294 rcu_unregister_thread();
295
296 return (NULL);
297 }
298
299 void *rcu_update_stress_test(void *arg)
300 {
301 int i;
302 struct rcu_stress *p;
303
304 while (goflag == GOFLAG_INIT)
305 poll(NULL, 0, 1);
306 while (goflag == GOFLAG_RUN) {
307 i = rcu_stress_idx + 1;
308 if (i >= RCU_STRESS_PIPE_LEN)
309 i = 0;
310 p = &rcu_stress_array[i];
311 p->mbtest = 0;
312 cmm_smp_mb();
313 p->pipe_count = 0;
314 p->mbtest = 1;
315 rcu_assign_pointer(rcu_stress_current, p);
316 rcu_stress_idx = i;
317 for (i = 0; i < RCU_STRESS_PIPE_LEN; i++)
318 if (i != rcu_stress_idx)
319 rcu_stress_array[i].pipe_count++;
320 synchronize_rcu();
321 n_updates++;
322 }
323 return NULL;
324 }
325
326 void *rcu_fake_update_stress_test(void *arg)
327 {
328 while (goflag == GOFLAG_INIT)
329 poll(NULL, 0, 1);
330 while (goflag == GOFLAG_RUN) {
331 synchronize_rcu();
332 poll(NULL, 0, 1);
333 }
334 return NULL;
335 }
336
337 void stresstest(int nreaders)
338 {
339 int i;
340 int t;
341 long long *p;
342 long long sum;
343
344 init_per_thread(n_reads_pt, 0LL);
345 for_each_thread(t) {
346 p = &per_thread(rcu_stress_count,t)[0];
347 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++)
348 p[i] = 0LL;
349 }
350 rcu_stress_current = &rcu_stress_array[0];
351 rcu_stress_current->pipe_count = 0;
352 rcu_stress_current->mbtest = 1;
353 for (i = 0; i < nreaders; i++)
354 create_thread(rcu_read_stress_test, NULL);
355 create_thread(rcu_update_stress_test, NULL);
356 for (i = 0; i < 5; i++)
357 create_thread(rcu_fake_update_stress_test, NULL);
358 cmm_smp_mb();
359 goflag = GOFLAG_RUN;
360 cmm_smp_mb();
361 sleep(10);
362 cmm_smp_mb();
363 goflag = GOFLAG_STOP;
364 cmm_smp_mb();
365 wait_all_threads();
366 for_each_thread(t)
367 n_reads += per_thread(n_reads_pt, t);
368 printf("n_reads: %lld n_updates: %ld n_mberror: %d\n",
369 n_reads, n_updates, n_mberror);
370 printf("rcu_stress_count:");
371 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
372 sum = 0LL;
373 for_each_thread(t) {
374 sum += per_thread(rcu_stress_count, t)[i];
375 }
376 printf(" %lld", sum);
377 }
378 printf("\n");
379 exit(0);
380 }
381
382 /*
383 * Mainprogram.
384 */
385
386 void usage(int argc, char *argv[])
387 {
388 fprintf(stderr, "Usage: %s [nreaders [ perf | stress ] ]\n", argv[0]);
389 exit(-1);
390 }
391
392 int main(int argc, char *argv[])
393 {
394 int nreaders = 1;
395 int cpustride = 1;
396
397 smp_init();
398 //rcu_init();
399
400 #ifdef DEBUG_YIELD
401 yield_active |= YIELD_READ;
402 yield_active |= YIELD_WRITE;
403 #endif
404
405 if (argc > 1) {
406 nreaders = strtoul(argv[1], NULL, 0);
407 if (argc == 2)
408 perftest(nreaders, cpustride);
409 if (argc > 3)
410 cpustride = strtoul(argv[3], NULL, 0);
411 if (strcmp(argv[2], "perf") == 0)
412 perftest(nreaders, cpustride);
413 else if (strcmp(argv[2], "rperf") == 0)
414 rperftest(nreaders, cpustride);
415 else if (strcmp(argv[2], "uperf") == 0)
416 uperftest(nreaders, cpustride);
417 else if (strcmp(argv[2], "stress") == 0)
418 stresstest(nreaders);
419 usage(argc, argv);
420 }
421 perftest(nreaders, cpustride);
422 return 0;
423 }
This page took 0.03712 seconds and 4 git commands to generate.