c4e6492bb253d1360bb2c2a74d50ac8169a24a16
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_lcore.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <unistd.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <dirent.h>
39
40 #include <rte_log.h>
41 #include <rte_eal.h>
42 #include <rte_lcore.h>
43 #include <rte_common.h>
44 #include <rte_string_fns.h>
45 #include <rte_debug.h>
46
47 #include "eal_private.h"
48 #include "eal_filesystem.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 = rte_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 static unsigned
76 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;
82         unsigned long id = 0;
83         struct dirent *e;
84         char *endptr = NULL;
85
86         int len = rte_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         closedir(d);
102         if (endptr == NULL || *endptr!='\0' || endptr == e->d_name+prefix_len) {
103                 RTE_LOG(ERR, EAL, "Error reading numa node link "
104                                 "for lcore %u - using physical package id instead\n",
105                                 lcore_id);
106
107                 len = rte_snprintf(path, sizeof(path), SYS_CPU_DIR "/%s",
108                                 lcore_id, PHYS_PKG_FILE);
109                 if (len <= 0 || (unsigned)len >= sizeof(path))
110                         goto err;
111                 if (eal_parse_sysfs_value(path, &id) != 0)
112                         goto err;
113         }
114         return (unsigned)id;
115
116 err:
117         RTE_LOG(ERR, EAL, "Error getting NUMA socket information from %s "
118                         "for lcore %u - assuming NUMA socket 0\n", SYS_CPU_DIR, lcore_id);
119         return 0;
120 }
121
122 /* Get the cpu core id value from the /sys/.../cpuX core_id value */
123 static unsigned
124 cpu_core_id(unsigned lcore_id)
125 {
126         char path[PATH_MAX];
127         unsigned long id;
128
129         int len = rte_snprintf(path, sizeof(path), SYS_CPU_DIR "/%s", lcore_id, CORE_ID_FILE);
130         if (len <= 0 || (unsigned)len >= sizeof(path))
131                 goto err;
132         if (eal_parse_sysfs_value(path, &id) != 0)
133                 goto err;
134         return (unsigned)id;
135
136 err:
137         RTE_LOG(ERR, EAL, "Error reading core id value from %s "
138                         "for lcore %u - assuming core 0\n", SYS_CPU_DIR, lcore_id);
139         return 0;
140 }
141
142 /*
143  * Parse /sys/devices/system/cpu to get the number of physical and logical
144  * processors on the machine. The function will fill the cpu_info
145  * structure.
146  */
147 int
148 rte_eal_cpu_init(void)
149 {
150         /* pointer to global configuration */
151         struct rte_config *config = rte_eal_get_configuration();
152         unsigned lcore_id;
153         unsigned count = 0;
154
155         /* disable lcores that were not detected */
156         RTE_LCORE_FOREACH(lcore_id) {
157
158                 lcore_config[lcore_id].detected = cpu_detected(lcore_id);
159                 if (lcore_config[lcore_id].detected == 0) {
160                         RTE_LOG(DEBUG, EAL, "Skip lcore %u (not detected)\n", lcore_id);
161                         config->lcore_role[lcore_id] = ROLE_OFF;
162                         continue;
163                 }
164                 lcore_config[lcore_id].core_id = cpu_core_id(lcore_id);
165                 lcore_config[lcore_id].socket_id = cpu_socket_id(lcore_id);
166                 if (lcore_config[lcore_id].socket_id >= RTE_MAX_NUMA_NODES)
167 #ifdef CONFIG_RTE_EAL_ALLOW_INV_SOCKET_ID
168                         lcore_config[lcore_id].socket_id = 0;
169 #else
170                         rte_panic("Socket ID (%u) is greater than "
171                                 "RTE_MAX_NUMA_NODES (%d)\n",
172                                 lcore_config[lcore_id].socket_id, RTE_MAX_NUMA_NODES);
173 #endif
174                 RTE_LOG(DEBUG, EAL, "Detected lcore %u as core %u on socket %u\n",
175                                 lcore_id,
176                                 lcore_config[lcore_id].core_id,
177                                 lcore_config[lcore_id].socket_id);
178                 count ++;
179         }
180
181         config->lcore_count = count;
182
183         return 0;
184 }