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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use super::{result::*, util::*};
use sigar_sys::*;
pub fn current_pid() -> SigarResult<u32> {
ffi_wrap_sigar_t!((|ptr_t| unsafe { sigar_pid_get(ptr_t) as u32 }))
}
pub fn kill(pid: u32, signal: i32) -> SigarResult<()> {
let res = unsafe { sigar_proc_kill(pid as sigar_pid_t, signal as ::std::os::raw::c_int) };
if res != SIGAR_CODE_OK {
let reason = ffi_wrap_sigar_t!((|ptr_t| error_string(ptr_t, res.into())))?;
return Err(Error::from_string(reason));
}
Ok(())
}
pub type PIDList = Vec<u32>;
pub fn list() -> SigarResult<PIDList> {
ffi_wrap_destroy!(
sigar_proc_list_get,
sigar_proc_list_destroy,
sigar_proc_list_t,
(|list: &sigar_proc_list_t| ffi_extract_list!(list, (|one: &sigar_pid_t| *one as u32)))
)
}
#[derive(Debug)]
pub struct Summary {
pub total: u64,
pub sleeping: u64,
pub running: u64,
pub zombie: u64,
pub stopped: u64,
pub idle: u64,
pub threads: u64,
}
pub fn summary() -> SigarResult<Summary> {
let raw = ffi_wrap!(sigar_proc_stat_get, sigar_proc_stat_t)?;
Ok(value_convert!(
Summary, raw, total, sleeping, running, zombie, stopped, idle, threads
))
}
#[derive(Debug, Default, Copy, Clone)]
pub struct Mem {
pub size: u64,
pub resident: u64,
pub share: u64,
pub minor_faults: u64,
pub major_faults: u64,
pub page_faults: u64,
}
pub fn mem(pid: u32) -> SigarResult<Mem> {
let raw = ffi_wrap!(sigar_proc_mem_get, (pid as sigar_pid_t), sigar_proc_mem_t)?;
Ok(value_convert!(
Mem,
raw,
size,
resident,
share,
minor_faults,
major_faults,
page_faults,
))
}
#[derive(Debug)]
pub struct DiskIO {
pub bytes_read: u64,
pub bytes_written: u64,
pub bytes_total: u64,
}
pub fn disk_io(pid: u32) -> SigarResult<DiskIO> {
let raw = ffi_wrap!(
sigar_proc_disk_io_get,
(pid as sigar_pid_t),
sigar_proc_disk_io_t
)?;
Ok(value_convert!(
DiskIO,
raw,
bytes_read,
bytes_written,
bytes_total,
))
}
pub fn cum_disk_io(pid: u32) -> SigarResult<DiskIO> {
let raw = ffi_wrap!(
sigar_proc_cumulative_disk_io_get,
(pid as sigar_pid_t),
sigar_proc_cumulative_disk_io_t
)?;
Ok(value_convert!(
DiskIO,
raw,
bytes_read,
bytes_written,
bytes_total,
))
}
#[derive(Debug)]
pub struct Cred {
pub uid: u32,
pub gid: u32,
pub euid: u32,
pub egid: u32,
}
pub fn cred(pid: u32) -> SigarResult<Cred> {
let raw = ffi_wrap!(sigar_proc_cred_get, (pid as sigar_pid_t), sigar_proc_cred_t)?;
Ok(value_convert!(Cred, raw, uid, gid, euid, egid))
}
#[derive(Debug)]
pub struct CredName {
pub user: Vec<u8>,
pub group: Vec<u8>,
}
pub fn cred_name(pid: u32) -> SigarResult<CredName> {
let raw = ffi_wrap!(
sigar_proc_cred_name_get,
(pid as sigar_pid_t),
sigar_proc_cred_name_t
)?;
Ok(CredName {
user: chars_to_bytes(&raw.user[..]),
group: chars_to_bytes(&raw.group[..]),
})
}
#[derive(Debug)]
pub struct Time {
pub start_time: u64,
pub user: u64,
pub sys: u64,
pub total: u64,
}
pub fn time(pid: u32) -> SigarResult<Time> {
let raw = ffi_wrap!(sigar_proc_time_get, (pid as sigar_pid_t), sigar_proc_time_t)?;
Ok(value_convert!(Time, raw, start_time, user, sys, total))
}
#[derive(Debug)]
pub struct CPU {
pub start_time: u64,
pub user: u64,
pub sys: u64,
pub total: u64,
pub last_time: u64,
pub percent: f64,
}
pub fn cpu(pid: u32) -> SigarResult<CPU> {
let raw = ffi_wrap!(sigar_proc_cpu_get, (pid as sigar_pid_t), sigar_proc_cpu_t)?;
Ok(value_convert!(
CPU, raw, start_time, user, sys, total, last_time, percent,
))
}
#[derive(Debug)]
pub struct State {
pub name: Vec<u8>,
pub state: u8,
pub ppid: i32,
pub tty: i32,
pub priority: i32,
pub nice: i32,
pub processor: i32,
pub threads: u64,
}
pub fn state(pid: u32) -> SigarResult<State> {
let raw = ffi_wrap!(
sigar_proc_state_get,
(pid as sigar_pid_t),
sigar_proc_state_t
)?;
Ok(value_convert!(
State, raw, ppid, tty, priority, nice, processor, threads,
(name: chars_to_bytes(&raw.name[..])),
(state: raw.state as u8),
))
}
#[derive(Debug)]
pub struct FD {
pub total: u64,
}
pub fn fd(pid: u32) -> SigarResult<FD> {
let raw = ffi_wrap!(sigar_proc_fd_get, (pid as sigar_pid_t), sigar_proc_fd_t)?;
Ok(value_convert!(FD, raw, total,))
}