total_duration_metric_reporter.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/scheduling_metrics/total_duration_metric_reporter.h"
  5. #include "base/cpu_reduction_experiment.h"
  6. namespace scheduling_metrics {
  7. namespace {
  8. constexpr base::TimeDelta kMinimalValue = base::Seconds(1);
  9. constexpr base::TimeDelta kMaximalValue = base::Hours(1);
  10. constexpr int kBucketCount = 50;
  11. } // namespace
  12. TotalDurationMetricReporter::TotalDurationMetricReporter(
  13. const char* positive_histogram_name,
  14. const char* negative_histogram_name)
  15. : positive_histogram_(base::Histogram::FactoryGet(
  16. positive_histogram_name,
  17. kMinimalValue.InSeconds(),
  18. kMaximalValue.InSeconds(),
  19. kBucketCount,
  20. base::Histogram::kUmaTargetedHistogramFlag)),
  21. negative_histogram_(base::Histogram::FactoryGet(
  22. negative_histogram_name,
  23. kMinimalValue.InSeconds(),
  24. kMaximalValue.InSeconds(),
  25. kBucketCount,
  26. base::Histogram::kUmaTargetedHistogramFlag)) {}
  27. TotalDurationMetricReporter::~TotalDurationMetricReporter() = default;
  28. void TotalDurationMetricReporter::RecordAdditionalDuration(
  29. base::TimeDelta duration) {
  30. static base::CpuReductionExperimentFilter filter;
  31. if (!filter.ShouldLogHistograms())
  32. return;
  33. if (reported_value_)
  34. negative_histogram_->Add(reported_value_->InSeconds());
  35. reported_value_ = reported_value_.value_or(base::TimeDelta()) + duration;
  36. positive_histogram_->Add(reported_value_->InSeconds());
  37. }
  38. void TotalDurationMetricReporter::Reset() {
  39. reported_value_ = absl::nullopt;
  40. }
  41. } // namespace scheduling_metrics