4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
42 #include <rte_lcore.h>
43 #include <rte_common.h>
44 #include <rte_string_fns.h>
45 #include <rte_debug.h>
47 #include "eal_private.h"
48 #include "eal_filesystem.h"
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"
54 /* Check if a cpu is present by the presence of the cpu information for it */
56 cpu_detected(unsigned lcore_id)
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))
63 if (access(path, F_OK) != 0)
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
76 cpu_socket_id(unsigned lcore_id)
78 const char node_prefix[] = "node";
79 const size_t prefix_len = sizeof(node_prefix) - 1;
86 int len = rte_snprintf(path, sizeof(path),
87 SYS_CPU_DIR, lcore_id);
88 if (len <= 0 || (unsigned)len >= sizeof(path))
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);
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",
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))
111 if (eal_parse_sysfs_value(path, &id) != 0)
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);
122 /* Get the cpu core id value from the /sys/.../cpuX core_id value */
124 cpu_core_id(unsigned lcore_id)
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))
132 if (eal_parse_sysfs_value(path, &id) != 0)
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);
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
148 rte_eal_cpu_init(void)
150 /* pointer to global configuration */
151 struct rte_config *config = rte_eal_get_configuration();
155 /* disable lcores that were not detected */
156 RTE_LCORE_FOREACH(lcore_id) {
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;
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;
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);
174 RTE_LOG(DEBUG, EAL, "Detected lcore %u as core %u on socket %u\n",
176 lcore_config[lcore_id].core_id,
177 lcore_config[lcore_id].socket_id);
181 config->lcore_count = count;