Ayende @ Rahien

It's a girl

Growable memory

I wish that I could have more control over virtual memory. In particular, what I would really like at this time is the ability to map two virtual address ranges to the same physical memory. And yes, I am well aware (and already using) the ability to do just that using memory mapped files.  However, what I would like to do is to have a pure memory based system, without any files being involved. This is meant to be used for the memory only system, which is commonly meant to be be used for unit testing.

The reason that I want to be able to map the physical memory multiple times is that I have a buffer. Let us say that it is 1MB in size. And now I need to grow it. I can’t ask the OS to just grow the buffer, since it might not have the virtual address available past the buffer end by the time I request it. What I would like to do is to request a new virtual allocation, let us say of 2 MB, and then ask the OS to map the same physical memory for the first buffer to the first section of the new buffer, and new memory for the rest.

The end result is that the first part of the buffer is mapped twice, and any changes you make there will be visible in both locations. Now, it is pretty easy to do this with memory mapped files, but I couldn’t find a way to do it sans files.

What I ended up doing is reserve a large portion of the virtual address space (using VirtualAlloc) and then committing it on demand. But I would really have liked to do something better, because now just moved the problem to when I run out of the reserved buffered space.

Comments

Rafal
01/03/2014 10:24 AM by
Rafal

But why not use mem mapped files? You don't have to commit them to disk, do you (i mean, as long as you don't run out of memory)?

tobi
01/03/2014 12:07 PM by
tobi

You can create a file mapping without a file using CreateFileMapping. It is backed by the paging file then, just like any other virtual memory.

alex
01/03/2014 04:56 PM by
alex

I must admit, I also cannot see the benefits of having a (virtual) memory based alternative. A memory map seems to do the job just fine in testing scenarios.

Don
01/03/2014 08:21 PM by
Don

This must be why big sql databases use RAW fs.

Howard Chu
01/04/2014 01:49 AM by
Howard Chu

VirtualAlloc has no corresponding function in POSIX, how are you going to port this bit to Linux?

Alexei K
01/04/2014 04:54 PM by
Alexei K

So the point of avoiding files is that you want the test to run faster? Why not "cheat" with RAM disk, then. This way the code path will not be different from production (thus testing is more legit), just the performance will be much higher.

Howard Chu
01/04/2014 06:20 PM by
Howard Chu

When running the OpenLDAP test suite I routinely use a RAMdisk for the test working directory. (tmpfs on Linux.) Saves unnecessary wear and tear on my SSDs.

Some SQL DBs have the notion of temporary tables, which are used to hold intermediate results for a large, complex query. The intent is not to store these on disk, and to have the tables automatically erased after the overall query completes. That's another situation where the DB engine would normally not use a disk file. (It's still moot, since this sort of space is backed by the paging file/swap partition anyway. A lot of what these systems do at higher levels is nonsensical when you look at how it actually works underneath.)

Ayende Rahien
01/05/2014 09:57 AM by
Ayende Rahien

Rafal, Because mem mapped file still have to be allocated to disk. And when you grow them, you need to grow the file on disk, which means that you have to allocate disk space. Which means that you have to interact with the disk in the first place. That is something that we really don't want.

Ayende Rahien
01/05/2014 09:58 AM by
Ayende Rahien

Tobi, While I can create a mem mapped file that is backed by the paging file, that isn't very helpful for growing it. Like any virtual mem, there is no way to specify, give me more memory at location X. With normal files, we can just do a whole new allocation after growing the file size, but that isn't a valid option for page file memory.

Ayende Rahien
01/05/2014 10:00 AM by
Ayende Rahien

Don, Not really, no. Most databases don't really care for the way the file system allocate things. They handle their own internal allocations and space usage internally. In fact, the more layers you have between the disk and the db, the worse we are off. From the point of view of the db, the file system is actually in the way.

Ayende Rahien
01/05/2014 10:06 AM by
Ayende Rahien

Howard, I thought that I would do that using PROT_NONE

Ayende Rahien
01/05/2014 10:07 AM by
Ayende Rahien

Alexei, A RAM disk require the user to install a driver. That isn't usually acceptable for many users.

Ayende Rahien
01/05/2014 10:09 AM by
Ayende Rahien

Howard, We want this to enable specific scenarios for our users, namely quick in memory testing, and they usually don't have RAM disk setup.

In Voron we also have the notion of scratch space. This is used for things like in flight transaction data and during restore operations.

Don
01/05/2014 05:05 PM by
Don

Ayende, that's my point. By using raw fs you can bypass the file system no?

Howard Chu
01/05/2014 06:19 PM by
Howard Chu

Unfortunately, a lot of OSs lack support for memory map on raw devices.

Raymond
01/07/2014 05:05 AM by
Raymond

Not sure why you're so concerned about "memory mapped file still has to be allocated to disk." ALL memory is allocated to disk. That's why it's called virtual memory. If you are worried about memory being allocated to disk, then you shouldn't use VirtualAlloc either. That is also allocated from the page file.

Ayende Rahien
01/07/2014 11:02 AM by
Ayende Rahien

Raymond, You are missing something important. Virtual memory will be paged to disk as needed. But with mmap files, you have to first allocate the file on disk.

Raymond
01/07/2014 06:23 PM by
Raymond

If you pass INVALIDHANDLEVALUE to CreateFileMapping, then no file is created. It just creates virtual memory.

Raymond
01/07/2014 06:26 PM by
Raymond

As for the issue of growing: Just create a second shared memory block and map it immediately after the first one.

Ayende Rahien
01/09/2014 08:34 AM by
Ayende Rahien

Raymond, That is correct, but that doesn't mean that you can grow that. In effect, you can't increase the size of the mapping, nor can you re-map that section again in a different virtual address. You can hope that you can do another mapping directly after the existing one, but that isn't guaranteed. That is why we need to do reserve & commit.

Ayende Rahien
01/09/2014 09:19 AM by
Ayende Rahien

Raymond, You cannot guarantee that you'll be able to place the next piece of memory immediately after the first one.

JDT
01/09/2014 02:46 PM by
JDT

Copied from MSDN:

"Non-persisted memory-mapped files

Non-persisted files are memory-mapped files that are not associated with a file on a disk. When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. These files are suitable for creating shared memory for inter-process communications (IPC)."

This sounds like what you want?

Ayende Rahien
01/09/2014 03:31 PM by
Ayende Rahien

JDT, Yes, they aren't growable

Raymond
01/09/2014 06:42 PM by
Raymond

As you yourself noted, you don't have the guarantee of placability even if you had growable sections, so the inability to grow in place is no different whether you had growable sections or not And you can map the section to mutliple virtual addresses by calling MapViewOfFile multiple times. I concede that it is more cumbersome finding address space for two consecutive regions since there is no atomic "find me a bunch of contiguous space and map these two separate objects into it".

Ayende Rahien
01/10/2014 07:08 AM by
Ayende Rahien

Raymond, Yes, I know that I can do that. In fact, that is what I do. I grow the file, then I remap the whole size (from start to new end) in another MapViewOfFile. This gives me the behavior that I need, in the sense that both old and new memory are actually pointing in the same physical memory. However, that requires me to physically allocate the file on disk.

Comments have been closed on this topic.