-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Auto-configure zram with optimal settings
- Loading branch information
Showing
7 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Default to 8192M as max zram block size. | ||
MAX_SIZE=8192 | ||
# Amount of memory to use for zram, from 1 to 100. | ||
PORTION=50 | ||
# Default to compress with zstd, which has an average compression ratio of 3.37. | ||
ALGO=zstd | ||
# Higher values encourage the kernel to be more eager to move pages to swap. | ||
SWAPPINESS=180 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vm.swappiness = 10 | ||
vm.watermark_boost_factor = 125 | ||
vm.dirty_bytes = 268435456 | ||
vm.dirty_background_bytes = 134217728 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/bin/bash | ||
# Copyright 2022 System76 <info@system76.com> | ||
# SPDX-License-Identifier: GPL-3.0-only | ||
|
||
# If a zram device already exists, ignore | ||
test -b /dev/zram0 && exit 0 | ||
|
||
ENV_CONF='/etc/pop-zram' | ||
|
||
# Load user-defined variabls if a config exists | ||
if test -f ${ENV_CONF}; then | ||
export $(grep -v '^#' ${ENV_CONF} | xargs) | ||
fi | ||
|
||
# Default to compress with zstd, which has an average compression ratio of 3.37. | ||
test -n "${ALGO}" || ALGO=zstd | ||
|
||
# Default to 8192M as max zram block size. | ||
test -n "${MAX_SIZE}" || MAX_SIZE=8192 | ||
|
||
# Higher values encourage the kernel to be more eager to move pages to swap. | ||
test -n "${SWAPPINESS}" || SWAPPINESS=180 | ||
|
||
# Amount of memory to use for zram, from 1 to 100. | ||
test -n "${PORTION}" || PORTION=50 | ||
|
||
if ((PORTION > 100)); then | ||
PORTION=100 | ||
elif ((PORTION < 1)); then | ||
PORTION=1 | ||
fi | ||
|
||
# Number of consecutive pages to read in advance. Higher values increase compression | ||
# for zram devices, but at the cost of significantly reduced IOPS and higher latency. | ||
# It is highly recommended to use 0 for zstd; and 1 for speedier algorithms. | ||
test -n "${PAGE_CLUSTERS}" || PAGE_CLUSTERS=$(test zstd = ${ALGO} && echo 0 || echo 1) | ||
|
||
# Total amount of memory accessible to the OS. | ||
test -n "${PAGE_CLUSTERS}" || PAGE_CLUSTERS=$(test zstd = ${ALGO} && echo 0 || echo 1) | ||
|
||
# Total amount of memory accessible to the OS. | ||
TOTAL=$(awk -v p=${PORTION} '/MemTotal/ {printf "%.0f", p * $2 / 102400}' /proc/meminfo) | ||
|
||
# The recommended size is `max(mem_total / 2, 8GB)`. | ||
SIZE=$(((TOTAL > MAX_SIZE)) && echo ${MAX_SIZE} || echo ${TOTAL}) | ||
|
||
# Load the zram module. | ||
modprobe zram num_devices=1 | ||
|
||
sync | ||
sleep 1 | ||
|
||
# Format it | ||
zramctl --size "${SIZE}M" --algorithm "${ALGO}" /dev/zram0 | ||
mkswap /dev/zram0 | ||
|
||
sync | ||
sleep 1 | ||
|
||
# Activate the zram device with a priority of 1000. | ||
# This device should have a higher priority than disk-based swap. | ||
swapon -p 1000 /dev/zram0 | ||
|
||
# Apply optimal sysctl values for the zram environment. | ||
sysctl -w "vm.page-cluster=${PAGE_CLUSTERS}" "vm.swappiness=${SWAPPINESS}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[Unit] | ||
Description=Auto-configure and enable zram on Pop | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/usr/bin/pop-zram-config | ||
|
||
[Install] | ||
WantedBy=multi-user.target |