diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index 3190ed03d..b337ccd9b 100644 --- a/lib/base/io-engine.cpp +++ b/lib/base/io-engine.cpp @@ -34,6 +34,31 @@ CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc) } } +/** + * Measures how long it takes to acquire a slot. + * + * @param yc Forwarded to the regular constructor. + * @param took Set to the time it took to acquire the slot. + */ +CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took) + : CpuBoundWork(std::move(yc), Clock::now(), took) +{ +} + +/** + * An internal helper layer between the regular constructor and the one that measures how long it takes. + * This is necessary to get the start time before the regular constructor is called. + * + * @param yc Forwarded to the regular constructor. + * @param started The current time. + * @param took Set to the time it took to acquire the slot. + */ +CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took) + : CpuBoundWork(std::move(yc)) +{ + took = Clock::now() - started; +} + CpuBoundWork::~CpuBoundWork() { if (!m_Done) { diff --git a/lib/base/io-engine.hpp b/lib/base/io-engine.hpp index 64831ff8c..dc46ab631 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -10,6 +10,7 @@ #include "base/logger.hpp" #include "base/shared.hpp" #include +#include #include #include #include @@ -36,8 +37,14 @@ namespace icinga */ class CpuBoundWork { +private: + using Clock = std::chrono::steady_clock; + + CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took); + public: CpuBoundWork(boost::asio::yield_context yc); + CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took); CpuBoundWork(const CpuBoundWork&) = delete; CpuBoundWork(CpuBoundWork&&) = delete; CpuBoundWork& operator=(const CpuBoundWork&) = delete;