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 and check return value from reader threads */
477 for (i = 0; i < num_cores; i++)
478 if (rte_eal_wait_lcore(enabled_core_ids[i]) < 0)
483 printf("Following numbers include calls to rte_hash functions\n");
484 printf("Cycles per 1 update(online/update/offline): %"PRIi64"\n",
485 rte_atomic64_read(&update_cycles) /
486 rte_atomic64_read(&updates));
488 printf("Cycles per 1 check(start, check): %"PRIi64"\n\n",
489 rte_atomic64_read(&check_cycles) /
490 rte_atomic64_read(&checks));
498 /* Wait until all readers have exited */
499 rte_eal_mp_wait_lcore();
503 for (i = 0; i < TOTAL_ENTRY; i++)
504 rte_free(hash_data[0][i]);
513 * Single writer, Single QS variable, Single QSBR query,
514 * Non-blocking rcu_qsbr_check
517 test_rcu_qsbr_sw_sv_1qs_non_blocking(void)
519 uint64_t token, begin, cycles;
520 int i, ret, tmp_num_cores, sz;
525 printf("Perf test: 1 writer, %d readers, 1 QSBR variable, 1 QSBR Query, Non-Blocking QSBR check\n", num_cores);
527 __atomic_store_n(&thr_id, 0, __ATOMIC_SEQ_CST);
529 if (all_registered == 1)
530 tmp_num_cores = num_cores;
532 tmp_num_cores = TEST_RCU_MAX_LCORE;
534 sz = rte_rcu_qsbr_get_memsize(tmp_num_cores);
535 t[0] = (struct rte_rcu_qsbr *)rte_zmalloc("rcu0", sz,
536 RTE_CACHE_LINE_SIZE);
537 /* QS variable is initialized */
538 rte_rcu_qsbr_init(t[0], tmp_num_cores);
540 /* Shared data structure created */
543 printf("Hash init failed\n");
547 /* Reader threads are launched */
548 for (i = 0; i < num_cores; i++)
549 rte_eal_remote_launch(test_rcu_qsbr_hash_reader, NULL,
550 enabled_core_ids[i]);
552 begin = rte_rdtsc_precise();
554 for (i = 0; i < TOTAL_ENTRY; i++) {
555 /* Delete elements from the shared data structure */
556 pos = rte_hash_del_key(h[0], keys + i);
558 printf("Delete key failed #%d\n", keys[i]);
561 /* Start the quiescent state query process */
562 token = rte_rcu_qsbr_start(t[0]);
564 /* Check the quiescent state status */
566 ret = rte_rcu_qsbr_check(t[0], token, false);
568 if (*hash_data[0][i] != COUNTER_VALUE &&
569 *hash_data[0][i] != 0) {
570 printf("Reader did not complete #%d = %d\n", i,
575 if (rte_hash_free_key_with_position(h[0], pos) < 0) {
576 printf("Failed to free the key #%d\n", keys[i]);
579 rte_free(hash_data[0][i]);
580 hash_data[0][i] = NULL;
583 cycles = rte_rdtsc_precise() - begin;
584 rte_atomic64_add(&check_cycles, cycles);
585 rte_atomic64_add(&checks, i);
588 /* Wait and check return value from reader threads */
589 for (i = 0; i < num_cores; i++)
590 if (rte_eal_wait_lcore(enabled_core_ids[i]) < 0)
595 printf("Following numbers include calls to rte_hash functions\n");
596 printf("Cycles per 1 update(online/update/offline): %"PRIi64"\n",
597 rte_atomic64_read(&update_cycles) /
598 rte_atomic64_read(&updates));
600 printf("Cycles per 1 check(start, check): %"PRIi64"\n\n",
601 rte_atomic64_read(&check_cycles) /
602 rte_atomic64_read(&checks));
610 /* Wait until all readers have exited */
611 rte_eal_mp_wait_lcore();
615 for (i = 0; i < TOTAL_ENTRY; i++)
616 rte_free(hash_data[0][i]);
624 test_rcu_qsbr_main(void)
626 rte_atomic64_init(&updates);
627 rte_atomic64_init(&update_cycles);
628 rte_atomic64_init(&checks);
629 rte_atomic64_init(&check_cycles);
631 if (get_enabled_cores_mask() != 0)
634 printf("Number of cores provided = %d\n", num_cores);
636 printf("Test failed! Need 2 or more cores\n");
639 if (num_cores > TEST_RCU_MAX_LCORE) {
640 printf("Test failed! %d cores supported\n", TEST_RCU_MAX_LCORE);
644 printf("Perf test with all reader threads registered\n");
645 printf("--------------------------------------------\n");
648 if (test_rcu_qsbr_perf() < 0)
651 if (test_rcu_qsbr_rperf() < 0)
654 if (test_rcu_qsbr_wperf() < 0)
657 if (test_rcu_qsbr_sw_sv_1qs() < 0)
660 if (test_rcu_qsbr_sw_sv_1qs_non_blocking() < 0)
663 /* Make sure the actual number of cores provided is less than
664 * TEST_RCU_MAX_LCORE. This will allow for some threads not
665 * to be registered on the QS variable.
667 if (num_cores >= TEST_RCU_MAX_LCORE) {
668 printf("Test failed! number of cores provided should be less than %d\n",
673 printf("Perf test with some of reader threads registered\n");
674 printf("------------------------------------------------\n");
677 if (test_rcu_qsbr_perf() < 0)
680 if (test_rcu_qsbr_rperf() < 0)
683 if (test_rcu_qsbr_wperf() < 0)
686 if (test_rcu_qsbr_sw_sv_1qs() < 0)
689 if (test_rcu_qsbr_sw_sv_1qs_non_blocking() < 0)
700 REGISTER_TEST_COMMAND(rcu_qsbr_perf_autotest, test_rcu_qsbr_main);