clinuxmemorytmpfs

how does mincore work with shared tmpfs files


According to the man page,

mincore() returns a vector that indicates whether pages of the calling process's virtual memory are resident in core (RAM), and so will not cause a disk access (page fault) if referenced.

But, I'm unclear as to the exact meaning of this -- specifically does RAM refer to the target process's virtual memory, or any processes mapped memory?

Say you have a tmpfs file (tmpfs.file) that is shared between two process's -- process A and process B. Process A maps tmpfs.file, but only accesses page 0. Now process B accesses page 2. And no one accesses page 1. What would be the expected output of mincore as run by process A -- would it be [1,0,0], or [1,0,1]? Technically, page2 exists in process B's RAM, or on the system's RAM, but process A might not have a virtual memory mapping to it, so I'm not sure if this counts... (Tests show it does appear, but I'd like to be sure this is correct).


Solution

  • RAM means physical memory. RAM belongs to the ayarwm, there's no such thing as process A's RAM and process B's RAM.

    When the processes map the file, they each have 3 pages of virtual memory allocated to the mapping (I'm assuming the file size is 3 pages).

    When A accesses page 0, that page is fetched into RAM. At that time, mincore() should return [1, 0, 0] when run by either process.

    When B accesses page 2, that page is fetched into RAM. Assuming nothing causes page 0 to be ejected, mincore() will then return [1, 0, 1] in either process.

    It doesn't matter whether the process that calls mincore() has accessed a particular page. If some other process has accessed the page, it will be loaded into RAM, and this process won't get a page fault when trying to access it.

    Things can get weird when using virtual filesystems like tmpfs, since the filesystem itself can be stored in RAM -- there's nothing to read in from disk.