Skip to content

Commit 996252c

Browse files
committed
Use mglru_min_ttl_ms=1000 as default
1 parent 9e7eb04 commit 996252c

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

include/swap-default.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ swap_mode=auto
8888
## swapfile_loop_scheduler=auto # I/O scheduler: auto, kyber, bfq, mq-deadline, none
8989
## swapfile_loop_backing_fs=auto # Backing filesystem: auto (host fs), f2fs
9090

91+
################################################################################
92+
# MGLRU (Multi-Gen LRU) Settings
93+
#
94+
# MGLRU protects the working set from premature eviction (kernel 6.1+).
95+
# min_ttl_ms sets the minimum age before a page can be reclaimed.
96+
# Higher values = more protection, less reclaim.
97+
################################################################################
98+
99+
## mglru_min_ttl_ms=1000 # Pages younger than this (ms) are never reclaimed
100+

src/autoconfig.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub struct RecommendedConfig {
9292
pub swapfc_free_ram_perc: u8,
9393
pub swapfc_free_swap_perc: u8,
9494
pub swapfc_remove_free_swap_perc: u8,
95+
96+
// MGLRU settings
97+
pub mglru_min_ttl_ms: u32,
9598
}
9699

97100
impl Default for RecommendedConfig {
@@ -112,6 +115,7 @@ impl RecommendedConfig {
112115
swapfc_free_ram_perc: defaults::SWAPFILE_FREE_RAM_PERC,
113116
swapfc_free_swap_perc: defaults::SWAPFILE_FREE_SWAP_PERC,
114117
swapfc_remove_free_swap_perc: defaults::SWAPFILE_REMOVE_FREE_SWAP_PERC,
118+
mglru_min_ttl_ms: defaults::MGLRU_MIN_TTL_MS,
115119
}
116120
}
117121

@@ -129,6 +133,7 @@ impl RecommendedConfig {
129133
swapfc_free_ram_perc: defaults::SWAPFILE_FREE_RAM_PERC,
130134
swapfc_free_swap_perc: defaults::SWAPFILE_FREE_SWAP_PERC,
131135
swapfc_remove_free_swap_perc: defaults::SWAPFILE_REMOVE_FREE_SWAP_PERC,
136+
mglru_min_ttl_ms: defaults::MGLRU_MIN_TTL_MS,
132137
}
133138
}
134139

@@ -160,6 +165,9 @@ impl RecommendedConfig {
160165
]);
161166
}
162167

168+
// MGLRU: always inject so auto mode configures it
169+
pairs.push(("mglru_min_ttl_ms", self.mglru_min_ttl_ms.to_string()));
170+
163171
pairs
164172
}
165173

src/defaults.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ pub const SWAPFILE_FREQUENCY: u32 = 1;
4040
pub const SWAPFILE_SHRINK_THRESHOLD: u8 = 30;
4141
pub const SWAPFILE_SAFE_HEADROOM: u8 = 40;
4242
pub const SWAPFILE_NOCOW: &str = "1";
43+
44+
// ── MGLRU (Multi-Gen LRU) ──────────────────────────────────────────────────
45+
46+
pub const MGLRU_MIN_TTL_MS: u32 = 1000;

src/main.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,39 @@ fn disable_zswap_for_zram() {
160160
}
161161

162162

163+
/// Configure MGLRU anti-thrashing protection
164+
/// Sets min_ttl_ms which protects the working set from premature eviction
165+
fn configure_mglru(config: &Config, recommended: Option<&RecommendedConfig>) {
166+
const MGLRU_MIN_TTL_PATH: &str = "/sys/kernel/mm/lru_gen/min_ttl_ms";
167+
168+
// Check if MGLRU is available
169+
if !Path::new(MGLRU_MIN_TTL_PATH).exists() {
170+
return;
171+
}
172+
173+
// Get configured value, or use recommended value from autoconfig
174+
let min_ttl_ms: u32 = config
175+
.get_opt("mglru_min_ttl_ms")
176+
.and_then(|v| v.parse::<u32>().ok())
177+
.unwrap_or_else(|| {
178+
recommended
179+
.map(|r| r.mglru_min_ttl_ms)
180+
.unwrap_or(defaults::MGLRU_MIN_TTL_MS)
181+
});
182+
183+
if min_ttl_ms == 0 {
184+
return;
185+
}
186+
187+
// Apply the setting
188+
match fs::write(MGLRU_MIN_TTL_PATH, min_ttl_ms.to_string()) {
189+
Ok(_) => info!(
190+
"MGLRU: min_ttl_ms = {} (working set protection)",
191+
min_ttl_ms
192+
),
193+
Err(e) => warn!("MGLRU: failed to set min_ttl_ms: {}", e),
194+
}
195+
}
163196

164197
/// Start the swap daemon
165198
fn start() -> Result<(), Box<dyn std::error::Error>> {
@@ -208,6 +241,9 @@ fn start() -> Result<(), Box<dyn std::error::Error>> {
208241
config.apply_autoconfig(&recommended);
209242
}
210243

244+
// Configure MGLRU early (protects working set during swap operations)
245+
configure_mglru(&config, Some(&recommended));
246+
211247
// Determine effective mode
212248
let effective_mode = match swap_mode {
213249
SwapMode::Auto => match recommended.swap_mode {

0 commit comments

Comments
 (0)