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