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
}
}