OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/metrics/file_metrics_provider.h" | 5 #include "components/metrics/file_metrics_provider.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/histogram_base.h" | 12 #include "base/metrics/histogram_base.h" |
13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
14 #include "base/metrics/histogram_persistence.h" | 14 #include "base/metrics/persistent_histogram_allocator.h" |
15 #include "base/metrics/persistent_memory_allocator.h" | 15 #include "base/metrics/persistent_memory_allocator.h" |
16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "components/metrics/metrics_pref_names.h" | 18 #include "components/metrics/metrics_pref_names.h" |
19 #include "components/metrics/metrics_service.h" | 19 #include "components/metrics/metrics_service.h" |
20 #include "components/prefs/pref_registry_simple.h" | 20 #include "components/prefs/pref_registry_simple.h" |
21 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
22 | 22 |
23 | 23 |
24 namespace metrics { | 24 namespace metrics { |
(...skipping 15 matching lines...) Expand all Loading... |
40 | 40 |
41 // The last-seen time of this file to detect change. | 41 // The last-seen time of this file to detect change. |
42 base::Time last_seen; | 42 base::Time last_seen; |
43 | 43 |
44 // Once a file has been recognized as needing to be read, it is |mapped| | 44 // Once a file has been recognized as needing to be read, it is |mapped| |
45 // into memory. If that file is "atomic" then the data from that file | 45 // into memory. If that file is "atomic" then the data from that file |
46 // will be copied to |data| and the mapped file released. If the file is | 46 // will be copied to |data| and the mapped file released. If the file is |
47 // "active", it remains mapped and nothing is copied to local memory. | 47 // "active", it remains mapped and nothing is copied to local memory. |
48 std::vector<uint8_t> data; | 48 std::vector<uint8_t> data; |
49 scoped_ptr<base::MemoryMappedFile> mapped; | 49 scoped_ptr<base::MemoryMappedFile> mapped; |
50 scoped_ptr<base::PersistentMemoryAllocator> allocator; | 50 scoped_ptr<base::PersistentHistogramAllocator> allocator; |
51 | 51 |
52 private: | 52 private: |
53 DISALLOW_COPY_AND_ASSIGN(FileInfo); | 53 DISALLOW_COPY_AND_ASSIGN(FileInfo); |
54 }; | 54 }; |
55 | 55 |
56 FileMetricsProvider::FileMetricsProvider( | 56 FileMetricsProvider::FileMetricsProvider( |
57 const scoped_refptr<base::TaskRunner>& task_runner, | 57 const scoped_refptr<base::TaskRunner>& task_runner, |
58 PrefService* local_state) | 58 PrefService* local_state) |
59 : task_runner_(task_runner), | 59 : task_runner_(task_runner), |
60 pref_service_(local_state), | 60 pref_service_(local_state), |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 base::Bind(&FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner, | 167 base::Bind(&FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner, |
168 base::Unretained(check_list)), | 168 base::Unretained(check_list)), |
169 base::Bind(&FileMetricsProvider::RecordFilesChecked, | 169 base::Bind(&FileMetricsProvider::RecordFilesChecked, |
170 weak_factory_.GetWeakPtr(), base::Owned(check_list))); | 170 weak_factory_.GetWeakPtr(), base::Owned(check_list))); |
171 } | 171 } |
172 | 172 |
173 void FileMetricsProvider::RecordHistogramSnapshotsFromFile( | 173 void FileMetricsProvider::RecordHistogramSnapshotsFromFile( |
174 base::HistogramSnapshotManager* snapshot_manager, | 174 base::HistogramSnapshotManager* snapshot_manager, |
175 FileInfo* file) { | 175 FileInfo* file) { |
176 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
177 base::PersistentMemoryAllocator::Iterator hist_iter; | 177 base::PersistentHistogramAllocator::Iterator histogram_iter; |
178 file->allocator->CreateIterator(&hist_iter); | 178 file->allocator->CreateIterator(&histogram_iter); |
179 | 179 |
180 int histogram_count = 0; | 180 int histogram_count = 0; |
181 while (true) { | 181 while (true) { |
182 scoped_ptr<base::HistogramBase> histogram( | 182 scoped_ptr<base::HistogramBase> histogram = |
183 base::GetNextPersistentHistogram(file->allocator.get(), &hist_iter)); | 183 file->allocator->GetNextHistogram(&histogram_iter); |
184 if (!histogram) | 184 if (!histogram) |
185 break; | 185 break; |
186 if (file->type == FILE_HISTOGRAMS_ATOMIC) | 186 if (file->type == FILE_HISTOGRAMS_ATOMIC) |
187 snapshot_manager->PrepareAbsoluteTakingOwnership(std::move(histogram)); | 187 snapshot_manager->PrepareAbsoluteTakingOwnership(std::move(histogram)); |
188 else | 188 else |
189 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); | 189 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); |
190 ++histogram_count; | 190 ++histogram_count; |
191 } | 191 } |
192 | 192 |
193 DVLOG(1) << "Reported " << histogram_count << " histograms from " | 193 DVLOG(1) << "Reported " << histogram_count << " histograms from " |
194 << file->path.value(); | 194 << file->path.value(); |
195 } | 195 } |
196 | 196 |
197 void FileMetricsProvider::CreateAllocatorForFile(FileInfo* file) { | 197 void FileMetricsProvider::CreateAllocatorForFile(FileInfo* file) { |
198 DCHECK(!file->allocator); | 198 DCHECK(!file->allocator); |
199 | 199 |
200 // File data was validated earlier. Files are not considered "untrusted" | 200 // File data was validated earlier. Files are not considered "untrusted" |
201 // as some processes might be (e.g. Renderer) so there's no need to check | 201 // as some processes might be (e.g. Renderer) so there's no need to check |
202 // again to try to thwart some malicious actor that may have modified the | 202 // again to try to thwart some malicious actor that may have modified the |
203 // data between then and now. | 203 // data between then and now. |
204 if (file->mapped) { | 204 if (file->mapped) { |
205 DCHECK(file->data.empty()); | 205 DCHECK(file->data.empty()); |
206 // TODO(bcwhite): Make this do read/write when supported for "active". | 206 // TODO(bcwhite): Make this do read/write when supported for "active". |
207 file->allocator.reset(new base::FilePersistentMemoryAllocator( | 207 file->allocator.reset(new base::PersistentHistogramAllocator( |
208 std::move(file->mapped), 0, std::string())); | 208 make_scoped_ptr(new base::FilePersistentMemoryAllocator( |
| 209 std::move(file->mapped), 0, "")))); |
209 } else { | 210 } else { |
210 DCHECK(!file->mapped); | 211 DCHECK(!file->mapped); |
211 file->allocator.reset(new base::PersistentMemoryAllocator( | 212 file->allocator.reset(new base::PersistentHistogramAllocator( |
212 &file->data[0], file->data.size(), 0, 0, "", true)); | 213 make_scoped_ptr(new base::PersistentMemoryAllocator( |
| 214 &file->data[0], file->data.size(), 0, 0, "", true)))); |
213 } | 215 } |
214 } | 216 } |
215 | 217 |
216 void FileMetricsProvider::RecordFilesChecked(FileInfoList* checked) { | 218 void FileMetricsProvider::RecordFilesChecked(FileInfoList* checked) { |
217 DCHECK(thread_checker_.CalledOnValidThread()); | 219 DCHECK(thread_checker_.CalledOnValidThread()); |
218 | 220 |
219 // Move each processed file to either the "to-read" list (for processing) or | 221 // Move each processed file to either the "to-read" list (for processing) or |
220 // the "to-check" list (for future checking). | 222 // the "to-check" list (for future checking). |
221 for (auto iter = checked->begin(); iter != checked->end();) { | 223 for (auto iter = checked->begin(); iter != checked->end();) { |
222 auto temp = iter++; | 224 auto temp = iter++; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 if (file->type == FILE_HISTOGRAMS_ATOMIC) { | 283 if (file->type == FILE_HISTOGRAMS_ATOMIC) { |
282 DCHECK(!file->mapped); | 284 DCHECK(!file->mapped); |
283 file->allocator.reset(); | 285 file->allocator.reset(); |
284 file->data.clear(); | 286 file->data.clear(); |
285 RecordFileAsSeen(file.get()); | 287 RecordFileAsSeen(file.get()); |
286 } | 288 } |
287 } | 289 } |
288 } | 290 } |
289 | 291 |
290 } // namespace metrics | 292 } // namespace metrics |
OLD | NEW |