perf tools: Fix find tids routine by excluding "." and ".."
[cris-mirror.git] / tools / perf / util / cpumap.c
blob4e01490e51e549c1be5b3e5d640683f65a765f7f
1 #include "util.h"
2 #include "../perf.h"
3 #include "cpumap.h"
4 #include <assert.h>
5 #include <stdio.h>
7 int cpumap[MAX_NR_CPUS];
9 static int default_cpu_map(void)
11 int nr_cpus, i;
13 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
14 assert(nr_cpus <= MAX_NR_CPUS);
15 assert((int)nr_cpus >= 0);
17 for (i = 0; i < nr_cpus; ++i)
18 cpumap[i] = i;
20 return nr_cpus;
23 int read_cpu_map(void)
25 FILE *onlnf;
26 int nr_cpus = 0;
27 int n, cpu, prev;
28 char sep;
30 onlnf = fopen("/sys/devices/system/cpu/online", "r");
31 if (!onlnf)
32 return default_cpu_map();
34 sep = 0;
35 prev = -1;
36 for (;;) {
37 n = fscanf(onlnf, "%u%c", &cpu, &sep);
38 if (n <= 0)
39 break;
40 if (prev >= 0) {
41 assert(nr_cpus + cpu - prev - 1 < MAX_NR_CPUS);
42 while (++prev < cpu)
43 cpumap[nr_cpus++] = prev;
45 assert (nr_cpus < MAX_NR_CPUS);
46 cpumap[nr_cpus++] = cpu;
47 if (n == 2 && sep == '-')
48 prev = cpu;
49 else
50 prev = -1;
51 if (n == 1 || sep == '\n')
52 break;
54 fclose(onlnf);
55 if (nr_cpus > 0)
56 return nr_cpus;
58 return default_cpu_map();