Commit 3b62632c authored by Chad Austin's avatar Chad Austin Committed by Facebook Github Bot

poll more efficiently in Subprocess::waitTimeout

Summary:
Unconditionally sleeping for 100ms in the wait polling loop has high average latency. If a process exits just after the initial wait() call, it's better to poll again sooner.

Start with a 2ms sleep and double it every sleep, capping out at 100ms.

Reviewed By: yfeldblum, mzhaom

Differential Revision: D17769851

fbshipit-source-id: efec8fdb8c2199b8db29b46b8c43fdaac56f69a8
parent 16d41c4b
......@@ -671,6 +671,9 @@ ProcessReturnCode Subprocess::waitTimeout(TimeoutDuration timeout) {
DCHECK_GT(pid_, 0) << "The subprocess has been waited already";
auto pollUntil = std::chrono::steady_clock::now() + timeout;
auto sleepDuration = std::chrono::milliseconds{2};
constexpr auto maximumSleepDuration = std::chrono::milliseconds{100};
for (;;) {
// Always call waitpid once after the full timeout has elapsed.
auto now = std::chrono::steady_clock::now();
......@@ -694,8 +697,10 @@ ProcessReturnCode Subprocess::waitTimeout(TimeoutDuration timeout) {
// Timed out: still running().
return returnCode_;
}
// The subprocess is still running, sleep for 100ms
std::this_thread::sleep_for(std::chrono::milliseconds{100});
// The subprocess is still running, sleep for increasing periods of time.
std::this_thread::sleep_for(sleepDuration);
sleepDuration =
std::min(maximumSleepDuration, sleepDuration + sleepDuration);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment