-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.rs
203 lines (182 loc) · 6.28 KB
/
data.rs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::time::Duration;
use bincode::{Decode, Encode};
use byte_unit::Byte;
#[cfg(target_os = "linux")]
use heim::process::os::linux::MemoryExt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct Data {
pub memory: Memory,
pub cpu_time: CpuTime,
pub cpu_usage: CpuUsage,
pub io: Io,
// linux only
pub net_io: HashMap<String, NetIo>,
}
#[derive(Clone, Encode, Decode, Serialize, Deserialize)]
pub struct CpuUsage(pub f32);
#[derive(Clone, Encode, Decode, Serialize, Deserialize)]
pub struct Memory {
pub rss: u64,
pub vms: u64,
// linux only
pub shared: Option<u64>,
pub text: Option<u64>,
pub data: Option<u64>,
}
#[derive(Clone, Encode, Decode, Serialize, Deserialize)]
pub struct CpuTime {
pub user: f64,
pub system: f64,
}
#[derive(Clone, Encode, Decode, Serialize, Deserialize)]
pub struct Io {
pub bytes_written: u64,
pub bytes_read: u64,
// linux only
pub disk_written: Option<u64>,
pub disk_read: Option<u64>,
pub syscall_written: Option<u64>,
pub syscall_read: Option<u64>,
}
#[derive(Clone, Encode, Decode, Serialize, Deserialize)]
pub struct NetIo {
bytes_sent: u64,
bytes_recv: u64,
packets_sent: u64,
packets_recv: u64,
errors_sent: u64,
errors_recv: u64,
drop_recv: u64,
drop_sent: u64,
}
#[cfg(target_os = "linux")]
impl From<heim::process::IoCounters> for NetIo {
fn from(io: heim::process::IoCounters) -> Self {
use heim::net::os::linux::IoCountersExt;
NetIo {
bytes_sent: io.bytes_sent().get::<heim::units::information::byte>(),
bytes_recv: io.bytes_recv().get::<heim::units::information::byte>(),
packets_sent: io.packets_sent(),
packets_recv: io.packets_recv(),
errors_sent: io.errors_sent(),
errors_recv: io.errors_recv(),
drop_recv: io.drop_recv(),
drop_sent: io.drop_sent(),
}
}
}
impl Debug for NetIo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let to_string = |x: Byte| x.get_appropriate_unit(false).to_string();
f.debug_struct("NetIo")
.field("bytes_sent", &to_string(Byte::from(self.bytes_sent)))
.field("bytes_recv", &to_string(Byte::from(self.bytes_recv)))
.field("packets_sent", &self.packets_sent)
.field("packets_recv", &self.packets_recv)
.field("errors_sent", &self.errors_sent)
.field("errors_recv", &self.errors_recv)
.field("drop_recv", &self.drop_recv)
.field("drop_sent", &self.drop_sent)
.finish()
}
}
#[cfg(not(target_os = "linux"))]
impl From<heim::process::IoCounters> for Io {
fn from(io: heim::process::IoCounters) -> Self {
Io {
bytes_written: io.bytes_written().get::<heim::units::information::byte>(),
bytes_read: io.bytes_read().get::<heim::units::information::byte>(),
disk_written: None,
disk_read: None,
syscall_written: None,
syscall_read: None,
}
}
}
impl Debug for Io {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let to_string = |x: Byte| x.get_appropriate_unit(false).to_string();
f.debug_struct("I/O")
.field("bytes_written", &to_string(Byte::from(self.bytes_written)))
.field("bytes_read", &to_string(Byte::from(self.bytes_read)))
.field(
"disk_written",
&self.disk_written.map(Byte::from).map(to_string),
)
.field("disk_read", &self.disk_read.map(Byte::from).map(to_string))
.field("syscall_written", &self.syscall_written)
.field("syscall_read", &self.syscall_read)
.finish()
}
}
impl From<heim::process::CpuTime> for CpuTime {
fn from(cpu_time: heim::process::CpuTime) -> Self {
CpuTime {
user: cpu_time.user().get::<heim::units::time::microsecond>(),
system: cpu_time.system().get::<heim::units::time::microsecond>(),
}
}
}
impl Debug for CpuTime {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CpuTime")
.field("user", &Duration::from_micros(self.user as u64))
.field("system", &Duration::from_micros(self.system as u64))
.finish()
}
}
impl Debug for CpuUsage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}%", self.0.round() / num_cpus::get() as f32)
}
}
impl From<heim::process::Memory> for Memory {
fn from(mem: heim::process::Memory) -> Self {
Memory {
rss: mem.rss().get::<heim::units::information::byte>(),
vms: mem.vms().get::<heim::units::information::byte>(),
shared: {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
Some(mem.shared().get::<heim::units::information::byte>())
} else {
None
}
}
},
text: {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
Some(mem.text().get::<heim::units::information::byte>())
} else {
None
}
}
},
data: {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
Some(mem.data().get::<heim::units::information::byte>())
} else {
None
}
}
},
}
}
}
impl Debug for Memory {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let to_string = |x: Byte| x.get_appropriate_unit(false).to_string();
f.debug_struct("Memory")
.field("vms", &to_string(Byte::from(self.vms)))
.field("rss", &to_string(Byte::from(self.rss)))
.field("shared", &self.shared.map(Byte::from).map(to_string))
.field("text", &self.text.map(Byte::from).map(to_string))
.field("data", &self.data.map(Byte::from).map(to_string))
.finish()
}
}