Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.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 "asm/bug.h"
9
10 static int max_cpu_num;
11 static int max_node_num;
12 static int *cpunode_map;
13
14 static struct cpu_map *cpu_map__default_new(void)
15 {
16         struct cpu_map *cpus;
17         int nr_cpus;
18
19         nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
20         if (nr_cpus < 0)
21                 return NULL;
22
23         cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
24         if (cpus != NULL) {
25                 int i;
26                 for (i = 0; i < nr_cpus; ++i)
27                         cpus->map[i] = i;
28
29                 cpus->nr = nr_cpus;
30                 atomic_set(&cpus->refcnt, 1);
31         }
32
33         return cpus;
34 }
35
36 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
37 {
38         size_t payload_size = nr_cpus * sizeof(int);
39         struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
40
41         if (cpus != NULL) {
42                 cpus->nr = nr_cpus;
43                 memcpy(cpus->map, tmp_cpus, payload_size);
44                 atomic_set(&cpus->refcnt, 1);
45         }
46
47         return cpus;
48 }
49
50 struct cpu_map *cpu_map__read(FILE *file)
51 {
52         struct cpu_map *cpus = NULL;
53         int nr_cpus = 0;
54         int *tmp_cpus = NULL, *tmp;
55         int max_entries = 0;
56         int n, cpu, prev;
57         char sep;
58
59         sep = 0;
60         prev = -1;
61         for (;;) {
62                 n = fscanf(file, "%u%c", &cpu, &sep);
63                 if (n <= 0)
64                         break;
65                 if (prev >= 0) {
66                         int new_max = nr_cpus + cpu - prev - 1;
67
68                         if (new_max >= max_entries) {
69                                 max_entries = new_max + MAX_NR_CPUS / 2;
70                                 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
71                                 if (tmp == NULL)
72                                         goto out_free_tmp;
73                                 tmp_cpus = tmp;
74                         }
75
76                         while (++prev < cpu)
77                                 tmp_cpus[nr_cpus++] = prev;
78                 }
79                 if (nr_cpus == max_entries) {
80                         max_entries += MAX_NR_CPUS;
81                         tmp = realloc(tmp_cpus, max_entries * sizeof(int));
82                         if (tmp == NULL)
83                                 goto out_free_tmp;
84                         tmp_cpus = tmp;
85                 }
86
87                 tmp_cpus[nr_cpus++] = cpu;
88                 if (n == 2 && sep == '-')
89                         prev = cpu;
90                 else
91                         prev = -1;
92                 if (n == 1 || sep == '\n')
93                         break;
94         }
95
96         if (nr_cpus > 0)
97                 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
98         else
99                 cpus = cpu_map__default_new();
100 out_free_tmp:
101         free(tmp_cpus);
102         return cpus;
103 }
104
105 static struct cpu_map *cpu_map__read_all_cpu_map(void)
106 {
107         struct cpu_map *cpus = NULL;
108         FILE *onlnf;
109
110         onlnf = fopen("/sys/devices/system/cpu/online", "r");
111         if (!onlnf)
112                 return cpu_map__default_new();
113
114         cpus = cpu_map__read(onlnf);
115         fclose(onlnf);
116         return cpus;
117 }
118
119 struct cpu_map *cpu_map__new(const char *cpu_list)
120 {
121         struct cpu_map *cpus = NULL;
122         unsigned long start_cpu, end_cpu = 0;
123         char *p = NULL;
124         int i, nr_cpus = 0;
125         int *tmp_cpus = NULL, *tmp;
126         int max_entries = 0;
127
128         if (!cpu_list)
129                 return cpu_map__read_all_cpu_map();
130
131         if (!isdigit(*cpu_list))
132                 goto out;
133
134         while (isdigit(*cpu_list)) {
135                 p = NULL;
136                 start_cpu = strtoul(cpu_list, &p, 0);
137                 if (start_cpu >= INT_MAX
138                     || (*p != '\0' && *p != ',' && *p != '-'))
139                         goto invalid;
140
141                 if (*p == '-') {
142                         cpu_list = ++p;
143                         p = NULL;
144                         end_cpu = strtoul(cpu_list, &p, 0);
145
146                         if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
147                                 goto invalid;
148
149                         if (end_cpu < start_cpu)
150                                 goto invalid;
151                 } else {
152                         end_cpu = start_cpu;
153                 }
154
155                 for (; start_cpu <= end_cpu; start_cpu++) {
156                         /* check for duplicates */
157                         for (i = 0; i < nr_cpus; i++)
158                                 if (tmp_cpus[i] == (int)start_cpu)
159                                         goto invalid;
160
161                         if (nr_cpus == max_entries) {
162                                 max_entries += MAX_NR_CPUS;
163                                 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
164                                 if (tmp == NULL)
165                                         goto invalid;
166                                 tmp_cpus = tmp;
167                         }
168                         tmp_cpus[nr_cpus++] = (int)start_cpu;
169                 }
170                 if (*p)
171                         ++p;
172
173                 cpu_list = p;
174         }
175
176         if (nr_cpus > 0)
177                 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
178         else
179                 cpus = cpu_map__default_new();
180 invalid:
181         free(tmp_cpus);
182 out:
183         return cpus;
184 }
185
186 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
187 {
188         int i;
189         size_t printed = fprintf(fp, "%d cpu%s: ",
190                                  map->nr, map->nr > 1 ? "s" : "");
191         for (i = 0; i < map->nr; ++i)
192                 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
193
194         return printed + fprintf(fp, "\n");
195 }
196
197 struct cpu_map *cpu_map__dummy_new(void)
198 {
199         struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
200
201         if (cpus != NULL) {
202                 cpus->nr = 1;
203                 cpus->map[0] = -1;
204                 atomic_set(&cpus->refcnt, 1);
205         }
206
207         return cpus;
208 }
209
210 struct cpu_map *cpu_map__empty_new(int nr)
211 {
212         struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
213
214         if (cpus != NULL) {
215                 int i;
216
217                 cpus->nr = nr;
218                 for (i = 0; i < nr; i++)
219                         cpus->map[i] = -1;
220
221                 atomic_set(&cpus->refcnt, 1);
222         }
223
224         return cpus;
225 }
226
227 static void cpu_map__delete(struct cpu_map *map)
228 {
229         if (map) {
230                 WARN_ONCE(atomic_read(&map->refcnt) != 0,
231                           "cpu_map refcnt unbalanced\n");
232                 free(map);
233         }
234 }
235
236 struct cpu_map *cpu_map__get(struct cpu_map *map)
237 {
238         if (map)
239                 atomic_inc(&map->refcnt);
240         return map;
241 }
242
243 void cpu_map__put(struct cpu_map *map)
244 {
245         if (map && atomic_dec_and_test(&map->refcnt))
246                 cpu_map__delete(map);
247 }
248
249 static int cpu__get_topology_int(int cpu, const char *name, int *value)
250 {
251         char path[PATH_MAX];
252
253         snprintf(path, PATH_MAX,
254                 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
255
256         return sysfs__read_int(path, value);
257 }
258
259 int cpu_map__get_socket_id(int cpu)
260 {
261         int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
262         return ret ?: value;
263 }
264
265 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
266 {
267         int cpu;
268
269         if (idx > map->nr)
270                 return -1;
271
272         cpu = map->map[idx];
273
274         return cpu_map__get_socket_id(cpu);
275 }
276
277 static int cmp_ids(const void *a, const void *b)
278 {
279         return *(int *)a - *(int *)b;
280 }
281
282 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
283                        int (*f)(struct cpu_map *map, int cpu, void *data),
284                        void *data)
285 {
286         struct cpu_map *c;
287         int nr = cpus->nr;
288         int cpu, s1, s2;
289
290         /* allocate as much as possible */
291         c = calloc(1, sizeof(*c) + nr * sizeof(int));
292         if (!c)
293                 return -1;
294
295         for (cpu = 0; cpu < nr; cpu++) {
296                 s1 = f(cpus, cpu, data);
297                 for (s2 = 0; s2 < c->nr; s2++) {
298                         if (s1 == c->map[s2])
299                                 break;
300                 }
301                 if (s2 == c->nr) {
302                         c->map[c->nr] = s1;
303                         c->nr++;
304                 }
305         }
306         /* ensure we process id in increasing order */
307         qsort(c->map, c->nr, sizeof(int), cmp_ids);
308
309         atomic_set(&c->refcnt, 1);
310         *res = c;
311         return 0;
312 }
313
314 int cpu_map__get_core_id(int cpu)
315 {
316         int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
317         return ret ?: value;
318 }
319
320 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
321 {
322         int cpu, s;
323
324         if (idx > map->nr)
325                 return -1;
326
327         cpu = map->map[idx];
328
329         cpu = cpu_map__get_core_id(cpu);
330
331         s = cpu_map__get_socket(map, idx, data);
332         if (s == -1)
333                 return -1;
334
335         /*
336          * encode socket in upper 16 bits
337          * core_id is relative to socket, and
338          * we need a global id. So we combine
339          * socket+ core id
340          */
341         return (s << 16) | (cpu & 0xffff);
342 }
343
344 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
345 {
346         return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
347 }
348
349 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
350 {
351         return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
352 }
353
354 /* setup simple routines to easily access node numbers given a cpu number */
355 static int get_max_num(char *path, int *max)
356 {
357         size_t num;
358         char *buf;
359         int err = 0;
360
361         if (filename__read_str(path, &buf, &num))
362                 return -1;
363
364         buf[num] = '\0';
365
366         /* start on the right, to find highest node num */
367         while (--num) {
368                 if ((buf[num] == ',') || (buf[num] == '-')) {
369                         num++;
370                         break;
371                 }
372         }
373         if (sscanf(&buf[num], "%d", max) < 1) {
374                 err = -1;
375                 goto out;
376         }
377
378         /* convert from 0-based to 1-based */
379         (*max)++;
380
381 out:
382         free(buf);
383         return err;
384 }
385
386 /* Determine highest possible cpu in the system for sparse allocation */
387 static void set_max_cpu_num(void)
388 {
389         const char *mnt;
390         char path[PATH_MAX];
391         int ret = -1;
392
393         /* set up default */
394         max_cpu_num = 4096;
395
396         mnt = sysfs__mountpoint();
397         if (!mnt)
398                 goto out;
399
400         /* get the highest possible cpu number for a sparse allocation */
401         ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
402         if (ret == PATH_MAX) {
403                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
404                 goto out;
405         }
406
407         ret = get_max_num(path, &max_cpu_num);
408
409 out:
410         if (ret)
411                 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
412 }
413
414 /* Determine highest possible node in the system for sparse allocation */
415 static void set_max_node_num(void)
416 {
417         const char *mnt;
418         char path[PATH_MAX];
419         int ret = -1;
420
421         /* set up default */
422         max_node_num = 8;
423
424         mnt = sysfs__mountpoint();
425         if (!mnt)
426                 goto out;
427
428         /* get the highest possible cpu number for a sparse allocation */
429         ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
430         if (ret == PATH_MAX) {
431                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
432                 goto out;
433         }
434
435         ret = get_max_num(path, &max_node_num);
436
437 out:
438         if (ret)
439                 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
440 }
441
442 int cpu__max_node(void)
443 {
444         if (unlikely(!max_node_num))
445                 set_max_node_num();
446
447         return max_node_num;
448 }
449
450 int cpu__max_cpu(void)
451 {
452         if (unlikely(!max_cpu_num))
453                 set_max_cpu_num();
454
455         return max_cpu_num;
456 }
457
458 int cpu__get_node(int cpu)
459 {
460         if (unlikely(cpunode_map == NULL)) {
461                 pr_debug("cpu_map not initialized\n");
462                 return -1;
463         }
464
465         return cpunode_map[cpu];
466 }
467
468 static int init_cpunode_map(void)
469 {
470         int i;
471
472         set_max_cpu_num();
473         set_max_node_num();
474
475         cpunode_map = calloc(max_cpu_num, sizeof(int));
476         if (!cpunode_map) {
477                 pr_err("%s: calloc failed\n", __func__);
478                 return -1;
479         }
480
481         for (i = 0; i < max_cpu_num; i++)
482                 cpunode_map[i] = -1;
483
484         return 0;
485 }
486
487 int cpu__setup_cpunode_map(void)
488 {
489         struct dirent *dent1, *dent2;
490         DIR *dir1, *dir2;
491         unsigned int cpu, mem;
492         char buf[PATH_MAX];
493         char path[PATH_MAX];
494         const char *mnt;
495         int n;
496
497         /* initialize globals */
498         if (init_cpunode_map())
499                 return -1;
500
501         mnt = sysfs__mountpoint();
502         if (!mnt)
503                 return 0;
504
505         n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
506         if (n == PATH_MAX) {
507                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
508                 return -1;
509         }
510
511         dir1 = opendir(path);
512         if (!dir1)
513                 return 0;
514
515         /* walk tree and setup map */
516         while ((dent1 = readdir(dir1)) != NULL) {
517                 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
518                         continue;
519
520                 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
521                 if (n == PATH_MAX) {
522                         pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
523                         continue;
524                 }
525
526                 dir2 = opendir(buf);
527                 if (!dir2)
528                         continue;
529                 while ((dent2 = readdir(dir2)) != NULL) {
530                         if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
531                                 continue;
532                         cpunode_map[cpu] = mem;
533                 }
534                 closedir(dir2);
535         }
536         closedir(dir1);
537         return 0;
538 }