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 8 #include <rte_log.h> 9 10 #include "eal_private.h" 11 #include "eal_filesystem.h" 12 #include "eal_thread.h" 13 14 #define SYS_CPU_DIR "/sys/devices/system/cpu/cpu%u" 15 #define CORE_ID_FILE "topology/core_id" 16 #define NUMA_NODE_PATH "/sys/devices/system/node" 17 18 /* Check if a cpu is present by the presence of the cpu information for it */ 19 int 20 eal_cpu_detected(unsigned lcore_id) 21 { 22 char path[PATH_MAX]; 23 int len = snprintf(path, sizeof(path), SYS_CPU_DIR 24 "/"CORE_ID_FILE, lcore_id); 25 if (len <= 0 || (unsigned)len >= sizeof(path)) 26 return 0; 27 if (access(path, F_OK) != 0) 28 return 0; 29 30 return 1; 31 } 32 33 /* 34 * Get CPU socket id (NUMA node) for a logical core. 35 * 36 * This searches each nodeX directories in /sys for the symlink for the given 37 * lcore_id and returns the numa node where the lcore is found. If lcore is not 38 * found on any numa node, returns zero. 39 */ 40 unsigned 41 eal_cpu_socket_id(unsigned lcore_id) 42 { 43 unsigned socket; 44 45 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) { 46 char path[PATH_MAX]; 47 48 snprintf(path, sizeof(path), "%s/node%u/cpu%u", NUMA_NODE_PATH, 49 socket, lcore_id); 50 if (access(path, F_OK) == 0) 51 return socket; 52 } 53 return 0; 54 } 55 56 /* Get the cpu core id value from the /sys/.../cpuX core_id value */ 57 unsigned 58 eal_cpu_core_id(unsigned lcore_id) 59 { 60 char path[PATH_MAX]; 61 unsigned long id; 62 63 int len = snprintf(path, sizeof(path), SYS_CPU_DIR "/%s", lcore_id, CORE_ID_FILE); 64 if (len <= 0 || (unsigned)len >= sizeof(path)) 65 goto err; 66 if (eal_parse_sysfs_value(path, &id) != 0) 67 goto err; 68 return (unsigned)id; 69 70 err: 71 EAL_LOG(ERR, "Error reading core id value from %s " 72 "for lcore %u - assuming core 0", SYS_CPU_DIR, lcore_id); 73 return 0; 74 } 75