-
Notifications
You must be signed in to change notification settings - Fork 5.3k
quiche: implement QuicClock interface #7028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2680577
05f8011
17802f8
d6af84a
b33d2ac
4d5fccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
|
|
||
| quic::QuicTime EnvoyQuicClock::Now() const { | ||
| return quic::QuicTime::Zero() + quic::QuicTime::Delta::FromMicroseconds( | ||
| microsecondsSinceEpoch(time_system_.monotonicTime())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove empty line.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which line?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.