| 1 | mmap_allocator - A STL allocator that mmaps files |
| 2 | ------------------------------------------------- |
| 3 | |
| 4 | Introduction |
| 5 | ------------ |
| 6 | |
| 7 | When reading large files (>100MB) into memory, read() calls are usually |
| 8 | not very space and time efficient, since the whole data is copiied at |
| 9 | least once. Furthermore, when using STL containers (like a vector for |
| 10 | example), data is copiied another time unless you specify the location |
| 11 | of the vector content as parameter to read(). |
| 12 | |
| 13 | It would be nice to tell the vector a filename and have the vector mmap |
| 14 | the file directly. This not only avoids the read() copiing (and the |
| 15 | STL vector copiing) but also allows different processes that read the |
| 16 | same file to see the same physical memory. Fortunately STL foresees |
| 17 | an interface to do exactly this. |
| 18 | |
| 19 | Put short, if you need to handle big files that contain unstructured data |
| 20 | (like doubles or even text files), mmap_allocator is worth a try. It was |
| 21 | written as part of a consulting project I did for a large Austrian bank. |
| 22 | |
| 23 | License |
| 24 | ------- |
| 25 | |
| 26 | This code is (c) Johannes Thoma 2012 and is LGPL. Please write me an email to |
| 27 | [email protected] if you find this useful, or if you find bugs or want |
| 28 | to extend the library. |
| 29 | |
| 30 | How it works |
| 31 | ------------ |
| 32 | |
| 33 | Many STL container classes accept allocators as part of their instantiation |
| 34 | and construction. The default allocator simply does a malloc on allocate and |
| 35 | a free on deallocate. By subclassing the default allocator and replacing the |
| 36 | allocate and deallocate methods with code that memory maps a file and return |
| 37 | a pointer to the mmaped area, we can create STL containers that use memory |
| 38 | mapped regions directly. |
| 39 | |
| 40 | Usage |
| 41 | ----- |
| 42 | |
| 43 | To build this, do a |
| 44 | |
| 45 | make |
| 46 | |
| 47 | followed by a |
| 48 | |
| 49 | make install |
| 50 | |
| 51 | When compiling, include the mmappable_vector.h file and link with |
| 52 | the -lmmap_allocator library. |
| 53 | |
| 54 | Example |
| 55 | ------- |
| 56 | |
| 57 | Suppose you have a file with 1024 ints. Then: |
| 58 | |
| 59 | mmappable_vector<int> my_vector; |
| 60 | |
| 61 | declares a vector that uses mmap_allocator to mmap the file. By calling: |
| 62 | |
| 63 | my_vector.mmap_file("testfile", READ_ONLY, 0, 1024); |
| 64 | |
| 65 | the mmappable_vector class will call allocate of the mmap_allocator |
| 66 | class, which in turn will mmap the file and set the content of the |
| 67 | vector to the mmapped area. The file's content can then be accessed |
| 68 | the usual way (my_vector[i]). |
| 69 | |
| 70 | Use |
| 71 | |
| 72 | my_vector.munmap_file(); |
| 73 | |
| 74 | to drop the mapping (it may remain in an internal cache, however). After |
| 75 | this call all accesses of mmappable_vector elements are invalid, same |
| 76 | goes for the iterators. |
| 77 | |
| 78 | Do not forget to: |
| 79 | |
| 80 | using namespace mmap_allocator_namespace; |
| 81 | |
| 82 | and include: |
| 83 | |
| 84 | #include "mmappable_vector.h" |
| 85 | |
| 86 | but you probably know that yourself ;) |
| 87 | |
| 88 | Please see the test_allocator.cpp file for more examples |
| 89 | (I used Testdriven development for this project and can highly recommend |
| 90 | this development model). |
| 91 | |
| 92 | Mode |
| 93 | ---- |
| 94 | |
| 95 | As part of the construction of the mmap_allocator object a mode field can be |
| 96 | specified. It does the following: |
| 97 | |
| 98 | * DEFAULT_STL_ALLOCATOR: Default STL allocator (malloc based). Reason |
| 99 | is to have containers that do both and are compatible |
| 100 | * READ_ONLY: Readonly modus. Segfaults when vector content is written to. |
| 101 | * READ_WRITE_PRIVATE: Read/write access, writes are not propagated to disk. |
| 102 | * READ_WRITE_SHARED: Read/write access, writes are propagated to disk |
| 103 | (file is modified) |
| 104 | |
| 105 | The offset parameter must be page aligned (PAGE_SIZE, usually 4K or 8K), |
| 106 | else mmap will return an error. |
| 107 | |
| 108 | Mmap file pool |
| 109 | -------------- |
| 110 | |
| 111 | Beginning with 0.3, mmap allocator uses a pool of mmaped files. If you |
| 112 | map the same file with the same access mode again it gets the same |
| 113 | (virtual and physical) memory assigned as it was mapped previously. |
| 114 | We needed this because our program (that uses mmap_allocator) mmaps |
| 115 | a file in many small chunks (10K junks) which eventually lead to a |
| 116 | Out of filedescriptors error (when mapping a 100Meg file in 10K junks). |
| 117 | |
| 118 | Following flags can be passed (by |'ing them together) to the mmap_file |
| 119 | method: |
| 120 | |
| 121 | * MAP_WHOLE_FILE: Set this flag when you know that you need |
| 122 | the whole (or most parts of the) file later and only want to |
| 123 | request a part of it now. |
| 124 | |
| 125 | * ALLOW_REMAP: Set this flag if you are allocating a vector |
| 126 | from a mmapped file and you know that you do not need previous |
| 127 | mappings any more. Normally this is used when the file size |
| 128 | changes (in particular when it grows). Be aware, however that |
| 129 | this could invalidate all allocations for that file that have |
| 130 | been made before. |
| 131 | |
| 132 | * BYPASS_FILE_POOL: Set this flag if you want a per-vector |
| 133 | private mapping. This is useful in conjunction with READ_WRITE_PRIVATE |
| 134 | mappings. |
| 135 | |
| 136 | Mmappable vector class |
| 137 | ---------------------- |
| 138 | |
| 139 | Beginning with version 0.4, there is a special std::vector subclass |
| 140 | for use with mmapped vectors. This was necessary because there is |
| 141 | no standard way to set the size of an STL vector without initializing |
| 142 | the content (which is not wanted since the content is coming from the |
| 143 | mmapped file). From now on, please use the mmapped_vector class for |
| 144 | using mmap_allocator. |
| 145 | |
| 146 | The old method by using the mmap_allocators constructor to set the |
| 147 | parameters for file mapping is still supported, however deprecated. |
| 148 | |
| 149 | READ_WRITE caveats |
| 150 | ------------------ |
| 151 | |
| 152 | Unlike reading from a file, a mmappable_vector that is mapped via |
| 153 | the mmap file pool contains all changes already made to the content |
| 154 | before. Suppose if you have: |
| 155 | |
| 156 | mmappable_vector<int> p; |
| 157 | if (method == MMAP) { |
| 158 | p.mmap_file("testfile", READ_WRITE_PRIVATE, 0, filesize("testfile")); |
| 159 | } else { |
| 160 | readFromFileIntoVector(p, "testfile", READ_WRITE_PRIVATE, 0, filesize("testfile") |
| 161 | } |
| 162 | |
| 163 | and then do something like: |
| 164 | |
| 165 | for (it = p.begin();it != p.end();it++) { |
| 166 | *it += 1; |
| 167 | } |
| 168 | |
| 169 | This will do not what you would expect at first glance when being called |
| 170 | twice. When the vector maps the file for the second time, it will map it |
| 171 | from the file pool and hence already have the values increased by one. |
| 172 | |
| 173 | To avoid this, use the bypass_file_pool flag which will cause the file |
| 174 | being mmapped another time with different virtual memory mappings. |
| 175 | |
| 176 | Exceptions |
| 177 | ---------- |
| 178 | |
| 179 | There is a custom exception class mmap_allocator_exception which is |
| 180 | used at various places, for example when the file to be mapped is |
| 181 | not found. Use the e.message() method to find out what happened. |
| 182 | |
| 183 | Conversion function templates |
| 184 | ----------------------------- |
| 185 | |
| 186 | To convert between a STL vector and a mmappable vector, use the |
| 187 | to_std_vector(mappable_vector) and to_mmappable_vector(std_vector) |
| 188 | function templates. However, try to avoid this because this will |
| 189 | copy the whole vector contents. |
| 190 | |
| 191 | Verbosity |
| 192 | --------- |
| 193 | |
| 194 | To debug the allocator, there are two ways: |
| 195 | |
| 196 | * call set_verbosity() with a positive value at runtime. |
| 197 | * define MMAP_ALLOCATOR_DEBUG as a positive value at compile time. |
| 198 | |
| 199 | The latter is done by make test. |
| 200 | |
| 201 | Version history |
| 202 | --------------- |
| 203 | |
| 204 | * 0.1.0, first release. |
| 205 | * 0.2.0, some interface changes. |
| 206 | * 0.3.0, mmaped file pool. |
| 207 | * 0.3.1, do not remap files when area fits in already mapped file. |
| 208 | * 0.3.2, never use MAP_FIXED. |
| 209 | * 0.3.3, bugfix in computing pointers. |
| 210 | * 0.4.0, mmapped_vector class. |
| 211 | * 0.5.0, cleaner interface, illegal offset bugfix. |
| 212 | * 0.5.1, bypass_file_pool flag. |
| 213 | * 0.5.2, changed bool parameters to flags argument. |
| 214 | * 0.5.3, set_verbosity() to toggle debug output. |
| 215 | * 0.6.0, to_stl_vector function template. |
| 216 | * 0.6.1, flags bugfix. |
| 217 | * 0.7.0, moved mmappable_vector to separate header. |
| 218 | * 0.8.0, to_mmappable_vector conversion function. |
| 219 | * 0.8.1, exception now knows what() method. |
| 220 | * 0.9.0, more standard conformant exception handling. |
| 221 | * 0.9.1, fixed a permission bug in MMAP_READWRITE_PRIVATE. |
| 222 | * 0.10.0, KEEP_FOREVER flag: never close any file. |
| 223 | * 0.10.1, fixed bug with allocating/deallocating 0 bytes. |
| 224 | |
| 225 | Author |
| 226 | ------ |
| 227 | |
| 228 | This was written by Johannes Thoma <[email protected]> |
| 229 | Thanks to Piotr Nycz from stackoverflow for contributing |
| 230 | the constructor function template in mmappable_vector.h |