e05a12bde0731abba4448b02764472863be2989f
[linux-2.6-microblaze.git] / tools / perf / util / cpumap.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <api/fs/fs.h>
3 #include "cpumap.h"
4 #include "debug.h"
5 #include "event.h"
6 #include <assert.h>
7 #include <dirent.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <linux/bitmap.h>
11 #include "asm/bug.h"
12
13 #include <linux/ctype.h>
14 #include <linux/zalloc.h>
15
16 static int max_cpu_num;
17 static int max_present_cpu_num;
18 static int max_node_num;
19 static int *cpunode_map;
20
21 static struct perf_cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
22 {
23         struct perf_cpu_map *map;
24
25         map = perf_cpu_map__empty_new(cpus->nr);
26         if (map) {
27                 unsigned i;
28
29                 for (i = 0; i < cpus->nr; i++) {
30                         /*
31                          * Special treatment for -1, which is not real cpu number,
32                          * and we need to use (int) -1 to initialize map[i],
33                          * otherwise it would become 65535.
34                          */
35                         if (cpus->cpu[i] == (u16) -1)
36                                 map->map[i] = -1;
37                         else
38                                 map->map[i] = (int) cpus->cpu[i];
39                 }
40         }
41
42         return map;
43 }
44
45 static struct perf_cpu_map *cpu_map__from_mask(struct perf_record_record_cpu_map *mask)
46 {
47         struct perf_cpu_map *map;
48         int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
49
50         nr = bitmap_weight(mask->mask, nbits);
51
52         map = perf_cpu_map__empty_new(nr);
53         if (map) {
54                 int cpu, i = 0;
55
56                 for_each_set_bit(cpu, mask->mask, nbits)
57                         map->map[i++] = cpu;
58         }
59         return map;
60
61 }
62
63 struct perf_cpu_map *cpu_map__new_data(struct perf_record_cpu_map_data *data)
64 {
65         if (data->type == PERF_CPU_MAP__CPUS)
66                 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
67         else
68                 return cpu_map__from_mask((struct perf_record_record_cpu_map *)data->data);
69 }
70
71 size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp)
72 {
73 #define BUFSIZE 1024
74         char buf[BUFSIZE];
75
76         cpu_map__snprint(map, buf, sizeof(buf));
77         return fprintf(fp, "%s\n", buf);
78 #undef BUFSIZE
79 }
80
81 struct perf_cpu_map *perf_cpu_map__empty_new(int nr)
82 {
83         struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
84
85         if (cpus != NULL) {
86                 int i;
87
88                 cpus->nr = nr;
89                 for (i = 0; i < nr; i++)
90                         cpus->map[i] = -1;
91
92                 refcount_set(&cpus->refcnt, 1);
93         }
94
95         return cpus;
96 }
97
98 static int cpu__get_topology_int(int cpu, const char *name, int *value)
99 {
100         char path[PATH_MAX];
101
102         snprintf(path, PATH_MAX,
103                 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
104
105         return sysfs__read_int(path, value);
106 }
107
108 int cpu_map__get_socket_id(int cpu)
109 {
110         int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
111         return ret ?: value;
112 }
113
114 struct aggr_cpu_id cpu_map__get_socket(struct perf_cpu_map *map, int idx,
115                                         void *data __maybe_unused)
116 {
117         int cpu;
118         struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
119
120         if (idx > map->nr)
121                 return id;
122
123         cpu = map->map[idx];
124
125         id.id = cpu_map__get_socket_id(cpu);
126         return id;
127 }
128
129 static int cmp_aggr_cpu_id(const void *a_pointer, const void *b_pointer)
130 {
131         struct aggr_cpu_id *a = (struct aggr_cpu_id *)a_pointer;
132         struct aggr_cpu_id *b = (struct aggr_cpu_id *)b_pointer;
133
134         return a->id - b->id;
135 }
136
137 int cpu_map__build_map(struct perf_cpu_map *cpus, struct perf_cpu_map **res,
138                        struct aggr_cpu_id (*f)(struct perf_cpu_map *map, int cpu, void *data),
139                        void *data)
140 {
141         int nr = cpus->nr;
142         struct perf_cpu_map *c = perf_cpu_map__empty_new(nr);
143         int cpu, s2;
144         struct aggr_cpu_id s1;
145
146         if (!c)
147                 return -1;
148
149         /* Reset size as it may only be partially filled */
150         c->nr = 0;
151
152         for (cpu = 0; cpu < nr; cpu++) {
153                 s1 = f(cpus, cpu, data);
154                 for (s2 = 0; s2 < c->nr; s2++) {
155                         if (s1.id == c->map[s2])
156                                 break;
157                 }
158                 if (s2 == c->nr) {
159                         c->map[c->nr] = s1.id;
160                         c->nr++;
161                 }
162         }
163         /* ensure we process id in increasing order */
164         qsort(c->map, c->nr, sizeof(struct aggr_cpu_id), cmp_aggr_cpu_id);
165
166         *res = c;
167         return 0;
168 }
169
170 int cpu_map__get_die_id(int cpu)
171 {
172         int value, ret = cpu__get_topology_int(cpu, "die_id", &value);
173
174         return ret ?: value;
175 }
176
177 struct aggr_cpu_id cpu_map__get_die(struct perf_cpu_map *map, int idx, void *data)
178 {
179         int cpu, s;
180         struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
181
182         if (idx > map->nr)
183                 return id;
184
185         cpu = map->map[idx];
186
187         id.id = cpu_map__get_die_id(cpu);
188         /* There is no die_id on legacy system. */
189         if (id.id == -1)
190                 id.id = 0;
191
192         s = cpu_map__get_socket(map, idx, data).id;
193         if (s == -1)
194                 return cpu_map__empty_aggr_cpu_id();
195
196         /*
197          * Encode socket in bit range 15:8
198          * die_id is relative to socket, and
199          * we need a global id. So we combine
200          * socket + die id
201          */
202         if (WARN_ONCE(id.id >> 8, "The die id number is too big.\n"))
203                 return cpu_map__empty_aggr_cpu_id();
204
205         if (WARN_ONCE(s >> 8, "The socket id number is too big.\n"))
206                 return cpu_map__empty_aggr_cpu_id();
207
208         id.id = (s << 8) | (id.id & 0xff);
209         return id;
210 }
211
212 int cpu_map__get_core_id(int cpu)
213 {
214         int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
215         return ret ?: value;
216 }
217
218 int cpu_map__get_node_id(int cpu)
219 {
220         return cpu__get_node(cpu);
221 }
222
223 struct aggr_cpu_id cpu_map__get_core(struct perf_cpu_map *map, int idx, void *data)
224 {
225         int cpu;
226         struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
227
228         if (idx > map->nr)
229                 return id;
230
231         cpu = map->map[idx];
232
233         cpu = cpu_map__get_core_id(cpu);
234
235         /* cpu_map__get_die returns the combination of socket + die id */
236         id = cpu_map__get_die(map, idx, data);
237         if (cpu_map__aggr_cpu_id_is_empty(id))
238                 return id;
239
240         /*
241          * encode socket in bit range 31:24
242          * encode die id in bit range 23:16
243          * core_id is relative to socket and die,
244          * we need a global id. So we combine
245          * socket + die id + core id
246          */
247         if (WARN_ONCE(cpu >> 16, "The core id number is too big.\n"))
248                 return cpu_map__empty_aggr_cpu_id();
249
250         id.id = (id.id << 16) | (cpu & 0xffff);
251         return id;
252 }
253
254 struct aggr_cpu_id cpu_map__get_node(struct perf_cpu_map *map, int idx, void *data __maybe_unused)
255 {
256         struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
257
258         if (idx < 0 || idx >= map->nr)
259                 return id;
260
261         id.id = cpu_map__get_node_id(map->map[idx]);
262         return id;
263 }
264
265 int cpu_map__build_socket_map(struct perf_cpu_map *cpus, struct perf_cpu_map **sockp)
266 {
267         return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
268 }
269
270 int cpu_map__build_die_map(struct perf_cpu_map *cpus, struct perf_cpu_map **diep)
271 {
272         return cpu_map__build_map(cpus, diep, cpu_map__get_die, NULL);
273 }
274
275 int cpu_map__build_core_map(struct perf_cpu_map *cpus, struct perf_cpu_map **corep)
276 {
277         return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
278 }
279
280 int cpu_map__build_node_map(struct perf_cpu_map *cpus, struct perf_cpu_map **numap)
281 {
282         return cpu_map__build_map(cpus, numap, cpu_map__get_node, NULL);
283 }
284
285 /* setup simple routines to easily access node numbers given a cpu number */
286 static int get_max_num(char *path, int *max)
287 {
288         size_t num;
289         char *buf;
290         int err = 0;
291
292         if (filename__read_str(path, &buf, &num))
293                 return -1;
294
295         buf[num] = '\0';
296
297         /* start on the right, to find highest node num */
298         while (--num) {
299                 if ((buf[num] == ',') || (buf[num] == '-')) {
300                         num++;
301                         break;
302                 }
303         }
304         if (sscanf(&buf[num], "%d", max) < 1) {
305                 err = -1;
306                 goto out;
307         }
308
309         /* convert from 0-based to 1-based */
310         (*max)++;
311
312 out:
313         free(buf);
314         return err;
315 }
316
317 /* Determine highest possible cpu in the system for sparse allocation */
318 static void set_max_cpu_num(void)
319 {
320         const char *mnt;
321         char path[PATH_MAX];
322         int ret = -1;
323
324         /* set up default */
325         max_cpu_num = 4096;
326         max_present_cpu_num = 4096;
327
328         mnt = sysfs__mountpoint();
329         if (!mnt)
330                 goto out;
331
332         /* get the highest possible cpu number for a sparse allocation */
333         ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
334         if (ret >= PATH_MAX) {
335                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
336                 goto out;
337         }
338
339         ret = get_max_num(path, &max_cpu_num);
340         if (ret)
341                 goto out;
342
343         /* get the highest present cpu number for a sparse allocation */
344         ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
345         if (ret >= PATH_MAX) {
346                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
347                 goto out;
348         }
349
350         ret = get_max_num(path, &max_present_cpu_num);
351
352 out:
353         if (ret)
354                 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
355 }
356
357 /* Determine highest possible node in the system for sparse allocation */
358 static void set_max_node_num(void)
359 {
360         const char *mnt;
361         char path[PATH_MAX];
362         int ret = -1;
363
364         /* set up default */
365         max_node_num = 8;
366
367         mnt = sysfs__mountpoint();
368         if (!mnt)
369                 goto out;
370
371         /* get the highest possible cpu number for a sparse allocation */
372         ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
373         if (ret >= PATH_MAX) {
374                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
375                 goto out;
376         }
377
378         ret = get_max_num(path, &max_node_num);
379
380 out:
381         if (ret)
382                 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
383 }
384
385 int cpu__max_node(void)
386 {
387         if (unlikely(!max_node_num))
388                 set_max_node_num();
389
390         return max_node_num;
391 }
392
393 int cpu__max_cpu(void)
394 {
395         if (unlikely(!max_cpu_num))
396                 set_max_cpu_num();
397
398         return max_cpu_num;
399 }
400
401 int cpu__max_present_cpu(void)
402 {
403         if (unlikely(!max_present_cpu_num))
404                 set_max_cpu_num();
405
406         return max_present_cpu_num;
407 }
408
409
410 int cpu__get_node(int cpu)
411 {
412         if (unlikely(cpunode_map == NULL)) {
413                 pr_debug("cpu_map not initialized\n");
414                 return -1;
415         }
416
417         return cpunode_map[cpu];
418 }
419
420 static int init_cpunode_map(void)
421 {
422         int i;
423
424         set_max_cpu_num();
425         set_max_node_num();
426
427         cpunode_map = calloc(max_cpu_num, sizeof(int));
428         if (!cpunode_map) {
429                 pr_err("%s: calloc failed\n", __func__);
430                 return -1;
431         }
432
433         for (i = 0; i < max_cpu_num; i++)
434                 cpunode_map[i] = -1;
435
436         return 0;
437 }
438
439 int cpu__setup_cpunode_map(void)
440 {
441         struct dirent *dent1, *dent2;
442         DIR *dir1, *dir2;
443         unsigned int cpu, mem;
444         char buf[PATH_MAX];
445         char path[PATH_MAX];
446         const char *mnt;
447         int n;
448
449         /* initialize globals */
450         if (init_cpunode_map())
451                 return -1;
452
453         mnt = sysfs__mountpoint();
454         if (!mnt)
455                 return 0;
456
457         n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
458         if (n >= PATH_MAX) {
459                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
460                 return -1;
461         }
462
463         dir1 = opendir(path);
464         if (!dir1)
465                 return 0;
466
467         /* walk tree and setup map */
468         while ((dent1 = readdir(dir1)) != NULL) {
469                 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
470                         continue;
471
472                 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
473                 if (n >= PATH_MAX) {
474                         pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
475                         continue;
476                 }
477
478                 dir2 = opendir(buf);
479                 if (!dir2)
480                         continue;
481                 while ((dent2 = readdir(dir2)) != NULL) {
482                         if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
483                                 continue;
484                         cpunode_map[cpu] = mem;
485                 }
486                 closedir(dir2);
487         }
488         closedir(dir1);
489         return 0;
490 }
491
492 bool cpu_map__has(struct perf_cpu_map *cpus, int cpu)
493 {
494         return perf_cpu_map__idx(cpus, cpu) != -1;
495 }
496
497 int cpu_map__cpu(struct perf_cpu_map *cpus, int idx)
498 {
499         return cpus->map[idx];
500 }
501
502 size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
503 {
504         int i, cpu, start = -1;
505         bool first = true;
506         size_t ret = 0;
507
508 #define COMMA first ? "" : ","
509
510         for (i = 0; i < map->nr + 1; i++) {
511                 bool last = i == map->nr;
512
513                 cpu = last ? INT_MAX : map->map[i];
514
515                 if (start == -1) {
516                         start = i;
517                         if (last) {
518                                 ret += snprintf(buf + ret, size - ret,
519                                                 "%s%d", COMMA,
520                                                 map->map[i]);
521                         }
522                 } else if (((i - start) != (cpu - map->map[start])) || last) {
523                         int end = i - 1;
524
525                         if (start == end) {
526                                 ret += snprintf(buf + ret, size - ret,
527                                                 "%s%d", COMMA,
528                                                 map->map[start]);
529                         } else {
530                                 ret += snprintf(buf + ret, size - ret,
531                                                 "%s%d-%d", COMMA,
532                                                 map->map[start], map->map[end]);
533                         }
534                         first = false;
535                         start = i;
536                 }
537         }
538
539 #undef COMMA
540
541         pr_debug2("cpumask list: %s\n", buf);
542         return ret;
543 }
544
545 static char hex_char(unsigned char val)
546 {
547         if (val < 10)
548                 return val + '0';
549         if (val < 16)
550                 return val - 10 + 'a';
551         return '?';
552 }
553
554 size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
555 {
556         int i, cpu;
557         char *ptr = buf;
558         unsigned char *bitmap;
559         int last_cpu = cpu_map__cpu(map, map->nr - 1);
560
561         if (buf == NULL)
562                 return 0;
563
564         bitmap = zalloc(last_cpu / 8 + 1);
565         if (bitmap == NULL) {
566                 buf[0] = '\0';
567                 return 0;
568         }
569
570         for (i = 0; i < map->nr; i++) {
571                 cpu = cpu_map__cpu(map, i);
572                 bitmap[cpu / 8] |= 1 << (cpu % 8);
573         }
574
575         for (cpu = last_cpu / 4 * 4; cpu >= 0; cpu -= 4) {
576                 unsigned char bits = bitmap[cpu / 8];
577
578                 if (cpu % 8)
579                         bits >>= 4;
580                 else
581                         bits &= 0xf;
582
583                 *ptr++ = hex_char(bits);
584                 if ((cpu % 32) == 0 && cpu > 0)
585                         *ptr++ = ',';
586         }
587         *ptr = '\0';
588         free(bitmap);
589
590         buf[size - 1] = '\0';
591         return ptr - buf;
592 }
593
594 const struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
595 {
596         static const struct perf_cpu_map *online = NULL;
597
598         if (!online)
599                 online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
600
601         return online;
602 }
603
604 bool cpu_map__compare_aggr_cpu_id(struct aggr_cpu_id a, struct aggr_cpu_id b)
605 {
606         return a.id == b.id;
607 }
608
609 bool cpu_map__aggr_cpu_id_is_empty(struct aggr_cpu_id a)
610 {
611         return a.id == -1;
612 }
613
614 struct aggr_cpu_id cpu_map__empty_aggr_cpu_id(void)
615 {
616         struct aggr_cpu_id ret = {
617                 .id = -1
618         };
619         return ret;
620 }