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.
5 #ifndef COMPONENTS_SCHEDULING_METRICS_TASK_DURATION_METRIC_REPORTER_H_
6 #define COMPONENTS_SCHEDULING_METRICS_TASK_DURATION_METRIC_REPORTER_H_
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "base/metrics/histogram.h"
13 #include "base/time/time.h"
19 namespace scheduling_metrics {
21 // A helper class to report total task runtime split by the different types of
22 // |TypeClass|. Only full seconds are reported. Note that partial seconds are
23 // rounded up/down, so that on average the correct value is reported when many
26 // |TaskClass| is an enum which should have kClass field.
27 template <class TaskClass>
28 class TaskDurationMetricReporter {
30 // Note that 1000*1000 is used to get microseconds precision.
31 explicit TaskDurationMetricReporter(const char* metric_name)
32 : value_per_type_histogram_(new base::ScaledLinearHistogram(
35 static_cast<int>(TaskClass::kCount),
36 static_cast<int>(TaskClass::kCount) + 1,
38 base::HistogramBase::kUmaTargetedHistogramFlag)){};
40 void RecordTask(TaskClass task_class, base::TimeDelta duration) {
41 DCHECK_LT(static_cast<int>(task_class),
42 static_cast<int>(TaskClass::kCount));
44 // To get mircoseconds precision, duration is converted to microseconds
45 // since |value_per_type_histogram_| is constructed with a scale of
47 if (!duration.is_zero()) {
48 value_per_type_histogram_->AddScaledCount(
49 static_cast<int>(task_class),
50 base::saturated_cast<int>(duration.InMicroseconds()));
55 std::unique_ptr<base::ScaledLinearHistogram> value_per_type_histogram_;
57 DISALLOW_COPY_AND_ASSIGN(TaskDurationMetricReporter);
60 } // namespace scheduling_metrics
62 #endif // COMPONENTS_SCHEDULING_METRICS_TASK_DURATION_METRIC_REPORTER_H_