5404922a87d2e2cdcccc718cb06ee545a56a24b6
[dpdk.git] / lib / librte_eal / common / eal_common_lcore.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <limits.h>
7 #include <string.h>
8
9 #include <rte_errno.h>
10 #include <rte_log.h>
11 #include <rte_eal.h>
12 #include <rte_lcore.h>
13 #include <rte_common.h>
14 #include <rte_debug.h>
15
16 #include "eal_private.h"
17 #include "eal_thread.h"
18
19 unsigned int rte_get_master_lcore(void)
20 {
21         return rte_eal_get_configuration()->master_lcore;
22 }
23
24 unsigned int rte_lcore_count(void)
25 {
26         return rte_eal_get_configuration()->lcore_count;
27 }
28
29 int rte_lcore_index(int lcore_id)
30 {
31         if (unlikely(lcore_id >= RTE_MAX_LCORE))
32                 return -1;
33
34         if (lcore_id < 0)
35                 lcore_id = (int)rte_lcore_id();
36
37         return lcore_config[lcore_id].core_index;
38 }
39
40 int rte_lcore_to_cpu_id(int lcore_id)
41 {
42         if (unlikely(lcore_id >= RTE_MAX_LCORE))
43                 return -1;
44
45         if (lcore_id < 0)
46                 lcore_id = (int)rte_lcore_id();
47
48         return lcore_config[lcore_id].core_id;
49 }
50
51 rte_cpuset_t rte_lcore_cpuset(unsigned int lcore_id)
52 {
53         return lcore_config[lcore_id].cpuset;
54 }
55
56 enum rte_lcore_role_t
57 rte_eal_lcore_role(unsigned int lcore_id)
58 {
59         struct rte_config *cfg = rte_eal_get_configuration();
60
61         if (lcore_id >= RTE_MAX_LCORE)
62                 return ROLE_OFF;
63         return cfg->lcore_role[lcore_id];
64 }
65
66 int rte_lcore_is_enabled(unsigned int lcore_id)
67 {
68         struct rte_config *cfg = rte_eal_get_configuration();
69
70         if (lcore_id >= RTE_MAX_LCORE)
71                 return 0;
72         return cfg->lcore_role[lcore_id] == ROLE_RTE;
73 }
74
75 unsigned int rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
76 {
77         i++;
78         if (wrap)
79                 i %= RTE_MAX_LCORE;
80
81         while (i < RTE_MAX_LCORE) {
82                 if (!rte_lcore_is_enabled(i) ||
83                     (skip_master && (i == rte_get_master_lcore()))) {
84                         i++;
85                         if (wrap)
86                                 i %= RTE_MAX_LCORE;
87                         continue;
88                 }
89                 break;
90         }
91         return i;
92 }
93
94 unsigned int
95 rte_lcore_to_socket_id(unsigned int lcore_id)
96 {
97         return lcore_config[lcore_id].socket_id;
98 }
99
100 static int
101 socket_id_cmp(const void *a, const void *b)
102 {
103         const int *lcore_id_a = a;
104         const int *lcore_id_b = b;
105
106         if (*lcore_id_a < *lcore_id_b)
107                 return -1;
108         if (*lcore_id_a > *lcore_id_b)
109                 return 1;
110         return 0;
111 }
112
113 /*
114  * Parse /sys/devices/system/cpu to get the number of physical and logical
115  * processors on the machine. The function will fill the cpu_info
116  * structure.
117  */
118 int
119 rte_eal_cpu_init(void)
120 {
121         /* pointer to global configuration */
122         struct rte_config *config = rte_eal_get_configuration();
123         unsigned lcore_id;
124         unsigned count = 0;
125         unsigned int socket_id, prev_socket_id;
126         int lcore_to_socket_id[RTE_MAX_LCORE];
127
128         /*
129          * Parse the maximum set of logical cores, detect the subset of running
130          * ones and enable them by default.
131          */
132         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
133                 lcore_config[lcore_id].core_index = count;
134
135                 /* init cpuset for per lcore config */
136                 CPU_ZERO(&lcore_config[lcore_id].cpuset);
137
138                 /* find socket first */
139                 socket_id = eal_cpu_socket_id(lcore_id);
140                 lcore_to_socket_id[lcore_id] = socket_id;
141
142                 if (eal_cpu_detected(lcore_id) == 0) {
143                         config->lcore_role[lcore_id] = ROLE_OFF;
144                         lcore_config[lcore_id].core_index = -1;
145                         continue;
146                 }
147
148                 /* By default, lcore 1:1 map to cpu id */
149                 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
150
151                 /* By default, each detected core is enabled */
152                 config->lcore_role[lcore_id] = ROLE_RTE;
153                 lcore_config[lcore_id].core_role = ROLE_RTE;
154                 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
155                 lcore_config[lcore_id].socket_id = socket_id;
156                 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
157                                 "core %u on socket %u\n",
158                                 lcore_id, lcore_config[lcore_id].core_id,
159                                 lcore_config[lcore_id].socket_id);
160                 count++;
161         }
162         for (; lcore_id < CPU_SETSIZE; lcore_id++) {
163                 if (eal_cpu_detected(lcore_id) == 0)
164                         continue;
165                 RTE_LOG(DEBUG, EAL, "Skipped lcore %u as core %u on socket %u\n",
166                         lcore_id, eal_cpu_core_id(lcore_id),
167                         eal_cpu_socket_id(lcore_id));
168         }
169
170         /* Set the count of enabled logical cores of the EAL configuration */
171         config->lcore_count = count;
172         RTE_LOG(DEBUG, EAL,
173                 "Support maximum %u logical core(s) by configuration.\n",
174                 RTE_MAX_LCORE);
175         RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
176
177         /* sort all socket id's in ascending order */
178         qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
179                         sizeof(lcore_to_socket_id[0]), socket_id_cmp);
180
181         prev_socket_id = -1;
182         config->numa_node_count = 0;
183         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
184                 socket_id = lcore_to_socket_id[lcore_id];
185                 if (socket_id != prev_socket_id)
186                         config->numa_nodes[config->numa_node_count++] =
187                                         socket_id;
188                 prev_socket_id = socket_id;
189         }
190         RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
191
192         return 0;
193 }
194
195 unsigned int
196 rte_socket_count(void)
197 {
198         const struct rte_config *config = rte_eal_get_configuration();
199         return config->numa_node_count;
200 }
201
202 int
203 rte_socket_id_by_idx(unsigned int idx)
204 {
205         const struct rte_config *config = rte_eal_get_configuration();
206         if (idx >= config->numa_node_count) {
207                 rte_errno = EINVAL;
208                 return -1;
209         }
210         return config->numa_nodes[idx];
211 }