xref: /dpdk/app/test/test_per_lcore.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <sys/queue.h>
8 
9 #include <rte_common.h>
10 #include <rte_memory.h>
11 #include <rte_per_lcore.h>
12 #include <rte_launch.h>
13 #include <rte_eal.h>
14 #include <rte_lcore.h>
15 #include <rte_cycles.h>
16 
17 #include "test.h"
18 
19 /*
20  * Per-lcore variables and lcore launch
21  * ====================================
22  *
23  * - Use ``rte_eal_mp_remote_launch()`` to call ``assign_vars()`` on
24  *   every available lcore. In this function, a per-lcore variable is
25  *   assigned to the lcore_id.
26  *
27  * - Use ``rte_eal_mp_remote_launch()`` to call ``display_vars()`` on
28  *   every available lcore. The function checks that the variable is
29  *   correctly set, or returns -1.
30  *
31  * - If at least one per-core variable was not correct, the test function
32  *   returns -1.
33  */
34 
35 static RTE_DEFINE_PER_LCORE(unsigned, test) = 0x12345678;
36 
37 static int
assign_vars(__rte_unused void * arg)38 assign_vars(__rte_unused void *arg)
39 {
40 	if (RTE_PER_LCORE(test) != 0x12345678)
41 		return -1;
42 	RTE_PER_LCORE(test) = rte_lcore_id();
43 	return 0;
44 }
45 
46 static int
display_vars(__rte_unused void * arg)47 display_vars(__rte_unused void *arg)
48 {
49 	unsigned lcore_id = rte_lcore_id();
50 	unsigned var = RTE_PER_LCORE(test);
51 	unsigned socket_id = rte_lcore_to_socket_id(lcore_id);
52 
53 	printf("on socket %u, on core %u, variable is %u\n", socket_id, lcore_id, var);
54 	if (lcore_id != var)
55 		return -1;
56 
57 	RTE_PER_LCORE(test) = 0x12345678;
58 	return 0;
59 }
60 
61 static int
test_per_lcore_delay(__rte_unused void * arg)62 test_per_lcore_delay(__rte_unused void *arg)
63 {
64 	rte_delay_ms(100);
65 	printf("wait 100ms on lcore %u\n", rte_lcore_id());
66 
67 	return 0;
68 }
69 
70 static int
test_per_lcore(void)71 test_per_lcore(void)
72 {
73 	unsigned lcore_id;
74 	int ret;
75 
76 	rte_eal_mp_remote_launch(assign_vars, NULL, SKIP_MAIN);
77 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
78 		if (rte_eal_wait_lcore(lcore_id) < 0)
79 			return -1;
80 	}
81 
82 	rte_eal_mp_remote_launch(display_vars, NULL, SKIP_MAIN);
83 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
84 		if (rte_eal_wait_lcore(lcore_id) < 0)
85 			return -1;
86 	}
87 
88 	/* test if it could do remote launch twice at the same time or not */
89 	ret = rte_eal_mp_remote_launch(test_per_lcore_delay, NULL, SKIP_MAIN);
90 	if (ret < 0) {
91 		printf("It fails to do remote launch but it should able to do\n");
92 		return -1;
93 	}
94 	/* it should not be able to launch a lcore which is running */
95 	ret = rte_eal_mp_remote_launch(test_per_lcore_delay, NULL, SKIP_MAIN);
96 	if (ret == 0) {
97 		printf("It does remote launch successfully but it should not at this time\n");
98 		return -1;
99 	}
100 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
101 		if (rte_eal_wait_lcore(lcore_id) < 0)
102 			return -1;
103 	}
104 
105 	return 0;
106 }
107 
108 REGISTER_FAST_TEST(per_lcore_autotest, true, true, test_per_lcore);
109