event_groups.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include "linux/perf_event.h"
  6. #include "tests.h"
  7. #include "debug.h"
  8. #include "pmu.h"
  9. #include "pmus.h"
  10. #include "header.h"
  11. #include "../perf-sys.h"
  12. /* hw: cycles,instructions sw: context-switch, uncore: [arch dependent] */
  13. static int types[] = {0, 1, -1};
  14. static unsigned long configs[] = {0, 3, 0};
  15. static unsigned long configs_hw[] = {1};
  16. #define NR_UNCORE_PMUS 5
  17. /* Uncore pmus that support more than 3 counters */
  18. static struct uncore_pmus {
  19. const char *name;
  20. __u64 config;
  21. } uncore_pmus[NR_UNCORE_PMUS] = {
  22. { "amd_l3", 0x0 },
  23. { "amd_df", 0x0 },
  24. { "uncore_imc_0", 0x1 }, /* Intel */
  25. { "core_imc", 0x318 }, /* PowerPC: core_imc/CPM_STCX_FIN/ */
  26. { "hv_24x7", 0x22000000003 }, /* PowerPC: hv_24x7/CPM_STCX_FIN/ */
  27. };
  28. static int event_open(int type, unsigned long config, int group_fd)
  29. {
  30. struct perf_event_attr attr;
  31. memset(&attr, 0, sizeof(struct perf_event_attr));
  32. attr.type = type;
  33. attr.size = sizeof(struct perf_event_attr);
  34. attr.config = config;
  35. /*
  36. * When creating an event group, typically the group leader is
  37. * initialized with disabled set to 1 and any child events are
  38. * initialized with disabled set to 0. Despite disabled being 0,
  39. * the child events will not start until the group leader is
  40. * enabled.
  41. */
  42. attr.disabled = group_fd == -1 ? 1 : 0;
  43. return sys_perf_event_open(&attr, -1, 0, group_fd, 0);
  44. }
  45. static int setup_uncore_event(void)
  46. {
  47. struct perf_pmu *pmu = NULL;
  48. int i, fd;
  49. while ((pmu = perf_pmus__scan(pmu)) != NULL) {
  50. for (i = 0; i < NR_UNCORE_PMUS; i++) {
  51. if (!strcmp(uncore_pmus[i].name, pmu->name)) {
  52. pr_debug("Using %s for uncore pmu event\n", pmu->name);
  53. types[2] = pmu->type;
  54. configs[2] = uncore_pmus[i].config;
  55. /*
  56. * Check if the chosen uncore pmu event can be
  57. * used in the test. For example, incase of accessing
  58. * hv_24x7 pmu counters, partition should have
  59. * additional permissions. If not, event open will
  60. * fail. So check if the event open succeeds
  61. * before proceeding.
  62. */
  63. fd = event_open(types[2], configs[2], -1);
  64. if (fd < 0)
  65. return -1;
  66. close(fd);
  67. return 0;
  68. }
  69. }
  70. }
  71. return -1;
  72. }
  73. static int run_test(int i, int j, int k)
  74. {
  75. int erroneous = ((((1 << i) | (1 << j) | (1 << k)) & 5) == 5);
  76. int group_fd, sibling_fd1, sibling_fd2;
  77. group_fd = event_open(types[i], configs[i], -1);
  78. if (group_fd == -1)
  79. return -1;
  80. sibling_fd1 = event_open(types[j], configs[j], group_fd);
  81. if (sibling_fd1 == -1) {
  82. close(group_fd);
  83. return erroneous ? 0 : -1;
  84. }
  85. /*
  86. * if all three events (leader and two sibling events)
  87. * are hardware events, use instructions as one of the
  88. * sibling event. There is event constraint in powerpc that
  89. * events using same counter cannot be programmed in a group.
  90. * Since PERF_COUNT_HW_INSTRUCTIONS is a generic hardware
  91. * event and present in all platforms, lets use that.
  92. */
  93. if (!i && !j && !k)
  94. sibling_fd2 = event_open(types[k], configs_hw[k], group_fd);
  95. else
  96. sibling_fd2 = event_open(types[k], configs[k], group_fd);
  97. if (sibling_fd2 == -1) {
  98. close(sibling_fd1);
  99. close(group_fd);
  100. return erroneous ? 0 : -1;
  101. }
  102. close(sibling_fd2);
  103. close(sibling_fd1);
  104. close(group_fd);
  105. return erroneous ? -1 : 0;
  106. }
  107. static int test__event_groups(struct test_suite *text __maybe_unused, int subtest __maybe_unused)
  108. {
  109. int i, j, k;
  110. int ret;
  111. int r;
  112. ret = setup_uncore_event();
  113. if (ret || types[2] == -1)
  114. return TEST_SKIP;
  115. ret = TEST_OK;
  116. for (i = 0; i < 3; i++) {
  117. for (j = 0; j < 3; j++) {
  118. for (k = 0; k < 3; k++) {
  119. r = run_test(i, j, k);
  120. if (r)
  121. ret = TEST_FAIL;
  122. /*
  123. * For all three events as HW events, second sibling
  124. * event is picked from configs_hw. So print accordingly
  125. */
  126. if (!i && !j && !k)
  127. pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
  128. types[i], configs[i], types[j], configs[j],
  129. types[k], configs_hw[k], r ? "Fail" : "Pass");
  130. else
  131. pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
  132. types[i], configs[i], types[j], configs[j],
  133. types[k], configs[k], r ? "Fail" : "Pass");
  134. }
  135. }
  136. }
  137. return ret;
  138. }
  139. DEFINE_SUITE("Event groups", event_groups);