memfaultd 1.26.1

Memfault daemon for embedded Linux systems. Observability, logging, crash reporting, and updating all in one service. Learn more at https://docs.memfault.com/
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//
// Copyright (c) Memfault, Inc.
// See License.txt for details
use crate::{
    logs::log_file::{LogFile, LogFileControl},
    util::disk_size::DiskSize,
};
use chrono::{DateTime, Utc};
use eyre::Result;

#[derive(Debug)]
enum Headroom {
    Ok,
    Shortage {
        num_dropped_logs: usize,
        has_rotated: bool,
    },
}

pub trait HeadroomCheck {
    fn check<L: LogFile>(
        &mut self,
        log_timestamp: &DateTime<Utc>,
        log_file_control: &mut impl LogFileControl<L>,
    ) -> Result<bool>;
}

pub struct HeadroomLimiter {
    state: Headroom,
    /// Minimum amount of free space that must be kept available in the mount point in which
    /// log_tmp_path resides. If there is not sufficient head room, logs will be dropped.
    min_headroom: DiskSize,
    get_available_space: Box<dyn FnMut() -> Result<DiskSize> + Send>,
}

impl HeadroomLimiter {
    pub fn new<S: FnMut() -> Result<DiskSize> + Send + 'static>(
        min_headroom: DiskSize,
        get_available_space: S,
    ) -> Self {
        Self {
            state: Headroom::Ok,
            min_headroom,
            get_available_space: Box::new(get_available_space),
        }
    }
}

impl HeadroomCheck for HeadroomLimiter {
    /// Checks whether there is enough headroom to continue writing logs.
    /// If there is not enough headroom, this will flush the current log file and rotate at most
    /// once when needed, until there is enough headroom again. When there's enough space again, it
    /// will emit a log message mentioning the number of dropped logs.
    /// Returns Ok(true) if there is enough headroom, Ok(false) if there is not enough headroom.
    /// It only returns an error if there is an error writing the "Dropped N logs" message.
    fn check<L: LogFile>(
        &mut self,
        log_timestamp: &DateTime<Utc>,
        log_file_control: &mut impl LogFileControl<L>,
    ) -> Result<bool> {
        let available = (self.get_available_space)()?;
        let has_headroom = available.exceeds(&self.min_headroom);

        self.state = match (has_headroom, &self.state) {
            // Enter insufficient headroom state:
            (false, Headroom::Ok) => {
                // Best-effort warning log & flush. If this fails, just keep going.
                let current_log = log_file_control.current_log()?;
                let _ = current_log.write_log(
                    *log_timestamp,
                    "WARN",
                    match (
                        available.bytes >= self.min_headroom.bytes,
                        available.inodes >= self.min_headroom.inodes,
                    ) {
                        (false, false) => "Low on disk space and inodes. Starting to drop logs...",
                        (false, true) => "Low on disk space. Starting to drop logs...",
                        (true, false) => "Low on inodes. Starting to drop logs...",
                        _ => unreachable!(),
                    },
                );
                let _ = current_log.flush();
                Headroom::Shortage {
                    has_rotated: log_file_control.rotate_if_needed().unwrap_or(false),
                    num_dropped_logs: 1,
                }
            }
            // Already in insufficient headroom state:
            (
                false,
                Headroom::Shortage {
                    has_rotated,
                    num_dropped_logs,
                },
            ) => {
                // Rotate logs once only:
                let num_dropped_logs = *num_dropped_logs + 1;
                let has_rotated =
                    *has_rotated || log_file_control.rotate_if_needed().unwrap_or(false);
                Headroom::Shortage {
                    num_dropped_logs,
                    has_rotated,
                }
            }
            // Exit insufficient headroom state:
            (
                true,
                Headroom::Shortage {
                    num_dropped_logs, ..
                },
            ) => {
                let current_log = log_file_control.current_log()?;
                current_log.write_log(
                    *log_timestamp,
                    "INFO",
                    format!(
                        "Recovered from low disk space. Dropped {} logs.",
                        num_dropped_logs
                    ),
                )?;
                Headroom::Ok
            }
            // Already in headroom OK state and staying in this state:
            (true, Headroom::Ok) => Headroom::Ok,
        };
        Ok(has_headroom)
    }
}

#[cfg(test)]
mod tests {
    use crate::{logs::log_entry::LogEntry, util::disk_size::DiskSize};
    use std::sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    };

    use super::*;
    use chrono::TimeZone;
    use eyre::eyre;
    use rstest::{fixture, rstest};

    #[rstest]
    fn returns_true_if_headroom_ok_and_stays_ok(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl::default();
        fixture.set_available_space(MIN_HEADROOM);

        // Enough headroom: check() returns true and no calls to log_file_control are made:
        assert!(fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());
        assert_eq!(0, log_file_control.logs_written.len());
        assert_eq!(0, log_file_control.flush_count);
        assert_eq!(0, log_file_control.rotation_count);
    }

    #[rstest]
    fn log_upon_enter_and_exit_headroom_space_shortage(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl::default();

        // Enter headroom shortage: check() returns false:
        fixture.set_available_space(MIN_HEADROOM - 1);
        assert!(!fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());

        // Check that the warning log was written:
        assert_eq!(1, log_file_control.logs_written.len());
        assert!(log_file_control.logs_written[0]
            .contains("Low on disk space. Starting to drop logs..."));
        // Check that the log was flushed:
        assert_eq!(1, log_file_control.flush_count);

        // Still not enough headroom: check() returns false:
        assert!(!fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());

        // Recover from headroom shortage: check() returns true again:
        fixture.set_available_space(MIN_HEADROOM);
        assert!(fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());

        // Check that the "recovered" log was written:
        assert_eq!(2, log_file_control.logs_written.len());
        assert!(log_file_control.logs_written[1]
            .contains("Recovered from low disk space. Dropped 2 logs."));
    }

    #[rstest]
    fn log_upon_enter_and_exit_headroom_node_shortage(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl::default();

        // Enter headroom shortage: check() returns false:
        fixture.set_available_inodes(MIN_INODES - 1);
        assert!(!fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());

        // Check that the warning log was written:
        assert_eq!(1, log_file_control.logs_written.len());
        assert!(
            log_file_control.logs_written[0].contains("Low on inodes. Starting to drop logs...")
        );
        // Check that the log was flushed:
        assert_eq!(1, log_file_control.flush_count);

        // Still not enough headroom: check() returns false:
        assert!(!fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());

        // Recover from headroom shortage: check() returns true again:
        fixture.set_available_inodes(MIN_INODES);
        assert!(fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap());

        // Check that the "recovered" log was written:
        assert_eq!(2, log_file_control.logs_written.len());
        assert!(log_file_control.logs_written[1]
            .contains("Recovered from low disk space. Dropped 2 logs."));
    }

    #[rstest]
    fn rotate_once_only_entering_headroom_shortage(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl {
            // Make log_file_control.rotate_if_needed() return Ok(true):
            rotate_return: Some(true),
            ..Default::default()
        };

        // Enter headroom shortage:
        fixture.set_available_space(MIN_HEADROOM - 1);
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 1);

        // Check again. Rotation should not be attempted again:
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 1);
    }

    #[rstest]
    fn rotate_once_only_during_headroom_shortage(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl::default();

        // Enter headroom shortage:
        fixture.set_available_space(MIN_HEADROOM - 1);
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 0);

        // Make log_file_control.rotate_if_needed() return Ok(true):
        log_file_control.rotate_return = Some(true);

        // Check again. Rotation should be attempted again:
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 1);

        // Check again. Rotation should not be attempted again:
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 1);
    }

    #[rstest]
    fn retry_rotate_after_failure(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl {
            // Make log_file_control.rotate_if_needed() return Err(...):
            rotate_return: None,
            ..Default::default()
        };

        // Enter headroom shortage:
        fixture.set_available_space(MIN_HEADROOM - 1);
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 0);

        // Check again. Rotation should be attempted again:
        // Make log_file_control.rotate_if_needed() return Ok(true):
        log_file_control.rotate_return = Some(true);
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        assert_eq!(log_file_control.rotation_count, 1);
    }

    #[rstest]
    fn write_error_of_initial_warning_message_is_ignored(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl::default();

        fixture.set_available_space(MIN_HEADROOM - 1);
        log_file_control.write_should_fail = true;
        assert!(fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .is_ok());
    }

    #[rstest]
    fn write_error_of_recovery_log_message_is_bubbled_up(mut fixture: Fixture) {
        let log_timestamp = build_date_time();
        let mut log_file_control = FakeLogFileControl::default();

        fixture.set_available_space(MIN_HEADROOM - 1);
        fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .unwrap();
        fixture.set_available_space(MIN_HEADROOM);
        log_file_control.write_should_fail = true;
        assert!(fixture
            .limiter
            .check(&log_timestamp, &mut log_file_control)
            .is_err());
    }

    struct FakeLogFileControl {
        logs_written: Vec<String>,
        write_should_fail: bool,
        flush_count: usize,
        flush_should_fail: bool,
        /// This controls the result of rotate_if_needed().
        /// Some(...) is mapped to Ok(...) and None is mapped to Err(...).
        rotate_return: Option<bool>,
        /// Number of times actually rotated (rotate_if_needed() calls while rotate_return was Some(true)):
        rotation_count: usize,
    }

    impl Default for FakeLogFileControl {
        fn default() -> Self {
            FakeLogFileControl {
                logs_written: Vec::new(),
                flush_count: 0,
                flush_should_fail: false,
                write_should_fail: false,
                rotate_return: Some(false),
                rotation_count: 0,
            }
        }
    }

    impl LogFile for FakeLogFileControl {
        fn write_json_line(&mut self, json: LogEntry) -> Result<()> {
            if self.write_should_fail {
                Err(eyre!("Write failed"))
            } else {
                self.logs_written.push(serde_json::to_string(&json)?);
                Ok(())
            }
        }

        fn flush(&mut self) -> Result<()> {
            self.flush_count += 1;
            if self.flush_should_fail {
                Err(eyre!("Flush failed"))
            } else {
                Ok(())
            }
        }
    }

    impl LogFileControl<FakeLogFileControl> for FakeLogFileControl {
        fn rotate_if_needed(&mut self) -> Result<bool> {
            match self.rotate_return {
                Some(rv) => {
                    if rv {
                        self.rotation_count += 1;
                    }
                    Ok(rv)
                }
                None => Err(eyre!("Rotate failed")),
            }
        }

        fn rotate_unless_empty(&mut self) -> Result<()> {
            unimplemented!();
        }

        fn current_log(&mut self) -> Result<&mut FakeLogFileControl> {
            Ok(self)
        }

        fn close(self) -> Result<()> {
            Ok(())
        }
    }

    struct Fixture {
        available_space: Arc<AtomicU64>,
        available_inodes: Arc<AtomicU64>,
        limiter: HeadroomLimiter,
    }

    impl Fixture {
        fn set_available_space(&mut self, available_space: u64) {
            self.available_space
                .store(available_space, Ordering::Relaxed)
        }
        fn set_available_inodes(&mut self, available_inodes: u64) {
            self.available_inodes
                .store(available_inodes, Ordering::Relaxed)
        }
    }

    const MIN_HEADROOM: u64 = 1024;
    const MIN_INODES: u64 = 10;
    const INITIAL_AVAILABLE_SPACE: u64 = 1024 * 1024;
    const INITIAL_AVAILABLE_INODES: u64 = 100;

    #[fixture]
    fn fixture() -> Fixture {
        let available_space = Arc::new(AtomicU64::new(INITIAL_AVAILABLE_SPACE));
        let available_inodes = Arc::new(AtomicU64::new(INITIAL_AVAILABLE_INODES));

        let space = available_space.clone();
        let inodes = available_inodes.clone();

        Fixture {
            limiter: HeadroomLimiter::new(
                DiskSize {
                    bytes: MIN_HEADROOM,
                    inodes: MIN_INODES,
                },
                move || {
                    Ok(DiskSize {
                        bytes: space.load(Ordering::Relaxed),
                        inodes: inodes.load(Ordering::Relaxed),
                    })
                },
            ),
            available_inodes,
            available_space,
        }
    }

    fn build_date_time() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(1990, 12, 16, 12, 0, 0).unwrap()
    }
}