Upgraded the rest of Time.
Change-Id: I0ee083837e51d8f74a798b7ba14a3b6bb3859f35
diff --git a/aos/common/logging/binary_log_writer.cc b/aos/common/logging/binary_log_writer.cc
index 10641fb..076d72d 100644
--- a/aos/common/logging/binary_log_writer.cc
+++ b/aos/common/logging/binary_log_writer.cc
@@ -1,27 +1,28 @@
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <mntent.h>
+#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
-#include <errno.h>
-#include <time.h>
#include <string.h>
-#include <string>
-#include <unistd.h>
#include <sys/types.h>
-#include <pwd.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <mntent.h>
+#include <time.h>
+#include <unistd.h>
+#include <string>
+#include <chrono>
#include <map>
#include <unordered_set>
-#include "aos/common/logging/implementations.h"
-#include "aos/common/logging/binary_log_file.h"
-#include "aos/linux_code/init.h"
-#include "aos/linux_code/configuration.h"
-#include "aos/linux_code/ipc_lib/queue.h"
-#include "aos/common/queue_types.h"
#include "aos/common/die.h"
+#include "aos/common/logging/binary_log_file.h"
+#include "aos/common/logging/implementations.h"
+#include "aos/common/queue_types.h"
#include "aos/common/time.h"
+#include "aos/linux_code/configuration.h"
+#include "aos/linux_code/init.h"
+#include "aos/linux_code/ipc_lib/queue.h"
namespace aos {
namespace logging {
@@ -213,8 +214,7 @@
// again so the queue can buffer up some logs. This avoids lots of context
// switches and mutex contention which happens if we're constantly reading
// new messages as they come in.
- static constexpr auto kSleepTime = ::aos::time::Time::InSeconds(0.1);
- ::aos::time::SleepFor(kSleepTime);
+ ::std::this_thread::sleep_for(::std::chrono::milliseconds(100));
continue;
}
diff --git a/aos/common/logging/implementations.cc b/aos/common/logging/implementations.cc
index 2f17f7a..697dec9 100644
--- a/aos/common/logging/implementations.cc
+++ b/aos/common/logging/implementations.cc
@@ -4,6 +4,7 @@
#include <inttypes.h>
#include <algorithm>
+#include <chrono>
#include "aos/common/die.h"
#include "aos/common/once.h"
@@ -16,6 +17,8 @@
namespace logging {
namespace {
+namespace chrono = ::std::chrono;
+
// The root LogImplementation. It only logs to stderr/stdout.
// Some of the things specified in the LogImplementation documentation doesn't
// apply here (mostly the parts about being able to use LOG) because this is the
@@ -84,9 +87,14 @@
memcpy(message->name, context->name, context->name_size);
message->name_length = context->name_size;
- time::Time now = time::Time::Now();
- message->seconds = now.sec();
- message->nseconds = now.nsec();
+ monotonic_clock::time_point monotonic_now = monotonic_clock::now();
+ message->seconds =
+ chrono::duration_cast<chrono::seconds>(monotonic_now.time_since_epoch())
+ .count();
+ message->nseconds =
+ chrono::duration_cast<chrono::nanoseconds>(
+ monotonic_now.time_since_epoch() - chrono::seconds(message->seconds))
+ .count();
message->sequence = context->sequence++;
}
@@ -325,9 +333,9 @@
RawQueue *queue = NULL;
int dropped_messages = 0;
-::aos::time::Time dropped_start, backoff_start;
+monotonic_clock::time_point dropped_start, backoff_start;
// Wait this long after dropping a message before even trying to write any more.
-constexpr ::aos::time::Time kDropBackoff = ::aos::time::Time::InSeconds(0.1);
+constexpr chrono::milliseconds kDropBackoff = chrono::milliseconds(100);
LogMessage *GetMessageOrDie() {
LogMessage *message = static_cast<LogMessage *>(queue->GetMessage());
@@ -340,8 +348,8 @@
void Write(LogMessage *msg) {
if (__builtin_expect(dropped_messages > 0, false)) {
- ::aos::time::Time message_time =
- ::aos::time::Time(msg->seconds, msg->nseconds);
+ monotonic_clock::time_point message_time(
+ chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds));
if (message_time - backoff_start < kDropBackoff) {
++dropped_messages;
queue->FreeMessage(msg);
@@ -349,10 +357,16 @@
}
LogMessage *dropped_message = GetMessageOrDie();
+ chrono::seconds dropped_start_sec = chrono::duration_cast<chrono::seconds>(
+ dropped_start.time_since_epoch());
+ chrono::nanoseconds dropped_start_nsec =
+ chrono::duration_cast<chrono::nanoseconds>(
+ dropped_start.time_since_epoch() - dropped_start_sec);
internal::FillInMessageVarargs(
ERROR, dropped_message,
"%d logs starting at %" PRId32 ".%" PRId32 " dropped\n",
- dropped_messages, dropped_start.sec(), dropped_start.nsec());
+ dropped_messages, static_cast<int32_t>(dropped_start_sec.count()),
+ static_cast<int32_t>(dropped_start_nsec.count()));
if (queue->WriteMessage(dropped_message, RawQueue::kNonBlock)) {
dropped_messages = 0;
} else {
@@ -367,8 +381,9 @@
}
if (!queue->WriteMessage(msg, RawQueue::kNonBlock)) {
if (dropped_messages == 0) {
- dropped_start = backoff_start =
- ::aos::time::Time(msg->seconds, msg->nseconds);
+ monotonic_clock::time_point message_time(
+ chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds));
+ dropped_start = backoff_start = message_time;
}
++dropped_messages;
}
diff --git a/aos/common/logging/implementations_test.cc b/aos/common/logging/implementations_test.cc
index bbee5b7..aade0d1 100644
--- a/aos/common/logging/implementations_test.cc
+++ b/aos/common/logging/implementations_test.cc
@@ -1,7 +1,8 @@
-#include <string>
-
#include <inttypes.h>
+#include <chrono>
+#include <string>
+
#include "gtest/gtest.h"
#include "aos/common/logging/implementations.h"
@@ -16,6 +17,8 @@
namespace logging {
namespace testing {
+namespace chrono = ::std::chrono;
+
class TestLogImplementation : public SimpleLogImplementation {
__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
void DoLog(log_level level, const char *format, va_list ap) override {
@@ -168,23 +171,25 @@
//static const long kTimingCycles = 5000000;
static const long kTimingCycles = 5000;
- time::Time start = time::Time::Now();
+ monotonic_clock::time_point start = monotonic_clock::now();
for (long i = 0; i < kTimingCycles; ++i) {
LOG(INFO, "a\n");
}
- time::Time end = time::Time::Now();
- time::Time diff = end - start;
+ monotonic_clock::time_point end = monotonic_clock::now();
+ auto diff = end - start;
printf("short message took %" PRId64 " nsec for %ld\n",
- diff.ToNSec(), kTimingCycles);
+ chrono::duration_cast<chrono::nanoseconds>(diff).count(),
+ kTimingCycles);
- start = time::Time::Now();
+ start = monotonic_clock::now();
for (long i = 0; i < kTimingCycles; ++i) {
LOG(INFO, "something longer than just \"a\" to log to test timing\n");
}
- end = time::Time::Now();
+ end = monotonic_clock::now();
diff = end - start;
printf("long message took %" PRId64 " nsec for %ld\n",
- diff.ToNSec(), kTimingCycles);
+ chrono::duration_cast<chrono::nanoseconds>(diff).count(),
+ kTimingCycles);
}
TEST(LoggingPrintFormatTest, Time) {
diff --git a/aos/common/logging/log_streamer.cc b/aos/common/logging/log_streamer.cc
index ab37e02..74e680b 100644
--- a/aos/common/logging/log_streamer.cc
+++ b/aos/common/logging/log_streamer.cc
@@ -1,40 +1,47 @@
-#include <stdio.h>
-#include <stdlib.h>
#include <errno.h>
-#include <time.h>
-#include <string.h>
-#include <string>
-#include <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+#include <chrono>
+#include <string>
+#include "aos/common/logging/implementations.h"
+#include "aos/common/logging/logging.h"
+#include "aos/common/time.h"
#include "aos/linux_code/init.h"
#include "aos/linux_code/ipc_lib/queue.h"
-#include "aos/common/logging/logging.h"
-#include "aos/common/logging/implementations.h"
-#include "aos/common/time.h"
namespace aos {
namespace logging {
namespace linux_code {
namespace {
+namespace chrono = ::std::chrono;
+
int LogStreamerMain() {
InitNRT();
RawQueue *queue = GetLoggingQueue();
- const time::Time now = time::Time::Now();
+ const monotonic_clock::time_point now = monotonic_clock::now();
+ chrono::seconds sec =
+ chrono::duration_cast<chrono::seconds>(now.time_since_epoch());
+ chrono::nanoseconds nsec =
+ chrono::duration_cast<chrono::nanoseconds>(now.time_since_epoch() - sec);
printf("starting at %" PRId32 "s%" PRId32 "ns-----------------------------\n",
- now.sec(), now.nsec());
+ static_cast<int32_t>(sec.count()), static_cast<int32_t>(nsec.count()));
while (true) {
const LogMessage *const msg = static_cast<const LogMessage *>(
queue->ReadMessage(RawQueue::kNonBlock));
if (msg == NULL) {
- ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
+ ::std::this_thread::sleep_for(::std::chrono::milliseconds(100));
} else {
internal::PrintMessage(stdout, *msg);
diff --git a/aos/common/logging/replay.cc b/aos/common/logging/replay.cc
index 7180c96..6b55e51 100644
--- a/aos/common/logging/replay.cc
+++ b/aos/common/logging/replay.cc
@@ -1,9 +1,13 @@
#include "aos/common/logging/replay.h"
+#include <chrono>
+
namespace aos {
namespace logging {
namespace linux_code {
+namespace chrono = ::std::chrono;
+
bool LogReplayer::ProcessMessage() {
const LogFileMessageHeader *message = reader_->ReadNextMessage(false);
if (message == nullptr) return true;
@@ -32,8 +36,9 @@
if (handler == handlers_.end()) return false;
handler->second->HandleStruct(
- ::aos::time::Time(message->time_sec, message->time_nsec), type_id,
- position,
+ monotonic_clock::time_point(chrono::seconds(message->time_sec) +
+ chrono::nanoseconds(message->time_nsec)),
+ type_id, position,
message->message_size -
(sizeof(type_id) + sizeof(message_length) + message_length));
return false;
diff --git a/aos/common/logging/replay.h b/aos/common/logging/replay.h
index 38f7120..1a8418f 100644
--- a/aos/common/logging/replay.h
+++ b/aos/common/logging/replay.h
@@ -80,8 +80,9 @@
public:
virtual ~StructHandlerInterface() {}
- virtual void HandleStruct(::aos::time::Time log_time, uint32_t type_id,
- const void *data, size_t data_size) = 0;
+ virtual void HandleStruct(::aos::monotonic_clock::time_point log_time,
+ uint32_t type_id, const void *data,
+ size_t data_size) = 0;
};
// Converts struct log messages to a message type and passes it to an
@@ -92,8 +93,9 @@
TypedStructHandler(::std::function<void(const T &message)> handler)
: handler_(handler) {}
- void HandleStruct(::aos::time::Time log_time, uint32_t type_id,
- const void *data, size_t data_size) override {
+ void HandleStruct(::aos::monotonic_clock::time_point log_time,
+ uint32_t type_id, const void *data,
+ size_t data_size) override {
CHECK_EQ(type_id, T::GetType()->id);
T message;
CHECK_EQ(data_size, T::Size());