Skip to content

Commit

Permalink
feat: Auto-configure zram with optimal settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Dec 15, 2022
1 parent 0b84561 commit 4f3077d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions debian/pop-default-settings.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ X-Repolib-Default-Mirror: ${SYS_REPO}"

case "$1" in
configure)
systemctl enable --now pop-zram.service || echo pop-zram configure failed
for divert in "${diverts[@]}"
do
source="${divert%%=*}"
Expand Down
1 change: 1 addition & 0 deletions debian/pop-default-settings.prerm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ diverts=(

case "$1" in
remove)
systemctl disable pop-zram.service
for divert in "${diverts[@]}"
do
source="${divert%%=*}"
Expand Down
8 changes: 8 additions & 0 deletions etc/default/pop-zram
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
1 change: 1 addition & 0 deletions etc/sysctl.d/10-pop-default-settings.conf
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
61 changes: 61 additions & 0 deletions usr/bin/pop-zram-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/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

UDEV_CONF='/etc/udev/rules.d/60-pop-zram.rules'

# Load user-defined variabls if a config exists
if test -f /etc/pop-zram; then
export $(grep -v '^#' /etc/pop-os/zram-config | 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.
if test -n ${PORTION}; then
PORTION=50
elif ((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.
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})

# Creates the udev rule for the block device at `${UDEV_CONF}`.
UDEV_RULE='KERNEL=="zram0", SUBSYSTEM=="block", DRIVER=="", ACTION=="add", ATTR{initstate}=="0", ATTR{comp_algorithm}="{{ALGO}}", ATTR{disksize}="{{SIZE}}", RUN+="/sbin/mkfs.ext4 -O ^has_journal -L $name $env{DEVNAME}"'
echo ${UDEV_RULE} | sed -e "s/{{SIZE}}/${SIZE}M/g" -e "s/{{ALGO}}/${ALGO}/g" > ${UDEV_CONF}

# Load the zram module.
modprobe zram num_devices=1

# Apply the udev rule, which creates and configures the zram device.
udevadm control --reload-rules
udevadm trigger
udevadm settle

# 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}"
9 changes: 9 additions & 0 deletions usr/lib/systemd/system/pop-zram.service
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

0 comments on commit 4f3077d

Please sign in to comment.