Hello everyone,
for quite some time I've been working on a portable mmap/virtual memory
library and I've finally found enough time to bring it out of the 'my
internal litle tool' state into something presentable:
https://github.com/psiha/mmap (C++14 currently).
I would now kindly ask fellow devs for opinions (on the good, the bad,
the ugly and the future;)
First let me answer the basic questions (i.e. on motivation and scope)...
Q&A:
(1) Why?
Considering Boost already offers two related solutions
* Interprocess:
http://www.boost.org/doc/libs/release/doc/html/interprocess/sharedmemorybetw...
* Iostreams:
http://www.boost.org/doc/libs/release/libs/iostreams/doc/classes/mapped_file...
why do we want a new one?
Even if the two mentioned solutions were adequate, the problem domain
would still merit a separate, dedicated library merely considering its
complexity (which will become apparent in later points).
(I suspect Ion would agree here considering he actually authored a
related standardization proposal:)
(2) Why a completely new library instead of a repackaging of existing
functionality?
I'm not satisfied with (a) the API semantics/design, (b) the API
'power'/library capabilities (c) and the implementation efficiency/overhead.
a) Insistence on POSIX semantics: that shared memory objects have to be
persistent (kernel lifetime) and resizable while not everyone wants or
needs this (https://svn.boost.org/trac/boost/ticket/4827). This has
implications both:
- in the interface (requiring manual cleanup guards, additional platform
specific shm types like windows_shared_memory)
- and the overhead of the library (emulating shm with mapped files on
major platforms that do not offer full POSIX compliance: Windows, OSX,
iOS and Android)
MMAP solves this with one class template 'named_memory' and policies:
https://github.com/psiha/mmap/blob/master/include/boost/mmap/mappable_object...
so the user can choose:
named_memory
Domagoj Saricgmail.com> writes: > > Hello everyone, > > for quite some time I've been working on a portable mmap/virtual memory > library and I've finally found enough time to bring it out of the 'my > internal litle tool' state into something presentable: > https://github.com/psiha/mmap (C++14 currently). > I would now kindly ask fellow devs for opinions (on the good, the bad, > the ugly and the future;) > First let me answer the basic questions (i.e. on motivation and scope)... I'm interested in the general idea of controlling virtual memory explicitly and portably. >From a quick look at the code, it seems to deal with the following aspects: - map a file (not sure if anonymous is well supported) - set protection/access privileges - set mapping sharing properties I'd like to have access to the following extended functionality: - map at a fixed address - actually allocate/free the pages - map the same hardware page to multiple places in virtual memory without a file
Hi Domagoj and Mathias.
for quite some time I've been working on a portable mmap/virtual memory
I have been using Boost.Interprocess for handling large genetic data files in the proposed Boost.Genetics, it is intended for sharing memory images between processes, but works just as well as a portable mmap wrapper including as a block allocator. The only shame is that it is not 100% header-based and so needs binaries for different platforms. mmap is the only sensible way of working with large in-memory datasets as it can exceed the swap file size. Andy.
On 4.3.2016. 1:18, Andy Thomason wrote:
Hi Domagoj and Mathias.
for quite some time I've been working on a portable mmap/virtual memory
I have been using Boost.Interprocess for handling large genetic data files in the proposed Boost.Genetics, it is intended for sharing memory images between processes, but works just as well as a portable mmap wrapper including as a block allocator.
And the "portable mmap wrapper" and "block allocator" are IMO two separate things and belong in separate libraries, as mmap-ing is not essentially an IPC thing. This is one of the chalenges in making a VM/MMAP library - find out the clear API boundary where the library should stop and Interprocess should then build upon.
The only shame is that it is not 100% header-based and so needs binaries for different platforms.
But Interprocess is a header only library?
mmap is the only sensible way of working with large in-memory datasets as it can exceed the swap file size.
Not sure what you mean by 'it can exceed swap file size'. A mapping can be larger than the available RAM+swap only if you use overcommit and/or 'uncommited'/'unreserved' mappings (and thus risk AV/SIGSEV crashes). From your description I gather that you need/use something like a scratch disk/file(s) (which can be useful on systems with a dedicated but 'not big enough' swap partition). IMO this is not something client code should worry about, i.e. it should be abstracted by the VM library: you would specify ('reserve') how much scratch space you need and the library would see if it can use the paging file as a scratch file (what it esentially is) or look for a disk/partition with more space (and it would choose appropriate mapping and file creation flags/system hints optimal for scratch storage)... And this brings me to another point not covered in the opening post: 9. Resizable views (currently a todo item) Besides the already mentioned resizable ('ftruncatable') mapping objects, we can also have resizable views of those mappings. For example, one might wish to 'walk' a file (possibly in a random fashion) in chunks (e.g. files that contain 'ready to use data', such as uncompressed audio or video files). The problems is what API would be an 'overall best' for such use cases. mapped_view objects are currently thin RAII wrappers around an iterator_range and iterator_ranges have the handy advance_begin() and advance_end() member functions (which are hidden by the non resizable mapped_view class). These however move in 'element'-sized chunks (bytes for char views) which does not seem handy or efficient for mapped_views: you'd usually want to either resize the view or advance in page-size or mapped_view.size() chunks. Perhaps I can, for the future resizable_mapped_view class, retain the advance_begin/end() interface and add - advance() (which does a simultanaeous begin/end advance) - increment/decrement operators which hop in chunks equal the current size of the view - a seek() function that can do an absolute jump and resizing in one call. An additional question is should resizable_mapped_views hold references to their parent mapping objects and resize them as needed (or fail if one tries to grow them beyond the size of the mapped file/shared memory object)? -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman
On 06/03/2016 22:19, Domagoj Saric wrote:
On 4.3.2016. 1:18, Andy Thomason wrote:
Hi Domagoj and Mathias.
for quite some time I've been working on a portable mmap/virtual memory
I have been using Boost.Interprocess for handling large genetic data files in the proposed Boost.Genetics, it is intended for sharing memory images between processes, but works just as well as a portable mmap wrapper including as a block allocator.
And the "portable mmap wrapper" and "block allocator" are IMO two separate things and belong in separate libraries, as mmap-ing is not essentially an IPC thing. This is one of the chalenges in making a VM/MMAP library - find out the clear API boundary where the library should stop and Interprocess should then build upon.
The only shame is that it is not 100% header-based and so needs binaries for different platforms.
But Interprocess is a header only library?
Yes. It depends on DateTime but no library function is needed so you can define BOOST_DATE_TIME_NO_LIB to avoid automatic linking. See: http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess.html#interprocess... Best, Ion
On 3.3.2016. 18:11, Mathias Gaunard wrote:
I'm interested in the general idea of controlling virtual memory explicitly and portably.
That whas point 3 in the opening post.
From a quick look at the code, it seems to deal with the following aspects: - map a file (not sure if anonymous is well supported)
Thanks for taking a look, however I'm not sure what do you mean by anonymous. Non-anonymous (i.e. named) file mappings exist natively only on Windows (point 5 from the opening post). However if you meant anonymous memory mappings then yes I did not cover those in the opening post. So let's 'name it' point 8: 8. Anonymous shared memory - memory shared between a parent and its child processes. This one is still a todo item, mostly because I'm not yet sure how to model the discrepancy between the Windows and POSIX models. Both Windows and POSIX support inherited handles/descriptors for child processes (created with CreateProcess()/execve()), POSIX however also supports forking along with the corresponding MAP_ANONYMOUS mmap flag (i.e. anonymous virtual memory 'views'). So, one way is what Interprocess does now: use inherited handles on Windows and MAP_ANONYMOUS on POSIX - but this does not cover the case if someone wants handle/descriptor inheritance on POSIX systems too, and it also entails different ways of communicating the anonymous mapping 'handle'/location between the processes on Windows vs POSIX systems. Another way would be for anonymous_shared_memory to use the inherited handle/descriptor approach and to define a separate POSIX-only class anonymous_shared_memory_view used for the fork scenario. Unfortunately to me this only seems as a less ugly approach then the one above because, if the most often use case in crossplatform code is to use CreateProcess() on Windows and fork() on POSIX, it will still cause #ifdefs in user code...
I'd like to have access to the following extended functionality: - map at a fixed address
Still a todo item (again still figuring how to add the parameter to the API without exploding the number of overloads and/or forcing the user to specify it everywhere).
- actually allocate/free the pages
You mean like direct virtual memory management (i.e. the Win32 VirtualAlloc* API)?
- map the same hardware page to multiple places in virtual memory without a file
You mean something like the 'magic ring buffer' example from point 3 from the opening post? Arbitrary remapping of just any address (even if page aligned) is AFAIK not possible on any of the major systems. I.e. you can map the same mappable object (shared memory or a file) at multiple locations (subject to 'allocation granularity') but you cannot for example make a 'virtual copy' of a std::vector (i.e. of memory allocated with malloc as opposed to memory allocated with mmap)... -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman
participants (4)
-
Andy Thomason
-
Domagoj Saric
-
Ion Gaztañaga
-
Mathias Gaunard