tool_pmu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. #include "debug.h"
  3. #include "evlist.h"
  4. #include "parse-events.h"
  5. #include "tests.h"
  6. #include "tool_pmu.h"
  7. static int do_test(enum tool_pmu_event ev, bool with_pmu)
  8. {
  9. struct evlist *evlist = evlist__new();
  10. struct evsel *evsel;
  11. struct parse_events_error err;
  12. int ret;
  13. char str[128];
  14. bool found = false;
  15. if (!evlist) {
  16. pr_err("evlist allocation failed\n");
  17. return TEST_FAIL;
  18. }
  19. if (with_pmu)
  20. snprintf(str, sizeof(str), "tool/%s/", tool_pmu__event_to_str(ev));
  21. else
  22. snprintf(str, sizeof(str), "%s", tool_pmu__event_to_str(ev));
  23. parse_events_error__init(&err);
  24. ret = parse_events(evlist, str, &err);
  25. if (ret) {
  26. if (!tool_pmu__event_to_str(ev)) {
  27. ret = TEST_OK;
  28. goto out;
  29. }
  30. pr_debug("FAILED %s:%d failed to parse event '%s', err %d\n",
  31. __FILE__, __LINE__, str, ret);
  32. parse_events_error__print(&err, str);
  33. ret = TEST_FAIL;
  34. goto out;
  35. }
  36. ret = TEST_OK;
  37. if (with_pmu ? (evlist->core.nr_entries != 1) : (evlist->core.nr_entries < 1)) {
  38. pr_debug("FAILED %s:%d Unexpected number of events for '%s' of %d\n",
  39. __FILE__, __LINE__, str, evlist->core.nr_entries);
  40. ret = TEST_FAIL;
  41. goto out;
  42. }
  43. evlist__for_each_entry(evlist, evsel) {
  44. if (perf_pmu__is_tool(evsel->pmu)) {
  45. if (evsel->core.attr.config != ev) {
  46. pr_debug("FAILED %s:%d Unexpected config for '%s', %lld != %d\n",
  47. __FILE__, __LINE__, str, evsel->core.attr.config, ev);
  48. ret = TEST_FAIL;
  49. goto out;
  50. }
  51. found = true;
  52. }
  53. }
  54. if (!found && tool_pmu__event_to_str(ev)) {
  55. pr_debug("FAILED %s:%d Didn't find tool event '%s' in parsed evsels\n",
  56. __FILE__, __LINE__, str);
  57. ret = TEST_FAIL;
  58. }
  59. out:
  60. parse_events_error__exit(&err);
  61. evlist__delete(evlist);
  62. return ret;
  63. }
  64. static int test__tool_pmu_without_pmu(struct test_suite *test __maybe_unused,
  65. int subtest __maybe_unused)
  66. {
  67. int i;
  68. tool_pmu__for_each_event(i) {
  69. int ret = do_test(i, /*with_pmu=*/false);
  70. if (ret != TEST_OK)
  71. return ret;
  72. }
  73. return TEST_OK;
  74. }
  75. static int test__tool_pmu_with_pmu(struct test_suite *test __maybe_unused,
  76. int subtest __maybe_unused)
  77. {
  78. int i;
  79. tool_pmu__for_each_event(i) {
  80. int ret = do_test(i, /*with_pmu=*/true);
  81. if (ret != TEST_OK)
  82. return ret;
  83. }
  84. return TEST_OK;
  85. }
  86. static struct test_case tests__tool_pmu[] = {
  87. TEST_CASE("Parsing without PMU name", tool_pmu_without_pmu),
  88. TEST_CASE("Parsing with PMU name", tool_pmu_with_pmu),
  89. { .name = NULL, }
  90. };
  91. struct test_suite suite__tool_pmu = {
  92. .desc = "Tool PMU",
  93. .test_cases = tests__tool_pmu,
  94. };