Exercise caution while editing /usr/local/omk/conf/opCommon.nmis, /usr/local/omk/conf/opCommon.json or /etc/mongod.conf; if a syntax error is induced all OMK applications will cease to function. Check out How to make configuration changes to opCommon.nmis and other files for some more details on best practices for modifying the configuration file. |
restarting the service
, rebooting might be a better option.A final disadvantage that needs mentioning is that the command `free` reports 20% more RAM than is present when configured to use 20% of memory for zram swap, for example:
# We have a server with 8GB RAM. # Expecting a compression ratio of 3.00, this server has been configured to use 20% of RAM (1.6GB) as zram swap. # This zram swap is displayed by the `free` command below as 4.8GB, being the uncompressed zram swap (1.6GB * compression ratio of 3 = 4.8GB). # The 1.6GB of RAM used as zram swap has not been deducted from the total Mem 7.8GB as displayed by the `free` command: # This server has 11GB of combined RAM + zram swap (7.8GB - 1.6GB + 4.8GB), not 12.6GB combined RAM + zram swap (7.8GB + 4.8GB); free -h total used free shared buff/cache available Mem: 7.8Gi 1.1Gi 264Mi 32Mi 6.4Gi 6.4Gi Swap: 4.8Gi 46Mi 1.9Gi |
https://www.kernel.org/doc/html/v5.5/admin-guide/blockdev/zram.html
# as a non-root user install Rust and Cargo: curl https://sh.rustup.rs -sSf | sh source $HOME/.cargo/env # as a non-root user navigate to an apppropriate directory and git clone zram-generator cd ~/git_repos/ git clone https://github.com/systemd/zram-generator.git cd zram-generator/ make build # disregard the one error at completion - we haven't installed ronn: /bin/sh: 1: ronn: not found make: *** [Makefile:41: man] Error 127 sudo make install NOBUILD=true # again disregard the one error at completion - we haven't installed ronn install: cannot stat 'man/zram-generator.8': No such file or directory make: *** [Makefile:65: install] Error 1 # create the config file with zram-size set at 20% of RAM sudo echo -e '[zram0]\nhost-memory-limit=none\n# 20% * expected compression ratio of 3 = 60%\nzram-size=ram/1.666666667\ncompression-algorithm=zstd'|sudo tee /etc/systemd/zram-generator.conf # check the config settings cat /etc/systemd/zram-generator.conf [zram0] host-memory-limit=none zram-size=ram/5 compression-algorithm=lzo-rle # Once installed and configured, the generator will be invoked by systemd early at boot, there is no need to do anything else: # # Start the systemd service - it defaults to enabled when installed: sudo systemctl daemon-reload sudo systemctl start /dev/zram0 # check service status sudo systemctl status /dev/zram0 # confirm the device is in use # - Note that DISKSIZE is the UNCOMPRESSED SIZE # - At a compression ratio of 3.00 the actual memory used by zram for this device will be 1.6GB: sudo zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lzo-rle 4.8G 46.1M 14.7M 15.7M 2 [SWAP] # Lastly, establish the best available compression-algorithm from best to worst: zstd | lz4 | lzo-rle | lzo # zstd is only supported by kernel 5.1 and newer: cat /sys/block/zram0/comp_algorithm lzo [lzo-rle] lz4 lz4hc 842 zstd # zstd is the best and available - edit /etc/systemd/zram-generator.conf and set compression-algorithm=zstd sudo nano /etc/systemd/zram-generator.conf # After changing compression-algorithm=zstd in /etc/systemd/zram-generator.conf, to restart the systemd service for device zram0 to update to using zstd: sudo systemctl restart systemd-zram-setup@zram0.service # and check the change has taken effect: cat /sys/block/zram0/comp_algorithm lzo lzo-rle lz4 lz4hc 842 [zstd] # confirm the device is in use: sudo zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 zstd 4.8G 46.1M 13.1M 15.7M 2 [SWAP] # Calculate the compression ratio for zram0 - it may take a short while to return the realistic expected compression ratio: cat /sys/block/zram0/mm_stat|awk '{print $1/$2}' 3.13493 # And finally check memory: free -h total used free shared buff/cache available Mem: 7.8Gi 1.1Gi 264Mi 32Mi 6.4Gi 6.4Gi Swap: 4.8Gi 46Mi 1.9Gi # Check the available swap devices: sudo swapon --show NAME TYPE SIZE USED PRIO /dev/zram0 partition 4.8G 0B 100 |
# Git clone zram-services to the servers /tmp/zram-services directory: cd /tmp/ git clone https://github.com/eylles/zram-service.git # Change directory into the cloned ./zram-service/ directory cd zram-service/ # Checkout our trusted commit: git checkout fa033ceebb297e01b3215dd32c9c0aea56a6bb8b # Make absolutely sure the command `sha512sum zram.sh` returns the following checksum for zram.sh: sha512sum zram.sh 6bf8c67834d375419e536c5e5c3070f0a1756f83f5bbcd7cc2d0f77b977f0df72536aef9025b8fc787870cf89bfa2a8c66317242f6d6bccfdba86fca70e663f6 zram.sh # Move the zram.sh script to /etc/init.d/zram (notice file at /etc/init.d/zram has no .sh extension): sudo mv zram.sh /etc/init.d/zram # Cleanup as we are finished with our cloned zram-service cd /tmp/ rm -rf zram-service # Create the config file: sudo echo -e 'ALGORITHM=lzo-rle\n# 20% * expected compression ratio of 3 = 60%\nRAM_PERCENTAGE=60\nPRIORITY=100'|sudo tee /etc/default/zram-config # Check the config settings cat /etc/default/zram-config ALGORITHM=lz4 RAM_PERCENTAGE=20 PRIORITY=100 # Enable the zram init.d service: sudo update-rc.d zram defaults # Start the zram service: sudo service zram start # Check the zram service status # - Note that DISKSIZE is the UNCOMPRESSED SIZE # - At a compression ratio of 3.00 the actual memory used by zram for this device will be 1.6GB: sudo service zram status NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lz4 4.8G 4K 75B 12K 2 [SWAP] # Lastly, establish the best available compression-algorithm from best to worst: zstd | lz4 | lzo-rle | lzo # zstd is only supported by kernel 5.1 and newer: cat /sys/block/zram0/comp_algorithm lzo [lzo-rle] lz4 lz4hc 842 zstd # zstd is the best and available - edit /etc/systemd/zram-generator.conf and set compression-algorithm=zstd sudo nano /etc/default/zram-config # After changing compression-algorithm=zstd in /etc/default/zram-config, restart the systemd service for device zram0 to update to using zstd: sudo service zram restart # and check the change has taken effect: cat /sys/block/zram0/comp_algorithm lzo lzo-rle lz4 lz4hc 842 [zstd] # Confirm the device is in use: sudo zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 zstd 4.8G 46.1M 13.1M 15.7M 2 [SWAP] # Calculate the compression ratio for zram0 - it may take a short while to return the realistic expected compression ratio: cat /sys/block/zram0/mm_stat|awk '{print $1/$2}' 3.13493 # And finally check memory: free -h total used free shared buff/cache available Mem: 7.8Gi 1.1Gi 264Mi 32Mi 6.4Gi 6.4Gi Swap: 4.8Gi 46Mi 1.9Gi # Check the available swap devices: sudo swapon --show NAME TYPE SIZE USED PRIO /dev/zram0 partition 4.8G 0B 100 |
First check whether the linux kernel supports zswap:
grep -i zswap /boot/config-$(uname -r) # CONFIG_ZSWAP is not set |
The output 'CONFIG_ZSWAP is not set' signals that the kernel does not support zswap.
The output 'CONFIG_ZSWAP=y' signals that the kernel does support zswap.
#!/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 # enable zswap debug info mount -t debugfs none /sys/kernel/debug 2>/dev/null||:; exit 0; |
On Debian 9.13 the following zswap parameters were set from the above /etc/rc.local script
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:
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 'echo;grep . /sys/module/zswap/parameters/*;echo;D=/sys/kernel/debug/zswap;grep . "${D}/"*;perl -E "say \"\nCompress Ratio: \".$(cat "${D}/stored_pages")*4096/$(cat "${D}/pool_total_size")" 2>/dev/null' /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 /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 |
when zpool is zsmalloc, the typical compression ratio will be 3.00,
as in the following example where swap did not hit the disk,
even though 9GB memory was allocated when the server had only 7.8GB memory available:
free -h total used free shared buff/cache available Mem: 7.8G 7.5G 245M 20K 73M 148M Swap: 7.7G 1.5G 6.2G sudo sh -c 'echo;grep . /sys/module/zswap/parameters/*;echo;D=/sys/kernel/debug/zswap;grep . "${D}/"*;perl -E "say \"\nCompress Ratio: \".$(cat "${D}/stored_pages")*4096/$(cat "${D}/pool_total_size")" 2>/dev/null' /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 /sys/kernel/debug/zswap/duplicate_entry:0 /sys/kernel/debug/zswap/pool_limit_hit:0 /sys/kernel/debug/zswap/pool_total_size:387317760 /sys/kernel/debug/zswap/reject_alloc_fail:1507 /sys/kernel/debug/zswap/reject_compress_poor:0 /sys/kernel/debug/zswap/reject_kmemcache_fail:0 /sys/kernel/debug/zswap/reject_reclaim_fail:0 /sys/kernel/debug/zswap/stored_pages:291753 /sys/kernel/debug/zswap/written_back_pages:0 Compress Ratio: 3.08537436548223 |
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):
SWAPFILE=/swapfile; sudo swapoff "${SWAPFILE:-FAIL_HERE_!!!}"; # count in kilobytes sudo dd if=/dev/zero of="${SWAPFILE:-FAIL_HERE_!!!}" bs=1024 count=2883584; sudo chmod 0600 "${SWAPFILE:-FAIL_HERE_!!!}"; sudo mkswap "${SWAPFILE:-FAIL_HERE_!!!}"; sudo swapon "${SWAPFILE:-FAIL_HERE_!!!}"; 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:
/swapfile swap swap defaults 0 0 |
If your MongoDB installation is using table type wiredtiger, please read
https://docs.mongodb.com/manual/reference/configuration-options/#storage.wiredTiger.engineConfig.cacheSizeGB