bp_signal_overflow.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "cloexec.h"
  25. static int overflows;
  26. __attribute__ ((noinline))
  27. static int test_function(void)
  28. {
  29. return time(NULL);
  30. }
  31. static void sig_handler(int signum __maybe_unused,
  32. siginfo_t *oh __maybe_unused,
  33. void *uc __maybe_unused)
  34. {
  35. overflows++;
  36. }
  37. static long long bp_count(int fd)
  38. {
  39. long long count;
  40. int ret;
  41. ret = read(fd, &count, sizeof(long long));
  42. if (ret != sizeof(long long)) {
  43. pr_debug("failed to read: %d\n", ret);
  44. return TEST_FAIL;
  45. }
  46. return count;
  47. }
  48. #define EXECUTIONS 10000
  49. #define THRESHOLD 100
  50. int test__bp_signal_overflow(int subtest __maybe_unused)
  51. {
  52. struct perf_event_attr pe;
  53. struct sigaction sa;
  54. long long count;
  55. int fd, i, fails = 0;
  56. /* setup SIGIO signal handler */
  57. memset(&sa, 0, sizeof(struct sigaction));
  58. sa.sa_sigaction = (void *) sig_handler;
  59. sa.sa_flags = SA_SIGINFO;
  60. if (sigaction(SIGIO, &sa, NULL) < 0) {
  61. pr_debug("failed setting up signal handler\n");
  62. return TEST_FAIL;
  63. }
  64. memset(&pe, 0, sizeof(struct perf_event_attr));
  65. pe.type = PERF_TYPE_BREAKPOINT;
  66. pe.size = sizeof(struct perf_event_attr);
  67. pe.config = 0;
  68. pe.bp_type = HW_BREAKPOINT_X;
  69. pe.bp_addr = (unsigned long) test_function;
  70. pe.bp_len = sizeof(long);
  71. pe.sample_period = THRESHOLD;
  72. pe.sample_type = PERF_SAMPLE_IP;
  73. pe.wakeup_events = 1;
  74. pe.disabled = 1;
  75. pe.exclude_kernel = 1;
  76. pe.exclude_hv = 1;
  77. fd = sys_perf_event_open(&pe, 0, -1, -1,
  78. perf_event_open_cloexec_flag());
  79. if (fd < 0) {
  80. pr_debug("failed opening event %llx\n", pe.config);
  81. return TEST_FAIL;
  82. }
  83. fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
  84. fcntl(fd, F_SETSIG, SIGIO);
  85. fcntl(fd, F_SETOWN, getpid());
  86. ioctl(fd, PERF_EVENT_IOC_RESET, 0);
  87. ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
  88. for (i = 0; i < EXECUTIONS; i++)
  89. test_function();
  90. ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
  91. count = bp_count(fd);
  92. close(fd);
  93. pr_debug("count %lld, overflow %d\n",
  94. count, overflows);
  95. if (count != EXECUTIONS) {
  96. pr_debug("\tWrong number of executions %lld != %d\n",
  97. count, EXECUTIONS);
  98. fails++;
  99. }
  100. if (overflows != EXECUTIONS / THRESHOLD) {
  101. pr_debug("\tWrong number of overflows %d != %d\n",
  102. overflows, EXECUTIONS / THRESHOLD);
  103. fails++;
  104. }
  105. return fails ? TEST_FAIL : TEST_OK;
  106. }