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 lcore_id = (int)rte_lcore_id();
37 return lcore_config[lcore_id].core_index;
40 int rte_lcore_to_cpu_id(int lcore_id)
42 if (unlikely(lcore_id >= RTE_MAX_LCORE))
46 lcore_id = (int)rte_lcore_id();
48 return lcore_config[lcore_id].core_id;
51 rte_cpuset_t rte_lcore_cpuset(unsigned int lcore_id)
53 return lcore_config[lcore_id].cpuset;
57 rte_eal_lcore_role(unsigned int lcore_id)
59 struct rte_config *cfg = rte_eal_get_configuration();
61 if (lcore_id >= RTE_MAX_LCORE)
63 return cfg->lcore_role[lcore_id];
66 int rte_lcore_is_enabled(unsigned int lcore_id)
68 struct rte_config *cfg = rte_eal_get_configuration();
70 if (lcore_id >= RTE_MAX_LCORE)
72 return cfg->lcore_role[lcore_id] == ROLE_RTE;
75 unsigned int rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
81 while (i < RTE_MAX_LCORE) {
82 if (!rte_lcore_is_enabled(i) ||
83 (skip_master && (i == rte_get_master_lcore()))) {
95 rte_lcore_to_socket_id(unsigned int lcore_id)
97 return lcore_config[lcore_id].socket_id;
101 socket_id_cmp(const void *a, const void *b)
103 const int *lcore_id_a = a;
104 const int *lcore_id_b = b;
106 if (*lcore_id_a < *lcore_id_b)
108 if (*lcore_id_a > *lcore_id_b)
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
119 rte_eal_cpu_init(void)
121 /* pointer to global configuration */
122 struct rte_config *config = rte_eal_get_configuration();
125 unsigned int socket_id, prev_socket_id;
126 int lcore_to_socket_id[RTE_MAX_LCORE];
129 * Parse the maximum set of logical cores, detect the subset of running
130 * ones and enable them by default.
132 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
133 lcore_config[lcore_id].core_index = count;
135 /* init cpuset for per lcore config */
136 CPU_ZERO(&lcore_config[lcore_id].cpuset);
138 /* find socket first */
139 socket_id = eal_cpu_socket_id(lcore_id);
140 lcore_to_socket_id[lcore_id] = socket_id;
142 if (eal_cpu_detected(lcore_id) == 0) {
143 config->lcore_role[lcore_id] = ROLE_OFF;
144 lcore_config[lcore_id].core_index = -1;
148 /* By default, lcore 1:1 map to cpu id */
149 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
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);
162 /* Set the count of enabled logical cores of the EAL configuration */
163 config->lcore_count = count;
165 "Support maximum %u logical core(s) by configuration.\n",
167 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
169 /* sort all socket id's in ascending order */
170 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
171 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
174 config->numa_node_count = 0;
175 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
176 socket_id = lcore_to_socket_id[lcore_id];
177 if (socket_id != prev_socket_id)
178 config->numa_nodes[config->numa_node_count++] =
180 prev_socket_id = socket_id;
182 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
188 rte_socket_count(void)
190 const struct rte_config *config = rte_eal_get_configuration();
191 return config->numa_node_count;
195 rte_socket_id_by_idx(unsigned int idx)
197 const struct rte_config *config = rte_eal_get_configuration();
198 if (idx >= config->numa_node_count) {
202 return config->numa_nodes[idx];