1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_errno.h>
13 #include <rte_lcore.h>
14 #include <rte_common.h>
15 #include <rte_debug.h>
17 #include "eal_private.h"
18 #include "eal_thread.h"
21 socket_id_cmp(const void *a, const void *b)
23 const int *lcore_id_a = a;
24 const int *lcore_id_b = b;
26 if (*lcore_id_a < *lcore_id_b)
28 if (*lcore_id_a > *lcore_id_b)
34 * Parse /sys/devices/system/cpu to get the number of physical and logical
35 * processors on the machine. The function will fill the cpu_info
39 rte_eal_cpu_init(void)
41 /* pointer to global configuration */
42 struct rte_config *config = rte_eal_get_configuration();
45 unsigned int socket_id, prev_socket_id;
46 int lcore_to_socket_id[RTE_MAX_LCORE];
49 * Parse the maximum set of logical cores, detect the subset of running
50 * ones and enable them by default.
52 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
53 lcore_config[lcore_id].core_index = count;
55 /* init cpuset for per lcore config */
56 CPU_ZERO(&lcore_config[lcore_id].cpuset);
58 /* find socket first */
59 socket_id = eal_cpu_socket_id(lcore_id);
60 if (socket_id >= RTE_MAX_NUMA_NODES) {
61 #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
64 RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than RTE_MAX_NUMA_NODES (%d)\n",
65 socket_id, RTE_MAX_NUMA_NODES);
69 lcore_to_socket_id[lcore_id] = socket_id;
71 /* in 1:1 mapping, record related cpu detected state */
72 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
73 if (lcore_config[lcore_id].detected == 0) {
74 config->lcore_role[lcore_id] = ROLE_OFF;
75 lcore_config[lcore_id].core_index = -1;
79 /* By default, lcore 1:1 map to cpu id */
80 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
82 /* By default, each detected core is enabled */
83 config->lcore_role[lcore_id] = ROLE_RTE;
84 lcore_config[lcore_id].core_role = ROLE_RTE;
85 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
86 lcore_config[lcore_id].socket_id = socket_id;
87 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
88 "core %u on socket %u\n",
89 lcore_id, lcore_config[lcore_id].core_id,
90 lcore_config[lcore_id].socket_id);
93 /* Set the count of enabled logical cores of the EAL configuration */
94 config->lcore_count = count;
96 "Support maximum %u logical core(s) by configuration.\n",
98 RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
100 /* sort all socket id's in ascending order */
101 qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
102 sizeof(lcore_to_socket_id[0]), socket_id_cmp);
105 config->numa_node_count = 0;
106 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
107 socket_id = lcore_to_socket_id[lcore_id];
108 if (socket_id != prev_socket_id)
109 config->numa_nodes[config->numa_node_count++] =
111 prev_socket_id = socket_id;
113 RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
118 unsigned int __rte_experimental
119 rte_socket_count(void)
121 const struct rte_config *config = rte_eal_get_configuration();
122 return config->numa_node_count;
125 int __rte_experimental
126 rte_socket_id_by_idx(unsigned int idx)
128 const struct rte_config *config = rte_eal_get_configuration();
129 if (idx >= config->numa_node_count) {
133 return config->numa_nodes[idx];