blob: fa935093a599429011fa214c24645fd0230e3b3a [file] [log] [blame]
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001#include "util.h"
Borislav Petkovcd0cfad2013-12-09 17:14:24 +01002#include <api/fs/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>
Jiri Olsaf77b57a2015-10-25 15:51:25 +01008#include <linux/bitmap.h>
Jiri Olsaf30a79b2015-06-23 00:36:04 +02009#include "asm/bug.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110010
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020011static struct cpu_map *cpu_map__default_new(void)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110012{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020013 struct cpu_map *cpus;
14 int nr_cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110015
16 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020017 if (nr_cpus < 0)
18 return NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110019
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020020 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 Mackerrasa12b51c2010-03-10 20:36:09 +110025
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020026 cpus->nr = nr_cpus;
Jiri Olsaf30a79b2015-06-23 00:36:04 +020027 atomic_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020028 }
29
30 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110031}
32
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020033static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110034{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020035 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 Olsaf30a79b2015-06-23 00:36:04 +020041 atomic_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020042 }
43
44 return cpus;
45}
46
Yan, Zheng7ae92e72012-09-10 15:53:50 +080047struct cpu_map *cpu_map__read(FILE *file)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020048{
49 struct cpu_map *cpus = NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110050 int nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020051 int *tmp_cpus = NULL, *tmp;
52 int max_entries = 0;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110053 int n, cpu, prev;
54 char sep;
55
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110056 sep = 0;
57 prev = -1;
58 for (;;) {
Yan, Zheng7ae92e72012-09-10 15:53:50 +080059 n = fscanf(file, "%u%c", &cpu, &sep);
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110060 if (n <= 0)
61 break;
62 if (prev >= 0) {
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020063 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 Mackerrasa12b51c2010-03-10 20:36:09 +110073 while (++prev < cpu)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020074 tmp_cpus[nr_cpus++] = prev;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110075 }
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020076 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 Mackerrasa12b51c2010-03-10 20:36:09 +110085 if (n == 2 && sep == '-')
86 prev = cpu;
87 else
88 prev = -1;
89 if (n == 1 || sep == '\n')
90 break;
91 }
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110092
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020093 if (nr_cpus > 0)
94 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
95 else
96 cpus = cpu_map__default_new();
97out_free_tmp:
98 free(tmp_cpus);
Yan, Zheng7ae92e72012-09-10 15:53:50 +080099 return cpus;
100}
101
102static 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 Melo60d567e2011-01-03 17:49:48 -0200112 fclose(onlnf);
113 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100114}
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200115
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200116struct cpu_map *cpu_map__new(const char *cpu_list)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200117{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200118 struct cpu_map *cpus = NULL;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200119 unsigned long start_cpu, end_cpu = 0;
120 char *p = NULL;
121 int i, nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200122 int *tmp_cpus = NULL, *tmp;
123 int max_entries = 0;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200124
125 if (!cpu_list)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200126 return cpu_map__read_all_cpu_map();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200127
128 if (!isdigit(*cpu_list))
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200129 goto out;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200130
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 Melo60d567e2011-01-03 17:49:48 -0200155 if (tmp_cpus[i] == (int)start_cpu)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200156 goto invalid;
157
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200158 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 Eranianc45c6ea2010-05-28 12:00:01 +0200166 }
167 if (*p)
168 ++p;
169
170 cpu_list = p;
171 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200172
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200173 if (nr_cpus > 0)
174 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
175 else
176 cpus = cpu_map__default_new();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200177invalid:
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200178 free(tmp_cpus);
179out:
180 return cpus;
181}
182
Jiri Olsaf77b57a2015-10-25 15:51:25 +0100183static 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
Jiri Olsa15d2b992016-01-06 11:49:55 +0100191 for (i = 0; i < cpus->nr; i++) {
192 /*
193 * Special treatment for -1, which is not real cpu number,
194 * and we need to use (int) -1 to initialize map[i],
195 * otherwise it would become 65535.
196 */
197 if (cpus->cpu[i] == (u16) -1)
198 map->map[i] = -1;
199 else
200 map->map[i] = (int) cpus->cpu[i];
201 }
Jiri Olsaf77b57a2015-10-25 15:51:25 +0100202 }
203
204 return map;
205}
206
207static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
208{
209 struct cpu_map *map;
210 int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
211
212 nr = bitmap_weight(mask->mask, nbits);
213
214 map = cpu_map__empty_new(nr);
215 if (map) {
216 int cpu, i = 0;
217
218 for_each_set_bit(cpu, mask->mask, nbits)
219 map->map[i++] = cpu;
220 }
221 return map;
222
223}
224
225struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
226{
227 if (data->type == PERF_CPU_MAP__CPUS)
228 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
229 else
230 return cpu_map__from_mask((struct cpu_map_mask *)data->data);
231}
232
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200233size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
234{
235 int i;
236 size_t printed = fprintf(fp, "%d cpu%s: ",
237 map->nr, map->nr > 1 ? "s" : "");
238 for (i = 0; i < map->nr; ++i)
239 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
240
241 return printed + fprintf(fp, "\n");
242}
243
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200244struct cpu_map *cpu_map__dummy_new(void)
245{
246 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
247
248 if (cpus != NULL) {
249 cpus->nr = 1;
250 cpus->map[0] = -1;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200251 atomic_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200252 }
253
254 return cpus;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200255}
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200256
Jiri Olsa2322f572015-10-25 15:51:17 +0100257struct cpu_map *cpu_map__empty_new(int nr)
258{
259 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
260
261 if (cpus != NULL) {
262 int i;
263
264 cpus->nr = nr;
265 for (i = 0; i < nr; i++)
266 cpus->map[i] = -1;
267
268 atomic_set(&cpus->refcnt, 1);
269 }
270
271 return cpus;
272}
273
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200274static void cpu_map__delete(struct cpu_map *map)
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200275{
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200276 if (map) {
277 WARN_ONCE(atomic_read(&map->refcnt) != 0,
278 "cpu_map refcnt unbalanced\n");
279 free(map);
280 }
281}
282
283struct cpu_map *cpu_map__get(struct cpu_map *map)
284{
285 if (map)
286 atomic_inc(&map->refcnt);
287 return map;
288}
289
290void cpu_map__put(struct cpu_map *map)
291{
292 if (map && atomic_dec_and_test(&map->refcnt))
293 cpu_map__delete(map);
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200294}
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100295
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300296static int cpu__get_topology_int(int cpu, const char *name, int *value)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100297{
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100298 char path[PATH_MAX];
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100299
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100300 snprintf(path, PATH_MAX,
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300301 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100302
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300303 return sysfs__read_int(path, value);
304}
Kan Liang193b6bd2015-09-01 09:58:11 -0400305
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300306int cpu_map__get_socket_id(int cpu)
307{
308 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
309 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400310}
311
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200312int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
Kan Liang193b6bd2015-09-01 09:58:11 -0400313{
314 int cpu;
315
316 if (idx > map->nr)
317 return -1;
318
319 cpu = map->map[idx];
320
321 return cpu_map__get_socket_id(cpu);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100322}
323
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100324static int cmp_ids(const void *a, const void *b)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100325{
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100326 return *(int *)a - *(int *)b;
327}
328
Jiri Olsaf1cbb8f2015-10-16 12:41:14 +0200329int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200330 int (*f)(struct cpu_map *map, int cpu, void *data),
331 void *data)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100332{
333 struct cpu_map *c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100334 int nr = cpus->nr;
335 int cpu, s1, s2;
336
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100337 /* allocate as much as possible */
338 c = calloc(1, sizeof(*c) + nr * sizeof(int));
339 if (!c)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100340 return -1;
341
342 for (cpu = 0; cpu < nr; cpu++) {
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200343 s1 = f(cpus, cpu, data);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100344 for (s2 = 0; s2 < c->nr; s2++) {
345 if (s1 == c->map[s2])
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100346 break;
347 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100348 if (s2 == c->nr) {
349 c->map[c->nr] = s1;
350 c->nr++;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100351 }
352 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100353 /* ensure we process id in increasing order */
354 qsort(c->map, c->nr, sizeof(int), cmp_ids);
355
Kan Liangbc1d0362015-10-09 06:59:23 -0400356 atomic_set(&c->refcnt, 1);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100357 *res = c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100358 return 0;
359}
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100360
Kan Liang193b6bd2015-09-01 09:58:11 -0400361int cpu_map__get_core_id(int cpu)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100362{
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300363 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
364 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400365}
366
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200367int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
Kan Liang193b6bd2015-09-01 09:58:11 -0400368{
369 int cpu, s;
370
371 if (idx > map->nr)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100372 return -1;
373
Kan Liang193b6bd2015-09-01 09:58:11 -0400374 cpu = map->map[idx];
375
376 cpu = cpu_map__get_core_id(cpu);
377
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200378 s = cpu_map__get_socket(map, idx, data);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100379 if (s == -1)
380 return -1;
381
382 /*
383 * encode socket in upper 16 bits
384 * core_id is relative to socket, and
385 * we need a global id. So we combine
386 * socket+ core id
387 */
388 return (s << 16) | (cpu & 0xffff);
389}
390
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100391int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
392{
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200393 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100394}
Stephane Eranian12c08a92013-02-14 13:57:29 +0100395
396int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
397{
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200398 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100399}
Don Zickus7780c252014-04-07 14:55:21 -0400400
401/* setup simple routines to easily access node numbers given a cpu number */
402static int get_max_num(char *path, int *max)
403{
404 size_t num;
405 char *buf;
406 int err = 0;
407
408 if (filename__read_str(path, &buf, &num))
409 return -1;
410
411 buf[num] = '\0';
412
413 /* start on the right, to find highest node num */
414 while (--num) {
415 if ((buf[num] == ',') || (buf[num] == '-')) {
416 num++;
417 break;
418 }
419 }
420 if (sscanf(&buf[num], "%d", max) < 1) {
421 err = -1;
422 goto out;
423 }
424
425 /* convert from 0-based to 1-based */
426 (*max)++;
427
428out:
429 free(buf);
430 return err;
431}
432
433/* Determine highest possible cpu in the system for sparse allocation */
434static void set_max_cpu_num(void)
435{
436 const char *mnt;
437 char path[PATH_MAX];
438 int ret = -1;
439
440 /* set up default */
441 max_cpu_num = 4096;
442
443 mnt = sysfs__mountpoint();
444 if (!mnt)
445 goto out;
446
447 /* get the highest possible cpu number for a sparse allocation */
Don Zickusf5b1f4e2014-04-07 14:55:22 -0400448 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
Don Zickus7780c252014-04-07 14:55:21 -0400449 if (ret == PATH_MAX) {
450 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
451 goto out;
452 }
453
454 ret = get_max_num(path, &max_cpu_num);
455
456out:
457 if (ret)
458 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
459}
460
461/* Determine highest possible node in the system for sparse allocation */
462static void set_max_node_num(void)
463{
464 const char *mnt;
465 char path[PATH_MAX];
466 int ret = -1;
467
468 /* set up default */
469 max_node_num = 8;
470
471 mnt = sysfs__mountpoint();
472 if (!mnt)
473 goto out;
474
475 /* get the highest possible cpu number for a sparse allocation */
476 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
477 if (ret == PATH_MAX) {
478 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
479 goto out;
480 }
481
482 ret = get_max_num(path, &max_node_num);
483
484out:
485 if (ret)
486 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
487}
488
489static int init_cpunode_map(void)
490{
491 int i;
492
493 set_max_cpu_num();
494 set_max_node_num();
495
496 cpunode_map = calloc(max_cpu_num, sizeof(int));
497 if (!cpunode_map) {
498 pr_err("%s: calloc failed\n", __func__);
499 return -1;
500 }
501
502 for (i = 0; i < max_cpu_num; i++)
503 cpunode_map[i] = -1;
504
505 return 0;
506}
507
508int cpu__setup_cpunode_map(void)
509{
510 struct dirent *dent1, *dent2;
511 DIR *dir1, *dir2;
512 unsigned int cpu, mem;
513 char buf[PATH_MAX];
514 char path[PATH_MAX];
515 const char *mnt;
516 int n;
517
518 /* initialize globals */
519 if (init_cpunode_map())
520 return -1;
521
522 mnt = sysfs__mountpoint();
523 if (!mnt)
524 return 0;
525
526 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
527 if (n == PATH_MAX) {
528 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
529 return -1;
530 }
531
532 dir1 = opendir(path);
533 if (!dir1)
534 return 0;
535
536 /* walk tree and setup map */
537 while ((dent1 = readdir(dir1)) != NULL) {
538 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
539 continue;
540
541 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
542 if (n == PATH_MAX) {
543 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
544 continue;
545 }
546
547 dir2 = opendir(buf);
548 if (!dir2)
549 continue;
550 while ((dent2 = readdir(dir2)) != NULL) {
551 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
552 continue;
553 cpunode_map[cpu] = mem;
554 }
555 closedir(dir2);
556 }
557 closedir(dir1);
558 return 0;
559}