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
use std::fs::File;
use std::time::{Duration, SystemTime};
use std::collections::{VecDeque, HashMap};
use num_cpus;
use {Meter, Error, Pid};
use error::IoStatError;
impl Meter {
pub fn new(scan_interval: Duration) -> Result<Meter, Error> {
Meter::_new(scan_interval)
}
#[cfg(target_os="linux")]
fn _new(scan_interval: Duration) -> Result<Meter, Error> {
let io_file = File::open("/proc/self/io").map_err(IoStatError::Io)?;
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),
io_file: io_file,
memory_swap_peak: 0,
memory_rss_peak: 0,
})
}
#[cfg(not(target_os="linux"))]
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);
}
}
#[cfg(target_os="linux")]
pub fn track_current_thread(&mut self, name: &str) -> Pid {
use libc::{syscall, SYS_gettid};
let tid = unsafe { syscall(SYS_gettid) } as Pid;
self.track_thread(tid, name);
return tid;
}
#[cfg(not(target_os="linux"))]
pub fn track_current_thread(&mut self, _name: &str) -> Pid {
0
}
#[cfg(target_os="linux")]
pub fn untrack_current_thread(&mut self) {
use libc::{syscall, SYS_gettid};
let tid = unsafe { syscall(SYS_gettid) } as Pid;
self.untrack_thread(tid);
}
#[cfg(not(target_os="linux"))]
pub fn untrack_current_thread(&mut self) {
}
pub fn get_scan_interval(&self) -> Duration {
self.scan_interval
}
}