ram-sentinel 0.2.0

A surgical OOM prevention daemon for Linux desktops. Configurably monitors RAM, swap, and/or PSI (Pressure Stall Information) to selectively kill low-priority processes (e.g., browser tabs) before the system freezes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::config_error::ConfigError;
use crate::events::{LogLevel, SentinelEvent};
use crate::logging;
use crate::psi;
use crate::utils::parse_size;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Config {
    // Metric Triggers
    pub psi: Option<psi::PsiConfig>,
    pub ram: Option<MemoryConfig>,
    pub swap: Option<MemoryConfig>,
    pub zram: Option<MemoryConfig>,

    // Operational Settings
    #[serde(default = "default_interval")]
    pub check_interval_ms: u64,
    #[serde(default = "warn_interval")]
    pub warn_reset_ms: u64,
    #[serde(default = "sigterm_wait_ms")]
    pub sigterm_wait_ms: u64,

    // Targeting Logic
    #[serde(default = "default_ignore_targets")]
    pub ignore_targets: Vec<String>,

    #[serde(default = "default_kill_targets")]
    pub kill_targets: Vec<String>,

    #[serde(default = "default_strategy")]
    pub kill_strategy: KillStrategy,

    #[serde(default = "default_allow_kill_outside_targets")]
    pub allow_kill_outside_targets: bool,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct MemoryConfig {
    pub warn_min_free_bytes: Option<String>,
    pub warn_min_free_percent: Option<f32>,
    pub kill_min_free_bytes: Option<String>,
    pub kill_min_free_percent: Option<f32>,
}
#[derive(Debug, Clone)]
pub struct MemoryConfigParsed {
    pub warn_min_free_bytes: Option<u64>,
    pub warn_min_free_percent: Option<f32>,
    pub kill_min_free_bytes: Option<u64>,
    pub kill_min_free_percent: Option<f32>,
}

impl MemoryConfigParsed {
    pub fn try_from_config(config: MemoryConfig) -> Result<Self, ConfigError> {
        let warn_min_free_bytes = if let Some(s) = config.warn_min_free_bytes.as_ref() {
            Some(parse_size(s).ok_or_else(|| {
                ConfigError::InvalidSize("warn_min_free_bytes".to_string(), s.clone())
            })?)
        } else {
            None
        };

        let kill_min_free_bytes = if let Some(s) = config.kill_min_free_bytes.as_ref() {
            Some(parse_size(s).ok_or_else(|| {
                ConfigError::InvalidSize("kill_min_free_bytes".to_string(), s.clone())
            })?)
        } else {
            None
        };

        if let Some(p) = config.warn_min_free_percent {
            if !(0.0..=100.0).contains(&p) {
                return Err(ConfigError::InvalidPercent(
                    "warn_min_free_percent".to_string(),
                    p,
                ));
            }
        }

        if let Some(p) = config.kill_min_free_percent {
            if !(0.0..=100.0).contains(&p) {
                return Err(ConfigError::InvalidPercent(
                    "kill_min_free_percent".to_string(),
                    p,
                ));
            }
        }

        Ok(Self {
            warn_min_free_bytes,
            warn_min_free_percent: config.warn_min_free_percent,
            kill_min_free_bytes,
            kill_min_free_percent: config.kill_min_free_percent,
        })
    }
}

#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum KillStrategy {
    LargestRss,
    HighestOomScore,
}

impl MemoryConfig {
    fn is_effectively_empty(&self) -> bool {
        self.warn_min_free_bytes.is_none()
            && self.warn_min_free_percent.is_none()
            && self.kill_min_free_bytes.is_none()
            && self.kill_min_free_percent.is_none()
    }
}

// Default Generators
fn default_interval() -> u64 {
    1000
}
fn warn_interval() -> u64 {
    30000
}
fn sigterm_wait_ms() -> u64 {
    5000
}
fn default_strategy() -> KillStrategy {
    KillStrategy::HighestOomScore
}
fn default_kill_targets() -> Vec<String> {
    vec!["type=renderer".to_string(), "-contentproc".to_string()]
}
fn default_allow_kill_outside_targets() -> bool {
    false
}
fn default_ignore_targets() -> Vec<String> {
    vec!["ram-sentinel".to_string()]
}

#[derive(Debug)]
pub struct RuntimeContext {
    pub psi: Option<psi::PsiConfigParsed>,
    pub ram: Option<MemoryConfigParsed>,
    pub swap: Option<MemoryConfigParsed>,
    pub zram: Option<MemoryConfigParsed>,

    pub check_interval_ms: u64,
    pub warn_reset_ms: u64,
    pub sigterm_wait_ms: u64,

    pub kill_strategy: KillStrategy,

    pub ignore_targets_regex: Vec<TargetPattern>,
    pub kill_targets_regex: Vec<TargetPattern>,
    pub allow_kill_outside_targets: bool,
}

#[derive(Debug)]
pub struct TargetPattern {
    pub cgroup: Option<Pattern>,
    pub cmdline: Pattern,
}

#[derive(Debug)]
pub enum Pattern {
    Literal(String),
    Regex(Regex),
    StartsWith(String),
}

impl Pattern {
    pub fn matches(&self, s: &str) -> bool {
        match self {
            Pattern::Literal(lit) => s.contains(lit),
            Pattern::Regex(re) => re.is_match(s),
            Pattern::StartsWith(prefix) => s.starts_with(prefix),
        }
    }
}

impl Config {
    pub fn load(cli_config_path: Option<PathBuf>) -> Result<RuntimeContext, ConfigError> {
        let config = Self::load_raw_validated(cli_config_path)?;

        // Optimization: Compile Regex patterns
        let ignore_targets_regex = compile_patterns(&config.ignore_targets, "ignore_targets")?;
        let kill_targets_regex = compile_patterns(&config.kill_targets, "kill_targets")?;

        let psi_parsed = if let Some(p) = config.psi {
            let parsed = psi::PsiConfigParsed::try_from_config(p, config.check_interval_ms)
                .map_err(|e| ConfigError::PsiConfig(e.to_string()))?;

            if let Err(e) = psi::validate_psi_availability() {
                return Err(ConfigError::PsiUnavailable(e.to_string()));
            }
            Some(parsed)
        } else {
            None
        };

        let ram_parsed = if let Some(r) = config.ram {
            Some(MemoryConfigParsed::try_from_config(r)?)
        } else {
            None
        };

        let swap_parsed = if let Some(s) = config.swap {
            Some(MemoryConfigParsed::try_from_config(s)?)
        } else {
            None
        };

        let zram_parsed = if let Some(z) = config.zram {
            Some(MemoryConfigParsed::try_from_config(z)?)
        } else {
            None
        };

        Ok(RuntimeContext {
            psi: psi_parsed,
            ram: ram_parsed,
            swap: swap_parsed,
            zram: zram_parsed,
            check_interval_ms: config.check_interval_ms,
            warn_reset_ms: config.warn_reset_ms,
            sigterm_wait_ms: config.sigterm_wait_ms,
            kill_strategy: config.kill_strategy,
            ignore_targets_regex,
            kill_targets_regex,
            allow_kill_outside_targets: config.allow_kill_outside_targets,
        })
    }

    pub fn load_raw_validated(cli_config_path: Option<PathBuf>) -> Result<Config, ConfigError> {
        let mut config = match cli_config_path {
            Some(path) => {
                if !path.exists() {
                    return Err(ConfigError::ConfigFileNotFound(path));
                }
                Self::parse_file(&path)?
            }
            None => Self::find_and_load_config()?,
        };

        config.validate()?;
        Ok(config)
    }

    fn find_and_load_config() -> Result<Config, ConfigError> {
        if let Some(config_home) =
            directories::BaseDirs::new().map(|b| b.config_dir().to_path_buf())
        {
            let path = config_home.join("ram-sentinel.toml");
            if path.exists() {
                return Self::parse_file(&path);
            }
        }

        logging::emit(&SentinelEvent::Message {
            level: LogLevel::Info,
            text: "No configuration file found. Loading sane defaults.",
        });
        Ok(Self::sane_defaults())
    }

    fn parse_file(path: &Path) -> Result<Config, ConfigError> {
        let content =
            fs::read_to_string(path).map_err(|e| ConfigError::FileRead(path.to_path_buf(), e))?;

        toml::from_str(&content)
            .map_err(|e| ConfigError::FileParse(path.to_path_buf(), e.to_string()))
    }

    pub fn sane_defaults() -> Config {
        Config {
            psi: Some(psi::PsiConfig {
                warn_max_percent: None,
                kill_max_percent: None,
                amount_to_free: None,
                check_interval_ms: None,
            }),
            ram: Some(MemoryConfig {
                warn_min_free_bytes: None,
                warn_min_free_percent: Some(10.0),
                kill_min_free_bytes: None,
                kill_min_free_percent: Some(5.0),
            }),
            swap: Some(MemoryConfig {
                warn_min_free_bytes: None,
                warn_min_free_percent: None,
                kill_min_free_bytes: None,
                kill_min_free_percent: None,
            }),
            zram: Some(MemoryConfig {
                warn_min_free_bytes: None,
                warn_min_free_percent: None,
                kill_min_free_bytes: None,
                kill_min_free_percent: None,
            }),
            check_interval_ms: default_interval(),
            warn_reset_ms: warn_interval(),
            sigterm_wait_ms: sigterm_wait_ms(),
            ignore_targets: default_ignore_targets(),
            kill_targets: default_kill_targets(),
            kill_strategy: default_strategy(),
            allow_kill_outside_targets: default_allow_kill_outside_targets(),
        }
    }

    fn validate(&mut self) -> Result<(), ConfigError> {
        let psi_empty = self.psi.as_ref().map_or(true, |p| p.is_effectively_empty());
        let ram_empty = self.ram.as_ref().map_or(true, |r| r.is_effectively_empty());
        let swap_empty = self
            .swap
            .as_ref()
            .map_or(true, |s| s.is_effectively_empty());
        let zram_empty = self
            .zram
            .as_ref()
            .map_or(true, |z| z.is_effectively_empty());

        if psi_empty && ram_empty && swap_empty && zram_empty {
            return Err(ConfigError::EffectiveEmpty);
        }

        // Detailed Validation
        if let Some(r) = self.ram.as_ref() {
            MemoryConfigParsed::try_from_config(r.clone())?;
        }

        if let Some(s) = self.swap.as_ref() {
            MemoryConfigParsed::try_from_config(s.clone())?;
        }

        if let Some(z) = self.zram.as_ref() {
            MemoryConfigParsed::try_from_config(z.clone())?;
        }

        if let Some(p) = self.psi.as_ref() {
            psi::PsiConfigParsed::try_from_config(p.clone(), self.check_interval_ms)
                .map_err(|e| ConfigError::PsiConfig(e.to_string()))?;
        }

        // Validate Patterns
        compile_patterns(&self.ignore_targets, "ignore_targets")?;
        compile_patterns(&self.kill_targets, "kill_targets")?;

        if psi_empty {
            self.psi = None;
        }
        if ram_empty {
            self.ram = None;
        }
        if swap_empty {
            self.swap = None;
        }
        if zram_empty {
            self.zram = None;
        }

        if self.check_interval_ms > 300000 {
            return Err(ConfigError::IntervalTooHigh(self.check_interval_ms));
        }

        if self.check_interval_ms < 100 {
            return Err(ConfigError::IntervalTooLow(self.check_interval_ms));
        }

        Ok(())
    }
}

fn compile_patterns(raw: &[String], field_name: &str) -> Result<Vec<TargetPattern>, ConfigError> {
    let mut patterns = Vec::new();
    for (i, raw_str) in raw.iter().enumerate() {
        if raw_str.starts_with("@[") {
            if let Some(end_bracket) = raw_str.find("]@ ") {
                let cgroup_part = &raw_str[2..end_bracket];
                let cmdline_part = &raw_str[end_bracket + 3..];

                let cgroup_pat = parse_single_pattern(cgroup_part, field_name, i, raw_str)?;
                let cmdline_pat = parse_single_pattern(cmdline_part, field_name, i, raw_str)?;

                patterns.push(TargetPattern {
                    cgroup: Some(cgroup_pat),
                    cmdline: cmdline_pat,
                });
                continue;
            } else {
                return Err(ConfigError::FileParse(
                    PathBuf::from("config"),
                    format!(
                        "Invalid pattern in {}: missing mandatory closing ']@ ' in '{}'",
                        field_name, raw_str
                    ),
                ));
            }
        }

        // Fallback or explicit cmdline-only
        let cmdline_pat = parse_single_pattern(raw_str, field_name, i, raw_str)?;
        patterns.push(TargetPattern {
            cgroup: None,
            cmdline: cmdline_pat,
        });
    }
    Ok(patterns)
}

fn parse_single_pattern(
    s: &str,
    field_name: &str,
    index: usize,
    raw_full: &str,
) -> Result<Pattern, ConfigError> {
    if s.starts_with('/') && s.ends_with('/') && s.len() > 2 {
        let regex_str = &s[1..s.len() - 1];
        match Regex::new(regex_str) {
            Ok(re) => Ok(Pattern::Regex(re)),
            Err(e) => Err(ConfigError::RegexError(
                field_name.to_string(),
                index,
                raw_full.to_string(),
                e.to_string(),
            )),
        }
    } else if s.starts_with('^') && s.len() > 1 {
        Ok(Pattern::StartsWith(s[1..].to_string()))
    } else {
        Ok(Pattern::Literal(s.to_string()))
    }
}