pfm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test support for libpfm4 event encodings.
  4. *
  5. * Copyright 2020 Google LLC.
  6. */
  7. #include "tests.h"
  8. #include "util/debug.h"
  9. #include "util/evlist.h"
  10. #include "util/pfm.h"
  11. #include <linux/kernel.h>
  12. #ifdef HAVE_LIBPFM
  13. static int count_pfm_events(struct perf_evlist *evlist)
  14. {
  15. struct perf_evsel *evsel;
  16. int count = 0;
  17. perf_evlist__for_each_entry(evlist, evsel) {
  18. count++;
  19. }
  20. return count;
  21. }
  22. static int test__pfm_events(struct test_suite *test __maybe_unused,
  23. int subtest __maybe_unused)
  24. {
  25. struct evlist *evlist;
  26. struct option opt;
  27. size_t i;
  28. const struct {
  29. const char *events;
  30. int nr_events;
  31. } table[] = {
  32. {
  33. .events = "",
  34. .nr_events = 0,
  35. },
  36. {
  37. .events = "instructions",
  38. .nr_events = 1,
  39. },
  40. {
  41. .events = "instructions,cycles",
  42. .nr_events = 2,
  43. },
  44. {
  45. .events = "stereolab",
  46. .nr_events = 0,
  47. },
  48. {
  49. .events = "instructions,instructions",
  50. .nr_events = 2,
  51. },
  52. {
  53. .events = "stereolab,instructions",
  54. .nr_events = 0,
  55. },
  56. {
  57. .events = "instructions,stereolab",
  58. .nr_events = 1,
  59. },
  60. };
  61. for (i = 0; i < ARRAY_SIZE(table); i++) {
  62. evlist = evlist__new();
  63. if (evlist == NULL)
  64. return -ENOMEM;
  65. opt.value = evlist;
  66. parse_libpfm_events_option(&opt,
  67. table[i].events,
  68. 0);
  69. TEST_ASSERT_EQUAL(table[i].events,
  70. count_pfm_events(&evlist->core),
  71. table[i].nr_events);
  72. TEST_ASSERT_EQUAL(table[i].events,
  73. evlist__nr_groups(evlist),
  74. 0);
  75. evlist__delete(evlist);
  76. }
  77. return 0;
  78. }
  79. static int test__pfm_group(struct test_suite *test __maybe_unused,
  80. int subtest __maybe_unused)
  81. {
  82. struct evlist *evlist;
  83. struct option opt;
  84. size_t i;
  85. const struct {
  86. const char *events;
  87. int nr_events;
  88. int nr_groups;
  89. } table[] = {
  90. {
  91. .events = "{},",
  92. .nr_events = 0,
  93. .nr_groups = 0,
  94. },
  95. {
  96. .events = "{instructions}",
  97. .nr_events = 1,
  98. .nr_groups = 0,
  99. },
  100. {
  101. .events = "{instructions},{}",
  102. .nr_events = 1,
  103. .nr_groups = 0,
  104. },
  105. {
  106. .events = "{},{instructions}",
  107. .nr_events = 1,
  108. .nr_groups = 0,
  109. },
  110. {
  111. .events = "{instructions},{instructions}",
  112. .nr_events = 2,
  113. .nr_groups = 0,
  114. },
  115. {
  116. .events = "{instructions,cycles},{instructions,cycles}",
  117. .nr_events = 4,
  118. .nr_groups = 2,
  119. },
  120. {
  121. .events = "{stereolab}",
  122. .nr_events = 0,
  123. .nr_groups = 0,
  124. },
  125. {
  126. .events =
  127. "{instructions,cycles},{instructions,stereolab}",
  128. .nr_events = 3,
  129. .nr_groups = 1,
  130. },
  131. {
  132. .events = "instructions}",
  133. .nr_events = 1,
  134. .nr_groups = 0,
  135. },
  136. {
  137. .events = "{{instructions}}",
  138. .nr_events = 0,
  139. .nr_groups = 0,
  140. },
  141. };
  142. for (i = 0; i < ARRAY_SIZE(table); i++) {
  143. evlist = evlist__new();
  144. if (evlist == NULL)
  145. return -ENOMEM;
  146. opt.value = evlist;
  147. parse_libpfm_events_option(&opt,
  148. table[i].events,
  149. 0);
  150. TEST_ASSERT_EQUAL(table[i].events,
  151. count_pfm_events(&evlist->core),
  152. table[i].nr_events);
  153. TEST_ASSERT_EQUAL(table[i].events,
  154. evlist__nr_groups(evlist),
  155. table[i].nr_groups);
  156. evlist__delete(evlist);
  157. }
  158. return 0;
  159. }
  160. #else
  161. static int test__pfm_events(struct test_suite *test __maybe_unused,
  162. int subtest __maybe_unused)
  163. {
  164. return TEST_SKIP;
  165. }
  166. static int test__pfm_group(struct test_suite *test __maybe_unused,
  167. int subtest __maybe_unused)
  168. {
  169. return TEST_SKIP;
  170. }
  171. #endif
  172. static struct test_case pfm_tests[] = {
  173. TEST_CASE_REASON("test of individual --pfm-events", pfm_events, "not compiled in"),
  174. TEST_CASE_REASON("test groups of --pfm-events", pfm_group, "not compiled in"),
  175. { .name = NULL, }
  176. };
  177. struct test_suite suite__pfm = {
  178. .desc = "Test libpfm4 support",
  179. .test_cases = pfm_tests,
  180. };