duration_counter.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "duration_counter.h"
  2. namespace NUnifiedAgent {
  3. using namespace NMonitoring;
  4. TDurationUsCounter::TDurationUsCounter(const TString& name, TDynamicCounters& owner)
  5. : Counter(*owner.GetCounter(name, true))
  6. , ActiveTimers()
  7. , Lock()
  8. {
  9. }
  10. NHPTimer::STime* TDurationUsCounter::Begin() {
  11. with_lock (Lock) {
  12. ActiveTimers.push_back(0);
  13. auto& result = ActiveTimers.back();
  14. NHPTimer::GetTime(&result);
  15. return &result;
  16. }
  17. }
  18. void TDurationUsCounter::End(NHPTimer::STime* startTime) {
  19. with_lock (Lock) {
  20. Counter += static_cast<ui64>(NHPTimer::GetTimePassed(startTime) * 1000000);
  21. *startTime = 0;
  22. while (!ActiveTimers.empty() && ActiveTimers.front() == 0) {
  23. ActiveTimers.pop_front();
  24. }
  25. }
  26. }
  27. void TDurationUsCounter::Update() {
  28. with_lock (Lock) {
  29. for (auto& startTime : ActiveTimers) {
  30. if (startTime != 0) {
  31. Counter += static_cast<ui64>(NHPTimer::GetTimePassed(&startTime) * 1000000);
  32. }
  33. }
  34. }
  35. }
  36. }