aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/duration_counter.cpp
blob: 118778a2268fa4acf345f7757d296347ffbc6529 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
                }
            }
        }
    }
}