entropyfs 0.4.0

Entropy-native Linux filesystem: persist irreducible state, materialize structure, preserve exact bytes.
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
//! Environment and context capture for benchmark evidence (§1, §42, §50).
//!
//! Every benchmark run records the full reproducibility context: git
//! revision, `Cargo.lock` hash, kernel, CPU model and feature set, governor,
//! memory, storage device, cache state, and the exact command line. JSON via
//! serde — evidence artifacts are human-readable; the permanent on-disk
//! format is explicit byte codecs elsewhere.

#![forbid(unsafe_code)]

use std::collections::BTreeMap;
use std::path::Path;
use std::process::Command;

use serde::{Deserialize, Serialize};

/// Full benchmark context (§1 of `docs/performance/methodology.md`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Environment {
    /// Unix seconds at capture.
    pub timestamp_unix: u64,
    /// Short git revision (best-effort).
    pub revision_short: String,
    /// Full git revision (best-effort).
    pub revision_full: String,
    /// BLAKE3 of `Cargo.lock` (hex).
    pub cargo_lock_hash: String,
    /// `Cargo.lock` size in bytes.
    pub cargo_lock_bytes: u64,
    /// Kernel ostype (`/proc/sys/kernel/ostype`).
    pub kernel_ostype: String,
    /// Kernel release (`/proc/sys/kernel/osrelease`).
    pub kernel_release: String,
    /// Kernel build version (`/proc/sys/kernel/version`).
    pub kernel_version: String,
    /// CPU model name (`/proc/cpuinfo`, first block).
    pub cpu_model: String,
    /// CPU feature flags (`/proc/cpuinfo`, first block).
    pub cpu_flags: String,
    /// Logical CPU count.
    pub cpu_count: usize,
    /// Nominal CPU frequency (MHz, best-effort from `/proc/cpuinfo`).
    pub cpu_mhz: Option<f64>,
    /// Total RAM (`/proc/meminfo` MemTotal, KiB).
    pub memory_kib: u64,
    /// CPU frequency governor (`scaling_governor`; "unknown" if absent).
    pub governor: String,
    /// Directory containing the benchmark stores.
    pub store_dir: String,
    /// Block device backing `store_dir` (`/proc/mounts`), e.g. `/dev/nvme1n1p1`.
    pub store_device: String,
    /// Filesystem type backing `store_dir`.
    pub store_fstype: String,
    /// Cache state: cold (dropped), warm (retained), or retained/unknown.
    pub cache_state: String,
    /// Exact command line (argv).
    pub command: String,
    /// Optimization policy mode (`balanced`, `capacity`, …).
    pub policy_mode: String,
    /// User id running the benchmark.
    pub uid: u32,
    /// Hostname.
    pub hostname: String,
}

/// `/proc/diskstats` counters for one block device.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DiskStats {
    /// Read operations issued.
    pub reads: u64,
    /// Sectors read.
    pub read_sectors: u64,
    /// Write operations issued.
    pub writes: u64,
    /// Sectors written.
    pub write_sectors: u64,
}

/// Device-level delta between two `DiskStats` samples.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiskDelta {
    /// Device name (e.g. `nvme1n1p1`).
    pub device: String,
    /// Reads issued at the device.
    pub reads: u64,
    /// Sectors read at the device.
    pub read_sectors: u64,
    /// Writes issued at the device.
    pub writes: u64,
    /// Sectors written at the device.
    pub write_sectors: u64,
}

impl DiskDelta {
    /// Bytes written at the device level (sectors × 512).
    pub fn written_bytes(&self) -> u64 {
        self.write_sectors * 512
    }

    /// Bytes read at the device level (sectors × 512).
    pub fn read_bytes(&self) -> u64 {
        self.read_sectors * 512
    }
}

/// Simple descriptive statistics over a latency/throughput sample.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)]
pub struct StatSummary {
    /// Number of samples.
    pub count: usize,
    /// Arithmetic mean.
    pub mean: f64,
    /// Minimum.
    pub min: f64,
    /// Median (p50).
    pub p50: f64,
    /// 95th percentile.
    pub p95: f64,
    /// 99th percentile.
    pub p99: f64,
    /// Maximum.
    pub max: f64,
}

impl Environment {
    /// Capture the full context.
    pub fn capture(
        repo_root: &Path,
        store_dir: &Path,
        cache_state: &str,
        policy_mode: &str,
    ) -> Environment {
        let (dev, fstype) = mount_of(store_dir);
        let cpu = cpu_info();
        let clock = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let lock_path = repo_root.join("Cargo.lock");
        let (lock_hash, lock_bytes) = std::fs::read(&lock_path)
            .map(|b| (blake3::hash(&b).to_hex().to_string(), b.len() as u64))
            .unwrap_or_else(|_| (String::new(), 0));
        Environment {
            timestamp_unix: clock,
            revision_short: git_output(&["rev-parse", "--short", "HEAD"]),
            revision_full: git_output(&["rev-parse", "HEAD"]),
            cargo_lock_hash: lock_hash,
            cargo_lock_bytes: lock_bytes,
            kernel_ostype: read_trimmed("/proc/sys/kernel/ostype"),
            kernel_release: read_trimmed("/proc/sys/kernel/osrelease"),
            kernel_version: read_trimmed("/proc/sys/kernel/version"),
            cpu_model: cpu.model,
            cpu_flags: cpu.flags,
            cpu_count: cpu.count,
            cpu_mhz: cpu.mhz,
            memory_kib: meminfo_total_kib(),
            governor: read_trimmed("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"),
            store_dir: store_dir.display().to_string(),
            store_device: dev,
            store_fstype: fstype,
            cache_state: cache_state.to_string(),
            command: std::env::args().collect::<Vec<_>>().join(" "),
            policy_mode: policy_mode.to_string(),
            uid: current_uid(),
            hostname: read_trimmed("/proc/sys/kernel/hostname"),
        }
    }

    /// Render as pretty JSON.
    pub fn to_json(&self) -> String {
        serde_json::to_string_pretty(self).unwrap_or_else(|_| "{}".into())
    }
}

/// Sample `/proc/diskstats` for the named device.
pub fn diskstats(device: &str) -> Option<DiskStats> {
    let body = std::fs::read_to_string("/proc/diskstats").ok()?;
    for line in body.lines() {
        let mut it = line.split_whitespace();
        let (_major, _minor, name) = (it.next()?, it.next()?, it.next()?);
        if name != device {
            continue;
        }
        let v: Vec<u64> = it.filter_map(|f| f.parse().ok()).collect();
        if v.len() >= 6 {
            return Some(DiskStats {
                reads: v[0],
                read_sectors: v[2],
                writes: v[4],
                write_sectors: v[6],
            });
        }
        return None;
    }
    None
}

/// Delta between two samples (saturating).
pub fn disk_delta(device: &str, before: &DiskStats, after: &DiskStats) -> DiskDelta {
    DiskDelta {
        device: device.to_string(),
        reads: after.reads.saturating_sub(before.reads),
        read_sectors: after.read_sectors.saturating_sub(before.read_sectors),
        writes: after.writes.saturating_sub(before.writes),
        write_sectors: after.write_sectors.saturating_sub(before.write_sectors),
    }
}

/// Nearest-rank percentile of a *sorted* sample.
pub fn percentile(sorted: &[f64], p: f64) -> f64 {
    if sorted.is_empty() {
        return 0.0;
    }
    let idx = ((p / 100.0) * sorted.len() as f64).ceil() as usize;
    sorted[idx.saturating_sub(1).min(sorted.len() - 1)]
}

/// Summary over an unsorted sample.
pub fn summary(vals: &[f64]) -> StatSummary {
    if vals.is_empty() {
        return StatSummary::default();
    }
    let mut sorted = vals.to_vec();
    sorted.sort_by(|a, b| a.total_cmp(b));
    let mean = sorted.iter().sum::<f64>() / sorted.len() as f64;
    StatSummary {
        count: sorted.len(),
        mean,
        min: sorted[0],
        p50: percentile(&sorted, 50.0),
        p95: percentile(&sorted, 95.0),
        p99: percentile(&sorted, 99.0),
        max: *sorted.last().unwrap(),
    }
}

/// The block device and filesystem type backing `path` (longest mount-point
/// prefix match over `/proc/mounts`).
pub fn mount_of(path: &Path) -> (String, String) {
    let canon = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    let target = canon.display().to_string();
    let mut best: Option<(String, String, usize)> = None;
    if let Ok(body) = std::fs::read_to_string("/proc/mounts") {
        for line in body.lines() {
            let mut it = line.split_whitespace();
            let dev = it.next().unwrap_or_default().to_string();
            let mp = it.next().unwrap_or_default().replace("\\040", " ");
            let fstype = it.next().unwrap_or_default().to_string();
            if mp.len() >= best.as_ref().map(|b| b.2).unwrap_or(0)
                && (target == mp || target.starts_with(&format!("{mp}/")))
            {
                best = Some((dev, fstype, mp.len()));
            }
        }
    }
    best.map(|(d, f, _)| (d, f))
        .unwrap_or_else(|| ("unknown".into(), "unknown".into()))
}

struct CpuInfo {
    model: String,
    flags: String,
    count: usize,
    mhz: Option<f64>,
}

fn cpu_info() -> CpuInfo {
    let body = std::fs::read_to_string("/proc/cpuinfo").unwrap_or_default();
    let mut model = String::new();
    let mut flags = String::new();
    let mut mhz = None;
    let mut count = 0usize;
    let mut first = true;
    for line in body.lines() {
        if let Some(v) = line.strip_prefix("model name") {
            if first {
                model = v.trim_start_matches(':').trim().to_string();
            }
        } else if let Some(v) = line.strip_prefix("flags") {
            if first {
                flags = v.trim_start_matches(':').trim().to_string();
            }
        } else if let Some(v) = line.strip_prefix("cpu MHz") {
            if first {
                mhz = v.trim_start_matches(':').trim().parse().ok();
            }
        } else if let Some(v) = line.strip_prefix("max MHz") {
            if first {
                mhz = v.trim_start_matches(':').trim().parse().ok();
            }
        } else if line.starts_with("processor") {
            count += 1;
        }
        if line.trim().is_empty() {
            first = false;
        }
    }
    if count == 0 {
        count = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);
    }
    CpuInfo {
        model,
        flags,
        count,
        mhz,
    }
}

fn meminfo_total_kib() -> u64 {
    std::fs::read_to_string("/proc/meminfo")
        .ok()
        .and_then(|body| {
            body.lines()
                .find(|l| l.starts_with("MemTotal"))
                .and_then(|l| l.split_whitespace().nth(1))
                .and_then(|v| v.parse().ok())
        })
        .unwrap_or(0)
}

fn read_trimmed(path: &str) -> String {
    std::fs::read_to_string(path)
        .map(|s| s.trim().to_string())
        .unwrap_or_default()
}

fn current_uid() -> u32 {
    // No `rustix::process` feature needed: parse the real uid from
    // /proc/self/status (first number of the `Uid:` line).
    std::fs::read_to_string("/proc/self/status")
        .ok()
        .and_then(|body| {
            body.lines()
                .find(|l| l.starts_with("Uid:"))
                .and_then(|l| l.split_whitespace().nth(1))
                .and_then(|v| v.parse().ok())
        })
        .unwrap_or(0)
}

fn git_output(args: &[&str]) -> String {
    Command::new("git")
        .args(args)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .unwrap_or_default()
}

/// Render a `BTreeMap<String, u64>` (representation families, byte
/// categories) as a sorted CSV table fragment for `results.csv`.
pub fn map_to_csv(prefix: &str, m: &BTreeMap<String, u64>) -> Vec<String> {
    m.iter().map(|(k, v)| format!("{prefix},{k},{v}")).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn percentile_nearest_rank() {
        let s = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        assert_eq!(percentile(&s, 50.0), 5.0);
        assert_eq!(percentile(&s, 100.0), 10.0);
        assert_eq!(percentile(&s, 0.0), 1.0);
        assert_eq!(percentile(&[], 50.0), 0.0);
    }

    #[test]
    fn summary_basics() {
        let s = summary(&[1.0, 2.0, 3.0]);
        assert_eq!(s.count, 3);
        assert_eq!(s.min, 1.0);
        assert_eq!(s.max, 3.0);
        assert!((s.mean - 2.0).abs() < 1e-9);
    }

    #[test]
    fn disk_delta_saturates() {
        let b = DiskStats {
            reads: 10,
            read_sectors: 20,
            writes: 5,
            write_sectors: 8,
        };
        let a = DiskStats {
            reads: 13,
            read_sectors: 21,
            writes: 5,
            write_sectors: 10,
        };
        let d = disk_delta("test", &b, &a);
        assert_eq!(d.reads, 3);
        assert_eq!(d.write_sectors, 2);
        assert_eq!(d.written_bytes(), 1024);
        assert_eq!(d.read_bytes(), 512);
    }

    #[test]
    fn mount_of_returns_something() {
        // The repo lives on a real filesystem; this should resolve to a
        // device and fstype rather than the "unknown" fallback.
        let (dev, fstype) = mount_of(Path::new("/"));
        assert!(dev.starts_with("/dev/") || dev == "/" || !dev.is_empty());
        assert!(!fstype.is_empty());
    }
}