Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions src/tools/be/memz.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::be_http_client;
use crate::config::Config;
use crate::error::Result;
use crate::tools::common::format_utils;
use crate::tools::{ExecutionResult, Tool};
use crate::ui;
use chrono::Utc;
Expand Down Expand Up @@ -102,23 +103,6 @@ impl Tool for MemzGlobalTool {
}
}

/// Format bytes to a human-readable string
fn format_bytes(bytes: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;

if bytes >= GB {
format!("{:.2} GB ({bytes} bytes)", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB ({bytes} bytes)", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB ({bytes} bytes)", bytes as f64 / KB as f64)
} else {
format!("{bytes} bytes")
}
}

/// Extract memory metrics from the HTML response
fn extract_memory_metrics(html_content: &str) -> (String, String) {
let re = Regex::new(r"Allocated: (\d+), active: (\d+), metadata: (\d+).*?, resident: (\d+), mapped: (\d+), retained: (\d+)").unwrap();
Expand All @@ -141,27 +125,27 @@ fn extract_memory_metrics(html_content: &str) -> (String, String) {
{
let caps = re.captures(html_content).unwrap();
if let Some(bytes) = caps.get(1).and_then(|m| m.as_str().parse::<u64>().ok()) {
allocated = format_bytes(bytes);
allocated = format_utils::format_bytes(bytes, 2, true);
}

if let Some(bytes) = caps.get(2).and_then(|m| m.as_str().parse::<u64>().ok()) {
active = format_bytes(bytes);
active = format_utils::format_bytes(bytes, 2, true);
}

if let Some(bytes) = caps.get(3).and_then(|m| m.as_str().parse::<u64>().ok()) {
metadata = format_bytes(bytes);
metadata = format_utils::format_bytes(bytes, 2, true);
}

if let Some(bytes) = caps.get(4).and_then(|m| m.as_str().parse::<u64>().ok()) {
resident = format_bytes(bytes);
resident = format_utils::format_bytes(bytes, 2, true);
}

if let Some(bytes) = caps.get(5).and_then(|m| m.as_str().parse::<u64>().ok()) {
mapped = format_bytes(bytes);
mapped = format_utils::format_bytes(bytes, 2, true);
}

if let Some(bytes) = caps.get(6).and_then(|m| m.as_str().parse::<u64>().ok()) {
retained = format_bytes(bytes);
retained = format_utils::format_bytes(bytes, 2, true);
}
}

Expand All @@ -170,15 +154,15 @@ fn extract_memory_metrics(html_content: &str) -> (String, String) {
.and_then(|caps| caps.get(1))
.and_then(|m| m.as_str().parse::<u64>().ok())
{
thread_cache = format_bytes(bytes);
thread_cache = format_utils::format_bytes(bytes, 2, true);
}

if let Some(bytes) = dirty_pages_re
.captures(html_content)
.and_then(|caps| caps.get(1))
.and_then(|m| m.as_str().parse::<u64>().ok())
{
dirty_pages = format_bytes(bytes);
dirty_pages = format_utils::format_bytes(bytes, 2, true);
}

let table = format!(
Expand Down
33 changes: 33 additions & 0 deletions src/tools/common/format_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// Format bytes to a human-readable string with customizable precision and format
pub fn format_bytes(bytes: u64, precision: usize, show_original: bool) -> String {
const KB: f64 = 1024.0;
const MB: f64 = KB * 1024.0;
const GB: f64 = MB * 1024.0;

if bytes >= GB as u64 {
let formatted = format!("{:.precision$} GB", bytes as f64 / GB);
if show_original {
format!("{} ({bytes} bytes)", formatted)
} else {
formatted
}
} else if bytes >= MB as u64 {
let formatted = format!("{:.precision$} MB", bytes as f64 / MB);
if show_original {
format!("{} ({bytes} bytes)", formatted)
} else {
formatted
}
} else if bytes >= KB as u64 {
let formatted = format!("{:.precision$} KB", bytes as f64 / KB);
if show_original {
format!("{} ({bytes} bytes)", formatted)
} else {
formatted
}
} else if show_original {
format!("{bytes} bytes")
} else {
format!("{} B", bytes)
}
}
1 change: 1 addition & 0 deletions src/tools/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod format_utils;
pub mod fs_utils;
pub mod jmap;
2 changes: 2 additions & 0 deletions src/tools/fe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mod jmap;
mod jstack;
mod profiler;
pub mod routine_load;
pub mod table_info;

pub use jmap::{JmapDumpTool, JmapHistoTool};
pub use jstack::JstackTool;
pub use profiler::FeProfilerTool;
pub use routine_load::{RoutineLoadJobLister, get_routine_load_tools};
pub use table_info::{FeTableInfoTool, TableIdentity, TableInfoReport};
Loading
Loading