1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Arm Limited
10 #include <sys/queue.h>
12 #include <rte_common.h>
13 #include <rte_memory.h>
14 #include <rte_per_lcore.h>
15 #include <rte_launch.h>
17 #include <rte_lcore.h>
18 #include <rte_cycles.h>
19 #include <rte_mcslock.h>
20 #include <rte_atomic.h>
28 * These tests are derived from spin lock test cases.
30 * - The functional test takes all of these locks and launches the
31 * ''test_mcslock_per_core()'' function on each core (except the main).
33 * - The function takes the global lock, display something, then releases
34 * the global lock on each core.
36 * - A load test is carried out, with all cores attempting to lock a single
37 * lock multiple times.
41 rte_mcslock_t *p_ml_try;
42 rte_mcslock_t *p_ml_perf;
44 static unsigned int count;
46 static rte_atomic32_t synchro;
49 test_mcslock_per_core(__rte_unused void *arg)
51 /* Per core me node. */
54 rte_mcslock_lock(&p_ml, &ml_me);
55 printf("MCS lock taken on core %u\n", rte_lcore_id());
56 rte_mcslock_unlock(&p_ml, &ml_me);
57 printf("MCS lock released on core %u\n", rte_lcore_id());
62 static uint64_t time_count[RTE_MAX_LCORE] = {0};
64 #define MAX_LOOP 1000000
67 load_loop_fn(void *func_param)
69 uint64_t time_diff = 0, begin;
70 uint64_t hz = rte_get_timer_hz();
71 volatile uint64_t lcount = 0;
72 const int use_lock = *(int *)func_param;
73 const unsigned int lcore = rte_lcore_id();
75 /**< Per core me node. */
76 rte_mcslock_t ml_perf_me;
79 while (rte_atomic32_read(&synchro) == 0)
82 begin = rte_get_timer_cycles();
83 while (lcount < MAX_LOOP) {
85 rte_mcslock_lock(&p_ml_perf, &ml_perf_me);
89 rte_mcslock_unlock(&p_ml_perf, &ml_perf_me);
91 time_diff = rte_get_timer_cycles() - begin;
92 time_count[lcore] = time_diff * 1000000 / hz;
97 test_mcslock_perf(void)
102 const unsigned int lcore = rte_lcore_id();
104 printf("\nTest with no lock on single core...\n");
105 rte_atomic32_set(&synchro, 1);
107 printf("Core [%u] Cost Time = %"PRIu64" us\n",
108 lcore, time_count[lcore]);
109 memset(time_count, 0, sizeof(time_count));
111 printf("\nTest with lock on single core...\n");
113 rte_atomic32_set(&synchro, 1);
115 printf("Core [%u] Cost Time = %"PRIu64" us\n",
116 lcore, time_count[lcore]);
117 memset(time_count, 0, sizeof(time_count));
119 printf("\nTest with lock on %u cores...\n", (rte_lcore_count()));
121 rte_atomic32_set(&synchro, 0);
122 rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MAIN);
124 /* start synchro and launch test on main */
125 rte_atomic32_set(&synchro, 1);
128 rte_eal_mp_wait_lcore();
130 RTE_LCORE_FOREACH(i) {
131 printf("Core [%u] Cost Time = %"PRIu64" us\n",
133 total += time_count[i];
136 printf("Total Cost Time = %"PRIu64" us\n", total);
142 * Use rte_mcslock_trylock() to trylock a mcs lock object,
143 * If it could not lock the object successfully, it would
144 * return immediately.
147 test_mcslock_try(__rte_unused void *arg)
149 /**< Per core me node. */
151 rte_mcslock_t ml_try_me;
153 /* Locked ml_try in the main lcore, so it should fail
154 * when trying to lock it in the worker lcore.
156 if (rte_mcslock_trylock(&p_ml_try, &ml_try_me) == 0) {
157 rte_mcslock_lock(&p_ml, &ml_me);
159 rte_mcslock_unlock(&p_ml, &ml_me);
167 * Test rte_eal_get_lcore_state() in addition to mcs locks
168 * as we have "waiting" then "running" lcores.
176 /* Define per core me node. */
178 rte_mcslock_t ml_try_me;
181 * Test mcs lock & unlock on each core
184 /* worker cores should be waiting: print it */
185 RTE_LCORE_FOREACH_WORKER(i) {
186 printf("lcore %d state: %d\n", i,
187 (int) rte_eal_get_lcore_state(i));
190 rte_mcslock_lock(&p_ml, &ml_me);
192 RTE_LCORE_FOREACH_WORKER(i) {
193 rte_eal_remote_launch(test_mcslock_per_core, NULL, i);
196 /* worker cores should be busy: print it */
197 RTE_LCORE_FOREACH_WORKER(i) {
198 printf("lcore %d state: %d\n", i,
199 (int) rte_eal_get_lcore_state(i));
202 rte_mcslock_unlock(&p_ml, &ml_me);
204 rte_eal_mp_wait_lcore();
207 * Test if it could return immediately from try-locking a locked object.
208 * Here it will lock the mcs lock object first, then launch all the
209 * worker lcores to trylock the same mcs lock object.
210 * All the worker lcores should give up try-locking a locked object and
211 * return immediately, and then increase the "count" initialized with
212 * zero by one per times.
213 * We can check if the "count" is finally equal to the number of all
214 * worker lcores to see if the behavior of try-locking a locked
215 * mcslock object is correct.
217 if (rte_mcslock_trylock(&p_ml_try, &ml_try_me) == 0)
221 RTE_LCORE_FOREACH_WORKER(i) {
222 rte_eal_remote_launch(test_mcslock_try, NULL, i);
224 rte_eal_mp_wait_lcore();
225 rte_mcslock_unlock(&p_ml_try, &ml_try_me);
227 /* Test is_locked API */
228 if (rte_mcslock_is_locked(p_ml)) {
229 printf("mcslock is locked but it should not be\n");
233 /* Counting the locked times in each core */
234 rte_mcslock_lock(&p_ml, &ml_me);
235 if (count != (rte_lcore_count() - 1))
237 rte_mcslock_unlock(&p_ml, &ml_me);
239 /* mcs lock perf test */
240 if (test_mcslock_perf() < 0)
246 REGISTER_TEST_COMMAND(mcslock_autotest, test_mcslock);