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