xref: /dpdk/app/test/test_ticketlock.c (revision b6a7e6852e9ab82ae0e05e2d2a0b83abca17de3b)
1efbcdaa5SJoyce Kong /* SPDX-License-Identifier: BSD-3-Clause
2efbcdaa5SJoyce Kong  * Copyright(c) 2018-2019 Arm Limited
3efbcdaa5SJoyce Kong  */
4efbcdaa5SJoyce Kong 
5efbcdaa5SJoyce Kong #include <inttypes.h>
6efbcdaa5SJoyce Kong #include <stdint.h>
7efbcdaa5SJoyce Kong #include <stdio.h>
8efbcdaa5SJoyce Kong #include <string.h>
9efbcdaa5SJoyce Kong #include <sys/queue.h>
10efbcdaa5SJoyce Kong #include <unistd.h>
11efbcdaa5SJoyce Kong 
12efbcdaa5SJoyce Kong #include <rte_common.h>
13efbcdaa5SJoyce Kong #include <rte_cycles.h>
14efbcdaa5SJoyce Kong #include <rte_eal.h>
15efbcdaa5SJoyce Kong #include <rte_launch.h>
16efbcdaa5SJoyce Kong #include <rte_lcore.h>
17efbcdaa5SJoyce Kong #include <rte_memory.h>
18efbcdaa5SJoyce Kong #include <rte_per_lcore.h>
19efbcdaa5SJoyce Kong #include <rte_ticketlock.h>
20efbcdaa5SJoyce Kong 
21efbcdaa5SJoyce Kong #include "test.h"
22efbcdaa5SJoyce Kong 
23efbcdaa5SJoyce Kong /*
24efbcdaa5SJoyce Kong  * Ticketlock test
25efbcdaa5SJoyce Kong  * =============
26efbcdaa5SJoyce Kong  *
27efbcdaa5SJoyce Kong  * - There is a global ticketlock and a table of ticketlocks (one per lcore).
28efbcdaa5SJoyce Kong  *
29efbcdaa5SJoyce Kong  * - The test function takes all of these locks and launches the
30cb056611SStephen Hemminger  *   ``test_ticketlock_per_core()`` function on each core (except the main).
31efbcdaa5SJoyce Kong  *
32efbcdaa5SJoyce Kong  *   - The function takes the global lock, display something, then releases
33efbcdaa5SJoyce Kong  *     the global lock.
34efbcdaa5SJoyce Kong  *   - The function takes the per-lcore lock, display something, then releases
35efbcdaa5SJoyce Kong  *     the per-core lock.
36efbcdaa5SJoyce Kong  *
37efbcdaa5SJoyce Kong  * - The main function unlocks the per-lcore locks sequentially and
38efbcdaa5SJoyce Kong  *   waits between each lock. This triggers the display of a message
39efbcdaa5SJoyce Kong  *   for each core, in the correct order. The autotest script checks that
40efbcdaa5SJoyce Kong  *   this order is correct.
41efbcdaa5SJoyce Kong  *
42efbcdaa5SJoyce Kong  * - A load test is carried out, with all cores attempting to lock a single lock
43efbcdaa5SJoyce Kong  *   multiple times
44efbcdaa5SJoyce Kong  */
45efbcdaa5SJoyce Kong 
46efbcdaa5SJoyce Kong static rte_ticketlock_t tl, tl_try;
47efbcdaa5SJoyce Kong static rte_ticketlock_t tl_tab[RTE_MAX_LCORE];
48efbcdaa5SJoyce Kong static rte_ticketlock_recursive_t tlr;
49efbcdaa5SJoyce Kong static unsigned int count;
50efbcdaa5SJoyce Kong 
51*b6a7e685STyler Retzlaff static RTE_ATOMIC(uint32_t) synchro;
52efbcdaa5SJoyce Kong 
53efbcdaa5SJoyce Kong static int
test_ticketlock_per_core(__rte_unused void * arg)54f2fc83b4SThomas Monjalon test_ticketlock_per_core(__rte_unused void *arg)
55efbcdaa5SJoyce Kong {
56efbcdaa5SJoyce Kong 	rte_ticketlock_lock(&tl);
57efbcdaa5SJoyce Kong 	printf("Global lock taken on core %u\n", rte_lcore_id());
58efbcdaa5SJoyce Kong 	rte_ticketlock_unlock(&tl);
59efbcdaa5SJoyce Kong 
60efbcdaa5SJoyce Kong 	rte_ticketlock_lock(&tl_tab[rte_lcore_id()]);
61efbcdaa5SJoyce Kong 	printf("Hello from core %u !\n", rte_lcore_id());
62efbcdaa5SJoyce Kong 	rte_ticketlock_unlock(&tl_tab[rte_lcore_id()]);
63efbcdaa5SJoyce Kong 
64efbcdaa5SJoyce Kong 	return 0;
65efbcdaa5SJoyce Kong }
66efbcdaa5SJoyce Kong 
67efbcdaa5SJoyce Kong static int
test_ticketlock_recursive_per_core(__rte_unused void * arg)68f2fc83b4SThomas Monjalon test_ticketlock_recursive_per_core(__rte_unused void *arg)
69efbcdaa5SJoyce Kong {
70efbcdaa5SJoyce Kong 	unsigned int id = rte_lcore_id();
71efbcdaa5SJoyce Kong 
72efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_lock(&tlr);
73efbcdaa5SJoyce Kong 	printf("Global recursive lock taken on core %u - count = %d\n",
74efbcdaa5SJoyce Kong 	       id, tlr.count);
75efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_lock(&tlr);
76efbcdaa5SJoyce Kong 	printf("Global recursive lock taken on core %u - count = %d\n",
77efbcdaa5SJoyce Kong 	       id, tlr.count);
78efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_lock(&tlr);
79efbcdaa5SJoyce Kong 	printf("Global recursive lock taken on core %u - count = %d\n",
80efbcdaa5SJoyce Kong 	       id, tlr.count);
81efbcdaa5SJoyce Kong 
82efbcdaa5SJoyce Kong 	printf("Hello from within recursive locks from core %u !\n", id);
83efbcdaa5SJoyce Kong 
84efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_unlock(&tlr);
85efbcdaa5SJoyce Kong 	printf("Global recursive lock released on core %u - count = %d\n",
86efbcdaa5SJoyce Kong 	       id, tlr.count);
87efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_unlock(&tlr);
88efbcdaa5SJoyce Kong 	printf("Global recursive lock released on core %u - count = %d\n",
89efbcdaa5SJoyce Kong 	       id, tlr.count);
90efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_unlock(&tlr);
91efbcdaa5SJoyce Kong 	printf("Global recursive lock released on core %u - count = %d\n",
92efbcdaa5SJoyce Kong 	       id, tlr.count);
93efbcdaa5SJoyce Kong 
94efbcdaa5SJoyce Kong 	return 0;
95efbcdaa5SJoyce Kong }
96efbcdaa5SJoyce Kong 
97efbcdaa5SJoyce Kong static rte_ticketlock_t lk = RTE_TICKETLOCK_INITIALIZER;
980efea35aSTyler Retzlaff static alignas(RTE_CACHE_LINE_SIZE) uint64_t lcount;
990efea35aSTyler Retzlaff static alignas(RTE_CACHE_LINE_SIZE) uint64_t lcore_count[RTE_MAX_LCORE];
100efbcdaa5SJoyce Kong static uint64_t time_cost[RTE_MAX_LCORE];
101efbcdaa5SJoyce Kong 
102efbcdaa5SJoyce Kong #define MAX_LOOP 10000
103efbcdaa5SJoyce Kong 
104efbcdaa5SJoyce Kong static int
load_loop_fn(void * func_param)105efbcdaa5SJoyce Kong load_loop_fn(void *func_param)
106efbcdaa5SJoyce Kong {
107efbcdaa5SJoyce Kong 	uint64_t time_diff = 0, begin;
108efbcdaa5SJoyce Kong 	uint64_t hz = rte_get_timer_hz();
109efbcdaa5SJoyce Kong 	const int use_lock = *(int *)func_param;
110efbcdaa5SJoyce Kong 	const unsigned int lcore = rte_lcore_id();
111efbcdaa5SJoyce Kong 
112cb056611SStephen Hemminger 	/* wait synchro for workers */
113cb056611SStephen Hemminger 	if (lcore != rte_get_main_lcore())
114*b6a7e685STyler Retzlaff 		rte_wait_until_equal_32((uint32_t *)(uintptr_t)&synchro, 1,
115*b6a7e685STyler Retzlaff 				rte_memory_order_relaxed);
116efbcdaa5SJoyce Kong 
117efbcdaa5SJoyce Kong 	begin = rte_rdtsc_precise();
118efbcdaa5SJoyce Kong 	while (lcore_count[lcore] < MAX_LOOP) {
119efbcdaa5SJoyce Kong 		if (use_lock)
120efbcdaa5SJoyce Kong 			rte_ticketlock_lock(&lk);
121efbcdaa5SJoyce Kong 		lcore_count[lcore]++;
122efbcdaa5SJoyce Kong 		lcount++;
123efbcdaa5SJoyce Kong 		if (use_lock)
124efbcdaa5SJoyce Kong 			rte_ticketlock_unlock(&lk);
125efbcdaa5SJoyce Kong 	}
126efbcdaa5SJoyce Kong 	time_diff = rte_rdtsc_precise() - begin;
127efbcdaa5SJoyce Kong 	time_cost[lcore] = time_diff * 1000000 / hz;
128efbcdaa5SJoyce Kong 	return 0;
129efbcdaa5SJoyce Kong }
130efbcdaa5SJoyce Kong 
131efbcdaa5SJoyce Kong static int
test_ticketlock_perf(void)132efbcdaa5SJoyce Kong test_ticketlock_perf(void)
133efbcdaa5SJoyce Kong {
134efbcdaa5SJoyce Kong 	unsigned int i;
135efbcdaa5SJoyce Kong 	uint64_t tcount = 0;
136efbcdaa5SJoyce Kong 	uint64_t total_time = 0;
137efbcdaa5SJoyce Kong 	int lock = 0;
138efbcdaa5SJoyce Kong 	const unsigned int lcore = rte_lcore_id();
139efbcdaa5SJoyce Kong 
140efbcdaa5SJoyce Kong 	printf("\nTest with no lock on single core...\n");
141efbcdaa5SJoyce Kong 	load_loop_fn(&lock);
142efbcdaa5SJoyce Kong 	printf("Core [%u] cost time = %"PRIu64" us\n", lcore, time_cost[lcore]);
143efbcdaa5SJoyce Kong 	memset(lcore_count, 0, sizeof(lcore_count));
144efbcdaa5SJoyce Kong 	memset(time_cost, 0, sizeof(time_cost));
145efbcdaa5SJoyce Kong 
146efbcdaa5SJoyce Kong 	printf("\nTest with lock on single core...\n");
147efbcdaa5SJoyce Kong 	lock = 1;
148efbcdaa5SJoyce Kong 	load_loop_fn(&lock);
149efbcdaa5SJoyce Kong 	printf("Core [%u] cost time = %"PRIu64" us\n", lcore, time_cost[lcore]);
150efbcdaa5SJoyce Kong 	memset(lcore_count, 0, sizeof(lcore_count));
151efbcdaa5SJoyce Kong 	memset(time_cost, 0, sizeof(time_cost));
152efbcdaa5SJoyce Kong 
153efbcdaa5SJoyce Kong 	lcount = 0;
154efbcdaa5SJoyce Kong 	printf("\nTest with lock on %u cores...\n", rte_lcore_count());
155efbcdaa5SJoyce Kong 
156cb056611SStephen Hemminger 	/* Clear synchro and start workers */
157*b6a7e685STyler Retzlaff 	rte_atomic_store_explicit(&synchro, 0, rte_memory_order_relaxed);
158cb056611SStephen Hemminger 	rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MAIN);
159efbcdaa5SJoyce Kong 
160cb056611SStephen Hemminger 	/* start synchro and launch test on main */
161*b6a7e685STyler Retzlaff 	rte_atomic_store_explicit(&synchro, 1, rte_memory_order_relaxed);
162efbcdaa5SJoyce Kong 	load_loop_fn(&lock);
163efbcdaa5SJoyce Kong 
164efbcdaa5SJoyce Kong 	rte_eal_mp_wait_lcore();
165efbcdaa5SJoyce Kong 
166efbcdaa5SJoyce Kong 	RTE_LCORE_FOREACH(i) {
167efbcdaa5SJoyce Kong 		printf("Core [%u] cost time = %"PRIu64" us\n", i, time_cost[i]);
168efbcdaa5SJoyce Kong 		tcount += lcore_count[i];
169efbcdaa5SJoyce Kong 		total_time += time_cost[i];
170efbcdaa5SJoyce Kong 	}
171efbcdaa5SJoyce Kong 
172efbcdaa5SJoyce Kong 	if (tcount != lcount)
173efbcdaa5SJoyce Kong 		return -1;
174efbcdaa5SJoyce Kong 
175efbcdaa5SJoyce Kong 	printf("Total cost time = %"PRIu64" us\n", total_time);
176efbcdaa5SJoyce Kong 
177efbcdaa5SJoyce Kong 	return 0;
178efbcdaa5SJoyce Kong }
179efbcdaa5SJoyce Kong 
180efbcdaa5SJoyce Kong /*
181efbcdaa5SJoyce Kong  * Use rte_ticketlock_trylock() to trylock a ticketlock object,
182efbcdaa5SJoyce Kong  * If it could not lock the object successfully, it would
183efbcdaa5SJoyce Kong  * return immediately and the variable of "count" would be
184efbcdaa5SJoyce Kong  * increased by one per times. the value of "count" could be
185efbcdaa5SJoyce Kong  * checked as the result later.
186efbcdaa5SJoyce Kong  */
187efbcdaa5SJoyce Kong static int
test_ticketlock_try(__rte_unused void * arg)188f2fc83b4SThomas Monjalon test_ticketlock_try(__rte_unused void *arg)
189efbcdaa5SJoyce Kong {
190efbcdaa5SJoyce Kong 	if (rte_ticketlock_trylock(&tl_try) == 0) {
191efbcdaa5SJoyce Kong 		rte_ticketlock_lock(&tl);
192efbcdaa5SJoyce Kong 		count++;
193efbcdaa5SJoyce Kong 		rte_ticketlock_unlock(&tl);
194efbcdaa5SJoyce Kong 	}
195efbcdaa5SJoyce Kong 
196efbcdaa5SJoyce Kong 	return 0;
197efbcdaa5SJoyce Kong }
198efbcdaa5SJoyce Kong 
199efbcdaa5SJoyce Kong 
200efbcdaa5SJoyce Kong /*
201efbcdaa5SJoyce Kong  * Test rte_eal_get_lcore_state() in addition to ticketlocks
202efbcdaa5SJoyce Kong  * as we have "waiting" then "running" lcores.
203efbcdaa5SJoyce Kong  */
204efbcdaa5SJoyce Kong static int
test_ticketlock(void)205efbcdaa5SJoyce Kong test_ticketlock(void)
206efbcdaa5SJoyce Kong {
207efbcdaa5SJoyce Kong 	int ret = 0;
208efbcdaa5SJoyce Kong 	int i;
209efbcdaa5SJoyce Kong 
210cb056611SStephen Hemminger 	/* worker cores should be waiting: print it */
211cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
212efbcdaa5SJoyce Kong 		printf("lcore %d state: %d\n", i,
213efbcdaa5SJoyce Kong 		       (int) rte_eal_get_lcore_state(i));
214efbcdaa5SJoyce Kong 	}
215efbcdaa5SJoyce Kong 
216efbcdaa5SJoyce Kong 	rte_ticketlock_init(&tl);
217efbcdaa5SJoyce Kong 	rte_ticketlock_init(&tl_try);
218efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_init(&tlr);
219cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
220efbcdaa5SJoyce Kong 		rte_ticketlock_init(&tl_tab[i]);
221efbcdaa5SJoyce Kong 	}
222efbcdaa5SJoyce Kong 
223efbcdaa5SJoyce Kong 	rte_ticketlock_lock(&tl);
224efbcdaa5SJoyce Kong 
225cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
226efbcdaa5SJoyce Kong 		rte_ticketlock_lock(&tl_tab[i]);
227efbcdaa5SJoyce Kong 		rte_eal_remote_launch(test_ticketlock_per_core, NULL, i);
228efbcdaa5SJoyce Kong 	}
229efbcdaa5SJoyce Kong 
230cb056611SStephen Hemminger 	/* worker cores should be busy: print it */
231cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
232efbcdaa5SJoyce Kong 		printf("lcore %d state: %d\n", i,
233efbcdaa5SJoyce Kong 		       (int) rte_eal_get_lcore_state(i));
234efbcdaa5SJoyce Kong 	}
235efbcdaa5SJoyce Kong 	rte_ticketlock_unlock(&tl);
236efbcdaa5SJoyce Kong 
237cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
238efbcdaa5SJoyce Kong 		rte_ticketlock_unlock(&tl_tab[i]);
239efbcdaa5SJoyce Kong 		rte_delay_ms(10);
240efbcdaa5SJoyce Kong 	}
241efbcdaa5SJoyce Kong 
242efbcdaa5SJoyce Kong 	rte_eal_mp_wait_lcore();
243efbcdaa5SJoyce Kong 
244efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_lock(&tlr);
245efbcdaa5SJoyce Kong 
246efbcdaa5SJoyce Kong 	/*
247efbcdaa5SJoyce Kong 	 * Try to acquire a lock that we already own
248efbcdaa5SJoyce Kong 	 */
249efbcdaa5SJoyce Kong 	if (!rte_ticketlock_recursive_trylock(&tlr)) {
250efbcdaa5SJoyce Kong 		printf("rte_ticketlock_recursive_trylock failed on a lock that "
251efbcdaa5SJoyce Kong 		       "we already own\n");
252efbcdaa5SJoyce Kong 		ret = -1;
253efbcdaa5SJoyce Kong 	} else
254efbcdaa5SJoyce Kong 		rte_ticketlock_recursive_unlock(&tlr);
255efbcdaa5SJoyce Kong 
256cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
257efbcdaa5SJoyce Kong 		rte_eal_remote_launch(test_ticketlock_recursive_per_core,
258efbcdaa5SJoyce Kong 					NULL, i);
259efbcdaa5SJoyce Kong 	}
260efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_unlock(&tlr);
261efbcdaa5SJoyce Kong 	rte_eal_mp_wait_lcore();
262efbcdaa5SJoyce Kong 
263efbcdaa5SJoyce Kong 	/*
264efbcdaa5SJoyce Kong 	 * Test if it could return immediately from try-locking a locked object.
265efbcdaa5SJoyce Kong 	 * Here it will lock the ticketlock object first, then launch all the
266cb056611SStephen Hemminger 	 * worker lcores to trylock the same ticketlock object.
267cb056611SStephen Hemminger 	 * All the worker lcores should give up try-locking a locked object and
268efbcdaa5SJoyce Kong 	 * return immediately, and then increase the "count" initialized with
269efbcdaa5SJoyce Kong 	 * zero by one per times.
270efbcdaa5SJoyce Kong 	 * We can check if the "count" is finally equal to the number of all
271cb056611SStephen Hemminger 	 * worker lcores to see if the behavior of try-locking a locked
272efbcdaa5SJoyce Kong 	 * ticketlock object is correct.
273efbcdaa5SJoyce Kong 	 */
274efbcdaa5SJoyce Kong 	if (rte_ticketlock_trylock(&tl_try) == 0)
275efbcdaa5SJoyce Kong 		return -1;
276efbcdaa5SJoyce Kong 
277efbcdaa5SJoyce Kong 	count = 0;
278cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(i) {
279efbcdaa5SJoyce Kong 		rte_eal_remote_launch(test_ticketlock_try, NULL, i);
280efbcdaa5SJoyce Kong 	}
281efbcdaa5SJoyce Kong 	rte_eal_mp_wait_lcore();
282efbcdaa5SJoyce Kong 	rte_ticketlock_unlock(&tl_try);
283efbcdaa5SJoyce Kong 	if (rte_ticketlock_is_locked(&tl)) {
284efbcdaa5SJoyce Kong 		printf("ticketlock is locked but it should not be\n");
285efbcdaa5SJoyce Kong 		return -1;
286efbcdaa5SJoyce Kong 	}
287efbcdaa5SJoyce Kong 	rte_ticketlock_lock(&tl);
288efbcdaa5SJoyce Kong 	if (count != (rte_lcore_count() - 1))
289efbcdaa5SJoyce Kong 		ret = -1;
290efbcdaa5SJoyce Kong 
291efbcdaa5SJoyce Kong 	rte_ticketlock_unlock(&tl);
292efbcdaa5SJoyce Kong 
293efbcdaa5SJoyce Kong 	/*
294efbcdaa5SJoyce Kong 	 * Test if it can trylock recursively.
295efbcdaa5SJoyce Kong 	 * Use rte_ticketlock_recursive_trylock() to check if it can lock
296efbcdaa5SJoyce Kong 	 * a ticketlock object recursively. Here it will try to lock a
297efbcdaa5SJoyce Kong 	 * ticketlock object twice.
298efbcdaa5SJoyce Kong 	 */
299efbcdaa5SJoyce Kong 	if (rte_ticketlock_recursive_trylock(&tlr) == 0) {
300efbcdaa5SJoyce Kong 		printf("It failed to do the first ticketlock_recursive_trylock "
301efbcdaa5SJoyce Kong 			   "but it should able to do\n");
302efbcdaa5SJoyce Kong 		return -1;
303efbcdaa5SJoyce Kong 	}
304efbcdaa5SJoyce Kong 	if (rte_ticketlock_recursive_trylock(&tlr) == 0) {
305efbcdaa5SJoyce Kong 		printf("It failed to do the second ticketlock_recursive_trylock "
306efbcdaa5SJoyce Kong 			   "but it should able to do\n");
307efbcdaa5SJoyce Kong 		return -1;
308efbcdaa5SJoyce Kong 	}
309efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_unlock(&tlr);
310efbcdaa5SJoyce Kong 	rte_ticketlock_recursive_unlock(&tlr);
311efbcdaa5SJoyce Kong 
312efbcdaa5SJoyce Kong 	if (test_ticketlock_perf() < 0)
313efbcdaa5SJoyce Kong 		return -1;
314efbcdaa5SJoyce Kong 
315efbcdaa5SJoyce Kong 	return ret;
316efbcdaa5SJoyce Kong }
317efbcdaa5SJoyce Kong 
318e0a8442cSBruce Richardson REGISTER_FAST_TEST(ticketlock_autotest, true, true, test_ticketlock);
319