cpumap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include <stdio.h>
  4. #include "cpumap.h"
  5. #include "event.h"
  6. #include "util/synthetic-events.h"
  7. #include <string.h>
  8. #include <linux/bitops.h>
  9. #include <perf/cpumap.h>
  10. #include "debug.h"
  11. struct machine;
  12. static int process_event_mask(struct perf_tool *tool __maybe_unused,
  13. union perf_event *event,
  14. struct perf_sample *sample __maybe_unused,
  15. struct machine *machine __maybe_unused)
  16. {
  17. struct perf_record_cpu_map *map_event = &event->cpu_map;
  18. struct perf_record_record_cpu_map *mask;
  19. struct perf_record_cpu_map_data *data;
  20. struct perf_cpu_map *map;
  21. int i;
  22. data = &map_event->data;
  23. TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
  24. mask = (struct perf_record_record_cpu_map *)data->data;
  25. TEST_ASSERT_VAL("wrong nr", mask->nr == 1);
  26. for (i = 0; i < 20; i++) {
  27. TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
  28. }
  29. map = cpu_map__new_data(data);
  30. TEST_ASSERT_VAL("wrong nr", map->nr == 20);
  31. for (i = 0; i < 20; i++) {
  32. TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
  33. }
  34. perf_cpu_map__put(map);
  35. return 0;
  36. }
  37. static int process_event_cpus(struct perf_tool *tool __maybe_unused,
  38. union perf_event *event,
  39. struct perf_sample *sample __maybe_unused,
  40. struct machine *machine __maybe_unused)
  41. {
  42. struct perf_record_cpu_map *map_event = &event->cpu_map;
  43. struct cpu_map_entries *cpus;
  44. struct perf_record_cpu_map_data *data;
  45. struct perf_cpu_map *map;
  46. data = &map_event->data;
  47. TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
  48. cpus = (struct cpu_map_entries *)data->data;
  49. TEST_ASSERT_VAL("wrong nr", cpus->nr == 2);
  50. TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1);
  51. TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256);
  52. map = cpu_map__new_data(data);
  53. TEST_ASSERT_VAL("wrong nr", map->nr == 2);
  54. TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
  55. TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
  56. TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
  57. perf_cpu_map__put(map);
  58. return 0;
  59. }
  60. int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
  61. {
  62. struct perf_cpu_map *cpus;
  63. /* This one is better stores in mask. */
  64. cpus = perf_cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
  65. TEST_ASSERT_VAL("failed to synthesize map",
  66. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
  67. perf_cpu_map__put(cpus);
  68. /* This one is better stores in cpu values. */
  69. cpus = perf_cpu_map__new("1,256");
  70. TEST_ASSERT_VAL("failed to synthesize map",
  71. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
  72. perf_cpu_map__put(cpus);
  73. return 0;
  74. }
  75. static int cpu_map_print(const char *str)
  76. {
  77. struct perf_cpu_map *map = perf_cpu_map__new(str);
  78. char buf[100];
  79. if (!map)
  80. return -1;
  81. cpu_map__snprint(map, buf, sizeof(buf));
  82. return !strcmp(buf, str);
  83. }
  84. int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
  85. {
  86. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
  87. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
  88. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
  89. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
  90. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
  91. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
  92. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
  93. return 0;
  94. }
  95. int test__cpu_map_merge(struct test *test __maybe_unused, int subtest __maybe_unused)
  96. {
  97. struct perf_cpu_map *a = perf_cpu_map__new("4,2,1");
  98. struct perf_cpu_map *b = perf_cpu_map__new("4,5,7");
  99. struct perf_cpu_map *c = perf_cpu_map__merge(a, b);
  100. char buf[100];
  101. TEST_ASSERT_VAL("failed to merge map: bad nr", c->nr == 5);
  102. cpu_map__snprint(c, buf, sizeof(buf));
  103. TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7"));
  104. perf_cpu_map__put(b);
  105. perf_cpu_map__put(c);
  106. return 0;
  107. }