How do I know if a pointer is in physical memory or is it causing a page error?

If I have a pointer and I care about memory access performance, I can check if the next operation will cause it to cause a page error. If so, the algorithm can be designed so that it realigns loop operations to minimize page errors.

Is there any portable (or linux / windows non-portable) way to check for a specific memory address, will access cause a page error?

+6
source share
3 answers

Not. There is no portable way to check if a given address is currently in physical memory or replaced in a swap file. In fact, I do not think that Linux or Windows offer the ability to test this in a non-portable way. (Of course, on Linux you could write it yourself).

As noted in the comments, you also want to check whether the data is in the cache or not (access from physical memory is much slower than from the cache).

It’s best to reorder the loop to minimize page errors (== maximizes link locality) anyway.

+3
source

About ten years ago, Emery Berger proposed a VM-friendly garbage collection strategy that required the application to know which pages were in memory. For testing purposes, he and his students released a kernel patch that notified the swap event application using real-time signals, allowing the garbage collector to save its own database of resident pages. (Although this seems like duplication of effort, it is much more efficient than several system calls to get information every time it is needed.)

You can find information about this interesting study on the page.

As far as I know, there is no implementation of this patch for the latest Linux kernel, but you could always resurrect it.

+6
source

Linux has a mechanism, see man proc :

/[pid]/pagemap This file displays the mapping of each of the virtual page processes to the physical page borders or swap space. It contains one 64-bit value for each virtual page with the bits set as follows:

  • 63 If ​​set, the page is present in RAM.
  • 62 If set, the page is in the swap space
  • ...

For instance,

 $ sudo hexdump -e '/0 "%08_ax "' -e '/8 "%016X" "\n"' /proc/self/pagemap 00000000 0600000000000000 * 00002000 A6000000000032FE 00002008 A60000000007F3A6 00002010 A600000000094560 00002018 A60000000008D0C0 00002020 A60000000009EBE6 00002028 A6000000000C8E87 
0
source

All Articles