forked from vmware-archive/node-replicated-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
99 lines (82 loc) · 2.83 KB
/
Copy pathutils.rs
File metadata and controls
99 lines (82 loc) · 2.83 KB
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
// Copyright © 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Utility code for dealing with memory / bytes etc.
use core::fmt;
use super::{BASE_PAGE_SIZE, LARGE_PAGE_SIZE};
/// Calculate how many base and large pages we need to fit a given size.
///
/// # Returns
/// - A tuple containing (base-pages, large-pages).
/// - base-pages will never exceed LARGE_PAGE_SIZE / BASE_PAGE_SIZE.
pub(crate) fn size_to_pages(size: usize) -> (usize, usize) {
let bytes_not_in_large = size % LARGE_PAGE_SIZE;
let div = bytes_not_in_large / BASE_PAGE_SIZE;
let rem = bytes_not_in_large % BASE_PAGE_SIZE;
let base_pages = if rem > 0 { div + 1 } else { div };
let remaining_size = size - bytes_not_in_large;
let div = remaining_size / LARGE_PAGE_SIZE;
let rem = remaining_size % LARGE_PAGE_SIZE;
let large_pages = if rem > 0 { div + 1 } else { div };
(base_pages, large_pages)
}
/// Human-readable representation of a data-size.
///
/// # Notes
/// Use for pretty printing and debugging only.
#[derive(PartialEq)]
pub(crate) enum DataSize {
Bytes(f64),
KiB(f64),
MiB(f64),
GiB(f64),
}
impl DataSize {
/// Construct a new DataSize passing the amount of `bytes`
/// we want to convert
pub(crate) fn from_bytes(bytes: usize) -> DataSize {
if bytes < 1024 {
DataSize::Bytes(bytes as f64)
} else if bytes < (1024 * 1024) {
DataSize::KiB(bytes as f64 / 1024.0)
} else if bytes < (1024 * 1024 * 1024) {
DataSize::MiB(bytes as f64 / (1024 * 1024) as f64)
} else {
DataSize::GiB(bytes as f64 / (1024 * 1024 * 1024) as f64)
}
}
/// Write rounded size and SI unit to `f`
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DataSize::Bytes(n) => write!(f, "{:.2} B", n),
DataSize::KiB(n) => write!(f, "{:.2} KiB", n),
DataSize::MiB(n) => write!(f, "{:.2} MiB", n),
DataSize::GiB(n) => write!(f, "{:.2} GiB", n),
}
}
}
impl fmt::Debug for DataSize {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
impl fmt::Display for DataSize {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::memory::{BASE_PAGE_SIZE, LARGE_PAGE_SIZE};
#[test]
fn size_formatting() {
let ds = DataSize::from_bytes(LARGE_PAGE_SIZE);
assert_eq!(ds, DataSize::MiB(2.0));
let ds = DataSize::from_bytes(BASE_PAGE_SIZE);
assert_eq!(ds, DataSize::KiB(4.0));
let ds = DataSize::from_bytes(1024 * LARGE_PAGE_SIZE);
assert_eq!(ds, DataSize::GiB(2.0));
let ds = DataSize::from_bytes(usize::MIN);
assert_eq!(ds, DataSize::Bytes(0.0));
}
}