cpumap.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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(const char *lhs, const char *rhs, int nr, const char *expected)
  122. {
  123. struct perf_cpu_map *a = perf_cpu_map__new(lhs);
  124. struct perf_cpu_map *b = perf_cpu_map__new(rhs);
  125. char buf[100];
  126. perf_cpu_map__merge(&a, b);
  127. TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(a) == nr);
  128. cpu_map__snprint(a, buf, sizeof(buf));
  129. TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, expected));
  130. perf_cpu_map__put(b);
  131. /*
  132. * If 'b' is a superset of 'a', 'a' points to the same map with the
  133. * map 'b'. In this case, the owner 'b' has released the resource above
  134. * but 'a' still keeps the ownership, the reference counter should be 1.
  135. */
  136. TEST_ASSERT_VAL("unexpected refcnt: bad result",
  137. refcount_read(perf_cpu_map__refcnt(a)) == 1);
  138. perf_cpu_map__put(a);
  139. return 0;
  140. }
  141. static int test__cpu_map_merge(struct test_suite *test __maybe_unused,
  142. int subtest __maybe_unused)
  143. {
  144. int ret;
  145. ret = __test__cpu_map_merge("4,2,1", "4,5,7", 5, "1-2,4-5,7");
  146. if (ret)
  147. return ret;
  148. ret = __test__cpu_map_merge("1-8", "6-9", 9, "1-9");
  149. if (ret)
  150. return ret;
  151. ret = __test__cpu_map_merge("1-8,12-20", "6-9,15", 18, "1-9,12-20");
  152. if (ret)
  153. return ret;
  154. ret = __test__cpu_map_merge("4,2,1", "1", 3, "1-2,4");
  155. if (ret)
  156. return ret;
  157. ret = __test__cpu_map_merge("1", "4,2,1", 3, "1-2,4");
  158. if (ret)
  159. return ret;
  160. ret = __test__cpu_map_merge("1", "1", 1, "1");
  161. return ret;
  162. }
  163. static int __test__cpu_map_intersect(const char *lhs, const char *rhs, int nr, const char *expected)
  164. {
  165. struct perf_cpu_map *a = perf_cpu_map__new(lhs);
  166. struct perf_cpu_map *b = perf_cpu_map__new(rhs);
  167. struct perf_cpu_map *c = perf_cpu_map__intersect(a, b);
  168. char buf[100];
  169. TEST_ASSERT_EQUAL("failed to intersect map: bad nr", perf_cpu_map__nr(c), nr);
  170. cpu_map__snprint(c, buf, sizeof(buf));
  171. TEST_ASSERT_VAL("failed to intersect map: bad result", !strcmp(buf, expected));
  172. perf_cpu_map__put(a);
  173. perf_cpu_map__put(b);
  174. perf_cpu_map__put(c);
  175. return 0;
  176. }
  177. static int test__cpu_map_intersect(struct test_suite *test __maybe_unused,
  178. int subtest __maybe_unused)
  179. {
  180. int ret;
  181. ret = __test__cpu_map_intersect("4,2,1", "4,5,7", 1, "4");
  182. if (ret)
  183. return ret;
  184. ret = __test__cpu_map_intersect("1-8", "6-9", 3, "6-8");
  185. if (ret)
  186. return ret;
  187. ret = __test__cpu_map_intersect("1-8,12-20", "6-9,15", 4, "6-8,15");
  188. if (ret)
  189. return ret;
  190. ret = __test__cpu_map_intersect("4,2,1", "1", 1, "1");
  191. if (ret)
  192. return ret;
  193. ret = __test__cpu_map_intersect("1", "4,2,1", 1, "1");
  194. if (ret)
  195. return ret;
  196. ret = __test__cpu_map_intersect("1", "1", 1, "1");
  197. return ret;
  198. }
  199. static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  200. {
  201. struct perf_cpu_map *any = perf_cpu_map__new_any_cpu();
  202. struct perf_cpu_map *one = perf_cpu_map__new("1");
  203. struct perf_cpu_map *two = perf_cpu_map__new("2");
  204. struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two);
  205. struct perf_cpu_map *pair = perf_cpu_map__new("1-2");
  206. struct perf_cpu_map *tmp;
  207. struct perf_cpu_map **maps[] = {&empty, &any, &one, &two, &pair};
  208. for (size_t i = 0; i < ARRAY_SIZE(maps); i++) {
  209. /* Maps equal themself. */
  210. TEST_ASSERT_VAL("equal", perf_cpu_map__equal(*maps[i], *maps[i]));
  211. for (size_t j = 0; j < ARRAY_SIZE(maps); j++) {
  212. /* Maps dont't equal each other. */
  213. if (i == j)
  214. continue;
  215. TEST_ASSERT_VAL("not equal", !perf_cpu_map__equal(*maps[i], *maps[j]));
  216. }
  217. }
  218. /* Maps equal made maps. */
  219. perf_cpu_map__merge(&two, one);
  220. TEST_ASSERT_VAL("pair", perf_cpu_map__equal(pair, two));
  221. tmp = perf_cpu_map__intersect(pair, one);
  222. TEST_ASSERT_VAL("one", perf_cpu_map__equal(one, tmp));
  223. perf_cpu_map__put(tmp);
  224. for (size_t i = 0; i < ARRAY_SIZE(maps); i++)
  225. perf_cpu_map__put(*maps[i]);
  226. return TEST_OK;
  227. }
  228. static struct test_case tests__cpu_map[] = {
  229. TEST_CASE("Synthesize cpu map", cpu_map_synthesize),
  230. TEST_CASE("Print cpu map", cpu_map_print),
  231. TEST_CASE("Merge cpu map", cpu_map_merge),
  232. TEST_CASE("Intersect cpu map", cpu_map_intersect),
  233. TEST_CASE("Equal cpu map", cpu_map_equal),
  234. { .name = NULL, }
  235. };
  236. struct test_suite suite__cpu_map = {
  237. .desc = "CPU map",
  238. .test_cases = tests__cpu_map,
  239. };