blob: 6fe2c1e7918be34e79b34fb929b0749eda32dc06 [file] [log] [blame]
Arnaldo Carvalho de Melo5875cf42019-06-13 18:29:05 -03001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa81858502018-03-09 11:14:36 +01002#include <linux/compiler.h>
3#include <linux/bitmap.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03004#include <linux/zalloc.h>
Jiri Olsa81858502018-03-09 11:14:36 +01005#include "cpumap.h"
6#include "mem2node.h"
7#include "tests.h"
8
9static 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
20static unsigned long *get_bitmap(const char *str, int nbits)
21{
Jiri Olsaf8548392019-07-21 13:23:49 +020022 struct perf_cpu_map *map = cpu_map__new(str);
Jiri Olsa81858502018-03-09 11:14:36 +010023 unsigned long *bm = NULL;
24 int i;
25
26 bm = bitmap_alloc(nbits);
27
28 if (map && bm) {
Jiri Olsa81858502018-03-09 11:14:36 +010029 for (i = 0; i < map->nr; i++) {
30 set_bit(map->map[i], bm);
31 }
32 }
33
34 if (map)
Jiri Olsa38f01d82019-07-21 13:24:17 +020035 perf_cpu_map__put(map);
Jiri Olsa81858502018-03-09 11:14:36 +010036 else
37 free(bm);
38
39 return bm && map ? bm : NULL;
40}
41
42int 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 Melod8f9da22019-07-04 12:06:20 -030071 zfree(&nodes[i].set);
Jiri Olsa81858502018-03-09 11:14:36 +010072
73 mem2node__exit(&map);
74 return 0;
75}