linux/tools/perf/arch/x86/tests/rdpmc.c
<<
>>
Prefs
   1#include <errno.h>
   2#include <unistd.h>
   3#include <stdlib.h>
   4#include <signal.h>
   5#include <sys/mman.h>
   6#include <sys/types.h>
   7#include <sys/wait.h>
   8#include <linux/types.h>
   9#include "perf.h"
  10#include "debug.h"
  11#include "tests/tests.h"
  12#include "cloexec.h"
  13#include "util.h"
  14#include "arch-tests.h"
  15
  16static u64 rdpmc(unsigned int counter)
  17{
  18        unsigned int low, high;
  19
  20        asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
  21
  22        return low | ((u64)high) << 32;
  23}
  24
  25static u64 rdtsc(void)
  26{
  27        unsigned int low, high;
  28
  29        asm volatile("rdtsc" : "=a" (low), "=d" (high));
  30
  31        return low | ((u64)high) << 32;
  32}
  33
  34static u64 mmap_read_self(void *addr)
  35{
  36        struct perf_event_mmap_page *pc = addr;
  37        u32 seq, idx, time_mult = 0, time_shift = 0;
  38        u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
  39
  40        do {
  41                seq = pc->lock;
  42                barrier();
  43
  44                enabled = pc->time_enabled;
  45                running = pc->time_running;
  46
  47                if (enabled != running) {
  48                        cyc = rdtsc();
  49                        time_mult = pc->time_mult;
  50                        time_shift = pc->time_shift;
  51                        time_offset = pc->time_offset;
  52                }
  53
  54                idx = pc->index;
  55                count = pc->offset;
  56                if (idx)
  57                        count += rdpmc(idx - 1);
  58
  59                barrier();
  60        } while (pc->lock != seq);
  61
  62        if (enabled != running) {
  63                u64 quot, rem;
  64
  65                quot = (cyc >> time_shift);
  66                rem = cyc & (((u64)1 << time_shift) - 1);
  67                delta = time_offset + quot * time_mult +
  68                        ((rem * time_mult) >> time_shift);
  69
  70                enabled += delta;
  71                if (idx)
  72                        running += delta;
  73
  74                quot = count / running;
  75                rem = count % running;
  76                count = quot * enabled + (rem * enabled) / running;
  77        }
  78
  79        return count;
  80}
  81
  82/*
  83 * If the RDPMC instruction faults then signal this back to the test parent task:
  84 */
  85static void segfault_handler(int sig __maybe_unused,
  86                             siginfo_t *info __maybe_unused,
  87                             void *uc __maybe_unused)
  88{
  89        exit(-1);
  90}
  91
  92static int __test__rdpmc(void)
  93{
  94        volatile int tmp = 0;
  95        u64 i, loops = 1000;
  96        int n;
  97        int fd;
  98        void *addr;
  99        struct perf_event_attr attr = {
 100                .type = PERF_TYPE_HARDWARE,
 101                .config = PERF_COUNT_HW_INSTRUCTIONS,
 102                .exclude_kernel = 1,
 103        };
 104        u64 delta_sum = 0;
 105        struct sigaction sa;
 106        char sbuf[STRERR_BUFSIZE];
 107
 108        sigfillset(&sa.sa_mask);
 109        sa.sa_sigaction = segfault_handler;
 110        sa.sa_flags = 0;
 111        sigaction(SIGSEGV, &sa, NULL);
 112
 113        fd = sys_perf_event_open(&attr, 0, -1, -1,
 114                                 perf_event_open_cloexec_flag());
 115        if (fd < 0) {
 116                pr_err("Error: sys_perf_event_open() syscall returned "
 117                       "with %d (%s)\n", fd,
 118                       str_error_r(errno, sbuf, sizeof(sbuf)));
 119                return -1;
 120        }
 121
 122        addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
 123        if (addr == (void *)(-1)) {
 124                pr_err("Error: mmap() syscall returned with (%s)\n",
 125                       str_error_r(errno, sbuf, sizeof(sbuf)));
 126                goto out_close;
 127        }
 128
 129        for (n = 0; n < 6; n++) {
 130                u64 stamp, now, delta;
 131
 132                stamp = mmap_read_self(addr);
 133
 134                for (i = 0; i < loops; i++)
 135                        tmp++;
 136
 137                now = mmap_read_self(addr);
 138                loops *= 10;
 139
 140                delta = now - stamp;
 141                pr_debug("%14d: %14Lu\n", n, (long long)delta);
 142
 143                delta_sum += delta;
 144        }
 145
 146        munmap(addr, page_size);
 147        pr_debug("   ");
 148out_close:
 149        close(fd);
 150
 151        if (!delta_sum)
 152                return -1;
 153
 154        return 0;
 155}
 156
 157int test__rdpmc(int subtest __maybe_unused)
 158{
 159        int status = 0;
 160        int wret = 0;
 161        int ret;
 162        int pid;
 163
 164        pid = fork();
 165        if (pid < 0)
 166                return -1;
 167
 168        if (!pid) {
 169                ret = __test__rdpmc();
 170
 171                exit(ret);
 172        }
 173
 174        wret = waitpid(pid, &status, 0);
 175        if (wret < 0 || status)
 176                return -1;
 177
 178        return 0;
 179}
 180