1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018 Arm Limited
9 #include <rte_rcu_qsbr.h>
11 #include <rte_hash_crc.h>
12 #include <rte_malloc.h>
13 #include <rte_cycles.h>
18 /* Check condition and return an error if true. */
19 #define TEST_RCU_MAX_LCORE 128
20 static uint16_t enabled_core_ids
[TEST_RCU_MAX_LCORE
];
21 static uint8_t num_cores
;
23 static uint32_t *keys
;
24 #define TOTAL_ENTRY (1024 * 8)
25 #define COUNTER_VALUE 4096
26 static uint32_t *hash_data
[TEST_RCU_MAX_LCORE
][TOTAL_ENTRY
];
27 static volatile uint8_t writer_done
;
28 static volatile uint8_t all_registered
;
29 static volatile uint32_t thr_id
;
31 static struct rte_rcu_qsbr
*t
[TEST_RCU_MAX_LCORE
];
32 static struct rte_hash
*h
[TEST_RCU_MAX_LCORE
];
33 static char hash_name
[TEST_RCU_MAX_LCORE
][8];
34 static rte_atomic64_t updates
, checks
;
35 static rte_atomic64_t update_cycles
, check_cycles
;
37 /* Scale down results to 1000 operations to support lower
40 #define RCU_SCALE_DOWN 1000
42 /* Simple way to allocate thread ids in 0 to TEST_RCU_MAX_LCORE space */
43 static inline uint32_t
48 tmp_thr_id
= __atomic_fetch_add(&thr_id
, 1, __ATOMIC_RELAXED
);
49 if (tmp_thr_id
>= TEST_RCU_MAX_LCORE
)
50 printf("Invalid thread id %u\n", tmp_thr_id
);
56 get_enabled_cores_mask(void)
59 uint32_t max_cores
= rte_lcore_count();
61 if (max_cores
> TEST_RCU_MAX_LCORE
) {
62 printf("Number of cores exceed %d\n", TEST_RCU_MAX_LCORE
);
68 RTE_LCORE_FOREACH_SLAVE(core_id
) {
69 enabled_core_ids
[num_cores
] = core_id
;
77 test_rcu_qsbr_reader_perf(void *arg
)
79 bool writer_present
= (bool)arg
;
80 uint32_t thread_id
= alloc_thread_id();
81 uint64_t loop_cnt
= 0;
82 uint64_t begin
, cycles
;
84 /* Register for report QS */
85 rte_rcu_qsbr_thread_register(t
[0], thread_id
);
86 /* Make the thread online */
87 rte_rcu_qsbr_thread_online(t
[0], thread_id
);
89 begin
= rte_rdtsc_precise();
92 while (!writer_done
) {
93 /* Update quiescent state counter */
94 rte_rcu_qsbr_quiescent(t
[0], thread_id
);
98 while (loop_cnt
< 100000000) {
99 /* Update quiescent state counter */
100 rte_rcu_qsbr_quiescent(t
[0], thread_id
);
105 cycles
= rte_rdtsc_precise() - begin
;
106 rte_atomic64_add(&update_cycles
, cycles
);
107 rte_atomic64_add(&updates
, loop_cnt
);
109 /* Make the thread offline */
110 rte_rcu_qsbr_thread_offline(t
[0], thread_id
);
111 /* Unregister before exiting to avoid writer from waiting */
112 rte_rcu_qsbr_thread_unregister(t
[0], thread_id
);
118 test_rcu_qsbr_writer_perf(void *arg
)
120 bool wait
= (bool)arg
;
122 uint64_t loop_cnt
= 0;
123 uint64_t begin
, cycles
;
125 begin
= rte_rdtsc_precise();
128 /* Start the quiescent state query process */
130 token
= rte_rcu_qsbr_start(t
[0]);
132 /* Check quiescent state status */
133 rte_rcu_qsbr_check(t
[0], token
, wait
);
135 } while (loop_cnt
< 20000000);
137 cycles
= rte_rdtsc_precise() - begin
;
138 rte_atomic64_add(&check_cycles
, cycles
);
139 rte_atomic64_add(&checks
, loop_cnt
);
144 * Perf test: Reader/writer
145 * Single writer, Multiple Readers, Single QS var, Non-Blocking rcu_qsbr_check
148 test_rcu_qsbr_perf(void)
155 rte_atomic64_clear(&updates
);
156 rte_atomic64_clear(&update_cycles
);
157 rte_atomic64_clear(&checks
);
158 rte_atomic64_clear(&check_cycles
);
160 printf("\nPerf Test: %d Readers/1 Writer('wait' in qsbr_check == true)\n",
163 __atomic_store_n(&thr_id
, 0, __ATOMIC_SEQ_CST
);
165 if (all_registered
== 1)
166 tmp_num_cores
= num_cores
- 1;
168 tmp_num_cores
= TEST_RCU_MAX_LCORE
;
170 sz
= rte_rcu_qsbr_get_memsize(tmp_num_cores
);
171 t
[0] = (struct rte_rcu_qsbr
*)rte_zmalloc("rcu0", sz
,
172 RTE_CACHE_LINE_SIZE
);
173 /* QS variable is initialized */
174 rte_rcu_qsbr_init(t
[0], tmp_num_cores
);
176 /* Reader threads are launched */
177 for (i
= 0; i
< num_cores
- 1; i
++)
178 rte_eal_remote_launch(test_rcu_qsbr_reader_perf
, (void *)1,
179 enabled_core_ids
[i
]);
181 /* Writer thread is launched */
182 rte_eal_remote_launch(test_rcu_qsbr_writer_perf
,
183 (void *)1, enabled_core_ids
[i
]);
185 /* Wait for the writer thread */
186 rte_eal_wait_lcore(enabled_core_ids
[i
]);
189 /* Wait until all readers have exited */
190 rte_eal_mp_wait_lcore();
192 printf("Total RCU updates = %"PRIi64
"\n", rte_atomic64_read(&updates
));
193 printf("Cycles per %d updates: %"PRIi64
"\n", RCU_SCALE_DOWN
,
194 rte_atomic64_read(&update_cycles
) /
195 (rte_atomic64_read(&updates
) / RCU_SCALE_DOWN
));
196 printf("Total RCU checks = %"PRIi64
"\n", rte_atomic64_read(&checks
));
197 printf("Cycles per %d checks: %"PRIi64
"\n", RCU_SCALE_DOWN
,
198 rte_atomic64_read(&check_cycles
) /
199 (rte_atomic64_read(&checks
) / RCU_SCALE_DOWN
));
208 * Single writer, Multiple readers, Single QS variable
211 test_rcu_qsbr_rperf(void)
216 rte_atomic64_clear(&updates
);
217 rte_atomic64_clear(&update_cycles
);
219 __atomic_store_n(&thr_id
, 0, __ATOMIC_SEQ_CST
);
221 printf("\nPerf Test: %d Readers\n", num_cores
);
223 if (all_registered
== 1)
224 tmp_num_cores
= num_cores
;
226 tmp_num_cores
= TEST_RCU_MAX_LCORE
;
228 sz
= rte_rcu_qsbr_get_memsize(tmp_num_cores
);
229 t
[0] = (struct rte_rcu_qsbr
*)rte_zmalloc("rcu0", sz
,
230 RTE_CACHE_LINE_SIZE
);
231 /* QS variable is initialized */
232 rte_rcu_qsbr_init(t
[0], tmp_num_cores
);
234 /* Reader threads are launched */
235 for (i
= 0; i
< num_cores
; i
++)
236 rte_eal_remote_launch(test_rcu_qsbr_reader_perf
, NULL
,
237 enabled_core_ids
[i
]);
239 /* Wait until all readers have exited */
240 rte_eal_mp_wait_lcore();
242 printf("Total RCU updates = %"PRIi64
"\n", rte_atomic64_read(&updates
));
243 printf("Cycles per %d updates: %"PRIi64
"\n", RCU_SCALE_DOWN
,
244 rte_atomic64_read(&update_cycles
) /
245 (rte_atomic64_read(&updates
) / RCU_SCALE_DOWN
));
254 * Multiple writer, Single QS variable, Non-blocking rcu_qsbr_check
257 test_rcu_qsbr_wperf(void)
261 rte_atomic64_clear(&checks
);
262 rte_atomic64_clear(&check_cycles
);
264 __atomic_store_n(&thr_id
, 0, __ATOMIC_SEQ_CST
);
266 printf("\nPerf test: %d Writers ('wait' in qsbr_check == false)\n",
269 /* Number of readers does not matter for QS variable in this test
270 * case as no reader will be registered.
272 sz
= rte_rcu_qsbr_get_memsize(TEST_RCU_MAX_LCORE
);
273 t
[0] = (struct rte_rcu_qsbr
*)rte_zmalloc("rcu0", sz
,
274 RTE_CACHE_LINE_SIZE
);
275 /* QS variable is initialized */
276 rte_rcu_qsbr_init(t
[0], TEST_RCU_MAX_LCORE
);
278 /* Writer threads are launched */
279 for (i
= 0; i
< num_cores
; i
++)
280 rte_eal_remote_launch(test_rcu_qsbr_writer_perf
,
281 (void *)0, enabled_core_ids
[i
]);
283 /* Wait until all readers have exited */
284 rte_eal_mp_wait_lcore();
286 printf("Total RCU checks = %"PRIi64
"\n", rte_atomic64_read(&checks
));
287 printf("Cycles per %d checks: %"PRIi64
"\n", RCU_SCALE_DOWN
,
288 rte_atomic64_read(&check_cycles
) /
289 (rte_atomic64_read(&checks
) / RCU_SCALE_DOWN
));
297 * RCU test cases using rte_hash data structure.
300 test_rcu_qsbr_hash_reader(void *arg
)
302 struct rte_rcu_qsbr
*temp
;
303 struct rte_hash
*hash
= NULL
;
305 uint64_t loop_cnt
= 0;
306 uint64_t begin
, cycles
;
307 uint32_t thread_id
= alloc_thread_id();
308 uint8_t read_type
= (uint8_t)((uintptr_t)arg
);
314 rte_rcu_qsbr_thread_register(temp
, thread_id
);
316 begin
= rte_rdtsc_precise();
319 rte_rcu_qsbr_thread_online(temp
, thread_id
);
320 for (i
= 0; i
< TOTAL_ENTRY
; i
++) {
321 rte_rcu_qsbr_lock(temp
, thread_id
);
322 if (rte_hash_lookup_data(hash
, keys
+i
,
323 (void **)&pdata
) != -ENOENT
) {
325 while (*pdata
< COUNTER_VALUE
)
328 rte_rcu_qsbr_unlock(temp
, thread_id
);
330 /* Update quiescent state counter */
331 rte_rcu_qsbr_quiescent(temp
, thread_id
);
332 rte_rcu_qsbr_thread_offline(temp
, thread_id
);
334 } while (!writer_done
);
336 cycles
= rte_rdtsc_precise() - begin
;
337 rte_atomic64_add(&update_cycles
, cycles
);
338 rte_atomic64_add(&updates
, loop_cnt
);
340 rte_rcu_qsbr_thread_unregister(temp
, thread_id
);
345 static struct rte_hash
*
346 init_hash(int hash_id
)
349 struct rte_hash
*h
= NULL
;
351 sprintf(hash_name
[hash_id
], "hash%d", hash_id
);
352 struct rte_hash_parameters hash_params
= {
353 .entries
= TOTAL_ENTRY
,
354 .key_len
= sizeof(uint32_t),
355 .hash_func_init_val
= 0,
356 .socket_id
= rte_socket_id(),
357 .hash_func
= rte_hash_crc
,
359 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF
,
360 .name
= hash_name
[hash_id
],
363 h
= rte_hash_create(&hash_params
);
365 printf("Hash create Failed\n");
369 for (i
= 0; i
< TOTAL_ENTRY
; i
++) {
370 hash_data
[hash_id
][i
] = rte_zmalloc(NULL
, sizeof(uint32_t), 0);
371 if (hash_data
[hash_id
][i
] == NULL
) {
372 printf("No memory\n");
376 keys
= rte_malloc(NULL
, sizeof(uint32_t) * TOTAL_ENTRY
, 0);
378 printf("No memory\n");
382 for (i
= 0; i
< TOTAL_ENTRY
; i
++)
385 for (i
= 0; i
< TOTAL_ENTRY
; i
++) {
386 if (rte_hash_add_key_data(h
, keys
+ i
,
387 (void *)((uintptr_t)hash_data
[hash_id
][i
]))
389 printf("Hash key add Failed #%d\n", i
);
398 * Single writer, Single QS variable Single QSBR query, Blocking rcu_qsbr_check
401 test_rcu_qsbr_sw_sv_1qs(void)
403 uint64_t token
, begin
, cycles
;
404 int i
, tmp_num_cores
, sz
;
409 rte_atomic64_clear(&updates
);
410 rte_atomic64_clear(&update_cycles
);
411 rte_atomic64_clear(&checks
);
412 rte_atomic64_clear(&check_cycles
);
414 __atomic_store_n(&thr_id
, 0, __ATOMIC_SEQ_CST
);
416 printf("\nPerf test: 1 writer, %d readers, 1 QSBR variable, 1 QSBR Query, Blocking QSBR Check\n", num_cores
);
418 if (all_registered
== 1)
419 tmp_num_cores
= num_cores
;
421 tmp_num_cores
= TEST_RCU_MAX_LCORE
;
423 sz
= rte_rcu_qsbr_get_memsize(tmp_num_cores
);
424 t
[0] = (struct rte_rcu_qsbr
*)rte_zmalloc("rcu0", sz
,
425 RTE_CACHE_LINE_SIZE
);
426 /* QS variable is initialized */
427 rte_rcu_qsbr_init(t
[0], tmp_num_cores
);
429 /* Shared data structure created */
432 printf("Hash init failed\n");
436 /* Reader threads are launched */
437 for (i
= 0; i
< num_cores
; i
++)
438 rte_eal_remote_launch(test_rcu_qsbr_hash_reader
, NULL
,
439 enabled_core_ids
[i
]);
441 begin
= rte_rdtsc_precise();
443 for (i
= 0; i
< TOTAL_ENTRY
; i
++) {
444 /* Delete elements from the shared data structure */
445 pos
= rte_hash_del_key(h
[0], keys
+ i
);
447 printf("Delete key failed #%d\n", keys
[i
]);
450 /* Start the quiescent state query process */
451 token
= rte_rcu_qsbr_start(t
[0]);
453 /* Check the quiescent state status */
454 rte_rcu_qsbr_check(t
[0], token
, true);
455 if (*hash_data
[0][i
] != COUNTER_VALUE
&&
456 *hash_data
[0][i
] != 0) {
457 printf("Reader did not complete #%d = %d\n", i
,
462 if (rte_hash_free_key_with_position(h
[0], pos
) < 0) {
463 printf("Failed to free the key #%d\n", keys
[i
]);
466 rte_free(hash_data
[0][i
]);
467 hash_data
[0][i
] = NULL
;
470 cycles
= rte_rdtsc_precise() - begin
;
471 rte_atomic64_add(&check_cycles
, cycles
);
472 rte_atomic64_add(&checks
, i
);
476 /* Wait until all readers have exited */
477 rte_eal_mp_wait_lcore();
478 /* Check return value from threads */
479 for (i
= 0; i
< num_cores
; i
++)
480 if (lcore_config
[enabled_core_ids
[i
]].ret
< 0)
485 printf("Following numbers include calls to rte_hash functions\n");
486 printf("Cycles per 1 update(online/update/offline): %"PRIi64
"\n",
487 rte_atomic64_read(&update_cycles
) /
488 rte_atomic64_read(&updates
));
490 printf("Cycles per 1 check(start, check): %"PRIi64
"\n\n",
491 rte_atomic64_read(&check_cycles
) /
492 rte_atomic64_read(&checks
));
500 /* Wait until all readers have exited */
501 rte_eal_mp_wait_lcore();
505 for (i
= 0; i
< TOTAL_ENTRY
; i
++)
506 rte_free(hash_data
[0][i
]);
515 * Single writer, Single QS variable, Single QSBR query,
516 * Non-blocking rcu_qsbr_check
519 test_rcu_qsbr_sw_sv_1qs_non_blocking(void)
521 uint64_t token
, begin
, cycles
;
522 int i
, ret
, tmp_num_cores
, sz
;
527 printf("Perf test: 1 writer, %d readers, 1 QSBR variable, 1 QSBR Query, Non-Blocking QSBR check\n", num_cores
);
529 __atomic_store_n(&thr_id
, 0, __ATOMIC_SEQ_CST
);
531 if (all_registered
== 1)
532 tmp_num_cores
= num_cores
;
534 tmp_num_cores
= TEST_RCU_MAX_LCORE
;
536 sz
= rte_rcu_qsbr_get_memsize(tmp_num_cores
);
537 t
[0] = (struct rte_rcu_qsbr
*)rte_zmalloc("rcu0", sz
,
538 RTE_CACHE_LINE_SIZE
);
539 /* QS variable is initialized */
540 rte_rcu_qsbr_init(t
[0], tmp_num_cores
);
542 /* Shared data structure created */
545 printf("Hash init failed\n");
549 /* Reader threads are launched */
550 for (i
= 0; i
< num_cores
; i
++)
551 rte_eal_remote_launch(test_rcu_qsbr_hash_reader
, NULL
,
552 enabled_core_ids
[i
]);
554 begin
= rte_rdtsc_precise();
556 for (i
= 0; i
< TOTAL_ENTRY
; i
++) {
557 /* Delete elements from the shared data structure */
558 pos
= rte_hash_del_key(h
[0], keys
+ i
);
560 printf("Delete key failed #%d\n", keys
[i
]);
563 /* Start the quiescent state query process */
564 token
= rte_rcu_qsbr_start(t
[0]);
566 /* Check the quiescent state status */
568 ret
= rte_rcu_qsbr_check(t
[0], token
, false);
570 if (*hash_data
[0][i
] != COUNTER_VALUE
&&
571 *hash_data
[0][i
] != 0) {
572 printf("Reader did not complete #%d = %d\n", i
,
577 if (rte_hash_free_key_with_position(h
[0], pos
) < 0) {
578 printf("Failed to free the key #%d\n", keys
[i
]);
581 rte_free(hash_data
[0][i
]);
582 hash_data
[0][i
] = NULL
;
585 cycles
= rte_rdtsc_precise() - begin
;
586 rte_atomic64_add(&check_cycles
, cycles
);
587 rte_atomic64_add(&checks
, i
);
590 /* Wait until all readers have exited */
591 rte_eal_mp_wait_lcore();
592 /* Check return value from threads */
593 for (i
= 0; i
< num_cores
; i
++)
594 if (lcore_config
[enabled_core_ids
[i
]].ret
< 0)
599 printf("Following numbers include calls to rte_hash functions\n");
600 printf("Cycles per 1 update(online/update/offline): %"PRIi64
"\n",
601 rte_atomic64_read(&update_cycles
) /
602 rte_atomic64_read(&updates
));
604 printf("Cycles per 1 check(start, check): %"PRIi64
"\n\n",
605 rte_atomic64_read(&check_cycles
) /
606 rte_atomic64_read(&checks
));
614 /* Wait until all readers have exited */
615 rte_eal_mp_wait_lcore();
619 for (i
= 0; i
< TOTAL_ENTRY
; i
++)
620 rte_free(hash_data
[0][i
]);
628 test_rcu_qsbr_main(void)
630 rte_atomic64_init(&updates
);
631 rte_atomic64_init(&update_cycles
);
632 rte_atomic64_init(&checks
);
633 rte_atomic64_init(&check_cycles
);
635 if (get_enabled_cores_mask() != 0)
638 printf("Number of cores provided = %d\n", num_cores
);
640 printf("Test failed! Need 2 or more cores\n");
643 if (num_cores
> TEST_RCU_MAX_LCORE
) {
644 printf("Test failed! %d cores supported\n", TEST_RCU_MAX_LCORE
);
648 printf("Perf test with all reader threads registered\n");
649 printf("--------------------------------------------\n");
652 if (test_rcu_qsbr_perf() < 0)
655 if (test_rcu_qsbr_rperf() < 0)
658 if (test_rcu_qsbr_wperf() < 0)
661 if (test_rcu_qsbr_sw_sv_1qs() < 0)
664 if (test_rcu_qsbr_sw_sv_1qs_non_blocking() < 0)
667 /* Make sure the actual number of cores provided is less than
668 * TEST_RCU_MAX_LCORE. This will allow for some threads not
669 * to be registered on the QS variable.
671 if (num_cores
>= TEST_RCU_MAX_LCORE
) {
672 printf("Test failed! number of cores provided should be less than %d\n",
677 printf("Perf test with some of reader threads registered\n");
678 printf("------------------------------------------------\n");
681 if (test_rcu_qsbr_perf() < 0)
684 if (test_rcu_qsbr_rperf() < 0)
687 if (test_rcu_qsbr_wperf() < 0)
690 if (test_rcu_qsbr_sw_sv_1qs() < 0)
693 if (test_rcu_qsbr_sw_sv_1qs_non_blocking() < 0)
704 REGISTER_TEST_COMMAND(rcu_qsbr_perf_autotest
, test_rcu_qsbr_main
);