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