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;
56 int rte_lcore_is_enabled(unsigned int lcore_id)
58 struct rte_config *cfg = rte_eal_get_configuration();
60 if (lcore_id >= RTE_MAX_LCORE)
62 return cfg->lcore_role[lcore_id] == ROLE_RTE;
65 unsigned int rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
71 while (i < RTE_MAX_LCORE) {
72 if (!rte_lcore_is_enabled(i) ||
73 (skip_master && (i == rte_get_master_lcore()))) {
85 rte_lcore_to_socket_id(unsigned int lcore_id)
87 return lcore_config[lcore_id].socket_id;
91 socket_id_cmp(const void *a, const void *b)
93 const int *lcore_id_a = a;
94 const int *lcore_id_b = b;
96 if (*lcore_id_a < *lcore_id_b)
98 if (*lcore_id_a > *lcore_id_b)
104 * Parse /sys/devices/system/cpu to get the number of physical and logical
105 * processors on the machine. The function will fill the cpu_info
109 rte_eal_cpu_init(void)
111 /* pointer to global configuration */
112 struct rte_config *config = rte_eal_get_configuration();
115 unsigned int socket_id, prev_socket_id;
116 int lcore_to_socket_id[RTE_MAX_LCORE];
119 * Parse the maximum set of logical cores, detect the subset of running
120 * ones and enable them by default.
122 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
123 lcore_config[lcore_id].core_index = count;
125 /* init cpuset for per lcore config */
126 CPU_ZERO(&lcore_config[lcore_id].cpuset);
128 /* find socket first */
129 socket_id = eal_cpu_socket_id(lcore_id);
130 lcore_to_socket_id[lcore_id] = socket_id;
132 /* in 1:1 mapping, record related cpu detected state */
133 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
134 if (lcore_config[lcore_id].detected == 0) {
135 config->lcore_role[lcore_id] = ROLE_OFF;
136 lcore_config[lcore_id].core_index = -1;
140 /* By default, lcore 1:1 map to cpu id */
141 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
143 /* By default, each detected core is enabled */
144 config->lcore_role[lcore_id] = ROLE_RTE;
145 lcore_config[lcore_id].core_role = ROLE_RTE;
146 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
147 lcore_config[lcore_id].socket_id = socket_id;
148 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
149 "core %u on socket %u\n",
150 lcore_id, lcore_config[lcore_id].core_id,
151 lcore_config[lcore_id].socket_id);
154 /* Set the count of enabled logical cores of the EAL configuration */
155 config->lcore_count = count;
157 "Support maximum %u logical core(s) by configuration.\n",
159 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
161 /* sort all socket id's in ascending order */
162 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
163 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
166 config->numa_node_count = 0;
167 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
168 socket_id = lcore_to_socket_id[lcore_id];
169 if (socket_id != prev_socket_id)
170 config->numa_nodes[config->numa_node_count++] =
172 prev_socket_id = socket_id;
174 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
180 rte_socket_count(void)
182 const struct rte_config *config = rte_eal_get_configuration();
183 return config->numa_node_count;
187 rte_socket_id_by_idx(unsigned int idx)
189 const struct rte_config *config = rte_eal_get_configuration();
190 if (idx >= config->numa_node_count) {
194 return config->numa_nodes[idx];