intel-cqm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "tests/tests.h"
  2. #include "perf.h"
  3. #include "cloexec.h"
  4. #include "debug.h"
  5. #include "evlist.h"
  6. #include "evsel.h"
  7. #include "arch-tests.h"
  8. #include <sys/mman.h>
  9. #include <string.h>
  10. static pid_t spawn(void)
  11. {
  12. pid_t pid;
  13. pid = fork();
  14. if (pid)
  15. return pid;
  16. while(1)
  17. sleep(5);
  18. return 0;
  19. }
  20. /*
  21. * Create an event group that contains both a sampled hardware
  22. * (cpu-cycles) and software (intel_cqm/llc_occupancy/) event. We then
  23. * wait for the hardware perf counter to overflow and generate a PMI,
  24. * which triggers an event read for both of the events in the group.
  25. *
  26. * Since reading Intel CQM event counters requires sending SMP IPIs, the
  27. * CQM pmu needs to handle the above situation gracefully, and return
  28. * the last read counter value to avoid triggering a WARN_ON_ONCE() in
  29. * smp_call_function_many() caused by sending IPIs from NMI context.
  30. */
  31. int test__intel_cqm_count_nmi_context(int subtest __maybe_unused)
  32. {
  33. struct perf_evlist *evlist = NULL;
  34. struct perf_evsel *evsel = NULL;
  35. struct perf_event_attr pe;
  36. int i, fd[2], flag, ret;
  37. size_t mmap_len;
  38. void *event;
  39. pid_t pid;
  40. int err = TEST_FAIL;
  41. flag = perf_event_open_cloexec_flag();
  42. evlist = perf_evlist__new();
  43. if (!evlist) {
  44. pr_debug("perf_evlist__new failed\n");
  45. return TEST_FAIL;
  46. }
  47. ret = parse_events(evlist, "intel_cqm/llc_occupancy/", NULL);
  48. if (ret) {
  49. pr_debug("parse_events failed, is \"intel_cqm/llc_occupancy/\" available?\n");
  50. err = TEST_SKIP;
  51. goto out;
  52. }
  53. evsel = perf_evlist__first(evlist);
  54. if (!evsel) {
  55. pr_debug("perf_evlist__first failed\n");
  56. goto out;
  57. }
  58. memset(&pe, 0, sizeof(pe));
  59. pe.size = sizeof(pe);
  60. pe.type = PERF_TYPE_HARDWARE;
  61. pe.config = PERF_COUNT_HW_CPU_CYCLES;
  62. pe.read_format = PERF_FORMAT_GROUP;
  63. pe.sample_period = 128;
  64. pe.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_READ;
  65. pid = spawn();
  66. fd[0] = sys_perf_event_open(&pe, pid, -1, -1, flag);
  67. if (fd[0] < 0) {
  68. pr_debug("failed to open event\n");
  69. goto out;
  70. }
  71. memset(&pe, 0, sizeof(pe));
  72. pe.size = sizeof(pe);
  73. pe.type = evsel->attr.type;
  74. pe.config = evsel->attr.config;
  75. fd[1] = sys_perf_event_open(&pe, pid, -1, fd[0], flag);
  76. if (fd[1] < 0) {
  77. pr_debug("failed to open event\n");
  78. goto out;
  79. }
  80. /*
  81. * Pick a power-of-two number of pages + 1 for the meta-data
  82. * page (struct perf_event_mmap_page). See tools/perf/design.txt.
  83. */
  84. mmap_len = page_size * 65;
  85. event = mmap(NULL, mmap_len, PROT_READ, MAP_SHARED, fd[0], 0);
  86. if (event == (void *)(-1)) {
  87. pr_debug("failed to mmap %d\n", errno);
  88. goto out;
  89. }
  90. sleep(1);
  91. err = TEST_OK;
  92. munmap(event, mmap_len);
  93. for (i = 0; i < 2; i++)
  94. close(fd[i]);
  95. kill(pid, SIGKILL);
  96. wait(NULL);
  97. out:
  98. perf_evlist__delete(evlist);
  99. return err;
  100. }