pfm.c 3.6 KB

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