Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zswap.zpool: z3fold is now deprecated and zbud could have the same faith in the near future, better handle this change #215

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swap-default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
zswap_enabled=1
zswap_compressor=zstd # lzo lz4 zstd lzo-rle lz4hc
zswap_max_pool_percent=25 # 1-99
zswap_zpool=z3fold # zbud z3fold (note z3fold requires kernel 4.8+)
zswap_zpool=zsmalloc # zbud z3fold (note z3fold requires kernel 4.8+, deprecated since 6.12) zsmalloc

################################################################################
# ZRam
Expand Down
4 changes: 2 additions & 2 deletions include/systemd-swap.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Manage swap spaces on zram, files and partitions.
[email protected]
[email protected]
[email protected] [email protected] [email protected]
[email protected] [email protected] [email protected]

[Service]
Type=notify
Expand Down
2 changes: 1 addition & 1 deletion man/swap.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ and
Percentage of ram that can be compressed.
.I
.IP zswap_zpool=
Set wich compressed memory pool to use, if unsure use z3fold.
Set wich compressed memory pool to use, if unsure use zsmalloc.
.PP
The following options are available in the "zram" section:
.I
Expand Down
51 changes: 38 additions & 13 deletions src/systemd-swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_mem_stats(fields: List[str]) -> Dict[str, int]:
stats[key] = int(items[1]) * 1024
if not fields:
break
assert len(fields) == 0
assert len(fields) <= 1
return stats


Expand All @@ -57,8 +57,8 @@ def get_mem_stats(fields: List[str]) -> Dict[str, int]:
)
WORK_DIR = "/run/systemd/swap"
LOCK_STARTED = f"{WORK_DIR}/.started"
ZSWAP_M = "/sys/module/zswap"
ZSWAP_M_P = "/sys/module/zswap/parameters"
MODULE_PATH = "/sys/module"
ZSWAP_M_P = f"{MODULE_PATH}/zswap/parameters"
KMAJOR, KMINOR = [int(v) for v in os.uname().release.split(".")[0:2]]
IS_DEBUG = False
sigterm_event = threading.Event()
Expand Down Expand Up @@ -423,15 +423,22 @@ def destroy_swapfile(self) -> None:

@staticmethod
def get_free_ram_perc() -> int:
ram_stats = get_mem_stats(["MemTotal", "MemFree"])
return round((ram_stats["MemFree"] * 100) / ram_stats["MemTotal"])
ram_stats = get_mem_stats(["MemTotal", "MemFree", "MemAvailable"])
if "MemAvailable" in ram_stats:
free_ram = ram_stats["MemAvailable"]
else:
free_ram = ram_stats["MemFree"]
return round((free_ram * 100) / ram_stats["MemTotal"])

@staticmethod
def get_free_swap_perc() -> int:
swap_stats = get_mem_stats(["SwapTotal", "SwapFree"])
# Minimum for total is 1 to prevent divide by zero.
return round((swap_stats["SwapFree"] * 100) / max(swap_stats["SwapTotal"], 1))

def module_loaded(module_name) -> bool:
return os.path.isdir(f"/sys/module/{module_name}")


def debug(msg: str) -> None:
if IS_DEBUG:
Expand Down Expand Up @@ -468,8 +475,11 @@ def relative_symlink(target: str, link_name: str) -> None:


def write(data: str, file: str) -> None:
with open(file, "w") as f:
f.write(data)
try:
with open(file, "w") as f:
f.write(data)
except:
warn(f"Failed writing {data} to file {file}")


def read(file: str) -> str:
Expand Down Expand Up @@ -625,8 +635,12 @@ def start_swapd() -> None:

def start_zswap() -> None:
systemd.daemon.notify("STATUS=Setting up Zswap...")
if not os.path.isdir(ZSWAP_M):
error("Zswap - not supported on current kernel")
info("Zswap: check module availability")
if module_loaded("zswap"):
info("Zswap: module loaded")
else:
error("Zswap: module not available")

info("Zswap: backup current configuration: start")
makedirs(f"{WORK_DIR}/zswap")
for file in os.listdir(ZSWAP_M_P):
Expand All @@ -640,19 +654,30 @@ def start_zswap() -> None:
f'{config.get("zswap_max_pool_percent")}, Zpool: '
f'{config.get("zswap_zpool")}'
)

zpool_allocator = config.get("zswap_zpool")
if (zpool_allocator != "zsmalloc") and (not module_loaded(zpool_allocator)):
warn(f"Zswap: Desired Zpool allocator {zpool_allocator} not available")
if zswap_parameters[f"{ZSWAP_M_P}/zpool"]:
zpool_allocator = zswap_parameters[f"{ZSWAP_M_P}/zpool"]
warn(f"Zswap: Keeping current {zpool_allocator}")
else:
zpool_allocator = "zsmalloc"
warn("Zswap: Defaulting to zsmalloc")

write(config.get("zswap_enabled"), f"{ZSWAP_M_P}/enabled")
write(config.get("zswap_compressor"), f"{ZSWAP_M_P}/compressor")
write(config.get("zswap_max_pool_percent"), f"{ZSWAP_M_P}/max_pool_percent")
write(config.get("zswap_zpool"), f"{ZSWAP_M_P}/zpool")
write(zpool_allocator, f"{ZSWAP_M_P}/zpool")
info("Zswap: set new parameters: complete")

def start_zram() -> None:
systemd.daemon.notify("STATUS=Setting up Zram...")
info("Zram: check module availability")
if not os.path.isdir("/sys/module/zram"):
error("Zram: module not availible")
if module_loaded("zram"):
info("Zram: module loaded")
else:
info("Zram: module found!")
error("Zram: module not available")

def zram_init() -> None:
info("Zram: trying to initialize free device")
Expand Down