Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate page cache in Linux?

I want to know what files are cached in Page Cache, and want to free the cache space of a specific file pragmatically. It is possible for me to write kernel module or even modify the kernel code if needed. Can anyone give me some clues?

like image 421
stephenjy Avatar asked Feb 08 '11 13:02

stephenjy


People also ask

Is it safe to clear page cache in Linux?

You can drop cache as explained above without rebooting the System i.e., no downtime required. Linux is designed in such a way that it looks into the disk cache before looking onto the disk.

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.

How do I view cache in Linux?

Using the free -m command to check your Linux memory usage, displays the values as MB instead of KB. The free column beside -/+ buffers/cache with 823 MB is the actual free memory available to Linux. 1024 MB is the total system memory available, which would be physical RAM.


1 Answers

Firstly, the kernel does not maintain a master list of all files in the page cache, because it has no need for such information. Instead, given an inode you can look up the associated page cache pages, and vice-versa.

For each page cache struct page, page_mapping() will return the struct address_space that it belongs to. The host member of struct address_space identifies the owning struct inode, and from there you can get the inode number and device.

like image 176
caf Avatar answered Sep 30 '22 02:09

caf