diff --git a/Cargo.lock b/Cargo.lock index 737d21ab..11c1fc3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ dependencies = [ "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "retry 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -142,6 +143,80 @@ dependencies = [ "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-complex 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-bigint" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-complex" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-iter" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-rational" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "number_prefix" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "protobuf" version = "1.0.20" @@ -180,6 +255,11 @@ dependencies = [ "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rustc-serialize" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "slab" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index 2c4342b2..c61521b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ env_logger = "0.3.3" libc = "0.2.10" log = "0.3.6" mio = "0.5" +number_prefix = "0.2.5" protobuf = "1.0.18" retry = "0.4.0" tempdir = "0.3.4" diff --git a/src/commands.rs b/src/commands.rs index 11b0ebe6..562c3820 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -22,6 +22,7 @@ use mock_command::{ CommandCreatorSync, ProcessCommandCreator, }; +use number_prefix::{binary_prefix, Standalone, Prefixed}; use protobuf::RepeatedField; use protocol::{ CacheStats, @@ -129,29 +130,6 @@ pub fn request_shutdown(mut conn : ServerConnection) -> io::Result { } } -/// Format `size_in_bytes` as a size in sensible units. -/// -/// e.g. format_size(3 * 1024 * 1024 * 1024) == "3 GB" -fn format_size(size_in_bytes : u64) -> String { - let mut size = size_in_bytes; - let mut remainder = 0; - for suffix in ["bytes", "kB", "MB", "GB", "TB"].iter() { - if size < 1024 { - let frac = if remainder > 0 { - let rem = (100.0 * remainder as f32 / 1024.0).trunc() as i32; - format!(".{}", if rem % 10 == 0 { rem / 10 } else { rem }) - } else { - "".to_owned() - }; - return format!("{}{} {}", size, frac, suffix); - } - remainder = size % 1024; - size = size / 1024; - } - //TODO: handle this more gracefully - return format!("{} {}", size, "PB"); -} - /// Print `stats` to stdout. fn print_stats(stats : CacheStats) -> io::Result<()> { for stat in stats.get_stats().iter() { @@ -162,7 +140,10 @@ fn print_stats(stats : CacheStats) -> io::Result<()> { } else if stat.has_str() { print!("{}", stat.get_str()); } else if stat.has_size() { - print!("{}", format_size(stat.get_size())); + match binary_prefix(stat.get_size() as f64) { + Standalone(bytes) => print!("{} bytes", bytes), + Prefixed(prefix, n) => print!("{:.0} {}B", n, prefix), + } } print!("\n"); } @@ -321,23 +302,3 @@ pub fn run_command(cmd : Command) -> i32 { }, } } - -#[cfg(test)] -mod test { - use super::format_size; - - #[test] - fn test_format_size() { - assert_eq!("10 bytes", format_size(10)); - assert_eq!("1023 bytes", format_size(1023)); - assert_eq!("1 kB", format_size(1024)); - assert_eq!("1.5 kB", format_size(1024 + 512)); - assert_eq!("1023.99 kB", format_size(1024 * 1024 - 1)); - assert_eq!("1 MB", format_size(1024 * 1024)); - assert_eq!("1 GB", format_size(1024 * 1024 * 1024)); - assert_eq!("1.25 GB", format_size(1024 * 1024 * (1024 + 256))); - assert_eq!("3 GB", format_size(1024 * 1024 * 1024 * 3)); - assert_eq!("1 TB", format_size(1024 * 1024 * 1024 * 1024)); - assert_eq!("1 PB", format_size(1024 * 1024 * 1024 * 1024 * 1024)); - } -} diff --git a/src/main.rs b/src/main.rs index 16fbfa38..b8286304 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ extern crate env_logger; #[macro_use] extern crate log; extern crate libc; extern crate mio; +extern crate number_prefix; extern crate protobuf; extern crate retry; extern crate tempdir;