Converted mock time over to using monotonic_clock
Change-Id: I17850e25586be9bf732a0ca3a4e0b1a76146a539
diff --git a/aos/common/controls/control_loop_test.cc b/aos/common/controls/control_loop_test.cc
index 44af93a..b71aa3a 100644
--- a/aos/common/controls/control_loop_test.cc
+++ b/aos/common/controls/control_loop_test.cc
@@ -5,14 +5,14 @@
namespace aos {
namespace testing {
-constexpr ::aos::time::Time ControlLoopTest::kTimeTick;
-constexpr ::aos::time::Time ControlLoopTest::kDSPacketTime;
+constexpr ::std::chrono::milliseconds ControlLoopTest::kTimeTick;
+constexpr ::std::chrono::milliseconds ControlLoopTest::kDSPacketTime;
ControlLoopTest::ControlLoopTest() {
::aos::joystick_state.Clear();
::aos::robot_state.Clear();
- ::aos::time::Time::EnableMockTime(current_time_);
+ ::aos::time::EnableMockTime(current_time_);
SendMessages(false);
}
@@ -21,11 +21,11 @@
::aos::joystick_state.Clear();
::aos::robot_state.Clear();
- ::aos::time::Time::DisableMockTime();
+ ::aos::time::DisableMockTime();
}
void ControlLoopTest::SendMessages(bool enabled) {
- if (current_time_ - last_ds_time_ >= kDSPacketTime ||
+ if (current_time_ >= kDSPacketTime + last_ds_time_ ||
last_enabled_ != enabled) {
last_ds_time_ = current_time_;
auto new_state = ::aos::joystick_state.MakeMessage();
diff --git a/aos/common/controls/control_loop_test.h b/aos/common/controls/control_loop_test.h
index 14fdd60..88ccfa5 100644
--- a/aos/common/controls/control_loop_test.h
+++ b/aos/common/controls/control_loop_test.h
@@ -25,9 +25,7 @@
// Sends out all of the required queue messages.
void SendMessages(bool enabled);
// Ticks time for a single control loop cycle.
- void TickTime() {
- ::aos::time::Time::SetMockTime(current_time_ += kTimeTick);
- }
+ void TickTime() { ::aos::time::SetMockTime(current_time_ += kTimeTick); }
// Simulates everything that happens during 1 loop time step.
void SimulateTimestep(bool enabled) {
@@ -47,16 +45,17 @@
}
private:
- static constexpr ::aos::time::Time kTimeTick = ::aos::time::Time::InUS(5000);
- static constexpr ::aos::time::Time kDSPacketTime =
- ::aos::time::Time::InMS(20);
+ static constexpr ::std::chrono::milliseconds kTimeTick{5};
+ static constexpr ::std::chrono::milliseconds kDSPacketTime{20};
uint16_t team_id_ = 971;
int32_t reader_pid_ = 1;
double battery_voltage_ = 12.4;
- ::aos::time::Time last_ds_time_ = ::aos::time::Time::InSeconds(0);
- ::aos::time::Time current_time_ = ::aos::time::Time::InSeconds(0);
+ ::aos::monotonic_clock::time_point last_ds_time_ =
+ ::aos::monotonic_clock::epoch();
+ ::aos::monotonic_clock::time_point current_time_ =
+ ::aos::monotonic_clock::epoch();
::aos::testing::TestSharedMemory my_shm_;
diff --git a/aos/common/time.cc b/aos/common/time.cc
index b2d3675..7cfc4bc 100644
--- a/aos/common/time.cc
+++ b/aos/common/time.cc
@@ -55,7 +55,7 @@
// Mutex to make time reads and writes thread safe.
Mutex time_mutex;
// Current time when time is mocked.
-Time current_mock_time(0, 0);
+monotonic_clock::time_point current_mock_time = monotonic_clock::epoch();
// TODO(aschuh): This doesn't include SleepFor and SleepUntil.
// TODO(aschuh): Create a clock source object and change the default?
@@ -85,22 +85,20 @@
const Time Time::kZero{0, 0};
-void Time::EnableMockTime(const Time &now) {
+void EnableMockTime(monotonic_clock::time_point now) {
MutexLocker time_mutex_locker(&time_mutex);
mock_time_enabled = true;
current_mock_time = now;
}
-void Time::UpdateMockTime() {
- SetMockTime(NowImpl(kDefaultClock));
-}
+void UpdateMockTime() { SetMockTime(monotonic_clock::now()); }
-void Time::DisableMockTime() {
+void DisableMockTime() {
MutexLocker time_mutex_locker(&time_mutex);
mock_time_enabled = false;
}
-void Time::SetMockTime(const Time &now) {
+void SetMockTime(monotonic_clock::time_point now) {
MutexLocker time_mutex_locker(&time_mutex);
if (__builtin_expect(!mock_time_enabled, 0)) {
LOG(FATAL, "Tried to set mock time and mock time is not enabled\n");
@@ -108,17 +106,19 @@
current_mock_time = now;
}
-void Time::IncrementMockTime(const Time &amount) {
+void IncrementMockTime(monotonic_clock::duration amount) {
static ::aos::Mutex mutex;
::aos::MutexLocker sync(&mutex);
- SetMockTime(Now() + amount);
+ SetMockTime(monotonic_clock::now() + amount);
}
Time Time::Now(clockid_t clock) {
{
if (mock_time_enabled.load(::std::memory_order_relaxed)) {
MutexLocker time_mutex_locker(&time_mutex);
- return current_mock_time;
+ return Time::InNS(
+ ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
+ current_mock_time.time_since_epoch()).count());
}
}
return NowImpl(clock);
@@ -278,8 +278,7 @@
{
if (time::mock_time_enabled.load(::std::memory_order_relaxed)) {
MutexLocker time_mutex_locker(&time::time_mutex);
- return monotonic_clock::time_point(
- ::std::chrono::nanoseconds(time::current_mock_time.ToNSec()));
+ return time::current_mock_time;
}
}
diff --git a/aos/common/time.h b/aos/common/time.h
index 8f57916..ab64916 100644
--- a/aos/common/time.h
+++ b/aos/common/time.h
@@ -39,6 +39,19 @@
namespace time {
+// Enables returning the mock time value for Now instead of checking the system
+// clock.
+void EnableMockTime(monotonic_clock::time_point now);
+// Calls SetMockTime with the current actual time.
+void UpdateMockTime();
+// Sets now when time is being mocked.
+void SetMockTime(monotonic_clock::time_point now);
+// Convenience function to just increment the mock time by a certain amount in
+// a thread safe way.
+void IncrementMockTime(monotonic_clock::duration amount);
+// Disables mocking time.
+void DisableMockTime();
+
// A nice structure for representing times.
// 0 <= nsec_ < kNSecInSec should always be true. All functions here will make
// sure that that is true if it was on all inputs (including *this).
@@ -243,19 +256,6 @@
return Time(-sec_ - 1, kNSecInSec - nsec_);
}
- // Enables returning the mock time value for Now instead of checking the
- // system clock.
- static void EnableMockTime(const Time &now = Now());
- // Calls SetMockTime with the current actual time.
- static void UpdateMockTime();
- // Sets now when time is being mocked.
- static void SetMockTime(const Time &now);
- // Convenience function to just increment the mock time by a certain amount in
- // a thread safe way.
- static void IncrementMockTime(const Time &amount);
- // Disables mocking time.
- static void DisableMockTime();
-
private:
int32_t sec_, nsec_;
@@ -292,12 +292,8 @@
// syscalls to find the real time).
class TimeFreezer {
public:
- TimeFreezer() {
- Time::EnableMockTime();
- }
- ~TimeFreezer() {
- Time::DisableMockTime();
- }
+ TimeFreezer() { EnableMockTime(monotonic_clock::now()); }
+ ~TimeFreezer() { DisableMockTime(); }
private:
DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
diff --git a/y2014/control_loops/shooter/shooter_lib_test.cc b/y2014/control_loops/shooter/shooter_lib_test.cc
index 277352e..e1efc1c 100644
--- a/y2014/control_loops/shooter/shooter_lib_test.cc
+++ b/y2014/control_loops/shooter/shooter_lib_test.cc
@@ -1,5 +1,6 @@
#include <unistd.h>
+#include <chrono>
#include <memory>
#include "gtest/gtest.h"
@@ -12,6 +13,8 @@
using ::aos::time::Time;
+namespace chrono = ::std::chrono;
+
namespace y2014 {
namespace control_loops {
namespace testing {
@@ -263,7 +266,7 @@
GetAbsolutePosition());
last_voltage_ = shooter_queue_.output->voltage;
- ::aos::time::Time::IncrementMockTime(::aos::time::Time::InMS(10.0));
+ ::aos::time::IncrementMockTime(chrono::milliseconds(10));
}
// pointer to plant