Skip to content

Commit 76503f6

Browse files
committed
Allow benchmarking of CpuBoundWork#CpuBoundWork() via additional out-param
1 parent a65f2d6 commit 76503f6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/base/io-engine.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc)
3434
}
3535
}
3636

37+
/**
38+
* Measures how long it takes to acquire a slot.
39+
*
40+
* @param yc Forwarded to the regular constructor.
41+
* @param took Set to the time it took to acquire the slot.
42+
*/
43+
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took)
44+
: CpuBoundWork(std::move(yc), Clock::now(), took)
45+
{
46+
}
47+
48+
/**
49+
* An internal helper layer between the regular constructor and the one that measures how long it takes.
50+
* This is necessary to get the start time before the regular constructor is called.
51+
*
52+
* @param yc Forwarded to the regular constructor.
53+
* @param started The current time.
54+
* @param took Set to the time it took to acquire the slot.
55+
*/
56+
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took)
57+
: CpuBoundWork(std::move(yc))
58+
{
59+
took = Clock::now() - started;
60+
}
61+
3762
CpuBoundWork::~CpuBoundWork()
3863
{
3964
if (!m_Done) {

lib/base/io-engine.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "base/logger.hpp"
1111
#include "base/shared.hpp"
1212
#include <atomic>
13+
#include <chrono>
1314
#include <exception>
1415
#include <memory>
1516
#include <thread>
@@ -36,8 +37,14 @@ namespace icinga
3637
*/
3738
class CpuBoundWork
3839
{
40+
private:
41+
using Clock = std::chrono::steady_clock;
42+
43+
CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took);
44+
3945
public:
4046
CpuBoundWork(boost::asio::yield_context yc);
47+
CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took);
4148
CpuBoundWork(const CpuBoundWork&) = delete;
4249
CpuBoundWork(CpuBoundWork&&) = delete;
4350
CpuBoundWork& operator=(const CpuBoundWork&) = delete;

0 commit comments

Comments
 (0)