HP-UX 11i v3 Memory Management Subsystem

20
Appendix 3 Operation of the pager (vhand)
Examination of the output from the top command illustrates two key points:
The total amount of virtual memory used by all of the processes running on the system can greatly
exceed the total amount of physical memory available to the system.
Any given process runs happily with only a fraction of its total virtual address space loaded into
memory. The virtual address space contains all the code and data defined by the program; real
memory is used only for objects actively used by the program. But any portion of the virtual
address space can be accessed at any time as the program executes.
The virtual memory subsystem makes judicious use of the physical memory and the swap space to
keep all processes running efficiently by moving objects between the two resources. The process that
performs page out activity is called
vhand.
vhand's function is to keep memory available by freeing up the least recently referenced pages. It
also performs other functions related to maintaining memory availability, such as garbage collection
of the kernel memory allocator free lists.
vhand uses a two-handed clock algorithm to decide which pages to free. Conceptually, it has two
hands (called the "age hand" and the "steal hand") passing through all of memory. The age hand
marks each page as "not recently referenced". The steal hand follows after a delay, and checks each
page to see whether it's been accessed (and so marked as recently referenced) since the first hand
cleared its referenced bit. Those which have not been accessed may be stolen (paged out and the
memory made available to other users).
In actual implementation, vhand steps through memory by following a doubly linked list of
pregions. A pregion represents a range of virtual memory in an address space. It doesn't step
through all
pregions each time it is woken, and normally looks at only a portion of the pages in
each
pregion.
Using pregions rather than simply scanning all pages (for example, using the physical page
database) has the advantage of automatically skipping kernel memory and memory that's already
free. However, it has the disadvantage of putting all the memory belonging to a single process
together. When the steal hand reached that process's
pregions, all the pages it stole would come
from that one process, leaving it frantically paging back in its working set. This would cause
noticeable lack of responsiveness in interactive programs. Therefore, only a portion of each
pregion is aged or stolen on each pass, and vhand thus needs multiple passes through the active
pregion list to visit all of pageable memory.
It's important to keep an appropriate distance between the hands. Too close, and pages are stolen
that are in fact in regular use. Too far, and the hands have to move faster to keep the same steal
rate; this means that
vhand will consume more processor time. The kernel automatically keeps an
appropriate distance between the hands, based on the available paging bandwidth, the number of
pages that need to be stolen, the number of pages already scheduled to be freed, and the frequency
at which
vhand runs.
The two hands cycle through the active pregion linked list of physical memory to look for memory
pages that have not been referenced recently and move them to the swap space. Pages that have not
been referenced from the time the age hand passes to the time the steal hand passes are pushed out
of memory. The hands rotate at a variable rate determined by the demand for memory.
The vhand daemon decides when to start paging by determining how much free memory is
available. Once free memory drops below an established threshold, paging occurs.
vhand attempts
to free enough pages to bring the supply of memory back up to that threshold. The page daemon
continues to age pages (that is, clear their reference bits) when woken even if there's enough memory
that it doesn't need to steal pages; of course, it won't be woken very often in that situation.