Skip to content

Commit

Permalink
contrib: add mmap_test
Browse files Browse the repository at this point in the history
Relates-to: #318
  • Loading branch information
rfjakob committed Jun 6, 2024
1 parent d5791d3 commit c759f1b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contrib/mmap_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/mmap_test
/test.bin
6 changes: 6 additions & 0 deletions contrib/mmap_test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mmap_test: Makefile *.c
gcc -Wall -Wextra -g -o mmap_test mmap_test.c

.PHONY: format
format:
clang-format --style=file -i *.c
50 changes: 50 additions & 0 deletions contrib/mmap_test/mmap_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

/*
HOW TO USE THIS:
truncate -s 10G test.bin && ./mmap_test
*/

int main()
{
int fd;
char* addr;
struct stat sb;

fd = open("test.bin", O_RDWR);
if (fd == -1) {
perror("open test.bin");
exit(1);
}

if (fstat(fd, &sb) == -1) {
perror("fstat");
exit(2);
}

addr = mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
perror("mmap");
exit(3);
}

printf("pid %d, mmap ok (%ld MiB)\n", getpid(), sb.st_size/1024/1024);

for(long i = 0; i < sb.st_size; i += 1024) {
volatile char x = addr[i];
x++;
}

printf("sleeping 1h\n");
sleep(3600);

munmap(addr, sb.st_size);
close(fd);

exit(0);
}

0 comments on commit c759f1b

Please sign in to comment.