-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathread_bytes.rs
More file actions
106 lines (90 loc) · 3.22 KB
/
Copy pathread_bytes.rs
File metadata and controls
106 lines (90 loc) · 3.22 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
use crate::process::ProcessInfo;
use crate::util::bytify;
use crate::{Column, column_default};
use std::cmp;
use std::collections::HashMap;
pub struct ReadBytes {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, u64>,
width: usize,
}
impl ReadBytes {
pub fn new(header: Option<String>) -> Self {
let header = header.unwrap_or_else(|| String::from("Read"));
let unit = String::from("[B/s]");
Self {
fmt_contents: HashMap::new(),
raw_contents: HashMap::new(),
width: 0,
header,
unit,
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Column for ReadBytes {
fn add(&mut self, proc: &ProcessInfo) {
let (fmt_content, raw_content) = if let Some(curr_io) = proc.curr_io
&& let Some(prev_io) = proc.prev_io
{
let interval_ms = proc.interval.as_secs() + u64::from(proc.interval.subsec_millis());
let io = (curr_io.read_bytes - prev_io.read_bytes) * 1000 / interval_ms;
(bytify(io), io)
} else {
(String::new(), 0)
};
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u64, true);
}
#[cfg(target_os = "macos")]
impl Column for ReadBytes {
fn add(&mut self, proc: &ProcessInfo) {
let (fmt_content, raw_content) = if proc.curr_res.is_some() && proc.prev_res.is_some() {
let interval_ms = proc.interval.as_secs() + u64::from(proc.interval.subsec_millis());
let io = (proc.curr_res.as_ref().unwrap().ri_diskio_bytesread
- proc.prev_res.as_ref().unwrap().ri_diskio_bytesread)
* 1000
/ interval_ms;
(bytify(io), io)
} else {
(String::from(""), 0)
};
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u64, true);
}
#[cfg(target_os = "windows")]
impl Column for ReadBytes {
fn add(&mut self, proc: &ProcessInfo) {
let interval_ms = proc.interval.as_secs() + u64::from(proc.interval.subsec_millis());
let io = (proc.disk_info.curr_read - proc.disk_info.prev_read) * 1000 / interval_ms;
let raw_content = io;
let fmt_content = bytify(raw_content);
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u64, true);
}
#[cfg(target_os = "freebsd")]
impl Column for ReadBytes {
fn add(&mut self, proc: &ProcessInfo) {
// io block size: 128KB
let block_size = 128 * 1024;
let interval_ms = proc.interval.as_secs() + u64::from(proc.interval.subsec_millis());
let io = (proc.curr_proc.info.rusage.inblock as u64
- proc.prev_proc.info.rusage.inblock as u64)
* block_size
* 1000
/ interval_ms;
let raw_content = io;
let fmt_content = bytify(raw_content);
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(u64, true);
}