|
| 1 | +/* |
| 2 | + * Microbench testies for MLP and memory latency in CXLMS |
| 3 | + * |
| 4 | + * By: Andrew Quinn |
| 5 | + * Yiwei Yang |
| 6 | + * |
| 7 | + * Copyright 2023 Regents of the Univeristy of California |
| 8 | + * UC Santa Cruz Sluglab. |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +#include <errno.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <assert.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include <cpuid.h> |
| 19 | +#include <pthread.h> |
| 20 | +#include <stdlib.h> |
| 21 | + |
| 22 | +#include <sys/mman.h> |
| 23 | + |
| 24 | + |
| 25 | +#define STR_HELPER(x) #x |
| 26 | +#define STR(x) STR_HELPER(x) |
| 27 | + |
| 28 | +#define MOVE_SIZE 128 |
| 29 | +#define MAP_SIZE (long)(1024 * 1024 * 1024) |
| 30 | +#define CACHELINE_SIZE 64 |
| 31 | + |
| 32 | +#ifndef FENCE_COUNT |
| 33 | +#define FENCE_COUNT 8 |
| 34 | +#endif |
| 35 | + |
| 36 | +#define FENCE_BOUND (FENCE_COUNT * MOVE_SIZE) |
| 37 | + |
| 38 | +// we need to jump in MOVE_SIZE increments otherwise segfault! |
| 39 | + |
| 40 | +#define BODY(start) \ |
| 41 | + "xor %%r8, %%r8 \n" \ |
| 42 | + "pxor %%xmm1, %%xmm1 \n" \ |
| 43 | + "LOOP_START%=: \n" \ |
| 44 | + "lea (%[" #start "], %%r8), %%r9 \n" \ |
| 45 | + "movdqa %%xmm1, (%%r9) \n" \ |
| 46 | + "add $" STR(MOVE_SIZE) ", %%r8 \n" \ |
| 47 | + "cmp $" STR(FENCE_BOUND) ",%%r8\n" \ |
| 48 | + "mfence \n" \ |
| 49 | + "jl LOOP_START%= \n" |
| 50 | + |
| 51 | + |
| 52 | +int main(int argc, char **argv) { |
| 53 | + |
| 54 | + // in principle, you would want to clear out cache lines (and the |
| 55 | + // pipeline) before doing any of the inline assembly stuff. But, |
| 56 | + // that's hard. And, its probably noise when you execute over |
| 57 | + // enough things. |
| 58 | + |
| 59 | + |
| 60 | + // allocate some meomery |
| 61 | + char *base =(char *) mmap(nullptr, |
| 62 | + MAP_SIZE, |
| 63 | + PROT_READ | PROT_WRITE, |
| 64 | + MAP_ANONYMOUS | MAP_PRIVATE, |
| 65 | + -1, |
| 66 | + 0); |
| 67 | + |
| 68 | + if (base == MAP_FAILED) { |
| 69 | + fprintf(stderr, "oops, you suck %d\n", errno); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + char *addr = NULL; |
| 73 | + |
| 74 | + intptr_t *iaddr = (intptr_t*) base; |
| 75 | + intptr_t hash = 0; |
| 76 | + struct timespec tstart = {0,0}, tend = {0,0}; |
| 77 | + |
| 78 | + // Necessary so that we don't include allocation costs in our benchmark |
| 79 | + while (iaddr < (intptr_t *)(base + MAP_SIZE)) { |
| 80 | + hash = hash ^ (intptr_t) iaddr; |
| 81 | + *iaddr = hash; |
| 82 | + iaddr++; |
| 83 | + } |
| 84 | + |
| 85 | + // should flush everything from the cache. But, how big is the cache? |
| 86 | + addr = base; |
| 87 | + while (addr < (base + MAP_SIZE)) { |
| 88 | + asm volatile( |
| 89 | + "mov %[buf], %%rsi\n" |
| 90 | + "clflush (%%rsi)\n" |
| 91 | + : |
| 92 | + : [buf] "r" (addr) |
| 93 | + : "rsi"); |
| 94 | + addr += CACHELINE_SIZE; |
| 95 | + } |
| 96 | + |
| 97 | + asm volatile ("mfence\n" :::); |
| 98 | + |
| 99 | + clock_gettime(CLOCK_MONOTONIC, &tstart); |
| 100 | + addr = base; |
| 101 | + while (addr < (base + MAP_SIZE)) { |
| 102 | + //fprintf (stderr, "addr %p bound %p\n", addr, base + MAP_SIZE); |
| 103 | + asm volatile( |
| 104 | + BODY(addr) |
| 105 | + : |
| 106 | + : [addr] "r" (addr) |
| 107 | + : "r8", "r9", "xmm0"); |
| 108 | + |
| 109 | + addr += (FENCE_COUNT * MOVE_SIZE); |
| 110 | + } |
| 111 | + clock_gettime(CLOCK_MONOTONIC, &tend); |
| 112 | + uint64_t nanos = (1000000000 * tend.tv_sec + tend.tv_nsec); |
| 113 | + nanos -= (1000000000 * tstart.tv_sec + tstart.tv_nsec); |
| 114 | + |
| 115 | + |
| 116 | + printf("%lu\n", nanos); |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
0 commit comments