xref: /dpdk/lib/eal/linux/eal_lcore.c (revision ae67895b507bb6af22263c79ba0d5c374b396485)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <unistd.h>
699a2dd95SBruce Richardson #include <limits.h>
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include <rte_log.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include "eal_private.h"
1199a2dd95SBruce Richardson #include "eal_filesystem.h"
1299a2dd95SBruce Richardson #include "eal_thread.h"
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson #define SYS_CPU_DIR "/sys/devices/system/cpu/cpu%u"
1599a2dd95SBruce Richardson #define CORE_ID_FILE "topology/core_id"
1699a2dd95SBruce Richardson #define NUMA_NODE_PATH "/sys/devices/system/node"
1799a2dd95SBruce Richardson 
1899a2dd95SBruce Richardson /* Check if a cpu is present by the presence of the cpu information for it */
1999a2dd95SBruce Richardson int
eal_cpu_detected(unsigned lcore_id)2099a2dd95SBruce Richardson eal_cpu_detected(unsigned lcore_id)
2199a2dd95SBruce Richardson {
2299a2dd95SBruce Richardson 	char path[PATH_MAX];
2399a2dd95SBruce Richardson 	int len = snprintf(path, sizeof(path), SYS_CPU_DIR
2499a2dd95SBruce Richardson 		"/"CORE_ID_FILE, lcore_id);
2599a2dd95SBruce Richardson 	if (len <= 0 || (unsigned)len >= sizeof(path))
2699a2dd95SBruce Richardson 		return 0;
2799a2dd95SBruce Richardson 	if (access(path, F_OK) != 0)
2899a2dd95SBruce Richardson 		return 0;
2999a2dd95SBruce Richardson 
3099a2dd95SBruce Richardson 	return 1;
3199a2dd95SBruce Richardson }
3299a2dd95SBruce Richardson 
3399a2dd95SBruce Richardson /*
3499a2dd95SBruce Richardson  * Get CPU socket id (NUMA node) for a logical core.
3599a2dd95SBruce Richardson  *
3699a2dd95SBruce Richardson  * This searches each nodeX directories in /sys for the symlink for the given
3799a2dd95SBruce Richardson  * lcore_id and returns the numa node where the lcore is found. If lcore is not
3899a2dd95SBruce Richardson  * found on any numa node, returns zero.
3999a2dd95SBruce Richardson  */
4099a2dd95SBruce Richardson unsigned
eal_cpu_socket_id(unsigned lcore_id)4199a2dd95SBruce Richardson eal_cpu_socket_id(unsigned lcore_id)
4299a2dd95SBruce Richardson {
4399a2dd95SBruce Richardson 	unsigned socket;
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson 	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
4699a2dd95SBruce Richardson 		char path[PATH_MAX];
4799a2dd95SBruce Richardson 
4899a2dd95SBruce Richardson 		snprintf(path, sizeof(path), "%s/node%u/cpu%u", NUMA_NODE_PATH,
4999a2dd95SBruce Richardson 				socket, lcore_id);
5099a2dd95SBruce Richardson 		if (access(path, F_OK) == 0)
5199a2dd95SBruce Richardson 			return socket;
5299a2dd95SBruce Richardson 	}
5399a2dd95SBruce Richardson 	return 0;
5499a2dd95SBruce Richardson }
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson /* Get the cpu core id value from the /sys/.../cpuX core_id value */
5799a2dd95SBruce Richardson unsigned
eal_cpu_core_id(unsigned lcore_id)5899a2dd95SBruce Richardson eal_cpu_core_id(unsigned lcore_id)
5999a2dd95SBruce Richardson {
6099a2dd95SBruce Richardson 	char path[PATH_MAX];
6199a2dd95SBruce Richardson 	unsigned long id;
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson 	int len = snprintf(path, sizeof(path), SYS_CPU_DIR "/%s", lcore_id, CORE_ID_FILE);
6499a2dd95SBruce Richardson 	if (len <= 0 || (unsigned)len >= sizeof(path))
6599a2dd95SBruce Richardson 		goto err;
6699a2dd95SBruce Richardson 	if (eal_parse_sysfs_value(path, &id) != 0)
6799a2dd95SBruce Richardson 		goto err;
6899a2dd95SBruce Richardson 	return (unsigned)id;
6999a2dd95SBruce Richardson 
7099a2dd95SBruce Richardson err:
71*ae67895bSDavid Marchand 	EAL_LOG(ERR, "Error reading core id value from %s "
72*ae67895bSDavid Marchand 			"for lcore %u - assuming core 0", SYS_CPU_DIR, lcore_id);
7399a2dd95SBruce Richardson 	return 0;
7499a2dd95SBruce Richardson }
75