Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 1 | #include "util.h" |
| 2 | #include "../perf.h" |
| 3 | #include "cpumap.h" |
| 4 | #include <assert.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | int cpumap[MAX_NR_CPUS]; |
| 8 | |
| 9 | static int default_cpu_map(void) |
| 10 | { |
| 11 | int nr_cpus, i; |
| 12 | |
| 13 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 14 | assert(nr_cpus <= MAX_NR_CPUS); |
| 15 | assert((int)nr_cpus >= 0); |
| 16 | |
| 17 | for (i = 0; i < nr_cpus; ++i) |
| 18 | cpumap[i] = i; |
| 19 | |
| 20 | return nr_cpus; |
| 21 | } |
| 22 | |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame^] | 23 | static int read_all_cpu_map(void) |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 24 | { |
| 25 | FILE *onlnf; |
| 26 | int nr_cpus = 0; |
| 27 | int n, cpu, prev; |
| 28 | char sep; |
| 29 | |
| 30 | onlnf = fopen("/sys/devices/system/cpu/online", "r"); |
| 31 | if (!onlnf) |
| 32 | return default_cpu_map(); |
| 33 | |
| 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; |
| 44 | } |
| 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; |
| 53 | } |
| 54 | fclose(onlnf); |
| 55 | if (nr_cpus > 0) |
| 56 | return nr_cpus; |
| 57 | |
| 58 | return default_cpu_map(); |
| 59 | } |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame^] | 60 | |
| 61 | int read_cpu_map(const char *cpu_list) |
| 62 | { |
| 63 | unsigned long start_cpu, end_cpu = 0; |
| 64 | char *p = NULL; |
| 65 | int i, nr_cpus = 0; |
| 66 | |
| 67 | if (!cpu_list) |
| 68 | return read_all_cpu_map(); |
| 69 | |
| 70 | if (!isdigit(*cpu_list)) |
| 71 | goto invalid; |
| 72 | |
| 73 | while (isdigit(*cpu_list)) { |
| 74 | p = NULL; |
| 75 | start_cpu = strtoul(cpu_list, &p, 0); |
| 76 | if (start_cpu >= INT_MAX |
| 77 | || (*p != '\0' && *p != ',' && *p != '-')) |
| 78 | goto invalid; |
| 79 | |
| 80 | if (*p == '-') { |
| 81 | cpu_list = ++p; |
| 82 | p = NULL; |
| 83 | end_cpu = strtoul(cpu_list, &p, 0); |
| 84 | |
| 85 | if (end_cpu >= INT_MAX || (*p != '\0' && *p != ',')) |
| 86 | goto invalid; |
| 87 | |
| 88 | if (end_cpu < start_cpu) |
| 89 | goto invalid; |
| 90 | } else { |
| 91 | end_cpu = start_cpu; |
| 92 | } |
| 93 | |
| 94 | for (; start_cpu <= end_cpu; start_cpu++) { |
| 95 | /* check for duplicates */ |
| 96 | for (i = 0; i < nr_cpus; i++) |
| 97 | if (cpumap[i] == (int)start_cpu) |
| 98 | goto invalid; |
| 99 | |
| 100 | assert(nr_cpus < MAX_NR_CPUS); |
| 101 | cpumap[nr_cpus++] = (int)start_cpu; |
| 102 | } |
| 103 | if (*p) |
| 104 | ++p; |
| 105 | |
| 106 | cpu_list = p; |
| 107 | } |
| 108 | if (nr_cpus > 0) |
| 109 | return nr_cpus; |
| 110 | |
| 111 | return default_cpu_map(); |
| 112 | invalid: |
| 113 | return -1; |
| 114 | } |