blob: a9b48c42e81eb36e34cce24e091be66437270d44 [file] [log] [blame]
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001#include "util.h"
Jiri Olsa4299a542013-11-05 15:14:45 +01002#include "fs.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11003#include "../perf.h"
4#include "cpumap.h"
5#include <assert.h>
6#include <stdio.h>
Stephane Eranian86ee6e12013-02-14 13:57:27 +01007#include <stdlib.h>
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11008
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02009static struct cpu_map *cpu_map__default_new(void)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110010{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020011 struct cpu_map *cpus;
12 int nr_cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110013
14 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020015 if (nr_cpus < 0)
16 return NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110017
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020018 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
19 if (cpus != NULL) {
20 int i;
21 for (i = 0; i < nr_cpus; ++i)
22 cpus->map[i] = i;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110023
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020024 cpus->nr = nr_cpus;
25 }
26
27 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110028}
29
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020030static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110031{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020032 size_t payload_size = nr_cpus * sizeof(int);
33 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
34
35 if (cpus != NULL) {
36 cpus->nr = nr_cpus;
37 memcpy(cpus->map, tmp_cpus, payload_size);
38 }
39
40 return cpus;
41}
42
Yan, Zheng7ae92e72012-09-10 15:53:50 +080043struct cpu_map *cpu_map__read(FILE *file)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020044{
45 struct cpu_map *cpus = NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110046 int nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020047 int *tmp_cpus = NULL, *tmp;
48 int max_entries = 0;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110049 int n, cpu, prev;
50 char sep;
51
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110052 sep = 0;
53 prev = -1;
54 for (;;) {
Yan, Zheng7ae92e72012-09-10 15:53:50 +080055 n = fscanf(file, "%u%c", &cpu, &sep);
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110056 if (n <= 0)
57 break;
58 if (prev >= 0) {
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020059 int new_max = nr_cpus + cpu - prev - 1;
60
61 if (new_max >= max_entries) {
62 max_entries = new_max + MAX_NR_CPUS / 2;
63 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
64 if (tmp == NULL)
65 goto out_free_tmp;
66 tmp_cpus = tmp;
67 }
68
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110069 while (++prev < cpu)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020070 tmp_cpus[nr_cpus++] = prev;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110071 }
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020072 if (nr_cpus == max_entries) {
73 max_entries += MAX_NR_CPUS;
74 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
75 if (tmp == NULL)
76 goto out_free_tmp;
77 tmp_cpus = tmp;
78 }
79
80 tmp_cpus[nr_cpus++] = cpu;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110081 if (n == 2 && sep == '-')
82 prev = cpu;
83 else
84 prev = -1;
85 if (n == 1 || sep == '\n')
86 break;
87 }
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110088
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020089 if (nr_cpus > 0)
90 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
91 else
92 cpus = cpu_map__default_new();
93out_free_tmp:
94 free(tmp_cpus);
Yan, Zheng7ae92e72012-09-10 15:53:50 +080095 return cpus;
96}
97
98static struct cpu_map *cpu_map__read_all_cpu_map(void)
99{
100 struct cpu_map *cpus = NULL;
101 FILE *onlnf;
102
103 onlnf = fopen("/sys/devices/system/cpu/online", "r");
104 if (!onlnf)
105 return cpu_map__default_new();
106
107 cpus = cpu_map__read(onlnf);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200108 fclose(onlnf);
109 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100110}
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200111
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200112struct cpu_map *cpu_map__new(const char *cpu_list)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200113{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200114 struct cpu_map *cpus = NULL;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200115 unsigned long start_cpu, end_cpu = 0;
116 char *p = NULL;
117 int i, nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200118 int *tmp_cpus = NULL, *tmp;
119 int max_entries = 0;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200120
121 if (!cpu_list)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200122 return cpu_map__read_all_cpu_map();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200123
124 if (!isdigit(*cpu_list))
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200125 goto out;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200126
127 while (isdigit(*cpu_list)) {
128 p = NULL;
129 start_cpu = strtoul(cpu_list, &p, 0);
130 if (start_cpu >= INT_MAX
131 || (*p != '\0' && *p != ',' && *p != '-'))
132 goto invalid;
133
134 if (*p == '-') {
135 cpu_list = ++p;
136 p = NULL;
137 end_cpu = strtoul(cpu_list, &p, 0);
138
139 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
140 goto invalid;
141
142 if (end_cpu < start_cpu)
143 goto invalid;
144 } else {
145 end_cpu = start_cpu;
146 }
147
148 for (; start_cpu <= end_cpu; start_cpu++) {
149 /* check for duplicates */
150 for (i = 0; i < nr_cpus; i++)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200151 if (tmp_cpus[i] == (int)start_cpu)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200152 goto invalid;
153
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200154 if (nr_cpus == max_entries) {
155 max_entries += MAX_NR_CPUS;
156 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
157 if (tmp == NULL)
158 goto invalid;
159 tmp_cpus = tmp;
160 }
161 tmp_cpus[nr_cpus++] = (int)start_cpu;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200162 }
163 if (*p)
164 ++p;
165
166 cpu_list = p;
167 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200168
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200169 if (nr_cpus > 0)
170 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
171 else
172 cpus = cpu_map__default_new();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200173invalid:
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200174 free(tmp_cpus);
175out:
176 return cpus;
177}
178
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200179size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
180{
181 int i;
182 size_t printed = fprintf(fp, "%d cpu%s: ",
183 map->nr, map->nr > 1 ? "s" : "");
184 for (i = 0; i < map->nr; ++i)
185 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
186
187 return printed + fprintf(fp, "\n");
188}
189
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200190struct cpu_map *cpu_map__dummy_new(void)
191{
192 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
193
194 if (cpus != NULL) {
195 cpus->nr = 1;
196 cpus->map[0] = -1;
197 }
198
199 return cpus;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200200}
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200201
202void cpu_map__delete(struct cpu_map *map)
203{
204 free(map);
205}
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100206
207int cpu_map__get_socket(struct cpu_map *map, int idx)
208{
209 FILE *fp;
210 const char *mnt;
211 char path[PATH_MAX];
212 int cpu, ret;
213
214 if (idx > map->nr)
215 return -1;
216
217 cpu = map->map[idx];
218
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300219 mnt = sysfs__mountpoint();
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100220 if (!mnt)
221 return -1;
222
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100223 snprintf(path, PATH_MAX,
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100224 "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
225 mnt, cpu);
226
227 fp = fopen(path, "r");
228 if (!fp)
229 return -1;
230 ret = fscanf(fp, "%d", &cpu);
231 fclose(fp);
232 return ret == 1 ? cpu : -1;
233}
234
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100235static int cmp_ids(const void *a, const void *b)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100236{
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100237 return *(int *)a - *(int *)b;
238}
239
240static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
241 int (*f)(struct cpu_map *map, int cpu))
242{
243 struct cpu_map *c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100244 int nr = cpus->nr;
245 int cpu, s1, s2;
246
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100247 /* allocate as much as possible */
248 c = calloc(1, sizeof(*c) + nr * sizeof(int));
249 if (!c)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100250 return -1;
251
252 for (cpu = 0; cpu < nr; cpu++) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100253 s1 = f(cpus, cpu);
254 for (s2 = 0; s2 < c->nr; s2++) {
255 if (s1 == c->map[s2])
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100256 break;
257 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100258 if (s2 == c->nr) {
259 c->map[c->nr] = s1;
260 c->nr++;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100261 }
262 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100263 /* ensure we process id in increasing order */
264 qsort(c->map, c->nr, sizeof(int), cmp_ids);
265
266 *res = c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100267 return 0;
268}
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100269
Stephane Eranian12c08a92013-02-14 13:57:29 +0100270int cpu_map__get_core(struct cpu_map *map, int idx)
271{
272 FILE *fp;
273 const char *mnt;
274 char path[PATH_MAX];
275 int cpu, ret, s;
276
277 if (idx > map->nr)
278 return -1;
279
280 cpu = map->map[idx];
281
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300282 mnt = sysfs__mountpoint();
Stephane Eranian12c08a92013-02-14 13:57:29 +0100283 if (!mnt)
284 return -1;
285
286 snprintf(path, PATH_MAX,
287 "%s/devices/system/cpu/cpu%d/topology/core_id",
288 mnt, cpu);
289
290 fp = fopen(path, "r");
291 if (!fp)
292 return -1;
293 ret = fscanf(fp, "%d", &cpu);
294 fclose(fp);
295 if (ret != 1)
296 return -1;
297
298 s = cpu_map__get_socket(map, idx);
299 if (s == -1)
300 return -1;
301
302 /*
303 * encode socket in upper 16 bits
304 * core_id is relative to socket, and
305 * we need a global id. So we combine
306 * socket+ core id
307 */
308 return (s << 16) | (cpu & 0xffff);
309}
310
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100311int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
312{
313 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket);
314}
Stephane Eranian12c08a92013-02-14 13:57:29 +0100315
316int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
317{
318 return cpu_map__build_map(cpus, corep, cpu_map__get_core);
319}