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.
5 #include "cc/metrics/throughput_ukm_reporter.h"
7 #include "cc/trees/ukm_manager.h"
12 // Collect UKM once per kNumberOfSamplesToReport UMA reports.
13 constexpr unsigned kNumberOfSamplesToReport = 100u;
16 ThroughputUkmReporter::ThroughputUkmReporter(UkmManager* ukm_manager)
17 : ukm_manager_(ukm_manager) {
21 ThroughputUkmReporter::~ThroughputUkmReporter() = default;
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.
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());
43 if (main_throughput_percent) {
44 ukm_manager_->RecordThroughputUKM(
45 type, FrameInfo::SmoothEffectDrivingThread::kMain,
46 main_throughput_percent.value());
49 DCHECK_GT(samples_to_next_event_[static_cast<int>(type)], 0u);
50 samples_to_next_event_[static_cast<int>(type)]--;
53 void ThroughputUkmReporter::ReportAggregateThroughput(
54 AggregationType aggregation_type,
56 if (samples_for_aggregated_report_ == 0) {
57 samples_for_aggregated_report_ = kNumberOfSamplesToReport;
58 ukm_manager_->RecordAggregateThroughput(aggregation_type, throughput);
60 DCHECK_GT(samples_for_aggregated_report_, 0u);
61 --samples_for_aggregated_report_;
64 uint32_t ThroughputUkmReporter::GetSamplesToNextEventForTesting(int index) {
65 DCHECK_LT(index, static_cast<int>(FrameSequenceTrackerType::kMaxType));
66 return samples_to_next_event_[index];