94 points · by nkurz · 15y ago · 25 comments
"mmap() interface is missing a non-blocking way to access memory."
At the end of the day your process will still be suspended when the page is brought in from disk the first time it is read. In some situations, when you can't afford your thread to be suspended waiting for disk IO (say if you could do something else in the meantime, like service network requests) it would be better to use AIO for example to schedule a file read into a buffer and be notified later when it is finished ( http://linux.die.net/man/3/aio_read )
It also seems silly to go through all of this, but end up using a thread pool to perform the page faults, which brings back the issues of thread starvation, context switches, and lock overhead which cooperative concurrency models try to avoid.
I'm trying to think thru all the scenarios, but it doesn't seem as if it's really practically possible (in C at least) to make mmap() I/O work in a cooperative concurrency model.
I'd say that generally, when the kernel can do something such that a cooperating VM can get a speed boost without doing very much, that's a good thing, all else being equal. Which it never is, when it comes to the kernel. But it's worth thinking about, even if it's too complicated for C to use reasonably. (It's time to let that bottleneck go anyhow. There's all kinds of very good things that are simply too complicated to reasonably use in C, and the solution is not to limit ourselves to C for the rest of eternity.)
I'd expect the limited address space on 32-bit processors to be a bigger issue than the complexity.
For many applications this is not the right way to go, but for completely predictable access patterns on a machine that is devoted to it, it can save the day.
Given the awesome performance of Varnish, I am surprised more applications haven't taken this approach.
Localmemcache (key-value database): http://localmemcache.rubyforge.org/
clispy (Port of PNorvig's lispy to C): https://github.com/sck/clispy
On Windows it seems like the best way to handle these errors is by either embedding SEH into the surrounding code or adding a vectored exception handler globally. On Unix, you have to set a SIGBUS signal handler. But then, mmap is apparently not guaranteed to be async-signal-safe if you want to remap a zero page over the broken one, and longjmp out of a signal handler is its own pile of potatoes; both seem to work on various modern Unixoids, but I haven't been able to find documentation saying that they'll continue to work. And with longjmp, or on Windows (where you can't remap pages over other pages directly, that I know of), any surrounding code that accesses the map needs to be abortable all the way up to a suitable error point rather than just having to handle bogus values. Much code assumes that a simple memory access will not cause a recoverable exception that may result in reëntering the code later.
And if you're in a library on Unix, good luck getting permission from the main process to alter signal handlers. The hook mechanism isn't as rich as that in Windows, so with the exception of large application framework libraries that are expected to take over the process anyway, it's an invasive and possibly irreversible activity.
This is all sad, because I love the idea of mmap. I was tinkering with a C library for accessing certain kinds of files, and I want to do it with all mmap, but I'm not sure I can overcome the I/O error problem adequately. (The blocking and address space problems are not too bad here; they impact performance and capacity, but not correctness.)
It's also not a hard rule that the OS cache is always better. It's probably very good at block-level caching, so don't rewrite that, but that's not very fine grained. The OS can't collect much more than some basic access statistics and madvise()s to figure out what to keep resident. It's kind of dishonest to make it seem like "advanced databases" like PostgreSQL should abandon their own caches entirely. In fact, most PostgreSQL tuners suggest that only a fraction of available RAM should be used as a buffer cache, and that it's prudent to just let the OS manage most of it. The query planner can even be advised as to how much effective cache space is available, including the OS disk cache.
So when epoll doesn't cope with regular files as author expected, why not use better interface: kqueue?
A bigger obstacle is that not all file systems support asynchronous I/O, the io_*() syscalls won't help you there.