Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <sys/mman.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- #define SIZE (1024*1024*1024)
- unsigned long pmdsize = (1UL << 21);
- unsigned long pagesize = (1UL << 12);
- static void pte_map_thps(char *mem, size_t size)
- {
- size_t offs;
- int ret = 0;
- /* PTE-map each THP by temporarily splitting the VMAs. */
- for (offs = 0; offs < size; offs += pmdsize) {
- ret |= madvise(mem + offs, pagesize, MADV_DONTFORK);
- ret |= madvise(mem + offs, pagesize, MADV_DOFORK);
- }
- if (ret) {
- fprintf(stderr, "ERROR: mprotect() failed\n");
- exit(1);
- }
- }
- int main(int argc, char *argv[])
- {
- long total_time = 0;
- char *p;
- int ret = 0;
- p = mmap((void *)(1UL << 30), SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- if (p != (1UL << 30)) {
- perror("mmap");
- return 1;
- }
- memset(p, 0, SIZE);
- if (madvise(p, SIZE, MADV_NOHUGEPAGE))
- perror("madvise");
- explicit_bzero(p, SIZE);
- pte_map_thps(p, SIZE);
- for (int loops = 0; loops < 400; loops++) {
- struct timespec start, end;
- if (clock_gettime(CLOCK_MONOTONIC, &start) != 0)
- exit(1);
- if (mprotect(p, SIZE, PROT_READ))
- perror("mprotect"), exit(1);
- if (mprotect(p, SIZE, PROT_READ|PROT_WRITE))
- perror("mprotect"), exit(1);
- if (clock_gettime(CLOCK_MONOTONIC, &end) != 0)
- exit(1);
- long elapsed_ns = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
- total_time += elapsed_ns;
- explicit_bzero(p, SIZE);
- }
- printf("TOTAL: %ld\n", total_time);
- }
Advertisement
Add Comment
Please, Sign In to add comment