Syntax highlighting of PageOutKswapd

= Pageout and kswapd =
''Before we delve deeper into the Linux Memory management, particularly focusing on Page replacement code, lets try to understand some of the common terms used in this document:''

'''Pageout'''

"The operation performed by the kernel to evict a page resident in physical memory to the swap space on a secondary memory device (eg. typically a disk)"

'''Swap space'''

"The region or area on a slower (with respect to the RAM) secondary device used to store evicted pages from the physical memory. Linux uses one or more separate partition(s) specifically for swap space. Such partitions are called Swap partitions and have a type code 0x82 in hex. While on other OSs like Windows for example the swap space is typically a large single or multiple files on the exisitng windows partition(s)."

'''kswapd'''

"This is a kernel deamon thread invoked at boop-up which is responsible for maintaining a constant balance of the number availabe free pages in the physical memory at any time. The thread is only invoked when there is a need to evict pages from memory. All other times this thread is in a SLEEP state (not using CPU)".

'''swap cache'''

"In order to improve performance by reducing the number of disk accesses (both read/writes), the linux kernel implements the swap cache. This essentially is a cache of most of the pages evicted that are either waiting to be written to the secondary swap space or might be reused in the near future. Thus, there is a very good chance for a process to find its recently evicted page still existing in this swap cache thereby avoiding an expensive read operation on the disk. Yes! As a counter argument one is right in saying that this reduces the number of available free pages in the system. But I guess, the performance gain is significant enough to have it this way rather than not having it at all!".

[[TableOfContents]]
= Efficient use of Memory =
Linux, like most Unix operating systems, tries to use memory as efficiently as possible.  That is, all memory that is not in use by the kernel or processes may be used as file cache, to reduce the number of disk accesses the system has to do.  One consequence of this is that a busy Linux system will constantly run with most of its memory in use, and most memory allocations mean that another page had to be evicted from memory by the pageout code.

Under typical workloads, most of the memory will be in use by the page cache and by processes, which get their memory on demand after a page fault, see PageFaultHandling.  The memory allocator (PageAllocation) will allocate a free page, and activate the pageout code if the number of free pages has fallen too low.

= Asynchronous and Synchronous Pageout =
Most of the time, the rate of page allocations is relatively low.  In this case, the kswapd kernel thread can free memory as fast as it is allocated and the allocating processes can immediately get a memory page when needed, without having to wait for the pageout code.  Having kswapd free memory in the background helps the applications run at maximum speed.

However, sometimes the rate of page allocation is so high that kswapd can not keep up.  When that happens, the applications that are allocating pages will help free pages themselves, by calling the function ''try_to_free_pages''.  This has the effect of throttling the heavy memory allocators and (on NUMA systems) focussing the pageout code on those memory zones which the heavily allocating processes allocate from.

= kswapd =
== kswapdThis is a kernel deamon thread invoked at boop-up in kswapd_init() which is responsible for maintaining a constant balance of the number availabe free pages in the physical memory at any time. The thread sleeps most of the times and is only invoked when there is a need to evict pages from memory. The main body of this thread is in function kswapd() in the file mm/vmscan.c . The kswapd thread in woken up by the physical page allocator only when the number of available free pages is less then pages_low (a variable declared as unsigned long in file  include/linux/mmzone.h). The value of variable pages_low depends on the number of pages in a particular zone. This is calculated as:zone->pages_low = (zone->pages_min * 5) / 4; /* in file mm/page_alloc.c */balance_pgdat ==
= try_to_free_pages =
= shrink_caches =
= shrink_zone =
== shrink_cache ==
=== shrink_list ===
=== pageout ===
=== swap_page ===
== refill_inactive_zone ==
= Shrink Slab =
Most kernel allocations are done through the slab allocator. Some kernel allocations are caches (eg. inodes and dentries), parts of which can be freed by the pageout code.  However, since each slab has its own data structure, they all need their own replacement algorithms, which are separate from the replacement algorithm used for page cache and process memory.  Usually the slab occupies a fairly small part of memory, and replacement of kernel data structures is beyond the scope of this document.


["