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?
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.
open()
syscalls, and make a record of all opened files, and then scan through all these files to see if one is cached. In this way, I can get rid of scanning the entire filesystem. Will it do? –
Biographer
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. The kernel returns residency information about the pages starting at the address addr, and continuing for length bytes.
To test whether a file currently mapped into your process is in cache, call mincore
with its mapped address.
To test whether an arbitrary file is in cache, open and map it, then follow the above.
There is a proposed fincore()
system call which would not require mapping the file first, but (at this point in time) it's not yet generally available.
(And then madvise(MADV_DONTNEED)
/fadvise(FADV_DONTNEED)
can drop parts of a mapping/file from cache.)
fincore()
, this helps me solve my another problem. By the way, fincore()
is not a system call, but a tool provided by linux-ftools, it uses mmap()
and mincore()
to determine which pages of the file are in pagecache, and use posix_fadvise()
with POSIX_FADV_DONTNEED
to drop parts of a mapping/file from cache. –
Biographer fincore
syscall, even though its results could be obtained in userland by using multiple existing syscalls. Looks like linux-ftools
does just that. –
Beitris You can free the contents of a file from the page cache under Linux by using
posix_fadvise(fd, POSIX_FADV_DONTNEED
As of Linux 2.6 this will immediately get rid of the parts of the page cache which are caching the given file or part of file; the call blocks until the operation is complete, but that behaviour is not guaranteed by posix.
Note that it won't have any effect if the pages have been modified, in that case you want to do a fdatasync or such like first.
EDIT: Sorry, I didn't fully read your question. I don't know how to tell which files are currently in the page cache. Sorry.
posix_fadvise()
solves my another problem :) –
Biographer © 2022 - 2024 — McMap. All rights reserved.
struct inode
? If there are several filenames point to the same inode, any one will be OK. – Biographer