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"
20 socket_id_cmp(const void *a, const void *b)
22 const int *lcore_id_a = a;
23 const int *lcore_id_b = b;
25 if (*lcore_id_a < *lcore_id_b)
27 if (*lcore_id_a > *lcore_id_b)
33 * Parse /sys/devices/system/cpu to get the number of physical and logical
34 * processors on the machine. The function will fill the cpu_info
38 rte_eal_cpu_init(void)
40 /* pointer to global configuration */
41 struct rte_config *config = rte_eal_get_configuration();
44 unsigned int socket_id, prev_socket_id;
45 int lcore_to_socket_id[RTE_MAX_LCORE];
48 * Parse the maximum set of logical cores, detect the subset of running
49 * ones and enable them by default.
51 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
52 lcore_config[lcore_id].core_index = count;
54 /* init cpuset for per lcore config */
55 CPU_ZERO(&lcore_config[lcore_id].cpuset);
57 /* find socket first */
58 socket_id = eal_cpu_socket_id(lcore_id);
59 if (socket_id >= RTE_MAX_NUMA_NODES) {
60 #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
63 RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than RTE_MAX_NUMA_NODES (%d)\n",
64 socket_id, RTE_MAX_NUMA_NODES);
68 lcore_to_socket_id[lcore_id] = socket_id;
70 /* in 1:1 mapping, record related cpu detected state */
71 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
72 if (lcore_config[lcore_id].detected == 0) {
73 config->lcore_role[lcore_id] = ROLE_OFF;
74 lcore_config[lcore_id].core_index = -1;
78 /* By default, lcore 1:1 map to cpu id */
79 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
81 /* By default, each detected core is enabled */
82 config->lcore_role[lcore_id] = ROLE_RTE;
83 lcore_config[lcore_id].core_role = ROLE_RTE;
84 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
85 lcore_config[lcore_id].socket_id = socket_id;
86 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
87 "core %u on socket %u\n",
88 lcore_id, lcore_config[lcore_id].core_id,
89 lcore_config[lcore_id].socket_id);
92 /* Set the count of enabled logical cores of the EAL configuration */
93 config->lcore_count = count;
95 "Support maximum %u logical core(s) by configuration.\n",
97 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
99 /* sort all socket id's in ascending order */
100 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
101 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
104 config->numa_node_count = 0;
105 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
106 socket_id = lcore_to_socket_id[lcore_id];
107 if (socket_id != prev_socket_id)
108 config->numa_nodes[config->numa_node_count++] =
110 prev_socket_id = socket_id;
112 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
117 unsigned int __rte_experimental
118 rte_socket_count(void)
120 const struct rte_config *config = rte_eal_get_configuration();
121 return config->numa_node_count;
124 int __rte_experimental
125 rte_socket_id_by_idx(unsigned int idx)
127 const struct rte_config *config = rte_eal_get_configuration();
128 if (idx >= config->numa_node_count) {
132 return config->numa_nodes[idx];