mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 18:24:08 +02:00
debup disks
This commit is contained in:
parent
ac3fbde789
commit
8e6dcac354
3 changed files with 34 additions and 5 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
|
@ -15,6 +15,17 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ahash"
|
||||||
|
version = "0.7.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7f200cbb1e856866d9eade941cf3aa0c5d7dd36f74311c4273b494f4ef036957"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom 0.2.2",
|
||||||
|
"once_cell",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
|
@ -614,6 +625,7 @@ checksum = "2386b4ebe91c2f7f51082d4cefa145d030e33a1842a96b12e4885cc3c01f7a55"
|
||||||
name = "palantir"
|
name = "palantir"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"ctrlc",
|
"ctrlc",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ regex = { version = "1", default-features = false, features = ["std"] }
|
||||||
once_cell = "1"
|
once_cell = "1"
|
||||||
hostname = "0.3"
|
hostname = "0.3"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
ahash = "0.7"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
iai = "0.1"
|
iai = "0.1"
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
|
use ahash::{AHasher, RandomState};
|
||||||
use color_eyre::{Report, Result};
|
use color_eyre::{Report, Result};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::array::IntoIter;
|
use std::array::IntoIter;
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use std::fs::{read, read_dir, read_to_string, File};
|
use std::fs::{read, read_dir, read_to_string, File};
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
|
@ -98,13 +101,13 @@ pub fn memory() -> Result<Memory> {
|
||||||
}
|
}
|
||||||
if let Some(line) = line.strip_suffix(" kB\n") {
|
if let Some(line) = line.strip_suffix(" kB\n") {
|
||||||
if let Some(line_total) = line.strip_prefix("MemTotal: ") {
|
if let Some(line_total) = line.strip_prefix("MemTotal: ") {
|
||||||
mem.total = line_total.trim().parse()?;
|
mem.total = line_total.trim().parse::<u64>()? * 1000;
|
||||||
}
|
}
|
||||||
if let Some(line_free) = line.strip_prefix("MemFree: ") {
|
if let Some(line_free) = line.strip_prefix("MemFree: ") {
|
||||||
mem.free = line_free.trim().parse()?;
|
mem.free = line_free.trim().parse::<u64>()? * 1000;
|
||||||
}
|
}
|
||||||
if let Some(line_available) = line.strip_prefix("MemAvailable: ") {
|
if let Some(line_available) = line.strip_prefix("MemAvailable: ") {
|
||||||
mem.available = line_available.trim().parse()?;
|
mem.available = line_available.trim().parse::<u64>()? * 1000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -204,12 +207,19 @@ pub fn disk_stats() -> Result<impl Iterator<Item = IoStats>> {
|
||||||
|
|
||||||
pub fn disk_usage() -> Result<impl Iterator<Item = DiskUsage>> {
|
pub fn disk_usage() -> Result<impl Iterator<Item = DiskUsage>> {
|
||||||
let stat = BufReader::new(File::open("/proc/mounts")?);
|
let stat = BufReader::new(File::open("/proc/mounts")?);
|
||||||
|
let mut found_disks = HashSet::with_capacity_and_hasher(8, RandomState::new());
|
||||||
Ok(stat
|
Ok(stat
|
||||||
.lines()
|
.lines()
|
||||||
.filter_map(Result::ok)
|
.filter_map(Result::ok)
|
||||||
.filter(|line| line.starts_with('/'))
|
.filter(|line| line.starts_with('/'))
|
||||||
.filter_map(|line: String| {
|
.filter(|line| !line.contains("/dev/loop"))
|
||||||
let mount_point = line.split_ascii_whitespace().nth(1)?;
|
.filter_map(move |line: String| {
|
||||||
|
let mut parts = line.split_ascii_whitespace();
|
||||||
|
let disk = parts.next()?;
|
||||||
|
if !found_disks.insert(hash_str(disk)) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mount_point = parts.next()?;
|
||||||
let mount_point = CString::new(mount_point).ok()?;
|
let mount_point = CString::new(mount_point).ok()?;
|
||||||
let stat = statvfs(&mount_point).ok()?;
|
let stat = statvfs(&mount_point).ok()?;
|
||||||
Some(DiskUsage {
|
Some(DiskUsage {
|
||||||
|
|
@ -251,3 +261,9 @@ fn cpu_count() -> Result<u64> {
|
||||||
Ok(result as u64)
|
Ok(result as u64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hash_str(s: &str) -> u64 {
|
||||||
|
let mut hasher = AHasher::default();
|
||||||
|
s.hash(&mut hasher);
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue