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 int rte_lcore_index(int lcore_id)
21 if (unlikely(lcore_id >= RTE_MAX_LCORE))
25 lcore_id = (int)rte_lcore_id();
27 return lcore_config[lcore_id].core_index;
30 int rte_lcore_to_cpu_id(int lcore_id)
32 if (unlikely(lcore_id >= RTE_MAX_LCORE))
36 lcore_id = (int)rte_lcore_id();
38 return lcore_config[lcore_id].core_id;
41 rte_cpuset_t rte_lcore_cpuset(unsigned int lcore_id)
43 return lcore_config[lcore_id].cpuset;
47 rte_lcore_to_socket_id(unsigned int lcore_id)
49 return lcore_config[lcore_id].socket_id;
53 socket_id_cmp(const void *a, const void *b)
55 const int *lcore_id_a = a;
56 const int *lcore_id_b = b;
58 if (*lcore_id_a < *lcore_id_b)
60 if (*lcore_id_a > *lcore_id_b)
66 * Parse /sys/devices/system/cpu to get the number of physical and logical
67 * processors on the machine. The function will fill the cpu_info
71 rte_eal_cpu_init(void)
73 /* pointer to global configuration */
74 struct rte_config *config = rte_eal_get_configuration();
77 unsigned int socket_id, prev_socket_id;
78 int lcore_to_socket_id[RTE_MAX_LCORE];
81 * Parse the maximum set of logical cores, detect the subset of running
82 * ones and enable them by default.
84 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
85 lcore_config[lcore_id].core_index = count;
87 /* init cpuset for per lcore config */
88 CPU_ZERO(&lcore_config[lcore_id].cpuset);
90 /* find socket first */
91 socket_id = eal_cpu_socket_id(lcore_id);
92 if (socket_id >= RTE_MAX_NUMA_NODES) {
93 #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
96 RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than RTE_MAX_NUMA_NODES (%d)\n",
97 socket_id, RTE_MAX_NUMA_NODES);
101 lcore_to_socket_id[lcore_id] = socket_id;
103 /* in 1:1 mapping, record related cpu detected state */
104 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
105 if (lcore_config[lcore_id].detected == 0) {
106 config->lcore_role[lcore_id] = ROLE_OFF;
107 lcore_config[lcore_id].core_index = -1;
111 /* By default, lcore 1:1 map to cpu id */
112 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
114 /* By default, each detected core is enabled */
115 config->lcore_role[lcore_id] = ROLE_RTE;
116 lcore_config[lcore_id].core_role = ROLE_RTE;
117 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
118 lcore_config[lcore_id].socket_id = socket_id;
119 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
120 "core %u on socket %u\n",
121 lcore_id, lcore_config[lcore_id].core_id,
122 lcore_config[lcore_id].socket_id);
125 /* Set the count of enabled logical cores of the EAL configuration */
126 config->lcore_count = count;
128 "Support maximum %u logical core(s) by configuration.\n",
130 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
132 /* sort all socket id's in ascending order */
133 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
134 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
137 config->numa_node_count = 0;
138 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
139 socket_id = lcore_to_socket_id[lcore_id];
140 if (socket_id != prev_socket_id)
141 config->numa_nodes[config->numa_node_count++] =
143 prev_socket_id = socket_id;
145 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
151 rte_socket_count(void)
153 const struct rte_config *config = rte_eal_get_configuration();
154 return config->numa_node_count;
158 rte_socket_id_by_idx(unsigned int idx)
160 const struct rte_config *config = rte_eal_get_configuration();
161 if (idx >= config->numa_node_count) {
165 return config->numa_nodes[idx];