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