bp_signal_overflow.c 2.8 KB

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