Skip to content

Instantly share code, notes, and snippets.

@rocallahan
Created June 24, 2021 14:24
Show Gist options
  • Save rocallahan/a456903d7bc1480f27d16794ac8437c0 to your computer and use it in GitHub Desktop.
Save rocallahan/a456903d7bc1480f27d16794ac8437c0 to your computer and use it in GitHub Desktop.
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <unistd.h>
#define MAP_SIZE 20LL*1024*1024*1024*1024 /* 20TB */
#define BUF_PAGE_COUNT 1024*1024
int main(void) {
void* p = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
int fd = open("/proc/self/pagemap", O_RDONLY);
uint64_t* buf = (uint64_t*)malloc(BUF_PAGE_COUNT*sizeof(uint64_t));
uint64_t pages_present = 0;
for (uintptr_t offset = 0; offset < MAP_SIZE; offset += PAGE_SIZE*BUF_PAGE_COUNT) {
pread(fd, buf, BUF_PAGE_COUNT*sizeof(uint64_t), 8*(((uintptr_t)p + offset)/PAGE_SIZE));
for (size_t i = 0; i < BUF_PAGE_COUNT; ++i) {
if (buf[i] & (1LL << 63)) {
++pages_present;
}
}
}
printf("Pages present: %lld\n", (long long)pages_present);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment