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