-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add TimerImpl::enableHRTimer - take two #9229
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
7a427c6
6bb6555
46cd973
62e53d4
384dfee
0c57cfc
911bac6
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 |
|---|---|---|
|
|
@@ -16,7 +16,34 @@ namespace Event { | |
| */ | ||
| class TimerUtils { | ||
| public: | ||
| static void millisecondsToTimeval(const std::chrono::milliseconds& d, timeval& tv); | ||
| /** | ||
| * Intended for consumption by enable(HR)Timer, this method is templated method to avoid implicit | ||
| * duration conversions for its input arguments. This lets us have an opportunity to check bounds | ||
| * before doing any conversions. When the passed in duration exceeds INT32_MAX max seconds, the | ||
| * output will be clipped to yield INT32_MAX seconds and 0 microseconds for the | ||
| * output argument. We clip to INT32_MAX to guard against overflowing the timeval structure. | ||
| * Throws an EnvoyException on negative duration input. | ||
| * @tparam Duration std::chrono duration type, e.g. seconds, milliseconds, ... | ||
| * @param d duration value | ||
| * @param tv output parameter that will be updated | ||
| */ | ||
| template <typename Duration> static void durationToTimeval(const Duration& d, timeval& tv) { | ||
| if (d.count() < 0) { | ||
|
Member
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. Additional thought: one thing that might be worth attempting here, is to typedef the duration args to using TimerDuration = std::chrono::duration<uint64_t, std::milli>;Doing that would void the need for this sanity check here, but would also bloat this PR a lot as call sites would have to be audited / updated.
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. Not sure if it is a big win. Perhaps this should be an ASSERT? Or treat negative duration as 0? ASSERT may however slow down fuzzers considerably if they go to negative duration. So probably would an exception here. |
||
| throw EnvoyException( | ||
| fmt::format("Negative duration passed to durationToTimeval(): {}", d.count())); | ||
| }; | ||
| constexpr int64_t clip_to = INT32_MAX; // 136.102208 years | ||
|
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. I certainly think this is more than enough. Out of curiosity though, why 32 bits? I think timeval uses time_t for seconds, which is 64 bit? |
||
| auto secs = std::chrono::duration_cast<std::chrono::seconds>(d); | ||
| if (secs.count() > clip_to) { | ||
| tv.tv_sec = clip_to; | ||
| tv.tv_usec = 0; | ||
| return; | ||
| } | ||
|
|
||
| auto usecs = std::chrono::duration_cast<std::chrono::microseconds>(d - secs); | ||
| tv.tv_sec = secs.count(); | ||
| tv.tv_usec = usecs.count(); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -28,10 +55,15 @@ class TimerImpl : public Timer, ImplBase { | |
|
|
||
| // Timer | ||
| void disableTimer() override; | ||
|
|
||
| void enableTimer(const std::chrono::milliseconds& d, const ScopeTrackedObject* scope) override; | ||
| void enableHRTimer(const std::chrono::microseconds& us, | ||
| const ScopeTrackedObject* object) override; | ||
|
|
||
| bool enabled() override; | ||
|
|
||
| private: | ||
| void internalEnableTimer(const timeval& tv, const ScopeTrackedObject* scope); | ||
| TimerCb cb_; | ||
| Dispatcher& dispatcher_; | ||
| // This has to be atomic for alarms which are handled out of thread, for | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -994,6 +994,7 @@ thru | |
| timespan | ||
| timestamp | ||
| timestamps | ||
| timeval | ||
| tm | ||
| tmp | ||
| tmpfile | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.