eal: remove unnecessary dirent.h include
[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 static int
20 socket_id_cmp(const void *a, const void *b)
21 {
22         const int *lcore_id_a = a;
23         const int *lcore_id_b = b;
24
25         if (*lcore_id_a < *lcore_id_b)
26                 return -1;
27         if (*lcore_id_a > *lcore_id_b)
28                 return 1;
29         return 0;
30 }
31
32 /*
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
35  * structure.
36  */
37 int
38 rte_eal_cpu_init(void)
39 {
40         /* pointer to global configuration */
41         struct rte_config *config = rte_eal_get_configuration();
42         unsigned lcore_id;
43         unsigned count = 0;
44         unsigned int socket_id, prev_socket_id;
45         int lcore_to_socket_id[RTE_MAX_LCORE];
46
47         /*
48          * Parse the maximum set of logical cores, detect the subset of running
49          * ones and enable them by default.
50          */
51         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
52                 lcore_config[lcore_id].core_index = count;
53
54                 /* init cpuset for per lcore config */
55                 CPU_ZERO(&lcore_config[lcore_id].cpuset);
56
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
61                         socket_id = 0;
62 #else
63                         RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than RTE_MAX_NUMA_NODES (%d)\n",
64                                         socket_id, RTE_MAX_NUMA_NODES);
65                         return -1;
66 #endif
67                 }
68                 lcore_to_socket_id[lcore_id] = socket_id;
69
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;
75                         continue;
76                 }
77
78                 /* By default, lcore 1:1 map to cpu id */
79                 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
80
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);
90                 count++;
91         }
92         /* Set the count of enabled logical cores of the EAL configuration */
93         config->lcore_count = count;
94         RTE_LOG(DEBUG, EAL,
95                 "Support maximum %u logical core(s) by configuration.\n",
96                 RTE_MAX_LCORE);
97         RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
98
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);
102
103         prev_socket_id = -1;
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++] =
109                                         socket_id;
110                 prev_socket_id = socket_id;
111         }
112         RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
113
114         return 0;
115 }
116
117 unsigned int __rte_experimental
118 rte_socket_count(void)
119 {
120         const struct rte_config *config = rte_eal_get_configuration();
121         return config->numa_node_count;
122 }
123
124 int __rte_experimental
125 rte_socket_id_by_idx(unsigned int idx)
126 {
127         const struct rte_config *config = rte_eal_get_configuration();
128         if (idx >= config->numa_node_count) {
129                 rte_errno = EINVAL;
130                 return -1;
131         }
132         return config->numa_nodes[idx];
133 }