use CLOCK_MONOTONIC from libc, added os x support
This commit is contained in:
@@ -2,8 +2,6 @@ extern crate libc;
|
|||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
const CLOCK_MONOTONIC: i32 = 1;
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
fn clock_nanosleep(clk_id: libc::clockid_t,
|
fn clock_nanosleep(clk_id: libc::clockid_t,
|
||||||
flags: libc::c_int,
|
flags: libc::c_int,
|
||||||
@@ -14,11 +12,7 @@ fn clock_nanosleep(clk_id: libc::clockid_t,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
fn clock_nanosleep(_clk_id: libc::c_int,
|
fn nanosleep(rqtp: *const libc::timespec, rmtp: *mut libc::timespec) -> libc::c_int {
|
||||||
_flags: libc::c_int,
|
|
||||||
rqtp: *const libc::timespec,
|
|
||||||
rmtp: *mut libc::timespec)
|
|
||||||
-> libc::c_int {
|
|
||||||
unsafe { libc::nanosleep(rqtp as *const _, rmtp as *mut _) }
|
unsafe { libc::nanosleep(rqtp as *const _, rmtp as *mut _) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +25,12 @@ pub fn sleep(duration: Duration) -> Result<Duration, libc::c_int> {
|
|||||||
tv_sec: 0,
|
tv_sec: 0,
|
||||||
tv_nsec: 0,
|
tv_nsec: 0,
|
||||||
};
|
};
|
||||||
let ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &mut remain);
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
let ret = clock_nanosleep(libc::CLOCK_MONOTONIC, 0, &ts, &mut remain);
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
let ret = nanosleep(&ts, &mut remain);
|
||||||
|
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
Ok(timespec_to_duration(remain))
|
Ok(timespec_to_duration(remain))
|
||||||
|
|||||||
@@ -8,10 +8,16 @@ use std::fs::File;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use argparse::{ArgumentParser, Print, Store, StoreTrue};
|
use argparse::{ArgumentParser, Print, Store};
|
||||||
use sleep::{set_priority, sleep};
|
use sleep::sleep;
|
||||||
use time::precise_time_ns;
|
use time::precise_time_ns;
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
use argparse::StoreTrue;
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
use sleep::set_priority;
|
||||||
|
|
||||||
fn duration_from_ns(duration_ns: u64) -> Duration {
|
fn duration_from_ns(duration_ns: u64) -> Duration {
|
||||||
Duration::new(duration_ns / 1_000_000_000,
|
Duration::new(duration_ns / 1_000_000_000,
|
||||||
(duration_ns % 1_000_000_000) as u32)
|
(duration_ns % 1_000_000_000) as u32)
|
||||||
@@ -22,11 +28,11 @@ fn duration_to_ns(duration: Duration) -> u64 {
|
|||||||
|
|
||||||
fn measure_duration(sleep_duration: Duration) -> Duration {
|
fn measure_duration(sleep_duration: Duration) -> Duration {
|
||||||
let start = precise_time_ns();
|
let start = precise_time_ns();
|
||||||
let remain = sleep(sleep_duration);
|
let _remain = sleep(sleep_duration);
|
||||||
let end = precise_time_ns();
|
let end = precise_time_ns();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("remain = {:?}", remain);
|
println!("remain = {:?}", _remain);
|
||||||
|
|
||||||
let duration = end - start;
|
let duration = end - start;
|
||||||
let duration = duration_from_ns(duration);
|
let duration = duration_from_ns(duration);
|
||||||
@@ -80,10 +86,15 @@ fn main() {
|
|||||||
let mut max: u64 = 100_000_000;
|
let mut max: u64 = 100_000_000;
|
||||||
let mut step: u64 = 1_000_000;
|
let mut step: u64 = 1_000_000;
|
||||||
let mut count: u64 = 100;
|
let mut count: u64 = 100;
|
||||||
let mut realtime: bool = false;
|
|
||||||
let mut output: String = "".to_string();
|
let mut output: String = "".to_string();
|
||||||
let mut file: Option<File> = None;
|
let mut file: Option<File> = None;
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
let mut realtime: bool = false;
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
let realtime: bool = false;
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut ap = ArgumentParser::new();
|
let mut ap = ArgumentParser::new();
|
||||||
ap.set_description(env!("CARGO_PKG_DESCRIPTION"));
|
ap.set_description(env!("CARGO_PKG_DESCRIPTION"));
|
||||||
@@ -91,6 +102,7 @@ fn main() {
|
|||||||
ap.refer(&mut max).add_option(&["--max"], Store, "Sleep period end");
|
ap.refer(&mut max).add_option(&["--max"], Store, "Sleep period end");
|
||||||
ap.refer(&mut step).add_option(&["--step"], Store, "Sleep period step size");
|
ap.refer(&mut step).add_option(&["--step"], Store, "Sleep period step size");
|
||||||
ap.refer(&mut count).add_option(&["--loop"], Store, "Count of measurements per period");
|
ap.refer(&mut count).add_option(&["--loop"], Store, "Count of measurements per period");
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
ap.refer(&mut realtime).add_option(&["--rt"], StoreTrue, "Output file");
|
ap.refer(&mut realtime).add_option(&["--rt"], StoreTrue, "Output file");
|
||||||
ap.refer(&mut output).add_option(&["-o", "--out"], Store, "Output file");
|
ap.refer(&mut output).add_option(&["-o", "--out"], Store, "Output file");
|
||||||
ap.add_option(&["-V", "--version"],
|
ap.add_option(&["-V", "--version"],
|
||||||
@@ -100,8 +112,12 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if realtime {
|
if realtime {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
let ret = set_priority(99);
|
let ret = set_priority(99);
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
let ret = 0;
|
||||||
|
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
panic!("Set realtime priority failed.");
|
panic!("Set realtime priority failed.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user