testsAndMisc/linux_configuration/fixes/lib/arch_sysctl.sh
Krzysztof kuhy Rudnicki 88d8b4454c
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Set vm.swappiness=100 for zram swap
The optimizer pinned swappiness at 10, which keeps the kernel from
using the 32 GiB zstd zram device. Raise it to 100.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWUvCN36e1dx3Ejii1p
2026-11-22 14:47:00 +01:00

125 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
# VM and network sysctl tuning.
#
# Sourced by optimize_arch_desktop.sh; split out to keep it under the 250-line
# cap. Sourced rather than run, so it inherits the caller's strict mode
# and the variables defined above the source line.
# ===================================================================
# 3. Memory & swap tuning via sysctl
# ===================================================================
tweak_vm_sysctl() {
# SYSCTL_DROPIN_DIR defaults to the real directory and is overridden only
# by the test harness, which runs un-jailed in ci_mirror.sh and in CI.
local dropin="${SYSCTL_DROPIN_DIR:-/etc/sysctl.d}/90-desktop-performance.conf"
# Desktop workloads: high swappiness (zram swap is compressed RAM, see fixes/setup_zram.sh), aggressive VFS caching, tuned dirty ratios
local -A params=(
["vm.swappiness"]="100"
["vm.vfs_cache_pressure"]="50"
["vm.dirty_ratio"]="15"
["vm.dirty_background_ratio"]="5"
["vm.dirty_writeback_centisecs"]="1500"
["vm.page-cluster"]="0"
)
local needs_update=false
local key
for key in "${!params[@]}"; do
local current
current=$(sysctl -n "$key" 2>/dev/null || true)
if [[ $current != "${params[$key]}" ]]; then
needs_update=true
break
fi
done
if [[ $needs_update == "false" && -f $dropin ]]; then
log_ok "VM sysctl parameters already tuned — skipping."
return 0
fi
cat >"$dropin" <<'VMEOF'
# Desktop performance tuning — managed by optimize_arch_desktop.sh
#
# vm.swappiness=100 — favor the 32 GiB zram swap (zstd, prio 100) over dropping page cache
# vm.vfs_cache_pressure=50 — favor keeping inode/dentry caches (speeds up file operations)
# vm.dirty_ratio=15 — allow up to 15% RAM dirty before synchronous writeback
# vm.dirty_background_ratio=5 — start async writeback at 5% dirty
# vm.dirty_writeback_centisecs=1500 — flush dirty pages every 15s (less I/O churn)
# vm.page-cluster=0 — read one page at a time from swap (reduces latency on SSD)
vm.swappiness = 100
vm.vfs_cache_pressure = 50
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.dirty_writeback_centisecs = 1500
vm.page-cluster = 0
VMEOF
sysctl --system >/dev/null 2>&1
return 0
}
# ===================================================================
# 4. Network: TCP BBR + fastopen + buffer tuning
# ===================================================================
tweak_network_sysctl() {
local dropin="${SYSCTL_DROPIN_DIR:-/etc/sysctl.d}/91-desktop-network.conf"
# Check if BBR module is available
if ! modprobe tcp_bbr 2>/dev/null; then
log_warn "tcp_bbr kernel module unavailable — skipping network tuning."
return 0
fi
local -A params=(
["net.core.default_qdisc"]="fq"
["net.ipv4.tcp_congestion_control"]="bbr"
["net.ipv4.tcp_fastopen"]="3"
["net.core.rmem_max"]="16777216"
["net.core.wmem_max"]="16777216"
["net.ipv4.tcp_rmem"]="4096 1048576 16777216"
["net.ipv4.tcp_wmem"]="4096 1048576 16777216"
["net.ipv4.tcp_mtu_probing"]="1"
)
local needs_update=false
local key
for key in "${!params[@]}"; do
local current
current=$(sysctl -n "$key" 2>/dev/null || true)
# Normalize whitespace for comparison (kernel uses tabs)
current=$(echo "$current" | xargs)
local expected
expected=$(echo "${params[$key]}" | xargs)
if [[ $current != "$expected" ]]; then
needs_update=true
break
fi
done
if [[ $needs_update == "false" && -f $dropin ]]; then
log_ok "Network sysctl parameters already tuned — skipping."
return 0
fi
cat >"$dropin" <<'NETEOF'
# Network performance tuning — managed by optimize_arch_desktop.sh
#
# BBR congestion control — better throughput and lower latency than cubic
# TCP fastopen — saves one RTT on repeated connections (both client and server)
# Larger buffers — helps on high-bandwidth or high-latency links
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_fastopen = 3
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 1048576 16777216
net.ipv4.tcp_wmem = 4096 1048576 16777216
net.ipv4.tcp_mtu_probing = 1
NETEOF
sysctl --system >/dev/null 2>&1
return 0
}