Commit ec0f1637 authored by Jan Kara's avatar Jan Kara Committed by Linus Torvalds

readahead: improve heuristic detecting sequential reads

Introduce ra.offset and store in it an offset where the previous read
ended.  This way we can detect whether reads are really sequential (and
thus we should not mark the page as accessed repeatedly) or whether they
are random and just happen to be in the same page (and the page should
really be marked accessed again).
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Acked-by: default avatarNick Piggin <nickpiggin@yahoo.com.au>
Cc: WU Fengguang <wfg@mail.ustc.edu.cn>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent b813e931
...@@ -702,6 +702,7 @@ struct file_ra_state { ...@@ -702,6 +702,7 @@ struct file_ra_state {
unsigned long ra_pages; /* Maximum readahead window */ unsigned long ra_pages; /* Maximum readahead window */
unsigned long mmap_hit; /* Cache hit stat for mmap accesses */ unsigned long mmap_hit; /* Cache hit stat for mmap accesses */
unsigned long mmap_miss; /* Cache miss stat for mmap accesses */ unsigned long mmap_miss; /* Cache miss stat for mmap accesses */
unsigned int offset; /* Offset where last read() ended in a page */
}; };
#define RA_FLAG_MISS 0x01 /* a cache miss occured against this file */ #define RA_FLAG_MISS 0x01 /* a cache miss occured against this file */
#define RA_FLAG_INCACHE 0x02 /* file is already in cache */ #define RA_FLAG_INCACHE 0x02 /* file is already in cache */
......
...@@ -868,6 +868,7 @@ void do_generic_mapping_read(struct address_space *mapping, ...@@ -868,6 +868,7 @@ void do_generic_mapping_read(struct address_space *mapping,
unsigned long last_index; unsigned long last_index;
unsigned long next_index; unsigned long next_index;
unsigned long prev_index; unsigned long prev_index;
unsigned int prev_offset;
loff_t isize; loff_t isize;
struct page *cached_page; struct page *cached_page;
int error; int error;
...@@ -877,6 +878,7 @@ void do_generic_mapping_read(struct address_space *mapping, ...@@ -877,6 +878,7 @@ void do_generic_mapping_read(struct address_space *mapping,
index = *ppos >> PAGE_CACHE_SHIFT; index = *ppos >> PAGE_CACHE_SHIFT;
next_index = index; next_index = index;
prev_index = ra.prev_page; prev_index = ra.prev_page;
prev_offset = ra.offset;
last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT; last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
offset = *ppos & ~PAGE_CACHE_MASK; offset = *ppos & ~PAGE_CACHE_MASK;
...@@ -924,10 +926,10 @@ page_ok: ...@@ -924,10 +926,10 @@ page_ok:
flush_dcache_page(page); flush_dcache_page(page);
/* /*
* When (part of) the same page is read multiple times * When a sequential read accesses a page several times,
* in succession, only mark it as accessed the first time. * only mark it as accessed the first time.
*/ */
if (prev_index != index) if (prev_index != index || offset != prev_offset)
mark_page_accessed(page); mark_page_accessed(page);
prev_index = index; prev_index = index;
...@@ -945,6 +947,7 @@ page_ok: ...@@ -945,6 +947,7 @@ page_ok:
offset += ret; offset += ret;
index += offset >> PAGE_CACHE_SHIFT; index += offset >> PAGE_CACHE_SHIFT;
offset &= ~PAGE_CACHE_MASK; offset &= ~PAGE_CACHE_MASK;
prev_offset = ra.offset = offset;
page_cache_release(page); page_cache_release(page);
if (ret == nr && desc->count) if (ret == nr && desc->count)
......
...@@ -207,6 +207,8 @@ out: ...@@ -207,6 +207,8 @@ out:
* If page_cache_readahead sees that it is again being called for * If page_cache_readahead sees that it is again being called for
* a page which it just looked at, it can return immediately without * a page which it just looked at, it can return immediately without
* making any state changes. * making any state changes.
* offset: Offset in the prev_page where the last read ended - used for
* detection of sequential file reading.
* ahead_start, * ahead_start,
* ahead_size: Together, these form the "ahead window". * ahead_size: Together, these form the "ahead window".
* ra_pages: The externally controlled max readahead for this fd. * ra_pages: The externally controlled max readahead for this fd.
...@@ -473,6 +475,7 @@ page_cache_readahead(struct address_space *mapping, struct file_ra_state *ra, ...@@ -473,6 +475,7 @@ page_cache_readahead(struct address_space *mapping, struct file_ra_state *ra,
/* Note that prev_page == -1 if it is a first read */ /* Note that prev_page == -1 if it is a first read */
sequential = (offset == ra->prev_page + 1); sequential = (offset == ra->prev_page + 1);
ra->prev_page = offset; ra->prev_page = offset;
ra->offset = 0;
max = get_max_readahead(ra); max = get_max_readahead(ra);
newsize = min(req_size, max); newsize = min(req_size, max);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment