bp_signal_overflow.c 2.7 KB

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