mem2node.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <linux/compiler.h>
  2. #include <linux/bitmap.h>
  3. #include "cpumap.h"
  4. #include "mem2node.h"
  5. #include "tests.h"
  6. static struct node {
  7. int node;
  8. const char *map;
  9. } test_nodes[] = {
  10. { .node = 0, .map = "0" },
  11. { .node = 1, .map = "1-2" },
  12. { .node = 3, .map = "5-7,9" },
  13. };
  14. #define T TEST_ASSERT_VAL
  15. static unsigned long *get_bitmap(const char *str, int nbits)
  16. {
  17. struct cpu_map *map = cpu_map__new(str);
  18. unsigned long *bm = NULL;
  19. int i;
  20. bm = bitmap_alloc(nbits);
  21. if (map && bm) {
  22. for (i = 0; i < map->nr; i++) {
  23. set_bit(map->map[i], bm);
  24. }
  25. }
  26. if (map)
  27. cpu_map__put(map);
  28. else
  29. free(bm);
  30. return bm && map ? bm : NULL;
  31. }
  32. int test__mem2node(struct test *t __maybe_unused, int subtest __maybe_unused)
  33. {
  34. struct mem2node map;
  35. struct memory_node nodes[3];
  36. struct perf_env env = {
  37. .memory_nodes = (struct memory_node *) &nodes[0],
  38. .nr_memory_nodes = ARRAY_SIZE(nodes),
  39. .memory_bsize = 0x100,
  40. };
  41. unsigned int i;
  42. for (i = 0; i < ARRAY_SIZE(nodes); i++) {
  43. nodes[i].node = test_nodes[i].node;
  44. nodes[i].size = 10;
  45. T("failed: alloc bitmap",
  46. (nodes[i].set = get_bitmap(test_nodes[i].map, 10)));
  47. }
  48. T("failed: mem2node__init", !mem2node__init(&map, &env));
  49. T("failed: mem2node__node", 0 == mem2node__node(&map, 0x50));
  50. T("failed: mem2node__node", 1 == mem2node__node(&map, 0x100));
  51. T("failed: mem2node__node", 1 == mem2node__node(&map, 0x250));
  52. T("failed: mem2node__node", 3 == mem2node__node(&map, 0x500));
  53. T("failed: mem2node__node", 3 == mem2node__node(&map, 0x650));
  54. T("failed: mem2node__node", -1 == mem2node__node(&map, 0x450));
  55. T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050));
  56. for (i = 0; i < ARRAY_SIZE(nodes); i++)
  57. free(nodes[i].set);
  58. mem2node__exit(&map);
  59. return 0;
  60. }