userver: userver/utils/statistics/busy.hpp Source File
Loading...
Searching...
No Matches
busy.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/busy.hpp
4/// @brief @copybrief utils::statistics::BusyMarker
5
6#include <memory>
7
8#include <userver/utils/statistics/recentperiod.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils::statistics {
13
14/// Measure how much time we've spent in work recently in percents that
15/// supports recirsive starts. Use
16/// utils::statistics::BusyMarker for RAII time measures.
17///
18/// @snippet utils/statistics/busy_test.cpp busy sample
19class BusyStorage final {
20public:
21 using Duration = std::chrono::steady_clock::duration;
22
23 BusyStorage(Duration epoch_duration, Duration history_period);
24
25 ~BusyStorage();
26
27 /// Safe to read concurrently with calling StartWork() and StopWork()
28 double GetCurrentLoad() const;
29
30 /// Starts the time measure, if it was not already started
31 void StartWork();
32
33 /// Stops the time measure if the count of StopWork() invocations matches the
34 /// StartWork() invocations count.
35 void StopWork() noexcept;
36
37 /// Returns true if the time measure is active
38 bool IsAlreadyStarted() const noexcept;
39
40private:
41 Duration GetNotCommittedLoad() const noexcept;
42
43 struct Impl;
44 std::unique_ptr<Impl> pimpl;
45};
46
47/// @brief A RAII-style guard to account code block execution time in
48/// utils::statistics::BusyStorage. Aware of recursive invocations in the same
49/// thread.
50///
51/// @snippet utils/statistics/busy_test.cpp busy sample
52class BusyMarker final {
53public:
54 BusyMarker(BusyStorage& storage) : storage_(storage) { storage_.StartWork(); }
55
56 BusyMarker(const BusyMarker&) = delete;
57 BusyMarker& operator=(const BusyMarker&) = delete;
58
59 ~BusyMarker() { storage_.StopWork(); }
60
61private:
62 BusyStorage& storage_;
63};
64
65} // namespace utils::statistics
66
67USERVER_NAMESPACE_END