xref: /openbmc/linux/tools/perf/tests/mem2node.c (revision 2359ccdd)
1 #include <linux/compiler.h>
2 #include <linux/bitmap.h>
3 #include "cpumap.h"
4 #include "mem2node.h"
5 #include "tests.h"
6 
7 static struct node {
8 	int		 node;
9 	const char 	*map;
10 } test_nodes[] = {
11 	{ .node = 0, .map = "0"     },
12 	{ .node = 1, .map = "1-2"   },
13 	{ .node = 3, .map = "5-7,9" },
14 };
15 
16 #define T TEST_ASSERT_VAL
17 
18 static unsigned long *get_bitmap(const char *str, int nbits)
19 {
20 	struct cpu_map *map = cpu_map__new(str);
21 	unsigned long *bm = NULL;
22 	int i;
23 
24 	bm = bitmap_alloc(nbits);
25 
26 	if (map && bm) {
27 		bitmap_zero(bm, nbits);
28 
29 		for (i = 0; i < map->nr; i++) {
30 			set_bit(map->map[i], bm);
31 		}
32 	}
33 
34 	if (map)
35 		cpu_map__put(map);
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++)
71 		free(nodes[i].set);
72 
73 	mem2node__exit(&map);
74 	return 0;
75 }
76