driver: split the Diagnostics out into their own object

more diagnostic refactoring to come
This commit is contained in:
2022-08-11 18:27:30 -07:00
parent 831cbfa76c
commit e85d38d415

View File

@@ -21,18 +21,35 @@ pub struct Driver<S> {
// TODO: use Rayon's thread pool?
render_pool: ThreadPool,
render_channel: (SyncSender<()>, Receiver<()>),
// TODO: split out into a Diagnostics struct
measurements: Vec<Arc<dyn AbstractMeasurement<S>>>,
stimuli: StimuliAdapter,
/// simulation end time
sim_end_time: Option<Frame>,
diag: Diagnostics,
}
struct Diagnostics {
time_spent_stepping: Duration,
time_spent_on_stimuli: Duration,
time_spent_prepping_render: Duration,
time_spent_blocked_on_render: Duration,
time_spent_rendering: Arc<Mutex<Duration>>,
measurements: Vec<Arc<dyn AbstractMeasurement<S>>>,
stimuli: StimuliAdapter,
start_time: Instant,
last_diag_time: Instant,
/// simulation end time
sim_end_time: Option<Frame>,
}
impl Diagnostics {
fn new() -> Self {
Self {
time_spent_stepping: Default::default(),
time_spent_on_stimuli: Default::default(),
time_spent_prepping_render: Default::default(),
time_spent_blocked_on_render: Default::default(),
time_spent_rendering: Default::default(),
start_time: Instant::now(),
last_diag_time: Instant::now(),
}
}
}
impl<S: AbstractSim> Driver<S> {
@@ -42,11 +59,6 @@ impl<S: AbstractSim> Driver<S> {
renderer: Arc::new(MultiRenderer::new()),
render_pool: ThreadPool::new(3),
render_channel: sync_channel(0),
time_spent_stepping: Default::default(),
time_spent_on_stimuli: Default::default(),
time_spent_prepping_render: Default::default(),
time_spent_blocked_on_render: Default::default(),
time_spent_rendering: Default::default(),
measurements: vec![
Arc::new(meas::Time),
Arc::new(meas::Meta),
@@ -54,9 +66,8 @@ impl<S: AbstractSim> Driver<S> {
Arc::new(meas::Power::world()),
],
stimuli: StimuliAdapter::new(),
start_time: Instant::now(),
last_diag_time: Instant::now(),
sim_end_time: None,
diag: Diagnostics::new(),
}
}
@@ -147,7 +158,7 @@ impl<S: AbstractSim + Clone + Default + Send + 'static> Driver<S> {
let their_state = self.state.clone();
let their_measurements = self.measurements.clone();
let renderer = self.renderer.clone();
let time_spent_rendering = self.time_spent_rendering.clone();
let time_spent_rendering = self.diag.time_spent_rendering.clone();
let sender = self.render_channel.0.clone();
self.render_pool.execute(move || {
// unblock the main thread (this limits the number of renders in-flight at any time
@@ -159,10 +170,10 @@ impl<S: AbstractSim + Clone + Default + Send + 'static> Driver<S> {
*time_spent_rendering.lock().unwrap() += start_time.elapsed();
trace!("render end");
});
self.time_spent_prepping_render += prep_start.elapsed();
self.diag.time_spent_prepping_render += prep_start.elapsed();
let block_start = Instant::now();
self.render_channel.1.recv().unwrap();
self.time_spent_blocked_on_render += block_start.elapsed();
self.diag.time_spent_blocked_on_render += block_start.elapsed();
}
/// Return the number of steps actually stepped
fn step_at_most(&mut self, at_most: u32) -> u32 {
@@ -185,17 +196,18 @@ impl<S: AbstractSim + Clone + Default + Send + 'static> Driver<S> {
trace!("step begin");
let start_time = Instant::now();
self.state.step_multiple(can_step, &self.stimuli);
self.time_spent_stepping += start_time.elapsed();
self.diag.time_spent_stepping += start_time.elapsed();
trace!("step end");
if self.last_diag_time.elapsed().as_secs_f64() >= 5.0 {
self.last_diag_time = Instant::now();
if self.diag.last_diag_time.elapsed().as_secs_f64() >= 5.0 {
// TODO: make this a method on the Diagnostics.
self.diag.last_diag_time = Instant::now();
let step = self.state.step_no();
let step_time = self.time_spent_stepping.as_secs_f64();
let stim_time = self.time_spent_on_stimuli.as_secs_f64();
let render_time = self.time_spent_rendering.lock().unwrap().as_secs_f64();
let render_prep_time = self.time_spent_prepping_render.as_secs_f64();
let block_time = self.time_spent_blocked_on_render.as_secs_f64();
let overall_time = self.start_time.elapsed().as_secs_f64();
let step_time = self.diag.time_spent_stepping.as_secs_f64();
let stim_time = self.diag.time_spent_on_stimuli.as_secs_f64();
let render_time = self.diag.time_spent_rendering.lock().unwrap().as_secs_f64();
let render_prep_time = self.diag.time_spent_prepping_render.as_secs_f64();
let block_time = self.diag.time_spent_blocked_on_render.as_secs_f64();
let overall_time = self.diag.start_time.elapsed().as_secs_f64();
let fps = (self.state.step_no() as f64) / overall_time;
let sim_time = self.state.time() as f64;
let percent_complete = match self.sim_end_time {