Last active
October 29, 2023 07:39
-
-
Save kumagi/4587f42ed94e79b19699bdcc36ab1f85 to your computer and use it in GitHub Desktop.
mmap_max
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory.h> | |
#define _XOPEN_SOURCE_EXTENDED 1 | |
#include <sys/mman.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <climits> | |
#include <algorithm> | |
int main() { | |
int flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE; | |
int prot = PROT_READ | PROT_WRITE; | |
int64_t ok = 0; | |
int64_t ng = INT64_MAX; | |
while (std::abs(ng - ok) > 1) { | |
uint64_t mid = (ok + ng) / 2; | |
uint64_t vmsize = mid; | |
void* virtMem = mmap(0, vmsize, prot, flags, -1, 0); | |
if ((uint64_t)(intptr_t)virtMem != 0xffffffffffffffff) { | |
printf("%lx success (%p)\n", vmsize, virtMem); | |
((char*)virtMem)[0] = 'a'; | |
munmap(virtMem, vmsize); | |
ok = mid; | |
} else { | |
printf("%lx failed (%p)\n", vmsize, virtMem); | |
ng = mid; | |
} | |
} | |
printf("finally %lfTiB\n", (double) ok / (1024llu * 1024 * 1024 * 1024)); | |
char* mem = (char*)mmap(0, ok, prot, flags, -1, 0); | |
const uint64_t kPageSize = 4096; | |
for (int64_t i = 0; i < ok; i += kPageSize) { | |
mem[i] = 'a'; | |
printf("used %lf MiB\n", (double)i / (1024 * 1024)); | |
madvise(&mem[i], kPageSize, MADV_DONTNEED); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment