Skip to content

Commit b90d58c

Browse files
committed
Add memory (RSS) size accounting to ddsperf example.
1 parent 5744be9 commit b90d58c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

examples/ddsperf/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn main() {
7777

7878
let this_process = procfs::process::Process::myself().unwrap();
7979
let process_ticks_per_second = procfs::ticks_per_second() as f32;
80+
let kernel_page_size = procfs::page_size();
8081

8182
let mut process_stat = this_process.stat().unwrap();
8283
let mut last_stat_instant = Instant::now();
@@ -89,11 +90,17 @@ fn main() {
8990
let call_interval = stat_instant.duration_since(last_stat_instant).as_secs_f32();
9091
last_stat_instant = stat_instant;
9192

93+
let stat_mem = this_process.statm().unwrap();
94+
let rss_size_bytes = stat_mem.resident * kernel_page_size;
95+
9296
let user_percentage =
9397
100.0 * ((process_stat.utime - prev_utime) as f32 / process_ticks_per_second) / call_interval;
9498
let sys_percentage =
9599
100.0 * ((process_stat.stime - prev_stime) as f32 / process_ticks_per_second) / call_interval;
96-
println!("user {user_percentage:2.0}% sys {sys_percentage:2.0}%");
100+
println!(
101+
"user {user_percentage:2.0}% sys {sys_percentage:2.0}% RSS {}B",
102+
format_count(rss_size_bytes)
103+
);
97104
};
98105

99106
#[cfg(debug_assertions)]
@@ -174,6 +181,7 @@ fn main() {
174181
format_count(sample_count), format_count(byte_count));
175182
sample_count = 0;
176183
byte_count = 0;
184+
print_and_reset_cpu_usage();
177185
}
178186

179187
result = sample_stream.select_next_some() => {
@@ -409,6 +417,7 @@ fn main() {
409417
format_count(sample_count as u64), format_count(byte_count));
410418
sample_count = 0;
411419
byte_count = 0;
420+
print_and_reset_cpu_usage();
412421
}
413422

414423
result = sample_stream.select_next_some() => {

0 commit comments

Comments
 (0)