Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_lcore.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <unistd.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <dirent.h>
38
39 #include <rte_log.h>
40 #include <rte_eal.h>
41 #include <rte_lcore.h>
42 #include <rte_common.h>
43 #include <rte_string_fns.h>
44 #include <rte_debug.h>
45
46 #include "eal_private.h"
47 #include "eal_filesystem.h"
48 #include "eal_thread.h"
49
50 #define SYS_CPU_DIR "/sys/devices/system/cpu/cpu%u"
51 #define CORE_ID_FILE "topology/core_id"
52 #define NUMA_NODE_PATH "/sys/devices/system/node"
53
54 /* Check if a cpu is present by the presence of the cpu information for it */
55 int
56 eal_cpu_detected(unsigned lcore_id)
57 {
58         char path[PATH_MAX];
59         int len = 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 /*
70  * Get CPU socket id (NUMA node) for a logical core.
71  *
72  * This searches each nodeX directories in /sys for the symlink for the given
73  * lcore_id and returns the numa node where the lcore is found. If lcore is not
74  * found on any numa node, returns zero.
75  */
76 unsigned
77 eal_cpu_socket_id(unsigned lcore_id)
78 {
79         unsigned socket;
80
81         for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
82                 char path[PATH_MAX];
83
84                 snprintf(path, sizeof(path), "%s/node%u/cpu%u", NUMA_NODE_PATH,
85                                 socket, lcore_id);
86                 if (access(path, F_OK) == 0)
87                         return socket;
88         }
89         return 0;
90 }
91
92 /* Get the cpu core id value from the /sys/.../cpuX core_id value */
93 unsigned
94 eal_cpu_core_id(unsigned lcore_id)
95 {
96         char path[PATH_MAX];
97         unsigned long id;
98
99         int len = snprintf(path, sizeof(path), SYS_CPU_DIR "/%s", lcore_id, CORE_ID_FILE);
100         if (len <= 0 || (unsigned)len >= sizeof(path))
101                 goto err;
102         if (eal_parse_sysfs_value(path, &id) != 0)
103                 goto err;
104         return (unsigned)id;
105
106 err:
107         RTE_LOG(ERR, EAL, "Error reading core id value from %s "
108                         "for lcore %u - assuming core 0\n", SYS_CPU_DIR, lcore_id);
109         return 0;
110 }