linux/tools/perf/tests/bp_signal_overflow.c
<<
>>
Prefs
   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/*
   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
  12#include <stdlib.h>
  13#include <stdio.h>
  14#include <unistd.h>
  15#include <string.h>
  16#include <sys/ioctl.h>
  17#include <time.h>
  18#include <fcntl.h>
  19#include <signal.h>
  20#include <sys/mman.h>
  21#include <linux/compiler.h>
  22#include <linux/hw_breakpoint.h>
  23
  24#include "tests.h"
  25#include "debug.h"
  26#include "perf.h"
  27#include "cloexec.h"
  28
  29static int overflows;
  30
  31static noinline int test_function(void)
  32{
  33        return time(NULL);
  34}
  35
  36static void sig_handler(int signum __maybe_unused,
  37                        siginfo_t *oh __maybe_unused,
  38                        void *uc __maybe_unused)
  39{
  40        overflows++;
  41}
  42
  43static long long bp_count(int fd)
  44{
  45        long long count;
  46        int ret;
  47
  48        ret = read(fd, &count, sizeof(long long));
  49        if (ret != sizeof(long long)) {
  50                pr_debug("failed to read: %d\n", ret);
  51                return TEST_FAIL;
  52        }
  53
  54        return count;
  55}
  56
  57#define EXECUTIONS 10000
  58#define THRESHOLD  100
  59
  60int test__bp_signal_overflow(struct test *test __maybe_unused, int subtest __maybe_unused)
  61{
  62        struct perf_event_attr pe;
  63        struct sigaction sa;
  64        long long count;
  65        int fd, i, fails = 0;
  66
  67        /* setup SIGIO signal handler */
  68        memset(&sa, 0, sizeof(struct sigaction));
  69        sa.sa_sigaction = (void *) sig_handler;
  70        sa.sa_flags = SA_SIGINFO;
  71
  72        if (sigaction(SIGIO, &sa, NULL) < 0) {
  73                pr_debug("failed setting up signal handler\n");
  74                return TEST_FAIL;
  75        }
  76
  77        memset(&pe, 0, sizeof(struct perf_event_attr));
  78        pe.type = PERF_TYPE_BREAKPOINT;
  79        pe.size = sizeof(struct perf_event_attr);
  80
  81        pe.config = 0;
  82        pe.bp_type = HW_BREAKPOINT_X;
  83        pe.bp_addr = (unsigned long) test_function;
  84        pe.bp_len = sizeof(long);
  85
  86        pe.sample_period = THRESHOLD;
  87        pe.sample_type = PERF_SAMPLE_IP;
  88        pe.wakeup_events = 1;
  89
  90        pe.disabled = 1;
  91        pe.exclude_kernel = 1;
  92        pe.exclude_hv = 1;
  93
  94        fd = sys_perf_event_open(&pe, 0, -1, -1,
  95                                 perf_event_open_cloexec_flag());
  96        if (fd < 0) {
  97                pr_debug("failed opening event %llx\n", pe.config);
  98                return TEST_FAIL;
  99        }
 100
 101        fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
 102        fcntl(fd, F_SETSIG, SIGIO);
 103        fcntl(fd, F_SETOWN, getpid());
 104
 105        ioctl(fd, PERF_EVENT_IOC_RESET, 0);
 106        ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
 107
 108        for (i = 0; i < EXECUTIONS; i++)
 109                test_function();
 110
 111        ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
 112
 113        count = bp_count(fd);
 114
 115        close(fd);
 116
 117        pr_debug("count %lld, overflow %d\n",
 118                 count, overflows);
 119
 120        if (count != EXECUTIONS) {
 121                pr_debug("\tWrong number of executions %lld != %d\n",
 122                count, EXECUTIONS);
 123                fails++;
 124        }
 125
 126        if (overflows != EXECUTIONS / THRESHOLD) {
 127                pr_debug("\tWrong number of overflows %d != %d\n",
 128                overflows, EXECUTIONS / THRESHOLD);
 129                fails++;
 130        }
 131
 132        return fails ? TEST_FAIL : TEST_OK;
 133}
 134