-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathusage_mem.rs
More file actions
151 lines (127 loc) · 4.12 KB
/
Copy pathusage_mem.rs
File metadata and controls
151 lines (127 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::process::ProcessInfo;
use crate::{column_default, Column};
#[cfg(any(target_os = "linux", target_os = "android"))]
use procfs::{Current, Meminfo, WithCurrentSystemInfo};
use std::cmp;
use std::collections::HashMap;
#[cfg(target_os = "windows")]
use std::mem::{size_of, zeroed};
#[cfg(target_os = "windows")]
use windows_sys::Win32::System::ProcessStatus::{GetPerformanceInfo, PERFORMANCE_INFORMATION};
pub struct UsageMem {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, u32>,
width: usize,
mem_total: u64,
}
impl UsageMem {
pub fn new(header: Option<String>) -> Self {
let header = header.unwrap_or_else(|| String::from("MEM"));
let unit = String::from("[%]");
Self {
fmt_contents: HashMap::new(),
raw_contents: HashMap::new(),
width: 0,
header,
unit,
mem_total: get_mem_total(),
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn get_mem_total() -> u64 {
let meminfo = Meminfo::current();
if let Ok(meminfo) = meminfo {
meminfo.mem_total
} else {
0
}
}
#[cfg(target_os = "macos")]
fn get_mem_total() -> u64 {
let mut mem_total: u64 = 0;
let mut mib = [0, 0];
unsafe {
crate::util::get_sys_value(
libc::CTL_HW as u32,
libc::HW_MEMSIZE as u32,
std::mem::size_of::<u64>(),
&mut mem_total as *mut u64 as *mut libc::c_void,
&mut mib,
);
}
mem_total
}
#[cfg(target_os = "windows")]
fn get_mem_total() -> u64 {
let mut info: PERFORMANCE_INFORMATION = unsafe { zeroed() };
let ret = unsafe { GetPerformanceInfo(&mut info, size_of::<PERFORMANCE_INFORMATION>() as u32) };
if ret != 0 {
info.PhysicalTotal as u64 * info.PageSize as u64
} else {
0
}
}
#[cfg(target_os = "freebsd")]
fn get_mem_total() -> u64 {
let mut mem_total: u64 = 0;
let name = std::ffi::CString::new("hw.availpages").unwrap();
let mut size = std::mem::size_of::<u64>();
let ptr: *mut u64 = &mut mem_total;
unsafe {
libc::sysctlbyname(
name.as_ptr(),
ptr as *mut libc::c_void,
&mut size,
std::ptr::null(),
0,
);
}
mem_total
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Column for UsageMem {
fn add(&mut self, proc: &ProcessInfo) {
let usage = proc.curr_proc.stat().rss_bytes().get() as f64 * 100.0 / self.mem_total as f64;
let fmt_content = format!("{usage:.1}");
let raw_content = (usage * 1000.0) as u32;
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u32, true);
}
#[cfg(target_os = "macos")]
impl Column for UsageMem {
fn add(&mut self, proc: &ProcessInfo) {
let usage = proc.curr_task.ptinfo.pti_resident_size as f64 * 100.0 / self.mem_total as f64;
let fmt_content = format!("{:.1}", usage);
let raw_content = (usage * 1000.0) as u32;
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u32, true);
}
#[cfg(target_os = "windows")]
impl Column for UsageMem {
fn add(&mut self, proc: &ProcessInfo) {
let usage = proc.memory_info.working_set_size as f64 * 100.0 / self.mem_total as f64;
let fmt_content = format!("{:.1}", usage);
let raw_content = (usage * 1000.0) as u32;
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u32, true);
}
#[cfg(target_os = "freebsd")]
impl Column for UsageMem {
fn add(&mut self, proc: &ProcessInfo) {
let usage = proc.curr_proc.info.rssize as f64 * 100.0 / self.mem_total as f64;
let fmt_content = format!("{:.1}", usage);
let raw_content = (usage * 1000.0) as u32;
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u32, true);
}