Commit | Line | Data |
---|---|---|
8a953620 MD |
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; | |
8a953620 MD |
115 | long long n_reads_local = 0; |
116 | ||
117 | urcu_register_thread(); | |
118 | run_on(me); | |
119 | __atomic_inc(&nthreadsrunning); | |
120 | while (goflag == GOFLAG_INIT) | |
121 | poll(NULL, 0, 1); | |
122 | mark_rcu_quiescent_state(); | |
123 | while (goflag == GOFLAG_RUN) { | |
124 | for (i = 0; i < RCU_READ_RUN; i++) { | |
125 | rcu_read_lock(); | |
126 | /* rcu_read_lock_nest(); */ | |
127 | /* rcu_read_unlock_nest(); */ | |
128 | rcu_read_unlock(); | |
129 | } | |
130 | n_reads_local += RCU_READ_RUN; | |
131 | mark_rcu_quiescent_state(); | |
132 | } | |
133 | __get_thread_var(n_reads_pt) += n_reads_local; | |
134 | put_thread_offline(); | |
135 | urcu_unregister_thread(); | |
136 | ||
137 | return (NULL); | |
138 | } | |
139 | ||
140 | void *rcu_update_perf_test(void *arg) | |
141 | { | |
142 | long long n_updates_local = 0; | |
143 | ||
144 | __atomic_inc(&nthreadsrunning); | |
145 | while (goflag == GOFLAG_INIT) | |
146 | poll(NULL, 0, 1); | |
147 | while (goflag == GOFLAG_RUN) { | |
148 | synchronize_rcu(); | |
149 | n_updates_local++; | |
150 | } | |
151 | __get_thread_var(n_updates_pt) += n_updates_local; | |
b0b31506 | 152 | return NULL; |
8a953620 MD |
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 | ||
b0b31506 | 245 | struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0 } }; |
8a953620 MD |
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 | } | |
b0b31506 | 317 | return NULL; |
8a953620 MD |
318 | } |
319 | ||
320 | void *rcu_fake_update_stress_test(void *arg) | |
321 | { | |
8a953620 MD |
322 | while (goflag == GOFLAG_INIT) |
323 | poll(NULL, 0, 1); | |
324 | while (goflag == GOFLAG_RUN) { | |
325 | synchronize_rcu(); | |
326 | poll(NULL, 0, 1); | |
327 | } | |
b0b31506 | 328 | return NULL; |
8a953620 MD |
329 | } |
330 | ||
331 | void stresstest(int nreaders) | |
332 | { | |
333 | int i; | |
334 | int t; | |
335 | long long *p; | |
336 | long long sum; | |
337 | ||
338 | init_per_thread(n_reads_pt, 0LL); | |
339 | for_each_thread(t) { | |
340 | p = &per_thread(rcu_stress_count,t)[0]; | |
341 | for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) | |
342 | p[i] = 0LL; | |
343 | } | |
344 | rcu_stress_current = &rcu_stress_array[0]; | |
345 | rcu_stress_current->pipe_count = 0; | |
346 | rcu_stress_current->mbtest = 1; | |
347 | for (i = 0; i < nreaders; i++) | |
348 | create_thread(rcu_read_stress_test, NULL); | |
349 | create_thread(rcu_update_stress_test, NULL); | |
350 | for (i = 0; i < 5; i++) | |
351 | create_thread(rcu_fake_update_stress_test, NULL); | |
352 | smp_mb(); | |
353 | goflag = GOFLAG_RUN; | |
354 | smp_mb(); | |
355 | sleep(10); | |
356 | smp_mb(); | |
357 | goflag = GOFLAG_STOP; | |
358 | smp_mb(); | |
359 | wait_all_threads(); | |
360 | for_each_thread(t) | |
361 | n_reads += per_thread(n_reads_pt, t); | |
b0b31506 | 362 | printf("n_reads: %lld n_updates: %ld n_mberror: %d\n", |
8a953620 MD |
363 | n_reads, n_updates, n_mberror); |
364 | printf("rcu_stress_count:"); | |
365 | for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) { | |
366 | sum = 0LL; | |
367 | for_each_thread(t) { | |
368 | sum += per_thread(rcu_stress_count, t)[i]; | |
369 | } | |
370 | printf(" %lld", sum); | |
371 | } | |
372 | printf("\n"); | |
373 | exit(0); | |
374 | } | |
375 | ||
376 | /* | |
377 | * Mainprogram. | |
378 | */ | |
379 | ||
380 | void usage(int argc, char *argv[]) | |
381 | { | |
382 | fprintf(stderr, "Usage: %s [nreaders [ perf | stress ] ]\n", argv[0]); | |
383 | exit(-1); | |
384 | } | |
385 | ||
386 | int main(int argc, char *argv[]) | |
387 | { | |
388 | int nreaders = 1; | |
389 | int cpustride = 1; | |
390 | ||
391 | smp_init(); | |
392 | //rcu_init(); | |
393 | ||
9b171f46 MD |
394 | #ifdef DEBUG_YIELD |
395 | yield_active |= YIELD_READ; | |
396 | yield_active |= YIELD_WRITE; | |
397 | #endif | |
398 | ||
8a953620 MD |
399 | if (argc > 1) { |
400 | nreaders = strtoul(argv[1], NULL, 0); | |
401 | if (argc == 2) | |
402 | perftest(nreaders, cpustride); | |
403 | if (argc > 3) | |
404 | cpustride = strtoul(argv[3], NULL, 0); | |
405 | if (strcmp(argv[2], "perf") == 0) | |
406 | perftest(nreaders, cpustride); | |
407 | else if (strcmp(argv[2], "rperf") == 0) | |
408 | rperftest(nreaders, cpustride); | |
409 | else if (strcmp(argv[2], "uperf") == 0) | |
410 | uperftest(nreaders, cpustride); | |
411 | else if (strcmp(argv[2], "stress") == 0) | |
412 | stresstest(nreaders); | |
413 | usage(argc, argv); | |
414 | } | |
415 | perftest(nreaders, cpustride); | |
416 | } |