1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <rte_lcore.h>
13 #include <rte_common.h>
14 #include <rte_debug.h>
16 #include "eal_private.h"
17 #include "eal_thread.h"
19 unsigned int rte_get_master_lcore(void)
21 return rte_eal_get_configuration()->master_lcore;
24 unsigned int rte_lcore_count(void)
26 return rte_eal_get_configuration()->lcore_count;
29 int rte_lcore_index(int lcore_id)
31 if (unlikely(lcore_id >= RTE_MAX_LCORE))
35 if (rte_lcore_id() == LCORE_ID_ANY)
38 lcore_id = (int)rte_lcore_id();
41 return lcore_config[lcore_id].core_index;
44 int rte_lcore_to_cpu_id(int lcore_id)
46 if (unlikely(lcore_id >= RTE_MAX_LCORE))
50 if (rte_lcore_id() == LCORE_ID_ANY)
53 lcore_id = (int)rte_lcore_id();
56 return lcore_config[lcore_id].core_id;
59 rte_cpuset_t rte_lcore_cpuset(unsigned int lcore_id)
61 return lcore_config[lcore_id].cpuset;
65 rte_eal_lcore_role(unsigned int lcore_id)
67 struct rte_config *cfg = rte_eal_get_configuration();
69 if (lcore_id >= RTE_MAX_LCORE)
71 return cfg->lcore_role[lcore_id];
75 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
77 struct rte_config *cfg = rte_eal_get_configuration();
79 if (lcore_id >= RTE_MAX_LCORE)
82 return cfg->lcore_role[lcore_id] == role;
85 int rte_lcore_is_enabled(unsigned int lcore_id)
87 struct rte_config *cfg = rte_eal_get_configuration();
89 if (lcore_id >= RTE_MAX_LCORE)
91 return cfg->lcore_role[lcore_id] == ROLE_RTE;
94 unsigned int rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
100 while (i < RTE_MAX_LCORE) {
101 if (!rte_lcore_is_enabled(i) ||
102 (skip_master && (i == rte_get_master_lcore()))) {
114 rte_lcore_to_socket_id(unsigned int lcore_id)
116 return lcore_config[lcore_id].socket_id;
120 socket_id_cmp(const void *a, const void *b)
122 const int *lcore_id_a = a;
123 const int *lcore_id_b = b;
125 if (*lcore_id_a < *lcore_id_b)
127 if (*lcore_id_a > *lcore_id_b)
133 * Parse /sys/devices/system/cpu to get the number of physical and logical
134 * processors on the machine. The function will fill the cpu_info
138 rte_eal_cpu_init(void)
140 /* pointer to global configuration */
141 struct rte_config *config = rte_eal_get_configuration();
144 unsigned int socket_id, prev_socket_id;
145 int lcore_to_socket_id[RTE_MAX_LCORE];
148 * Parse the maximum set of logical cores, detect the subset of running
149 * ones and enable them by default.
151 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
152 lcore_config[lcore_id].core_index = count;
154 /* init cpuset for per lcore config */
155 CPU_ZERO(&lcore_config[lcore_id].cpuset);
157 /* find socket first */
158 socket_id = eal_cpu_socket_id(lcore_id);
159 lcore_to_socket_id[lcore_id] = socket_id;
161 if (eal_cpu_detected(lcore_id) == 0) {
162 config->lcore_role[lcore_id] = ROLE_OFF;
163 lcore_config[lcore_id].core_index = -1;
167 /* By default, lcore 1:1 map to cpu id */
168 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
170 /* By default, each detected core is enabled */
171 config->lcore_role[lcore_id] = ROLE_RTE;
172 lcore_config[lcore_id].core_role = ROLE_RTE;
173 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
174 lcore_config[lcore_id].socket_id = socket_id;
175 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
176 "core %u on socket %u\n",
177 lcore_id, lcore_config[lcore_id].core_id,
178 lcore_config[lcore_id].socket_id);
181 for (; lcore_id < CPU_SETSIZE; lcore_id++) {
182 if (eal_cpu_detected(lcore_id) == 0)
184 RTE_LOG(DEBUG, EAL, "Skipped lcore %u as core %u on socket %u\n",
185 lcore_id, eal_cpu_core_id(lcore_id),
186 eal_cpu_socket_id(lcore_id));
189 /* Set the count of enabled logical cores of the EAL configuration */
190 config->lcore_count = count;
192 "Support maximum %u logical core(s) by configuration.\n",
194 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
196 /* sort all socket id's in ascending order */
197 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
198 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
201 config->numa_node_count = 0;
202 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
203 socket_id = lcore_to_socket_id[lcore_id];
204 if (socket_id != prev_socket_id)
205 config->numa_nodes[config->numa_node_count++] =
207 prev_socket_id = socket_id;
209 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
215 rte_socket_count(void)
217 const struct rte_config *config = rte_eal_get_configuration();
218 return config->numa_node_count;
222 rte_socket_id_by_idx(unsigned int idx)
224 const struct rte_config *config = rte_eal_get_configuration();
225 if (idx >= config->numa_node_count) {
229 return config->numa_nodes[idx];