summaryrefslogtreecommitdiff
path: root/core/test_mmap.cpp
blob: 9166521c607eb697b6a113d3b327feee2cc5fbfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
}
contact: Jan Huwald // Impressum