mmap_vec/
stats.rs

1use std::{
2    fmt,
3    sync::atomic::{AtomicU64, Ordering},
4};
5
6pub(crate) static COUNT_ACTIVE_SEGMENT: AtomicU64 = AtomicU64::new(0);
7pub(crate) static COUNT_FTRUNCATE_FAILED: AtomicU64 = AtomicU64::new(0);
8pub(crate) static COUNT_MMAP_FAILED: AtomicU64 = AtomicU64::new(0);
9pub(crate) static COUNT_MUNMAP_FAILED: AtomicU64 = AtomicU64::new(0);
10
11/// Provides few statistics about low level segment allocation.
12///
13/// This stats can be useful to debug or to export in various monitoring
14/// systems.
15#[derive(Default)]
16pub struct MmapStats;
17
18impl fmt::Debug for MmapStats {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        f.debug_struct("MmapStats")
21            .field("active", &self.active_segment())
22            .field("ftruncate_failed", &self.ftruncate_failed())
23            .field("map_failed", &self.map_failed())
24            .field("unmap_failed", &self.unmap_failed())
25            .finish()
26    }
27}
28
29impl MmapStats {
30    /// Get number of current segment mounted by this library.
31    ///
32    /// On linux there is a `sysctl` limit you can access with:
33    /// ```shell
34    /// sysctl vm.max_map_count
35    /// ```
36    #[inline(always)]
37    pub fn active_segment(&self) -> u64 {
38        COUNT_ACTIVE_SEGMENT.load(Ordering::Relaxed)
39    }
40
41    /// Get number of file truncate failed.
42    #[inline(always)]
43    pub fn ftruncate_failed(&self) -> u64 {
44        COUNT_FTRUNCATE_FAILED.load(Ordering::Relaxed)
45    }
46
47    /// Get number of segment creation failed.
48    #[inline(always)]
49    pub fn map_failed(&self) -> u64 {
50        COUNT_MMAP_FAILED.load(Ordering::Relaxed)
51    }
52
53    /// Get number of segment deletion failed.
54    #[inline(always)]
55    pub fn unmap_failed(&self) -> u64 {
56        COUNT_MUNMAP_FAILED.load(Ordering::Relaxed)
57    }
58}