Mountain/Binary/IPC/ProcessCommand/
process_get_memory_info.rs1#![allow(non_snake_case)]
2
3use serde_json::{Value, json};
10
11#[tauri::command]
12pub async fn process_get_memory_info() -> Result<Value, String> {
13 #[cfg(target_os = "macos")]
14 {
15 let Output = std::process::Command::new("ps")
16 .args(["-o", "rss=,vsz=", "-p", &std::process::id().to_string()])
17 .output();
18
19 match Output {
20 Ok(Out) => {
21 let Text = String::from_utf8_lossy(&Out.stdout);
22 let Parts:Vec<&str> = Text.split_whitespace().collect();
23 let Rss = Parts.first().and_then(|V| V.parse::<u64>().ok()).unwrap_or(0) * 1024;
24 let _Vsz = Parts.get(1).and_then(|V| V.parse::<u64>().ok()).unwrap_or(0) * 1024;
25 Ok(json!({ "private": Rss, "shared": 0, "residentSet": Rss }))
26 },
27 Err(_) => Ok(json!({ "private": 0, "shared": 0, "residentSet": 0 })),
28 }
29 }
30
31 #[cfg(target_os = "windows")]
32 {
33 let Output = std::process::Command::new("tasklist")
34 .args(["/FI", &format!("PID eq {}", std::process::id()), "/FO", "CSV", "/NH"])
35 .output();
36
37 match Output {
38 Ok(Out) => {
39 let Text = String::from_utf8_lossy(&Out.stdout);
40 let MemStr = Text.split(',').nth(4).unwrap_or("\"0 K\"");
41 let MemKb:u64 = MemStr
42 .chars()
43 .filter(|C| C.is_ascii_digit())
44 .collect::<String>()
45 .parse()
46 .unwrap_or(0);
47 let MemBytes = MemKb * 1024;
48 Ok(json!({ "private": MemBytes, "shared": 0, "residentSet": MemBytes }))
49 },
50 Err(_) => Ok(json!({ "private": 0, "shared": 0, "residentSet": 0 })),
51 }
52 }
53
54 #[cfg(target_os = "linux")]
55 {
56 match tokio::fs::read_to_string("/proc/self/statm").await {
57 Ok(Content) => {
58 let Parts:Vec<&str> = Content.split_whitespace().collect();
59 let PageSize:u64 = 4096;
60 let _Vsz = Parts.first().and_then(|V| V.parse::<u64>().ok()).unwrap_or(0) * PageSize;
61 let Rss = Parts.get(1).and_then(|V| V.parse::<u64>().ok()).unwrap_or(0) * PageSize;
62 let Shared = Parts.get(2).and_then(|V| V.parse::<u64>().ok()).unwrap_or(0) * PageSize;
63 Ok(json!({
64 "private": Rss.saturating_sub(Shared),
65 "shared": Shared,
66 "residentSet": Rss
67 }))
68 },
69 Err(_) => Ok(json!({ "private": 0, "shared": 0, "residentSet": 0 })),
70 }
71 }
72
73 #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
74 {
75 Ok(json!({ "private": 0, "shared": 0, "residentSet": 0 }))
76 }
77}