xref: /linux/tools/perf/tests/mem2node.c (revision d68f0365087395fe232e39ac9c8ee53627522c3c)
15875cf4cSArnaldo Carvalho de Melo // SPDX-License-Identifier: GPL-2.0
28185850aSJiri Olsa #include <linux/compiler.h>
38185850aSJiri Olsa #include <linux/bitmap.h>
4b6b5574bSArnaldo Carvalho de Melo #include <linux/kernel.h>
5d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
69c3516d1SJiri Olsa #include <perf/cpumap.h>
787ffb6c6SArnaldo Carvalho de Melo #include <internal/cpumap.h>
85e51b0bbSArnaldo Carvalho de Melo #include "debug.h"
9b6b5574bSArnaldo Carvalho de Melo #include "env.h"
108185850aSJiri Olsa #include "mem2node.h"
118185850aSJiri Olsa #include "tests.h"
128185850aSJiri Olsa 
138185850aSJiri Olsa static struct node {
148185850aSJiri Olsa 	int		 node;
158185850aSJiri Olsa 	const char 	*map;
168185850aSJiri Olsa } test_nodes[] = {
178185850aSJiri Olsa 	{ .node = 0, .map = "0"     },
188185850aSJiri Olsa 	{ .node = 1, .map = "1-2"   },
198185850aSJiri Olsa 	{ .node = 3, .map = "5-7,9" },
208185850aSJiri Olsa };
218185850aSJiri Olsa 
228185850aSJiri Olsa #define T TEST_ASSERT_VAL
238185850aSJiri Olsa 
248185850aSJiri Olsa static unsigned long *get_bitmap(const char *str, int nbits)
258185850aSJiri Olsa {
269c3516d1SJiri Olsa 	struct perf_cpu_map *map = perf_cpu_map__new(str);
278185850aSJiri Olsa 	unsigned long *bm = NULL;
288185850aSJiri Olsa 	int i;
298185850aSJiri Olsa 
307fc5b571SAndy Shevchenko 	bm = bitmap_zalloc(nbits);
318185850aSJiri Olsa 
328185850aSJiri Olsa 	if (map && bm) {
338185850aSJiri Olsa 		for (i = 0; i < map->nr; i++) {
348185850aSJiri Olsa 			set_bit(map->map[i], bm);
358185850aSJiri Olsa 		}
368185850aSJiri Olsa 	}
378185850aSJiri Olsa 
388185850aSJiri Olsa 	if (map)
3938f01d8dSJiri Olsa 		perf_cpu_map__put(map);
408185850aSJiri Olsa 	else
418185850aSJiri Olsa 		free(bm);
428185850aSJiri Olsa 
438185850aSJiri Olsa 	return bm && map ? bm : NULL;
448185850aSJiri Olsa }
458185850aSJiri Olsa 
46*d68f0365SIan Rogers static int test__mem2node(struct test *t __maybe_unused, int subtest __maybe_unused)
478185850aSJiri Olsa {
488185850aSJiri Olsa 	struct mem2node map;
498185850aSJiri Olsa 	struct memory_node nodes[3];
508185850aSJiri Olsa 	struct perf_env env = {
518185850aSJiri Olsa 		.memory_nodes    = (struct memory_node *) &nodes[0],
528185850aSJiri Olsa 		.nr_memory_nodes = ARRAY_SIZE(nodes),
538185850aSJiri Olsa 		.memory_bsize    = 0x100,
548185850aSJiri Olsa 	};
558185850aSJiri Olsa 	unsigned int i;
568185850aSJiri Olsa 
578185850aSJiri Olsa 	for (i = 0; i < ARRAY_SIZE(nodes); i++) {
588185850aSJiri Olsa 		nodes[i].node = test_nodes[i].node;
598185850aSJiri Olsa 		nodes[i].size = 10;
608185850aSJiri Olsa 
618185850aSJiri Olsa 		T("failed: alloc bitmap",
628185850aSJiri Olsa 		  (nodes[i].set = get_bitmap(test_nodes[i].map, 10)));
638185850aSJiri Olsa 	}
648185850aSJiri Olsa 
658185850aSJiri Olsa 	T("failed: mem2node__init", !mem2node__init(&map, &env));
668185850aSJiri Olsa 	T("failed: mem2node__node",  0 == mem2node__node(&map,   0x50));
678185850aSJiri Olsa 	T("failed: mem2node__node",  1 == mem2node__node(&map,  0x100));
688185850aSJiri Olsa 	T("failed: mem2node__node",  1 == mem2node__node(&map,  0x250));
698185850aSJiri Olsa 	T("failed: mem2node__node",  3 == mem2node__node(&map,  0x500));
708185850aSJiri Olsa 	T("failed: mem2node__node",  3 == mem2node__node(&map,  0x650));
718185850aSJiri Olsa 	T("failed: mem2node__node", -1 == mem2node__node(&map,  0x450));
728185850aSJiri Olsa 	T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050));
738185850aSJiri Olsa 
748185850aSJiri Olsa 	for (i = 0; i < ARRAY_SIZE(nodes); i++)
75d8f9da24SArnaldo Carvalho de Melo 		zfree(&nodes[i].set);
768185850aSJiri Olsa 
778185850aSJiri Olsa 	mem2node__exit(&map);
788185850aSJiri Olsa 	return 0;
798185850aSJiri Olsa }
80*d68f0365SIan Rogers 
81*d68f0365SIan Rogers DEFINE_SUITE("mem2node", mem2node);
82