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 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 7 | static struct cpu_map *cpu_map__default_new(void) |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 8 | { |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 9 | struct cpu_map *cpus; |
| 10 | int nr_cpus; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 11 | |
| 12 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 13 | if (nr_cpus < 0) |
| 14 | return NULL; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 16 | cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int)); |
| 17 | if (cpus != NULL) { |
| 18 | int i; |
| 19 | for (i = 0; i < nr_cpus; ++i) |
| 20 | cpus->map[i] = i; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 21 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 22 | cpus->nr = nr_cpus; |
| 23 | } |
| 24 | |
| 25 | return cpus; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 26 | } |
| 27 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 28 | static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus) |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 29 | { |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 30 | size_t payload_size = nr_cpus * sizeof(int); |
| 31 | struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size); |
| 32 | |
| 33 | if (cpus != NULL) { |
| 34 | cpus->nr = nr_cpus; |
| 35 | memcpy(cpus->map, tmp_cpus, payload_size); |
| 36 | } |
| 37 | |
| 38 | return cpus; |
| 39 | } |
| 40 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 41 | struct cpu_map *cpu_map__read(FILE *file) |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 42 | { |
| 43 | struct cpu_map *cpus = NULL; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 44 | int nr_cpus = 0; |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 45 | int *tmp_cpus = NULL, *tmp; |
| 46 | int max_entries = 0; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 47 | int n, cpu, prev; |
| 48 | char sep; |
| 49 | |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 50 | sep = 0; |
| 51 | prev = -1; |
| 52 | for (;;) { |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 53 | n = fscanf(file, "%u%c", &cpu, &sep); |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 54 | if (n <= 0) |
| 55 | break; |
| 56 | if (prev >= 0) { |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 57 | int new_max = nr_cpus + cpu - prev - 1; |
| 58 | |
| 59 | if (new_max >= max_entries) { |
| 60 | max_entries = new_max + MAX_NR_CPUS / 2; |
| 61 | tmp = realloc(tmp_cpus, max_entries * sizeof(int)); |
| 62 | if (tmp == NULL) |
| 63 | goto out_free_tmp; |
| 64 | tmp_cpus = tmp; |
| 65 | } |
| 66 | |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 67 | while (++prev < cpu) |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 68 | tmp_cpus[nr_cpus++] = prev; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 69 | } |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 70 | if (nr_cpus == max_entries) { |
| 71 | max_entries += MAX_NR_CPUS; |
| 72 | tmp = realloc(tmp_cpus, max_entries * sizeof(int)); |
| 73 | if (tmp == NULL) |
| 74 | goto out_free_tmp; |
| 75 | tmp_cpus = tmp; |
| 76 | } |
| 77 | |
| 78 | tmp_cpus[nr_cpus++] = cpu; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 79 | if (n == 2 && sep == '-') |
| 80 | prev = cpu; |
| 81 | else |
| 82 | prev = -1; |
| 83 | if (n == 1 || sep == '\n') |
| 84 | break; |
| 85 | } |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 86 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 87 | if (nr_cpus > 0) |
| 88 | cpus = cpu_map__trim_new(nr_cpus, tmp_cpus); |
| 89 | else |
| 90 | cpus = cpu_map__default_new(); |
| 91 | out_free_tmp: |
| 92 | free(tmp_cpus); |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 93 | return cpus; |
| 94 | } |
| 95 | |
| 96 | static struct cpu_map *cpu_map__read_all_cpu_map(void) |
| 97 | { |
| 98 | struct cpu_map *cpus = NULL; |
| 99 | FILE *onlnf; |
| 100 | |
| 101 | onlnf = fopen("/sys/devices/system/cpu/online", "r"); |
| 102 | if (!onlnf) |
| 103 | return cpu_map__default_new(); |
| 104 | |
| 105 | cpus = cpu_map__read(onlnf); |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 106 | fclose(onlnf); |
| 107 | return cpus; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 108 | } |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 109 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 110 | struct cpu_map *cpu_map__new(const char *cpu_list) |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 111 | { |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 112 | struct cpu_map *cpus = NULL; |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 113 | unsigned long start_cpu, end_cpu = 0; |
| 114 | char *p = NULL; |
| 115 | int i, nr_cpus = 0; |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 116 | int *tmp_cpus = NULL, *tmp; |
| 117 | int max_entries = 0; |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 118 | |
| 119 | if (!cpu_list) |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 120 | return cpu_map__read_all_cpu_map(); |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 121 | |
| 122 | if (!isdigit(*cpu_list)) |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 123 | goto out; |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 124 | |
| 125 | while (isdigit(*cpu_list)) { |
| 126 | p = NULL; |
| 127 | start_cpu = strtoul(cpu_list, &p, 0); |
| 128 | if (start_cpu >= INT_MAX |
| 129 | || (*p != '\0' && *p != ',' && *p != '-')) |
| 130 | goto invalid; |
| 131 | |
| 132 | if (*p == '-') { |
| 133 | cpu_list = ++p; |
| 134 | p = NULL; |
| 135 | end_cpu = strtoul(cpu_list, &p, 0); |
| 136 | |
| 137 | if (end_cpu >= INT_MAX || (*p != '\0' && *p != ',')) |
| 138 | goto invalid; |
| 139 | |
| 140 | if (end_cpu < start_cpu) |
| 141 | goto invalid; |
| 142 | } else { |
| 143 | end_cpu = start_cpu; |
| 144 | } |
| 145 | |
| 146 | for (; start_cpu <= end_cpu; start_cpu++) { |
| 147 | /* check for duplicates */ |
| 148 | for (i = 0; i < nr_cpus; i++) |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 149 | if (tmp_cpus[i] == (int)start_cpu) |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 150 | goto invalid; |
| 151 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 152 | if (nr_cpus == max_entries) { |
| 153 | max_entries += MAX_NR_CPUS; |
| 154 | tmp = realloc(tmp_cpus, max_entries * sizeof(int)); |
| 155 | if (tmp == NULL) |
| 156 | goto invalid; |
| 157 | tmp_cpus = tmp; |
| 158 | } |
| 159 | tmp_cpus[nr_cpus++] = (int)start_cpu; |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 160 | } |
| 161 | if (*p) |
| 162 | ++p; |
| 163 | |
| 164 | cpu_list = p; |
| 165 | } |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 166 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 167 | if (nr_cpus > 0) |
| 168 | cpus = cpu_map__trim_new(nr_cpus, tmp_cpus); |
| 169 | else |
| 170 | cpus = cpu_map__default_new(); |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 171 | invalid: |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 172 | free(tmp_cpus); |
| 173 | out: |
| 174 | return cpus; |
| 175 | } |
| 176 | |
Arnaldo Carvalho de Melo | 9ae7d33 | 2012-01-19 14:07:23 -0200 | [diff] [blame] | 177 | size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp) |
| 178 | { |
| 179 | int i; |
| 180 | size_t printed = fprintf(fp, "%d cpu%s: ", |
| 181 | map->nr, map->nr > 1 ? "s" : ""); |
| 182 | for (i = 0; i < map->nr; ++i) |
| 183 | printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]); |
| 184 | |
| 185 | return printed + fprintf(fp, "\n"); |
| 186 | } |
| 187 | |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 188 | struct cpu_map *cpu_map__dummy_new(void) |
| 189 | { |
| 190 | struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int)); |
| 191 | |
| 192 | if (cpus != NULL) { |
| 193 | cpus->nr = 1; |
| 194 | cpus->map[0] = -1; |
| 195 | } |
| 196 | |
| 197 | return cpus; |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 198 | } |
Arnaldo Carvalho de Melo | 915fce2 | 2011-01-14 16:19:12 -0200 | [diff] [blame] | 199 | |
| 200 | void cpu_map__delete(struct cpu_map *map) |
| 201 | { |
| 202 | free(map); |
| 203 | } |