Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

process memory and page cache clarification is needed

4 views
Skip to first unread message

hopehope_123

unread,
Sep 23, 2005, 1:50:40 AM9/23/05
to
Hi Gurus ,

I need to understand the interaction between the page cache (which is
used for file systemn data cache) , and virtual memory map of a process
which uses data from the cache.

Where does the pages which are read from the disk is mapped inside a
process?

Imagine a simple c program:

char *buf;

buf = (char *)malloc(1000);

This buf is allocated inside the heap , or in detail it is first
reserved (in swap file) ,and on demand , it is allocated (inside the
phy.ram)

Then imagine that the code has the following line:

*buf = 65 ;

So i modifed the contents the buffer.

As far as i know , if the pages which are modified like this , needs to
be written to the swap disk in order to be reused again .

So if these pages pageout by the os , they are written to the swap
file.

Now consider that ,i start to read data files by using read ,or
readv or pread system calls. ( readv and pread are the io calls which
oracle or other database systems uses on unix)

fdes=open("/data/spss/x1.dat",O_RDONLY);

while (fdes)

{

printf("%d\n",read(fdes, buf,sz));

}

I wonder how things happen now.

How the data blocks which are cached inside the page cache mapped to
the process?

If the file is read first time by this process , the file must be read
from the disk. Then the blocks are cached inside the page cache. The
page cache has no backing storage inside the swap file ,

but instead it is directly mapped from the data file itself.

(Is this correct?)

When my process reads data from the disk , then does the page that is
inside the page cache copied into the process map ? Or is it shared and
no copy takes place?

Kind Regards,

tolga

Anton Rang

unread,
Sep 23, 2005, 11:14:12 AM9/23/05
to
"hopehope_123" <hopeho...@yahoo.com> writes:
> I need to understand the interaction between the page cache (which is
> used for file systemn data cache) , and virtual memory map of a process
> which uses data from the cache.

OK. You can probably find a lot of this on the web with a little effort, too.

> Where does the pages which are read from the disk is mapped inside a
> process?

I'll summarize this briefly.

If you read from a file using read(), the file pages are not mapped
into the process's address space at all. They are mapped temporarily
by the kernel and then the data is copied into the process's buffer.

If you use mmap() to map a file's data directly into a process's address
space, the pages containing data of that file are shared between that process
and the kernel.

> Imagine a simple c program [trimmed a little for clarity]:
> buf = (char *)malloc(1000);
> *buf = 65 ;


> As far as i know , if the pages which are modified like this , needs to
> be written to the swap disk in order to be reused again .

Right, if the system runs low on memory, it may choose to write the page(s)
containing the buffer to the swap file.

> [ ... ] read(fdes, buf,sz)


>
> How the data blocks which are cached inside the page cache mapped to
> the process?
>
> If the file is read first time by this process , the file must be read
> from the disk. Then the blocks are cached inside the page cache. The
> page cache has no backing storage inside the swap file ,
> but instead it is directly mapped from the data file itself.
> (Is this correct?)

Yes, I think you understand this well.

> When my process reads data from the disk , then does the page that is
> inside the page cache copied into the process map ? Or is it shared and
> no copy takes place?

Actually, the data which you read is copied from the page cache into
the pages comprising the process's buffer. So the execution flow is
something like this:

1. process issues read() system call
2. kernel maps part of the file into kernel address space (allocates pages)
3. kernel reads data from the file into the newly allocated pages
4. kernel copies the newly read data into the process's own pages

The pages which were allocated by the kernel as temporary buffers are
part of the page cache; a subsequent read which references the same
part of the file will find those pages in memory and be able to skip
step 3.

Step 4 is basically just a bcopy() operation.

It's possible on UFS/QFS to enable "direct i/o", which avoids the copy
altogether by reading data directly into the process's own address
space. However, this bypasses the page cache, so other reads won't
find a copy of the data and will have to go to disk. This is useful
in databases or for backups, for instance, where the data will be
cached by the application or is unlikely to be reused.

-- Anton

hopehope_123

unread,
Sep 26, 2005, 9:25:49 AM9/26/05
to
Hi Anton ,

Thanks for your mail.

Can we say that For directio (this is what i really use , both on ufs
and also on ocfs - oracle clustered file system) , based on your mail ,
only step 1 and 4 exist?

-If you read from a file using read(), the file pages are not mapped
-into the process's address space at all. They are mapped temporarily
-by the kernel and then the data is copied into the process's buffer.

Here , 'the data is copied into the process's buffer' means process
heap segment (as anon pages ) doesnt it?

At this point ,
i know that when a process reads a file , heap segment is the place
that is used to hold the file data.

If i use directio, instead of using page cache , the data is directly
read from the disk into the process .

*buf=65;

This modification also means modification in heap segment , and if it
is necessary it can be paged out to swap disk.

At this point , now consider , i modify the file by using the contents
of the buffer:

pwrite(18,buf, 16384, 1506574336) = 16384


according to the all these things , I think , If the memory which
stores the buf is pagedout at the time of this call , then first of all
it must be paged in. This has no relation with disk file and page
cache. ( )

If file is cached inside the page cache , then it must be updated
inside the page cache , (I think these are the pages which are created
at step 2) (the kernel updates these cached data inside the kernel
address space ,) then these pages must be written to the data file on
the disk .At this time we know that there exists some memory pages
inside the page cahce which are dirty and hold file system data. Now if
operating system needs to use these pages , how does it behave? Does
the operating system page out these data pages by writing them to the
data file (not swap file) in order to make them free?


Kind Regards,
tolga

0 new messages