^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <sys/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <sys/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <api/fs/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "cputopo.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "cpumap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "env.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define CORE_SIB_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) "%s/devices/system/cpu/cpu%d/topology/core_siblings_list"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define DIE_SIB_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) "%s/devices/system/cpu/cpu%d/topology/die_cpus_list"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define THRD_SIB_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) "%s/devices/system/cpu/cpu%d/topology/thread_siblings_list"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define THRD_SIB_FMT_NEW \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) "%s/devices/system/cpu/cpu%d/topology/core_cpus_list"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define NODE_ONLINE_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) "%s/devices/system/node/online"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define NODE_MEMINFO_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) "%s/devices/system/node/node%d/meminfo"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define NODE_CPULIST_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) "%s/devices/system/node/node%d/cpulist"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int build_cpu_topology(struct cpu_topology *tp, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) char filename[MAXPATHLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) char *buf = NULL, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ssize_t sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) u32 i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) scnprintf(filename, MAXPATHLEN, CORE_SIB_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) sysfs__mountpoint(), cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) fp = fopen(filename, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) goto try_dies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) sret = getline(&buf, &len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (sret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto try_dies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) for (i = 0; i < tp->core_sib; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!strcmp(buf, tp->core_siblings[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (i == tp->core_sib) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) tp->core_siblings[i] = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) tp->core_sib++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) try_dies:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!tp->die_siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) goto try_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) scnprintf(filename, MAXPATHLEN, DIE_SIB_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) sysfs__mountpoint(), cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fp = fopen(filename, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto try_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) sret = getline(&buf, &len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (sret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) goto try_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (i = 0; i < tp->die_sib; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!strcmp(buf, tp->die_siblings[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (i == tp->die_sib) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) tp->die_siblings[i] = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) tp->die_sib++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) try_threads:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) scnprintf(filename, MAXPATHLEN, THRD_SIB_FMT_NEW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sysfs__mountpoint(), cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (access(filename, F_OK) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) scnprintf(filename, MAXPATHLEN, THRD_SIB_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) sysfs__mountpoint(), cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fp = fopen(filename, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (getline(&buf, &len, fp) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) for (i = 0; i < tp->thread_sib; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!strcmp(buf, tp->thread_siblings[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (i == tp->thread_sib) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) tp->thread_siblings[i] = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) tp->thread_sib++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) void cpu_topology__delete(struct cpu_topology *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) for (i = 0 ; i < tp->core_sib; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) zfree(&tp->core_siblings[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (tp->die_sib) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) for (i = 0 ; i < tp->die_sib; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) zfree(&tp->die_siblings[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (i = 0 ; i < tp->thread_sib; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) zfree(&tp->thread_siblings[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) free(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static bool has_die_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) char filename[MAXPATHLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct utsname uts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (uname(&uts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (strncmp(uts.machine, "x86_64", 6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) scnprintf(filename, MAXPATHLEN, DIE_SIB_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) sysfs__mountpoint(), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (access(filename, F_OK) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct cpu_topology *cpu_topology__new(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct cpu_topology *tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) u32 nr, i, nr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) size_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) long ncpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct perf_cpu_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) bool has_die = has_die_topology();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ncpus = cpu__max_present_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* build online CPU map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) map = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (map == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pr_debug("failed to get system cpumap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) nr = (u32)(ncpus & UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) sz = nr * sizeof(char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (has_die)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) nr_addr = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) nr_addr = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) addr = calloc(1, sizeof(*tp) + nr_addr * sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) tp = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) addr += sizeof(*tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) tp->core_siblings = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) addr += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (has_die) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) tp->die_siblings = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) addr += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) tp->thread_siblings = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!cpu_map__has(map, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ret = build_cpu_topology(tp, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) perf_cpu_map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) cpu_topology__delete(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int load_numa_node(struct numa_topology_node *node, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) char str[MAXPATHLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) char field[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) char *buf = NULL, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) u64 mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) node->node = (u32) nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) scnprintf(str, MAXPATHLEN, NODE_MEMINFO_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) sysfs__mountpoint(), nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) fp = fopen(str, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) while (getline(&buf, &len, fp) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* skip over invalid lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (!strchr(buf, ':'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!strcmp(field, "MemTotal:"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) node->mem_total = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!strcmp(field, "MemFree:"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) node->mem_free = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (node->mem_total && node->mem_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) fp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) scnprintf(str, MAXPATHLEN, NODE_CPULIST_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) sysfs__mountpoint(), nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) fp = fopen(str, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (getline(&buf, &len, fp) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) node->cpus = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct numa_topology *numa_topology__new(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct perf_cpu_map *node_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct numa_topology *tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) char path[MAXPATHLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) u32 nr, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) char *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) scnprintf(path, MAXPATHLEN, NODE_ONLINE_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) sysfs__mountpoint());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) fp = fopen(path, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (getline(&buf, &len, fp) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) c = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *c = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) node_map = perf_cpu_map__new(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!node_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) nr = (u32) node_map->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0])*nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) tp->nr = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (load_numa_node(&tp->nodes[i], node_map->map[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) numa_topology__delete(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) perf_cpu_map__put(node_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) void numa_topology__delete(struct numa_topology *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) for (i = 0; i < tp->nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) zfree(&tp->nodes[i].cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) free(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }