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
use std::{
    fmt,
    sync::atomic::{AtomicU64, Ordering},
};

pub(crate) static COUNT_ACTIVE_SEGMENT: AtomicU64 = AtomicU64::new(0);
pub(crate) static COUNT_FTRUNCATE_FAILED: AtomicU64 = AtomicU64::new(0);
pub(crate) static COUNT_MMAP_FAILED: AtomicU64 = AtomicU64::new(0);
pub(crate) static COUNT_MUNMAP_FAILED: AtomicU64 = AtomicU64::new(0);

/// Provides few statistics about low level segment allocation.
///
/// This stats can be useful to debug or to export in various monitoring
/// systems.
#[derive(Default)]
pub struct MmapStats;

impl fmt::Debug for MmapStats {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MmapStats")
            .field("active", &self.active_segment())
            .field("ftruncate_failed", &self.ftruncate_failed())
            .field("map_failed", &self.map_failed())
            .field("unmap_failed", &self.unmap_failed())
            .finish()
    }
}

impl MmapStats {
    /// Get number of current segment mounted by this library.
    ///
    /// On linux there is a `systctl` limit you can access with:
    /// ```shell
    /// sysctl vm.max_map_count
    /// ```
    #[inline(always)]
    pub fn active_segment(&self) -> u64 {
        COUNT_ACTIVE_SEGMENT.load(Ordering::Relaxed)
    }

    /// Get number of file truncate failed.
    #[inline(always)]
    pub fn ftruncate_failed(&self) -> u64 {
        COUNT_FTRUNCATE_FAILED.load(Ordering::Relaxed)
    }

    /// Get number of segment creation failed.
    #[inline(always)]
    pub fn map_failed(&self) -> u64 {
        COUNT_MMAP_FAILED.load(Ordering::Relaxed)
    }

    /// Get number of segment deletion failed.
    #[inline(always)]
    pub fn unmap_failed(&self) -> u64 {
        COUNT_MUNMAP_FAILED.load(Ordering::Relaxed)
    }
}