how to traverse page cache tree (radix tree) of a file address space in linux kernel

心不动则不痛 提交于 2019-12-12 10:42:39

问题


I need to get page-cache statistics of an open file. There is a address_space pointer(f_mapping) in file struct which in turn has the root of the radix tree called page_tree. I need to traverse that tree to get information about all the cached pages for that open file.

There are some functions like radix_tree_for_each_chunk(to iterate over chunks), radix_tree_for_each_chunk_slot (to iterate over slots in one chunk) etc, using these the functionality can be achieved. I am unsure about the proper use (arguments) of the same. It would be helpful if any example is posted.


回答1:


I figured it out from Linux kernel source code.

struct file *file = filp_open("filename",O_RDONLY,0);
struct address_space *file_addr_space = file->f_mapping;            
if(file_addr_space==NULL){
    printk("error")
}           
struct radix_tree_root file_page_tree_root  = file_addr_space->page_tree;   //contains all pages in page cache                                      
struct radix_tree_iter iter;            
void **slot;            
int num_dirty = 0;
radix_tree_for_each_slot(slot,&file_page_tree_root,&iter,0){
    struct page *page = radix_tree_deref_slot(slot);
    if(page!=NULL){
        //printk("information about page");                 
    }
}


来源:https://stackoverflow.com/questions/29848182/how-to-traverse-page-cache-tree-radix-tree-of-a-file-address-space-in-linux-ke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!