[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / metrics / throughput_ukm_reporter.cc
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cc/metrics/throughput_ukm_reporter.h"
6
7 #include "cc/trees/ukm_manager.h"
8
9 namespace cc {
10
11 namespace {
12 // Collect UKM once per kNumberOfSamplesToReport UMA reports.
13 constexpr unsigned kNumberOfSamplesToReport = 100u;
14 }  // namespace
15
16 ThroughputUkmReporter::ThroughputUkmReporter(UkmManager* ukm_manager)
17     : ukm_manager_(ukm_manager) {
18   DCHECK(ukm_manager_);
19 }
20
21 ThroughputUkmReporter::~ThroughputUkmReporter() = default;
22
23 void ThroughputUkmReporter::ReportThroughputUkm(
24     const absl::optional<int>& slower_throughput_percent,
25     const absl::optional<int>& impl_throughput_percent,
26     const absl::optional<int>& main_throughput_percent,
27     FrameSequenceTrackerType type) {
28   // It is possible that when a tab shuts down, the ukm_manager_ owned by the
29   // LayerTreeHostImpl is cleared, and yet we try to report to UKM here. In this
30   // case, the |ukm_manager_| here is null.
31   if (!ukm_manager_)
32     return;
33   if (samples_to_next_event_[static_cast<int>(type)] == 0) {
34     // Sample every 100 events. If a tracker reports UMA every 5s, then the
35     // system collects UKM once per 100*5 = 500 seconds. This number may need to
36     // be tuned to not throttle the UKM system.
37     samples_to_next_event_[static_cast<int>(type)] = kNumberOfSamplesToReport;
38     if (impl_throughput_percent) {
39       ukm_manager_->RecordThroughputUKM(
40           type, FrameInfo::SmoothEffectDrivingThread::kCompositor,
41           impl_throughput_percent.value());
42     }
43     if (main_throughput_percent) {
44       ukm_manager_->RecordThroughputUKM(
45           type, FrameInfo::SmoothEffectDrivingThread::kMain,
46           main_throughput_percent.value());
47     }
48   }
49   DCHECK_GT(samples_to_next_event_[static_cast<int>(type)], 0u);
50   samples_to_next_event_[static_cast<int>(type)]--;
51 }
52
53 void ThroughputUkmReporter::ReportAggregateThroughput(
54     AggregationType aggregation_type,
55     int throughput) {
56   if (samples_for_aggregated_report_ == 0) {
57     samples_for_aggregated_report_ = kNumberOfSamplesToReport;
58     ukm_manager_->RecordAggregateThroughput(aggregation_type, throughput);
59   }
60   DCHECK_GT(samples_for_aggregated_report_, 0u);
61   --samples_for_aggregated_report_;
62 }
63
64 uint32_t ThroughputUkmReporter::GetSamplesToNextEventForTesting(int index) {
65   DCHECK_LT(index, static_cast<int>(FrameSequenceTrackerType::kMaxType));
66   return samples_to_next_event_[index];
67 }
68
69 }  // namespace cc