OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/browser/metrics/external_metrics.h" | 5 #include "chromecast/browser/metrics/external_metrics.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
18 #include "base/metrics/statistics_recorder.h" | 18 #include "base/metrics/statistics_recorder.h" |
| 19 #include "base/task_scheduler/post_task.h" |
| 20 #include "base/task_scheduler/task_traits.h" |
| 21 #include "base/threading/thread_restrictions.h" |
19 #include "chromecast/base/metrics/cast_histograms.h" | 22 #include "chromecast/base/metrics/cast_histograms.h" |
20 #include "chromecast/base/metrics/cast_metrics_helper.h" | 23 #include "chromecast/base/metrics/cast_metrics_helper.h" |
21 #include "chromecast/browser/metrics/cast_stability_metrics_provider.h" | 24 #include "chromecast/browser/metrics/cast_stability_metrics_provider.h" |
22 #include "components/metrics/metrics_service.h" | 25 #include "components/metrics/metrics_service.h" |
23 #include "components/metrics/serialization/metric_sample.h" | 26 #include "components/metrics/serialization/metric_sample.h" |
24 #include "components/metrics/serialization/serialization_utils.h" | 27 #include "components/metrics/serialization/serialization_utils.h" |
25 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
26 | 29 |
27 namespace chromecast { | 30 namespace chromecast { |
28 namespace metrics { | 31 namespace metrics { |
(...skipping 11 matching lines...) Expand all Loading... |
40 base::StatisticsRecorder::FindHistogram(name); | 43 base::StatisticsRecorder::FindHistogram(name); |
41 if (!histogram) | 44 if (!histogram) |
42 return true; | 45 return true; |
43 return histogram->HasConstructionArguments(minimum, maximum, bucket_count); | 46 return histogram->HasConstructionArguments(minimum, maximum, bucket_count); |
44 } | 47 } |
45 | 48 |
46 bool CheckLinearValues(const std::string& name, int maximum) { | 49 bool CheckLinearValues(const std::string& name, int maximum) { |
47 return CheckValues(name, 1, maximum, maximum + 1); | 50 return CheckValues(name, 1, maximum, maximum + 1); |
48 } | 51 } |
49 | 52 |
| 53 // Returns a task runner appropriate for running background tasks that perform |
| 54 // file I/O. |
| 55 scoped_refptr<base::SequencedTaskRunner> CreateTaskRunner() { |
| 56 // Note that CollectEvents accesses a global singleton, and thus |
| 57 // scheduling with CONTINUE_ON_SHUTDOWN might not be safe. |
| 58 return base::CreateSequencedTaskRunnerWithTraits( |
| 59 {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 60 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
| 61 } |
| 62 |
50 } // namespace | 63 } // namespace |
51 | 64 |
52 // The interval between external metrics collections in seconds | 65 // The interval between external metrics collections in seconds |
53 static const int kExternalMetricsCollectionIntervalSeconds = 30; | 66 static const int kExternalMetricsCollectionIntervalSeconds = 30; |
54 | 67 |
55 ExternalMetrics::ExternalMetrics( | 68 ExternalMetrics::ExternalMetrics( |
56 CastStabilityMetricsProvider* stability_provider, | 69 CastStabilityMetricsProvider* stability_provider, |
57 const std::string& uma_events_file) | 70 const std::string& uma_events_file) |
58 : stability_provider_(stability_provider), | 71 : stability_provider_(stability_provider), |
59 uma_events_file_(uma_events_file), | 72 uma_events_file_(uma_events_file), |
| 73 task_runner_(CreateTaskRunner()), |
60 weak_factory_(this) { | 74 weak_factory_(this) { |
61 DCHECK(stability_provider); | 75 DCHECK(stability_provider); |
| 76 |
| 77 // The sequence checker verifies that all of the interesting work done by this |
| 78 // class is done on the |task_runner_|, rather than on the sequence that this |
| 79 // object was created on. |
| 80 DETACH_FROM_SEQUENCE(sequence_checker_); |
62 } | 81 } |
63 | 82 |
64 ExternalMetrics::~ExternalMetrics() { | 83 ExternalMetrics::~ExternalMetrics() { |
65 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | 84 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
66 } | 85 } |
67 | 86 |
68 void ExternalMetrics::StopAndDestroy() { | 87 void ExternalMetrics::StopAndDestroy() { |
69 content::BrowserThread::DeleteSoon( | 88 task_runner_->DeleteSoon(FROM_HERE, this); |
70 content::BrowserThread::FILE, FROM_HERE, this); | |
71 } | 89 } |
72 | 90 |
73 void ExternalMetrics::Start() { | 91 void ExternalMetrics::Start() { |
74 bool result = content::BrowserThread::PostDelayedTask( | 92 ScheduleCollection(); |
75 content::BrowserThread::FILE, | |
76 FROM_HERE, | |
77 base::Bind(&ExternalMetrics::CollectEventsAndReschedule, | |
78 weak_factory_.GetWeakPtr()), | |
79 base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); | |
80 DCHECK(result); | |
81 } | 93 } |
82 | 94 |
83 void ExternalMetrics::ProcessExternalEvents(const base::Closure& cb) { | 95 void ExternalMetrics::ProcessExternalEvents(const base::Closure& cb) { |
84 content::BrowserThread::PostTaskAndReply( | 96 task_runner_->PostTaskAndReply( |
85 content::BrowserThread::FILE, FROM_HERE, | 97 FROM_HERE, |
86 base::Bind( | 98 base::BindOnce(base::IgnoreResult(&ExternalMetrics::CollectEvents), |
87 base::IgnoreResult(&ExternalMetrics::CollectEvents), | 99 weak_factory_.GetWeakPtr()), |
88 weak_factory_.GetWeakPtr()), | |
89 cb); | 100 cb); |
90 } | 101 } |
91 | 102 |
92 void ExternalMetrics::RecordCrash(const std::string& crash_kind) { | 103 void ExternalMetrics::RecordCrash(const std::string& crash_kind) { |
93 content::BrowserThread::PostTask( | 104 content::BrowserThread::PostTask( |
94 content::BrowserThread::UI, | 105 content::BrowserThread::UI, FROM_HERE, |
95 FROM_HERE, | 106 base::BindOnce(&CastStabilityMetricsProvider::LogExternalCrash, |
96 base::Bind(&CastStabilityMetricsProvider::LogExternalCrash, | 107 base::Unretained(stability_provider_), crash_kind)); |
97 base::Unretained(stability_provider_), | |
98 crash_kind)); | |
99 } | 108 } |
100 | 109 |
101 void ExternalMetrics::RecordSparseHistogram( | 110 void ExternalMetrics::RecordSparseHistogram( |
102 const ::metrics::MetricSample& sample) { | 111 const ::metrics::MetricSample& sample) { |
103 CHECK_EQ(::metrics::MetricSample::SPARSE_HISTOGRAM, sample.type()); | 112 CHECK_EQ(::metrics::MetricSample::SPARSE_HISTOGRAM, sample.type()); |
104 base::HistogramBase* counter = base::SparseHistogram::FactoryGet( | 113 base::HistogramBase* counter = base::SparseHistogram::FactoryGet( |
105 sample.name(), base::HistogramBase::kUmaTargetedHistogramFlag); | 114 sample.name(), base::HistogramBase::kUmaTargetedHistogramFlag); |
106 counter->Add(sample.sample()); | 115 counter->Add(sample.sample()); |
107 } | 116 } |
108 | 117 |
109 int ExternalMetrics::CollectEvents() { | 118 int ExternalMetrics::CollectEvents() { |
| 119 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 120 base::ThreadRestrictions::AssertIOAllowed(); |
| 121 |
110 std::vector<std::unique_ptr<::metrics::MetricSample>> samples; | 122 std::vector<std::unique_ptr<::metrics::MetricSample>> samples; |
111 ::metrics::SerializationUtils::ReadAndTruncateMetricsFromFile( | 123 ::metrics::SerializationUtils::ReadAndTruncateMetricsFromFile( |
112 uma_events_file_, &samples); | 124 uma_events_file_, &samples); |
113 | 125 |
114 for (auto it = samples.begin(); it != samples.end(); ++it) { | 126 for (auto it = samples.begin(); it != samples.end(); ++it) { |
115 const ::metrics::MetricSample& sample = **it; | 127 const ::metrics::MetricSample& sample = **it; |
116 | 128 |
117 switch (sample.type()) { | 129 switch (sample.type()) { |
118 case ::metrics::MetricSample::CRASH: | 130 case ::metrics::MetricSample::CRASH: |
119 RecordCrash(sample.name()); | 131 RecordCrash(sample.name()); |
(...skipping 25 matching lines...) Expand all Loading... |
145 case ::metrics::MetricSample::SPARSE_HISTOGRAM: | 157 case ::metrics::MetricSample::SPARSE_HISTOGRAM: |
146 RecordSparseHistogram(sample); | 158 RecordSparseHistogram(sample); |
147 break; | 159 break; |
148 } | 160 } |
149 } | 161 } |
150 | 162 |
151 return samples.size(); | 163 return samples.size(); |
152 } | 164 } |
153 | 165 |
154 void ExternalMetrics::CollectEventsAndReschedule() { | 166 void ExternalMetrics::CollectEventsAndReschedule() { |
155 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | 167 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
156 CollectEvents(); | 168 CollectEvents(); |
157 bool result = content::BrowserThread::PostDelayedTask( | 169 ScheduleCollection(); |
158 content::BrowserThread::FILE, | 170 } |
| 171 |
| 172 void ExternalMetrics::ScheduleCollection() { |
| 173 task_runner_->PostDelayedTask( |
159 FROM_HERE, | 174 FROM_HERE, |
160 base::Bind(&ExternalMetrics::CollectEventsAndReschedule, | 175 base::BindOnce(&ExternalMetrics::CollectEventsAndReschedule, |
161 weak_factory_.GetWeakPtr()), | 176 weak_factory_.GetWeakPtr()), |
162 base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); | 177 base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); |
163 DCHECK(result); | |
164 } | 178 } |
165 | 179 |
166 } // namespace metrics | 180 } // namespace metrics |
167 } // namespace chromecast | 181 } // namespace chromecast |
OLD | NEW |