pfm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 test__pfm_events(void);
  14. static int test__pfm_group(void);
  15. #endif
  16. static const struct {
  17. int (*func)(void);
  18. const char *desc;
  19. } pfm_testcase_table[] = {
  20. #ifdef HAVE_LIBPFM
  21. {
  22. .func = test__pfm_events,
  23. .desc = "test of individual --pfm-events",
  24. },
  25. {
  26. .func = test__pfm_group,
  27. .desc = "test groups of --pfm-events",
  28. },
  29. #endif
  30. };
  31. #ifdef HAVE_LIBPFM
  32. static int count_pfm_events(struct perf_evlist *evlist)
  33. {
  34. struct perf_evsel *evsel;
  35. int count = 0;
  36. perf_evlist__for_each_entry(evlist, evsel) {
  37. count++;
  38. }
  39. return count;
  40. }
  41. static int test__pfm_events(void)
  42. {
  43. struct evlist *evlist;
  44. struct option opt;
  45. size_t i;
  46. const struct {
  47. const char *events;
  48. int nr_events;
  49. } table[] = {
  50. {
  51. .events = "",
  52. .nr_events = 0,
  53. },
  54. {
  55. .events = "instructions",
  56. .nr_events = 1,
  57. },
  58. {
  59. .events = "instructions,cycles",
  60. .nr_events = 2,
  61. },
  62. {
  63. .events = "stereolab",
  64. .nr_events = 0,
  65. },
  66. {
  67. .events = "instructions,instructions",
  68. .nr_events = 2,
  69. },
  70. {
  71. .events = "stereolab,instructions",
  72. .nr_events = 0,
  73. },
  74. {
  75. .events = "instructions,stereolab",
  76. .nr_events = 1,
  77. },
  78. };
  79. for (i = 0; i < ARRAY_SIZE(table); i++) {
  80. evlist = evlist__new();
  81. if (evlist == NULL)
  82. return -ENOMEM;
  83. opt.value = evlist;
  84. parse_libpfm_events_option(&opt,
  85. table[i].events,
  86. 0);
  87. TEST_ASSERT_EQUAL(table[i].events,
  88. count_pfm_events(&evlist->core),
  89. table[i].nr_events);
  90. TEST_ASSERT_EQUAL(table[i].events,
  91. evlist->nr_groups,
  92. 0);
  93. evlist__delete(evlist);
  94. }
  95. return 0;
  96. }
  97. static int test__pfm_group(void)
  98. {
  99. struct evlist *evlist;
  100. struct option opt;
  101. size_t i;
  102. const struct {
  103. const char *events;
  104. int nr_events;
  105. int nr_groups;
  106. } table[] = {
  107. {
  108. .events = "{},",
  109. .nr_events = 0,
  110. .nr_groups = 0,
  111. },
  112. {
  113. .events = "{instructions}",
  114. .nr_events = 1,
  115. .nr_groups = 1,
  116. },
  117. {
  118. .events = "{instructions},{}",
  119. .nr_events = 1,
  120. .nr_groups = 1,
  121. },
  122. {
  123. .events = "{},{instructions}",
  124. .nr_events = 0,
  125. .nr_groups = 0,
  126. },
  127. {
  128. .events = "{instructions},{instructions}",
  129. .nr_events = 2,
  130. .nr_groups = 2,
  131. },
  132. {
  133. .events = "{instructions,cycles},{instructions,cycles}",
  134. .nr_events = 4,
  135. .nr_groups = 2,
  136. },
  137. {
  138. .events = "{stereolab}",
  139. .nr_events = 0,
  140. .nr_groups = 0,
  141. },
  142. {
  143. .events =
  144. "{instructions,cycles},{instructions,stereolab}",
  145. .nr_events = 3,
  146. .nr_groups = 1,
  147. },
  148. };
  149. for (i = 0; i < ARRAY_SIZE(table); i++) {
  150. evlist = evlist__new();
  151. if (evlist == NULL)
  152. return -ENOMEM;
  153. opt.value = evlist;
  154. parse_libpfm_events_option(&opt,
  155. table[i].events,
  156. 0);
  157. TEST_ASSERT_EQUAL(table[i].events,
  158. count_pfm_events(&evlist->core),
  159. table[i].nr_events);
  160. TEST_ASSERT_EQUAL(table[i].events,
  161. evlist->nr_groups,
  162. table[i].nr_groups);
  163. evlist__delete(evlist);
  164. }
  165. return 0;
  166. }
  167. #endif
  168. const char *test__pfm_subtest_get_desc(int i)
  169. {
  170. if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
  171. return NULL;
  172. return pfm_testcase_table[i].desc;
  173. }
  174. int test__pfm_subtest_get_nr(void)
  175. {
  176. return (int)ARRAY_SIZE(pfm_testcase_table);
  177. }
  178. int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
  179. {
  180. #ifdef HAVE_LIBPFM
  181. if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
  182. return TEST_FAIL;
  183. return pfm_testcase_table[i].func();
  184. #else
  185. return TEST_SKIP;
  186. #endif
  187. }