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 lcore_to_socket_id[lcore_id] = socket_id;
94 /* in 1:1 mapping, record related cpu detected state */
95 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
96 if (lcore_config[lcore_id].detected == 0) {
97 config->lcore_role[lcore_id] = ROLE_OFF;
98 lcore_config[lcore_id].core_index = -1;
102 /* By default, lcore 1:1 map to cpu id */
103 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
105 /* By default, each detected core is enabled */
106 config->lcore_role[lcore_id] = ROLE_RTE;
107 lcore_config[lcore_id].core_role = ROLE_RTE;
108 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
109 lcore_config[lcore_id].socket_id = socket_id;
110 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
111 "core %u on socket %u\n",
112 lcore_id, lcore_config[lcore_id].core_id,
113 lcore_config[lcore_id].socket_id);
116 /* Set the count of enabled logical cores of the EAL configuration */
117 config->lcore_count = count;
119 "Support maximum %u logical core(s) by configuration.\n",
121 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
123 /* sort all socket id's in ascending order */
124 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
125 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
128 config->numa_node_count = 0;
129 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
130 socket_id = lcore_to_socket_id[lcore_id];
131 if (socket_id != prev_socket_id)
132 config->numa_nodes[config->numa_node_count++] =
134 prev_socket_id = socket_id;
136 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
142 rte_socket_count(void)
144 const struct rte_config *config = rte_eal_get_configuration();
145 return config->numa_node_count;
149 rte_socket_id_by_idx(unsigned int idx)
151 const struct rte_config *config = rte_eal_get_configuration();
152 if (idx >= config->numa_node_count) {
156 return config->numa_nodes[idx];