event_groups.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, sw: context-switch, uncore: [arch dependent] */
  13. static int types[] = {0, 1, -1};
  14. static unsigned long configs[] = {0, 3, 0};
  15. #define NR_UNCORE_PMUS 5
  16. /* Uncore pmus that support more than 3 counters */
  17. static struct uncore_pmus {
  18. const char *name;
  19. __u64 config;
  20. } uncore_pmus[NR_UNCORE_PMUS] = {
  21. { "amd_l3", 0x0 },
  22. { "amd_df", 0x0 },
  23. { "uncore_imc_0", 0x1 }, /* Intel */
  24. { "core_imc", 0x318 }, /* PowerPC: core_imc/CPM_STCX_FIN/ */
  25. { "hv_24x7", 0x22000000003 }, /* PowerPC: hv_24x7/CPM_STCX_FIN/ */
  26. };
  27. static int event_open(int type, unsigned long config, int group_fd)
  28. {
  29. struct perf_event_attr attr;
  30. memset(&attr, 0, sizeof(struct perf_event_attr));
  31. attr.type = type;
  32. attr.size = sizeof(struct perf_event_attr);
  33. attr.config = config;
  34. /*
  35. * When creating an event group, typically the group leader is
  36. * initialized with disabled set to 1 and any child events are
  37. * initialized with disabled set to 0. Despite disabled being 0,
  38. * the child events will not start until the group leader is
  39. * enabled.
  40. */
  41. attr.disabled = group_fd == -1 ? 1 : 0;
  42. return sys_perf_event_open(&attr, -1, 0, group_fd, 0);
  43. }
  44. static int setup_uncore_event(void)
  45. {
  46. struct perf_pmu *pmu = NULL;
  47. int i, fd;
  48. while ((pmu = perf_pmus__scan(pmu)) != NULL) {
  49. for (i = 0; i < NR_UNCORE_PMUS; i++) {
  50. if (!strcmp(uncore_pmus[i].name, pmu->name)) {
  51. pr_debug("Using %s for uncore pmu event\n", pmu->name);
  52. types[2] = pmu->type;
  53. configs[2] = uncore_pmus[i].config;
  54. /*
  55. * Check if the chosen uncore pmu event can be
  56. * used in the test. For example, incase of accessing
  57. * hv_24x7 pmu counters, partition should have
  58. * additional permissions. If not, event open will
  59. * fail. So check if the event open succeeds
  60. * before proceeding.
  61. */
  62. fd = event_open(types[2], configs[2], -1);
  63. if (fd < 0)
  64. return -1;
  65. close(fd);
  66. return 0;
  67. }
  68. }
  69. }
  70. return -1;
  71. }
  72. static int run_test(int i, int j, int k)
  73. {
  74. int erroneous = ((((1 << i) | (1 << j) | (1 << k)) & 5) == 5);
  75. int group_fd, sibling_fd1, sibling_fd2;
  76. group_fd = event_open(types[i], configs[i], -1);
  77. if (group_fd == -1)
  78. return -1;
  79. sibling_fd1 = event_open(types[j], configs[j], group_fd);
  80. if (sibling_fd1 == -1) {
  81. close(group_fd);
  82. return erroneous ? 0 : -1;
  83. }
  84. sibling_fd2 = event_open(types[k], configs[k], group_fd);
  85. if (sibling_fd2 == -1) {
  86. close(sibling_fd1);
  87. close(group_fd);
  88. return erroneous ? 0 : -1;
  89. }
  90. close(sibling_fd2);
  91. close(sibling_fd1);
  92. close(group_fd);
  93. return erroneous ? -1 : 0;
  94. }
  95. static int test__event_groups(struct test_suite *text __maybe_unused, int subtest __maybe_unused)
  96. {
  97. int i, j, k;
  98. int ret;
  99. int r;
  100. ret = setup_uncore_event();
  101. if (ret || types[2] == -1)
  102. return TEST_SKIP;
  103. ret = TEST_OK;
  104. for (i = 0; i < 3; i++) {
  105. for (j = 0; j < 3; j++) {
  106. for (k = 0; k < 3; k++) {
  107. r = run_test(i, j, k);
  108. if (r)
  109. ret = TEST_FAIL;
  110. pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
  111. types[i], configs[i], types[j], configs[j],
  112. types[k], configs[k], r ? "Fail" : "Pass");
  113. }
  114. }
  115. }
  116. return ret;
  117. }
  118. DEFINE_SUITE("Event groups", event_groups);