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
use std::time::{Duration, SystemTime};
use std::collections::{VecDeque, HashMap};
use num_cpus;
use libc::{syscall, SYS_gettid};
use {Meter, Error, Pid};
impl Meter {
pub fn new(scan_interval: Duration) -> Result<Meter, Error> {
Ok(Meter {
scan_interval: scan_interval,
num_cpus: num_cpus::get(),
num_snapshots: 10,
start_time: SystemTime::now(),
snapshots: VecDeque::with_capacity(10),
thread_names: HashMap::new(),
text_buf: String::with_capacity(1024),
path_buf: String::with_capacity(100),
memory_swap_peak: 0,
memory_rss_peak: 0,
})
}
pub fn track_thread(&mut self, tid: Pid, name: &str) {
self.thread_names.insert(tid, name.to_string());
}
pub fn untrack_thread(&mut self, tid: Pid) {
self.thread_names.remove(&tid);
for s in &mut self.snapshots {
s.threads.remove(&tid);
}
}
pub fn track_current_thread(&mut self, name: &str) -> Pid {
let tid = unsafe { syscall(SYS_gettid) } as Pid;
self.track_thread(tid, name);
return tid;
}
pub fn untrack_current_thread(&mut self) {
let tid = unsafe { syscall(SYS_gettid) } as Pid;
self.untrack_thread(tid);
}
pub fn get_scan_interval(&self) -> Duration {
self.scan_interval
}
}