total_duration_metric_reporter.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #ifndef COMPONENTS_SCHEDULING_METRICS_TOTAL_DURATION_METRIC_REPORTER_H_
  5. #define COMPONENTS_SCHEDULING_METRICS_TOTAL_DURATION_METRIC_REPORTER_H_
  6. #include "base/component_export.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/metrics/histogram.h"
  9. #include "base/time/time.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace scheduling_metrics {
  12. // Helper class to measure the total duration of the event accounting for
  13. // possibility of the renderer process going away at any point.
  14. //
  15. // This is implemented by "undoing" old total value and "applying" new one
  16. // each time RecordAdditionalDuration() is called.
  17. // "Undoing" is implemented by having a second "negative" histogram, so to
  18. // obtain the result, |positive_histogram - negative_histogram| difference
  19. // should be analysed.
  20. class COMPONENT_EXPORT(SCHEDULING_METRICS) TotalDurationMetricReporter {
  21. public:
  22. TotalDurationMetricReporter(const char* positive_histogram_name,
  23. const char* negative_histogram_name);
  24. ~TotalDurationMetricReporter();
  25. void RecordAdditionalDuration(base::TimeDelta duration);
  26. void Reset();
  27. private:
  28. absl::optional<base::TimeDelta> reported_value_;
  29. raw_ptr<base::HistogramBase> positive_histogram_;
  30. raw_ptr<base::HistogramBase> negative_histogram_;
  31. };
  32. } // namespace scheduling_metrics
  33. #endif // COMPONENTS_SCHEDULING_METRICS_TOTAL_DURATION_METRIC_REPORTER_H_