OSDN Git Service

Merge "defconfig: Disable OverlayFS for SDM660 and MSM8998"
[sagit-ice-cold/kernel_xiaomi_msm8998.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         /*
132          * must handle the case of empty cpumap to cover
133          * TOPOLOGY header for NUMA nodes with no CPU
134          * ( e.g., because of CPU hotplug)
135          */
136         if (!isdigit(*cpu_list) && *cpu_list != '\0')
137                 goto out;
138
139         while (isdigit(*cpu_list)) {
140                 p = NULL;
141                 start_cpu = strtoul(cpu_list, &p, 0);
142                 if (start_cpu >= INT_MAX
143                     || (*p != '\0' && *p != ',' && *p != '-'))
144                         goto invalid;
145
146                 if (*p == '-') {
147                         cpu_list = ++p;
148                         p = NULL;
149                         end_cpu = strtoul(cpu_list, &p, 0);
150
151                         if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
152                                 goto invalid;
153
154                         if (end_cpu < start_cpu)
155                                 goto invalid;
156                 } else {
157                         end_cpu = start_cpu;
158                 }
159
160                 for (; start_cpu <= end_cpu; start_cpu++) {
161                         /* check for duplicates */
162                         for (i = 0; i < nr_cpus; i++)
163                                 if (tmp_cpus[i] == (int)start_cpu)
164                                         goto invalid;
165
166                         if (nr_cpus == max_entries) {
167                                 max_entries += MAX_NR_CPUS;
168                                 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
169                                 if (tmp == NULL)
170                                         goto invalid;
171                                 tmp_cpus = tmp;
172                         }
173                         tmp_cpus[nr_cpus++] = (int)start_cpu;
174                 }
175                 if (*p)
176                         ++p;
177
178                 cpu_list = p;
179         }
180
181         if (nr_cpus > 0)
182                 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
183         else if (*cpu_list != '\0')
184                 cpus = cpu_map__default_new();
185         else
186                 cpus = cpu_map__dummy_new();
187 invalid:
188         free(tmp_cpus);
189 out:
190         return cpus;
191 }
192
193 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
194 {
195         int i;
196         size_t printed = fprintf(fp, "%d cpu%s: ",
197                                  map->nr, map->nr > 1 ? "s" : "");
198         for (i = 0; i < map->nr; ++i)
199                 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
200
201         return printed + fprintf(fp, "\n");
202 }
203
204 struct cpu_map *cpu_map__dummy_new(void)
205 {
206         struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
207
208         if (cpus != NULL) {
209                 cpus->nr = 1;
210                 cpus->map[0] = -1;
211                 atomic_set(&cpus->refcnt, 1);
212         }
213
214         return cpus;
215 }
216
217 struct cpu_map *cpu_map__empty_new(int nr)
218 {
219         struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
220
221         if (cpus != NULL) {
222                 int i;
223
224                 cpus->nr = nr;
225                 for (i = 0; i < nr; i++)
226                         cpus->map[i] = -1;
227
228                 atomic_set(&cpus->refcnt, 1);
229         }
230
231         return cpus;
232 }
233
234 static void cpu_map__delete(struct cpu_map *map)
235 {
236         if (map) {
237                 WARN_ONCE(atomic_read(&map->refcnt) != 0,
238                           "cpu_map refcnt unbalanced\n");
239                 free(map);
240         }
241 }
242
243 struct cpu_map *cpu_map__get(struct cpu_map *map)
244 {
245         if (map)
246                 atomic_inc(&map->refcnt);
247         return map;
248 }
249
250 void cpu_map__put(struct cpu_map *map)
251 {
252         if (map && atomic_dec_and_test(&map->refcnt))
253                 cpu_map__delete(map);
254 }
255
256 static int cpu__get_topology_int(int cpu, const char *name, int *value)
257 {
258         char path[PATH_MAX];
259
260         snprintf(path, PATH_MAX,
261                 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
262
263         return sysfs__read_int(path, value);
264 }
265
266 int cpu_map__get_socket_id(int cpu)
267 {
268         int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
269         return ret ?: value;
270 }
271
272 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
273 {
274         int cpu;
275
276         if (idx > map->nr)
277                 return -1;
278
279         cpu = map->map[idx];
280
281         return cpu_map__get_socket_id(cpu);
282 }
283
284 static int cmp_ids(const void *a, const void *b)
285 {
286         return *(int *)a - *(int *)b;
287 }
288
289 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
290                        int (*f)(struct cpu_map *map, int cpu, void *data),
291                        void *data)
292 {
293         struct cpu_map *c;
294         int nr = cpus->nr;
295         int cpu, s1, s2;
296
297         /* allocate as much as possible */
298         c = calloc(1, sizeof(*c) + nr * sizeof(int));
299         if (!c)
300                 return -1;
301
302         for (cpu = 0; cpu < nr; cpu++) {
303                 s1 = f(cpus, cpu, data);
304                 for (s2 = 0; s2 < c->nr; s2++) {
305                         if (s1 == c->map[s2])
306                                 break;
307                 }
308                 if (s2 == c->nr) {
309                         c->map[c->nr] = s1;
310                         c->nr++;
311                 }
312         }
313         /* ensure we process id in increasing order */
314         qsort(c->map, c->nr, sizeof(int), cmp_ids);
315
316         atomic_set(&c->refcnt, 1);
317         *res = c;
318         return 0;
319 }
320
321 int cpu_map__get_core_id(int cpu)
322 {
323         int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
324         return ret ?: value;
325 }
326
327 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
328 {
329         int cpu, s;
330
331         if (idx > map->nr)
332                 return -1;
333
334         cpu = map->map[idx];
335
336         cpu = cpu_map__get_core_id(cpu);
337
338         s = cpu_map__get_socket(map, idx, data);
339         if (s == -1)
340                 return -1;
341
342         /*
343          * encode socket in upper 16 bits
344          * core_id is relative to socket, and
345          * we need a global id. So we combine
346          * socket+ core id
347          */
348         return (s << 16) | (cpu & 0xffff);
349 }
350
351 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
352 {
353         return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
354 }
355
356 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
357 {
358         return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
359 }
360
361 /* setup simple routines to easily access node numbers given a cpu number */
362 static int get_max_num(char *path, int *max)
363 {
364         size_t num;
365         char *buf;
366         int err = 0;
367
368         if (filename__read_str(path, &buf, &num))
369                 return -1;
370
371         buf[num] = '\0';
372
373         /* start on the right, to find highest node num */
374         while (--num) {
375                 if ((buf[num] == ',') || (buf[num] == '-')) {
376                         num++;
377                         break;
378                 }
379         }
380         if (sscanf(&buf[num], "%d", max) < 1) {
381                 err = -1;
382                 goto out;
383         }
384
385         /* convert from 0-based to 1-based */
386         (*max)++;
387
388 out:
389         free(buf);
390         return err;
391 }
392
393 /* Determine highest possible cpu in the system for sparse allocation */
394 static void set_max_cpu_num(void)
395 {
396         const char *mnt;
397         char path[PATH_MAX];
398         int ret = -1;
399
400         /* set up default */
401         max_cpu_num = 4096;
402
403         mnt = sysfs__mountpoint();
404         if (!mnt)
405                 goto out;
406
407         /* get the highest possible cpu number for a sparse allocation */
408         ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
409         if (ret == PATH_MAX) {
410                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
411                 goto out;
412         }
413
414         ret = get_max_num(path, &max_cpu_num);
415
416 out:
417         if (ret)
418                 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
419 }
420
421 /* Determine highest possible node in the system for sparse allocation */
422 static void set_max_node_num(void)
423 {
424         const char *mnt;
425         char path[PATH_MAX];
426         int ret = -1;
427
428         /* set up default */
429         max_node_num = 8;
430
431         mnt = sysfs__mountpoint();
432         if (!mnt)
433                 goto out;
434
435         /* get the highest possible cpu number for a sparse allocation */
436         ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
437         if (ret == PATH_MAX) {
438                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
439                 goto out;
440         }
441
442         ret = get_max_num(path, &max_node_num);
443
444 out:
445         if (ret)
446                 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
447 }
448
449 int cpu__max_node(void)
450 {
451         if (unlikely(!max_node_num))
452                 set_max_node_num();
453
454         return max_node_num;
455 }
456
457 int cpu__max_cpu(void)
458 {
459         if (unlikely(!max_cpu_num))
460                 set_max_cpu_num();
461
462         return max_cpu_num;
463 }
464
465 int cpu__get_node(int cpu)
466 {
467         if (unlikely(cpunode_map == NULL)) {
468                 pr_debug("cpu_map not initialized\n");
469                 return -1;
470         }
471
472         return cpunode_map[cpu];
473 }
474
475 static int init_cpunode_map(void)
476 {
477         int i;
478
479         set_max_cpu_num();
480         set_max_node_num();
481
482         cpunode_map = calloc(max_cpu_num, sizeof(int));
483         if (!cpunode_map) {
484                 pr_err("%s: calloc failed\n", __func__);
485                 return -1;
486         }
487
488         for (i = 0; i < max_cpu_num; i++)
489                 cpunode_map[i] = -1;
490
491         return 0;
492 }
493
494 int cpu__setup_cpunode_map(void)
495 {
496         struct dirent *dent1, *dent2;
497         DIR *dir1, *dir2;
498         unsigned int cpu, mem;
499         char buf[PATH_MAX];
500         char path[PATH_MAX];
501         const char *mnt;
502         int n;
503
504         /* initialize globals */
505         if (init_cpunode_map())
506                 return -1;
507
508         mnt = sysfs__mountpoint();
509         if (!mnt)
510                 return 0;
511
512         n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
513         if (n == PATH_MAX) {
514                 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
515                 return -1;
516         }
517
518         dir1 = opendir(path);
519         if (!dir1)
520                 return 0;
521
522         /* walk tree and setup map */
523         while ((dent1 = readdir(dir1)) != NULL) {
524                 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
525                         continue;
526
527                 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
528                 if (n == PATH_MAX) {
529                         pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
530                         continue;
531                 }
532
533                 dir2 = opendir(buf);
534                 if (!dir2)
535                         continue;
536                 while ((dent2 = readdir(dir2)) != NULL) {
537                         if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
538                                 continue;
539                         cpunode_map[cpu] = mem;
540                 }
541                 closedir(dir2);
542         }
543         closedir(dir1);
544         return 0;
545 }