diff options
Diffstat (limited to 'core/test_mmap.cpp')
-rw-r--r-- | core/test_mmap.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/core/test_mmap.cpp b/core/test_mmap.cpp new file mode 100644 index 0000000..9166521 --- /dev/null +++ b/core/test_mmap.cpp @@ -0,0 +1,52 @@ +#include <malloc.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> + +// how many mmap areas to create +#define mapMax 100 +// how large should they be +#define mapSize ((size_t)1024 * 1024 * 1024 * 100) + +int fd[mapMax]; +char name[mapMax][100]; + +void quit(int ret) { + for (int i=0; i<mapMax; i++) { + // close(fd[i]); + unlink(name[i]); + } + exit(ret); +} + +int main() { + memset(name[0], 0, sizeof(name)); + + fprintf(stderr, "Allocating\t%f GB\t=\t%d\tx\t%f GB\n", ((double) mapMax * mapSize) / 1024.0 / 1024 / 1024, mapMax, (double) mapSize / 1024.0 / 1024 / 1024 ); + + // create tmp files + for (int i=0; i<mapMax; i++) { + tmpnam(name[i]); + fd[i] = open( name[i], O_CREAT | O_EXCL | O_RDWR, S_IRWXU ); + if (fd[i] == -1) { + fprintf(stderr, "opening tmp file #%d failed\n", i); + quit(-1); + } + } + + // mmap them + for (int i=0; i<mapMax; i++) { + if (MAP_FAILED == mmap((void*) NULL, mapSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NONBLOCK, fd[i], 0)) { + fprintf(stderr, "mmap #%d failed (errno: %d, errstr: %s)\n", i, errno, strerror(errno)); + quit(-1); + } + } + + quit(0); +} |