• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / catalyst-libs / 16054905839

03 Jul 2025 03:45PM UTC coverage: 65.701% (+0.02%) from 65.683%
16054905839

Pull #405

github

web-flow
Merge 30c505c79 into 31f00a4a8
Pull Request #405: fix(rust/rbac-registration): Fix `validate_txn_inputs_hash` error message

0 of 1 new or added line in 1 file covered. (0.0%)

6 existing lines in 2 files now uncovered.

10972 of 16700 relevant lines covered (65.7%)

2614.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/rust/catalyst-types/src/mmap_file.rs
1
//! Memory-mapped file.
2

3
use std::{
4
    path::Path,
5
    sync::{Arc, RwLock},
6
};
7

8
use fmmap::{MmapFile, MmapFileExt};
9
use once_cell::sync::Lazy;
10
use serde::Serialize;
11
use tracing::error;
12

13
/// Memory-mapped file.
14
pub struct MemoryMapFile {
15
    /// The memory-mapped file.
16
    file: MmapFile,
17
    /// The size of the memory-mapped file.
18
    size: u64,
19
}
20

21
/// Global statistic for memory-mapped files.
22
static MEMMAP_FILE_STATS: Lazy<Arc<RwLock<MemMapFileStat>>> =
23
    Lazy::new(|| Arc::new(RwLock::new(MemMapFileStat::default())));
×
24

25
/// Memory-mapped file statistic.
26
#[derive(Debug, Default, Clone, Serialize)]
27
pub struct MemMapFileStat {
28
    /// A counter for the number of memory-mapped files.
29
    file_count: u64,
30
    /// The total size of memory-mapped files.
31
    total_size: u64,
32
    /// The amount of time that memory-mapped files have been dropped.
33
    drop_count: u64,
34
    /// The total size of memory-mapped files that have been dropped.
35
    drop_size: u64,
36
    /// A count of errors encountered.
37
    error_count: u64,
38
}
39

40
impl MemMapFileStat {
41
    /// Get the global memory-mapped file statistics.
42
    /// Return default statistics if the mutex is poisoned.
43
    pub fn current() -> MemMapFileStat {
×
44
        if let Ok(stat) = MEMMAP_FILE_STATS.read() {
×
45
            stat.clone()
×
46
        } else {
47
            error!("RwLock read poisoned, failed to read memory-mapped file statistics.");
×
48
            MemMapFileStat::default()
×
49
        }
50
    }
×
51

52
    /// Get the statistic file count.
53
    #[must_use]
54
    pub fn file_count(&self) -> u64 {
×
55
        self.file_count
×
56
    }
×
57

58
    /// Get the statistic total size.
59
    #[must_use]
60
    pub fn total_size(&self) -> u64 {
×
61
        self.total_size
×
62
    }
×
63

64
    /// Get the statistic drop count.
65
    #[must_use]
66
    pub fn drop_count(&self) -> u64 {
×
67
        self.drop_count
×
68
    }
×
69

70
    /// Get the statistic drop size.
71
    #[must_use]
72
    pub fn drop_size(&self) -> u64 {
×
73
        self.drop_size
×
74
    }
×
75

76
    /// Get the statistic error count.
77
    #[must_use]
78
    pub fn error_count(&self) -> u64 {
×
79
        self.error_count
×
80
    }
×
81

82
    /// Update the global stats when a file is created.
83
    fn update_create_stat(size: u64) {
×
84
        if let Ok(mut stat) = MEMMAP_FILE_STATS.write() {
×
85
            stat.file_count = stat.file_count.saturating_add(1);
×
86
            stat.total_size = stat.total_size.saturating_add(size);
×
87
        } else {
×
88
            error!(
×
89
                "RwLock write poisoned, failed to update created memory-mapped file statistics."
×
90
            );
91
        }
92
    }
×
93

94
    /// Update the global stats when a file is dropped.
95
    fn update_drop_stat(size: u64) {
×
96
        if let Ok(mut stat) = MEMMAP_FILE_STATS.write() {
×
97
            stat.drop_count = stat.drop_count.saturating_add(1);
×
98
            stat.drop_size = stat.drop_size.saturating_add(size);
×
99
        } else {
×
100
            error!(
×
101
                "RwLock write poisoned, failed to update dropped memory-mapped file statistics."
×
102
            );
103
        }
104
    }
×
105

106
    /// Update the global error count when an error occurs.
107
    fn update_err_stat() {
×
108
        if let Ok(mut stat) = MEMMAP_FILE_STATS.write() {
×
109
            stat.error_count = stat.error_count.saturating_add(1);
×
110
        } else {
×
111
            error!("RwLock write poisoned, failed to update error memory-mapped file statistics.");
×
112
        }
113
    }
×
114
}
115

116
impl MemoryMapFile {
117
    /// Get the size of the memory-mapped file.
118
    pub fn size(&self) -> u64 {
×
119
        self.size
×
120
    }
×
121

122
    /// Get the memory-mapped file as a slice.
123
    pub fn as_slice(&self) -> &[u8] {
×
124
        self.file.as_slice()
×
125
    }
×
126
}
127

128
impl Drop for MemoryMapFile {
129
    fn drop(&mut self) {
×
130
        MemMapFileStat::update_drop_stat(self.size);
×
131
    }
×
132
}
133

134
impl TryFrom<&Path> for MemoryMapFile {
135
    type Error = fmmap::error::Error;
136

137
    fn try_from(path: &Path) -> Result<Self, Self::Error> {
×
138
        // Attempt to open the file with memory mapping options
×
139
        match MmapFile::open_with_options(path, fmmap::Options::new().read(true).populate()) {
×
140
            Ok(file) => {
×
141
                let len = file.len() as u64;
×
142
                let memory_map_file = MemoryMapFile { file, size: len };
×
143
                MemMapFileStat::update_create_stat(len);
×
144
                Ok(memory_map_file)
×
145
            },
146
            Err(error) => {
×
147
                MemMapFileStat::update_err_stat();
×
148
                Err(error)
×
149
            },
150
        }
151
    }
×
152
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc