Thursday, 26 May 2011

Basics of Memory Management


Reading all these article in Wikipedia and different blogs about memory management technologies put me to shame. I literally didn't know anything about it and would probably never find it out if I stayed pure networking guy. Since I want to go a bit deeper into vSphere memory management (Memory Virtiualizaion, Virtual MMU, TSP) in the my next articles, today I need to build a foundation on top of which I can put additional layers later.  I will try to explain new stuff in the same way I would like it to be explained to me one week ago.

I need to start with explanations of some definitions and basic principles of memory management.
Mostly all modern applications/OS work with virtual memory.  None of them has any idea where its actual memory is located. Each process truly believes that it has it own address space.
A Memory Page is the fixed length contiguous block of data used for memory allocation, usually its size is 4 Kbyte (Small Page) or 2 Mbyte (Large Page). 
The Page Table keeps all mapping entries between Page Virtual Address used by Application and Page Physical Address where the actual data is stored. So there has to be some tool that will crawl through all Page Table in order to find the required mapping of Virtual Address (VA) to Physical Address (PA), whenever there is request for access from Application to Memory. 
This tool is called Memory Management Unit (MMU). This process can be very slow if you just calculate how many pages can be in 2 GByte Virtual Memory Address Space and how many mappings you need with 4 Kbyte page. If my math is correct it can go up to  524,288 pages. 
The MMU has specific type of memory called Translation Lookaside Buffer (TLB) that keeps VA -- PA translations for recently used memory accesses. When there is request for translation MMU first checks TLB for a VA -- PA match. If it finds a match it returns PA to the CPU - this is called TLB hit. If there is no match MMU has to walk through entire Page Table to find translation entry  - this is called TLB miss. As soon as it finds VA -- PA mapping in Page Table this translation is copied to TLB and the Process sends request to MMU again where it is successfully translated by TLB. It may happen that Page is swapped from physical memory to hard disk to make some free space. So if there is memory access request for swapped Page it has to be swapped back to physical memory. Then again the proper translation has to be written to TLB and Proccess again has to re-send the request to MMU.  In a nutshell, all memory access is done through TLB by MMU, and the performance cost of TLB miss can be pretty big (it depends on hardware functionality and specs).  


This drawing can serve you as a visual hint for the information above.

Since TLB is limited in size it can keep very limited number of translations. So it is here you can see benefits of using Large Pages with regard to size of memory that can be addressed. Let's assume the TLB can keep 2048  translation entries, that means TLB can cover only 8 Mbytes of addressable space with small pages (SP) of 4 Kbyte. With Large Pages of 2 MByte (LP) you can have 4 Gbytes of addressable space.  This significantly reduces the number of TLB misses. 

Since this blog is supposed to be about vSphere we need to mention one more layer of address translation. When the Application in Virtual Machine (VM) makes a memory access request it uses VA that has to be translated to Physical Address of VM (PA), which is still virtual address from ESX standpoint. Therefore, there has to be another translation of PA  to Host Address (HA). That means that there should be at least two Page Tables: one for VA -- PA translation and another for PA -- HA translation.

If you find it interesting I would really recommend you to go through all links in this article to get more details that I omitted (otherwise this material would be complicated and boring to read at once). I would also like to encourage you to add to your bookmarks two great blogs that I read all the time and use as main references for my VCAP study - Yellow Bricks and Frank Denneman.

In the next article I will provide detailed review on how ESX manages memory, and the difference between old memory management technique with Shadow Pages and new Virtual MMU.


If you find this post useful please share it with any of the buttons below. 

No comments:

Post a Comment