Pages

Tuesday, February 14, 2017

What is Paging, Swaping, Page-in & Page Out

Computer has a certain amount of RAM, which is its "memory". It simulates more RAM by allowing extra data to be saved to the hard disk, which is known as Virtual Memory.

To do this, it breaks your memory space up into "pages". Applications that need access to data that is in memory call the data by page. If an application calls a page and it is in the RAM, then it is a "Page in" occurs. If an app calls for a page from memory, and that page is currently stored on the hard disk and has to be read back into the RAM, then a "Page Out" occurs.

A "Page-out" slows the operation of the system down because it has to read the data from a hard disk into RAM first, rather than reading straight from the RAM. Hard disks take about 300 times as long to transfer a page of data, which adds up to slow performance.

If page-outs exceed page-ins, you definitely don't have enough RAM. Ideally, page-outs should be less than 20% of the number of page-ins (the fewer page-outs, the faster your machine is performing) 

Adding more RAM, or reducing the number of open applications, are the only ways to reduce page-outs. While freeing up memory by working with fewer and smaller files and apps may help, more RAM is the only reasaonable solution.


Paging is a memory management scheme by which a computer stores and retrieves data from secondary storage[a] for use in main memory

to get the pagesize in linux

# getconf PAGESIZE

4096

What is Swapping?

This scheme involves every page in the system having an age which changes as the page is accessed. The more that a page is accessed, the younger it is; the less that it is accessed the older it becomes. Old pages are good candidates for swapping.

Swapping is the process of moving all the segments belonging to a process between the main memory and a secondary storage device. Swapping occurs under heavier work loads. Operating system kernel would move all the memory segments belonging to a process in to an area called swap area. When selecting a process for swapping, the operating system will select a process that will not become active for a while. When the main memory has enough space to hold the process, it will be transferred back in to the main memory from the swap space so that its execution could be continued.

This scheme involves every page in the system having an age which changes as the page is accessed. The more that a page is accessed, the younger it is; the less that it is accessed the older it becomes. Old pages are good candidates for swapping.

page-out === The system's free memory is less than a threhsold "lotsfree" and vhand daemon used "LFU" algorithm to move some unused / least used pages to the swap area.

page-in === One process which is running requested for a page that is not in the current memory (page-fault), vhand daemon is bringing it's pages to memory.

swap-out === System is thrashing and swapper daemon has de-activated a process and it's memory pages are moved into the swap area.

swap-in === A deactivated process is back to work and it's pages are being brought into the memory

What is Swappiness


The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.
swappiness can have a value of between 0 and 100

swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible

swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment. Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.

To check the swappiness value

cat /proc/sys/vm/swappiness


What is the difference between Paging and Swapping?

In Paging, blocks of equal size (called pages) are transferred between the main memory and a secondary storage device, Since paging allows moving pages (it could be a part of the address space of a process), it is more flexible than swapping. Since, paging only moves pages (unlike swapping, which move a whole process), paging would allow more processes to reside on the main memory at the same time, when compared with a swapping system.

In swapping, all the segments belonging to a process will be moved back and forth between the main memory and a secondary storage device.  Swapping is more suitable when running heavier workloads.

What is the Page Fault

When the page (data) requested by a program is not available in the memory, it is called as a page fault. This usually results in the application being shut down.

Though the term "page fault" sounds like an error, page faults are common and are part of the normal way computers handle virtual memory. In programming terms, a page fault generates an "exception," which notifies the operating system that it must retrieve the memory blocks or "pages" from virtual memory in order for the program to continue. Once the data is moved into physical memory, the program continues as normal. This process takes place in the background and usually goes unnoticed by the user.

Most page faults are handled without any problems. However, an invalid page fault may cause a program to hang or crash. This type of page fault may occur when a program tries to access a memory address that does not exist. Some programs can handle these types of errors by finding a new memory address or relocating the data. However, if the program cannot handle the invalid page fault, it will get passed to the operating system, which may terminate the process. This can cause the program to unexpectedly quit.

While page faults are common when working with virtual memory, each page fault requires transferring data from secondary memory to primary memory. This process may only take a few milliseconds, but that can still be several thousand times slower than accessing data directly from memory. Therefore, installing more system memory can increase your computer's performance, since it will need to access virtual memory less often.

When your program reads/writes a memory location, it will use a virtual address to refer to that location in memory. Your system will then translate that virtual address into a physical address, and the data will be written to (or read from) that physical address. But your program has no idea that this is happening— the virtual to physical address translation is all kind of “under the hood”. The point is, your program doesn’t need to know that this is happening. The system will just maintain a happy illusion for the program to live in.
There are a number of reasons why you’d want to have this system; nearly all memory systems today use virtual memory, except in certain specialized situations. One major advantage of virtual memory is that you can use it to “pretend” that your machine has more RAM than it actually has installed— this usually happens through a system called “paging” (sometimes called “swapping”).

The idea is that the physical RAM only holds a certain number of “pages” of memory. Typically a “page” is about 4096 bytes, although it varies from system to system. In order to “pretend” that there is more RAM than physically possible, some of the pages are actually in the RAM, and some of them are stored on the hard drive.

The virtual memory system uses something called a “page table” to map virtual addresses to physical addresses. Since our machine could possibly have less RAM than our program thinks it has, it’s possible to have more virtual addresses than physical addresses. That means not all virtual addresses in a page table will have a valid corresponding physical address (i.e. not all virtual addresses will have a valid entry in the page table). If a virtual address has no valid entry in the page table, then any attempt by your program to access that virtual address will cause a page fault to occur

So what happens when we have a page fault? Well, when that happens, your OS invokes something called a page fault handler. As you might imagine, it’s a piece of code that handles page faults. Usually the page fault handler will do the following:
Figure out which page the virtual address is supposed to map to. Figure out where that page is located on the hard drive.
Choose an existing page in physical RAM that we (probably) aren’t currently using. Write that page back to the hard drive, and evict it from RAM (i.e. kick it out) to make room for the new page.
Load the new page into RAM, from the hard drive.

Update the page table, so that the virtual address that caused the page fault now has a valid entry. Likewise, clear the virtual address entry for the page that we just evicted, so that virtual addresses that correspond to the evicted page are no longer valid.

Now that the correct page is loaded in RAM, and the page table is up to date, return control to the program that was running before all these shenanigans occurred, and retry the memory access instruction that initially caused the page fault. If all goes well, this second time around the faulting instruction will work correctly, now that the correct page has been loaded into physical RAM.

No comments:

Post a Comment