Arnaldo Carvalho de Melo | 5875cf4 | 2019-06-13 18:29:05 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 2 | #include <linux/compiler.h> |
| 3 | #include <linux/bitmap.h> |
Arnaldo Carvalho de Melo | b6b5574 | 2019-08-29 17:10:59 -0300 | [diff] [blame] | 4 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 5 | #include <linux/zalloc.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 6 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | 87ffb6c | 2019-09-10 16:29:02 +0100 | [diff] [blame] | 7 | #include <internal/cpumap.h> |
Arnaldo Carvalho de Melo | 5e51b0b | 2019-08-22 10:48:31 -0300 | [diff] [blame] | 8 | #include "debug.h" |
Arnaldo Carvalho de Melo | b6b5574 | 2019-08-29 17:10:59 -0300 | [diff] [blame] | 9 | #include "env.h" |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 10 | #include "mem2node.h" |
| 11 | #include "tests.h" |
| 12 | |
| 13 | static struct node { |
| 14 | int node; |
| 15 | const char *map; |
| 16 | } test_nodes[] = { |
| 17 | { .node = 0, .map = "0" }, |
| 18 | { .node = 1, .map = "1-2" }, |
| 19 | { .node = 3, .map = "5-7,9" }, |
| 20 | }; |
| 21 | |
| 22 | #define T TEST_ASSERT_VAL |
| 23 | |
| 24 | static unsigned long *get_bitmap(const char *str, int nbits) |
| 25 | { |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 26 | struct perf_cpu_map *map = perf_cpu_map__new(str); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 27 | unsigned long *bm = NULL; |
| 28 | int i; |
| 29 | |
Andy Shevchenko | 7fc5b57 | 2021-09-07 19:59:35 -0700 | [diff] [blame] | 30 | bm = bitmap_zalloc(nbits); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 31 | |
| 32 | if (map && bm) { |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 33 | for (i = 0; i < map->nr; i++) { |
| 34 | set_bit(map->map[i], bm); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (map) |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 39 | perf_cpu_map__put(map); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 40 | else |
| 41 | free(bm); |
| 42 | |
| 43 | return bm && map ? bm : NULL; |
| 44 | } |
| 45 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 46 | static int test__mem2node(struct test_suite *t __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 47 | { |
| 48 | struct mem2node map; |
| 49 | struct memory_node nodes[3]; |
| 50 | struct perf_env env = { |
| 51 | .memory_nodes = (struct memory_node *) &nodes[0], |
| 52 | .nr_memory_nodes = ARRAY_SIZE(nodes), |
| 53 | .memory_bsize = 0x100, |
| 54 | }; |
| 55 | unsigned int i; |
| 56 | |
| 57 | for (i = 0; i < ARRAY_SIZE(nodes); i++) { |
| 58 | nodes[i].node = test_nodes[i].node; |
| 59 | nodes[i].size = 10; |
| 60 | |
| 61 | T("failed: alloc bitmap", |
| 62 | (nodes[i].set = get_bitmap(test_nodes[i].map, 10))); |
| 63 | } |
| 64 | |
| 65 | T("failed: mem2node__init", !mem2node__init(&map, &env)); |
| 66 | T("failed: mem2node__node", 0 == mem2node__node(&map, 0x50)); |
| 67 | T("failed: mem2node__node", 1 == mem2node__node(&map, 0x100)); |
| 68 | T("failed: mem2node__node", 1 == mem2node__node(&map, 0x250)); |
| 69 | T("failed: mem2node__node", 3 == mem2node__node(&map, 0x500)); |
| 70 | T("failed: mem2node__node", 3 == mem2node__node(&map, 0x650)); |
| 71 | T("failed: mem2node__node", -1 == mem2node__node(&map, 0x450)); |
| 72 | T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050)); |
| 73 | |
| 74 | for (i = 0; i < ARRAY_SIZE(nodes); i++) |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 75 | zfree(&nodes[i].set); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 76 | |
| 77 | mem2node__exit(&map); |
| 78 | return 0; |
| 79 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 80 | |
| 81 | DEFINE_SUITE("mem2node", mem2node); |