Description
Currently, console-subscriber
records every timestamp by calling SystemTime::now()
. This is not necessarily the most efficient way to record a timestamp; every SystemTime::now()
call requires a system call.
Also, perhaps more importantly, I don't think using system timestamps is really the most correct approach. The main reason the console records timestamps at all is to calculate durations. However, the system clock can go backwards; it's not guaranteed to be monotonic. This means that durations calculated using SystemTime
might be incorrect. We should really be using monotonic time, such as Instant
s.
I believe the main reason console-subscriber
doesn't currently use monotonic timestamps is that it sends timestamps on the wire using prost_types::Timestamp
, which has a From<SystemTime>
impl. Since Instant
s are opaque, there's no From<Instant>
implementation for protobuf timestamps.
One solution might be to switch to recording timestamps using Instant
s, but anchor them against a fixed SystemTime
, so that they can be converted into SystemTime
s. The way we would do this is by recording a fixed SystemTime
and Instant
pair when the ConsoleLayer
is created. Then, when we need to serialize an Instant
timestamp, we would take the duration elapsed between that Instant
and the "anchor" instant. Since the anchor Instant
and anchor SystemTime
represent (nearly) the same timestamp, we could then convert the Instant
timestamp into a SystemTime
for serialization, by adding the elapsed duration from the anchor Instant
to the anchor SystemTime
.
Another option we might want to consider is using the quanta
crate by @tobz. This would offer a few advantages. In particular, quanta::Instant
does provide an Into<prost_types::Timestamp>
implementation. Additionally, quanta::Instant
s can be converted into u64
s (unlike std's instants). This would allow them to be stored in atomics, which could be beneficial for #238. Finally, quanta
may introduce less overhead compared std::time::Instant
, at least on x86 platforms, where it uses rdtsc