Skip to content

Commit 0d0c1b8

Browse files
committed
The partition format verification has been corrected and MGLRU configuration has been improved.
1 parent 0e27b95 commit 0d0c1b8

6 files changed

Lines changed: 316 additions & 270 deletions

File tree

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ Smart dynamic swap management for Linux, written in Rust.
44

55
## Features
66

7-
- **Auto-detection**: Automatically chooses optimal swap strategy for your filesystem
8-
- **Multi-filesystem**: Supports btrfs, ext4, and xfs for swap files
7+
- **Auto-detection**: Automatically chooses optimal swap strategy based on filesystem (btrfs/ext4/xfs)
98
- **Zswap + SwapFC**: Compressed RAM cache with dynamic swap files
109
- **Zram + SwapFC**: Alternative mode with zram as primary swap
1110
- **Zram writeback**: Move idle pages from zram to disk (kernel 5.4+)
12-
- **Fallback support**: Automatically falls back to zram-only if swap files fail
11+
- **Multi-filesystem**: Supports btrfs, ext4, and xfs
1312
- **Lightweight**: ~250 KB binary vs ~10 MB Python version
1413

1514
## Swap Modes
1615

1716
| Mode | Description | Best For |
1817
|------|-------------|----------|
1918
| `auto` | Auto-detect: btrfs/ext4/xfs → zswap+swapfc, other → zram | Most users |
20-
| `zswap+swapfc` | Zswap cache + dynamic swap files | **Desktop** |
19+
| `zswap+swapfc` | Zswap cache + dynamic swap files | **Desktop (btrfs)** |
2120
| `zram+swapfc` | Zram primary + swap files overflow | Memory-constrained |
22-
| `zram` | Zram only (no disk swap) | Unsupported filesystems |
21+
| `zram` | Zram only (no disk swap) | Non-btrfs systems |
2322
| `manual` | Use explicit settings | Advanced users |
2423

2524
### How Each Mode Works
2625

2726
**zswap + swapfc (default for btrfs/ext4/xfs)**:
2827
- Zswap compresses pages in RAM before writing to swap
29-
- SwapFC creates pre-allocated swap files (512MB each)
30-
- Disk space is only used when zswap pool is full
31-
- Best desktop performance with efficient disk usage
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
3232

3333
**zram + swapfc**:
3434
- Zram creates compressed block device in RAM (highest priority)
@@ -113,13 +113,12 @@ swap_mode=auto
113113

114114
################################################################################
115115
# Zswap (used in zswap+swapfc mode)
116-
# Modern defaults for desktop Linux (kernel 6.x+)
117116
################################################################################
118-
zswap_compressor=zstd # lzo lz4 zstd lzo-rle lz4hc (zstd = best)
119-
zswap_max_pool_percent=45 # Max % of RAM for pool (20-50 typical)
120-
zswap_zpool=zsmalloc # Memory allocator (default upstream)
121-
zswap_shrinker_enabled=1 # Evict cold pages to disk (default since 6.8)
122-
zswap_accept_threshold=90 # Accept threshold after pool full
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
123122

124123
################################################################################
125124
# Zram (used in zram modes)
@@ -142,10 +141,12 @@ swapfc_free_ram_perc=35 # Create when free RAM < this %
142141
swapfc_free_swap_perc=25 # Create more when free swap < this %
143142
swapfc_path=/swapfc/swapfile # Path for swap files
144143

145-
# Pre-allocated files (default) - more stable, no loop device needed
146-
swapfc_use_sparse=0 # 0=pre-allocate (default), 1=sparse
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
147148

148-
# Btrfs compression mode (experimental) and need use loop device
149+
# Btrfs compression mode (experimental)
149150
swapfc_use_btrfs_compression=0 # Double compression: zswap + btrfs
150151
```
151152

@@ -173,22 +174,21 @@ zram_writeback_dev=/dev/sda5
173174

174175
Requires kernel compiled with `CONFIG_ZRAM_WRITEBACK`.
175176

176-
## File Allocation Mode
177+
## Sparse Files (Thin Provisioning)
177178

178-
By default, swap files are **pre-allocated** using `fallocate`:
179+
By default, swap files are created as sparse files:
179180

180-
- Files reserve 512M on disk immediately
181-
- More stable under memory pressure (no loop device needed)
182-
- Better for most desktop and server workloads
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)
183185

184-
To use sparse files (thin provisioning) instead:
186+
To disable and pre-allocate all disk space:
185187

186188
```ini
187-
swapfc_use_sparse=1
189+
swapfc_use_sparse_disable=1
188190
```
189191

190-
Sparse mode creates files that only allocate disk space when written, but requires a loop device which can cause issues under extreme memory pressure.
191-
192192
## File Locations
193193

194194
| Path | Description |

include/swap-default.conf

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
################################################################################
77
# Swap Mode - How swap is configured
88
#
9-
# auto - Auto-detect: btrfs → zswap+swapfc, non-btrfs → zram only
9+
# auto - Auto-detect: btrfs/ext4/xfs → zswap+swapfc, other → zram only
1010
# zswap+swapfc - Zswap cache + swap files (default for btrfs)
1111
# zram+swapfc - Zram (fast) + swap files for overflow
1212
# zram - Zram only (no disk swap)
@@ -24,9 +24,9 @@ swap_mode=auto
2424
################################################################################
2525

2626
zswap_compressor=zstd # lzo lz4 zstd lzo-rle lz4hc (zstd = best balance)
27-
zswap_max_pool_percent=45 # Max % of RAM for compressed pool (20-50 typical)
28-
zswap_zpool=zsmalloc # Memory allocator (zsmalloc = best, default upstream)
29-
zswap_shrinker_enabled=1 # Proactively evict cold pages to disk (default since 6.8)
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)
3030
zswap_accept_threshold=90 # Accept pages again after pool was full (%)
3131

3232
################################################################################
@@ -55,7 +55,7 @@ zram_writeback_size=1G # Size of auto-created backing file
5555
# Used in zram+swapfc, zswap+swapfc modes, or manual with swapfc_enabled=1
5656
#
5757
# Creates swap files on-demand as memory pressure increases.
58-
# If swap file creation fails, automatically falls back to zram-only mode.
58+
# Files are pre-allocated with fallocate for stability.
5959
################################################################################
6060

6161
# swapfc_chunk_size supports: 1G, 512M, 10% (percentage of RAM)
@@ -66,20 +66,29 @@ swapfc_free_ram_perc=35 # Create swap when free RAM < this %
6666
swapfc_free_swap_perc=25 # Create more swap when free swap < this %
6767
swapfc_remove_free_swap_perc=55 # Remove swap when free swap > this %
6868
swapfc_priority=50 # Swap priority (decreases per file)
69-
swapfc_path=/swapfc/swapfile # Path for swap files (must be btrfs)
69+
swapfc_path=/swapfc/swapfile # Path for swap files
7070
swapfc_frequency=1 # Check interval in seconds
7171

7272
# File allocation mode:
73-
# - Pre-allocated (default): Uses fallocate, reserves disk space upfront, no loop device
74-
# - Sparse (thin): Uses truncate, only uses disk when written, requires loop device
75-
# Pre-allocated is more stable and avoids potential deadlocks under memory pressure.
76-
swapfc_use_sparse=0 # 0=pre-allocate (stable, default), 1=sparse (loop device)
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)
7776

78-
# Btrfs compression mode: uses loop device over sparse file on compressed btrfs
79-
# This enables swap data to be compressed on disk by btrfs (zstd)
80-
# RAM: zswap compresses → Disk: btrfs compresses again (double compression!)
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).
8180
swapfc_use_btrfs_compression=0
8281

82+
################################################################################
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).
88+
################################################################################
89+
90+
mglru_min_ttl_ms=1000 # Protection time in ms (0=disabled, 1000=recommended)
91+
8392
################################################################################
8493
# Enable/disable settings
8594
# zswap_enabled: Set to 0 to disable zswap (default: 1, enabled when not using zram)

0 commit comments

Comments
 (0)