xref: /openbmc/linux/tools/perf/tests/mem2node.c (revision 03638e62)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/bitmap.h>
4 #include <linux/zalloc.h>
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 {
22 	struct cpu_map *map = cpu_map__new(str);
23 	unsigned long *bm = NULL;
24 	int i;
25 
26 	bm = bitmap_alloc(nbits);
27 
28 	if (map && bm) {
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 		zfree(&nodes[i].set);
72 
73 	mem2node__exit(&map);
74 	return 0;
75 }
76