1 // SPDX-License-Identifier: GPL-2.0
3 #include <sys/utsname.h>
8 #include <linux/zalloc.h>
9 #include <perf/cpumap.h>
15 #include "pmu-hybrid.h"
17 #define PACKAGE_CPUS_FMT \
18 "%s/devices/system/cpu/cpu%d/topology/package_cpus_list"
19 #define PACKAGE_CPUS_FMT_OLD \
20 "%s/devices/system/cpu/cpu%d/topology/core_siblings_list"
21 #define DIE_CPUS_FMT \
22 "%s/devices/system/cpu/cpu%d/topology/die_cpus_list"
23 #define CORE_CPUS_FMT \
24 "%s/devices/system/cpu/cpu%d/topology/core_cpus_list"
25 #define CORE_CPUS_FMT_OLD \
26 "%s/devices/system/cpu/cpu%d/topology/thread_siblings_list"
27 #define NODE_ONLINE_FMT \
28 "%s/devices/system/node/online"
29 #define NODE_MEMINFO_FMT \
30 "%s/devices/system/node/node%d/meminfo"
31 #define NODE_CPULIST_FMT \
32 "%s/devices/system/node/node%d/cpulist"
34 static int build_cpu_topology(struct cpu_topology *tp, int cpu)
37 char filename[MAXPATHLEN];
44 scnprintf(filename, MAXPATHLEN, PACKAGE_CPUS_FMT,
45 sysfs__mountpoint(), cpu);
46 if (access(filename, F_OK) == -1) {
47 scnprintf(filename, MAXPATHLEN, PACKAGE_CPUS_FMT_OLD,
48 sysfs__mountpoint(), cpu);
50 fp = fopen(filename, "r");
54 sret = getline(&buf, &len, fp);
59 p = strchr(buf, '\n');
63 for (i = 0; i < tp->package_cpus_lists; i++) {
64 if (!strcmp(buf, tp->package_cpus_list[i]))
67 if (i == tp->package_cpus_lists) {
68 tp->package_cpus_list[i] = buf;
69 tp->package_cpus_lists++;
76 if (!tp->die_cpus_list)
79 scnprintf(filename, MAXPATHLEN, DIE_CPUS_FMT,
80 sysfs__mountpoint(), cpu);
81 fp = fopen(filename, "r");
85 sret = getline(&buf, &len, fp);
90 p = strchr(buf, '\n');
94 for (i = 0; i < tp->die_cpus_lists; i++) {
95 if (!strcmp(buf, tp->die_cpus_list[i]))
98 if (i == tp->die_cpus_lists) {
99 tp->die_cpus_list[i] = buf;
100 tp->die_cpus_lists++;
107 scnprintf(filename, MAXPATHLEN, CORE_CPUS_FMT,
108 sysfs__mountpoint(), cpu);
109 if (access(filename, F_OK) == -1) {
110 scnprintf(filename, MAXPATHLEN, CORE_CPUS_FMT_OLD,
111 sysfs__mountpoint(), cpu);
113 fp = fopen(filename, "r");
117 if (getline(&buf, &len, fp) <= 0)
120 p = strchr(buf, '\n');
124 for (i = 0; i < tp->core_cpus_lists; i++) {
125 if (!strcmp(buf, tp->core_cpus_list[i]))
128 if (i == tp->core_cpus_lists) {
129 tp->core_cpus_list[i] = buf;
130 tp->core_cpus_lists++;
141 void cpu_topology__delete(struct cpu_topology *tp)
148 for (i = 0 ; i < tp->package_cpus_lists; i++)
149 zfree(&tp->package_cpus_list[i]);
151 for (i = 0 ; i < tp->die_cpus_lists; i++)
152 zfree(&tp->die_cpus_list[i]);
154 for (i = 0 ; i < tp->core_cpus_lists; i++)
155 zfree(&tp->core_cpus_list[i]);
160 static bool has_die_topology(void)
162 char filename[MAXPATHLEN];
168 if (strncmp(uts.machine, "x86_64", 6))
171 scnprintf(filename, MAXPATHLEN, DIE_CPUS_FMT,
172 sysfs__mountpoint(), 0);
173 if (access(filename, F_OK) == -1)
179 struct cpu_topology *cpu_topology__new(void)
181 struct cpu_topology *tp = NULL;
187 struct perf_cpu_map *map;
188 bool has_die = has_die_topology();
190 ncpus = cpu__max_present_cpu().cpu;
192 /* build online CPU map */
193 map = perf_cpu_map__new(NULL);
195 pr_debug("failed to get system cpumap\n");
199 nr = (u32)(ncpus & UINT_MAX);
201 sz = nr * sizeof(char *);
206 addr = calloc(1, sizeof(*tp) + nr_addr * sz);
212 tp->package_cpus_list = addr;
215 tp->die_cpus_list = addr;
218 tp->core_cpus_list = addr;
220 for (i = 0; i < nr; i++) {
221 if (!perf_cpu_map__has(map, (struct perf_cpu){ .cpu = i }))
224 ret = build_cpu_topology(tp, i);
230 perf_cpu_map__put(map);
232 cpu_topology__delete(tp);
238 static int load_numa_node(struct numa_topology_node *node, int nr)
240 char str[MAXPATHLEN];
242 char *buf = NULL, *p;
248 node->node = (u32) nr;
250 scnprintf(str, MAXPATHLEN, NODE_MEMINFO_FMT,
251 sysfs__mountpoint(), nr);
252 fp = fopen(str, "r");
256 while (getline(&buf, &len, fp) > 0) {
257 /* skip over invalid lines */
258 if (!strchr(buf, ':'))
260 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
262 if (!strcmp(field, "MemTotal:"))
263 node->mem_total = mem;
264 if (!strcmp(field, "MemFree:"))
265 node->mem_free = mem;
266 if (node->mem_total && node->mem_free)
273 scnprintf(str, MAXPATHLEN, NODE_CPULIST_FMT,
274 sysfs__mountpoint(), nr);
276 fp = fopen(str, "r");
280 if (getline(&buf, &len, fp) <= 0)
283 p = strchr(buf, '\n');
298 struct numa_topology *numa_topology__new(void)
300 struct perf_cpu_map *node_map = NULL;
301 struct numa_topology *tp = NULL;
302 char path[MAXPATHLEN];
309 scnprintf(path, MAXPATHLEN, NODE_ONLINE_FMT,
310 sysfs__mountpoint());
312 fp = fopen(path, "r");
316 if (getline(&buf, &len, fp) <= 0)
319 c = strchr(buf, '\n');
323 node_map = perf_cpu_map__new(buf);
327 nr = (u32) node_map->nr;
329 tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0])*nr);
335 for (i = 0; i < nr; i++) {
336 if (load_numa_node(&tp->nodes[i], node_map->map[i].cpu)) {
337 numa_topology__delete(tp);
346 perf_cpu_map__put(node_map);
350 void numa_topology__delete(struct numa_topology *tp)
354 for (i = 0; i < tp->nr; i++)
355 zfree(&tp->nodes[i].cpus);
360 static int load_hybrid_node(struct hybrid_topology_node *node,
361 struct perf_pmu *pmu)
365 char *buf = NULL, *p;
369 node->pmu_name = strdup(pmu->name);
373 sysfs = sysfs__mountpoint();
377 snprintf(path, PATH_MAX, CPUS_TEMPLATE_CPU, sysfs, pmu->name);
378 fp = fopen(path, "r");
382 if (getline(&buf, &len, fp) <= 0) {
387 p = strchr(buf, '\n');
396 zfree(&node->pmu_name);
401 struct hybrid_topology *hybrid_topology__new(void)
403 struct perf_pmu *pmu;
404 struct hybrid_topology *tp = NULL;
407 nr = perf_pmu__hybrid_pmu_num();
411 tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0]) * nr);
416 perf_pmu__for_each_hybrid_pmu(pmu) {
417 if (load_hybrid_node(&tp->nodes[i], pmu)) {
418 hybrid_topology__delete(tp);
427 void hybrid_topology__delete(struct hybrid_topology *tp)
431 for (i = 0; i < tp->nr; i++) {
432 zfree(&tp->nodes[i].pmu_name);
433 zfree(&tp->nodes[i].cpus);