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];
74 int rte_lcore_is_enabled(unsigned int lcore_id)
76 struct rte_config *cfg = rte_eal_get_configuration();
78 if (lcore_id >= RTE_MAX_LCORE)
80 return cfg->lcore_role[lcore_id] == ROLE_RTE;
83 unsigned int rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
89 while (i < RTE_MAX_LCORE) {
90 if (!rte_lcore_is_enabled(i) ||
91 (skip_master && (i == rte_get_master_lcore()))) {
103 rte_lcore_to_socket_id(unsigned int lcore_id)
105 return lcore_config[lcore_id].socket_id;
109 socket_id_cmp(const void *a, const void *b)
111 const int *lcore_id_a = a;
112 const int *lcore_id_b = b;
114 if (*lcore_id_a < *lcore_id_b)
116 if (*lcore_id_a > *lcore_id_b)
122 * Parse /sys/devices/system/cpu to get the number of physical and logical
123 * processors on the machine. The function will fill the cpu_info
127 rte_eal_cpu_init(void)
129 /* pointer to global configuration */
130 struct rte_config *config = rte_eal_get_configuration();
133 unsigned int socket_id, prev_socket_id;
134 int lcore_to_socket_id[RTE_MAX_LCORE];
137 * Parse the maximum set of logical cores, detect the subset of running
138 * ones and enable them by default.
140 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
141 lcore_config[lcore_id].core_index = count;
143 /* init cpuset for per lcore config */
144 CPU_ZERO(&lcore_config[lcore_id].cpuset);
146 /* find socket first */
147 socket_id = eal_cpu_socket_id(lcore_id);
148 lcore_to_socket_id[lcore_id] = socket_id;
150 if (eal_cpu_detected(lcore_id) == 0) {
151 config->lcore_role[lcore_id] = ROLE_OFF;
152 lcore_config[lcore_id].core_index = -1;
156 /* By default, lcore 1:1 map to cpu id */
157 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
159 /* By default, each detected core is enabled */
160 config->lcore_role[lcore_id] = ROLE_RTE;
161 lcore_config[lcore_id].core_role = ROLE_RTE;
162 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
163 lcore_config[lcore_id].socket_id = socket_id;
164 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
165 "core %u on socket %u\n",
166 lcore_id, lcore_config[lcore_id].core_id,
167 lcore_config[lcore_id].socket_id);
170 for (; lcore_id < CPU_SETSIZE; lcore_id++) {
171 if (eal_cpu_detected(lcore_id) == 0)
173 RTE_LOG(DEBUG, EAL, "Skipped lcore %u as core %u on socket %u\n",
174 lcore_id, eal_cpu_core_id(lcore_id),
175 eal_cpu_socket_id(lcore_id));
178 /* Set the count of enabled logical cores of the EAL configuration */
179 config->lcore_count = count;
181 "Support maximum %u logical core(s) by configuration.\n",
183 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
185 /* sort all socket id's in ascending order */
186 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
187 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
190 config->numa_node_count = 0;
191 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
192 socket_id = lcore_to_socket_id[lcore_id];
193 if (socket_id != prev_socket_id)
194 config->numa_nodes[config->numa_node_count++] =
196 prev_socket_id = socket_id;
198 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
204 rte_socket_count(void)
206 const struct rte_config *config = rte_eal_get_configuration();
207 return config->numa_node_count;
211 rte_socket_id_by_idx(unsigned int idx)
213 const struct rte_config *config = rte_eal_get_configuration();
214 if (idx >= config->numa_node_count) {
218 return config->numa_nodes[idx];