Skip to content

Commit d6ef4a5

Browse files
committed
Optimize lot configurations
1 parent 0d0c1b8 commit d6ef4a5

10 files changed

Lines changed: 867 additions & 382 deletions

File tree

README.md

Lines changed: 34 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -2,211 +2,75 @@
22

33
Smart dynamic swap management for Linux, written in Rust.
44

5-
## Features
5+
## Overview
6+
7+
`systemd-swap` automatically configures the best swap strategy for your system:
68

7-
- **Auto-detection**: Automatically chooses optimal swap strategy based on filesystem (btrfs/ext4/xfs)
8-
- **Zswap + SwapFC**: Compressed RAM cache with dynamic swap files
9-
- **Zram + SwapFC**: Alternative mode with zram as primary swap
10-
- **Zram writeback**: Move idle pages from zram to disk (kernel 5.4+)
11-
- **Multi-filesystem**: Supports btrfs, ext4, and xfs
12-
- **Lightweight**: ~250 KB binary vs ~10 MB Python version
13-
14-
## Swap Modes
15-
16-
| Mode | Description | Best For |
17-
|------|-------------|----------|
18-
| `auto` | Auto-detect: btrfs/ext4/xfs → zswap+swapfc, other → zram | Most users |
19-
| `zswap+swapfc` | Zswap cache + dynamic swap files | **Desktop (btrfs)** |
20-
| `zram+swapfc` | Zram primary + swap files overflow | Memory-constrained |
21-
| `zram` | Zram only (no disk swap) | Non-btrfs systems |
22-
| `manual` | Use explicit settings | Advanced users |
23-
24-
### How Each Mode Works
25-
26-
**zswap + swapfc (default for btrfs/ext4/xfs)**:
27-
- Zswap compresses pages in RAM before writing to swap
28-
- Shrinker moves cold pages to disk proactively, freeing RAM
29-
- SwapFC creates pre-allocated swap files with fallocate
30-
- Most data stays in RAM; disk is only used for cold pages
31-
- Best desktop performance with efficient memory usage
32-
33-
**zram + swapfc**:
34-
- Zram creates compressed block device in RAM (highest priority)
35-
- SwapFC provides overflow to disk when zram is full
36-
- Optional: zram writeback moves idle pages to disk
37-
38-
**zram only**:
39-
- All swap in compressed RAM
40-
- No disk I/O, ideal for Live USB
9+
- **Auto-detection**: Smartly chooses between Zswap and Zram based on your filesystem.
10+
- **Zswap + SwapFC**: (Default for Btrfs/Ext4/XFS) Uses compressed RAM cache + dynamic swap files. Most efficient for desktops.
11+
- **Zram**: (Default for others) Uses compressed RAM block device. Good for systems without disk swap support.
12+
- **Dynamic Scaling**: Creates swap files on-demand, starting small and growing as needed.
4113

4214
## Installation
4315

16+
### Arch Linux / BigLinux
4417
```bash
45-
# Build (requires Rust 1.70+)
46-
cargo build --release
47-
48-
# Install
49-
sudo make install
50-
51-
# Enable
52-
sudo systemctl enable --now systemd-swap
18+
cd pkgbuild
19+
makepkg -si
5320
```
5421

55-
### Arch Linux / BigLinux
22+
### Manual Build
23+
Requirements: Rust 1.70+, `btrfs-progs`, `util-linux`
5624

5725
```bash
58-
cd pkgbuild
59-
makepkg -si
26+
cargo build --release
27+
sudo make install
28+
sudo systemctl enable --now systemd-swap
6029
```
6130

6231
## Usage
6332

33+
Check status:
6434
```bash
65-
# Check status (works without root, extra stats when root)
6635
systemd-swap status
67-
68-
# Show help
69-
systemd-swap --help
70-
71-
# Restart after config changes
72-
sudo systemctl restart systemd-swap
73-
74-
# View logs
75-
journalctl -u systemd-swap -f
7636
```
7737

78-
### Status Output (as root)
79-
80-
```
81-
Zswap:
82-
enabled: true
83-
compressor: zstd
84-
zpool: zsmalloc
85-
max_pool_percent: 45%
86-
87-
=== Pool Statistics (debugfs) ===
88-
pool_size: 234567890 (223.7 MiB)
89-
stored_pages: 58432 (228.2 MiB uncompressed)
90-
pool_utilization: 48%
91-
compress_ratio: 98%
92-
93-
=== Writeback Statistics ===
94-
written_back_pages: 1234 (4.8 MiB)
95-
pool_limit_hit: 0
96-
97-
=== Effective Swap Usage ===
98-
kernel_reported_used: 300.0 MiB
99-
in_zswap_pool (RAM): 228.2 MiB
100-
actual_disk_used: 71.8 MiB
101-
swap_in_ram: 76%
38+
Reload configuration:
39+
```bash
40+
sudo systemctl restart systemd-swap
10241
```
10342

10443
## Configuration
10544

106-
Edit `/etc/systemd/swap.conf`:
45+
Configuration is located at `/etc/systemd/swap.conf`.
10746

47+
### Common Options
48+
49+
**Change Swap Mode:**
10850
```ini
109-
################################################################################
110-
# Swap Mode
111-
################################################################################
51+
# Modes: auto, zswap+swapfc, zram, manuall
11252
swap_mode=auto
113-
114-
################################################################################
115-
# Zswap (used in zswap+swapfc mode)
116-
################################################################################
117-
zswap_compressor=zstd # lzo lz4 zstd lzo-rle lz4hc
118-
zswap_max_pool_percent=45 # Max % of RAM for pool
119-
zswap_zpool=zsmalloc # Memory allocator
120-
zswap_shrinker_enabled=1 # Move cold pages to disk
121-
zswap_accept_threshold=80 # Accept threshold after pool full
122-
123-
################################################################################
124-
# Zram (used in zram modes)
125-
################################################################################
126-
zram_size=50% # 50%, 1G, 512M, 100%
127-
zram_alg=zstd # Compression algorithm
128-
zram_prio=32767 # Swap priority
129-
130-
# Zram writeback (requires CONFIG_ZRAM_WRITEBACK)
131-
zram_writeback=0 # 0=disabled, 1=enabled
132-
zram_writeback_dev= # Partition or empty for auto loop
133-
zram_writeback_size=1G # Auto backing file size
134-
135-
################################################################################
136-
# SwapFC - Dynamic swap files
137-
################################################################################
138-
swapfc_chunk_size=512M # Size: 512M, 1G, 10% (of RAM)
139-
swapfc_max_count=32 # Maximum swap files
140-
swapfc_free_ram_perc=35 # Create when free RAM < this %
141-
swapfc_free_swap_perc=25 # Create more when free swap < this %
142-
swapfc_path=/swapfc/swapfile # Path for swap files
143-
144-
# Sparse files (thin provisioning) - ENABLED BY DEFAULT
145-
# Swap files appear full size but only allocate disk space when written.
146-
# Ideal with zswap: pages stay in RAM, disk is only used on writeback.
147-
# swapfc_use_sparse_disable=1 # Uncomment to pre-allocate disk space
148-
149-
# Btrfs compression mode (experimental)
150-
swapfc_use_btrfs_compression=0 # Double compression: zswap + btrfs
15153
```
15254

153-
## Custom Swap Location
154-
155-
You can place swap files on a different btrfs partition:
156-
55+
**Customize Zram Size:**
15756
```ini
158-
swapfc_path=/mnt/swap-drive/swapfile
57+
zram_size=50%
15958
```
16059

161-
## Zram Writeback
162-
163-
Move idle/incompressible pages from zram to disk:
164-
60+
**Customize Swap File Location:**
16561
```ini
166-
# Enable with auto loop device
167-
zram_writeback=1
168-
zram_writeback_size=2G
169-
170-
# Or use dedicated partition
171-
zram_writeback=1
172-
zram_writeback_dev=/dev/sda5
62+
swapfc_path=/mnt/data/swapfile
17363
```
17464

175-
Requires kernel compiled with `CONFIG_ZRAM_WRITEBACK`.
176-
177-
## Sparse Files (Thin Provisioning)
65+
See `/usr/share/systemd-swap/swap-default.conf` for all available options and defaults.
17866

179-
By default, swap files are created as sparse files:
180-
181-
- Files appear as 512M but start with 0 bytes on disk
182-
- Disk blocks are allocated only when data is actually written
183-
- With zswap, most pages stay compressed in RAM
184-
- Disk is only used when zswap pool is full (writeback)
185-
186-
To disable and pre-allocate all disk space:
187-
188-
```ini
189-
swapfc_use_sparse_disable=1
190-
```
191-
192-
## File Locations
193-
194-
| Path | Description |
195-
|------|-------------|
196-
| `/usr/bin/systemd-swap` | Main binary |
197-
| `/etc/systemd/swap.conf` | User configuration |
198-
| `/usr/share/systemd-swap/swap-default.conf` | Default configuration |
199-
| `/run/systemd-swap/` | Runtime data |
200-
| `/swapfc/` | Swap files (default) |
201-
202-
## Requirements
67+
## Features
20368

204-
- Linux kernel 5.0+
205-
- Rust 1.70+ (build only)
206-
- btrfs-progs (for btrfs features)
207-
- util-linux (zramctl, losetup)
69+
- **Zero Configuration**: Works out of the box for most systems.
70+
- **Sparse Files**: Swap files use zero disk space until data is actually written (when using Zswap).
71+
- **MGLRU Support**: Integrates with Multi-Gen LRU (Kernel 6.1+) to prevent thrashing.
72+
- **Zram Writeback**: Can offload cold pages from Zram to disk.
20873

20974
## License
21075

21176
GPL-3.0-or-later
212-

include/swap-default.conf

Lines changed: 46 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,73 @@
1-
# This file is part of systemd-swap.
2-
#
3-
# Configuration for systemd-swap daemon.
1+
# Configuration for systemd-swap
2+
# This file is part of systemd-swap.
43
# See swap.conf(5) for details.
54

65
################################################################################
7-
# Swap Mode - How swap is configured
6+
# Swap Mode
87
#
9-
# auto - Auto-detect: btrfs/ext4/xfs → zswap+swapfc, other → zram only
10-
# zswap+swapfc - Zswap cache + swap files (default for btrfs)
11-
# zram+swapfc - Zram (fast) + swap files for overflow
8+
# auto - Auto-detect best mode (default)
9+
# - btrfs/ext4/xfs -> zswap + swapfc
10+
# - others -> zram only
11+
# zswap+swapfc - Zswap + swap files (best for desktop/btrfs)
12+
# zram+swapfc - Zram + swap files (good for low RAM)
1213
# zram - Zram only (no disk swap)
13-
# manual - Use explicit settings below (legacy mode)
14+
# manual - Use explicit settings below
1415
################################################################################
1516

1617
swap_mode=auto
1718

1819
################################################################################
19-
# Zswap - Compressed RAM cache for swap pages
20-
# Used in zswap+swapfc mode or manual with zswap_enabled=1
21-
#
22-
# zswap compresses pages before writing to swap, reducing I/O.
23-
# Requires a backing swap device (swap file or partition).
20+
# Zswap Settings (used in zswap+swapfc mode)
2421
################################################################################
2522

26-
zswap_compressor=zstd # lzo lz4 zstd lzo-rle lz4hc (zstd = best balance)
27-
zswap_max_pool_percent=45 # Max % of RAM for compressed pool (30-50 typical)
28-
zswap_zpool=zsmalloc # Memory allocator (zsmalloc only, z3fold/zbud deprecated)
29-
zswap_shrinker_enabled=1 # Move cold pages to disk proactively (default since 6.8)
30-
zswap_accept_threshold=90 # Accept pages again after pool was full (%)
23+
zswap_compressor=zstd # Compression algo: zstd, lz4, lzo
24+
zswap_max_pool_percent=60 # Max RAM for compressed pool
25+
zswap_zpool=zsmalloc # Allocator
26+
zswap_shrinker_enabled=1 # Proactively move cold pages to disk
27+
zswap_accept_threshold=90 # Restart accepting pages when pool drops to X%
3128

3229
################################################################################
33-
# Zram - Compressed RAM disk
34-
# Used in zram+swapfc, zram-only modes, or manual with zram_enabled=1
35-
#
36-
# zram creates a compressed block device in RAM for swap
37-
# Faster than zswap - ideal for desktop systems
30+
# Zram Settings (used in zram modes)
3831
################################################################################
3932

40-
# zram_size supports: 1G, 512M, 50%, 100% (full RAM)
41-
zram_size=80% # Size of zram device
42-
zram_alg=zstd # Compression: lzo lz4 zstd lzo-rle lz4hc
43-
zram_prio=32767 # Swap priority (highest = used first)
33+
zram_size=80% # Size of zram device (e.g., 50%, 1G)
34+
zram_alg=zstd # Compression algo
35+
zram_prio=32767 # Priority (highest)
4436

45-
# Zram writeback: move idle/incompressible pages to disk
46-
# Requires CONFIG_ZRAM_WRITEBACK in kernel
47-
# When enabled with empty dev, auto-creates sparse file + loop device
48-
zram_writeback=0 # 0=disabled, 1=enabled
49-
zram_writeback_dev= # Backing device (partition or empty for auto loop)
50-
zram_writeback_size=1G # Size of auto-created backing file
37+
# Zram Writeback (optional, off by default)
38+
# Moves incompressible/idle pages to backing storage
39+
zram_writeback=0
40+
zram_writeback_size=1G # Initial backing file size
41+
zram_writeback_max_size=8G # Max size
42+
zram_writeback_threshold=50 # Trigger when zram is X% full
5143

5244
################################################################################
53-
# SwapFC - Dynamic swap file management
54-
# Supports: btrfs, ext4, xfs (auto-detected)
55-
# Used in zram+swapfc, zswap+swapfc modes, or manual with swapfc_enabled=1
56-
#
57-
# Creates swap files on-demand as memory pressure increases.
58-
# Files are pre-allocated with fallocate for stability.
45+
# SwapFC - Dynamic Swap Files (used in swapfc modes)
5946
################################################################################
6047

61-
# swapfc_chunk_size supports: 1G, 512M, 10% (percentage of RAM)
62-
swapfc_chunk_size=512M # Size of each swap file (512M = good balance)
63-
swapfc_max_count=32 # Maximum number of swap files
64-
swapfc_min_count=1 # Minimum (1 for zswap backing)
65-
swapfc_free_ram_perc=35 # Create swap when free RAM < this %
66-
swapfc_free_swap_perc=25 # Create more swap when free swap < this %
67-
swapfc_remove_free_swap_perc=55 # Remove swap when free swap > this %
68-
swapfc_priority=50 # Swap priority (decreases per file)
69-
swapfc_path=/swapfc/swapfile # Path for swap files
70-
swapfc_frequency=1 # Check interval in seconds
48+
# Progressive Scaling: Start small, grow as needed
49+
# e.g. 256M -> 256M -> 512M -> 512M... doubles every 4 files
50+
swapfc_enabled=1 # Enable swap files
51+
swapfc_path=/swapfc/swapfile # Directory and filename prefix
52+
swapfc_chunk_size=256M # Base size
53+
swapfc_max_chunk_size=32G # Max single file size
54+
swapfc_max_count=28 # Max number of files
55+
swapfc_scaling_step=4 # Double size every X files
7156

72-
# File allocation mode:
73-
# Pre-allocated (default): Uses fallocate, reserves disk space upfront.
74-
# This is the recommended mode for stability under memory pressure.
75-
# swapfc_use_sparse=1 # Uncomment for thin provisioning (advanced, less stable)
76-
77-
# Btrfs compression mode (experimental, btrfs only):
78-
# Uses loop device over file on compressed btrfs subvolume.
79-
# Enables double compression: zswap (RAM) + btrfs zstd (disk).
80-
swapfc_use_btrfs_compression=0
57+
# Triggers
58+
swapfc_free_ram_perc=35 # Create swap when free RAM < X%
59+
swapfc_free_swap_perc=25 # Create more when free swap < X%
60+
swapfc_remove_free_swap_perc=55 # Remove files when free swap > X%
8161

8262
################################################################################
83-
# MGLRU - Multi-Gen LRU tuning (kernel 6.1+)
84-
# Protects working set from premature eviction during memory pressure.
85-
# If min_ttl_ms > 0, pages are protected for that duration before eviction.
86-
# This prevents thrashing at the cost of triggering OOM killer if memory
87-
# cannot be maintained. 0 = disabled (kernel default).
63+
# Advanced / Experimental
8864
################################################################################
8965

90-
mglru_min_ttl_ms=1000 # Protection time in ms (0=disabled, 1000=recommended)
91-
92-
################################################################################
93-
# Enable/disable settings
94-
# zswap_enabled: Set to 0 to disable zswap (default: 1, enabled when not using zram)
95-
# zram_enabled: Only used in manual mode
96-
# swapfc_enabled: Only used in manual mode
97-
################################################################################
66+
# MGLRU Anti-Thrashing (Kernel 6.1+)
67+
# Protect working set for X ms. 0 to disable.
68+
mglru_min_ttl_ms=1000
9869

99-
zswap_enabled=1
100-
zram_enabled=0
101-
swapfc_enabled=0
102-
swapd_auto_swapon=0
70+
# Sparse Files
71+
# Create files that only use disk space when actually written to.
72+
# Highly recommended for Zswap mode.
73+
# swapfc_use_sparse_disable=1 # Uncomment to disable (pre-allocate all space)

0 commit comments

Comments
 (0)