Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions source/extensions/quic_listeners/quiche/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "envoy_quic_clock_lib",
srcs = ["envoy_quic_clock.cc"],
hdrs = ["envoy_quic_clock.h"],
visibility = ["//visibility:public"],
deps = [
"//include/envoy/event:timer_interface",
"@com_googlesource_quiche//:quic_platform",
],
)

envoy_cc_library(
name = "quiche_common_platform_impl_lib",
hdrs = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "extensions/quic_listeners/quiche/platform/envoy_quic_clock.h"

namespace Envoy {
namespace Quic {

quic::QuicTime EnvoyQuicClock::ApproximateNow() const {
// This might be expensive as Dispatcher doesn't store approximate time_point.
return Now();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worst case we can have a thread local alarm which latches time once per dispatcher loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LibeventScheduler::onPrepare() is called each libevent loop. But there are some plumbing to pass the timestamp get from onPrepare() to TimeSystem. But it's doable, and can be an future improvement.

}

quic::QuicTime EnvoyQuicClock::Now() const {
return quic::QuicTime::Zero() + quic::QuicTime::Delta::FromMicroseconds(
microsecondsSinceEpoch(time_system_.monotonicTime()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which line?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it looked like there is an empty line between line 12 and 13, but it's not.

}

quic::QuicWallTime EnvoyQuicClock::WallNow() const {
return quic::QuicWallTime::FromUNIXMicroseconds(
microsecondsSinceEpoch(time_system_.systemTime()));
}

} // namespace Quic
} // namespace Envoy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <chrono>

#include "envoy/event/timer.h"

#include "quiche/quic/platform/api/quic_clock.h"

namespace Envoy {
namespace Quic {

class EnvoyQuicClock : public quic::QuicClock {
public:
EnvoyQuicClock(Event::TimeSystem& time_system) : time_system_(time_system) {}

// quic::QuicClock
quic::QuicTime ApproximateNow() const override;
quic::QuicTime Now() const override;
quic::QuicWallTime WallNow() const override;

private:
template <typename T> int64_t microsecondsSinceEpoch(std::chrono::time_point<T> time) const {
return std::chrono::duration_cast<std::chrono::microseconds>(time.time_since_epoch()).count();
}

Event::TimeSystem& time_system_;
};

} // namespace Quic
} // namespace Envoy
10 changes: 10 additions & 0 deletions test/extensions/quic_listeners/quiche/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,13 @@ envoy_cc_test_library(
":quic_platform_expect_bug_impl_lib",
],
)

envoy_cc_test(
name = "envoy_quic_clock_test",
srcs = ["envoy_quic_clock_test.cc"],
deps = [
"//source/extensions/quic_listeners/quiche/platform:envoy_quic_clock_lib",
"//test/test_common:simulated_time_system_lib",
"//test/test_common:test_time_lib",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <memory>

#include "extensions/quic_listeners/quiche/platform/envoy_quic_clock.h"

#include "test/test_common/simulated_time_system.h"
#include "test/test_common/test_time.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace Envoy {
namespace Quic {

TEST(EnvoyQuicClockTest, TestNow) {
Event::SimulatedTimeSystemHelper time_system;
EnvoyQuicClock clock(time_system);
uint64_t mono_time = std::chrono::duration_cast<std::chrono::microseconds>(
time_system.monotonicTime().time_since_epoch())
.count();
uint64_t sys_time = std::chrono::duration_cast<std::chrono::microseconds>(
time_system.systemTime().time_since_epoch())
.count();
// Advance time by 1000000us.
time_system.sleep(std::chrono::microseconds(1000000));
EXPECT_EQ(mono_time + 1000000, (clock.Now() - quic::QuicTime::Zero()).ToMicroseconds());
EXPECT_EQ(sys_time + 1000000, clock.WallNow().ToUNIXMicroseconds());

// Advance time by 10us.
time_system.sleep(std::chrono::microseconds(10));
EXPECT_EQ(mono_time + 1000000 + 10, (clock.Now() - quic::QuicTime::Zero()).ToMicroseconds());
EXPECT_EQ(sys_time + 1000000 + 10, clock.WallNow().ToUNIXMicroseconds());
EXPECT_EQ(clock.ApproximateNow(), clock.Now());

// Advance time by 2ms.
time_system.sleep(std::chrono::milliseconds(2));
EXPECT_EQ(mono_time + 1000000 + 10 + 2 * 1000,
(clock.Now() - quic::QuicTime::Zero()).ToMicroseconds());
EXPECT_EQ(sys_time + 1000000 + 10 + 2 * 1000, clock.WallNow().ToUNIXMicroseconds());
}

// Tests that Now() should never go back.
TEST(EnvoyQuicClockTest, TestMonotonicityWithReadTimeSystem) {
Event::TestRealTimeSystem time_system;
EnvoyQuicClock clock(time_system);
quic::QuicTime last_now = clock.Now();
for (int i = 0; i < 1000; ++i) {
quic::QuicTime now = clock.Now();
ASSERT_LE(last_now, now);
last_now = now;
}
}

} // namespace Quic
} // namespace Envoy