cpumap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <internal/cpumap.h>
  10. #include "debug.h"
  11. struct machine;
  12. static int process_event_mask(const 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_cpu_map_data *data;
  19. struct perf_cpu_map *map;
  20. unsigned int long_size;
  21. data = &map_event->data;
  22. TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
  23. long_size = data->mask32_data.long_size;
  24. TEST_ASSERT_VAL("wrong long_size", long_size == 4 || long_size == 8);
  25. TEST_ASSERT_VAL("wrong nr", data->mask32_data.nr == 1);
  26. TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(0, data));
  27. TEST_ASSERT_VAL("wrong cpu", !perf_record_cpu_map_data__test_bit(1, data));
  28. for (int i = 2; i <= 20; i++)
  29. TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(i, data));
  30. map = cpu_map__new_data(data);
  31. TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 20);
  32. TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 0);
  33. for (int i = 2; i <= 20; i++)
  34. TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, i - 1).cpu == i);
  35. perf_cpu_map__put(map);
  36. return 0;
  37. }
  38. static int process_event_cpus(const struct perf_tool *tool __maybe_unused,
  39. union perf_event *event,
  40. struct perf_sample *sample __maybe_unused,
  41. struct machine *machine __maybe_unused)
  42. {
  43. struct perf_record_cpu_map *map_event = &event->cpu_map;
  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. TEST_ASSERT_VAL("wrong nr", data->cpus_data.nr == 2);
  49. TEST_ASSERT_VAL("wrong cpu", data->cpus_data.cpu[0] == 1);
  50. TEST_ASSERT_VAL("wrong cpu", data->cpus_data.cpu[1] == 256);
  51. map = cpu_map__new_data(data);
  52. TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 2);
  53. TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1);
  54. TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 1).cpu == 256);
  55. TEST_ASSERT_VAL("wrong refcnt", refcount_read(perf_cpu_map__refcnt(map)) == 1);
  56. perf_cpu_map__put(map);
  57. return 0;
  58. }
  59. static int process_event_range_cpus(const struct perf_tool *tool __maybe_unused,
  60. union perf_event *event,
  61. struct perf_sample *sample __maybe_unused,
  62. struct machine *machine __maybe_unused)
  63. {
  64. struct perf_record_cpu_map *map_event = &event->cpu_map;
  65. struct perf_record_cpu_map_data *data;
  66. struct perf_cpu_map *map;
  67. data = &map_event->data;
  68. TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__RANGE_CPUS);
  69. TEST_ASSERT_VAL("wrong any_cpu", data->range_cpu_data.any_cpu == 0);
  70. TEST_ASSERT_VAL("wrong start_cpu", data->range_cpu_data.start_cpu == 1);
  71. TEST_ASSERT_VAL("wrong end_cpu", data->range_cpu_data.end_cpu == 256);
  72. map = cpu_map__new_data(data);
  73. TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 256);
  74. TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1);
  75. TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__max(map).cpu == 256);
  76. TEST_ASSERT_VAL("wrong refcnt", refcount_read(perf_cpu_map__refcnt(map)) == 1);
  77. perf_cpu_map__put(map);
  78. return 0;
  79. }
  80. static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  81. {
  82. struct perf_cpu_map *cpus;
  83. /* This one is better stored in a mask. */
  84. cpus = perf_cpu_map__new("0,2-20");
  85. TEST_ASSERT_VAL("failed to synthesize map",
  86. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
  87. perf_cpu_map__put(cpus);
  88. /* This one is better stored in cpu values. */
  89. cpus = perf_cpu_map__new("1,256");
  90. TEST_ASSERT_VAL("failed to synthesize map",
  91. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
  92. perf_cpu_map__put(cpus);
  93. /* This one is better stored as a range. */
  94. cpus = perf_cpu_map__new("1-256");
  95. TEST_ASSERT_VAL("failed to synthesize map",
  96. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_range_cpus, NULL));
  97. perf_cpu_map__put(cpus);
  98. return 0;
  99. }
  100. static int cpu_map_print(const char *str)
  101. {
  102. struct perf_cpu_map *map = perf_cpu_map__new(str);
  103. char buf[100];
  104. if (!map)
  105. return -1;
  106. cpu_map__snprint(map, buf, sizeof(buf));
  107. perf_cpu_map__put(map);
  108. return !strcmp(buf, str);
  109. }
  110. static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  111. {
  112. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
  113. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
  114. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
  115. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
  116. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
  117. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
  118. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
  119. return 0;
  120. }
  121. static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  122. {
  123. struct perf_cpu_map *a = perf_cpu_map__new("4,2,1");
  124. struct perf_cpu_map *b = perf_cpu_map__new("4,5,7");
  125. struct perf_cpu_map *c = perf_cpu_map__merge(a, b);
  126. char buf[100];
  127. TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) == 5);
  128. cpu_map__snprint(c, buf, sizeof(buf));
  129. TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7"));
  130. perf_cpu_map__put(b);
  131. perf_cpu_map__put(c);
  132. return 0;
  133. }
  134. static int __test__cpu_map_intersect(const char *lhs, const char *rhs, int nr, const char *expected)
  135. {
  136. struct perf_cpu_map *a = perf_cpu_map__new(lhs);
  137. struct perf_cpu_map *b = perf_cpu_map__new(rhs);
  138. struct perf_cpu_map *c = perf_cpu_map__intersect(a, b);
  139. char buf[100];
  140. TEST_ASSERT_EQUAL("failed to intersect map: bad nr", perf_cpu_map__nr(c), nr);
  141. cpu_map__snprint(c, buf, sizeof(buf));
  142. TEST_ASSERT_VAL("failed to intersect map: bad result", !strcmp(buf, expected));
  143. perf_cpu_map__put(a);
  144. perf_cpu_map__put(b);
  145. perf_cpu_map__put(c);
  146. return 0;
  147. }
  148. static int test__cpu_map_intersect(struct test_suite *test __maybe_unused,
  149. int subtest __maybe_unused)
  150. {
  151. int ret;
  152. ret = __test__cpu_map_intersect("4,2,1", "4,5,7", 1, "4");
  153. if (ret)
  154. return ret;
  155. ret = __test__cpu_map_intersect("1-8", "6-9", 3, "6-8");
  156. if (ret)
  157. return ret;
  158. ret = __test__cpu_map_intersect("1-8,12-20", "6-9,15", 4, "6-8,15");
  159. if (ret)
  160. return ret;
  161. ret = __test__cpu_map_intersect("4,2,1", "1", 1, "1");
  162. if (ret)
  163. return ret;
  164. ret = __test__cpu_map_intersect("1", "4,2,1", 1, "1");
  165. if (ret)
  166. return ret;
  167. ret = __test__cpu_map_intersect("1", "1", 1, "1");
  168. return ret;
  169. }
  170. static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  171. {
  172. struct perf_cpu_map *any = perf_cpu_map__new_any_cpu();
  173. struct perf_cpu_map *one = perf_cpu_map__new("1");
  174. struct perf_cpu_map *two = perf_cpu_map__new("2");
  175. struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two);
  176. struct perf_cpu_map *pair = perf_cpu_map__new("1-2");
  177. struct perf_cpu_map *tmp;
  178. struct perf_cpu_map *maps[] = {empty, any, one, two, pair};
  179. for (size_t i = 0; i < ARRAY_SIZE(maps); i++) {
  180. /* Maps equal themself. */
  181. TEST_ASSERT_VAL("equal", perf_cpu_map__equal(maps[i], maps[i]));
  182. for (size_t j = 0; j < ARRAY_SIZE(maps); j++) {
  183. /* Maps dont't equal each other. */
  184. if (i == j)
  185. continue;
  186. TEST_ASSERT_VAL("not equal", !perf_cpu_map__equal(maps[i], maps[j]));
  187. }
  188. }
  189. /* Maps equal made maps. */
  190. tmp = perf_cpu_map__merge(perf_cpu_map__get(one), two);
  191. TEST_ASSERT_VAL("pair", perf_cpu_map__equal(pair, tmp));
  192. perf_cpu_map__put(tmp);
  193. tmp = perf_cpu_map__intersect(pair, one);
  194. TEST_ASSERT_VAL("one", perf_cpu_map__equal(one, tmp));
  195. perf_cpu_map__put(tmp);
  196. for (size_t i = 0; i < ARRAY_SIZE(maps); i++)
  197. perf_cpu_map__put(maps[i]);
  198. return TEST_OK;
  199. }
  200. static struct test_case tests__cpu_map[] = {
  201. TEST_CASE("Synthesize cpu map", cpu_map_synthesize),
  202. TEST_CASE("Print cpu map", cpu_map_print),
  203. TEST_CASE("Merge cpu map", cpu_map_merge),
  204. TEST_CASE("Intersect cpu map", cpu_map_intersect),
  205. TEST_CASE("Equal cpu map", cpu_map_equal),
  206. { .name = NULL, }
  207. };
  208. struct test_suite suite__cpu_map = {
  209. .desc = "CPU map",
  210. .test_cases = tests__cpu_map,
  211. };