Saturday, August 22, 2015

RH442 Memory Reclamation

RH442 - Chapter 12

 rename
 jandro's version from 1-9-2011

Section 12 - Memory Reclamation ( pg. 262 )

Characterizing page status ( 12 -3 )

    Pages available for use
        Free - can be used for allocation
        Inactive 
            clean
note being used
data not changed or has already been written to disk
available for allocation
            dirty
not being used
data has changed and waiting to be written to disk
available for allocation after being written to disk

        Pages in use
            active
currently in use by a process

Calculating dirty and clean memory ( 12 - 4 )

    Per process - /proc/PID/smaps
each open file is listed
remember a shared library may be listed under multiple processes

Reclaiming Dirty Pages ( 12 - 5 )

    The kernel thread(s) pdflush is used to flush dirty pages to disk
2 to 8 threads run at any one time
# of threads controlled by several kernel tunable
/proc/sys/vm/nr_pdflush_threads shows the current number of threads running

    Pages are flushed when triggered by:
in response to a sync() call
when the page cache runs out of pages
if the pages have been in memory for too long
if there are too mange dirty pages in the page cache

Tuning pdflush ( 12 - 6 )

      Key pdflush tuneables
/proc/sys/vm/dirty_writeback_centisecs - In hundredths of a second, this is how often pdflush wakes up to write data to disk
/proc/sys/vm/dirty_expire_centiseconds - In hundredths of a second, how long data can be in the page cache before it's considered expired and must be written. ( default 300 )
/proc/sys/vm/dirty_background_ratio - Maximum percentge of active that can be filled with dirty pages before pdflush beings to write them ( default 10 )
/proc/sys/vm/dirty_ratio - Maximum percentage of total memory that can be filled with dirty pages before proceses are force to write dirty buffers ( default 40 )
      Tuning may be desirable on write heavy systems
test lowerring dirty_background_ratio and dirty_expire_centiseconds

Reclaiming clean pages ( 12 - 7 )

    Pages can be reclaimed from the command line
        buffers
sync force changed blocks to disk, update the superblock
ALT-SysRq-S
must be enabled in /proc/sys/kernel/sysrq
Kernel Doc sysrq.txt
echo s > /proc/sysrq-trigger

Reclaim clean pages
/proc/sys/vm/drop_caches
write a 1 to this file
all clean cached will be dropped

Out-of-memory killer ( 12 - 8 )

      Will sacrifice one or more processes in order to free up memory for the system when all else fails

      Runs when
No more pages are available in the VM
No more user address space available
Both of the above

      Will target process that
Are non privileged process
Together with it's children uses lots of memory
has been nice'd ( non-interactive processes )
does no raw I/O

      Run manually
echo f > /proc/sysrq-trigger

      View likelihood of kill
/proc/PID/oom_score
higher score - more likely target

Tuning OOM policy ( 12 - 9 )

    Protect a process or daemon
echo > /proc/PID/oom_adj
range of allowed values -17 to 15
lower values protect the process
-17 disables oom killer for this process
    Disable OOM killer
echo 1 > /proc/sys/vm/panic_on_oom
set in /etc/sysctl.conf to be permanent

Detecting Memory Leaks ( 12 - 10 )

    Process requests memory and never returns it to the system
typically a malloc call with a corresponding free
virtual - memory is never used
real - memory used but never returned

    Process tools to abserve memory usage
watch -n1 'pmap -p PID'
use watch and ps

    System wide usage
sar -R 1 2
watch -n1 free

    Memory debugger
valgrind distributed with RHEL
valgrind - - tool=memcheck , --leak-check= [default: summary]

What is SWAP ( 12 - 11 )


    The active of moving pages between RAM and disk
used when systems requries more memory that is physically available
swap space size plus RAM is the total amount of virtual memory

    Types of Pages
Kernel pages - Pages holding the program contents of the kernel itself. Fixed in memory and are never moved
Program pages - Pages storing the contents of programs and libraries. These are read-only, so no updates to disk are needed.
File-backed pages - Pages storing the contents of files on disk. If this page has been changed in memory it will eventually need to be written out to disk to synchronize the changes
Anonymous pages - Pages not backed by anything on disk. When a program requests memory to be allocated to perform computations or record informaiton the information resiedes in anonymous pages

    Target pages for SWAP
Anonymous pages
Inactive pages

Tuning Swappiness ( 12 - 13 )


    Trade off
efficient use of memory ( more swap )
high number of CPU cycles and disk IO to swap out data

    swap_tendency algorithm
swap tendenc = mapped_ratio / 2 + distress + vm_swappiness

    Tuneable
/proc/sys/vm/swappiness
RHEL5 default = 60
lower value less swap
higher values - more memory not actively being used to swap and free more memory for applications

Tuning swap size ( 12 - 14 )


      Performance issues
Two bytes ZONE_NORMAL used for each page swap
swaplock of thrashing can occur if swap reuests exeed I/O bandwidth

     Red Hat recommendations
Amount of RAMRecommend Amount of SWAP
batch servers4X RAM
database server<= 1GiB RAM
application server>= 0.5 X RAM
RAM 1-2 GiB1.5X RAM
RAM 2-8 GiBSame size as RAM
RAM > 8 GiB0.75 X RAM
memorize

Tuning swap for think time ( 12 - 15 )

    Number of pages swapped at a time
/proc/sys/vm/page-cluster
logarithmic value (power of 2) - setting it ot zero means "1 page", setting it to 1 means "2 pages", setting it to 2 means "4 pages", etc ( default 3 ( 8 pages ) )

    RHEL 5.1 has
/proc/sys/vm/swap_token_timeout
removed in kernel 2.6.20
can affect swap out of current process when thrashing

Tuning swap visit count ( 12 - 16 )


    Up to 32 devices allowed
    ~Use priorities
swapon -p 5 /swap/device
in /etc/fstab: /swap/device swap swap pri=5 0 0
prefer higher priority on swap devices

    View
cat /proc/swap
swapon -s




https://memorize.com/rh442-table-of-contents/jandro 

No comments: