eal: remove dead code on NUMA node detection
[dpdk.git] / lib / librte_eal / common / eal_common_lcore.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <limits.h>
7 #include <string.h>
8
9 #include <rte_errno.h>
10 #include <rte_log.h>
11 #include <rte_eal.h>
12 #include <rte_lcore.h>
13 #include <rte_common.h>
14 #include <rte_debug.h>
15
16 #include "eal_private.h"
17 #include "eal_thread.h"
18
19 int rte_lcore_index(int lcore_id)
20 {
21         if (unlikely(lcore_id >= RTE_MAX_LCORE))
22                 return -1;
23
24         if (lcore_id < 0)
25                 lcore_id = (int)rte_lcore_id();
26
27         return lcore_config[lcore_id].core_index;
28 }
29
30 int rte_lcore_to_cpu_id(int lcore_id)
31 {
32         if (unlikely(lcore_id >= RTE_MAX_LCORE))
33                 return -1;
34
35         if (lcore_id < 0)
36                 lcore_id = (int)rte_lcore_id();
37
38         return lcore_config[lcore_id].core_id;
39 }
40
41 rte_cpuset_t rte_lcore_cpuset(unsigned int lcore_id)
42 {
43         return lcore_config[lcore_id].cpuset;
44 }
45
46 unsigned int
47 rte_lcore_to_socket_id(unsigned int lcore_id)
48 {
49         return lcore_config[lcore_id].socket_id;
50 }
51
52 static int
53 socket_id_cmp(const void *a, const void *b)
54 {
55         const int *lcore_id_a = a;
56         const int *lcore_id_b = b;
57
58         if (*lcore_id_a < *lcore_id_b)
59                 return -1;
60         if (*lcore_id_a > *lcore_id_b)
61                 return 1;
62         return 0;
63 }
64
65 /*
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
68  * structure.
69  */
70 int
71 rte_eal_cpu_init(void)
72 {
73         /* pointer to global configuration */
74         struct rte_config *config = rte_eal_get_configuration();
75         unsigned lcore_id;
76         unsigned count = 0;
77         unsigned int socket_id, prev_socket_id;
78         int lcore_to_socket_id[RTE_MAX_LCORE];
79
80         /*
81          * Parse the maximum set of logical cores, detect the subset of running
82          * ones and enable them by default.
83          */
84         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
85                 lcore_config[lcore_id].core_index = count;
86
87                 /* init cpuset for per lcore config */
88                 CPU_ZERO(&lcore_config[lcore_id].cpuset);
89
90                 /* find socket first */
91                 socket_id = eal_cpu_socket_id(lcore_id);
92                 lcore_to_socket_id[lcore_id] = socket_id;
93
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;
99                         continue;
100                 }
101
102                 /* By default, lcore 1:1 map to cpu id */
103                 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
104
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);
114                 count++;
115         }
116         /* Set the count of enabled logical cores of the EAL configuration */
117         config->lcore_count = count;
118         RTE_LOG(DEBUG, EAL,
119                 "Support maximum %u logical core(s) by configuration.\n",
120                 RTE_MAX_LCORE);
121         RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
122
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);
126
127         prev_socket_id = -1;
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++] =
133                                         socket_id;
134                 prev_socket_id = socket_id;
135         }
136         RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
137
138         return 0;
139 }
140
141 unsigned int
142 rte_socket_count(void)
143 {
144         const struct rte_config *config = rte_eal_get_configuration();
145         return config->numa_node_count;
146 }
147
148 int
149 rte_socket_id_by_idx(unsigned int idx)
150 {
151         const struct rte_config *config = rte_eal_get_configuration();
152         if (idx >= config->numa_node_count) {
153                 rte_errno = EINVAL;
154                 return -1;
155         }
156         return config->numa_nodes[idx];
157 }