mem2node.c (2025B)
1// SPDX-License-Identifier: GPL-2.0 2#include <linux/compiler.h> 3#include <linux/bitmap.h> 4#include <linux/kernel.h> 5#include <linux/zalloc.h> 6#include <perf/cpumap.h> 7#include <internal/cpumap.h> 8#include "debug.h" 9#include "env.h" 10#include "mem2node.h" 11#include "tests.h" 12 13static struct node { 14 int node; 15 const char *map; 16} test_nodes[] = { 17 { .node = 0, .map = "0" }, 18 { .node = 1, .map = "1-2" }, 19 { .node = 3, .map = "5-7,9" }, 20}; 21 22#define T TEST_ASSERT_VAL 23 24static unsigned long *get_bitmap(const char *str, int nbits) 25{ 26 struct perf_cpu_map *map = perf_cpu_map__new(str); 27 unsigned long *bm = NULL; 28 29 bm = bitmap_zalloc(nbits); 30 31 if (map && bm) { 32 struct perf_cpu cpu; 33 int i; 34 35 perf_cpu_map__for_each_cpu(cpu, i, map) 36 set_bit(cpu.cpu, bm); 37 } 38 39 if (map) 40 perf_cpu_map__put(map); 41 else 42 free(bm); 43 44 return bm && map ? bm : NULL; 45} 46 47static int test__mem2node(struct test_suite *t __maybe_unused, int subtest __maybe_unused) 48{ 49 struct mem2node map; 50 struct memory_node nodes[3]; 51 struct perf_env env = { 52 .memory_nodes = (struct memory_node *) &nodes[0], 53 .nr_memory_nodes = ARRAY_SIZE(nodes), 54 .memory_bsize = 0x100, 55 }; 56 unsigned int i; 57 58 for (i = 0; i < ARRAY_SIZE(nodes); i++) { 59 nodes[i].node = test_nodes[i].node; 60 nodes[i].size = 10; 61 62 T("failed: alloc bitmap", 63 (nodes[i].set = get_bitmap(test_nodes[i].map, 10))); 64 } 65 66 T("failed: mem2node__init", !mem2node__init(&map, &env)); 67 T("failed: mem2node__node", 0 == mem2node__node(&map, 0x50)); 68 T("failed: mem2node__node", 1 == mem2node__node(&map, 0x100)); 69 T("failed: mem2node__node", 1 == mem2node__node(&map, 0x250)); 70 T("failed: mem2node__node", 3 == mem2node__node(&map, 0x500)); 71 T("failed: mem2node__node", 3 == mem2node__node(&map, 0x650)); 72 T("failed: mem2node__node", -1 == mem2node__node(&map, 0x450)); 73 T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050)); 74 75 for (i = 0; i < ARRAY_SIZE(nodes); i++) 76 zfree(&nodes[i].set); 77 78 mem2node__exit(&map); 79 return 0; 80} 81 82DEFINE_SUITE("mem2node", mem2node);