blob: 58062eff6b67fca10e1d40aca8630fe83d394885 [file] [log] [blame]
Sean Mooney224fe1b2023-08-07 19:30:31 +00001#!/bin/bash
2
3# Kernel Samepage Merging (KSM)
4# -----------------------------
5
6# Processes that mark their memory as mergeable can share identical memory
7# pages if KSM is enabled. This is particularly useful for nova + libvirt
8# backends but any other setup that marks its memory as mergeable can take
9# advantage. The drawback is there is higher cpu load; however, we tend to
10# be memory bound not cpu bound so enable KSM by default but allow people
11# to opt out if the CPU time is more important to them.
12ENABLE_KSM=$(trueorfalse True ENABLE_KSM)
13ENABLE_KSMTUNED=$(trueorfalse True ENABLE_KSMTUNED)
14function configure_ksm {
15 if [[ $ENABLE_KSMTUNED == "True" ]] ; then
16 install_package "ksmtuned"
17 fi
18 if [[ -f /sys/kernel/mm/ksm/run ]] ; then
19 echo $(bool_to_int ENABLE_KSM) | sudo tee /sys/kernel/mm/ksm/run
20 fi
21}
22
23# Compressed swap (ZSWAP)
24#------------------------
25
26# as noted in the kernel docs https://docs.kernel.org/admin-guide/mm/zswap.html
27# Zswap is a lightweight compressed cache for swap pages.
28# It takes pages that are in the process of being swapped out and attempts
29# to compress them into a dynamically allocated RAM-based memory pool.
30# zswap basically trades CPU cycles for potentially reduced swap I/O.
31# This trade-off can also result in a significant performance improvement
32# if reads from the compressed cache are faster than reads from a swap device.
33
34ENABLE_ZSWAP=$(trueorfalse False ENABLE_ZSWAP)
35# lz4 is very fast although it does not have the best compression
36# zstd has much better compression but more latency
37ZSWAP_COMPRESSOR=${ZSWAP_COMPRESSOR:="lz4"}
Mohammed Naser5cb2abf2025-02-25 22:54:05 -050038ZSWAP_ZPOOL=${ZSWAP_ZPOOL:="zsmalloc"}
Sean Mooney224fe1b2023-08-07 19:30:31 +000039function configure_zswap {
Sean Mooney5c1736b2024-01-24 10:53:12 +000040 if [[ $ENABLE_ZSWAP == "True" ]] ; then
Sean Mooney224fe1b2023-08-07 19:30:31 +000041 # Centos 9 stream seems to only support enabling but not run time
42 # tuning so dont try to choose better default on centos
43 if is_ubuntu; then
44 echo ${ZSWAP_COMPRESSOR} | sudo tee /sys/module/zswap/parameters/compressor
45 echo ${ZSWAP_ZPOOL} | sudo tee /sys/module/zswap/parameters/zpool
46 fi
47 echo 1 | sudo tee /sys/module/zswap/parameters/enabled
48 # print curent zswap kernel config
49 sudo grep -R . /sys/module/zswap/parameters || /bin/true
50 fi
51}
52
53ENABLE_SYSCTL_MEM_TUNING=$(trueorfalse False ENABLE_SYSCTL_MEM_TUNING)
54function configure_sysctl_mem_parmaters {
55 if [[ $ENABLE_SYSCTL_MEM_TUNING == "True" ]] ; then
56 # defer write when memory is available
57 sudo sysctl -w vm.dirty_ratio=60
58 sudo sysctl -w vm.dirty_background_ratio=10
59 sudo sysctl -w vm.vfs_cache_pressure=50
60 # assume swap is compressed so on new kernels
61 # give it equal priority as page cache which is
62 # uncompressed. on kernels < 5.8 the max is 100
63 # not 200 so it will strongly prefer swapping.
64 sudo sysctl -w vm.swappiness=100
65 sudo grep -R . /proc/sys/vm/ || /bin/true
66 fi
67}
68
69function configure_host_mem {
70 configure_zswap
71 configure_ksm
72 configure_sysctl_mem_parmaters
73}
74
75ENABLE_SYSCTL_NET_TUNING=$(trueorfalse False ENABLE_SYSCTL_NET_TUNING)
76function configure_sysctl_net_parmaters {
77 if [[ $ENABLE_SYSCTL_NET_TUNING == "True" ]] ; then
78 # detect dead TCP connections after 120 seconds
79 sudo sysctl -w net.ipv4.tcp_keepalive_time=60
80 sudo sysctl -w net.ipv4.tcp_keepalive_intvl=10
81 sudo sysctl -w net.ipv4.tcp_keepalive_probes=6
82 # reudce network latency for new connections
83 sudo sysctl -w net.ipv4.tcp_fastopen=3
84 # print tcp options
85 sudo grep -R . /proc/sys/net/ipv4/tcp* || /bin/true
86 # disable qos by default
87 sudo sysctl -w net.core.default_qdisc=pfifo_fast
88 fi
89}
90
91function configure_host_net {
92 configure_sysctl_net_parmaters
93}
94
95function tune_host {
96 configure_host_mem
97 configure_host_net
huicoffee5f5255b2024-03-15 17:15:33 +080098}