xref: /linux-6.15/tools/perf/tests/bitmap.c (revision 71bc3ac8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ff3e33b0SJiri Olsa #include <linux/compiler.h>
3ff3e33b0SJiri Olsa #include <linux/bitmap.h>
49c3516d1SJiri Olsa #include <perf/cpumap.h>
587ffb6c6SArnaldo Carvalho de Melo #include <internal/cpumap.h>
6ff3e33b0SJiri Olsa #include "tests.h"
7ff3e33b0SJiri Olsa #include "debug.h"
8ff3e33b0SJiri Olsa 
9ff3e33b0SJiri Olsa #define NBITS 100
10ff3e33b0SJiri Olsa 
get_bitmap(const char * str,int nbits)11ff3e33b0SJiri Olsa static unsigned long *get_bitmap(const char *str, int nbits)
12ff3e33b0SJiri Olsa {
139c3516d1SJiri Olsa 	struct perf_cpu_map *map = perf_cpu_map__new(str);
14*71bc3ac8SIan Rogers 	unsigned long *bm;
15ff3e33b0SJiri Olsa 
167fc5b571SAndy Shevchenko 	bm = bitmap_zalloc(nbits);
17ff3e33b0SJiri Olsa 
18ff3e33b0SJiri Olsa 	if (map && bm) {
19*71bc3ac8SIan Rogers 		int i;
20*71bc3ac8SIan Rogers 		struct perf_cpu cpu;
21*71bc3ac8SIan Rogers 
22*71bc3ac8SIan Rogers 		perf_cpu_map__for_each_cpu(cpu, i, map)
23*71bc3ac8SIan Rogers 			__set_bit(cpu.cpu, bm);
24ff3e33b0SJiri Olsa 	}
25ff3e33b0SJiri Olsa 
2638f01d8dSJiri Olsa 	perf_cpu_map__put(map);
27ff3e33b0SJiri Olsa 	return bm;
28ff3e33b0SJiri Olsa }
29ff3e33b0SJiri Olsa 
test_bitmap(const char * str)30ff3e33b0SJiri Olsa static int test_bitmap(const char *str)
31ff3e33b0SJiri Olsa {
32ff3e33b0SJiri Olsa 	unsigned long *bm = get_bitmap(str, NBITS);
33ff3e33b0SJiri Olsa 	char buf[100];
34ff3e33b0SJiri Olsa 	int ret;
35ff3e33b0SJiri Olsa 
36ff3e33b0SJiri Olsa 	bitmap_scnprintf(bm, NBITS, buf, sizeof(buf));
37ff3e33b0SJiri Olsa 	pr_debug("bitmap: %s\n", buf);
38ff3e33b0SJiri Olsa 
39ff3e33b0SJiri Olsa 	ret = !strcmp(buf, str);
40ff3e33b0SJiri Olsa 	free(bm);
41ff3e33b0SJiri Olsa 	return ret;
42ff3e33b0SJiri Olsa }
43ff3e33b0SJiri Olsa 
test__bitmap_print(struct test_suite * test __maybe_unused,int subtest __maybe_unused)4433f44bfdSIan Rogers static int test__bitmap_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
45ff3e33b0SJiri Olsa {
46ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("1"));
47ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,5"));
48ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3,5,7,9,11,13,15,17,19,21-40"));
49ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("2-5"));
50ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
51ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
52ff3e33b0SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", test_bitmap("1-10,12-20,22-30,32-40"));
53ff3e33b0SJiri Olsa 	return 0;
54ff3e33b0SJiri Olsa }
55d68f0365SIan Rogers 
56d68f0365SIan Rogers DEFINE_SUITE("Print bitmap", bitmap_print);
57