diff options
author | Don Zickus <dzickus@redhat.com> | 2014-04-07 14:55:21 -0400 |
---|---|---|
committer | Jiri Olsa <jolsa@redhat.com> | 2014-04-22 11:39:12 -0400 |
commit | 7780c25bae59fd042281710f5e1243268e2c18dc (patch) | |
tree | d829ae6b0a4f48152326e79c0927f52f23e4a660 | |
parent | 7c2f8164e5415ef48954f6929f4acae5764afdb4 (diff) |
perf tools: Allow ability to map cpus to nodes easily
This patch figures out the max number of cpus and nodes that are on the
system and creates a map of cpu to node. This allows us to provide a cpu
and quickly get the node associated with it.
It was mostly copied from builtin-kmem.c and tweaked slightly to use less memory
(use possible cpus instead of max). It also calculates the max number of nodes.
Signed-off-by: Don Zickus <dzickus@redhat.com>
Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Link: http://lkml.kernel.org/r/1396896924-129847-2-git-send-email-dzickus@redhat.com
[ Removing out label code in init_cpunode_map ]
[ Adding check for snprintf error ]
[ Removing unneeded returns ]
Signed-off-by: Jiri Olsa <jolsa@redhat.com>
-rw-r--r-- | tools/perf/util/cpumap.c | 160 | ||||
-rw-r--r-- | tools/perf/util/cpumap.h | 35 |
2 files changed, 195 insertions, 0 deletions
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 7fe4994eeb63..526da022b3c4 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c | |||
@@ -317,3 +317,163 @@ int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep) | |||
317 | { | 317 | { |
318 | return cpu_map__build_map(cpus, corep, cpu_map__get_core); | 318 | return cpu_map__build_map(cpus, corep, cpu_map__get_core); |
319 | } | 319 | } |
320 | |||
321 | /* setup simple routines to easily access node numbers given a cpu number */ | ||
322 | static int get_max_num(char *path, int *max) | ||
323 | { | ||
324 | size_t num; | ||
325 | char *buf; | ||
326 | int err = 0; | ||
327 | |||
328 | if (filename__read_str(path, &buf, &num)) | ||
329 | return -1; | ||
330 | |||
331 | buf[num] = '\0'; | ||
332 | |||
333 | /* start on the right, to find highest node num */ | ||
334 | while (--num) { | ||
335 | if ((buf[num] == ',') || (buf[num] == '-')) { | ||
336 | num++; | ||
337 | break; | ||
338 | } | ||
339 | } | ||
340 | if (sscanf(&buf[num], "%d", max) < 1) { | ||
341 | err = -1; | ||
342 | goto out; | ||
343 | } | ||
344 | |||
345 | /* convert from 0-based to 1-based */ | ||
346 | (*max)++; | ||
347 | |||
348 | out: | ||
349 | free(buf); | ||
350 | return err; | ||
351 | } | ||
352 | |||
353 | /* Determine highest possible cpu in the system for sparse allocation */ | ||
354 | static void set_max_cpu_num(void) | ||
355 | { | ||
356 | const char *mnt; | ||
357 | char path[PATH_MAX]; | ||
358 | int ret = -1; | ||
359 | |||
360 | /* set up default */ | ||
361 | max_cpu_num = 4096; | ||
362 | |||
363 | mnt = sysfs__mountpoint(); | ||
364 | if (!mnt) | ||
365 | goto out; | ||
366 | |||
367 | /* get the highest possible cpu number for a sparse allocation */ | ||
368 | ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/kernel_max", mnt); | ||
369 | if (ret == PATH_MAX) { | ||
370 | pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX); | ||
371 | goto out; | ||
372 | } | ||
373 | |||
374 | ret = get_max_num(path, &max_cpu_num); | ||
375 | |||
376 | out: | ||
377 | if (ret) | ||
378 | pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num); | ||
379 | } | ||
380 | |||
381 | /* Determine highest possible node in the system for sparse allocation */ | ||
382 | static void set_max_node_num(void) | ||
383 | { | ||
384 | const char *mnt; | ||
385 | char path[PATH_MAX]; | ||
386 | int ret = -1; | ||
387 | |||
388 | /* set up default */ | ||
389 | max_node_num = 8; | ||
390 | |||
391 | mnt = sysfs__mountpoint(); | ||
392 | if (!mnt) | ||
393 | goto out; | ||
394 | |||
395 | /* get the highest possible cpu number for a sparse allocation */ | ||
396 | ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt); | ||
397 | if (ret == PATH_MAX) { | ||
398 | pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX); | ||
399 | goto out; | ||
400 | } | ||
401 | |||
402 | ret = get_max_num(path, &max_node_num); | ||
403 | |||
404 | out: | ||
405 | if (ret) | ||
406 | pr_err("Failed to read max nodes, using default of %d\n", max_node_num); | ||
407 | } | ||
408 | |||
409 | static int init_cpunode_map(void) | ||
410 | { | ||
411 | int i; | ||
412 | |||
413 | set_max_cpu_num(); | ||
414 | set_max_node_num(); | ||
415 | |||
416 | cpunode_map = calloc(max_cpu_num, sizeof(int)); | ||
417 | if (!cpunode_map) { | ||
418 | pr_err("%s: calloc failed\n", __func__); | ||
419 | return -1; | ||
420 | } | ||
421 | |||
422 | for (i = 0; i < max_cpu_num; i++) | ||
423 | cpunode_map[i] = -1; | ||
424 | |||
425 | return 0; | ||
426 | } | ||
427 | |||
428 | int cpu__setup_cpunode_map(void) | ||
429 | { | ||
430 | struct dirent *dent1, *dent2; | ||
431 | DIR *dir1, *dir2; | ||
432 | unsigned int cpu, mem; | ||
433 | char buf[PATH_MAX]; | ||
434 | char path[PATH_MAX]; | ||
435 | const char *mnt; | ||
436 | int n; | ||
437 | |||
438 | /* initialize globals */ | ||
439 | if (init_cpunode_map()) | ||
440 | return -1; | ||
441 | |||
442 | mnt = sysfs__mountpoint(); | ||
443 | if (!mnt) | ||
444 | return 0; | ||
445 | |||
446 | n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt); | ||
447 | if (n == PATH_MAX) { | ||
448 | pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX); | ||
449 | return -1; | ||
450 | } | ||
451 | |||
452 | dir1 = opendir(path); | ||
453 | if (!dir1) | ||
454 | return 0; | ||
455 | |||
456 | /* walk tree and setup map */ | ||
457 | while ((dent1 = readdir(dir1)) != NULL) { | ||
458 | if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1) | ||
459 | continue; | ||
460 | |||
461 | n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name); | ||
462 | if (n == PATH_MAX) { | ||
463 | pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX); | ||
464 | continue; | ||
465 | } | ||
466 | |||
467 | dir2 = opendir(buf); | ||
468 | if (!dir2) | ||
469 | continue; | ||
470 | while ((dent2 = readdir(dir2)) != NULL) { | ||
471 | if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1) | ||
472 | continue; | ||
473 | cpunode_map[cpu] = mem; | ||
474 | } | ||
475 | closedir(dir2); | ||
476 | } | ||
477 | closedir(dir1); | ||
478 | return 0; | ||
479 | } | ||
diff --git a/tools/perf/util/cpumap.h b/tools/perf/util/cpumap.h index b123bb9d6f55..61a654849002 100644 --- a/tools/perf/util/cpumap.h +++ b/tools/perf/util/cpumap.h | |||
@@ -4,6 +4,9 @@ | |||
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | #include <stdbool.h> | 5 | #include <stdbool.h> |
6 | 6 | ||
7 | #include "perf.h" | ||
8 | #include "util/debug.h" | ||
9 | |||
7 | struct cpu_map { | 10 | struct cpu_map { |
8 | int nr; | 11 | int nr; |
9 | int map[]; | 12 | int map[]; |
@@ -46,4 +49,36 @@ static inline bool cpu_map__empty(const struct cpu_map *map) | |||
46 | return map ? map->map[0] == -1 : true; | 49 | return map ? map->map[0] == -1 : true; |
47 | } | 50 | } |
48 | 51 | ||
52 | int max_cpu_num; | ||
53 | int max_node_num; | ||
54 | int *cpunode_map; | ||
55 | |||
56 | int cpu__setup_cpunode_map(void); | ||
57 | |||
58 | static inline int cpu__max_node(void) | ||
59 | { | ||
60 | if (unlikely(!max_node_num)) | ||
61 | pr_debug("cpu_map not initialized\n"); | ||
62 | |||
63 | return max_node_num; | ||
64 | } | ||
65 | |||
66 | static inline int cpu__max_cpu(void) | ||
67 | { | ||
68 | if (unlikely(!max_cpu_num)) | ||
69 | pr_debug("cpu_map not initialized\n"); | ||
70 | |||
71 | return max_cpu_num; | ||
72 | } | ||
73 | |||
74 | static inline int cpu__get_node(int cpu) | ||
75 | { | ||
76 | if (unlikely(cpunode_map == NULL)) { | ||
77 | pr_debug("cpu_map not initialized\n"); | ||
78 | return -1; | ||
79 | } | ||
80 | |||
81 | return cpunode_map[cpu]; | ||
82 | } | ||
83 | |||
49 | #endif /* __PERF_CPUMAP_H */ | 84 | #endif /* __PERF_CPUMAP_H */ |