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