how to traverse page cache tree (radix tree) of a

2019-06-21 12:55发布

问题:

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");                 
    }
}