From d910e8c1a06d248368aa651369fc0d7735fbb4f6 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Mon, 24 Feb 2025 14:53:09 +0200 Subject: [PATCH] user: replace third-party `humansize` --- Cargo.lock | 4 ++++ Cargo.toml | 3 ++- userspace/Cargo.lock | 15 +++++---------- userspace/Cargo.toml | 6 ++++-- userspace/sysutils/Cargo.toml | 3 +-- userspace/sysutils/src/dd.rs | 5 +++-- userspace/sysutils/src/ls.rs | 13 ++++++++++--- userspace/sysutils/src/sysmon.rs | 11 +++++++---- 8 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b3204145..2e420bae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1357,6 +1357,10 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "libutil" +version = "0.1.0" + [[package]] name = "libyalloc" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 32e43b7a..18bb9413 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,8 @@ members = [ "lib/libyalloc", "lib/runtime", "lib/qemu", - "lib/abi-serde" + "lib/abi-serde", + "lib/libutil" ] [workspace.dependencies] diff --git a/userspace/Cargo.lock b/userspace/Cargo.lock index 944bc7eb..81f77898 100644 --- a/userspace/Cargo.lock +++ b/userspace/Cargo.lock @@ -1053,15 +1053,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "humansize" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" -dependencies = [ - "libm", -] - [[package]] name = "humantime" version = "2.1.0" @@ -1371,6 +1362,10 @@ dependencies = [ "yggdrasil-rt", ] +[[package]] +name = "libutil" +version = "0.1.0" + [[package]] name = "linux-raw-sys" version = "0.4.15" @@ -2654,9 +2649,9 @@ dependencies = [ "chrono", "clap", "cross", - "humansize", "init", "libterm", + "libutil", "log", "logsink", "pci-ids", diff --git a/userspace/Cargo.toml b/userspace/Cargo.toml index fea8a6b8..416f5a2b 100644 --- a/userspace/Cargo.toml +++ b/userspace/Cargo.toml @@ -19,8 +19,9 @@ members = [ "crypt", "lib/runtime", "lib/uipc", - "lib/logsink" -, "lib/libpsf"] + "lib/logsink", + "lib/libpsf" +] exclude = ["dynload-program", "test-kernel-module", "lib/ygglibc"] [workspace.dependencies] @@ -57,6 +58,7 @@ yggdrasil-rt.path = "../lib/runtime" yggdrasil-abi = { path = "../lib/abi", features = ["serde", "alloc", "bytemuck"] } abi-serde = { path = "../lib/abi-serde" } logsink.path = "lib/logsink" +libutil.path = "../lib/libutil" [workspace.lints.rust] unexpected_cfgs = { level = "allow", check-cfg = ['cfg(rust_analyzer)'] } diff --git a/userspace/sysutils/Cargo.toml b/userspace/sysutils/Cargo.toml index e9b42404..732296f8 100644 --- a/userspace/sysutils/Cargo.toml +++ b/userspace/sysutils/Cargo.toml @@ -12,6 +12,7 @@ yggdrasil-abi.workspace = true yggdrasil-rt.workspace = true cross.workspace = true logsink.workspace = true +libutil.workspace = true log.workspace = true rand.workspace = true @@ -22,8 +23,6 @@ serde_json.workspace = true sha2.workspace = true chrono.workspace = true -# TODO own impl -humansize = { version = "2.1.3", features = ["impl_style"] } pci-ids = { version = "0.2.5" } init = { path = "../init" } diff --git a/userspace/sysutils/src/dd.rs b/userspace/sysutils/src/dd.rs index 315dcb04..67da9014 100644 --- a/userspace/sysutils/src/dd.rs +++ b/userspace/sysutils/src/dd.rs @@ -5,6 +5,7 @@ use std::{ }; use clap::Parser; +use libutil::fmt::FormatSize; use sysutils::{Input, Output}; #[derive(Parser)] @@ -67,10 +68,10 @@ fn dump_block(offset: u64, data: &[u8]) { } fn print_throughput(duration: Duration, bytes_read: usize) { - let read_total = humansize::format_size(bytes_read as u64, humansize::FormatSizeOptions::default()); let ms = duration.as_millis() as u64; let read_per_ms = bytes_read as u64 / ms; - let read_speed = humansize::format_size(read_per_ms * 1000, humansize::FormatSizeOptions::default()); + let read_total = FormatSize::default(bytes_read as u64); + let read_speed = FormatSize::default(read_per_ms * 1000); eprintln!("{read_speed}/s ({read_total} in {duration:?})"); } diff --git a/userspace/sysutils/src/ls.rs b/userspace/sysutils/src/ls.rs index 37d45b46..b23ad3ff 100644 --- a/userspace/sysutils/src/ls.rs +++ b/userspace/sysutils/src/ls.rs @@ -2,7 +2,14 @@ #![feature(let_chains, decl_macro)] use std::{ - cmp::Ordering, ffi::OsString, fmt, fs::{read_dir, FileType, Metadata}, io, path::{Path, PathBuf}, process::ExitCode, time::SystemTime + cmp::Ordering, + ffi::OsString, + fmt, + fs::{read_dir, FileType, Metadata}, + io, + path::{Path, PathBuf}, + process::ExitCode, + time::SystemTime, }; #[cfg(unix)] @@ -12,7 +19,7 @@ use std::os::yggdrasil::fs::MetadataExt; use chrono::{Datelike, Timelike}; use clap::Parser; -use humansize::{FormatSize, BINARY}; +use libutil::fmt::FormatSize; #[derive(Parser)] #[clap(disable_help_flag = true)] @@ -60,7 +67,7 @@ trait MetadataImpl { impl DisplaySizeBit for u64 { fn display_size_bit(self, opts: &Args, f: &mut fmt::Formatter<'_>) -> fmt::Result { if opts.human_readable { - fmt::Display::fmt(&self.format_size(BINARY), f) + fmt::Display::fmt(&FormatSize::default(self), f) } else { fmt::Display::fmt(&self, f) } diff --git a/userspace/sysutils/src/sysmon.rs b/userspace/sysutils/src/sysmon.rs index b37ab1d5..dbc63626 100644 --- a/userspace/sysutils/src/sysmon.rs +++ b/userspace/sysutils/src/sysmon.rs @@ -5,8 +5,8 @@ use std::{ fmt::Write }; -use humansize::FormatSize; use libterm::{Clear, Term}; +use libutil::fmt::FormatSize; use yggdrasil_rt::system; fn get_memory_stats() -> system::SystemMemoryStats { @@ -50,12 +50,15 @@ fn main() { let free_bytes = stats.free_pages * stats.page_size; let total_usable_bytes = stats.total_usable_pages * stats.page_size; + let allocated_mem = FormatSize::default(allocated_bytes as u64); + let free_mem = FormatSize::default(free_bytes as u64); + let total_usable_mem = FormatSize::default(total_usable_bytes as u64); write!( term, "U: {} F: {} T: {} ({}%)", - allocated_bytes.format_size(Default::default()), - free_bytes.format_size(Default::default()), - total_usable_bytes.format_size(Default::default()), + allocated_mem, + free_mem, + total_usable_mem, 100 * stats.allocated_pages / stats.total_usable_pages ) .ok();