Linux Perf
mem2node.c
Go to the documentation of this file.
1 #include <linux/compiler.h>
2 #include <linux/bitmap.h>
3 #include "cpumap.h"
4 #include "mem2node.h"
5 #include "tests.h"
6 
7 static struct node {
8  int node;
9  const char *map;
10 } test_nodes[] = {
11  { .node = 0, .map = "0" },
12  { .node = 1, .map = "1-2" },
13  { .node = 3, .map = "5-7,9" },
14 };
15 
16 #define T TEST_ASSERT_VAL
17 
18 static unsigned long *get_bitmap(const char *str, int nbits)
19 {
20  struct cpu_map *map = cpu_map__new(str);
21  unsigned long *bm = NULL;
22  int i;
23 
24  bm = bitmap_alloc(nbits);
25 
26  if (map && bm) {
27  bitmap_zero(bm, nbits);
28 
29  for (i = 0; i < map->nr; i++) {
30  set_bit(map->map[i], bm);
31  }
32  }
33 
34  if (map)
35  cpu_map__put(map);
36  else
37  free(bm);
38 
39  return bm && map ? bm : NULL;
40 }
41 
42 int 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++)
71  free(nodes[i].set);
72 
73  mem2node__exit(&map);
74  return 0;
75 }
const char * map
Definition: mem2node.c:9
int nr
Definition: cpumap.h:14
int mem2node__node(struct mem2node *map, u64 addr)
Definition: mem2node.c:114
Definition: mem2node.c:7
Definition: env.h:36
u64 node
Definition: env.h:31
Definition: cpumap.h:12
void mem2node__exit(struct mem2node *map)
Definition: mem2node.c:109
u64 size
Definition: env.h:32
void cpu_map__put(struct cpu_map *map)
Definition: cpumap.c:298
struct memory_node * memory_nodes
Definition: env.h:64
static unsigned long * get_bitmap(const char *str, int nbits)
Definition: mem2node.c:18
static int str(yyscan_t scanner, int token)
#define T
Definition: mem2node.c:16
int map[]
Definition: cpumap.h:15
int node
Definition: mem2node.c:8
Definition: jevents.c:228
int test__mem2node(struct test *t __maybe_unused, int subtest __maybe_unused)
Definition: mem2node.c:42
Definition: tests.h:30
int mem2node__init(struct mem2node *map, struct perf_env *env)
Definition: mem2node.c:43
static struct node test_nodes[]
void free(void *)
struct cpu_map * cpu_map__new(const char *cpu_list)
Definition: cpumap.c:125