[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Memory usage: buffer and cache



On Sun, 2004-10-24 at 10:46 +0200, Jan Kesten wrote:
> 
> Buffers: Relatively temporary storage for raw disk blocks
> ~         shouldn't get tremendously large (20MB or so)
> ~ Cached: in-memory cache for files read from the disk (the
> ~         pagecache).  Doesn't include SwapCached

Yes.

> Cached instead is a cache for files read from disk. You notice it's
> funtion if you access a file. First time you access it, it takes some
> time since it is loaded. If you close it and then reopen it, this is
> much faster:
> 
> teefix:~# time cp linux-2.6.8.1.tar /dev/null
> real    0m10.456s
> user    0m0.049s
> sys     0m1.408s
> teefix:~# time cp linux-2.6.8.1.tar /dev/null
> real    0m0.975s
> user    0m0.034s
> sys     0m0.911s
> 
> After doing much disk access:
> 
> teefix:~# time cp linux-2.6.8.1.tar /dev/null
> real    0m10.474s
> user    0m0.036s
> sys     0m1.433s
> teefix:~# time cp linux-2.6.8.1.tar /dev/null
> real    0m1.007s
> user    0m0.036s
> sys     0m0.936s
> 
> Hope I'm not too wrong in my explanation - if drop me a mail :-)
> 

You are correct. What the tests above show is that the CPU time spent is
always relatively the same (user & sys). What is changing is "real" time
which is actual wall time. So the question you need to ask is... if the
time spent on the CPU isn't changing why is it taking me 10 seconds to
cp this file the first time but only one second on the following try? 

It's because a process blocks on I/O. If a process needs data from disk
then it is taken off the CPU and put to sleep while the I/O request is
being handled. In the meantime another process is placed on the CPU.
This is referred to as a "context switch". There is no reason why a
process should sit idle on the CPU while it waits for some disk I/O
request. This forms the basis for "multitasking". 

So what these tests demonstrate is that if we run the command in
succession we see the affect of pagecache after the initial disk I/O
takes place. The pagecache has shaved nearly 9 seconds off the time
which is a big deal.

We can also see that as the disk is accessed more and more the pages
that once sat in pagecache have been replaced by more recent ones.


-- 
Eric Gaumer <gaumerel@ecs.fullerton.edu>

Attachment: signature.asc
Description: This is a digitally signed message part


Reply to: