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