eal: add lcore accessors
[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                 if (socket_id >= RTE_MAX_NUMA_NODES) {
93 #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
94                         socket_id = 0;
95 #else
96                         RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than RTE_MAX_NUMA_NODES (%d)\n",
97                                         socket_id, RTE_MAX_NUMA_NODES);
98                         return -1;
99 #endif
100                 }
101                 lcore_to_socket_id[lcore_id] = socket_id;
102
103                 /* in 1:1 mapping, record related cpu detected state */
104                 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
105                 if (lcore_config[lcore_id].detected == 0) {
106                         config->lcore_role[lcore_id] = ROLE_OFF;
107                         lcore_config[lcore_id].core_index = -1;
108                         continue;
109                 }
110
111                 /* By default, lcore 1:1 map to cpu id */
112                 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
113
114                 /* By default, each detected core is enabled */
115                 config->lcore_role[lcore_id] = ROLE_RTE;
116                 lcore_config[lcore_id].core_role = ROLE_RTE;
117                 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
118                 lcore_config[lcore_id].socket_id = socket_id;
119                 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
120                                 "core %u on socket %u\n",
121                                 lcore_id, lcore_config[lcore_id].core_id,
122                                 lcore_config[lcore_id].socket_id);
123                 count++;
124         }
125         /* Set the count of enabled logical cores of the EAL configuration */
126         config->lcore_count = count;
127         RTE_LOG(DEBUG, EAL,
128                 "Support maximum %u logical core(s) by configuration.\n",
129                 RTE_MAX_LCORE);
130         RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
131
132         /* sort all socket id's in ascending order */
133         qsort(lcore_to_socket_id, RTE_DIM(lcore_to_socket_id),
134                         sizeof(lcore_to_socket_id[0]), socket_id_cmp);
135
136         prev_socket_id = -1;
137         config->numa_node_count = 0;
138         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
139                 socket_id = lcore_to_socket_id[lcore_id];
140                 if (socket_id != prev_socket_id)
141                         config->numa_nodes[config->numa_node_count++] =
142                                         socket_id;
143                 prev_socket_id = socket_id;
144         }
145         RTE_LOG(INFO, EAL, "Detected %u NUMA nodes\n", config->numa_node_count);
146
147         return 0;
148 }
149
150 unsigned int
151 rte_socket_count(void)
152 {
153         const struct rte_config *config = rte_eal_get_configuration();
154         return config->numa_node_count;
155 }
156
157 int
158 rte_socket_id_by_idx(unsigned int idx)
159 {
160         const struct rte_config *config = rte_eal_get_configuration();
161         if (idx >= config->numa_node_count) {
162                 rte_errno = EINVAL;
163                 return -1;
164         }
165         return config->numa_nodes[idx];
166 }