Commit | Line | Data |
---|---|---|
5135d5ef JO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include <sys/param.h> | |
acae8b36 | 3 | #include <sys/utsname.h> |
48e6c5ac | 4 | #include <inttypes.h> |
215a0d30 | 5 | #include <stdlib.h> |
5e51b0bb | 6 | #include <string.h> |
e19a01c1 | 7 | #include <api/fs/fs.h> |
7f7c536f | 8 | #include <linux/zalloc.h> |
9c3516d1 | 9 | #include <perf/cpumap.h> |
5135d5ef JO |
10 | |
11 | #include "cputopo.h" | |
12 | #include "cpumap.h" | |
5e51b0bb | 13 | #include "debug.h" |
48e6c5ac | 14 | #include "env.h" |
1215795c | 15 | #include "pmu.h" |
1eaf496e | 16 | #include "pmus.h" |
5135d5ef | 17 | |
48f07b0b IR |
18 | #define PACKAGE_CPUS_FMT \ |
19 | "%s/devices/system/cpu/cpu%d/topology/package_cpus_list" | |
20 | #define PACKAGE_CPUS_FMT_OLD \ | |
e19a01c1 | 21 | "%s/devices/system/cpu/cpu%d/topology/core_siblings_list" |
406018dc | 22 | #define DIE_CPUS_FMT \ |
acae8b36 | 23 | "%s/devices/system/cpu/cpu%d/topology/die_cpus_list" |
0b6b84cc | 24 | #define CORE_CPUS_FMT \ |
0ccdb840 | 25 | "%s/devices/system/cpu/cpu%d/topology/core_cpus_list" |
0b6b84cc IR |
26 | #define CORE_CPUS_FMT_OLD \ |
27 | "%s/devices/system/cpu/cpu%d/topology/thread_siblings_list" | |
e19a01c1 JO |
28 | #define NODE_ONLINE_FMT \ |
29 | "%s/devices/system/node/online" | |
30 | #define NODE_MEMINFO_FMT \ | |
31 | "%s/devices/system/node/node%d/meminfo" | |
32 | #define NODE_CPULIST_FMT \ | |
33 | "%s/devices/system/node/node%d/cpulist" | |
5135d5ef JO |
34 | |
35 | static int build_cpu_topology(struct cpu_topology *tp, int cpu) | |
36 | { | |
37 | FILE *fp; | |
38 | char filename[MAXPATHLEN]; | |
39 | char *buf = NULL, *p; | |
40 | size_t len = 0; | |
41 | ssize_t sret; | |
42 | u32 i = 0; | |
43 | int ret = -1; | |
44 | ||
48f07b0b | 45 | scnprintf(filename, MAXPATHLEN, PACKAGE_CPUS_FMT, |
e19a01c1 | 46 | sysfs__mountpoint(), cpu); |
48f07b0b IR |
47 | if (access(filename, F_OK) == -1) { |
48 | scnprintf(filename, MAXPATHLEN, PACKAGE_CPUS_FMT_OLD, | |
49 | sysfs__mountpoint(), cpu); | |
50 | } | |
5135d5ef JO |
51 | fp = fopen(filename, "r"); |
52 | if (!fp) | |
acae8b36 | 53 | goto try_dies; |
5135d5ef JO |
54 | |
55 | sret = getline(&buf, &len, fp); | |
56 | fclose(fp); | |
57 | if (sret <= 0) | |
acae8b36 | 58 | goto try_dies; |
5135d5ef JO |
59 | |
60 | p = strchr(buf, '\n'); | |
61 | if (p) | |
62 | *p = '\0'; | |
63 | ||
48f07b0b IR |
64 | for (i = 0; i < tp->package_cpus_lists; i++) { |
65 | if (!strcmp(buf, tp->package_cpus_list[i])) | |
5135d5ef JO |
66 | break; |
67 | } | |
48f07b0b IR |
68 | if (i == tp->package_cpus_lists) { |
69 | tp->package_cpus_list[i] = buf; | |
70 | tp->package_cpus_lists++; | |
5135d5ef JO |
71 | buf = NULL; |
72 | len = 0; | |
73 | } | |
74 | ret = 0; | |
75 | ||
acae8b36 | 76 | try_dies: |
406018dc | 77 | if (!tp->die_cpus_list) |
acae8b36 KL |
78 | goto try_threads; |
79 | ||
406018dc | 80 | scnprintf(filename, MAXPATHLEN, DIE_CPUS_FMT, |
acae8b36 KL |
81 | sysfs__mountpoint(), cpu); |
82 | fp = fopen(filename, "r"); | |
83 | if (!fp) | |
84 | goto try_threads; | |
85 | ||
86 | sret = getline(&buf, &len, fp); | |
87 | fclose(fp); | |
88 | if (sret <= 0) | |
89 | goto try_threads; | |
90 | ||
91 | p = strchr(buf, '\n'); | |
92 | if (p) | |
93 | *p = '\0'; | |
94 | ||
406018dc IR |
95 | for (i = 0; i < tp->die_cpus_lists; i++) { |
96 | if (!strcmp(buf, tp->die_cpus_list[i])) | |
acae8b36 KL |
97 | break; |
98 | } | |
406018dc IR |
99 | if (i == tp->die_cpus_lists) { |
100 | tp->die_cpus_list[i] = buf; | |
101 | tp->die_cpus_lists++; | |
acae8b36 KL |
102 | buf = NULL; |
103 | len = 0; | |
104 | } | |
105 | ret = 0; | |
106 | ||
5135d5ef | 107 | try_threads: |
0b6b84cc | 108 | scnprintf(filename, MAXPATHLEN, CORE_CPUS_FMT, |
e19a01c1 | 109 | sysfs__mountpoint(), cpu); |
0ccdb840 | 110 | if (access(filename, F_OK) == -1) { |
0b6b84cc | 111 | scnprintf(filename, MAXPATHLEN, CORE_CPUS_FMT_OLD, |
0ccdb840 KL |
112 | sysfs__mountpoint(), cpu); |
113 | } | |
5135d5ef JO |
114 | fp = fopen(filename, "r"); |
115 | if (!fp) | |
116 | goto done; | |
117 | ||
118 | if (getline(&buf, &len, fp) <= 0) | |
119 | goto done; | |
120 | ||
121 | p = strchr(buf, '\n'); | |
122 | if (p) | |
123 | *p = '\0'; | |
124 | ||
0b6b84cc IR |
125 | for (i = 0; i < tp->core_cpus_lists; i++) { |
126 | if (!strcmp(buf, tp->core_cpus_list[i])) | |
5135d5ef JO |
127 | break; |
128 | } | |
0b6b84cc IR |
129 | if (i == tp->core_cpus_lists) { |
130 | tp->core_cpus_list[i] = buf; | |
131 | tp->core_cpus_lists++; | |
5135d5ef JO |
132 | buf = NULL; |
133 | } | |
134 | ret = 0; | |
135 | done: | |
136 | if (fp) | |
137 | fclose(fp); | |
138 | free(buf); | |
139 | return ret; | |
140 | } | |
141 | ||
142 | void cpu_topology__delete(struct cpu_topology *tp) | |
143 | { | |
144 | u32 i; | |
145 | ||
146 | if (!tp) | |
147 | return; | |
148 | ||
48f07b0b IR |
149 | for (i = 0 ; i < tp->package_cpus_lists; i++) |
150 | zfree(&tp->package_cpus_list[i]); | |
5135d5ef | 151 | |
406018dc IR |
152 | for (i = 0 ; i < tp->die_cpus_lists; i++) |
153 | zfree(&tp->die_cpus_list[i]); | |
acae8b36 | 154 | |
0b6b84cc IR |
155 | for (i = 0 ; i < tp->core_cpus_lists; i++) |
156 | zfree(&tp->core_cpus_list[i]); | |
5135d5ef JO |
157 | |
158 | free(tp); | |
159 | } | |
160 | ||
09b73fe9 IR |
161 | bool cpu_topology__smt_on(const struct cpu_topology *topology) |
162 | { | |
163 | for (u32 i = 0; i < topology->core_cpus_lists; i++) { | |
164 | const char *cpu_list = topology->core_cpus_list[i]; | |
165 | ||
166 | /* | |
167 | * If there is a need to separate siblings in a core then SMT is | |
168 | * enabled. | |
169 | */ | |
170 | if (strchr(cpu_list, ',') || strchr(cpu_list, '-')) | |
171 | return true; | |
172 | } | |
173 | return false; | |
174 | } | |
175 | ||
cc2c4e26 IR |
176 | bool cpu_topology__core_wide(const struct cpu_topology *topology, |
177 | const char *user_requested_cpu_list) | |
178 | { | |
179 | struct perf_cpu_map *user_requested_cpus; | |
180 | ||
181 | /* | |
182 | * If user_requested_cpu_list is empty then all CPUs are recorded and so | |
183 | * core_wide is true. | |
184 | */ | |
185 | if (!user_requested_cpu_list) | |
186 | return true; | |
187 | ||
188 | user_requested_cpus = perf_cpu_map__new(user_requested_cpu_list); | |
189 | /* Check that every user requested CPU is the complete set of SMT threads on a core. */ | |
190 | for (u32 i = 0; i < topology->core_cpus_lists; i++) { | |
191 | const char *core_cpu_list = topology->core_cpus_list[i]; | |
192 | struct perf_cpu_map *core_cpus = perf_cpu_map__new(core_cpu_list); | |
193 | struct perf_cpu cpu; | |
194 | int idx; | |
195 | bool has_first, first = true; | |
196 | ||
197 | perf_cpu_map__for_each_cpu(cpu, idx, core_cpus) { | |
198 | if (first) { | |
199 | has_first = perf_cpu_map__has(user_requested_cpus, cpu); | |
200 | first = false; | |
201 | } else { | |
202 | /* | |
203 | * If the first core CPU is user requested then | |
204 | * all subsequent CPUs in the core must be user | |
205 | * requested too. If the first CPU isn't user | |
206 | * requested then none of the others must be | |
207 | * too. | |
208 | */ | |
209 | if (perf_cpu_map__has(user_requested_cpus, cpu) != has_first) { | |
210 | perf_cpu_map__put(core_cpus); | |
211 | perf_cpu_map__put(user_requested_cpus); | |
212 | return false; | |
213 | } | |
214 | } | |
215 | } | |
216 | perf_cpu_map__put(core_cpus); | |
217 | } | |
218 | perf_cpu_map__put(user_requested_cpus); | |
219 | return true; | |
220 | } | |
221 | ||
acae8b36 KL |
222 | static bool has_die_topology(void) |
223 | { | |
224 | char filename[MAXPATHLEN]; | |
225 | struct utsname uts; | |
226 | ||
227 | if (uname(&uts) < 0) | |
228 | return false; | |
229 | ||
a6e62743 TR |
230 | if (strncmp(uts.machine, "x86_64", 6) && |
231 | strncmp(uts.machine, "s390x", 5)) | |
acae8b36 KL |
232 | return false; |
233 | ||
406018dc | 234 | scnprintf(filename, MAXPATHLEN, DIE_CPUS_FMT, |
acae8b36 KL |
235 | sysfs__mountpoint(), 0); |
236 | if (access(filename, F_OK) == -1) | |
237 | return false; | |
238 | ||
239 | return true; | |
240 | } | |
241 | ||
207f7df7 IR |
242 | const struct cpu_topology *online_topology(void) |
243 | { | |
244 | static const struct cpu_topology *topology; | |
245 | ||
246 | if (!topology) { | |
247 | topology = cpu_topology__new(); | |
248 | if (!topology) { | |
249 | pr_err("Error creating CPU topology"); | |
250 | abort(); | |
251 | } | |
252 | } | |
253 | return topology; | |
254 | } | |
255 | ||
5135d5ef JO |
256 | struct cpu_topology *cpu_topology__new(void) |
257 | { | |
258 | struct cpu_topology *tp = NULL; | |
259 | void *addr; | |
acae8b36 | 260 | u32 nr, i, nr_addr; |
5135d5ef JO |
261 | size_t sz; |
262 | long ncpus; | |
263 | int ret = -1; | |
f854839b | 264 | struct perf_cpu_map *map; |
acae8b36 | 265 | bool has_die = has_die_topology(); |
5135d5ef | 266 | |
6d18804b | 267 | ncpus = cpu__max_present_cpu().cpu; |
5135d5ef JO |
268 | |
269 | /* build online CPU map */ | |
effe957c | 270 | map = perf_cpu_map__new_online_cpus(); |
5135d5ef JO |
271 | if (map == NULL) { |
272 | pr_debug("failed to get system cpumap\n"); | |
273 | return NULL; | |
274 | } | |
275 | ||
276 | nr = (u32)(ncpus & UINT_MAX); | |
277 | ||
278 | sz = nr * sizeof(char *); | |
acae8b36 KL |
279 | if (has_die) |
280 | nr_addr = 3; | |
281 | else | |
282 | nr_addr = 2; | |
283 | addr = calloc(1, sizeof(*tp) + nr_addr * sz); | |
5135d5ef JO |
284 | if (!addr) |
285 | goto out_free; | |
286 | ||
287 | tp = addr; | |
288 | addr += sizeof(*tp); | |
48f07b0b | 289 | tp->package_cpus_list = addr; |
5135d5ef | 290 | addr += sz; |
acae8b36 | 291 | if (has_die) { |
406018dc | 292 | tp->die_cpus_list = addr; |
acae8b36 KL |
293 | addr += sz; |
294 | } | |
0b6b84cc | 295 | tp->core_cpus_list = addr; |
5135d5ef JO |
296 | |
297 | for (i = 0; i < nr; i++) { | |
6d18804b | 298 | if (!perf_cpu_map__has(map, (struct perf_cpu){ .cpu = i })) |
5135d5ef JO |
299 | continue; |
300 | ||
301 | ret = build_cpu_topology(tp, i); | |
302 | if (ret < 0) | |
303 | break; | |
304 | } | |
305 | ||
306 | out_free: | |
38f01d8d | 307 | perf_cpu_map__put(map); |
5135d5ef JO |
308 | if (ret) { |
309 | cpu_topology__delete(tp); | |
310 | tp = NULL; | |
311 | } | |
312 | return tp; | |
313 | } | |
48e6c5ac JO |
314 | |
315 | static int load_numa_node(struct numa_topology_node *node, int nr) | |
316 | { | |
317 | char str[MAXPATHLEN]; | |
318 | char field[32]; | |
319 | char *buf = NULL, *p; | |
320 | size_t len = 0; | |
321 | int ret = -1; | |
322 | FILE *fp; | |
323 | u64 mem; | |
324 | ||
325 | node->node = (u32) nr; | |
326 | ||
e19a01c1 JO |
327 | scnprintf(str, MAXPATHLEN, NODE_MEMINFO_FMT, |
328 | sysfs__mountpoint(), nr); | |
48e6c5ac JO |
329 | fp = fopen(str, "r"); |
330 | if (!fp) | |
331 | return -1; | |
332 | ||
333 | while (getline(&buf, &len, fp) > 0) { | |
334 | /* skip over invalid lines */ | |
335 | if (!strchr(buf, ':')) | |
336 | continue; | |
337 | if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2) | |
338 | goto err; | |
339 | if (!strcmp(field, "MemTotal:")) | |
340 | node->mem_total = mem; | |
341 | if (!strcmp(field, "MemFree:")) | |
342 | node->mem_free = mem; | |
343 | if (node->mem_total && node->mem_free) | |
344 | break; | |
345 | } | |
346 | ||
347 | fclose(fp); | |
348 | fp = NULL; | |
349 | ||
e19a01c1 JO |
350 | scnprintf(str, MAXPATHLEN, NODE_CPULIST_FMT, |
351 | sysfs__mountpoint(), nr); | |
48e6c5ac JO |
352 | |
353 | fp = fopen(str, "r"); | |
354 | if (!fp) | |
355 | return -1; | |
356 | ||
357 | if (getline(&buf, &len, fp) <= 0) | |
358 | goto err; | |
359 | ||
360 | p = strchr(buf, '\n'); | |
361 | if (p) | |
362 | *p = '\0'; | |
363 | ||
364 | node->cpus = buf; | |
365 | fclose(fp); | |
366 | return 0; | |
367 | ||
368 | err: | |
369 | free(buf); | |
370 | if (fp) | |
371 | fclose(fp); | |
372 | return ret; | |
373 | } | |
374 | ||
375 | struct numa_topology *numa_topology__new(void) | |
376 | { | |
f854839b | 377 | struct perf_cpu_map *node_map = NULL; |
48e6c5ac | 378 | struct numa_topology *tp = NULL; |
e19a01c1 | 379 | char path[MAXPATHLEN]; |
48e6c5ac JO |
380 | char *buf = NULL; |
381 | size_t len = 0; | |
382 | u32 nr, i; | |
383 | FILE *fp; | |
384 | char *c; | |
385 | ||
e19a01c1 JO |
386 | scnprintf(path, MAXPATHLEN, NODE_ONLINE_FMT, |
387 | sysfs__mountpoint()); | |
388 | ||
389 | fp = fopen(path, "r"); | |
48e6c5ac JO |
390 | if (!fp) |
391 | return NULL; | |
392 | ||
393 | if (getline(&buf, &len, fp) <= 0) | |
394 | goto out; | |
395 | ||
396 | c = strchr(buf, '\n'); | |
397 | if (c) | |
398 | *c = '\0'; | |
399 | ||
9c3516d1 | 400 | node_map = perf_cpu_map__new(buf); |
48e6c5ac JO |
401 | if (!node_map) |
402 | goto out; | |
403 | ||
44028699 | 404 | nr = (u32) perf_cpu_map__nr(node_map); |
48e6c5ac JO |
405 | |
406 | tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0])*nr); | |
407 | if (!tp) | |
408 | goto out; | |
409 | ||
410 | tp->nr = nr; | |
411 | ||
412 | for (i = 0; i < nr; i++) { | |
44028699 | 413 | if (load_numa_node(&tp->nodes[i], perf_cpu_map__cpu(node_map, i).cpu)) { |
48e6c5ac JO |
414 | numa_topology__delete(tp); |
415 | tp = NULL; | |
416 | break; | |
417 | } | |
418 | } | |
419 | ||
420 | out: | |
421 | free(buf); | |
422 | fclose(fp); | |
38f01d8d | 423 | perf_cpu_map__put(node_map); |
48e6c5ac JO |
424 | return tp; |
425 | } | |
426 | ||
427 | void numa_topology__delete(struct numa_topology *tp) | |
428 | { | |
429 | u32 i; | |
430 | ||
431 | for (i = 0; i < tp->nr; i++) | |
d8f9da24 | 432 | zfree(&tp->nodes[i].cpus); |
48e6c5ac JO |
433 | |
434 | free(tp); | |
435 | } | |
f7d74ce3 JY |
436 | |
437 | static int load_hybrid_node(struct hybrid_topology_node *node, | |
438 | struct perf_pmu *pmu) | |
439 | { | |
f7d74ce3 JY |
440 | char *buf = NULL, *p; |
441 | FILE *fp; | |
442 | size_t len = 0; | |
443 | ||
444 | node->pmu_name = strdup(pmu->name); | |
445 | if (!node->pmu_name) | |
446 | return -1; | |
447 | ||
d50a79cd | 448 | fp = perf_pmu__open_file(pmu, "cpus"); |
f7d74ce3 JY |
449 | if (!fp) |
450 | goto err; | |
451 | ||
452 | if (getline(&buf, &len, fp) <= 0) { | |
453 | fclose(fp); | |
454 | goto err; | |
455 | } | |
456 | ||
457 | p = strchr(buf, '\n'); | |
458 | if (p) | |
459 | *p = '\0'; | |
460 | ||
461 | fclose(fp); | |
462 | node->cpus = buf; | |
463 | return 0; | |
464 | ||
465 | err: | |
466 | zfree(&node->pmu_name); | |
467 | free(buf); | |
468 | return -1; | |
469 | } | |
470 | ||
471 | struct hybrid_topology *hybrid_topology__new(void) | |
472 | { | |
1215795c | 473 | struct perf_pmu *pmu = NULL; |
f7d74ce3 | 474 | struct hybrid_topology *tp = NULL; |
94f9eb95 | 475 | int nr = perf_pmus__num_core_pmus(), i = 0; |
f7d74ce3 | 476 | |
94f9eb95 | 477 | if (nr <= 1) |
f7d74ce3 JY |
478 | return NULL; |
479 | ||
480 | tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0]) * nr); | |
481 | if (!tp) | |
482 | return NULL; | |
483 | ||
484 | tp->nr = nr; | |
9d6a1df9 | 485 | while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { |
f7d74ce3 JY |
486 | if (load_hybrid_node(&tp->nodes[i], pmu)) { |
487 | hybrid_topology__delete(tp); | |
488 | return NULL; | |
489 | } | |
490 | i++; | |
491 | } | |
492 | ||
493 | return tp; | |
494 | } | |
495 | ||
496 | void hybrid_topology__delete(struct hybrid_topology *tp) | |
497 | { | |
498 | u32 i; | |
499 | ||
500 | for (i = 0; i < tp->nr; i++) { | |
501 | zfree(&tp->nodes[i].pmu_name); | |
502 | zfree(&tp->nodes[i].cpus); | |
503 | } | |
504 | ||
505 | free(tp); | |
506 | } |