Wise people learn when they can; fools learn when they must - Arthur Wellesley

Monday, 15 September 2014

Swap / Virtual Memory - Solaris

                                                                                                
                                                                  SWAP

Well… what is this Swap ?

       Before Swap we need to know about our Physical Memory i.e. RAM installed on system
       So How to know that how much Physical memory present in our system  ?
      root@sol-test-2:>/# prtconf -v |grep -i mem
      Memory size: 1024 Megabytes
             name='acpi-srat-memory-0' type=int items=6
            name='acpi-srat-memory-1' type=int items=6
            name='maxallocmem' type=int items=1
            name='device-memory' type=int items=6

Ok.. now I know what is my Phy. Mem. ,
Consider a situation where the System has only Phy.  Mem.  And system is running from 45 days, it might be possible or not that our RAM is full… Suppose RAM is full… Now what ?
Should I have to reboot the system?

No… We have an Option of SWAP… By providing some space on HDD for memory usage.
That Space on HDD is called SWAP Space. Generally it is on Slice 1 starting from Cylinder 0


Now…  We understand what Phy. & swap is

Another Term Here is Virtual Memory (Solaris works on the concept of this Virtual Mem.)
Let’s define some Terms:
Physical Memory                     We already know this
Swap Space                              We know this also
Available Memory                  
Virtual Memory

So… what is this ?


We have 32gb of Physical Memory is installed on our system, but not all 32gb is given to us for use,

Some of them are reserved for Kernel Code & system data structure.
Rest of the Ram is termed as AVAILABLE MEMORY

Well…
Phy Mem / Swap / Available Mem is done then
what is this VIRTUAL MEM ?
The usable Phy Mem ( Available Mem) + Swap Space



SO… if our total Phy mem. is 32gb but usable (available mem) is 27gb and the swap is 16gb…hence
VIRTUAL MEM will be 27gb + 16gb = 43gb

I THINK now we able to define what is
Phy Mem … Swap Space … Available Mem … & VIRTUAL MEM
** There is a limitation of Swap in 32bit Solaris Versions
It max supports 2gb of swap
So if we have to assign 8g of swap the we need to configure 4 … 2gb swap space
There is no Limitations on 64bit
Well … How to know our sys is 64 or 32 bit ?
root@sol-test-2:>/# isainfo
amd64 i386
root@sol-test-2:>/# isainfo -b
64
root@sol-test-2:>/# isainfo -kv
64-bit amd64 kernel modules

OK… Now Some more about Mem and Processes…


Suppose I have to run a process … then how the process will run ?
Leave the rest technical jargons behind… just process and memory

Well… whenever a process initiates to run… it requires memory… 1 minute … who will assign mem to process ?
Obviously Kernel … who else?

But it is not like … that someone needs to bath and I will give him a bucket full of water then relax…
I will give him a bucket with some water then I keep some mugs of water with me to see how fast he is using the water of bucket …

Let’s Some Technical…
When a process is about to run… it needs memory… kernel will assign that mem to process… after assigning mem

Kernel splits that program in to chunks… i.e. called PAGE,
Then the kernel loads some pages in to the mem, which is already assigned to that program,
Then Prog starts using pages from its assigned memory, after using one page, the other and then other page and so on… OK…   if the required next page not found then what ?
A page fault occur [ DEMAND PAGING]

Means kernel should load more pages into the memory
Once the amount of available free mem falls below a certain level , PAGING will occur… The page scanner will run

And using a Last Recently Used (LRU) algorithm to determine that there are pages which are not used from some time, and then the page scanner will mark them Unused and waits for some more time… if still after some time they are unused then that pages are taken and put back to the free list.

Well… Paging is transfer of pages in between ram and swap
Page Out – from RAM to Swap
Page In – from swap to RAM

OK… reached far… but what is SWAPPING?        Still unanswered….
Well … moving of pages is PAGING but moving an entire Process is swapping.
Assume that System is at its Normal… means there are processes running … page in … page out … is working very fine

Everything is normal until our Super Admin started an SQL Process … system started crying …and thinking that what should I do now ? B’coz I don’t have enough mem to start this SQL process

Well… Here Kernel will come as rescuer and it selects an entire process and moves it to swap … to make room for the new SQL Process…

Good… but this option will be selected as last option by kernel, B’coz this will greatly impact the service of that process which has been moved to swap.
[Kernel have to do this (swapping) in case of running out of phy mem to avoid system crash]

SO… WHAT IS SWAP ?

It is an area that is used by kernel to store running programs, when there is not enough space in memory.

That is SWAP and moving the Running Processes from RAM to SWAP (swap out ) & back to RAM (swap in) is SWAPPING

IN today’s world … swap is not that much necessary b’coz of systems with huge physical memory
But this is matter of prior testing that what processes we are going to run on that system and up to what extent system will bear that processes with resources.

Well… one more important aspect of SWAP is while generating CRASH DUMPs
Swap should be large enough to hold the image of Physical Memory, Until there are another Dump Device is defined

GREAT…

One more thing…. Who will decide that which process should be get out from ram and which should reside ?
Sched process with PID 0
root@sol-test-2:>/# ps -el
 F S    UID   PID  PPID   C PRI NI     ADDR     SZ    WCHAN TTY         TIME CMD
 1 T      0     0     0   0   0 SY        ?      0          ?          29:25 sched


OK…  Now How to add extra Swap to our System
I have a 1g slice for this

root@sol-test-1:>/# swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c1t0d0s1   30,1       8 2104504 2104504

root@sol-test-1:>/# swap -s
total: 205116k bytes allocated + 58188k reserved = 263304k used, 1083324k available

root@sol-test-1:>/# swap -a /dev/dsk/c1t2d0s0

root@sol-test-1:>/# swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c1t0d0s1   30,1       8 2104504 2104504
/dev/dsk/c1t2d0s0   30,192      8 2104504 2104504

See… we have an extra swap slice

But this Temporary Solution..
Let’s make it Permanent
root@sol-test-1:>/# vi /etc/vfstab
/dev/dsk/c1t2d0s0       -       -       swap    -       no      -  [ I made this entry to vfstab]

root@sol-test-1:>/# cat /etc/vfstab
#device to mount       device to fsck        mount point       FS type     fsck pass      mount at boot   mount options
/dev/dsk/c1t2d0s0                  -                              -                      swap             -                              no                           -

NOW ONE MORE METHOD… TO ASSIGN SWAP FILE…

root@sol-test-1:>/# mkfile 100m /extra-swap-file

root@sol-test-1:>/# swap -a /extra-swap-file

root@sol-test-1:>/# vi /etc/vfstab
/extra-swap-file        -       -       swap    -       no      -

After making these entries I rebooted the system …

root@sol-test-1:>/# who -b
   .       system boot  Sep 15 14:40

root@sol-test-1:>/# swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c1t0d0s1   30,1       8 2104504 2104504
/dev/dsk/c1t2d0s0   30,192      8 2104504 2104504
/extra-swap-file      -        8 204792 204792

Ok… lets intersects the o/p
swapfile                                       dev        swaplo                  blocks                                   free
/dev/dsk/c1t0d0s1                    30,1                8                          2104504                2104504
/dev/dsk/c1t2d0s0                  30,192              8                           2104504                2104504
/extra-swap-file                        -                     8                          204792                  204792

Swapfile                   device/swap  path
Dev                          device major / minor no
Swaplo                     512 bytes Block… from here the usable swap begins, before that the        swaplo value locks  paging/swap operation, to avoid conflicts with vtoc 
                                 or  boot blocks.
Blocks                      size of swap in 512 bytes
Free                          unused swap space in 512 bytes blocks

/dev/dsk/c1t0d0s1                                   30,1          8                          2104504                2104504

root@sol-test-1:>/# bc
2104504 *512                             [block size in bytes and 1 block is 512 bytes]
1077506048                                [1077506048 bytes]
1077506048 /1024                      [1024 bytes =1kb ]
1052252                                      [1052252 kb]
1052252/1024                             [1024 kb =1mb]
1027                                            [1027mb]
1027/1024                                   [1024mb = 1 GB]
1                                                                                      [1 GB]

Well here swap is not used at all that’s why blocks & free both are same
So let’s calculate for free at once
2104504*512              /1024                      /1024                    /1024
                 Blocks          for kb                    for mb                  for gb   
o/p = 1

Let’s calculate the same for the swap file we created of 100mb
204792*512/1024/1024
99

** swap –l does not contain… physical memory**

root@sol-test-1:>/# swap -s
total: 204660k bytes allocated + 54296k reserved = 258956k used, 2244396k available

    Though while looking at o/p it seems very simple, but it is not
First what is 204660k bytes allocated?

Total amounts of Swap Allocated (1024kb in unit) currently allocated as backing store (disk backed swap space)

What is this Disk Backed?

Each page of Phy Mem is backed by swap space, i.e. the swap space is reserved at this time. The reserved space is not really allocated yet, just reserved for future use.    


root@sol-test-1:>/# prtconf -v |grep -i mem
Memory size: 512 Megabytes

54296k reserved                       Not currently allocated but claimed by mem for future use
258956k used                             allocated + reserved
2244396k available                   currently available for future reservation / allocation
 
204660k bytes allocated         
204660 /1024
199mb

54296k reserved
54296/1024
53

258956k used
258956/1024
252
Well we can see here that 199+53 = 252 i.e. (allocated + reserved = used)

2244396k available
2244396/1024
2191

  So we have total VIRTUAL MEMORY IS
  2192 + 252 = 2444
 Phy mem = 512mb
 System Swap = 1g
 Extra Swap Slice =1g
 Extra Swap File =100mb
 ---------------------------------------------------
 TOTAL = 512+1024+1024+100 = 2660
 OUT OF 2660 OUR VIRTUAL MEM (Avl + Swap) is 2444
          
FRANKLY 
I DON’T KNOW WHY SUN HAS MADE THIS SO COMPLICATED AND  USELESS?
(well...this is my opinion about this cmd)


Better to use vmstat
root@sol-test-1:>/# vmstat
 kthr      memory                                page                   disk                    faults                  cpu
 r b w     swap   free                  re mf pi po fr de sr      f0 s0 s1 s2         in   sy          cs us sy id
 0 0 0  2241396 111800            2   3  2  0  0  0  6        -0  4 -0 -0         22   24         16  0 96  4

memory
swap     Current  free  swap  (KB).
free       Current  free  memory  (KB).
Page
re            Page  reclaims  per  second.
mf          Minor  faults  per  second.
pi            KB  paged  in  per  second.
po           KB  paged  out  per  second.
fr             KB  freed  per  second.
de           Anticipated  short-term  memory  shortfall   (KB)  per  second.
sr            Pages  scanned  by  clock  algorithm  per  second.

WELL … WHAT IS TO CHECK HERE ?
Page / sr column …
if there is very high value means we are running out of memory … we need to monitor that situation for a while, there might be a process is reading a large amount of uncached data… if it so then this value will fall down automatically after some time… but if there no change then we need to be alarmed.

   Now I want to remove the assigned extra swap…
   Just remove the entry from /etc/vfstab
   Then
   root@sol-test-1:>/# swap -d /dev/dsk/c1t2d0s0
   root@sol-test-1:>/# swap -d /extra-swap-file
    SOME COMMON ERROR MSGS RELATED TO MEM ISSUE,
                Program out  of  memory
malloc error  0
WARNING:   Sorry,   no swap space to grow stack for pid
/tmp:   File system full,   swap space limit exceeded

/tmp:   File system full,   memory allocation failed

No comments:

Post a Comment