xref: /linux/tools/perf/tests/mem2node.c (revision 75d7ba32f9829e778484cf6e96e6e8f80914b0b3)
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 
297fc5b571SAndy Shevchenko 	bm = bitmap_zalloc(nbits);
308185850aSJiri Olsa 
318185850aSJiri Olsa 	if (map && bm) {
3244028699SIan Rogers 		struct perf_cpu cpu;
3344028699SIan Rogers 		int i;
3444028699SIan Rogers 
3544028699SIan Rogers 		perf_cpu_map__for_each_cpu(cpu, i, map)
36*75d7ba32SSean Christopherson 			__set_bit(cpu.cpu, bm);
378185850aSJiri Olsa 	}
388185850aSJiri Olsa 
398185850aSJiri Olsa 	if (map)
4038f01d8dSJiri Olsa 		perf_cpu_map__put(map);
418185850aSJiri Olsa 	else
428185850aSJiri Olsa 		free(bm);
438185850aSJiri Olsa 
448185850aSJiri Olsa 	return bm && map ? bm : NULL;
458185850aSJiri Olsa }
468185850aSJiri Olsa 
4733f44bfdSIan Rogers static int test__mem2node(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
488185850aSJiri Olsa {
498185850aSJiri Olsa 	struct mem2node map;
508185850aSJiri Olsa 	struct memory_node nodes[3];
518185850aSJiri Olsa 	struct perf_env env = {
528185850aSJiri Olsa 		.memory_nodes    = (struct memory_node *) &nodes[0],
538185850aSJiri Olsa 		.nr_memory_nodes = ARRAY_SIZE(nodes),
548185850aSJiri Olsa 		.memory_bsize    = 0x100,
558185850aSJiri Olsa 	};
568185850aSJiri Olsa 	unsigned int i;
578185850aSJiri Olsa 
588185850aSJiri Olsa 	for (i = 0; i < ARRAY_SIZE(nodes); i++) {
598185850aSJiri Olsa 		nodes[i].node = test_nodes[i].node;
608185850aSJiri Olsa 		nodes[i].size = 10;
618185850aSJiri Olsa 
628185850aSJiri Olsa 		T("failed: alloc bitmap",
638185850aSJiri Olsa 		  (nodes[i].set = get_bitmap(test_nodes[i].map, 10)));
648185850aSJiri Olsa 	}
658185850aSJiri Olsa 
668185850aSJiri Olsa 	T("failed: mem2node__init", !mem2node__init(&map, &env));
678185850aSJiri Olsa 	T("failed: mem2node__node",  0 == mem2node__node(&map,   0x50));
688185850aSJiri Olsa 	T("failed: mem2node__node",  1 == mem2node__node(&map,  0x100));
698185850aSJiri Olsa 	T("failed: mem2node__node",  1 == mem2node__node(&map,  0x250));
708185850aSJiri Olsa 	T("failed: mem2node__node",  3 == mem2node__node(&map,  0x500));
718185850aSJiri Olsa 	T("failed: mem2node__node",  3 == mem2node__node(&map,  0x650));
728185850aSJiri Olsa 	T("failed: mem2node__node", -1 == mem2node__node(&map,  0x450));
738185850aSJiri Olsa 	T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050));
748185850aSJiri Olsa 
758185850aSJiri Olsa 	for (i = 0; i < ARRAY_SIZE(nodes); i++)
76d8f9da24SArnaldo Carvalho de Melo 		zfree(&nodes[i].set);
778185850aSJiri Olsa 
788185850aSJiri Olsa 	mem2node__exit(&map);
798185850aSJiri Olsa 	return 0;
808185850aSJiri Olsa }
81d68f0365SIan Rogers 
82d68f0365SIan Rogers DEFINE_SUITE("mem2node", mem2node);
83