2

Currently working on arm Xilinx development platforms. Using linux and C.

I mmap some pages. e.g. a = mmap(...) . I want to know which pages are the least recently used so i can copy them somewhere else.

  • 9
    [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? **Why** do you need to know? Who accesses them? Simple answer: You can't, unless you wrap those accesses in functions everyone else uses. –  Nov 06 '17 at 12:51
  • I'm writing a lib, and I want to know which pages are least recently used, so i can copy them, elsewhere. – Emmanouil Skordalakis Nov 06 '17 at 12:54
  • 2
    A library can't do that, except when it wraps all accesses in functions that callers **must** use (which would be **very** inconvenient). A *kernel* maybe could do something like that. –  Nov 06 '17 at 12:56
  • 5
    You can be made aware when the pages are accessed: Set the access rights to `PROT_NONE` (with mprotect) and install a segv-handler, which will be called any time a variable is accessed. This, however, of course results in a significant performance impact if variables are accessed frequently. – Ctx Nov 06 '17 at 13:12
  • either you insert a watchpoint in debugger, or insert a profiler instrumentation or simply put log info. – alinsoar Nov 06 '17 at 13:21
  • @FelixPalmen. I edited the question since it was an XY. – Emmanouil Skordalakis Nov 06 '17 at 13:28
  • @Ctx It's true that this will drastically reduce the performance so I can't really use it. – Emmanouil Skordalakis Nov 06 '17 at 13:28
  • @alinsoar. The thing is, how do I update the log, since I don't know what is accessed when. – Emmanouil Skordalakis Nov 06 '17 at 13:28

2 Answers2

3

The linux kernel itself keeps track of LRU pages, for example, to find out which pages to swap or not to swap out on memory contention.

You can read the LRU flag from userspace if you want to use it in your library:

  • Iterate over the virtual page entries you want to track in /proc/pid/pagemap and remember the page frame numbers
  • read the flags corresponding to these pages from /proc/kpageflags

Now you have several flags, which you can use for your logic:

 3. UPTODATE  page has up-to-date data
              ie. for file backed page: (in-memory data revision >= on-disk one)
 4. DIRTY     page has been written to, hence contains new data
              ie. for file backed page: (in-memory data revision >  on-disk one)
 8. WRITEBACK page is being synced to disk

    [LRU related page flags]
 5. LRU         page is in one of the LRU lists
 6. ACTIVE      page is in the active LRU list
18. UNEVICTABLE page is in the unevictable (non-)LRU list
                It is somehow pinned and not a candidate for LRU page reclaims,
        eg. ramfs pages, shmctl(SHM_LOCK) and mlock() memory segments
 2. REFERENCED  page has been referenced since last LRU list enqueue/requeue
 9. RECLAIM     page will be reclaimed soon after its pageout IO completed
11. MMAP        a memory mapped page
12. ANON        a memory mapped page that is not part of a file
13. SWAPCACHE   page is mapped to swap space, ie. has an associated swap entry
14. SWAPBACKED  page is backed by swap/RAM

Some more flags are described in this document

There is also a feature called soft-dirty, which is especially intended for tracking recent writes (reads are ignored) (see the Soft Dirty documentation). You can clear the soft-dirty flags by /proc and read them again through /proc/pid/pagemap. Maybe this comes in handy, too, for your application.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • Awesome. Took me a while to understand how to parse those proc entries. The link below help me understand the parsing. Thanks a lot. https://stackoverflow.com/questions/17021214/how-to-decode-proc-pid-pagemap-entries-in-linux – Emmanouil Skordalakis Nov 07 '17 at 16:25
0

Have you considered using vmstat(8) Report virtual memory statistics ?

vmstat reports information about processes, memory, paging, block IO, traps, and cpu activity. (emphasis mine).

Including:

Field Description For Slab Mode

cache: Cache name num: Number of currently active objects total: Total number of available objects size: Size of each object pages: Number of pages with at least one active object totpages: Total number of allocated pages pslab: Number of pages per slab

Here is a Linux Journal article going into more detail how vmstat can be used to monitor OS statistics. Here is an excerpt:

Using vmstat

vmstat, as its name suggests, reports virtual memory statistics. It shows how much virtual memory there is, how much is free and paging activity. Most important, you can observe page-ins and page-outs as they happen. This is extremely useful.

To monitor the virtual memory activity on your system, it's best to use vmstat with a delay. A delay is the number of seconds between updates. If you don't supply a delay, vmstat reports the averages since the last boot and quit. Five seconds is the recommended delay interval.

To run vmstat with a five-second delay, type:

vmstat 5

ryyker
  • 22,849
  • 3
  • 43
  • 87