Extract CPU measurer from DVQA

Bug: b/196229820
Change-Id: I1f8f21ea5864f9ba98365e4699572fabd8cb1ece
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/228560
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34741}
diff --git a/test/pc/e2e/BUILD.gn b/test/pc/e2e/BUILD.gn
index 355e65a..ff3b701 100644
--- a/test/pc/e2e/BUILD.gn
+++ b/test/pc/e2e/BUILD.gn
@@ -639,10 +639,8 @@
       "../../../rtc_base:criticalsection",
       "../../../rtc_base:logging",
       "../../../rtc_base:rtc_base_approved",
-      "../../../rtc_base:rtc_base_tests_utils",
       "../../../rtc_base:rtc_event",
       "../../../rtc_base:rtc_numerics",
-      "../../../rtc_base:timeutils",
       "../../../rtc_base/synchronization:mutex",
       "../../../rtc_tools:video_quality_analysis",
       "../../../system_wrappers",
@@ -657,6 +655,8 @@
 
     testonly = true
     sources = [
+      "analyzer/video/default_video_quality_analyzer_cpu_measurer.cc",
+      "analyzer/video/default_video_quality_analyzer_cpu_measurer.h",
       "analyzer/video/default_video_quality_analyzer_internal_shared_objects.cc",
       "analyzer/video/default_video_quality_analyzer_internal_shared_objects.h",
     ]
@@ -666,7 +666,10 @@
       "../../../api/numerics:numerics",
       "../../../api/units:timestamp",
       "../../../api/video:video_frame",
+      "../../../rtc_base:rtc_base_tests_utils",
       "../../../rtc_base:stringutils",
+      "../../../rtc_base:timeutils",
+      "../../../rtc_base/synchronization:mutex",
     ]
     absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
   }
diff --git a/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc b/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc
index fc8fea4..f3e2344 100644
--- a/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc
+++ b/test/pc/e2e/analyzer/video/default_video_quality_analyzer.cc
@@ -19,7 +19,6 @@
 #include "api/units/time_delta.h"
 #include "api/video/i420_buffer.h"
 #include "common_video/libyuv/include/webrtc_libyuv.h"
-#include "rtc_base/cpu_time.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/strings/string_builder.h"
@@ -132,7 +131,7 @@
     state_ = State::kActive;
     start_time_ = Now();
   }
-  StartMeasuringCpuProcessTime();
+  cpu_measurer_.StartMeasuringCpuProcessTime();
 }
 
 uint16_t DefaultVideoQualityAnalyzer::OnFrameCaptured(
@@ -557,7 +556,7 @@
     }
     state_ = State::kStopped;
   }
-  StopMeasuringCpuProcessTime();
+  cpu_measurer_.StopMeasuringCpuProcessTime();
   comparison_available_event_.Set();
   thread_pool_.clear();
 
@@ -663,7 +662,7 @@
     absl::optional<VideoFrame> rendered,
     bool dropped,
     FrameStats frame_stats) {
-  StartExcludingCpuThreadTime();
+  cpu_measurer_.StartExcludingCpuThreadTime();
   analyzer_stats_.comparisons_queue_size.AddSample(
       StatsSample(comparisons_.size(), Now()));
   // If there too many computations waiting in the queue, we won't provide
@@ -682,7 +681,7 @@
                               std::move(frame_stats), overload_reason);
   }
   comparison_available_event_.Set();
-  StopExcludingCpuThreadTime();
+  cpu_measurer_.StopExcludingCpuThreadTime();
 }
 
 void DefaultVideoQualityAnalyzer::ProcessComparisons() {
@@ -715,9 +714,9 @@
       continue;
     }
 
-    StartExcludingCpuThreadTime();
+    cpu_measurer_.StartExcludingCpuThreadTime();
     ProcessComparison(comparison.value());
-    StopExcludingCpuThreadTime();
+    cpu_measurer_.StopExcludingCpuThreadTime();
   }
 }
 
@@ -1001,31 +1000,8 @@
   return key.ToString();
 }
 
-void DefaultVideoQualityAnalyzer::StartMeasuringCpuProcessTime() {
-  MutexLock lock(&cpu_measurement_lock_);
-  cpu_time_ -= rtc::GetProcessCpuTimeNanos();
-  wallclock_time_ -= rtc::SystemTimeNanos();
-}
-
-void DefaultVideoQualityAnalyzer::StopMeasuringCpuProcessTime() {
-  MutexLock lock(&cpu_measurement_lock_);
-  cpu_time_ += rtc::GetProcessCpuTimeNanos();
-  wallclock_time_ += rtc::SystemTimeNanos();
-}
-
-void DefaultVideoQualityAnalyzer::StartExcludingCpuThreadTime() {
-  MutexLock lock(&cpu_measurement_lock_);
-  cpu_time_ += rtc::GetThreadCpuTimeNanos();
-}
-
-void DefaultVideoQualityAnalyzer::StopExcludingCpuThreadTime() {
-  MutexLock lock(&cpu_measurement_lock_);
-  cpu_time_ -= rtc::GetThreadCpuTimeNanos();
-}
-
 double DefaultVideoQualityAnalyzer::GetCpuUsagePercent() {
-  MutexLock lock(&cpu_measurement_lock_);
-  return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
+  return cpu_measurer_.GetCpuUsagePercent();
 }
 
 uint16_t DefaultVideoQualityAnalyzer::StreamState::PopFront(size_t peer) {
diff --git a/test/pc/e2e/analyzer/video/default_video_quality_analyzer.h b/test/pc/e2e/analyzer/video/default_video_quality_analyzer.h
index 6c7507b..d388b1b 100644
--- a/test/pc/e2e/analyzer/video/default_video_quality_analyzer.h
+++ b/test/pc/e2e/analyzer/video/default_video_quality_analyzer.h
@@ -29,6 +29,7 @@
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/synchronization/mutex.h"
 #include "system_wrappers/include/clock.h"
+#include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.h"
 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_internal_shared_objects.h"
 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_shared_objects.h"
 #include "test/pc/e2e/analyzer/video/multi_head_queue.h"
@@ -338,11 +339,6 @@
   std::string StatsKeyToMetricName(const StatsKey& key) const
       RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
 
-  void StartMeasuringCpuProcessTime();
-  void StopMeasuringCpuProcessTime();
-  void StartExcludingCpuThreadTime();
-  void StopExcludingCpuThreadTime();
-
   // TODO(titovartem) restore const when old constructor will be removed.
   DefaultVideoQualityAnalyzerOptions options_;
   webrtc::Clock* const clock_;
@@ -398,9 +394,7 @@
   std::vector<rtc::PlatformThread> thread_pool_;
   rtc::Event comparison_available_event_;
 
-  Mutex cpu_measurement_lock_;
-  int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_) = 0;
-  int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_) = 0;
+  DefaultVideoQualityAnalyzerCpuMeasurer cpu_measurer_;
 };
 
 }  // namespace webrtc_pc_e2e
diff --git a/test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.cc b/test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.cc
new file mode 100644
index 0000000..847c9f0
--- /dev/null
+++ b/test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.cc
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+#include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.h"
+
+#include "rtc_base/cpu_time.h"
+#include "rtc_base/synchronization/mutex.h"
+#include "rtc_base/system_time.h"
+
+namespace webrtc {
+
+void DefaultVideoQualityAnalyzerCpuMeasurer::StartMeasuringCpuProcessTime() {
+  MutexLock lock(&mutex_);
+  cpu_time_ -= rtc::GetProcessCpuTimeNanos();
+  wallclock_time_ -= rtc::SystemTimeNanos();
+}
+
+void DefaultVideoQualityAnalyzerCpuMeasurer::StopMeasuringCpuProcessTime() {
+  MutexLock lock(&mutex_);
+  cpu_time_ += rtc::GetProcessCpuTimeNanos();
+  wallclock_time_ += rtc::SystemTimeNanos();
+}
+
+void DefaultVideoQualityAnalyzerCpuMeasurer::StartExcludingCpuThreadTime() {
+  MutexLock lock(&mutex_);
+  cpu_time_ += rtc::GetThreadCpuTimeNanos();
+}
+
+void DefaultVideoQualityAnalyzerCpuMeasurer::StopExcludingCpuThreadTime() {
+  MutexLock lock(&mutex_);
+  cpu_time_ -= rtc::GetThreadCpuTimeNanos();
+}
+
+double DefaultVideoQualityAnalyzerCpuMeasurer::GetCpuUsagePercent() {
+  MutexLock lock(&mutex_);
+  return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
+}
+
+}  // namespace webrtc
diff --git a/test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.h b/test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.h
new file mode 100644
index 0000000..dd9fa07
--- /dev/null
+++ b/test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.h
@@ -0,0 +1,36 @@
+/*
+ *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_CPU_MEASURER_H_
+#define TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_CPU_MEASURER_H_
+
+#include "rtc_base/synchronization/mutex.h"
+
+namespace webrtc {
+
+// This class is thread safe.
+class DefaultVideoQualityAnalyzerCpuMeasurer {
+ public:
+  double GetCpuUsagePercent();
+
+  void StartMeasuringCpuProcessTime();
+  void StopMeasuringCpuProcessTime();
+  void StartExcludingCpuThreadTime();
+  void StopExcludingCpuThreadTime();
+
+ private:
+  Mutex mutex_;
+  int64_t cpu_time_ RTC_GUARDED_BY(mutex_) = 0;
+  int64_t wallclock_time_ RTC_GUARDED_BY(mutex_) = 0;
+};
+
+}  // namespace webrtc
+
+#endif  // TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_CPU_MEASURER_H_