CS111 Lecture 15 Notes

By Yichen Pan & Haonan Zhou

Outline
- Uses of virtual memory (Examples)
- Allocation of memeory(malloc
- Apache
- Fast Message sending
Mechanism: how the kernel deals with a page fault?
Policy question: how do you choose a victim page?
- Policy 0
- Policy 1
- Policy 2
- Policy 3
- Policy 4
More optimization

Uses of virtual memory

Example1 Allocation of memory(malloc)
Think of it as a malloc arena, where some entries contain useful data and others contain garbage
 
Problem 
How to work out malloc so that it is FAST? For example: malloc(537)
 
Approach1
We want to implement this with as few instructions as possible, say 10 instructions on average. Therefore one obviously solution is looking for free space of 537 bytes long in memory. However, this process is slow and we cannot make this less than 10 instructions.
 
Approach2
Using page table to get atomic updates to full pages (Copying Garbage Collector)

Example2 Apache
Problem
Apache needs to do the following: 
A first attempt
We can call fork() for each request: 
for(;;)
{
     r= get_a_request();
     fork();                            // clones virtual memory
     process(r);                     //exelp("python"), probably won't use all the virtual memory we copied
}

The problem with this approach is that each call of fork() makes a copy all the virtual memory. However, imagine that process() executes a simple program (e.g. exelp("python") ). In this case, we copied a lot of unnecessary memory. 

Another approach
Using a controversial system call: vfork()vfork() acts like fork(), EXECPT:
A cleaner approach
Making fork() more efficient (copy-on-write, or COW)
Solution
Incorporate python into Apache: No fork() needed! 
 
- Problem: performance issue: code is sequential
We can create a thread for each request. However, creating thread will have overhead, and every request will have it.
- How to avoid? 
We can create threads at beginning, say, 100 threads (because we have about 100 cores) For each thread we block until the request is read to process.
 
Example 3: Fast massage sending
One approach
Using a pipe
 
Problem: 'write' makes a copy, 'read' also makes a copy. If the message is big (several MBs), the procedure slows down significantly.
 
A better way
Using virtual memory

Mechanism: how the kernel deals with a page fault
 
Policy question: how do you choose a victim page? 
 
Let's assume the following: 
- There are 5 virtual pages: 0, 1, 2, 3 and 4
- There are 3 physical pages: A, B and C
- The sequence of page request is: 012301401234
 
Policy 0
Always choose the first physical page
 
Policy 1
Choose each page in turn

Policy 2
Choose a page that is least recently used (LRU)

Policy 3
First Come First Serve (FCFS) or First In First Out (FIFO)

- What if we have 4 physical pages?
- 11 page faults with 4 physical pages
 
- We have more page faults when we have more physical pages? 
- This is called Belody's anomaly -> rare in practice
 
Policy 4
(optimal) Look for a page that will not be used in the furthest future
 
•    7 page faults
•    Aside: how to know when a page will be used in the future? 

More optimization

Demand page
Don’t read page from swap space until program needs it