Page 274 - DCAP403_Operating System
P. 274
Unit 13: Case Study: Linux
Every time the timer expires, the swap daemon looks to see if the number of free pages in the Notes
system is getting too low. It uses two variables, free_pages_high and free_pages_low to decide
if it should free some pages. So long as the number of free pages in the system remains above
free_pages_high, the kernel swap daemon does nothing; it sleeps again until its timer next
expires. For the purposes of this check the kernel swap daemon takes into account the number
of pages currently being written out to the swap file. It keeps a count of these in nr_async_pages;
this is incremented each time a page is queued waiting to be written out to the swap fi le and
decremented when the write to the swap device has completed. free_pages_low and free_pages_
high are set at system startup time and are related to the number of physical pages in the system.
If the number of free pages in the system has fallen below free_pages_high or worse still free_
pages_low, the kernel swap daemon will try three ways to reduce the number of physical pages
being used by the system:
1. Reducing the size of the buffer and page caches,
2. Swapping out System V shared memory pages,
3. Swapping out and discarding pages.
If the number of free pages in the system has fallen below free_pages_low, the kernel swap
daemon will try to free 6 pages before it next runs. Otherwise it will try to free 3 pages. Each
of the above methods are tried in turn until enough pages have been freed. The kernel swap
daemon remembers which method it was using the last time that it attempted to free physical
pages. Each time it runs it will start trying to free pages using this last successful method.
After it has free sufficient pages, the swap daemon sleeps again until its timer expires. If the
reason that the kernel swap daemon freed pages was that the number of free pages in the system
had fallen below free_pages_low, it only sleeps for half its usual time. Once the number of free
pages is more than free_pages_low the kernel swap daemon goes back to sleeping longer between
checks.
13.5.13 Reducing the Size of the Page and Buffer Caches
The pages held in the page and buffer caches are good candidates for being freed into the
free_area vector. The Page Cache, which contains pages of memory mapped files, may contain
unneccessary pages that are filling up the system’s memory. Likewise the Buffer Cache, which
contains buffers read from or being written to physical devices, may also contain unneeded
buffers. When the physical pages in the system start to run out, discarding pages from these
caches is relatively easy as it requires no writing to physical devices (unlike swapping pages
out of memory). Discarding these pages does not have too many harmful side effects other than
making access to physical devices and memory mapped files slower. However, if the discarding
of pages from these caches is done fairly, all processes will suffer equally.
Every time the Kernel swap daemon tries to shrink these caches it examines a block of pages
in the mem_map page vector to see if any can be discarded from physical memory. The size of
the block of pages examined is higher if the kernel swap daemon is intensively swapping; that
is if the number of free pages in the system has fallen dangerously low. The blocks of pages are
examined in a cyclical manner; a different block of pages is examined each time an attempt is
made to shrink the memory map. This is known as the clock algorithm as, rather like the minute
hand of a clock, the whole mem_map page vector is examined a few pages at a time.
Each page being examined is checked to see if it is cached in either the page cache or the buffer
cache. You should note that shared pages are not considered for discarding at this time and that
a page cannot be in both caches at the same time. If the page is not in either cache then the next
page in the mem_map page vector is examined.
Pages are cached in the buffer cache (or rather the buffers within the pages are cached) to make
buffer allocation and deallocation more effi cient. The memory map shrinking code tries to free
the buffers that are contained within the page being examined.
LOVELY PROFESSIONAL UNIVERSITY 267