pub fn readahead(
fd: impl AsRawFd,
offset: u64,
len: usize,
) -> Result<(), CoreError>Expand description
Advise the kernel to begin reading file data into the page cache.
This is an advisory hint only. It can help warm likely-needed file ranges, but the kernel may ignore the request, perform only part of it, or return before the data is fully resident in memory.
The offset and len identify the byte range to prefetch for fd.
Success means the kernel accepted the request, not that subsequent reads
are guaranteed to be cache hits.
Examples found in repository?
examples/readahead_file.rs (line 12)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let path = std::env::args()
7 .nth(1)
8 .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "usage: readahead_file <path>"))?;
9 let file = File::open(&path)?;
10 let len = file.metadata()?.len().min(128 * 1024) as usize;
11
12 readahead(file, 0, len)?;
13 println!("Requested readahead for {} bytes from {}", len, path);
14
15 Ok(())
16}More examples
examples/readahead_bench.rs (line 24)
15fn main() -> Result<(), Box<dyn std::error::Error>> {
16 let path = std::env::args()
17 .nth(1)
18 .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "usage: readahead_bench <path>"))?;
19
20 let mut file = File::open(&path)?;
21 let len = file.metadata()?.len().min(8 * 1024 * 1024) as usize;
22
23 let start = Instant::now();
24 readahead(RawFdRef(file.as_raw_fd()), 0, len)?;
25 let readahead_elapsed = start.elapsed();
26
27 let mut buf = vec![0u8; len];
28 let start = Instant::now();
29 let read_len = file.read(&mut buf)?;
30 let read_elapsed = start.elapsed();
31
32 println!("readahead({} bytes): {:?}", len, readahead_elapsed);
33 println!("read({} bytes): {:?}", read_len, read_elapsed);
34
35 Ok(())
36}