lscpu
: Use own function to parse cache sizes
This commit is contained in:
parent
bc44b86fe7
commit
2a381e5321
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -513,12 +513,6 @@ dependencies = [
|
|||||||
"unicode-width 0.2.0",
|
"unicode-width 0.2.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "parse-size"
|
|
||||||
version = "1.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "487f2ccd1e17ce8c1bfab3a65c89525af41cfad4c8659021a1e9a2aacd73b89b"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parse_datetime"
|
name = "parse_datetime"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
@ -1119,7 +1113,6 @@ name = "uu_lscpu"
|
|||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"parse-size",
|
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -46,7 +46,6 @@ dns-lookup = "2.0.4"
|
|||||||
libc = "0.2.152"
|
libc = "0.2.152"
|
||||||
linux-raw-sys = { version = "0.7.0", features = ["ioctl"] }
|
linux-raw-sys = { version = "0.7.0", features = ["ioctl"] }
|
||||||
nix = { version = "0.29", default-features = false }
|
nix = { version = "0.29", default-features = false }
|
||||||
parse-size = "1.1.0"
|
|
||||||
phf = "0.11.2"
|
phf = "0.11.2"
|
||||||
phf_codegen = "0.11.2"
|
phf_codegen = "0.11.2"
|
||||||
rand = { version = "0.9.0", features = ["small_rng"] }
|
rand = { version = "0.9.0", features = ["small_rng"] }
|
||||||
|
@ -17,4 +17,3 @@ uucore = { workspace = true }
|
|||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
serde_json = { workspace = true }
|
serde_json = { workspace = true }
|
||||||
parse-size = { workspace = true }
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use parse_size::parse_size;
|
|
||||||
use std::{collections::HashSet, fs, path::PathBuf};
|
use std::{collections::HashSet, fs, path::PathBuf};
|
||||||
|
|
||||||
pub struct CpuVulnerability {
|
pub struct CpuVulnerability {
|
||||||
@ -113,7 +112,7 @@ fn read_cpu_caches(cpu_index: usize) -> Vec<CpuCache> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let size_string = fs::read_to_string(cache_path.join("size")).unwrap();
|
let size_string = fs::read_to_string(cache_path.join("size")).unwrap();
|
||||||
let c_size = parse_size(size_string.trim()).unwrap();
|
let c_size = parse_cache_size(size_string.trim());
|
||||||
|
|
||||||
let shared_cpu_map = fs::read_to_string(cache_path.join("shared_cpu_map"))
|
let shared_cpu_map = fs::read_to_string(cache_path.join("shared_cpu_map"))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -202,6 +201,42 @@ fn parse_cpu_list(list: &str) -> Vec<usize> {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_cache_size(s: &str) -> u64 {
|
||||||
|
// Yes, this will break if we ever reach a point where caches exceed terabytes in size...
|
||||||
|
const EXPONENTS: [(char, u32); 4] = [('K', 1), ('M', 2), ('G', 3), ('T', 4)];
|
||||||
|
|
||||||
|
// If we only have numbers, treat it as a raw amount of bytes and parse as-is
|
||||||
|
if s.chars().all(|c| c.is_numeric()) {
|
||||||
|
return s.parse::<u64>().expect("Could not parse cache size");
|
||||||
|
};
|
||||||
|
|
||||||
|
for (suffix, exponent) in EXPONENTS {
|
||||||
|
if s.ends_with(suffix) {
|
||||||
|
let nums = s.strip_suffix(suffix).unwrap();
|
||||||
|
let value = nums.parse::<u64>().expect("Could not parse cache size");
|
||||||
|
let multiplier = 1024_u64.pow(exponent);
|
||||||
|
|
||||||
|
return value * multiplier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic!("No known suffix in cache size string");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_cache_size() {
|
||||||
|
assert_eq!(parse_cache_size("512"), 512);
|
||||||
|
|
||||||
|
assert_eq!(parse_cache_size("1K"), 1024);
|
||||||
|
assert_eq!(parse_cache_size("1M"), 1024 * 1024);
|
||||||
|
assert_eq!(parse_cache_size("1G"), 1024 * 1024 * 1024);
|
||||||
|
assert_eq!(parse_cache_size("1T"), 1024 * 1024 * 1024 * 1024);
|
||||||
|
|
||||||
|
assert_eq!(parse_cache_size("123K"), 123 * 1024);
|
||||||
|
assert_eq!(parse_cache_size("32M"), 32 * 1024 * 1024);
|
||||||
|
assert_eq!(parse_cache_size("345G"), 345 * 1024 * 1024 * 1024);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_cpu_list() {
|
fn test_parse_cpu_list() {
|
||||||
assert_eq!(parse_cpu_list(""), Vec::<usize>::new());
|
assert_eq!(parse_cpu_list(""), Vec::<usize>::new());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user