Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Page Cache - Deleting a page from the page cache in kernel

Tags:

My question is an extension of this one How to manipulate page cache in Linux?

I was trying to do a small project that aims to limit the size of page cache used on a per file basis. The approach I used was as follows -

  1. Maintain a kfifo queue of page pointers as they are added to the page cache.
  2. Add a hook in add_to_page_cache_lru() and see if the size of the radix tree (the address_space) of a file is more than a pre-determined size then choose a victim from the fifo queue and delete the page from page cache.
  3. I used the functions delete_from_page_cache() and try_to_unmap() to evict the page from the page cache, followed by put_page() to release the page.

I expect this code to free the pages and release the memory but that doesn't seem to happen. For example, if I read a file of size 25MB and I've restricted the size of page cache for this file to be 512 pages (2MB), then I expect to see a change of only 2MB in the free memory (free -m). What I see instead is that the full 25MB is eaten up and shows up in the free command.

What more should I do to ensure that my requirements are fulfilled? I've not thought about dirty pages yet as I couldn't even make it work for reads (cat the file). Any pointers would be helpful.

P.S. - I'm using linux 4.0 for this project.

like image 878
Singh Avatar asked May 02 '15 05:05

Singh


People also ask

How does page cache work in Linux?

Under Linux, the Page Cache accelerates many accesses to files on non volatile storage. This happens because, when it first reads from or writes to data media like hard drives, Linux also stores data in unused areas of memory, which acts as a cache.

What is stored in page cache?

The page cache is the main disk cache used by the Linux kernel. In most cases, the kernel refers to the page cache when reading from or writing to disk. New pages are added to the page cache to satisfy User Mode processes's read requests.

Is it safe to clear page cache in Linux?

Risks from clearing page cacheClearing cache will free RAM, but it causes the kernel to look for files on the disk, rather than in the cache. This can cause performance issues. Normally, the kernel will clear the cache when the available RAM is depleted. It frequently writes dirtied content to the disk using pdflush.

Is inode cached in page cache?

A page does not necessarily contain physically adjacent disk blocks, and it cannot thus be identified by a device number and a block number. Instead, a page in the page cache is identified by a file's inode and by the offset within the file.


1 Answers

You might have to do way more than just delete_from_page_cache() + try_to_unmap() + put_page()...

See how shrink_page_list() behaves, including the checks for page_check_references():

More here: How to unmap struct page from all PTEs mapping it

like image 188
Vlad Avatar answered Oct 04 '22 11:10

Vlad