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 bool cpu_topology__smt_on(const struct cpu_topology *topology)
162 for (u32 i = 0; i < topology->core_cpus_lists; i++) {
163 const char *cpu_list = topology->core_cpus_list[i];
166 * If there is a need to separate siblings in a core then SMT is
169 if (strchr(cpu_list, ',') || strchr(cpu_list, '-'))
175 bool cpu_topology__core_wide(const struct cpu_topology *topology,
176 const char *user_requested_cpu_list)
178 struct perf_cpu_map *user_requested_cpus;
181 * If user_requested_cpu_list is empty then all CPUs are recorded and so
184 if (!user_requested_cpu_list)
187 user_requested_cpus = perf_cpu_map__new(user_requested_cpu_list);
188 /* Check that every user requested CPU is the complete set of SMT threads on a core. */
189 for (u32 i = 0; i < topology->core_cpus_lists; i++) {
190 const char *core_cpu_list = topology->core_cpus_list[i];
191 struct perf_cpu_map *core_cpus = perf_cpu_map__new(core_cpu_list);
194 bool has_first, first = true;
196 perf_cpu_map__for_each_cpu(cpu, idx, core_cpus) {
198 has_first = perf_cpu_map__has(user_requested_cpus, cpu);
202 * If the first core CPU is user requested then
203 * all subsequent CPUs in the core must be user
204 * requested too. If the first CPU isn't user
205 * requested then none of the others must be
208 if (perf_cpu_map__has(user_requested_cpus, cpu) != has_first) {
209 perf_cpu_map__put(core_cpus);
210 perf_cpu_map__put(user_requested_cpus);
215 perf_cpu_map__put(core_cpus);
217 perf_cpu_map__put(user_requested_cpus);
221 static bool has_die_topology(void)
223 char filename[MAXPATHLEN];
229 if (strncmp(uts.machine, "x86_64", 6) &&
230 strncmp(uts.machine, "s390x", 5))
233 scnprintf(filename, MAXPATHLEN, DIE_CPUS_FMT,
234 sysfs__mountpoint(), 0);
235 if (access(filename, F_OK) == -1)
241 struct cpu_topology *cpu_topology__new(void)
243 struct cpu_topology *tp = NULL;
249 struct perf_cpu_map *map;
250 bool has_die = has_die_topology();
252 ncpus = cpu__max_present_cpu().cpu;
254 /* build online CPU map */
255 map = perf_cpu_map__new(NULL);
257 pr_debug("failed to get system cpumap\n");
261 nr = (u32)(ncpus & UINT_MAX);
263 sz = nr * sizeof(char *);
268 addr = calloc(1, sizeof(*tp) + nr_addr * sz);
274 tp->package_cpus_list = addr;
277 tp->die_cpus_list = addr;
280 tp->core_cpus_list = addr;
282 for (i = 0; i < nr; i++) {
283 if (!perf_cpu_map__has(map, (struct perf_cpu){ .cpu = i }))
286 ret = build_cpu_topology(tp, i);
292 perf_cpu_map__put(map);
294 cpu_topology__delete(tp);
300 static int load_numa_node(struct numa_topology_node *node, int nr)
302 char str[MAXPATHLEN];
304 char *buf = NULL, *p;
310 node->node = (u32) nr;
312 scnprintf(str, MAXPATHLEN, NODE_MEMINFO_FMT,
313 sysfs__mountpoint(), nr);
314 fp = fopen(str, "r");
318 while (getline(&buf, &len, fp) > 0) {
319 /* skip over invalid lines */
320 if (!strchr(buf, ':'))
322 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
324 if (!strcmp(field, "MemTotal:"))
325 node->mem_total = mem;
326 if (!strcmp(field, "MemFree:"))
327 node->mem_free = mem;
328 if (node->mem_total && node->mem_free)
335 scnprintf(str, MAXPATHLEN, NODE_CPULIST_FMT,
336 sysfs__mountpoint(), nr);
338 fp = fopen(str, "r");
342 if (getline(&buf, &len, fp) <= 0)
345 p = strchr(buf, '\n');
360 struct numa_topology *numa_topology__new(void)
362 struct perf_cpu_map *node_map = NULL;
363 struct numa_topology *tp = NULL;
364 char path[MAXPATHLEN];
371 scnprintf(path, MAXPATHLEN, NODE_ONLINE_FMT,
372 sysfs__mountpoint());
374 fp = fopen(path, "r");
378 if (getline(&buf, &len, fp) <= 0)
381 c = strchr(buf, '\n');
385 node_map = perf_cpu_map__new(buf);
389 nr = (u32) perf_cpu_map__nr(node_map);
391 tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0])*nr);
397 for (i = 0; i < nr; i++) {
398 if (load_numa_node(&tp->nodes[i], perf_cpu_map__cpu(node_map, i).cpu)) {
399 numa_topology__delete(tp);
408 perf_cpu_map__put(node_map);
412 void numa_topology__delete(struct numa_topology *tp)
416 for (i = 0; i < tp->nr; i++)
417 zfree(&tp->nodes[i].cpus);
422 static int load_hybrid_node(struct hybrid_topology_node *node,
423 struct perf_pmu *pmu)
427 char *buf = NULL, *p;
431 node->pmu_name = strdup(pmu->name);
435 sysfs = sysfs__mountpoint();
439 snprintf(path, PATH_MAX, CPUS_TEMPLATE_CPU, sysfs, pmu->name);
440 fp = fopen(path, "r");
444 if (getline(&buf, &len, fp) <= 0) {
449 p = strchr(buf, '\n');
458 zfree(&node->pmu_name);
463 struct hybrid_topology *hybrid_topology__new(void)
465 struct perf_pmu *pmu;
466 struct hybrid_topology *tp = NULL;
469 nr = perf_pmu__hybrid_pmu_num();
473 tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0]) * nr);
478 perf_pmu__for_each_hybrid_pmu(pmu) {
479 if (load_hybrid_node(&tp->nodes[i], pmu)) {
480 hybrid_topology__delete(tp);
489 void hybrid_topology__delete(struct hybrid_topology *tp)
493 for (i = 0; i < tp->nr; i++) {
494 zfree(&tp->nodes[i].pmu_name);
495 zfree(&tp->nodes[i].cpus);