Anda di halaman 1dari 4

08/05/2010 http://hpc.serc.iisc.ernet.

in/~govind/hp…
--------------------------------------------------------------------------
Lecture Notes scripted by students of the course. All disclaimers and
copyrights apply ! :-) (c) RG/SERC/IISc
--------------------------------------------------------------------------

HIGH PERFORMANCE COMPUTING


----------------------------
NOTES OF CLASS 9 (13-09-2000)
----------------------------

Page Replacement:

On a page fault, the OS will look for page frames which are free
in main memory. If there are no page frames free, it replaces an existing page
from main memory. Which page is to be replaced is decided by whether we are
following FIFO (first in first out) or LRU (least recently used page
replacement policy). Suppose if the memory locations accessed by a
particular process are:
Ox0000 100C, 0000 10DC, 0000 2008, 0000 3A40, 0000 1B54, 0000 2008
0000 200C, 0000 23DC, 0000 5010, 0000 7A40, 0000 75B4, 0000 8080
Further suppose the page size 4Kbytes, then we use the "reference string" of
the pages accessed to determine the number of page faults and the pages
replaces. The reference string essentially consists of the page numbers
of the memory locations access, removing any duplication among successive
references. This is because, if the last reference is to a page p,
then an immediately following reference to the same page will never
cause a fault. Hence it is sufficient to consider the reference string
with the duplicate entries among successive references removed. For
the above accesses, the reference string is:
1, 2, 3, 1, 2, 5, 7, 8

Let us assume (for illustrative purposes) that only 4 main memory frames
are allocated per process in a multiprogramming environments.
For the above accesses, first, page 1 is required and will cause a
page fault. It is then brought into the memory. The next two references
to pages 2 and 3 will also cause page faults and the pages will be brought
into the memory. The subsequent access to page 1 will not cause any
page fault, as page 1 is already in memory. similarly the next reference
to page 2 will also not cause any page fault.

1 2 3 1 2 5 7 8
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 7 | | 7 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | 8 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | 3 | | 3 | | 3 | | 3 | | 3 | | 3 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | | | | | | | 5 | | 5 | | 5 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+

The subsequent access to page 5, causes a page faults and gets the last
free frame allocated for it. On the next reference, 7, which cause a
page fault, the memory management needs to find a page to be replaced.
for this if a FIFO page replacement algorithm is used, then the oldest
page, in this case page 1, is replaced, and 7 is brought in that place!
Subsequent reference 8, will replace page 2 as shown above.

If LRU replacement policy is used, then the references causing replacements,

…ernet.in/~govind/…/L8-Mem-Mgmt.txt 1/4
08/05/2010 http://hpc.serc.iisc.ernet.in/~govind/hp…
(reference to 7 and 8) will result in the replacing the least recently used
pages (3 and 1 respectively, and in that order) to be replaced, as
shown below.

1 2 3 1 2 5 7 8
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 8 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | 3 | | 3 | | 3 | | 3 | | 7 | | 7 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | | | | | | | 5 | | 5 | | 5 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+

There are a number of other page replacement algorithm, including the


(theoretical) optimal algorithm, approximate LRU, reference-bit
algorithm, second-chance algorithm and so on. Unlike a cache replacement
(to be studied in Memory management), the page replacement algorithm
has to be more sophisticated, and should choose a replacement page which is
least likely to cause a page fault. Fortunately, a page fault usually
incurs access to the disk and hence takes in the order of milliseconds.
Therefore the OS can afford to run a sophisticated, software implemented
page replacement algorithm to minimize the number of page faults.

Replacing a page may cause, the page (victim page) to written in the
disk. This must be done before the new page is written on the frame!
If a page is to be replaced, then the corresponding page table entry
in the page table is looked up. Associated with each page table entry,
the "Dirty" bit, indicates whether or not the page has been modified.
If it has been been modified (that is dirty bit set to 1), then the
page is written in to the disk.

Working Set:

As discussed earlier, programs exhibit locality. Thus the accesses made


by a process during a phase of computation tend to be for a few pages,
rather than to all pages in the address space. The model uses a window
of W page references (including adjacent duplicate entries).
The most recent pages accessed during the last W references form the
"working set". The working set may change during the course of program
execution. Using the work set, the OS allocates enough frames for a
process to hold its working set. That is the no. of frames allocated
to a process equals to working set size.

Let us consider a simple example which incurs a large number of page


faults:

double a[4096][4096]; // requires 128MB = 32K pages of space!


float s=2.3;
for(j=0;j<4096;j++)
{
for(i=0;i<4096;i++)
a[i][j]=a[i][j]+s;
}

this require 128MB of memory for matrix 'a'. if the page size is 4KB,
then we require 32K pages for this array. Now to see how (in what
order) the memory locations are accessed, let us understand the order
in which multi-dimensional arrays are arranged in the data segment.

…ernet.in/~govind/…/L8-Mem-Mgmt.txt 2/4
08/05/2010 http://hpc.serc.iisc.ernet.in/~govind/hp…
In C language, elements of a multi-dimension array are arranged in
the row-major ordering (in Fortran it is column major ordering),
which means that successive memory locations will have array elements

a[0][0]
a[0][1]
:
:
:
a[0][4095]
a[1][0]
a[1][1]
:
:
:
a[1][4095]
:
:
:
a[4095][0]
a[4095][1]
:
:
:
a[4095][4095]

Now, each page (4KB) will contain 4096/(size of double) = 4096/8 = 512
array elements. Thus if a[0][0] starts at the first byte of a page p,
then page p will end with the element a[0][511]. The element a[0][512]
will be in page p+1. Proceeding this way, we can say that
a[1][0] would be in page p+8 ,
a[2][0] would be in page p+16 , ...
a[4095][0] would be in page p+32760

Now, observe that in the above code, array elements are accessed in
the order a[0][0], a[1][0], ... a[4095][0], a[0][1], ... , a[4095][1],
... a[0][4095], a[1][4095], ... , a[4095][4095]

Thus if we concentrate only on the accesses to array elements a, the


reference trace would be

p, p+8, p+16, ... , p+32760, p, p+8, ... , p+32760, ...


p+1, p+9, ..., p+32761 , ... (repeated how many times?)
p+2, p+10, ..., p+32762, ...
... ... ...
p+7, p+15, ..., p+32767, ...

Now even if the process is allocated 2048 memory frames (8MB memory),
[or, 4095 frames -- 1 less than 4096 ] every access will cause a
page fault!

How many page faults are there if 2048 frames are


allocated for this process and FIFO replacement policy is used?
What happens is we use LRU replacement policy?

How do we rectify this? if the i and j loops are interchanged


(which is possible in this example, and does not cause any
violation of dependences -- check if this is indeed true!)
then reference string would reduce to

…ernet.in/~govind/…/L8-Mem-Mgmt.txt 3/4
08/05/2010 http://hpc.serc.iisc.ernet.in/~govind/hp…
p, p+1, p+2, ... p+32767

where each page is accessed 1024 times (512 loads and 512 stores)
before the next page is needed.

…ernet.in/~govind/…/L8-Mem-Mgmt.txt 4/4

Anda mungkin juga menyukai