Skip to content

Commit

Permalink
[runtime] Enhancement: Add bounds check to timer resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbonde committed Jun 12, 2024
1 parent bac702e commit 26c274b
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/rust/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,21 @@ use ::std::{
SystemTime,
},
};
use std::pin::Pin;
use std::{
cmp::{
max,
min,
},
pin::Pin,
};

//======================================================================================================================
// Constants
//======================================================================================================================

// TODO: Make this more accurate using rdtsc.
// FIXME: https://github.com/microsoft/demikernel/issues/1226
const TIMER_RESOLUTION: usize = 64;
const TIMER_FINER_RESOLUTION: usize = 2;
const MIN_TIMER_RESOLUTION: usize = 64;
const MAX_TIMER_RESOLUTION: usize = 1_000_000;
const FINE_TIMER_RESOLUTION: usize = 2;

//======================================================================================================================
// Structures
Expand Down Expand Up @@ -158,7 +163,7 @@ impl SharedDemiRuntime {
rdtscp
},
last_time: Instant::now(),
timer_resolution: TIMER_RESOLUTION,
timer_resolution: MIN_TIMER_RESOLUTION,
completed_tasks: HashMap::<QToken, (QDesc, OperationResult)>::new(),
}))
}
Expand Down Expand Up @@ -371,7 +376,7 @@ impl SharedDemiRuntime {
fn run_next(&mut self, timeout: Duration) -> Option<(QToken, QDesc, OperationResult)> {
let iterations: usize = match timeout {
timeout if timeout.as_secs() > 0 => self.timer_resolution,
_ => TIMER_FINER_RESOLUTION,
_ => FINE_TIMER_RESOLUTION,
};
if let Some(boxed_task) = self.scheduler.get_next_completed_task(iterations) {
// Perform bookkeeping for the completed and removed task.
Expand Down Expand Up @@ -565,12 +570,12 @@ impl SharedDemiRuntime {
return;
}

let cycles_per_quanta: usize = cycles_per_second / TIMER_RESOLUTION;
let cycles_per_quanta: usize = cycles_per_second / MIN_TIMER_RESOLUTION;
if cycles_per_quanta == 0 {
return;
}

self.timer_resolution = cycles_per_quanta;
self.timer_resolution = max(MIN_TIMER_RESOLUTION, min(MAX_TIMER_RESOLUTION, cycles_per_quanta));
trace!("Adjusted timer resolution to: {:?}", self.timer_resolution);
self.last_tsc = curr_tsc;
self.last_time = now;
Expand Down Expand Up @@ -642,7 +647,7 @@ impl Default for SharedDemiRuntime {
tsc
},
last_time: Instant::now(),
timer_resolution: TIMER_RESOLUTION,
timer_resolution: MIN_TIMER_RESOLUTION,
completed_tasks: HashMap::<QToken, (QDesc, OperationResult)>::new(),
}))
}
Expand Down

0 comments on commit 26c274b

Please sign in to comment.