aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@kernel.org>2019-02-19 04:58:15 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2019-02-19 10:21:10 -0500
commite19a01c1438e123d169fd09376a221d844797174 (patch)
treecb5d1b22fb596468d673ab88078f63900c4b35bf
parent48e6c5acd36873ff022cd97de866bfd0a36a3b99 (diff)
perf tools: Use sysfs__mountpoint() when reading cpu topology
Use sysfs__mountpoint() when reading sysfs files to obtain cpu/numa topologies. Also use scnprintf instead of sprintf as suggested by Namhyung. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20190219095815.15931-5-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/util/cputopo.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/tools/perf/util/cputopo.c b/tools/perf/util/cputopo.c
index 83ffca2ea9ee..ece0710249d4 100644
--- a/tools/perf/util/cputopo.c
+++ b/tools/perf/util/cputopo.c
@@ -1,6 +1,7 @@
1// SPDX-License-Identifier: GPL-2.0 1// SPDX-License-Identifier: GPL-2.0
2#include <sys/param.h> 2#include <sys/param.h>
3#include <inttypes.h> 3#include <inttypes.h>
4#include <api/fs/fs.h>
4 5
5#include "cputopo.h" 6#include "cputopo.h"
6#include "cpumap.h" 7#include "cpumap.h"
@@ -9,9 +10,15 @@
9 10
10 11
11#define CORE_SIB_FMT \ 12#define CORE_SIB_FMT \
12 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" 13 "%s/devices/system/cpu/cpu%d/topology/core_siblings_list"
13#define THRD_SIB_FMT \ 14#define THRD_SIB_FMT \
14 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" 15 "%s/devices/system/cpu/cpu%d/topology/thread_siblings_list"
16#define NODE_ONLINE_FMT \
17 "%s/devices/system/node/online"
18#define NODE_MEMINFO_FMT \
19 "%s/devices/system/node/node%d/meminfo"
20#define NODE_CPULIST_FMT \
21 "%s/devices/system/node/node%d/cpulist"
15 22
16static int build_cpu_topology(struct cpu_topology *tp, int cpu) 23static int build_cpu_topology(struct cpu_topology *tp, int cpu)
17{ 24{
@@ -23,7 +30,8 @@ static int build_cpu_topology(struct cpu_topology *tp, int cpu)
23 u32 i = 0; 30 u32 i = 0;
24 int ret = -1; 31 int ret = -1;
25 32
26 sprintf(filename, CORE_SIB_FMT, cpu); 33 scnprintf(filename, MAXPATHLEN, CORE_SIB_FMT,
34 sysfs__mountpoint(), cpu);
27 fp = fopen(filename, "r"); 35 fp = fopen(filename, "r");
28 if (!fp) 36 if (!fp)
29 goto try_threads; 37 goto try_threads;
@@ -50,7 +58,8 @@ static int build_cpu_topology(struct cpu_topology *tp, int cpu)
50 ret = 0; 58 ret = 0;
51 59
52try_threads: 60try_threads:
53 sprintf(filename, THRD_SIB_FMT, cpu); 61 scnprintf(filename, MAXPATHLEN, THRD_SIB_FMT,
62 sysfs__mountpoint(), cpu);
54 fp = fopen(filename, "r"); 63 fp = fopen(filename, "r");
55 if (!fp) 64 if (!fp)
56 goto done; 65 goto done;
@@ -157,7 +166,8 @@ static int load_numa_node(struct numa_topology_node *node, int nr)
157 166
158 node->node = (u32) nr; 167 node->node = (u32) nr;
159 168
160 sprintf(str, "/sys/devices/system/node/node%d/meminfo", nr); 169 scnprintf(str, MAXPATHLEN, NODE_MEMINFO_FMT,
170 sysfs__mountpoint(), nr);
161 fp = fopen(str, "r"); 171 fp = fopen(str, "r");
162 if (!fp) 172 if (!fp)
163 return -1; 173 return -1;
@@ -179,7 +189,8 @@ static int load_numa_node(struct numa_topology_node *node, int nr)
179 fclose(fp); 189 fclose(fp);
180 fp = NULL; 190 fp = NULL;
181 191
182 sprintf(str, "/sys/devices/system/node/node%d/cpulist", nr); 192 scnprintf(str, MAXPATHLEN, NODE_CPULIST_FMT,
193 sysfs__mountpoint(), nr);
183 194
184 fp = fopen(str, "r"); 195 fp = fopen(str, "r");
185 if (!fp) 196 if (!fp)
@@ -207,13 +218,17 @@ struct numa_topology *numa_topology__new(void)
207{ 218{
208 struct cpu_map *node_map = NULL; 219 struct cpu_map *node_map = NULL;
209 struct numa_topology *tp = NULL; 220 struct numa_topology *tp = NULL;
221 char path[MAXPATHLEN];
210 char *buf = NULL; 222 char *buf = NULL;
211 size_t len = 0; 223 size_t len = 0;
212 u32 nr, i; 224 u32 nr, i;
213 FILE *fp; 225 FILE *fp;
214 char *c; 226 char *c;
215 227
216 fp = fopen("/sys/devices/system/node/online", "r"); 228 scnprintf(path, MAXPATHLEN, NODE_ONLINE_FMT,
229 sysfs__mountpoint());
230
231 fp = fopen(path, "r");
217 if (!fp) 232 if (!fp)
218 return NULL; 233 return NULL;
219 234