bp_signal_overflow.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Originally done by Vince Weaver <vincent.weaver@maine.edu> for
  3. * perf_event_tests (git://github.com/deater/perf_event_tests)
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <sys/ioctl.h>
  10. #include <time.h>
  11. #include <fcntl.h>
  12. #include <signal.h>
  13. #include <sys/mman.h>
  14. #include <linux/compiler.h>
  15. #include <linux/hw_breakpoint.h>
  16. #include "tests.h"
  17. #include "debug.h"
  18. #include "perf.h"
  19. static int overflows;
  20. __attribute__ ((noinline))
  21. static int test_function(void)
  22. {
  23. return time(NULL);
  24. }
  25. static void sig_handler(int signum __maybe_unused,
  26. siginfo_t *oh __maybe_unused,
  27. void *uc __maybe_unused)
  28. {
  29. overflows++;
  30. }
  31. static long long bp_count(int fd)
  32. {
  33. long long count;
  34. int ret;
  35. ret = read(fd, &count, sizeof(long long));
  36. if (ret != sizeof(long long)) {
  37. pr_debug("failed to read: %d\n", ret);
  38. return TEST_FAIL;
  39. }
  40. return count;
  41. }
  42. #define EXECUTIONS 10000
  43. #define THRESHOLD 100
  44. int test__bp_signal_overflow(void)
  45. {
  46. struct perf_event_attr pe;
  47. struct sigaction sa;
  48. long long count;
  49. int fd, i, fails = 0;
  50. /* setup SIGIO signal handler */
  51. memset(&sa, 0, sizeof(struct sigaction));
  52. sa.sa_sigaction = (void *) sig_handler;
  53. sa.sa_flags = SA_SIGINFO;
  54. if (sigaction(SIGIO, &sa, NULL) < 0) {
  55. pr_debug("failed setting up signal handler\n");
  56. return TEST_FAIL;
  57. }
  58. memset(&pe, 0, sizeof(struct perf_event_attr));
  59. pe.type = PERF_TYPE_BREAKPOINT;
  60. pe.size = sizeof(struct perf_event_attr);
  61. pe.config = 0;
  62. pe.bp_type = HW_BREAKPOINT_X;
  63. pe.bp_addr = (unsigned long) test_function;
  64. pe.bp_len = sizeof(long);
  65. pe.sample_period = THRESHOLD;
  66. pe.sample_type = PERF_SAMPLE_IP;
  67. pe.wakeup_events = 1;
  68. pe.disabled = 1;
  69. pe.exclude_kernel = 1;
  70. pe.exclude_hv = 1;
  71. fd = sys_perf_event_open(&pe, 0, -1, -1, 0);
  72. if (fd < 0) {
  73. pr_debug("failed opening event %llx\n", pe.config);
  74. return TEST_FAIL;
  75. }
  76. fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
  77. fcntl(fd, F_SETSIG, SIGIO);
  78. fcntl(fd, F_SETOWN, getpid());
  79. ioctl(fd, PERF_EVENT_IOC_RESET, 0);
  80. ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
  81. for (i = 0; i < EXECUTIONS; i++)
  82. test_function();
  83. ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
  84. count = bp_count(fd);
  85. close(fd);
  86. pr_debug("count %lld, overflow %d\n",
  87. count, overflows);
  88. if (count != EXECUTIONS) {
  89. pr_debug("\tWrong number of executions %lld != %d\n",
  90. count, EXECUTIONS);
  91. fails++;
  92. }
  93. if (overflows != EXECUTIONS / THRESHOLD) {
  94. pr_debug("\tWrong number of overflows %d != %d\n",
  95. overflows, EXECUTIONS / THRESHOLD);
  96. fails++;
  97. }
  98. return fails ? TEST_FAIL : TEST_OK;
  99. }