Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

      • https://www.kernel.org/doc/html/latest/vm/zswap.html provides that:
        • Zswap is a lightweight compressed cache for swap pages. It takes pages that are in the process of being swapped out and attempts to compress them
          into a dynamically allocated RAM-based memory pool. zswap basically trades CPU cycles for potentially reduced swap I/O.
          This trade-off can also result in a significant performance improvement if
          reads from the compressed cache are faster than reads from a swap device.
        • Zswap is a new feature as of v3.11 and interacts heavily with memory reclaim. This interaction has not been fully explored on the large set of potential configurations and workloads that exist.
          For this reason, zswap is a work in progress and should be considered experimental.
        • Overcommitted guests that share a common I/O resource can dramatically reduce their swap I/O pressure, avoiding heavy handed I/O throttling by the hypervisor.
          This allows more work to get done with less impact to the guest workload and guests sharing the I/O subsystem.
        • Users with SSDs as swap devices can extend the life of the device by drastically reducing life-shortening writes.
      • Performance Analysis of Compressed Caching Technique
        • See the CONCLUSION of this paper for insights as to why zswap should not be used on a server with less than 1GB RAM.

      • For zswap to function correctly it needs swap space equivalent to the maximum uncompressed RAM zswap may contain in its pool
        • To cater for a zswap compression ratio of 4.0, with the default 20% zswap pool (1/5th),
          one would provision swap space in a server with 10GB RAM installed of 2 GB zswap pool * (4.0-1) = 6.0 GB RAM: Server with 10GB RAM + at least 6.0 GB swap space should then be provisioned
        • One can expect at least a zswap compression ratio of 3.1, so when enabling zswap on a server with 10GB RAM installed, one should provision a minimum of 4.2 GB swap space.
      • Don't be tempted to increase maximum pool percent from the default setting of 20: this will most likely affect performance adversely.
      • Command to view zswap info during operation when enabled:
        • sudo grep -rRn . /sys/kernel/debug/zswap/
          may require mounting debugfs :
          sudo mount -t debugfs none /sys/kernel/debug


    • https://www.ibm.com/support/pages/new-linux-zswap-compression-functionality provides that for a server with 10GB RAM and 20% zswap pool size:

       
      • For the x86 runs, the pool limit was hit earlier - starting at the 15.5 GB data point
      • On x86, the average zswap compression ratio was 3.6

      • Average zswap compression ratio of 3.6 ties in with the 15.5 GB data point a which real disk swap started  as follows
        (as given in https://www.ibm.com/support/pages/new-linux-zswap-compression-functionality):
        • 10 GB RAM with 2 GB zswap pool =
          • 8 GB available RAM + 2  GB zswap pool =
            • 8 GB available RAM + (2  GB zswap pool * 3.6 average zswap compression ratio) =
              • 8 GB available RAM + 7.2GB zswap compressed RAM =
                • 15.2 GB RAM


    • Real world example 2021-12-22:

      • The following code was placed in /etc/rc.local to use the best zswap options available,
        as improvements have been made to zswap compression since this article was originally written

        • Code Block
          title/etc/rc.local
          #!/bin/sh -e
          #
          # rc.local
          #
          # This script is executed at the end of each multiuser runlevel.
          # Make sure that the script will "exit 0" on success or any other
          # value on error.
          #
          # In order to enable or disable this script just change the execution
          # bits.
          #
          # By default this script does nothing.
          
          # enable zswap:
          echo 20 > /sys/module/zswap/parameters/max_pool_percent||:;
          echo 1 > /sys/module/zswap/parameters/enabled||:;
          # https://linuxreviews.org/Zswap
          sh -c 'CMP=/sys/module/zswap/parameters/compressor;echo zstd >"${CMP}"||echo lz4 >"${CMP}"||echo lzo-rle >"${CMP}"||:;' 2>/dev/null
          # use the best zpool compressed memory pool allocator available:
          sh -c 'ZP=/sys/module/zswap/parameters/zpool;echo z3fold >"${ZP}"||echo zsmalloc >"${ZP}"||:;' 2>/dev/null
          
          exit 0;


        • On Debian 9.13 the following zswap parameters were set from the above /etc/rc.local script

          Code Block
          sudo -i
          chmod +x /etc/rc.local
          source /etc/rc.local
          grep . /sys/module/zswap/parameters/*
          
          /sys/module/zswap/parameters/compressor:lz4
          /sys/module/zswap/parameters/enabled:Y
          /sys/module/zswap/parameters/max_pool_percent:20
          /sys/module/zswap/parameters/zpool:zsmalloc


        • When this server was swapping the following zswap stats were achieved:

          Code Block
          free -h
                        total        used        free      shared  buff/cache   available
          Mem:           2.0G        1.9G         58M        728K         27M         17M
          Swap:          2.7G        1.4G        1.4G
          
          
          sudo sh -c 'D=/sys/kernel/debug/zswap;echo;grep . "${D}/"*;perl -E  "say \"\nCompress Ratio: \".$(cat "${D}/stored_pages")*4096/$(cat "${D}/pool_total_size")" 2>/dev/null'
          
          /sys/kernel/debug/zswap/duplicate_entry:0
          /sys/kernel/debug/zswap/pool_limit_hit:565978
          /sys/kernel/debug/zswap/pool_total_size:287293440
          /sys/kernel/debug/zswap/reject_alloc_fail:593
          /sys/kernel/debug/zswap/reject_compress_poor:436
          /sys/kernel/debug/zswap/reject_kmemcache_fail:0
          /sys/kernel/debug/zswap/reject_reclaim_fail:1596
          /sys/kernel/debug/zswap/stored_pages:360385
          /sys/kernel/debug/zswap/written_back_pages:1145522
          
          Compress Ratio: 5.1370556204920


        • How to interpret the above stats:
          • 'free -h' command provides that 1.4G of swap was used.
            That stat refers to the uncompressed swap:
            '/sys/kernel/debug/zswap/stored_pages * 4096' = '360385 * 4096' = 1476136960 bytes = 1.37476 GB
          • The 1.37476 GB of swap was compressed to '/sys/kernel/debug/zswap/pool_total_size' = 287293440 bytes = 0.26756 GB of RAM.
          • Compression Ratio is therefore '1.37476 GB/0.26756 GB' = 5.13706

        • This VM's swapsize was set knowing the compression ratios zswap achieved on this VM.

          To allow zswap to achieve a compression ratio of 5.13706,
          zswap needs swap space equivalent to the maximum uncompressed RAM zswap may contain in its pool when compressing at compression ratio of 5.13706.

          Consequently, the minimum swap space this VM should then have is:
          • '2 GB RAM * /sys/module/zswap/parameters/max_pool_percent * Compression Ratio'
            = '2 GB RAM * 20% * 5.13706'
            = 2.055 GB swap

        • However, the reason why the swap on this VM was actually set at 2.75 GB is that the command
          watch free -h

          showed that this VMs' zswap compression ratio was fluctuating between 4.8 and 6.1

          Consequently the decision was made to set

          '2GB RAM * 20% * 6.875'
          = 2.75 GB swap on this VM.


        • Use a swapfile for swap as this provides better flexibility in sizing swap than a swap partition.
          To resize an existing /swapfile to 2.75 GB (2883584 kilobytes):

          Code Block
          sudo swapoff /swapfile;
          # count in kilobytes
          sudo dd if=/dev/zero of=/swapfile bs=1024 count=2883584
          sudo chmod 0600 /swapfile
          sudo mkswap /swapfile
          sudo swapon /swapfile
          sudo swapon --show
          NAME      TYPE SIZE USED PRIO
          /swapfile file  12G 512K   -2


        • To make a new /swapfile persist after a reboot append this entry to /etc/fstab:

          Code Block
          /swapfile swap swap defaults 0 0


...