Skip to content

Commit 3e7c117

Browse files
committed
ddsperf: Add conditional compilation to procfs-based CPU statistics.
1 parent b90d58c commit 3e7c117

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ ctrlc = "3.1.6"
110110
# async-shapes-demo
111111
smol = "2.0"
112112

113+
async-io ="*" # ddsperf
114+
113115
[target.'cfg(unix)'.dev-dependencies]
114116
# turle_teleop
115117
termion = "4.0.2"
116118

117-
# ddsperf
118-
async-io ="*"
119-
procfs = "0.17"
119+
120+
[target.'cfg(target_os = "linux")'.dev-dependencies]
121+
procfs = "0.17" # for ddsperf

examples/ddsperf/main.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Performance test program inspired by `ddsperf` in CycloneDDS
22
3-
use std::time::{Duration, Instant};
3+
use std::time::Duration;
4+
#[cfg(target_os = "linux")]
5+
use std::time::Instant;
46

57
use log::error;
68
use rustdds::{
@@ -75,33 +77,7 @@ enum PubModeArgs {
7577
fn main() {
7678
let command_line_args = CommandLineArgs::parse();
7779

78-
let this_process = procfs::process::Process::myself().unwrap();
79-
let process_ticks_per_second = procfs::ticks_per_second() as f32;
80-
let kernel_page_size = procfs::page_size();
81-
82-
let mut process_stat = this_process.stat().unwrap();
83-
let mut last_stat_instant = Instant::now();
84-
let mut print_and_reset_cpu_usage = move || {
85-
let prev_utime = process_stat.utime;
86-
let prev_stime = process_stat.stime;
87-
process_stat = this_process.stat().unwrap();
88-
89-
let stat_instant = Instant::now();
90-
let call_interval = stat_instant.duration_since(last_stat_instant).as_secs_f32();
91-
last_stat_instant = stat_instant;
92-
93-
let stat_mem = this_process.statm().unwrap();
94-
let rss_size_bytes = stat_mem.resident * kernel_page_size;
95-
96-
let user_percentage =
97-
100.0 * ((process_stat.utime - prev_utime) as f32 / process_ticks_per_second) / call_interval;
98-
let sys_percentage =
99-
100.0 * ((process_stat.stime - prev_stime) as f32 / process_ticks_per_second) / call_interval;
100-
println!(
101-
"user {user_percentage:2.0}% sys {sys_percentage:2.0}% RSS {}B",
102-
format_count(rss_size_bytes)
103-
);
104-
};
80+
let mut print_and_reset_cpu_usage = cpu_usage_printer_closure();
10581

10682
#[cfg(debug_assertions)]
10783
println!("-------\nNOTE: Running debug build for performace test. It will be slow.\n-------");
@@ -496,3 +472,42 @@ fn format_count(count: u64) -> String {
496472
format!("{:2.1}G", count as f64 / 1_000_000_000.0)
497473
}
498474
}
475+
476+
#[cfg(target_os = "linux")] // procfs is onl available on linux
477+
fn cpu_usage_printer_closure() -> impl FnMut() {
478+
let this_process = procfs::process::Process::myself().unwrap();
479+
let process_ticks_per_second = procfs::ticks_per_second() as f32;
480+
let kernel_page_size = procfs::page_size();
481+
482+
let mut process_stat = this_process.stat().unwrap();
483+
let mut last_stat_instant = Instant::now();
484+
485+
move || {
486+
let prev_utime = process_stat.utime;
487+
let prev_stime = process_stat.stime;
488+
process_stat = this_process.stat().unwrap();
489+
490+
let stat_instant = Instant::now();
491+
let call_interval = stat_instant.duration_since(last_stat_instant).as_secs_f32();
492+
last_stat_instant = stat_instant;
493+
494+
let stat_mem = this_process.statm().unwrap();
495+
let rss_size_bytes = stat_mem.resident * kernel_page_size;
496+
497+
let user_percentage =
498+
100.0 * ((process_stat.utime - prev_utime) as f32 / process_ticks_per_second) / call_interval;
499+
let sys_percentage =
500+
100.0 * ((process_stat.stime - prev_stime) as f32 / process_ticks_per_second) / call_interval;
501+
println!(
502+
"user {user_percentage:2.0}% sys {sys_percentage:2.0}% RSS {}B",
503+
format_count(rss_size_bytes)
504+
);
505+
}
506+
}
507+
508+
#[cfg(not(target_os = "linux"))]
509+
fn cpu_usage_printer_closure() -> impl FnMut() {
510+
|| {
511+
// no-op
512+
}
513+
}

0 commit comments

Comments
 (0)