[PATCH/RFC] readahead applet

Michael Opdenacker michael-lists at free-electrons.com
Mon Aug 21 16:50:55 UTC 2006


Rich Felker wrote:
>> Sounds useful to me.
>>     
>
> It does? Anyone care to explain how this is different/better than:
>
> 'nice -n 20 cat "$file" > /dev/null &'
>   
Hi Rich,

Well, using the "cat" command generated a great deal of system calls, 
typically reading the file block by block, and writing to /dev/null 
block by block too.

With our new "readahead" command, everything is done in one system call. 
So, almost everything is done in kernel mode, possibly in an optimized 
way between the filesystem and page cache. And of course, no buffer copy 
from kernel-space to user-space is done!

To prove this I ran "time readahead" and "time cat" on 2 identical files.

time readahead file

real    0m1.375s
user    0m0.000s
sys     0m0.028s

time cat file-copy > /dev/null

real    0m1.485s
user    0m0.012s
sys     0m0.080s

Forget about the "real" line. Other jobs could be running in parallel. 
You can see that readahead spends a negligible amount of time in 
user-space, unlike cat. You can also see that 1 system call (readahead) 
is much more efficient than many (cat).

"readahead" is also much more efficient when the file is already in the 
page cache. I ran the same command twice:

time readahead file (second time)
real    0m0.003s
user    0m0.000s
sys     0m0.004s

time cat file-copy > /dev/null (second time)
real    0m0.092s
user    0m0.020s
sys     0m0.056s

With "cat", you see that there still are all the system calls that it's 
making. Each block must still be read one by one, just to realize that 
it doesn't need to be reread again. You see that time spent in kernel 
code is not even half of what it was during the first time.

With "readahead", time spent in kernel code is 7 times smaller!

It's true that "readahead" is also cleaner and more explicit than using 
"cat".

    Cheers,

    Michael.

-- 
Michael Opdenacker, Free Electrons
Free Embedded Linux Training Materials
on http://free-electrons.com/training
(More than 1000 pages!)




More information about the busybox mailing list