7 ms·
Why mmap is faster than system calls (2019)
- waynesonfire 6y agoThat was insightful and well written. TLDR author suggests mmap's improved performance can be attributed to the use of AVX instructions during memory copy.
- vlovich123 6y agoFantastic article! Could the kernel restrict the AVX use to memcpy to/from user buffers & to use a different API so that it was clear at the syscall boundary when that save/restore would be needed (+ you could potentially restrict the number of AVX registers used)? That way it wouldn't have the concerns of replacing all memcpy operations within the kernel & would just focus on I/O operations from userspace. Or is the space for potentially stashing away the AVX registers for each thread also a major concern rather than just the cost of doing so?
- correct_horse 6y agoCall me pedantic, but mmap is a syscall. The title would have been better as "Why memory mapped files are faster". The author does address this with "Mmap stands for memory-mapped files." It does? I thought mmap stands for memory map. To be clear I enjoyed the contents of the article.
- dyingkneepad 6y agoBut you call mmap only once per file: it gives you a pointer and then you just use the pointer normally. While for read() you have to call once per read() operation (minus some user space buffering).
- correct_horse 6y agommap is more general than memory mapping files. It "establish[es] a mapping between an address space of a process and a memory object". In particular, many allocators (malloc and friends) use mmap to get a block of memory from the OS with mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS, -1, 0); or similar.
- mhh__ 6y agoDo any allocators use anything else these days? D's std.experimental.allocator module is helpful for building allocators bit by bit even for eventual use in C++ or C.
- JoshTriplett 6y ago> Do any allocators use anything else these days? Yes, some allocators still support using sbrk, though that's a bad idea for security reasons (ASLR).
- jashmatthews 6y agoIIUC, glibc malloc still manages one arena with sbrk and then grabs other arenas via mmap.
- morei 6y agoYes, the more accurate title would be "Why is it faster to read data from a file via mmap() than via the read() system call?"
- inopinatus 6y ago> Call me pedantic Surely persnickety, no? If we're wordsmithing anyway.
- chrisseaton 6y agoWhat's the difference between the two words?
- bch 6y ago3
- xeromal 6y agoPersnickety is more fun
- inopinatus 6y agoThe wale of one's trousers.
- chrisseaton 6y ago> Call me pedantic, but mmap is a syscall. You're misunderstanding. You call mmap once, which is a system call, and then instead of using system calls you just access memory which performs the operations as needed through loading on page faults, not through system calls. This compares to using system calls all the time.
- rowanG077 6y agoCopying blocks of memory is so common. Why isn't there a specific instruction for that in cpu architectures?
- vhold 6y agoI can't exactly answer that question because I'm not even sure if the premise is true, but I do know that "memory to memory" copying is often offloaded entirely off the CPU. Here's a technical overview of some of the technologies from 2017 https://www2.eecs.berkeley.edu/Pubs/TechRpts/2017/EECS-2017-2.pdf https://www2.eecs.berkeley.edu/Pubs/TechRpts/2017/EECS-2017-...
- jchw 6y agoThere are, kind of. For example x86 traditionally has `rep movsb`. However that doesn’t mean it will remain the fastest way to do things in the long term; AVX/SSE lets you copy more at once with the caveat that it is more complicated to use (due to alignment requirements and register usage.) (I think certain ‘obvious’ patterns are optimized in microcode, but it’s probably hard to beat moving 32 bytes or 64 bytes at once with moving bytes/words at a time, no matter what the processor does.)
- JoshTriplett 6y ago> For example x86 traditionally has `rep movsb`. However that doesn’t mean it will remain the fastest way to do things in the long term Starting with Ice Lake, you can just use `rep movsb` all the time, and the processor will move memory in the fastest way it can. The CPUID feature "ERMS", "Enhanced REP MOVSB", means you should use `rep movsb` for any memory copy that's at least 128 bytes. The CPUID feature "FSRM", "Fast Short REP MOVSB", implies ERMS and additionally means that you should use `rep movsb` for any memory copy, even if it's shorter than 128 bytes.
- twoodfin 6y agoNeat! Have any of the common libc’s picked this up yet? How does ERMS deal with interrupts? If I `rep movsb` while interrupted, do I also get the fast implementation?
- brandmeyer 6y agoAnd yet, grep and grep alternatives run much faster with the sequential file I/O tools than mmap. https://blog.burntsushi.net/ripgrep/ https://blog.burntsushi.net/ripgrep/
- deleted 6y ago[deleted]
- cb321 6y agoDiscussion of this exact issue over at https://lobste.rs/s/h5dfjo/why_mmap_is_faster_than_system_calls https://lobste.rs/s/h5dfjo/why_mmap_is_faster_than_system_ca...
- burntsushi 6y agoThat's not quite true... My blog post there says mmap is slower in some cases (where you're searching a lot of files), but if you're just searching a single file, mmap does give a bit of a speed boost: $ time rg 'Sherlock Holmes' OpenSubtitles2018.raw.en -c --mmap 7673 real 1.754 user 1.235 sys 0.516 maxmem 12510 MB faults 0 $ time rg 'Sherlock Holmes' OpenSubtitles2018.raw.en -c --no-mmap 7673 real 2.663 user 1.130 sys 1.530 maxmem 6 MB faults 0 See the sibling comment for a link to a bit more discussion and some interesting observations! Namely, it looks like using mmap can be slower when searching a lot of files because of CPU cache effects.
- labawi 6y ago> When a user program requests to read a file, the page from the file is (usually) first put into the buffer cache. Then the data is copied from the buffer cache out to the user-supplied buffer during the return from the system call. Doesn't linux do shared-read-only mapping of pages on read(2) and only copy if you modify? Though it can only do so on page-aligned buffers. Maybe results would be different if the buffers were aligned? > So I changed my test to invoke mmap with MAP_POPULATE and learned that the experiment completes about 36% faster. If your result spuriously change by 36%, you don't really have a robust statistic, and a model/understaing of objects measured. AFAIU, mmap with MAP_POPULATE should be equivalent to a read of whole file in a single call, on an aligned buffer - significant performance differences here would be interesting.
- kentonv 6y ago> Doesn't linux do shared-read-only mapping of pages on read(2) and only copy if you modify? Eh? Do you have a reference for this? I'd be pretty interested to learn more. This sounds like a very weird optimization to me, because, as you point out, it would only work if your buffer (and file offset) was page-aligned. That doesn't happen by accident, so "normal" programs that use read() would almost never get a benefit. To get guaranteed page alignment, you probably have to use mmap() to allocate the buffer in the first place, so... why wouldn't you just mmap the file, then? In fact, you could arguably translate a page-aligned pread() into mmap() in userspace, using MAP_FIXED to force the mapping to be placed at your buffer address. No need for kernel help? So why would a kernel implement this optimization? It seems like a lot of risk (of unexpected side effects) with little potential benefit.
- skbpush 6y agoA small jumping point that you can start from. https://en.wikipedia.org/wiki/Copy-on-write https://en.wikipedia.org/wiki/Copy-on-write The only reason I remember is from first encountering this in code and wondering what the hell the "COW" flag was. >To get guaranteed page alignment, you probably have to use mmap() Most modern implementations of malloc will call mmap in the background.
- dan-robertson 6y agoIt’s a bit surprising to me that modern processors don’t have their own special handling for recognising and speeding up these copies. But maybe that’s too hard to do. I’m also surprised that Linux can’t use a vector based copy function (at least for any moderately large copy) as it seems saving a few avx registers would be much cheaper than the copy.
- twoodfin 6y agoModern processors speed up large copies with SIMD instructions like AVX. The Linux kernel chooses not to use SIMD because the cost of copying & saving the relevant processor state (registers, in this case) would be borne by every syscall and consume precious CPU cache. This would be true for any similar copying mechanism that spanned multiple cycles; the state—however represented—must be preserved.
- dan-robertson 6y agoThat’s a statement about modern implementations of memcpy, whereas I was asking about what processors themselves do when they see a non-vector memcpy loop.
- loeg 6y agoThey do; 'rep movsb' uses very wide operations internally on any recent x86. https://news.ycombinator.com/item?id=24843364 https://news.ycombinator.com/item?id=24843364
- burntsushi 6y agoI had a good exchange about this concerning ripgrep over on lobste.rs: https://lobste.rs/s/h5dfjo/why_mmap_is_faster_than_system_calls#c_sladeu https://lobste.rs/s/h5dfjo/why_mmap_is_faster_than_system_ca... Hah, this conversation has already been linked in other comments.
- scottlamb 6y agoI bet if this article were written in 2020, they'd throw io_uring into the comparison. Does it allow directly returning the page cache memory without kernel copying also? I expect so (since it's a modern, performance-oriented interface) but don't know the details of how the buffer management works—seems like the kernel would have to supply the buffer then, it'd have to be at least page size, etc. If so, it should have a similar benefit as what this article describes for mmap: either your program can completely avoid copying or it can do so in userspace with SIMD instructions.
- scottlamb 6y agoAlso, even when io_uring does copying in kernelspace, I wonder if it'd be more practical for it to use AVX instructions than from within a read syscall. As I understand it, userspace<->kernel transitions are lighter-weight than full context switches between processes. As discussed here: not saving/restoring the SIMD registers. (I think a bunch of other ways, too, like not replacing all the memory mappings.) There are also a bunch of pure-kernel processes (I don't know the terminology) shown on ps in square brackets, like [kthreadd], [rcu_gp], [rcu_par_gp], etc. I think a full context switch happens on entry/exit to one of these, just as between two different userspace processes. If the io_uring work always happens within one of these (does it?), the SIMD registers are already being saved/restored, and it'd be safe for it to use an avx_memcpy variant without paying any additional cost.
- Const-me 6y agoGood article. However, it would be interesting to compare these two things against better system calls. > the data referred to by these pointers must be copied into the kernel Modern OS kernels avoid that when they can. The article is about Linux, modern ones have io_uring: https://unixism.net/loti/what_is_io_uring.html https://unixism.net/loti/what_is_io_uring.html In short, that thing allows to setup these pointers in advance, and make the memory visible to both user-mode code and OS kernel at the same time. This way no need to copy the data. Unfortunately, implementing that thing is way more complicated than calling read() or setting up memory mapping. For optimal performance you gonna need to zero-copy all the way up to whatever code is consuming the data read from these files.
- tinus_hn 6y agoHow do they avoid race conditions if the kernel can use these pointers while userspace can modify them at the same time?
- Const-me 6y agoThe common pattern with any I/O, race conditions are avoided by writing in the documentation "please don't modify the memory after the I/O is initiated and before it's completed". Enforcing that would cause too much overhead.
- tinus_hn 6y agoWouldn’t that violate the rule that userspace can’t crash the kernel? On Windows this is solved by using the memory protection provided by the cpu and Structured Exception Handling; I am curious if Linux has something similar.
- labawi 6y agoThe kernel likely keeps it's own copies of necessary validated commands and pointers, but not of data buffers. If you read/write in an inappropriate time, you are likely to read/write garbage, get misinterpreted commands or segfaults, but that's your problem. It shouldn't affect the kernel, aside from bugs present.
- eloff 6y agoDid anyone else notice that read kept increasing substantially in speed with larger buffer sizes? Like if the trend continues it would be about equal with mmap at 64kb buffer size. No way to know for sure without testing it, but that really jumped out at me.
- throwaway373438 6y agoYes, the overhead is per-syscall. The number of read() syscalls shrinks as the per-call buffer grows. With mmap() of course we only ever have one syscall to create the initial mapping. Everything else is a memory read. We can get read() down to just one syscall too, with a 4G buffer ;) I can't recall if GB sized buffers are possible, but I have certainly used MB sized read buffers for exactly this reason.
- loeg 6y agoOn Linux, read() (and similar system calls) will transfer at most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.)
- eloff 6y agoYes, of course. What I mean to say is that it's weird the author did not see that trend and test it. There wouldn't have been an article if the result was "mmap is faster than read using buffer size < 64kb". It seems kind of central to their whole thesis.
- throwaway373438 6y agoAh, I see your point. Here's the catch: Large buffer sizes only increase efficiency for sequential reads. mmap() is still much faster for random access within a file. Doubly so, because we need to not just read() but also lseek() for every read. The inefficiencies of read() can be minimized in the sequential case only.
- returnfalse 6y agoNice article. Could someone suggest me a book that explains this stuff further?
- hendry 6y agoMUCH prefer to see system calls since strace becomes actually useful and readable. IIRC this is why plan9 is nice to work with. You can actually understand what a program is doing. On a typical Linux system, mmaps galore and you have no idea what's happening. Pretty awful situation.
- nitrogen 6y agoIs there a way to trace page faults in a program?
- hendry 6y agoSomething like https://aur.archlinux.org/packages/electricfence/ https://aur.archlinux.org/packages/electricfence/ or do I misunderstand you?
- tonyarkles 6y agoWhen you mmap, it gives you back a pointer that you can read from, but doesn’t necessarily populate your process’s page table with all of the data you can read. Instead, when you go to access it, there will be a page fault if the page you’re trying to read hasn’t been read from disk yet. The kernel catches the page fault, allocates a page, does the read from disk, and restarts your read as if nothing happened. The user process has no indication that any of this happened other than perhaps a bit of latency.
- nitrogen 6y agoI'm curious now if electricfence could be configured to detect mmap faults, since it's described as using the virtual memory hardware for malloc debugging. I haven't used -lefence since well before I learned about Valgrind though.
- tonyarkles 6y agoI’m curious now too!
- malkia 6y agoAnd then again, for databases trying to rely on mmap might lead you to poorer results - for example trying to do in-place update, ignoring compression, dealing with concurrency, etc. Without mmap, and by using append-only data, no updates on exising ones (except metadata), and the ability to do compression might give you a big edge over just trying to work it out through mmap. Check WiredTiger's articles/videos on how they've found previous mmap approach for mongodb to be much slower (I'm not saying mmap is slower than os calls reads, I'm saying it leads to architectural decisions that might limit you later.) (edit) This actually presents it better than above - https://15721.courses.cs.cmu.edu/spring2019/slides/01-inmemory.pdf https://15721.courses.cs.cmu.edu/spring2019/slides/01-inmemo...
- halfer53 6y agoWhy can AVX be used during page fault but not during syscall mode?