[ Pobierz całość w formacie PDF ]
.So long as the number of free pages in the systemremains above free pages high, the kernel swap daemon does nothing; it sleeps againuntil its timer next expires.For the purposes of this check the kernel swap daemontakes into account the number of pages currently being written out to the swap le.It keeps a count of these in nr async pages ; this is incremented each time a page isqueued waiting to be written out to the swap le and decremented when the write tothe swap device has completed.free pages low and free pages high are set at systemstartup time and are related to the number of physical pages in the system.If thenumber of free pages in the system has fallen below free pages high or worse stillfree pages low, the kernel swap daemon will try three ways to reduce the number ofphysical pages being used by the system:Reducing the size of the bu er and page caches,Swapping out System V shared memory pages,Swapping out and discarding pages.If the number of free pages in the system has fallen below free pages low, the kernelswap daemon will try to free 6 pages before it next runs.Otherwise it will try tofree 3 pages.Each of the above methods are tried in turn until enough pages havebeen freed.The kernel swap daemon remembers which method it was using the lasttime that it attempted to free physical pages.Each time it runs it will start tryingto free pages using this last successful method.After it has free su cient 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 freepp g g gpages in the system had fallen below free pages low, it only sleeps for half its usualtime.Once the number of free pages is more than free pages low the kernel swapdaemon goes back to sleeping longer between checks.3.8.1 Reducing the Size of the Page and Bu er CachesThe pages held in the page and bu er caches are good candidates for being freed intothe free area vector.The Page Cache, which contains pages of memory mappedles, may contain unneccessary pages that are lling up the system's memory.Like-wise the Bu er Cache, which contains bu ers read from or being written to physicaldevices, may also contain unneeded bu ers.When the physical pages in the systemstart to run out, discarding pages from these caches is relatively easy as it requiresno writing to physical devices unlike swapping pages out of memory.Discardingthese pages does not have too many harmful side e ects other than making accessto physical devices and memory mapped les slower.However, if the discarding ofpages from these caches is done fairly, all processes will su er equally.Seeshrink mmap inEvery time the Kernel swap daemon tries to shrink these caches it examines a blockmm filemap.cof pages in the mem map page vector to see if any can be discarded from physicalmemory.The size of the block of pages examined is higher if the kernel swap daemonis intensively swapping; that is if the number of free pages in the system has fallendangerously low.The blocks of pages are examined in a cyclical manner; a di erentblock 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, thewhole 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 orthe bu er cache.You should note that shared pages are not considered for discardingat this time and that a page cannot be in both caches at the same time.If the pageis not in either cache then the next page in the mem map page vector is examined.Pages are cached in the bu er cache or rather the bu ers within the pages arecached to make bu er allocation and deallocation more e cient.The memory mapshrinking code tries to free the bu ers that are contained within the page beingSee try toexamined.If all the bu ers are freed, then the pages that contain them are also be free buffer infs buffer.cfreed.If the examined page is in the Linux page cache, it is removed from the pagecache and freed.When enough pages have been freed on this attempt then the kernel swap daemonwill wait until the next time it is periodically woken.As none of the freed pageswere part of any process's virtual memory they were cached pages , then no pagetables need updating.If there were not enough cached pages discarded then the swapdaemon will try to swap out some shared pages.3.8.2 Swapping Out System V Shared Memory PagesSystem V shared memory is an inter-process communication mechanism which al-lows two or more processes to share virtual memory in order to pass informationamongst themselves.How processes share memory in this way is described inmore detail in Chapter 5.For now it is enough to say that each area of Sys-tem V shared memory is described by a shmid ds data structure.This containsa pointer to a list of vm area struct data structures, one for each process sharingp y gthis area of virtual memory.The vm area struct data structures describe wherein each processes virtual memory this area of System V shared memory goes.Eachvm area struct data structure for this System V shared memory is linked togetherusing the vm next shared and vm prev shared pointers.Each shmid ds data struc-ture also contains a list of page table entries each of which describes the physicalpage that a shared virtual page maps to.The kernel swap daemon also uses a clock algorithm when swapping out System VSee shm swapshared memory pages.Each time it runs it remembers which page of which sharedin ipc shm.cvirtual memory area it last swapped out.It does this by keeping two indices, therst is an index into the set of shmid ds data structures, the second into the list ofpage table entries for this area of System V shared memory.This makes sure that itfairly victimizes the areas of System V shared memory.As the physical page frame number for a given virtual page of System V sharedmemory is contained in the page tables of all of the processes sharing this area ofvirtual memory, the kernel swap daemon must modify all of these page tables toshow that the page is no longer in memory but is now held in the swap le.Foreach shared page it is swapping out, the kernel swap daemon nds the page tableentry in each of the sharing processes page tables by following a pointer from eachvm area struct data structure.If this processes page table entry for this page ofSystem V shared memory is valid, it converts it into an invalid but swapped outpage table entry and reduces this shared page's count of users by one.The formatof a swapped out System V shared page table entry contains an index into the setof shmid ds data structures and an index into the page table entries for this area ofSystem V shared memory.If the page's count is zero after the page tables of the sharing processes have all beenmodi ed, the shared page can be written out to the swap le.The page table entryin the list pointed at by the shmid ds data structure for this area of System V sharedmemory is replaced by a swapped out page table entry.A swapped out page tableentry is invalid but contains an index into the set of open swap les and the o setin that le where the swapped out page can be found.This information will be usedwhen the page has to be brought back into physical memory.3.8.3 Swapping Out and Discarding PagesThe swap daemon looks at each process in the system in turn to see if it is a goodSee swap outcandidate for swapping.Good candidates are processes that can be swapped somein mm vmscan.ccannot and that have one or more pages which can be swapped or discarded frommemory
[ Pobierz całość w formacie PDF ]