added realtime parameter
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
extern crate libc;
|
||||
|
||||
use std::time::Duration;
|
||||
use libc::{c_int, c_long, time_t, timespec};
|
||||
|
||||
const CLOCK_MONOTONIC: i32 = 1;
|
||||
|
||||
@@ -15,9 +14,9 @@ fn clock_nanosleep(_clk_id: libc::c_int, _flags: libc::c_int, rqtp: *const libc:
|
||||
unsafe { libc::nanosleep(rqtp as *const _, rmtp as *mut _) }
|
||||
}
|
||||
|
||||
pub fn sleep(duration: Duration) -> Result<Duration, c_int> {
|
||||
pub fn sleep(duration: Duration) -> Result<Duration, libc::c_int> {
|
||||
let ts = duration_to_timespec(duration);
|
||||
let mut remain = timespec {
|
||||
let mut remain = libc::timespec {
|
||||
tv_sec: 0,
|
||||
tv_nsec: 0,
|
||||
};
|
||||
@@ -30,13 +29,28 @@ pub fn sleep(duration: Duration) -> Result<Duration, c_int> {
|
||||
}
|
||||
}
|
||||
|
||||
fn duration_to_timespec(duration: Duration) -> timespec {
|
||||
timespec {
|
||||
tv_sec: duration.as_secs() as time_t,
|
||||
tv_nsec: duration.subsec_nanos() as c_long,
|
||||
fn duration_to_timespec(duration: Duration) -> libc::timespec {
|
||||
libc::timespec {
|
||||
tv_sec: duration.as_secs() as libc::time_t,
|
||||
tv_nsec: duration.subsec_nanos() as libc::c_long,
|
||||
}
|
||||
}
|
||||
|
||||
fn timespec_to_duration(timespec: timespec) -> Duration {
|
||||
fn timespec_to_duration(timespec: libc::timespec) -> Duration {
|
||||
Duration::new(timespec.tv_sec as u64, timespec.tv_nsec as u32)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn set_scheduler(policy: i32, priority: i32) -> libc::c_int {
|
||||
let pid: libc::pid_t = 0;
|
||||
let param = libc::sched_param {
|
||||
sched_priority: priority
|
||||
};
|
||||
|
||||
unsafe { libc::sched_setscheduler(pid as libc::pid_t, policy as libc::c_int, ¶m) }
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn set_priority(priority: i32) -> libc::c_int {
|
||||
set_scheduler(libc::SCHED_RR, priority)
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
use argparse::{ArgumentParser, Print, Store};
|
||||
use sleep::sleep;
|
||||
use argparse::{ArgumentParser, Print, Store, StoreTrue};
|
||||
use sleep::{set_priority, sleep};
|
||||
use time::precise_time_ns;
|
||||
|
||||
fn duration_from_ns(duration_ns: u64) -> Duration {
|
||||
@@ -79,6 +79,7 @@ fn main() {
|
||||
let mut max: u64 = 100_000_000;
|
||||
let mut step: u64 = 1_000_000;
|
||||
let mut count: u64 = 100;
|
||||
let mut realtime: bool = false;
|
||||
let mut output: String = "".to_string();
|
||||
let mut file: Option<File> = None;
|
||||
|
||||
@@ -97,6 +98,9 @@ fn main() {
|
||||
ap.refer(&mut count)
|
||||
.add_option(&["--loop"], Store,
|
||||
"Count of measurements per period");
|
||||
ap.refer(&mut realtime)
|
||||
.add_option(&["--rt"], StoreTrue,
|
||||
"Output file");
|
||||
ap.refer(&mut output)
|
||||
.add_option(&["-o", "--out"], Store,
|
||||
"Output file");
|
||||
@@ -105,6 +109,14 @@ fn main() {
|
||||
ap.parse_args_or_exit();
|
||||
}
|
||||
|
||||
if realtime {
|
||||
let ret = set_priority(99);
|
||||
|
||||
if ret != 0 {
|
||||
panic!("Set realtime priority failed.");
|
||||
}
|
||||
}
|
||||
|
||||
if output != "" {
|
||||
file = Some(File::create(Path::new(output.as_str()))
|
||||
.expect(format!("Open file '{}' failed", output).as_str()));
|
||||
|
||||
Reference in New Issue
Block a user