aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/duration_counter.cpp
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2023-03-31 10:54:08 +0300
committerDaniil Cherednik <dan.cherednik@gmail.com>2023-03-31 12:28:07 +0300
commitfc1cffcfa7f0497a1f97b384a24bcbf23362f3be (patch)
treec15f7ab5b9e9b20fd0ef8fc07d598d28e8b32004 /library/cpp/unified_agent_client/duration_counter.cpp
parent8a749596d40e91c896a1907afcd108d9221fbde1 (diff)
downloadydb-e9cbe5c5cf67db853d223fd365c9f05b695f7b96.tar.gz
Ydb stable 23-1-1923.1.19
x-stable-origin-commit: c5d5a396e89d0a72e0267a55e93d8404d4fb54fe
Diffstat (limited to 'library/cpp/unified_agent_client/duration_counter.cpp')
-rw-r--r--library/cpp/unified_agent_client/duration_counter.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/duration_counter.cpp b/library/cpp/unified_agent_client/duration_counter.cpp
new file mode 100644
index 0000000000..118778a226
--- /dev/null
+++ b/library/cpp/unified_agent_client/duration_counter.cpp
@@ -0,0 +1,41 @@
+#include "duration_counter.h"
+
+namespace NUnifiedAgent {
+ using namespace NMonitoring;
+
+ TDurationUsCounter::TDurationUsCounter(const TString& name, TDynamicCounters& owner)
+ : Counter(*owner.GetCounter(name, true))
+ , ActiveTimers()
+ , Lock()
+ {
+ }
+
+ NHPTimer::STime* TDurationUsCounter::Begin() {
+ with_lock (Lock) {
+ ActiveTimers.push_back(0);
+ auto& result = ActiveTimers.back();
+ NHPTimer::GetTime(&result);
+ return &result;
+ }
+ }
+
+ void TDurationUsCounter::End(NHPTimer::STime* startTime) {
+ with_lock (Lock) {
+ Counter += static_cast<ui64>(NHPTimer::GetTimePassed(startTime) * 1000000);
+ *startTime = 0;
+ while (!ActiveTimers.empty() && ActiveTimers.front() == 0) {
+ ActiveTimers.pop_front();
+ }
+ }
+ }
+
+ void TDurationUsCounter::Update() {
+ with_lock (Lock) {
+ for (auto& startTime : ActiveTimers) {
+ if (startTime != 0) {
+ Counter += static_cast<ui64>(NHPTimer::GetTimePassed(&startTime) * 1000000);
+ }
+ }
+ }
+ }
+}