Skip to content

Commit cbaea83

Browse files
authored
support puma info (#18)
Signed-off-by: kerthcet <kerthcet@gmail.com>
1 parent ba41b68 commit cbaea83

7 files changed

Lines changed: 280 additions & 36 deletions

File tree

Cargo.lock

Lines changed: 131 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ hf-hub = { version = "0.5.0", features = ["tokio"] }
2121
colored = "2.1"
2222
chrono = "0.4"
2323
serde_json = "1.0"
24+
sysinfo = "0.32"
2425

2526
[dev-dependencies]
2627
tempfile = "3.12"

src/cli/commands.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use prettytable::{format, row, Table};
44
use crate::downloader::downloader::Downloader;
55
use crate::downloader::huggingface::HuggingFaceDownloader;
66
use crate::registry::model_registry::ModelRegistry;
7+
use crate::system::system_info::SystemInfo;
78
use crate::util::format::{format_size, format_time_ago};
89

910
#[derive(Parser)]
@@ -158,7 +159,8 @@ pub async fn run(cli: Cli) {
158159
}
159160

160161
Commands::INFO => {
161-
println!("Displaying system-wide information...");
162+
let info = SystemInfo::collect();
163+
info.display();
162164
}
163165

164166
Commands::INSPECT => {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod cli;
22
mod downloader;
33
mod registry;
4+
mod system;
45
mod util;
56

67
use clap::Parser;

src/system/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod system_info;

src/system/system_info.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::fs;
3+
use std::os::unix::fs::MetadataExt;
4+
use std::path::PathBuf;
5+
use sysinfo::System;
6+
7+
use crate::registry::model_registry::ModelRegistry;
8+
use crate::util::file;
9+
use crate::util::format::format_size;
10+
11+
#[derive(Debug, Serialize, Deserialize)]
12+
pub struct SystemInfo {
13+
pub version: String,
14+
pub os: String,
15+
pub architecture: String,
16+
pub cpu_cores: usize,
17+
pub total_memory: String,
18+
pub available_memory: String,
19+
pub cache_dir: String,
20+
pub cache_size: String,
21+
pub models_count: usize,
22+
pub running_models: usize,
23+
}
24+
25+
impl SystemInfo {
26+
pub fn collect() -> Self {
27+
let mut sys = System::new_all();
28+
sys.refresh_all();
29+
30+
let cache_dir = file::cache_dir();
31+
let cache_size = Self::calculate_cache_size(&cache_dir);
32+
33+
let registry = ModelRegistry::new(None);
34+
let models_count = registry.load_models().unwrap_or_default().len();
35+
36+
SystemInfo {
37+
version: env!("CARGO_PKG_VERSION").to_string(),
38+
os: System::name().unwrap_or_else(|| "Unknown".to_string()),
39+
architecture: System::cpu_arch().unwrap_or_else(|| "Unknown".to_string()),
40+
cpu_cores: sys.cpus().len(),
41+
total_memory: format_size(sys.total_memory()),
42+
available_memory: format_size(sys.available_memory()),
43+
cache_dir: cache_dir.to_string_lossy().to_string(),
44+
cache_size: format_size(cache_size),
45+
models_count,
46+
running_models: 0, // TODO: implement running models tracking
47+
}
48+
}
49+
50+
fn calculate_cache_size(cache_dir: &PathBuf) -> u64 {
51+
if !cache_dir.exists() {
52+
return 0;
53+
}
54+
55+
let mut total_size = 0u64;
56+
57+
if let Ok(entries) = fs::read_dir(cache_dir) {
58+
for entry in entries.flatten() {
59+
if let Ok(metadata) = entry.metadata() {
60+
if metadata.is_file() {
61+
// Use blocks * 512 to get actual disk usage (handles sparse files)
62+
total_size += metadata.blocks() * 512;
63+
} else if metadata.is_dir() {
64+
total_size += Self::dir_size(&entry.path());
65+
}
66+
}
67+
}
68+
}
69+
70+
total_size
71+
}
72+
73+
fn dir_size(path: &PathBuf) -> u64 {
74+
let mut total_size = 0u64;
75+
76+
if let Ok(entries) = fs::read_dir(path) {
77+
for entry in entries.flatten() {
78+
if let Ok(metadata) = entry.metadata() {
79+
if metadata.is_file() {
80+
// Use blocks * 512 to get actual disk usage (handles sparse files)
81+
total_size += metadata.blocks() * 512;
82+
} else if metadata.is_dir() {
83+
total_size += Self::dir_size(&entry.path());
84+
}
85+
}
86+
}
87+
}
88+
89+
total_size
90+
}
91+
92+
pub fn display(&self) {
93+
println!("System Information:");
94+
println!(" Operating System: {}", self.os);
95+
println!(" Architecture: {}", self.architecture);
96+
println!(" CPU Cores: {}", self.cpu_cores);
97+
println!(" Total Memory: {}", self.total_memory);
98+
println!(" Available Memory: {}", self.available_memory);
99+
println!();
100+
println!("PUMA Information:");
101+
println!(" PUMA Version: {}", self.version);
102+
println!(" Cache Directory: {}", self.cache_dir);
103+
println!(" Cache Size: {}", self.cache_size);
104+
println!(" Models: {}", self.models_count);
105+
println!(" Running Models: {}", self.running_models);
106+
}
107+
}

0 commit comments

Comments
 (0)