Computer Science 111
Lecture 15: VM and Processes

by Shengqian Liu
Lecture Date: 11/26/2014

Effiency of Virtual Memory using Programs

Apache

Assume we use a large program (e.g Apache). Apache calls malloc() which uses lots of virtual memory.

for (;;)
 r = get_a_request();
 if (fork() == 0) { // clones virtual memory
  execlp("python"); //process r
 }
}

Solution

Ways to speed up virtual memory


Page Fault Mechanism

Page Fault

A page fault is a trap to the software raised by the hardware when a program accesses a page that is mapped in the virtual address space, but not loaded in physical memory.

Page Fault Mechanism: how page fault is dealt inside kernel

The physical memory is checked and a "victim" is picked. After picking the victim, we save it to the disk in case the victim is useful to other users.
Page fault mechanism can be illustrated by the following graph[1]:


page fault mechanism

Page Fault Policy: how to choose the victim

If the memory is random accessed, then any policy works fine since victim can be chosen at random.

- FCFS (First Come First Served)

We choose the entry that has been in the memory fot the longest amount of time.
Assume the trace is:
0 1 2 3 0 1 4 0 1 2 3 4
Also assume 5 virtual pages and 3 physical pages.

0

1

2

3

0

1

4

0

1

2

3

4

A

^0

0

0

^3

3

3

^4

4

4

4

4

4

B

^1

1

1

^0

0

0

0

0

^2

2

2

C

^2

2

2

^1

1

1

1

1

^3

3

There are 9 page faults in total.
If we increase number of physical page to 4, number of page faults also increases.
This situation is called Belady's anomaly:

0

1

2

3

0

1

4

0

1

2

3

4

A

^0

0

0

0

0

0

^4

4

4

4

^3

3

B

^1

1

1

1

1

1

^0

0

0

0

^4

C

^2

2

2

2

2

2

^1

1

1

1

D

^3

3

3

3

3

3

^2

2

2

10 page faults in total.

- LRU (Least Recently Used)

The victim is the entry that has not been accessed for the longest time.

0

1

2

3

0

1

4

0

1

2

3

4

A

^0

0

0

^3

3

3

^4

4

4

^2

2

2

B

^1

1

1

^0

0

0

0

0

0

^3

3

C

^2

2

2

^1

1

1

1

1

1

^4

Cost is 10 page faults.

- Oracle of Delphi

Look into the future to see which page is not needed for the longest time.

0

1

2

3

0

1

4

0

1

2

3

4

A

^0

0

0

0

0

0

0

0

0

^2

2

2

B

^1

1

1

1

1

1

1

1

1

^3

3

C

^2

^3

3

3

^4

4

4

4

4

4

7 page faults in total.


Virtual Machine Optimization


References