Monday, July 9, 2012

Listing swap status

Listing swap information is easy, but interpreting it is difficult.
So far I believe the tools aren't precise and just give rough approximations.

Traditionally, swap used to mean only the "disk portion" of the virtual memory.
At some point Solaris has changed that to: swap = "traditional swap" + "unused RAM".
And by doing this, it turned out that swap somehow became the virtual memory itself.
It's also known as virtual swap causing a great deal of confusion and obscurity.

I apologize in advance if I incidently get caught by what I just stated above.

The "disk space" portion of virtual swap is reported by option -l (list status).
For example:
  
$ swap -l
swapfile                    dev  swaplo    blocks      free
/dev/zvol/dsk/rpool/swap  195,2       8  16777208  16765736
 
On the above output, the ZFS volume rpool/swap is the only disk-based swap device.
The last three fields in the output are all in terms of 512 bytes units:
 
  • swaplo : Starting page offset on the device.
     
    On the given example:
    8 x 512 = 4096 = 4 KB, because it's an X86.
    In X86 the page size is 4KB, while SPARC it's 8KB, typically.
     
  • blocks: amount of 512 bytes blocks on the disk-based swap device.
     
    On the given example:
    16777208 x 512 = 8589930496 = 8388604 KB, approximately 7.999 GB.
    As rpool/swap is 8 GB, the difference can be ZFS metadata overhead (?).
     
  • free: amount of 512 bytes free blocks on the disk-based swap device.
    Two fundamental observations for subsequent conclusions are:

    1. Is it equal or not to the blocks field?
        
      On the given example:
      (16777208 - 16765736) x 512 = 11472 x 512 = 5873664 = 5736 KB.
      Approximately 0.005 GB disk-based swap is being used.
      If it's equal, of course, then no disk-based swap is being used.
       
    2. Is it less or not than reserved from swap -s ?
        
      On the given example:
      16765736 x 512 = 8584056832 = 8382868  KB, around 7.994 GB.
       
The /usr/bin/swap from a Solaris 11 and its -lh option works fine on a Solaris 10.
Of course the binary needs to be of the same architecture (X86 or SPARC).
That's great because -h is very useful.