From 687fbd24137fafc0fb08a7fd76420501c1880dca Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 20 Sep 2024 10:17:33 +0200 Subject: [PATCH] Allow benchmarking of CpuBoundWork#CpuBoundWork() via additional out-param --- lib/base/io-engine.cpp | 25 +++++++++++++++++++++++++ lib/base/io-engine.hpp | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index 26125feb310..914ae8dea9a 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 326f04fdc47..194a3869814 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -8,6 +8,7 @@ #include "base/logger.hpp" #include "base/shared-object.hpp" #include +#include #include #include #include @@ -29,8 +30,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;