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 | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 4 | #include <linux/zalloc.h> |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 5 | #include "cpumap.h" |
| 6 | #include "mem2node.h" |
| 7 | #include "tests.h" |
| 8 | |
| 9 | static struct node { |
| 10 | int node; |
| 11 | const char *map; |
| 12 | } test_nodes[] = { |
| 13 | { .node = 0, .map = "0" }, |
| 14 | { .node = 1, .map = "1-2" }, |
| 15 | { .node = 3, .map = "5-7,9" }, |
| 16 | }; |
| 17 | |
| 18 | #define T TEST_ASSERT_VAL |
| 19 | |
| 20 | static unsigned long *get_bitmap(const char *str, int nbits) |
| 21 | { |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 22 | struct perf_cpu_map *map = cpu_map__new(str); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 23 | unsigned long *bm = NULL; |
| 24 | int i; |
| 25 | |
| 26 | bm = bitmap_alloc(nbits); |
| 27 | |
| 28 | if (map && bm) { |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 29 | for (i = 0; i < map->nr; i++) { |
| 30 | set_bit(map->map[i], bm); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | if (map) |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame^] | 35 | perf_cpu_map__put(map); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 36 | else |
| 37 | free(bm); |
| 38 | |
| 39 | return bm && map ? bm : NULL; |
| 40 | } |
| 41 | |
| 42 | int test__mem2node(struct test *t __maybe_unused, int subtest __maybe_unused) |
| 43 | { |
| 44 | struct mem2node map; |
| 45 | struct memory_node nodes[3]; |
| 46 | struct perf_env env = { |
| 47 | .memory_nodes = (struct memory_node *) &nodes[0], |
| 48 | .nr_memory_nodes = ARRAY_SIZE(nodes), |
| 49 | .memory_bsize = 0x100, |
| 50 | }; |
| 51 | unsigned int i; |
| 52 | |
| 53 | for (i = 0; i < ARRAY_SIZE(nodes); i++) { |
| 54 | nodes[i].node = test_nodes[i].node; |
| 55 | nodes[i].size = 10; |
| 56 | |
| 57 | T("failed: alloc bitmap", |
| 58 | (nodes[i].set = get_bitmap(test_nodes[i].map, 10))); |
| 59 | } |
| 60 | |
| 61 | T("failed: mem2node__init", !mem2node__init(&map, &env)); |
| 62 | T("failed: mem2node__node", 0 == mem2node__node(&map, 0x50)); |
| 63 | T("failed: mem2node__node", 1 == mem2node__node(&map, 0x100)); |
| 64 | T("failed: mem2node__node", 1 == mem2node__node(&map, 0x250)); |
| 65 | T("failed: mem2node__node", 3 == mem2node__node(&map, 0x500)); |
| 66 | T("failed: mem2node__node", 3 == mem2node__node(&map, 0x650)); |
| 67 | T("failed: mem2node__node", -1 == mem2node__node(&map, 0x450)); |
| 68 | T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050)); |
| 69 | |
| 70 | for (i = 0; i < ARRAY_SIZE(nodes); i++) |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 71 | zfree(&nodes[i].set); |
Jiri Olsa | 8185850 | 2018-03-09 11:14:36 +0100 | [diff] [blame] | 72 | |
| 73 | mem2node__exit(&map); |
| 74 | return 0; |
| 75 | } |