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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "eckit/eckit.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/log/Bytes.h"
#include "eckit/memory/MMap.h"
#include "eckit/testing/Test.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
int get_pagesize() {
int pagesize;
errno = 0ll;
if ((pagesize = sysconf(_SC_PAGE_SIZE)) == -1) {
if (errno == 0) {
printf("PAGE_SIZE not supported by sysconf implementation\n");
}
else {
perror("sysconf error.");
}
exit(EXIT_FAILURE);
}
return pagesize;
}
// |--------------------------------|--------------------------------|
// ^2G limit ^4Gb
// ^--------^ mapped region
#define SIZE_2G 2147483648 // 2^31
#define TOTAL_SIZE 2 * SIZE_2G // 4Gb file
#define ELEMS_2G (SIZE_2G / sizeof(int))
#define NELEMS 8 * 1024
#define MAP_SIZE (NELEMS * sizeof(int))
#define ELEM_LW (ELEMS_2G - NELEMS / 2)
#define ELEM_UP (ELEM_LW + NELEMS)
#define SIZE_UP (ELEM_UP * sizeof(int))
#define SIZE_LW (ELEM_LW * sizeof(int))
//----------------------------------------------------------------------------------------------------------------------
CASE("Test memory map") {
// compute the 2GB limit and its number of pages
int pagesize = get_pagesize();
size_t nbpages_2gb = SIZE_2G / pagesize;
EXPECT(nbpages_2gb * pagesize == SIZE_2G);
std::cout << Bytes(SIZE_2G) << " are " << nbpages_2gb << " memory pages" << std::endl;
// mmapped array of int's
int* map;
// make temporary file name
int fd;
char filename[] = "mmaped.XXXXXX";
if ((fd = ::mkstemp(filename)) == -1) {
perror("cannot create temporary file"), exit(EXIT_FAILURE);
}
// file is opened in mkstemp, so this is not needed
// if( ( fd = ::open(filename, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600) ) == -1 )
// perror("Error opening file"), exit(EXIT_FAILURE);
// stretch the file size to the size of the (mmapped) array of ints
if ((long long)::lseek(fd, TOTAL_SIZE - 1, SEEK_SET) < 0) {
perror("Error calling lseek() to 'stretch' the file"), exit(EXIT_FAILURE);
}
// write something to the end of the file to actually resize it correctly
if (::write(fd, "", 1) != 1) {
close(fd), perror("Error writing last byte of the file"), exit(EXIT_FAILURE);
}
// map the file
std::cout << "mapping " << Bytes(MAP_SIZE) << " from " << Bytes(SIZE_LW) << " to " << Bytes(SIZE_UP) << std::endl;
if ((map = (int*)MMap::mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SIZE_LW)) == MAP_FAILED) {
close(fd), perror("Error mmapping the file"), exit(EXIT_FAILURE);
}
// write to file as if it were memory
for (int i = 0; i < NELEMS; ++i) {
map[i] = 2 * i;
}
// free the mmapped memory
if (MMap::munmap(map, MAP_SIZE) == -1) {
perror("Error un-mmapping the file"), exit(EXIT_FAILURE);
}
// close the file
if (close(fd) == -1) {
perror("Error closing file descriptot"), exit(EXIT_FAILURE);
}
// test that the contents of the file are correct
// remove the file -- its large!
if (::unlink(filename) == -1) {
perror("Error removing the file"), exit(EXIT_FAILURE);
}
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return run_tests(argc, argv);
}
|