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