Chromium Code Reviews| 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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 PrefService* local_state) | 62 PrefService* local_state) |
| 63 : task_runner_(task_runner), | 63 : task_runner_(task_runner), |
| 64 pref_service_(local_state), | 64 pref_service_(local_state), |
| 65 weak_factory_(this) { | 65 weak_factory_(this) { |
| 66 } | 66 } |
| 67 | 67 |
| 68 FileMetricsProvider::~FileMetricsProvider() {} | 68 FileMetricsProvider::~FileMetricsProvider() {} |
| 69 | 69 |
| 70 void FileMetricsProvider::RegisterFile(const base::FilePath& path, | 70 void FileMetricsProvider::RegisterFile(const base::FilePath& path, |
| 71 FileType type, | 71 FileType type, |
| 72 FileAssociation file_association, | |
| 72 const base::StringPiece prefs_key) { | 73 const base::StringPiece prefs_key) { |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 | 75 |
| 75 std::unique_ptr<FileInfo> file(new FileInfo(type)); | 76 std::unique_ptr<FileInfo> file(new FileInfo(type)); |
| 76 file->path = path; | 77 file->path = path; |
| 77 file->prefs_key = prefs_key.as_string(); | 78 file->prefs_key = prefs_key.as_string(); |
| 78 | 79 |
| 79 // |prefs_key| may be empty if the caller does not wish to persist the | 80 // |prefs_key| may be empty if the caller does not wish to persist the |
| 80 // state across instances of the program. | 81 // state across instances of the program. |
| 81 if (pref_service_ && !prefs_key.empty()) { | 82 if (pref_service_ && !prefs_key.empty()) { |
| 82 file->last_seen = base::Time::FromInternalValue( | 83 file->last_seen = base::Time::FromInternalValue( |
| 83 pref_service_->GetInt64(metrics::prefs::kMetricsLastSeenPrefix + | 84 pref_service_->GetInt64(metrics::prefs::kMetricsLastSeenPrefix + |
| 84 file->prefs_key)); | 85 file->prefs_key)); |
| 85 } | 86 } |
| 86 | 87 |
| 87 files_to_check_.push_back(std::move(file)); | 88 switch (file_association) { |
| 89 case ASSOCIATE_CURRENT_RUN: | |
| 90 files_to_check_.push_back(std::move(file)); | |
| 91 break; | |
| 92 case ASSOCIATE_PREVIOUS_RUN: | |
| 93 DCHECK_EQ(FILE_HISTOGRAMS_ATOMIC, file->type); | |
| 94 files_for_previous_run_.push_back(std::move(file)); | |
| 95 break; | |
| 96 } | |
| 88 } | 97 } |
| 89 | 98 |
| 90 // static | 99 // static |
| 91 void FileMetricsProvider::RegisterPrefs(PrefRegistrySimple* prefs, | 100 void FileMetricsProvider::RegisterPrefs(PrefRegistrySimple* prefs, |
| 92 const base::StringPiece prefs_key) { | 101 const base::StringPiece prefs_key) { |
| 93 prefs->RegisterInt64Pref(metrics::prefs::kMetricsLastSeenPrefix + | 102 prefs->RegisterInt64Pref(metrics::prefs::kMetricsLastSeenPrefix + |
| 94 prefs_key.as_string(), 0); | 103 prefs_key.as_string(), 0); |
| 95 } | 104 } |
| 96 | 105 |
| 97 // static | 106 // static |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 DCHECK(thread_checker_.CalledOnValidThread()); | 189 DCHECK(thread_checker_.CalledOnValidThread()); |
| 181 base::PersistentHistogramAllocator::Iterator histogram_iter( | 190 base::PersistentHistogramAllocator::Iterator histogram_iter( |
| 182 file->allocator.get()); | 191 file->allocator.get()); |
| 183 | 192 |
| 184 int histogram_count = 0; | 193 int histogram_count = 0; |
| 185 while (true) { | 194 while (true) { |
| 186 std::unique_ptr<base::HistogramBase> histogram = histogram_iter.GetNext(); | 195 std::unique_ptr<base::HistogramBase> histogram = histogram_iter.GetNext(); |
| 187 if (!histogram) | 196 if (!histogram) |
| 188 break; | 197 break; |
| 189 if (file->type == FILE_HISTOGRAMS_ATOMIC) | 198 if (file->type == FILE_HISTOGRAMS_ATOMIC) |
| 190 snapshot_manager->PrepareAbsoluteTakingOwnership(std::move(histogram)); | 199 snapshot_manager->PrepareFinalDeltaTakingOwnership(std::move(histogram)); |
| 191 else | 200 else |
| 192 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); | 201 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); |
| 193 ++histogram_count; | 202 ++histogram_count; |
| 194 } | 203 } |
| 195 | 204 |
| 196 DVLOG(1) << "Reported " << histogram_count << " histograms from " | 205 DVLOG(1) << "Reported " << histogram_count << " histograms from " |
| 197 << file->path.value(); | 206 << file->path.value(); |
| 198 } | 207 } |
| 199 | 208 |
| 200 void FileMetricsProvider::CreateAllocatorForFile(FileInfo* file) { | 209 void FileMetricsProvider::CreateAllocatorForFile(FileInfo* file) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 | 270 |
| 262 if (!file->allocator && !file->mapped && file->data.empty()) | 271 if (!file->allocator && !file->mapped && file->data.empty()) |
| 263 files_to_check_.splice(files_to_check_.end(), files_to_read_, temp); | 272 files_to_check_.splice(files_to_check_.end(), files_to_read_, temp); |
| 264 } | 273 } |
| 265 | 274 |
| 266 // Schedule a check to see if there are new metrics to load. If so, they | 275 // Schedule a check to see if there are new metrics to load. If so, they |
| 267 // will be reported during the next collection run after this one. The | 276 // will be reported during the next collection run after this one. The |
| 268 // check is run off of the worker-pool so as to not cause delays on the | 277 // check is run off of the worker-pool so as to not cause delays on the |
| 269 // main UI thread (which is currently where metric collection is done). | 278 // main UI thread (which is currently where metric collection is done). |
| 270 ScheduleFilesCheck(); | 279 ScheduleFilesCheck(); |
| 280 | |
| 281 // Clear any data for initial metrics since they're always reported | |
| 282 // before the first call to this method. | |
|
Ilya Sherman
2016/05/05 22:22:52
Please document here why it's not appropriate for
bcwhite
2016/05/06 16:59:27
Done.
| |
| 283 #if DCHECK_IS_ON() | |
| 284 for (const std::unique_ptr<FileInfo>& file : files_for_previous_run_) | |
| 285 DCHECK(file->read_complete); | |
| 286 #endif | |
| 287 files_for_previous_run_.clear(); | |
| 288 } | |
| 289 | |
| 290 bool FileMetricsProvider::HasInitialStabilityMetrics() { | |
| 291 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 292 | |
| 293 // Check all files for previous run to see if they need to be read. | |
| 294 for (auto iter = files_for_previous_run_.begin(); | |
| 295 iter != files_for_previous_run_.end();) { | |
| 296 SCOPED_UMA_HISTOGRAM_TIMER("UMA.FileMetricsProvider.InitialCheckTime"); | |
| 297 | |
| 298 auto temp = iter++; | |
| 299 FileInfo* file = temp->get(); | |
| 300 | |
| 301 // This would normally be done on a background I/O thread but there | |
| 302 // hasn't been a chance to run any at the time this method is called. | |
| 303 // Do the check in-line. | |
| 304 AccessResult result = CheckAndMapNewMetrics(file); | |
| 305 UMA_HISTOGRAM_ENUMERATION("UMA.FileMetricsProvider.InitialAccessResult", | |
| 306 result, ACCESS_RESULT_MAX); | |
| 307 | |
| 308 // If it couldn't be accessed, remove it from the list. There is only ever | |
| 309 // one chance to record it no point keeping it around for later. Also mark | |
|
Ilya Sherman
2016/05/05 22:22:52
nit: "it no" -> "it, so no"
bcwhite
2016/05/06 16:59:27
Done.
| |
| 310 // it as having been read since uploading it with a future browser run | |
| 311 // would associate it the previous run which would no longer be the run | |
| 312 // from which it came. | |
| 313 if (result != ACCESS_RESULT_SUCCESS) { | |
| 314 RecordFileAsSeen(file); | |
| 315 files_for_previous_run_.erase(temp); | |
| 316 } | |
| 317 } | |
| 318 | |
| 319 return !files_for_previous_run_.empty(); | |
| 271 } | 320 } |
| 272 | 321 |
| 273 void FileMetricsProvider::RecordHistogramSnapshots( | 322 void FileMetricsProvider::RecordHistogramSnapshots( |
| 274 base::HistogramSnapshotManager* snapshot_manager) { | 323 base::HistogramSnapshotManager* snapshot_manager) { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 324 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 | 325 |
| 277 for (std::unique_ptr<FileInfo>& file : files_to_read_) { | 326 for (std::unique_ptr<FileInfo>& file : files_to_read_) { |
| 278 // Skip this file if the data has already been read. | 327 // Skip this file if the data has already been read. |
| 279 if (file->read_complete) | 328 if (file->read_complete) |
| 280 continue; | 329 continue; |
| 281 | 330 |
| 331 SCOPED_UMA_HISTOGRAM_TIMER("UMA.FileMetricsProvider.SnapshotTime"); | |
|
Ilya Sherman
2016/05/05 22:22:52
Please also add a histogram to measure the snapsho
bcwhite
2016/05/06 16:59:27
Done.
| |
| 332 | |
| 282 // If the file is mapped or loaded then it needs to have an allocator | 333 // If the file is mapped or loaded then it needs to have an allocator |
| 283 // attached to it in order to read histograms out of it. | 334 // attached to it in order to read histograms out of it. |
| 284 if (file->mapped || !file->data.empty()) | 335 if (file->mapped || !file->data.empty()) |
| 285 CreateAllocatorForFile(file.get()); | 336 CreateAllocatorForFile(file.get()); |
| 286 | 337 |
| 287 // A file should not be under "files to read" unless it has an allocator | 338 // A file should not be under "files to read" unless it has an allocator |
| 288 // or is memory-mapped (at which point it will have received an allocator | 339 // or is memory-mapped (at which point it will have received an allocator |
| 289 // above). However, if this method gets called twice before the scheduled- | 340 // above). However, if this method gets called twice before the scheduled- |
| 290 // files-check has a chance to clean up, this may trigger. This also | 341 // files-check has a chance to clean up, this may trigger. This also |
| 291 // catches the case where creating an allocator from the file has failed. | 342 // catches the case where creating an allocator from the file has failed. |
| 292 if (!file->allocator) | 343 if (!file->allocator) |
| 293 continue; | 344 continue; |
| 294 | 345 |
| 295 // Dump all histograms contained within the file to the snapshot-manager. | 346 // Dump all histograms contained within the file to the snapshot-manager. |
| 296 RecordHistogramSnapshotsFromFile(snapshot_manager, file.get()); | 347 RecordHistogramSnapshotsFromFile(snapshot_manager, file.get()); |
| 297 | 348 |
| 298 // Update the last-seen time so it isn't read again unless it changes. | 349 // Update the last-seen time so it isn't read again unless it changes. |
| 299 RecordFileAsSeen(file.get()); | 350 RecordFileAsSeen(file.get()); |
| 300 } | 351 } |
| 301 } | 352 } |
| 302 | 353 |
| 354 void FileMetricsProvider::RecordInitialHistogramSnapshots( | |
| 355 base::HistogramSnapshotManager* snapshot_manager) { | |
| 356 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 357 | |
| 358 for (const std::unique_ptr<FileInfo>& file : files_for_previous_run_) { | |
| 359 SCOPED_UMA_HISTOGRAM_TIMER("UMA.FileMetricsProvider.InitialSnapshotTime"); | |
| 360 | |
| 361 // The file needs to have an allocator attached to it in order to read | |
| 362 // histograms out of it. | |
| 363 DCHECK(file->mapped || !file->data.empty()); | |
| 364 CreateAllocatorForFile(file.get()); | |
| 365 DCHECK(file->allocator); | |
| 366 | |
| 367 // Dump all histograms contained within the file to the snapshot-manager. | |
| 368 RecordHistogramSnapshotsFromFile(snapshot_manager, file.get()); | |
| 369 | |
| 370 // Update the last-seen time so it isn't read again unless it changes. | |
| 371 RecordFileAsSeen(file.get()); | |
| 372 } | |
| 373 } | |
| 374 | |
| 303 } // namespace metrics | 375 } // namespace metrics |
| OLD | NEW |