evsel-roundtrip-name.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "evlist.h"
  3. #include "evsel.h"
  4. #include "parse-events.h"
  5. #include "tests.h"
  6. #include "debug.h"
  7. #include <errno.h>
  8. #include <linux/kernel.h>
  9. static int perf_evsel__roundtrip_cache_name_test(void)
  10. {
  11. char name[128];
  12. int type, op, err = 0, ret = 0, i, idx;
  13. struct perf_evsel *evsel;
  14. struct perf_evlist *evlist = perf_evlist__new();
  15. if (evlist == NULL)
  16. return -ENOMEM;
  17. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  18. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  19. /* skip invalid cache type */
  20. if (!perf_evsel__is_cache_op_valid(type, op))
  21. continue;
  22. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  23. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  24. name, sizeof(name));
  25. err = parse_events(evlist, name, NULL);
  26. if (err)
  27. ret = err;
  28. }
  29. }
  30. }
  31. idx = 0;
  32. evsel = perf_evlist__first(evlist);
  33. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  34. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  35. /* skip invalid cache type */
  36. if (!perf_evsel__is_cache_op_valid(type, op))
  37. continue;
  38. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  39. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  40. name, sizeof(name));
  41. if (evsel->idx != idx)
  42. continue;
  43. ++idx;
  44. if (strcmp(perf_evsel__name(evsel), name)) {
  45. pr_debug("%s != %s\n", perf_evsel__name(evsel), name);
  46. ret = -1;
  47. }
  48. evsel = perf_evsel__next(evsel);
  49. }
  50. }
  51. }
  52. perf_evlist__delete(evlist);
  53. return ret;
  54. }
  55. static int __perf_evsel__name_array_test(const char *names[], int nr_names)
  56. {
  57. int i, err;
  58. struct perf_evsel *evsel;
  59. struct perf_evlist *evlist = perf_evlist__new();
  60. if (evlist == NULL)
  61. return -ENOMEM;
  62. for (i = 0; i < nr_names; ++i) {
  63. err = parse_events(evlist, names[i], NULL);
  64. if (err) {
  65. pr_debug("failed to parse event '%s', err %d\n",
  66. names[i], err);
  67. goto out_delete_evlist;
  68. }
  69. }
  70. err = 0;
  71. evlist__for_each_entry(evlist, evsel) {
  72. if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) {
  73. --err;
  74. pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]);
  75. }
  76. }
  77. out_delete_evlist:
  78. perf_evlist__delete(evlist);
  79. return err;
  80. }
  81. #define perf_evsel__name_array_test(names) \
  82. __perf_evsel__name_array_test(names, ARRAY_SIZE(names))
  83. int test__perf_evsel__roundtrip_name_test(struct test *test __maybe_unused, int subtest __maybe_unused)
  84. {
  85. int err = 0, ret = 0;
  86. err = perf_evsel__name_array_test(perf_evsel__hw_names);
  87. if (err)
  88. ret = err;
  89. err = __perf_evsel__name_array_test(perf_evsel__sw_names,
  90. PERF_COUNT_SW_DUMMY + 1);
  91. if (err)
  92. ret = err;
  93. err = perf_evsel__roundtrip_cache_name_test();
  94. if (err)
  95. ret = err;
  96. return ret;
  97. }