1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2019-2020 Arm Limited
8 #include <rte_rcu_qsbr.h>
10 #include <rte_hash_crc.h>
11 #include <rte_malloc.h>
12 #include <rte_cycles.h>
13 #include <rte_random.h>
18 /* Check condition and return an error if true. */
19 #define TEST_RCU_QSBR_RETURN_IF_ERROR(cond, str, ...) \
22 printf("ERROR file %s, line %d: " str "\n", __FILE__, \
23 __LINE__, ##__VA_ARGS__); \
28 /* Check condition and go to label if true. */
29 #define TEST_RCU_QSBR_GOTO_IF_ERROR(label, cond, str, ...) \
32 printf("ERROR file %s, line %d: " str "\n", __FILE__, \
33 __LINE__, ##__VA_ARGS__); \
38 /* Make sure that this has the same value as __RTE_QSBR_CNT_INIT */
39 #define TEST_RCU_QSBR_CNT_INIT 1
41 static uint16_t enabled_core_ids[RTE_MAX_LCORE];
42 static unsigned int num_cores;
44 static uint32_t *keys;
45 #define TOTAL_ENTRY (1024 * 8)
46 #define COUNTER_VALUE 4096
47 static uint32_t *hash_data[RTE_MAX_LCORE][TOTAL_ENTRY];
48 static uint8_t writer_done;
49 static uint8_t cb_failed;
51 static struct rte_rcu_qsbr *t[RTE_MAX_LCORE];
52 static struct rte_hash *h[RTE_MAX_LCORE];
53 static char hash_name[RTE_MAX_LCORE][8];
55 struct test_rcu_thread_info {
56 /* Index in RCU array */
58 /* Index in hash array */
60 /* lcore IDs registered on the RCU variable */
61 uint16_t r_core_ids[2];
63 static struct test_rcu_thread_info thread_info[RTE_MAX_LCORE/4];
71 sz = rte_rcu_qsbr_get_memsize(RTE_MAX_LCORE);
73 for (i = 0; i < RTE_MAX_LCORE; i++)
74 t[i] = (struct rte_rcu_qsbr *)rte_zmalloc(NULL, sz,
85 for (i = 0; i < RTE_MAX_LCORE; i++)
92 * rte_rcu_qsbr_thread_register: Add a reader thread, to the list of threads
93 * reporting their quiescent state on a QS variable.
96 test_rcu_qsbr_get_memsize(void)
100 printf("\nTest rte_rcu_qsbr_thread_register()\n");
102 sz = rte_rcu_qsbr_get_memsize(0);
103 TEST_RCU_QSBR_RETURN_IF_ERROR((sz != 1), "Get Memsize for 0 threads");
105 sz = rte_rcu_qsbr_get_memsize(128);
107 * for machines with cache line size of 64B - 8384
108 * for machines with cache line size of 128 - 16768
110 if (RTE_CACHE_LINE_SIZE == 64)
111 TEST_RCU_QSBR_RETURN_IF_ERROR((sz != 8384),
112 "Get Memsize for 128 threads");
113 else if (RTE_CACHE_LINE_SIZE == 128)
114 TEST_RCU_QSBR_RETURN_IF_ERROR((sz != 16768),
115 "Get Memsize for 128 threads");
121 * rte_rcu_qsbr_init: Initialize a QSBR variable.
124 test_rcu_qsbr_init(void)
128 printf("\nTest rte_rcu_qsbr_init()\n");
130 r = rte_rcu_qsbr_init(NULL, RTE_MAX_LCORE);
131 TEST_RCU_QSBR_RETURN_IF_ERROR((r != 1), "NULL variable");
137 * rte_rcu_qsbr_thread_register: Add a reader thread, to the list of threads
138 * reporting their quiescent state on a QS variable.
141 test_rcu_qsbr_thread_register(void)
145 printf("\nTest rte_rcu_qsbr_thread_register()\n");
147 ret = rte_rcu_qsbr_thread_register(NULL, enabled_core_ids[0]);
148 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "NULL variable check");
150 ret = rte_rcu_qsbr_thread_register(NULL, 100000);
151 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
152 "NULL variable, invalid thread id");
154 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
156 /* Register valid thread id */
157 ret = rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[0]);
158 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1), "Valid thread id");
160 /* Re-registering should not return error */
161 ret = rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[0]);
162 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1),
163 "Already registered thread id");
165 /* Register valid thread id - max allowed thread id */
166 ret = rte_rcu_qsbr_thread_register(t[0], RTE_MAX_LCORE - 1);
167 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1), "Max thread id");
169 ret = rte_rcu_qsbr_thread_register(t[0], 100000);
170 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
171 "NULL variable, invalid thread id");
177 * rte_rcu_qsbr_thread_unregister: Remove a reader thread, from the list of
178 * threads reporting their quiescent state on a QS variable.
181 test_rcu_qsbr_thread_unregister(void)
183 unsigned int num_threads[3] = {1, RTE_MAX_LCORE, 1};
185 unsigned int skip_thread_id;
189 printf("\nTest rte_rcu_qsbr_thread_unregister()\n");
191 ret = rte_rcu_qsbr_thread_unregister(NULL, enabled_core_ids[0]);
192 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "NULL variable check");
194 ret = rte_rcu_qsbr_thread_unregister(NULL, 100000);
195 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
196 "NULL variable, invalid thread id");
198 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
200 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[0]);
202 ret = rte_rcu_qsbr_thread_unregister(t[0], 100000);
203 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
204 "NULL variable, invalid thread id");
206 /* Find first disabled core */
207 for (i = 0; i < RTE_MAX_LCORE; i++) {
208 if (enabled_core_ids[i] == 0)
211 /* Test with disabled lcore */
212 ret = rte_rcu_qsbr_thread_unregister(t[0], i);
213 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1),
214 "disabled thread id");
215 /* Unregister already unregistered core */
216 ret = rte_rcu_qsbr_thread_unregister(t[0], i);
217 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1),
218 "Already unregistered core");
220 /* Test with enabled lcore */
221 ret = rte_rcu_qsbr_thread_unregister(t[0], enabled_core_ids[0]);
222 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1),
223 "enabled thread id");
224 /* Unregister already unregistered core */
225 ret = rte_rcu_qsbr_thread_unregister(t[0], enabled_core_ids[0]);
226 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1),
227 "Already unregistered core");
230 * Test with different thread_ids:
232 * 2 - All possible thread_ids, from 0 to RTE_MAX_LCORE
233 * 3 - thread_id = RTE_MAX_LCORE - 1
235 for (j = 0; j < 3; j++) {
236 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
238 for (i = 0; i < num_threads[j]; i++)
239 rte_rcu_qsbr_thread_register(t[0],
240 (j == 2) ? (RTE_MAX_LCORE - 1) : i);
242 token = rte_rcu_qsbr_start(t[0]);
243 TEST_RCU_QSBR_RETURN_IF_ERROR(
244 (token != (TEST_RCU_QSBR_CNT_INIT + 1)), "QSBR Start");
245 skip_thread_id = rte_rand() % RTE_MAX_LCORE;
246 /* Update quiescent state counter */
247 for (i = 0; i < num_threads[j]; i++) {
248 /* Skip one update */
249 if ((j == 1) && (i == skip_thread_id))
251 rte_rcu_qsbr_quiescent(t[0],
252 (j == 2) ? (RTE_MAX_LCORE - 1) : i);
256 /* Validate the updates */
257 ret = rte_rcu_qsbr_check(t[0], token, false);
258 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
259 "Non-blocking QSBR check");
260 /* Update the previously skipped thread */
261 rte_rcu_qsbr_quiescent(t[0], skip_thread_id);
264 /* Validate the updates */
265 ret = rte_rcu_qsbr_check(t[0], token, false);
266 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
267 "Non-blocking QSBR check");
269 for (i = 0; i < num_threads[j]; i++)
270 rte_rcu_qsbr_thread_unregister(t[0],
271 (j == 2) ? (RTE_MAX_LCORE - 1) : i);
273 /* Check with no thread registered */
274 ret = rte_rcu_qsbr_check(t[0], token, true);
275 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0),
276 "Blocking QSBR check");
282 * rte_rcu_qsbr_start: Ask the worker threads to report the quiescent state
286 test_rcu_qsbr_start(void)
291 printf("\nTest rte_rcu_qsbr_start()\n");
293 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
295 for (i = 0; i < num_cores; i++)
296 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[i]);
298 token = rte_rcu_qsbr_start(t[0]);
299 TEST_RCU_QSBR_RETURN_IF_ERROR(
300 (token != (TEST_RCU_QSBR_CNT_INIT + 1)), "QSBR Start");
305 test_rcu_qsbr_check_reader(void *arg)
307 struct rte_rcu_qsbr *temp;
308 uint8_t read_type = (uint8_t)((uintptr_t)arg);
313 /* Update quiescent state counter */
314 for (i = 0; i < num_cores; i++) {
316 rte_rcu_qsbr_quiescent(temp, enabled_core_ids[i]);
318 rte_rcu_qsbr_thread_unregister(temp,
319 enabled_core_ids[i]);
325 * rte_rcu_qsbr_check: Checks if all the worker threads have entered the queis-
326 * cent state 'n' number of times. 'n' is provided in rte_rcu_qsbr_start API.
329 test_rcu_qsbr_check(void)
335 printf("\nTest rte_rcu_qsbr_check()\n");
337 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
339 token = rte_rcu_qsbr_start(t[0]);
340 TEST_RCU_QSBR_RETURN_IF_ERROR(
341 (token != (TEST_RCU_QSBR_CNT_INIT + 1)), "QSBR Start");
344 ret = rte_rcu_qsbr_check(t[0], 0, false);
345 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "Token = 0");
347 ret = rte_rcu_qsbr_check(t[0], token, true);
348 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "Blocking QSBR check");
350 for (i = 0; i < num_cores; i++)
351 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[i]);
353 ret = rte_rcu_qsbr_check(t[0], token, false);
354 /* Threads are offline, hence this should pass */
355 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "Non-blocking QSBR check");
357 token = rte_rcu_qsbr_start(t[0]);
358 TEST_RCU_QSBR_RETURN_IF_ERROR(
359 (token != (TEST_RCU_QSBR_CNT_INIT + 2)), "QSBR Start");
361 ret = rte_rcu_qsbr_check(t[0], token, false);
362 /* Threads are offline, hence this should pass */
363 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "Non-blocking QSBR check");
365 for (i = 0; i < num_cores; i++)
366 rte_rcu_qsbr_thread_unregister(t[0], enabled_core_ids[i]);
368 ret = rte_rcu_qsbr_check(t[0], token, true);
369 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "Blocking QSBR check");
371 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
373 for (i = 0; i < num_cores; i++)
374 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[i]);
376 token = rte_rcu_qsbr_start(t[0]);
377 TEST_RCU_QSBR_RETURN_IF_ERROR(
378 (token != (TEST_RCU_QSBR_CNT_INIT + 1)), "QSBR Start");
380 rte_eal_remote_launch(test_rcu_qsbr_check_reader, NULL,
381 enabled_core_ids[0]);
383 rte_eal_mp_wait_lcore();
384 ret = rte_rcu_qsbr_check(t[0], token, true);
385 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 1), "Blocking QSBR check");
391 test_rcu_qsbr_synchronize_reader(void *arg)
393 uint32_t lcore_id = rte_lcore_id();
396 /* Register and become online */
397 rte_rcu_qsbr_thread_register(t[0], lcore_id);
398 rte_rcu_qsbr_thread_online(t[0], lcore_id);
401 rte_rcu_qsbr_quiescent(t[0], lcore_id);
403 rte_rcu_qsbr_thread_offline(t[0], lcore_id);
404 rte_rcu_qsbr_thread_unregister(t[0], lcore_id);
410 * rte_rcu_qsbr_synchronize: Wait till all the reader threads have entered
411 * the quiescent state.
414 test_rcu_qsbr_synchronize(void)
418 printf("\nTest rte_rcu_qsbr_synchronize()\n");
420 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
422 /* Test if the API returns when there are no threads reporting
423 * QS on the variable.
425 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
427 /* Test if the API returns when there are threads registered
430 for (i = 0; i < RTE_MAX_LCORE; i++)
431 rte_rcu_qsbr_thread_register(t[0], i);
432 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
434 /* Test if the API returns when the caller is also
435 * reporting the QS status.
437 rte_rcu_qsbr_thread_online(t[0], 0);
438 rte_rcu_qsbr_synchronize(t[0], 0);
439 rte_rcu_qsbr_thread_offline(t[0], 0);
441 /* Check the other boundary */
442 rte_rcu_qsbr_thread_online(t[0], RTE_MAX_LCORE - 1);
443 rte_rcu_qsbr_synchronize(t[0], RTE_MAX_LCORE - 1);
444 rte_rcu_qsbr_thread_offline(t[0], RTE_MAX_LCORE - 1);
446 /* Test if the API returns after unregistering all the threads */
447 for (i = 0; i < RTE_MAX_LCORE; i++)
448 rte_rcu_qsbr_thread_unregister(t[0], i);
449 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
451 /* Test if the API returns with the live threads */
453 for (i = 0; i < num_cores; i++)
454 rte_eal_remote_launch(test_rcu_qsbr_synchronize_reader,
455 NULL, enabled_core_ids[i]);
456 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
457 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
458 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
459 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
460 rte_rcu_qsbr_synchronize(t[0], RTE_QSBR_THRID_INVALID);
463 rte_eal_mp_wait_lcore();
469 * rte_rcu_qsbr_thread_online: Add a registered reader thread, to
470 * the list of threads reporting their quiescent state on a QS variable.
473 test_rcu_qsbr_thread_online(void)
478 printf("Test rte_rcu_qsbr_thread_online()\n");
480 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
482 /* Register 2 threads to validate that only the
483 * online thread is waited upon.
485 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[0]);
486 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[1]);
488 /* Use qsbr_start to verify that the thread_online API
491 token = rte_rcu_qsbr_start(t[0]);
493 /* Make the thread online */
494 rte_rcu_qsbr_thread_online(t[0], enabled_core_ids[0]);
496 /* Check if the thread is online */
497 ret = rte_rcu_qsbr_check(t[0], token, true);
498 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "thread online");
500 /* Check if the online thread, can report QS */
501 token = rte_rcu_qsbr_start(t[0]);
502 rte_rcu_qsbr_quiescent(t[0], enabled_core_ids[0]);
503 ret = rte_rcu_qsbr_check(t[0], token, true);
504 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "thread update");
506 /* Make all the threads online */
507 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
508 token = rte_rcu_qsbr_start(t[0]);
509 for (i = 0; i < RTE_MAX_LCORE; i++) {
510 rte_rcu_qsbr_thread_register(t[0], i);
511 rte_rcu_qsbr_thread_online(t[0], i);
513 /* Check if all the threads are online */
514 ret = rte_rcu_qsbr_check(t[0], token, true);
515 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "thread online");
516 /* Check if all the online threads can report QS */
517 token = rte_rcu_qsbr_start(t[0]);
518 for (i = 0; i < RTE_MAX_LCORE; i++)
519 rte_rcu_qsbr_quiescent(t[0], i);
520 ret = rte_rcu_qsbr_check(t[0], token, true);
521 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "thread update");
527 * rte_rcu_qsbr_thread_offline: Remove a registered reader thread, from
528 * the list of threads reporting their quiescent state on a QS variable.
531 test_rcu_qsbr_thread_offline(void)
536 printf("\nTest rte_rcu_qsbr_thread_offline()\n");
538 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
540 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[0]);
542 /* Make the thread offline */
543 rte_rcu_qsbr_thread_offline(t[0], enabled_core_ids[0]);
545 /* Use qsbr_start to verify that the thread_offline API
548 token = rte_rcu_qsbr_start(t[0]);
549 /* Check if the thread is offline */
550 ret = rte_rcu_qsbr_check(t[0], token, true);
551 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "thread offline");
553 /* Bring an offline thread online and check if it can
556 rte_rcu_qsbr_thread_online(t[0], enabled_core_ids[0]);
557 /* Check if the online thread, can report QS */
558 token = rte_rcu_qsbr_start(t[0]);
559 rte_rcu_qsbr_quiescent(t[0], enabled_core_ids[0]);
560 ret = rte_rcu_qsbr_check(t[0], token, true);
561 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "offline to online");
564 * Check a sequence of online/status/offline/status/online/status
566 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
567 token = rte_rcu_qsbr_start(t[0]);
568 /* Make the threads online */
569 for (i = 0; i < RTE_MAX_LCORE; i++) {
570 rte_rcu_qsbr_thread_register(t[0], i);
571 rte_rcu_qsbr_thread_online(t[0], i);
574 /* Check if all the threads are online */
575 ret = rte_rcu_qsbr_check(t[0], token, true);
576 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "thread online");
578 /* Check if all the online threads can report QS */
579 token = rte_rcu_qsbr_start(t[0]);
580 for (i = 0; i < RTE_MAX_LCORE; i++)
581 rte_rcu_qsbr_quiescent(t[0], i);
582 ret = rte_rcu_qsbr_check(t[0], token, true);
583 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "report QS");
585 /* Make all the threads offline */
586 for (i = 0; i < RTE_MAX_LCORE; i++)
587 rte_rcu_qsbr_thread_offline(t[0], i);
588 /* Make sure these threads are not being waited on */
589 token = rte_rcu_qsbr_start(t[0]);
590 ret = rte_rcu_qsbr_check(t[0], token, true);
591 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "offline QS");
593 /* Make the threads online */
594 for (i = 0; i < RTE_MAX_LCORE; i++)
595 rte_rcu_qsbr_thread_online(t[0], i);
596 /* Check if all the online threads can report QS */
597 token = rte_rcu_qsbr_start(t[0]);
598 for (i = 0; i < RTE_MAX_LCORE; i++)
599 rte_rcu_qsbr_quiescent(t[0], i);
600 ret = rte_rcu_qsbr_check(t[0], token, true);
601 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "online again");
607 test_rcu_qsbr_free_resource1(void *p, void *e, unsigned int n)
609 if (p != NULL || e != NULL || n != 1) {
610 printf("%s: Test failed\n", __func__);
616 test_rcu_qsbr_free_resource2(void *p, void *e, unsigned int n)
618 if (p != NULL || e == NULL || n != 1) {
619 printf("%s: Test failed\n", __func__);
625 * rte_rcu_qsbr_dq_create: create a queue used to store the data structure
626 * elements that can be freed later. This queue is referred to as 'defer queue'.
629 test_rcu_qsbr_dq_create(void)
631 char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
632 struct rte_rcu_qsbr_dq_parameters params;
633 struct rte_rcu_qsbr_dq *dq;
635 printf("\nTest rte_rcu_qsbr_dq_create()\n");
637 /* Pass invalid parameters */
638 dq = rte_rcu_qsbr_dq_create(NULL);
639 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
641 memset(¶ms, 0, sizeof(struct rte_rcu_qsbr_dq_parameters));
642 dq = rte_rcu_qsbr_dq_create(¶ms);
643 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
645 snprintf(rcu_dq_name, sizeof(rcu_dq_name), "TEST_RCU");
646 params.name = rcu_dq_name;
647 dq = rte_rcu_qsbr_dq_create(¶ms);
648 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
650 params.free_fn = test_rcu_qsbr_free_resource1;
651 dq = rte_rcu_qsbr_dq_create(¶ms);
652 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
654 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
656 dq = rte_rcu_qsbr_dq_create(¶ms);
657 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
660 dq = rte_rcu_qsbr_dq_create(¶ms);
661 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
664 dq = rte_rcu_qsbr_dq_create(¶ms);
665 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
667 params.trigger_reclaim_limit = 0;
668 params.max_reclaim_size = 0;
669 dq = rte_rcu_qsbr_dq_create(¶ms);
670 TEST_RCU_QSBR_RETURN_IF_ERROR((dq != NULL), "dq create invalid params");
672 /* Pass all valid parameters */
674 params.trigger_reclaim_limit = 0;
675 params.max_reclaim_size = params.size;
676 dq = rte_rcu_qsbr_dq_create(¶ms);
677 TEST_RCU_QSBR_RETURN_IF_ERROR((dq == NULL), "dq create valid params");
678 rte_rcu_qsbr_dq_delete(dq);
681 params.flags = RTE_RCU_QSBR_DQ_MT_UNSAFE;
682 dq = rte_rcu_qsbr_dq_create(¶ms);
683 TEST_RCU_QSBR_RETURN_IF_ERROR((dq == NULL), "dq create valid params");
684 rte_rcu_qsbr_dq_delete(dq);
690 * rte_rcu_qsbr_dq_enqueue: enqueue one resource to the defer queue,
691 * to be freed later after at least one grace period is over.
694 test_rcu_qsbr_dq_enqueue(void)
698 char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
699 struct rte_rcu_qsbr_dq_parameters params;
700 struct rte_rcu_qsbr_dq *dq;
702 printf("\nTest rte_rcu_qsbr_dq_enqueue()\n");
704 /* Create a queue with simple parameters */
705 memset(¶ms, 0, sizeof(struct rte_rcu_qsbr_dq_parameters));
706 snprintf(rcu_dq_name, sizeof(rcu_dq_name), "TEST_RCU");
707 params.name = rcu_dq_name;
708 params.free_fn = test_rcu_qsbr_free_resource1;
709 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
713 params.trigger_reclaim_limit = 0;
714 params.max_reclaim_size = params.size;
715 dq = rte_rcu_qsbr_dq_create(¶ms);
716 TEST_RCU_QSBR_RETURN_IF_ERROR((dq == NULL), "dq create valid params");
718 /* Pass invalid parameters */
719 ret = rte_rcu_qsbr_dq_enqueue(NULL, NULL);
720 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "dq enqueue invalid params");
722 ret = rte_rcu_qsbr_dq_enqueue(dq, NULL);
723 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "dq enqueue invalid params");
725 ret = rte_rcu_qsbr_dq_enqueue(NULL, &r);
726 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "dq enqueue invalid params");
728 ret = rte_rcu_qsbr_dq_delete(dq);
729 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 1), "dq delete valid params");
735 * rte_rcu_qsbr_dq_reclaim: Reclaim resources from the defer queue.
738 test_rcu_qsbr_dq_reclaim(void)
741 char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
742 struct rte_rcu_qsbr_dq_parameters params;
743 struct rte_rcu_qsbr_dq *dq;
745 printf("\nTest rte_rcu_qsbr_dq_reclaim()\n");
747 /* Pass invalid parameters */
748 ret = rte_rcu_qsbr_dq_reclaim(NULL, 10, NULL, NULL, NULL);
749 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 1), "dq reclaim invalid params");
751 /* Pass invalid parameters */
752 memset(¶ms, 0, sizeof(struct rte_rcu_qsbr_dq_parameters));
753 snprintf(rcu_dq_name, sizeof(rcu_dq_name), "TEST_RCU");
754 params.name = rcu_dq_name;
755 params.free_fn = test_rcu_qsbr_free_resource1;
756 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
760 params.trigger_reclaim_limit = 0;
761 params.max_reclaim_size = params.size;
762 dq = rte_rcu_qsbr_dq_create(¶ms);
763 ret = rte_rcu_qsbr_dq_reclaim(dq, 0, NULL, NULL, NULL);
764 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 1), "dq reclaim invalid params");
770 * rte_rcu_qsbr_dq_delete: Delete a defer queue.
773 test_rcu_qsbr_dq_delete(void)
776 char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
777 struct rte_rcu_qsbr_dq_parameters params;
778 struct rte_rcu_qsbr_dq *dq;
780 printf("\nTest rte_rcu_qsbr_dq_delete()\n");
782 /* Pass invalid parameters */
783 ret = rte_rcu_qsbr_dq_delete(NULL);
784 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 0), "dq delete invalid params");
786 memset(¶ms, 0, sizeof(struct rte_rcu_qsbr_dq_parameters));
787 snprintf(rcu_dq_name, sizeof(rcu_dq_name), "TEST_RCU");
788 params.name = rcu_dq_name;
789 params.free_fn = test_rcu_qsbr_free_resource1;
790 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
794 params.trigger_reclaim_limit = 0;
795 params.max_reclaim_size = params.size;
796 dq = rte_rcu_qsbr_dq_create(¶ms);
797 TEST_RCU_QSBR_RETURN_IF_ERROR((dq == NULL), "dq create valid params");
798 ret = rte_rcu_qsbr_dq_delete(dq);
799 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 0), "dq delete valid params");
805 * rte_rcu_qsbr_dq_enqueue: enqueue one resource to the defer queue,
806 * to be freed later after at least one grace period is over.
809 test_rcu_qsbr_dq_functional(int32_t size, int32_t esize, uint32_t flags)
812 char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
813 struct rte_rcu_qsbr_dq_parameters params;
814 struct rte_rcu_qsbr_dq *dq;
819 printf("\nTest rte_rcu_qsbr_dq_xxx functional tests()\n");
820 printf("Size = %d, esize = %d, flags = 0x%x\n", size, esize, flags);
822 e = (uint64_t *)rte_zmalloc(NULL, esize, RTE_CACHE_LINE_SIZE);
827 /* Initialize the RCU variable. No threads are registered */
828 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
830 /* Create a queue with simple parameters */
831 memset(¶ms, 0, sizeof(struct rte_rcu_qsbr_dq_parameters));
832 snprintf(rcu_dq_name, sizeof(rcu_dq_name), "TEST_RCU");
833 params.name = rcu_dq_name;
834 params.flags = flags;
835 params.free_fn = test_rcu_qsbr_free_resource2;
838 params.esize = esize;
839 params.trigger_reclaim_limit = size >> 3;
840 params.max_reclaim_size = (size >> 4)?(size >> 4):1;
841 dq = rte_rcu_qsbr_dq_create(¶ms);
842 TEST_RCU_QSBR_RETURN_IF_ERROR((dq == NULL), "dq create valid params");
844 /* Given the size calculate the maximum number of entries
845 * that can be stored on the defer queue (look at the logic used
846 * in capacity calculation of rte_ring).
848 max_entries = rte_align32pow2(size + 1) - 1;
849 printf("max_entries = %d\n", max_entries);
851 /* Enqueue few counters starting with the value 'sc' */
852 /* The queue size will be rounded up to 2. The enqueue API also
853 * reclaims if the queue size is above certain limit. Since, there
854 * are no threads registered, reclamation succeeds. Hence, it should
855 * be possible to enqueue more than the provided queue size.
857 for (i = 0; i < 10; i++) {
858 ret = rte_rcu_qsbr_dq_enqueue(dq, e);
859 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (ret != 0),
860 "dq enqueue functional, i = %d", i);
861 for (j = 0; j < esize/8; j++)
865 /* Validate that call back function did not return any error */
866 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (cb_failed == 1), "CB failed");
868 /* Register a thread on the RCU QSBR variable. Reclamation will not
869 * succeed. It should not be possible to enqueue more than the size
870 * number of resources.
872 rte_rcu_qsbr_thread_register(t[0], 1);
873 rte_rcu_qsbr_thread_online(t[0], 1);
875 for (i = 0; i < max_entries; i++) {
876 ret = rte_rcu_qsbr_dq_enqueue(dq, e);
877 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (ret != 0),
878 "dq enqueue functional, max_entries = %d, i = %d",
880 for (j = 0; j < esize/8; j++)
884 /* Enqueue fails as queue is full */
885 ret = rte_rcu_qsbr_dq_enqueue(dq, e);
886 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (ret == 0), "defer queue is not full");
888 /* Delete should fail as there are elements in defer queue which
889 * cannot be reclaimed.
891 ret = rte_rcu_qsbr_dq_delete(dq);
892 TEST_RCU_QSBR_RETURN_IF_ERROR((ret == 0), "dq delete valid params");
894 /* Report quiescent state, enqueue should succeed */
895 rte_rcu_qsbr_quiescent(t[0], 1);
896 for (i = 0; i < max_entries; i++) {
897 ret = rte_rcu_qsbr_dq_enqueue(dq, e);
898 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (ret != 0),
899 "dq enqueue functional");
900 for (j = 0; j < esize/8; j++)
904 /* Validate that call back function did not return any error */
905 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (cb_failed == 1), "CB failed");
908 ret = rte_rcu_qsbr_dq_enqueue(dq, e);
909 TEST_RCU_QSBR_GOTO_IF_ERROR(end, (ret == 0), "defer queue is not full");
911 /* Report quiescent state, delete should succeed */
912 rte_rcu_qsbr_quiescent(t[0], 1);
913 ret = rte_rcu_qsbr_dq_delete(dq);
914 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 0), "dq delete valid params");
918 /* Validate that call back function did not return any error */
919 TEST_RCU_QSBR_RETURN_IF_ERROR((cb_failed == 1), "CB failed");
925 ret = rte_rcu_qsbr_dq_delete(dq);
926 TEST_RCU_QSBR_RETURN_IF_ERROR((ret != 0), "dq delete valid params");
931 * rte_rcu_qsbr_dump: Dump status of a single QS variable to a file
934 test_rcu_qsbr_dump(void)
938 printf("\nTest rte_rcu_qsbr_dump()\n");
941 rte_rcu_qsbr_dump(NULL, t[0]);
942 rte_rcu_qsbr_dump(stdout, NULL);
943 rte_rcu_qsbr_dump(NULL, NULL);
945 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
946 rte_rcu_qsbr_init(t[1], RTE_MAX_LCORE);
948 /* QS variable with 0 core mask */
949 rte_rcu_qsbr_dump(stdout, t[0]);
951 rte_rcu_qsbr_thread_register(t[0], enabled_core_ids[0]);
953 for (i = 1; i < num_cores; i++)
954 rte_rcu_qsbr_thread_register(t[1], enabled_core_ids[i]);
956 rte_rcu_qsbr_dump(stdout, t[0]);
957 rte_rcu_qsbr_dump(stdout, t[1]);
963 test_rcu_qsbr_reader(void *arg)
965 struct rte_rcu_qsbr *temp;
966 struct rte_hash *hash = NULL;
968 uint32_t lcore_id = rte_lcore_id();
969 struct test_rcu_thread_info *ti;
972 ti = (struct test_rcu_thread_info *)arg;
977 rte_rcu_qsbr_thread_register(temp, lcore_id);
978 rte_rcu_qsbr_thread_online(temp, lcore_id);
979 for (i = 0; i < TOTAL_ENTRY; i++) {
980 rte_rcu_qsbr_lock(temp, lcore_id);
981 if (rte_hash_lookup_data(hash, keys+i,
982 (void **)&pdata) != -ENOENT) {
984 while (pdata[lcore_id] < COUNTER_VALUE)
987 rte_rcu_qsbr_unlock(temp, lcore_id);
989 /* Update quiescent state counter */
990 rte_rcu_qsbr_quiescent(temp, lcore_id);
991 rte_rcu_qsbr_thread_offline(temp, lcore_id);
992 rte_rcu_qsbr_thread_unregister(temp, lcore_id);
993 } while (!writer_done);
999 test_rcu_qsbr_writer(void *arg)
1002 int32_t i, pos, del;
1004 struct rte_rcu_qsbr *temp;
1005 struct rte_hash *hash = NULL;
1006 struct test_rcu_thread_info *ti;
1008 ti = (struct test_rcu_thread_info *)arg;
1012 /* Delete element from the shared data structure */
1013 del = rte_lcore_id() % TOTAL_ENTRY;
1014 pos = rte_hash_del_key(hash, keys + del);
1016 printf("Delete key failed #%d\n", keys[del]);
1019 /* Start the quiescent state query process */
1020 token = rte_rcu_qsbr_start(temp);
1021 /* Check the quiescent state status */
1022 rte_rcu_qsbr_check(temp, token, true);
1023 for (i = 0; i < 2; i++) {
1024 c = hash_data[ti->ih][del][ti->r_core_ids[i]];
1025 if (c != COUNTER_VALUE && c != 0) {
1026 printf("Reader lcore id %u did not complete = %u\t",
1032 if (rte_hash_free_key_with_position(hash, pos) < 0) {
1033 printf("Failed to free the key #%d\n", keys[del]);
1036 rte_free(hash_data[ti->ih][del]);
1037 hash_data[ti->ih][del] = NULL;
1042 static struct rte_hash *
1043 init_hash(int hash_id)
1046 struct rte_hash *h = NULL;
1048 sprintf(hash_name[hash_id], "hash%d", hash_id);
1049 struct rte_hash_parameters hash_params = {
1050 .entries = TOTAL_ENTRY,
1051 .key_len = sizeof(uint32_t),
1052 .hash_func_init_val = 0,
1053 .socket_id = rte_socket_id(),
1054 .hash_func = rte_hash_crc,
1056 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF,
1057 .name = hash_name[hash_id],
1060 h = rte_hash_create(&hash_params);
1062 printf("Hash create Failed\n");
1066 for (i = 0; i < TOTAL_ENTRY; i++) {
1067 hash_data[hash_id][i] =
1068 rte_zmalloc(NULL, sizeof(uint32_t) * RTE_MAX_LCORE, 0);
1069 if (hash_data[hash_id][i] == NULL) {
1070 printf("No memory\n");
1074 keys = rte_malloc(NULL, sizeof(uint32_t) * TOTAL_ENTRY, 0);
1076 printf("No memory\n");
1080 for (i = 0; i < TOTAL_ENTRY; i++)
1083 for (i = 0; i < TOTAL_ENTRY; i++) {
1084 if (rte_hash_add_key_data(h, keys + i,
1085 (void *)((uintptr_t)hash_data[hash_id][i]))
1087 printf("Hash key add Failed #%d\n", i);
1096 * Single writer, Single QS variable, simultaneous QSBR Queries
1099 test_rcu_qsbr_sw_sv_3qs(void)
1108 printf("Test: 1 writer, 1 QSBR variable, simultaneous QSBR queries\n");
1110 rte_rcu_qsbr_init(t[0], RTE_MAX_LCORE);
1112 /* Shared data structure created */
1113 h[0] = init_hash(0);
1115 printf("Hash init failed\n");
1119 /* No need to fill the registered core IDs as the writer
1120 * thread is not launched.
1122 thread_info[0].ir = 0;
1123 thread_info[0].ih = 0;
1125 /* Reader threads are launched */
1126 /* Keep the number of reader threads low to reduce
1127 * the execution time.
1129 num_readers = num_cores < 4 ? num_cores : 4;
1130 for (i = 0; i < num_readers; i++)
1131 rte_eal_remote_launch(test_rcu_qsbr_reader, &thread_info[0],
1132 enabled_core_ids[i]);
1134 /* Delete element from the shared data structure */
1135 pos[0] = rte_hash_del_key(h[0], keys + 0);
1137 printf("Delete key failed #%d\n", keys[0]);
1140 /* Start the quiescent state query process */
1141 token[0] = rte_rcu_qsbr_start(t[0]);
1143 /* Delete element from the shared data structure */
1144 pos[1] = rte_hash_del_key(h[0], keys + 3);
1146 printf("Delete key failed #%d\n", keys[3]);
1149 /* Start the quiescent state query process */
1150 token[1] = rte_rcu_qsbr_start(t[0]);
1152 /* Delete element from the shared data structure */
1153 pos[2] = rte_hash_del_key(h[0], keys + 6);
1155 printf("Delete key failed #%d\n", keys[6]);
1158 /* Start the quiescent state query process */
1159 token[2] = rte_rcu_qsbr_start(t[0]);
1161 /* Check the quiescent state status */
1162 rte_rcu_qsbr_check(t[0], token[0], true);
1163 for (i = 0; i < num_readers; i++) {
1164 c = hash_data[0][0][enabled_core_ids[i]];
1165 if (c != COUNTER_VALUE && c != 0) {
1166 printf("Reader lcore %d did not complete #0 = %d\n",
1167 enabled_core_ids[i], c);
1172 if (rte_hash_free_key_with_position(h[0], pos[0]) < 0) {
1173 printf("Failed to free the key #%d\n", keys[0]);
1176 rte_free(hash_data[0][0]);
1177 hash_data[0][0] = NULL;
1179 /* Check the quiescent state status */
1180 rte_rcu_qsbr_check(t[0], token[1], true);
1181 for (i = 0; i < num_readers; i++) {
1182 c = hash_data[0][3][enabled_core_ids[i]];
1183 if (c != COUNTER_VALUE && c != 0) {
1184 printf("Reader lcore %d did not complete #3 = %d\n",
1185 enabled_core_ids[i], c);
1190 if (rte_hash_free_key_with_position(h[0], pos[1]) < 0) {
1191 printf("Failed to free the key #%d\n", keys[3]);
1194 rte_free(hash_data[0][3]);
1195 hash_data[0][3] = NULL;
1197 /* Check the quiescent state status */
1198 rte_rcu_qsbr_check(t[0], token[2], true);
1199 for (i = 0; i < num_readers; i++) {
1200 c = hash_data[0][6][enabled_core_ids[i]];
1201 if (c != COUNTER_VALUE && c != 0) {
1202 printf("Reader lcore %d did not complete #6 = %d\n",
1203 enabled_core_ids[i], c);
1208 if (rte_hash_free_key_with_position(h[0], pos[2]) < 0) {
1209 printf("Failed to free the key #%d\n", keys[6]);
1212 rte_free(hash_data[0][6]);
1213 hash_data[0][6] = NULL;
1217 /* Wait and check return value from reader threads */
1218 for (i = 0; i < num_readers; i++)
1219 if (rte_eal_wait_lcore(enabled_core_ids[i]) < 0)
1221 rte_hash_free(h[0]);
1228 /* Wait until all readers have exited */
1229 rte_eal_mp_wait_lcore();
1231 rte_hash_free(h[0]);
1233 for (i = 0; i < TOTAL_ENTRY; i++)
1234 rte_free(hash_data[0][i]);
1240 * Multi writer, Multiple QS variable, simultaneous QSBR queries
1243 test_rcu_qsbr_mw_mv_mqs(void)
1246 unsigned int test_cores;
1248 if (RTE_MAX_LCORE < 5 || num_cores < 4) {
1249 printf("Not enough cores for %s, expecting at least 5\n",
1251 return TEST_SKIPPED;
1255 test_cores = num_cores / 4;
1256 test_cores = test_cores * 4;
1258 printf("Test: %d writers, %d QSBR variable, simultaneous QSBR queries\n",
1259 test_cores / 2, test_cores / 4);
1261 for (i = 0; i < test_cores / 4; i++) {
1263 rte_rcu_qsbr_init(t[i], RTE_MAX_LCORE);
1264 h[i] = init_hash(i);
1266 printf("Hash init failed\n");
1269 thread_info[i].ir = i;
1270 thread_info[i].ih = i;
1271 thread_info[i].r_core_ids[0] = enabled_core_ids[j];
1272 thread_info[i].r_core_ids[1] = enabled_core_ids[j + 1];
1274 /* Reader threads are launched */
1275 rte_eal_remote_launch(test_rcu_qsbr_reader,
1276 (void *)&thread_info[i],
1277 enabled_core_ids[j]);
1278 rte_eal_remote_launch(test_rcu_qsbr_reader,
1279 (void *)&thread_info[i],
1280 enabled_core_ids[j + 1]);
1282 /* Writer threads are launched */
1283 rte_eal_remote_launch(test_rcu_qsbr_writer,
1284 (void *)&thread_info[i],
1285 enabled_core_ids[j + 2]);
1286 rte_eal_remote_launch(test_rcu_qsbr_writer,
1287 (void *)&thread_info[i],
1288 enabled_core_ids[j + 3]);
1291 /* Wait and check return value from writer threads */
1292 for (i = 0; i < test_cores / 4; i++) {
1294 if (rte_eal_wait_lcore(enabled_core_ids[j + 2]) < 0)
1297 if (rte_eal_wait_lcore(enabled_core_ids[j + 3]) < 0)
1302 /* Wait and check return value from reader threads */
1303 for (i = 0; i < test_cores / 4; i++) {
1305 if (rte_eal_wait_lcore(enabled_core_ids[j]) < 0)
1308 if (rte_eal_wait_lcore(enabled_core_ids[j + 1]) < 0)
1312 for (i = 0; i < test_cores / 4; i++)
1313 rte_hash_free(h[i]);
1321 /* Wait until all readers and writers have exited */
1322 rte_eal_mp_wait_lcore();
1324 for (i = 0; i < test_cores / 4; i++)
1325 rte_hash_free(h[i]);
1327 for (j = 0; j < test_cores / 4; j++)
1328 for (i = 0; i < TOTAL_ENTRY; i++)
1329 rte_free(hash_data[j][i]);
1335 test_rcu_qsbr_main(void)
1340 RTE_LCORE_FOREACH_WORKER(core_id) {
1341 enabled_core_ids[num_cores] = core_id;
1345 /* Error-checking test cases */
1346 if (test_rcu_qsbr_get_memsize() < 0)
1349 if (test_rcu_qsbr_init() < 0)
1354 if (test_rcu_qsbr_thread_register() < 0)
1357 if (test_rcu_qsbr_thread_unregister() < 0)
1360 if (test_rcu_qsbr_start() < 0)
1363 if (test_rcu_qsbr_check() < 0)
1366 if (test_rcu_qsbr_synchronize() < 0)
1369 if (test_rcu_qsbr_dump() < 0)
1372 if (test_rcu_qsbr_thread_online() < 0)
1375 if (test_rcu_qsbr_thread_offline() < 0)
1378 if (test_rcu_qsbr_dq_create() < 0)
1381 if (test_rcu_qsbr_dq_reclaim() < 0)
1384 if (test_rcu_qsbr_dq_delete() < 0)
1387 if (test_rcu_qsbr_dq_enqueue() < 0)
1390 printf("\nFunctional tests\n");
1392 if (test_rcu_qsbr_sw_sv_3qs() < 0)
1395 if (test_rcu_qsbr_mw_mv_mqs() < 0)
1398 if (test_rcu_qsbr_dq_functional(1, 8, 0) < 0)
1401 if (test_rcu_qsbr_dq_functional(2, 8, RTE_RCU_QSBR_DQ_MT_UNSAFE) < 0)
1404 if (test_rcu_qsbr_dq_functional(303, 16, 0) < 0)
1407 if (test_rcu_qsbr_dq_functional(7, 128, RTE_RCU_QSBR_DQ_MT_UNSAFE) < 0)
1421 REGISTER_TEST_COMMAND(rcu_qsbr_autotest, test_rcu_qsbr_main);