eal: get socket id from cpu id
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_lcore.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <dirent.h>
38
39 #include <rte_log.h>
40 #include <rte_eal.h>
41 #include <rte_lcore.h>
42 #include <rte_common.h>
43 #include <rte_string_fns.h>
44 #include <rte_debug.h>
45
46 #include "eal_private.h"
47 #include "eal_filesystem.h"
48 #include "eal_thread.h"
49
50 #define SYS_CPU_DIR "/sys/devices/system/cpu/cpu%u"
51 #define CORE_ID_FILE "topology/core_id"
52 #define PHYS_PKG_FILE "topology/physical_package_id"
53
54 /* Check if a cpu is present by the presence of the cpu information for it */
55 static int
56 cpu_detected(unsigned lcore_id)
57 {
58         char path[PATH_MAX];
59         int len = snprintf(path, sizeof(path), SYS_CPU_DIR
60                 "/"CORE_ID_FILE, lcore_id);
61         if (len <= 0 || (unsigned)len >= sizeof(path))
62                 return 0;
63         if (access(path, F_OK) != 0)
64                 return 0;
65
66         return 1;
67 }
68
69 /* Get CPU socket id (NUMA node) by reading directory
70  * /sys/devices/system/cpu/cpuX looking for symlink "nodeY"
71  * which gives the NUMA topology information.
72  * Note: physical package id != NUMA node, but we use it as a
73  * fallback for kernels which don't create a nodeY link
74  */
75 unsigned
76 eal_cpu_socket_id(unsigned lcore_id)
77 {
78         const char node_prefix[] = "node";
79         const size_t prefix_len = sizeof(node_prefix) - 1;
80         char path[PATH_MAX];
81         DIR *d = NULL;
82         unsigned long id = 0;
83         struct dirent *e;
84         char *endptr = NULL;
85
86         int len = snprintf(path, sizeof(path),
87                                SYS_CPU_DIR, lcore_id);
88         if (len <= 0 || (unsigned)len >= sizeof(path))
89                 goto err;
90
91         d = opendir(path);
92         if (!d)
93                 goto err;
94
95         while ((e = readdir(d)) != NULL) {
96                 if (strncmp(e->d_name, node_prefix, prefix_len) == 0) {
97                         id = strtoul(e->d_name+prefix_len, &endptr, 0);
98                         break;
99                 }
100         }
101         if (endptr == NULL || *endptr!='\0' || endptr == e->d_name+prefix_len) {
102                 RTE_LOG(WARNING, EAL, "Cannot read numa node link "
103                                 "for lcore %u - using physical package id instead\n",
104                                 lcore_id);
105
106                 len = snprintf(path, sizeof(path), SYS_CPU_DIR "/%s",
107                                 lcore_id, PHYS_PKG_FILE);
108                 if (len <= 0 || (unsigned)len >= sizeof(path))
109                         goto err;
110                 if (eal_parse_sysfs_value(path, &id) != 0)
111                         goto err;
112         }
113         closedir(d);
114         return (unsigned)id;
115
116 err:
117         if (d)
118                 closedir(d);
119         RTE_LOG(ERR, EAL, "Error getting NUMA socket information from %s "
120                         "for lcore %u - assuming NUMA socket 0\n", SYS_CPU_DIR, lcore_id);
121         return 0;
122 }
123
124 /* Get the cpu core id value from the /sys/.../cpuX core_id value */
125 static unsigned
126 cpu_core_id(unsigned lcore_id)
127 {
128         char path[PATH_MAX];
129         unsigned long id;
130
131         int len = snprintf(path, sizeof(path), SYS_CPU_DIR "/%s", lcore_id, CORE_ID_FILE);
132         if (len <= 0 || (unsigned)len >= sizeof(path))
133                 goto err;
134         if (eal_parse_sysfs_value(path, &id) != 0)
135                 goto err;
136         return (unsigned)id;
137
138 err:
139         RTE_LOG(ERR, EAL, "Error reading core id value from %s "
140                         "for lcore %u - assuming core 0\n", SYS_CPU_DIR, lcore_id);
141         return 0;
142 }
143
144 /*
145  * Parse /sys/devices/system/cpu to get the number of physical and logical
146  * processors on the machine. The function will fill the cpu_info
147  * structure.
148  */
149 int
150 rte_eal_cpu_init(void)
151 {
152         /* pointer to global configuration */
153         struct rte_config *config = rte_eal_get_configuration();
154         unsigned lcore_id;
155         unsigned count = 0;
156
157         /*
158          * Parse the maximum set of logical cores, detect the subset of running
159          * ones and enable them by default.
160          */
161         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
162                 /* init cpuset for per lcore config */
163                 CPU_ZERO(&lcore_config[lcore_id].cpuset);
164
165                 /* in 1:1 mapping, record related cpu detected state */
166                 lcore_config[lcore_id].detected = cpu_detected(lcore_id);
167                 if (lcore_config[lcore_id].detected == 0) {
168                         config->lcore_role[lcore_id] = ROLE_OFF;
169                         continue;
170                 }
171
172                 /* By default, lcore 1:1 map to cpu id */
173                 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
174
175                 /* By default, each detected core is enabled */
176                 config->lcore_role[lcore_id] = ROLE_RTE;
177                 lcore_config[lcore_id].core_id = cpu_core_id(lcore_id);
178                 lcore_config[lcore_id].socket_id = eal_cpu_socket_id(lcore_id);
179                 if (lcore_config[lcore_id].socket_id >= RTE_MAX_NUMA_NODES)
180 #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
181                         lcore_config[lcore_id].socket_id = 0;
182 #else
183                         rte_panic("Socket ID (%u) is greater than "
184                                 "RTE_MAX_NUMA_NODES (%d)\n",
185                                 lcore_config[lcore_id].socket_id, RTE_MAX_NUMA_NODES);
186 #endif
187                 RTE_LOG(DEBUG, EAL, "Detected lcore %u as core %u on socket %u\n",
188                                 lcore_id,
189                                 lcore_config[lcore_id].core_id,
190                                 lcore_config[lcore_id].socket_id);
191                 count ++;
192         }
193         /* Set the count of enabled logical cores of the EAL configuration */
194         config->lcore_count = count;
195         RTE_LOG(DEBUG, EAL, "Support maximum %u logical core(s) by configuration.\n",
196                 RTE_MAX_LCORE);
197         RTE_LOG(DEBUG, EAL, "Detected %u lcore(s)\n", config->lcore_count);
198
199         return 0;
200 }