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