From 9368c42c6e717b8b5afd42fd65acd9f5b351cedf Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 23 Nov 2018 09:44:08 -0800 Subject: [PATCH] improve error message for cars too close, and add error dumping to headless --- sim/src/driving.rs | 32 ++++++++++++++++++++++++++++++-- sim/src/helpers.rs | 14 +++++++++++++- sim/src/kinematics.rs | 2 +- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/sim/src/driving.rs b/sim/src/driving.rs index 1ae05c786b..ba240a627e 100644 --- a/sim/src/driving.rs +++ b/sim/src/driving.rs @@ -243,9 +243,21 @@ impl Car { .unwrap(), ); if self.debug { + let describe_accel = if safe_accel == vehicle.max_accel { + format!("max_accel ({})", safe_accel) + } else if safe_accel == vehicle.max_deaccel { + format!("max_deaccel ({})", safe_accel) + } else { + format!("{}", safe_accel) + }; + let describe_speed = if Some(self.speed) == vehicle.max_speed { + format!("max_speed ({})", self.speed) + } else { + format!("{}", self.speed) + }; debug!( "At {}, {} chose {}, with current speed {}", - time, self.id, safe_accel, self.speed + time, self.id, describe_accel, describe_speed ); } @@ -381,7 +393,23 @@ impl SimQueue { let ((dist1, c1), (dist2, c2)) = (slice[0], slice[1]); let following_dist = cars[&c1].vehicle.following_dist(); if dist1 - dist2 < following_dist { - return Err(Error::new(format!("uh oh! on {:?}, reset to {:?} broke. min following distance is {}, but we have {} at {} and {} at {}. dist btwn is just {}. prev queue was {:?}", self.id, self.cars_queue, following_dist, c1, dist1, c2, dist2, dist1 - dist2, old_queue))); + let mut err = format!( + "On {:?}, {} and {} are {} apart -- that's {} too close\n", + self.id, + c1, + c2, + dist1 - dist2, + following_dist - (dist1 - dist2), + ); + err.push_str(&format!("Old queue ({}):\n", old_queue.len())); + for (dist, id) in old_queue { + err.push_str(&format!("- {} at {}\n", id, dist)); + } + err.push_str(&format!("New queue ({}):\n", self.cars_queue.len())); + for (dist, id) in &self.cars_queue { + err.push_str(&format!("- {} at {}\n", id, dist)); + } + return Err(Error::new(err)); } } Ok(()) diff --git a/sim/src/helpers.rs b/sim/src/helpers.rs index 205c423943..db6285668b 100644 --- a/sim/src/helpers.rs +++ b/sim/src/helpers.rs @@ -2,6 +2,7 @@ use abstutil::WeightedUsizeChoice; use control::ControlMap; use map_model::{BuildingID, BusRoute, Map, RoadID}; use std::collections::{BTreeSet, VecDeque}; +use std::panic; use { BorderSpawnOverTime, CarID, Event, OriginDestination, Scenario, SeedParkedCars, Sim, SpawnOverTime, Tick, @@ -14,7 +15,18 @@ impl Sim { pub fn run_until_done(&mut self, map: &Map, control_map: &ControlMap, callback: Box) { let mut benchmark = self.start_benchmark(); loop { - self.step(&map, &control_map); + match panic::catch_unwind(panic::AssertUnwindSafe(|| { + self.step(&map, &control_map); + })) { + Ok(()) => {} + Err(err) => { + error!("********************************************************************************"); + error!("Sim broke:"); + self.dump_before_abort(); + panic::resume_unwind(err); + } + } + if self.time.is_multiple_of(Tick::from_minutes(1)) { let speed = self.measure_speed(&mut benchmark); info!("{0}, speed = {1:.2}x", self.summary(), speed); diff --git a/sim/src/kinematics.rs b/sim/src/kinematics.rs index f18ade2c43..eaf08e01df 100644 --- a/sim/src/kinematics.rs +++ b/sim/src/kinematics.rs @@ -182,7 +182,7 @@ impl Vehicle { if self.debug { debug!( - "accel_to_stop_in_dist({}, {}) would normally recommend {} and take {} to finish", + " accel_to_stop_in_dist({}, {}) would normally recommend {} and take {} to finish", speed, dist, normal_case, required_time ); }