Skip to content

Commit 687fbd2

Browse files
committed
Allow benchmarking of CpuBoundWork#CpuBoundWork() via additional out-param
1 parent e678f09 commit 687fbd2

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
@@ -8,6 +8,7 @@
88
#include "base/logger.hpp"
99
#include "base/shared-object.hpp"
1010
#include <atomic>
11+
#include <chrono>
1112
#include <exception>
1213
#include <memory>
1314
#include <thread>
@@ -29,8 +30,14 @@ namespace icinga
2930
*/
3031
class CpuBoundWork
3132
{
33+
private:
34+
using Clock = std::chrono::steady_clock;
35+
36+
CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took);
37+
3238
public:
3339
CpuBoundWork(boost::asio::yield_context yc);
40+
CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took);
3441
CpuBoundWork(const CpuBoundWork&) = delete;
3542
CpuBoundWork(CpuBoundWork&&) = delete;
3643
CpuBoundWork& operator=(const CpuBoundWork&) = delete;

0 commit comments

Comments
 (0)