Faster way to move memory page than mremap()?

后端 未结 2 1877
夕颜
夕颜 2021-02-02 13:50

I\'ve been experimenting with mremap(). I\'d like to be able to move virtual memory pages around at high speeds. At least higher speeds than copying them. I have some ideas for

相关标签:
2条回答
  • 2021-02-02 14:09

    It appears that there is no faster user-land mechanism to re-order memory pages than memcpy(). mremap() is far slower and therefore only useful for re-sizing an area of memory previously assigned using mmap().

    But page tables must be extremely fast I hear you saying! And it's possible for user-land to call kernel functions millions of times per second! The following references help explain why mremap() is so slow:

    "An Introduction to Intel Memory Management" is a nice introduction to the theory of memory page mapping.

    "Key concepts of Intel virtual memory" shows how it all works in more detail, in case you plan on writing your own OS :-)

    "Sharing Page Tables in the Linux Kernel" shows some of the difficult Linux memory page mapping architectural decisions and their effect on performance.

    Looking at all three references together then we can see that there has been little effort so far from kernel architects to expose memory page mapping to user-land in an efficient way. Even in the kernel, manipulation of the page table must be done by using up to three locks which will be slow.

    Going forwards, since the page table itself is made up of 4k pages, it may be possible to change the kernel so that particular page table pages are unique to a particular thread and can be assumed to have lock-less access for the duration of the process. This would facilitate very efficient manipulation of that particular page table page via user-land. But this moves outside the scope of the original question.

    0 讨论(0)
  • 2021-02-02 14:10

    What makes you think mremap could ever be efficient for swapping single 4k pages? At the very least, a round-trip to kernelspace even just to read a single value (like pid) and return it will cost more than moving 4k of data. And that's before we get to the cache invalidation/TLB costs of remapping memory, which I don't understand well enough to address in this answer, but which should have some serious cost.

    mremap is useful for basically one thing: implementing realloc for large allocations that were serviced by mmap. And by large, I mean probably at least 100k.

    0 讨论(0)
提交回复
热议问题