Skip to content

Instantly share code, notes, and snippets.

@chengscott
Last active January 26, 2023 22:49
Show Gist options
  • Save chengscott/bd99de0e8c5fc41d7c8706104ee3157e to your computer and use it in GitHub Desktop.
Save chengscott/bd99de0e8c5fc41d7c8706104ee3157e to your computer and use it in GitHub Desktop.
sudo sysctl -w vm.nr_hugepages=256; gcc -O2 cache.c -o cache
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#define BASE_SIZE 128 // array base size = 1KB
#define MAX_SIZE 17 // array size ranges from 1KB to 64MB
#define LG_STRIDE 0 // stride = 1
#define MAX_STRIDE 7 // stide ranges from 1 to 64
#define TOTAL_RUNS 10
#define ITERS_PER_RUN (1<<25)
static int RunOnce(size_t n, size_t s) {
struct timespec begin, end;
uint64_t *arr;
if ((n * sizeof(uint64_t)) < (1 << 21)) {
arr = (uint64_t *) calloc(n, sizeof(uint64_t));
} else {
arr = aligned_alloc(1 << 21, n * sizeof(uint64_t));
madvise(arr, n * sizeof(uint64_t), MADV_HUGEPAGE);
}
int fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
write(fd, "3", 1);
clock_gettime(CLOCK_MONOTONIC, &begin);
for (size_t i = 0; i < ITERS_PER_RUN; ++i) {
arr[(i << s) & (n - 1)] += 0xdeadbeaf;
}
clock_gettime(CLOCK_MONOTONIC, &end);
free(arr);
close(fd);
return (1000000000ULL * (end.tv_sec - begin.tv_sec) + end.tv_nsec - begin.tv_nsec) / 1000ULL;
}
static int Run(size_t n, size_t s) {
int ret = 0;
for (size_t i = 0; i < TOTAL_RUNS; ++i) {
ret += RunOnce(n, s) / TOTAL_RUNS;
}
return ret;
}
int main() {
for(size_t s = 0; s < MAX_STRIDE; ++s) {
for (size_t i = 0; i < MAX_SIZE; ++i) {
int res = Run(BASE_SIZE << i, LG_STRIDE+s);
fprintf(stderr, "%d\t", res);
}
fprintf(stderr, "\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment