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?
相关问题
- What uses more memory in c++? An 2 ints or 2 funct
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Memory for python.exe on Windows 7 python 32 - Num
- Error building gcc 4.8.3 from source: libstdc++.so
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 thestruct address_space
that it belongs to. Thehost
member ofstruct address_space
identifies the owningstruct inode
, and from there you can get the inode number and device.You can free the contents of a file from the page cache under Linux by using
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.
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.)