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