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
257
258
259
260
261
262
263
264
265
extern crate io_utils;
extern crate libc;
extern crate string_utils;
use io_utils::*;
use libc::{getuid, rusage, RLIMIT_NPROC};
use libc::{rlimit, setrlimit};
use std::{
cmp::min,
fs::File,
io::{BufRead, BufReader},
process::*,
time::Instant,
};
use string_utils::*;
pub fn elapsed(start: &Instant) -> f64 {
let d = start.elapsed();
d.as_secs() as f64 + d.subsec_nanos() as f64 / 1e9
}
pub fn nthreads() -> i64 {
let procfn = format!("/proc/self/status");
let prob1 = "\nWARNING: nthreads() failed to access /proc status file.\n";
let crap = "This suggests something is badly broken in the \
operating environment.\nContinuing nonetheless, by returning -1.";
let mut threads: i64 = -1;
let f = std::fs::File::open(procfn);
if !f.is_ok() {
println!("{}{}", prob1, crap);
} else {
let f = f.unwrap();
let f = std::io::BufReader::new(f);
for line in f.lines() {
let s = line.unwrap();
if s.starts_with("Threads:") {
let mut s = s.after("Threads:").to_string();
s = s.replace("\t", "").to_string();
s = s.replace(" ", "").to_string();
threads = s.force_i64();
break;
}
}
}
threads
}
pub fn set_max_threads(n: u64) {
unsafe {
let limit = rlimit {
rlim_cur: n,
rlim_max: n,
};
let _ = setrlimit(RLIMIT_NPROC, &limit);
}
}
pub fn peak_mem_usage_bytes() -> i64 {
let procfn = format!("/proc/self/status");
let prob1 = "\nWARNING: peak_mem_usage_bytes( ) failed to \
access /proc status file.\n";
let crap = "This suggests something is badly broken in the \
operating environment.\nContinuing nonetheless, by returning -1.";
let mut bytes: i64 = -1;
let f = std::fs::File::open(procfn);
if !f.is_ok() {
println!("{}{}", prob1, crap);
} else {
let f = f.unwrap();
let f = std::io::BufReader::new(f);
for line in f.lines() {
let s = line.unwrap();
if s.starts_with("VmHWM") {
let split: Vec<&str> = s.split_whitespace().collect();
let printfail = |msg| {
println!(
"\nWARNING:\
peak_mem_usage_bytes( ) encountered broken line in\
/proc status file.\n{}\nLine = {}\n{}",
msg, s, crap
)
};
if split.len() != 3 {
printfail("Field count wrong.");
} else if split[2] != "kB" {
printfail("Bad units field.");
} else if !split[1].parse::<i64>().is_ok() {
printfail("Bad count field.");
} else {
bytes = split[1].force_i64() * 1024;
}
break;
}
}
}
bytes
}
pub fn peak_mem_usage_gb() -> f64 {
peak_mem_usage_bytes() as f64 / ((1024 * 1024 * 1024) as f64)
}
pub fn available_mem_gb() -> Option<f64> {
let procfn = format!("/proc/meminfo");
let f = std::fs::File::open(procfn);
if !f.is_ok() {
return None;
} else {
let f = f.unwrap();
let f = std::io::BufReader::new(f);
for line in f.lines() {
let s = line.unwrap();
if s.starts_with("MemAvailable") {
let split: Vec<&str> = s.split_whitespace().collect();
if split.len() != 3 || split[2] != "kB" {
return None;
}
if !split[1].parse::<i64>().is_ok() {
return None;
}
return Some(split[1].force_f64() / (1024 * 1024) as f64);
}
}
}
None
}
pub fn getrusage() -> rusage {
use std::mem::MaybeUninit;
let usage: rusage = unsafe { MaybeUninit::zeroed().assume_init() };
usage
}
pub fn mem_usage_bytes() -> i64 {
let procfn = format!("/proc/self/statm");
let prob1 = "\nWARNING: mem_usage_bytes( ) failed to access /proc/self/statm.\n";
let crap = "This suggests something is badly broken in the \
operating environment.\nContinuing nonetheless, by returning -1.";
let f = std::fs::File::open(procfn);
if !f.is_ok() {
println!("{}{}", prob1, crap);
} else {
let f = f.unwrap();
let f = std::io::BufReader::new(f);
for line in f.lines() {
let s = line.unwrap();
let fields: Vec<&str> = s.split_whitespace().collect();
let page_size = 4096 as i64;
return fields[1].force_i64() * page_size;
}
}
-1 as i64
}
pub fn mem_usage_gb() -> f64 {
mem_usage_bytes() as f64 / ((1024 * 1024 * 1024) as f64)
}
pub fn ps_me() {
let uid = unsafe { getuid() } as i64;
println!(
"\nPROCESSES HAVING THE SAME OWNER (UID={}) AS THIS PROCESS = {}\n",
uid,
id()
);
let procs = dir_list("/proc");
println!("{:>6} {:>6} {:>7} {}", "PPID", "PID", "GB", "CMD");
const RIGHT: usize = 60;
'outer: for i in 0..procs.len() {
if !procs[i].parse::<i64>().is_ok() {
continue;
}
let (mut pid, mut ppid) = (-1 as i64, -1 as i64);
let mut rss = -1 as f64;
let mut cmd = String::new();
let f = format!("/proc/{}/status", procs[i]);
let f = File::open(&f);
if !f.is_ok() {
continue;
}
let f = f.unwrap();
let f = BufReader::new(f);
for line in f.lines() {
if !line.is_ok() {
continue 'outer;
}
let s = line.unwrap();
if s.starts_with("Uid:\t") {
let this_uid = s.after("Uid:\t").before("\t").force_i64();
if this_uid != uid {
continue 'outer;
}
} else if s.starts_with("Pid:\t") {
pid = s.after("Pid:\t").force_i64();
} else if s.starts_with("PPid:\t") {
ppid = s.after("PPid:\t").force_i64();
} else if s.starts_with("VmRSS:\t") {
let t = s.after("VmRSS:\t");
let t = t.to_string().replace(" ", "");
rss = t.before("k").force_f64() / (1024 * 1024) as f64;
}
}
let f = format!("/proc/{}/cmdline", procs[i]);
let f = File::open(&f);
if !f.is_ok() {
continue;
}
let f = f.unwrap();
let f = BufReader::new(f);
for line in f.lines() {
if !line.is_ok() {
continue 'outer;
}
cmd = line.unwrap();
cmd = cmd.replace("