OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/metrics/file_metrics_provider.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/file.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/files/memory_mapped_file.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_base.h" |
| 13 #include "base/metrics/histogram_persistence.h" |
| 14 #include "base/metrics/persistent_memory_allocator.h" |
| 15 #include "base/task_runner.h" |
| 16 #include "base/threading/worker_pool.h" |
| 17 #include "base/time/time.h" |
| 18 #include "components/metrics/metrics_pref_names.h" |
| 19 #include "components/metrics/metrics_service.h" |
| 20 #include "components/prefs/pref_registry_simple.h" |
| 21 #include "components/prefs/pref_service.h" |
| 22 |
| 23 namespace metrics { |
| 24 |
| 25 // Out-of-line constructor and destructor needed for code efficiency. |
| 26 FileMetricsProvider::FileInformation::FileInformation() {} |
| 27 FileMetricsProvider::FileInformation::~FileInformation() {} |
| 28 |
| 29 FileMetricsProvider::FileMetricsProvider( |
| 30 const scoped_refptr<base::TaskRunner>& task_runner, |
| 31 PrefService* local_state) |
| 32 : task_runner_(task_runner), |
| 33 pref_service_(local_state) { |
| 34 } |
| 35 |
| 36 FileMetricsProvider::~FileMetricsProvider() { |
| 37 } |
| 38 |
| 39 void FileMetricsProvider::RegisterFile(const base::FilePath& path, |
| 40 FileType type, |
| 41 const base::StringPiece& prefs_key) { |
| 42 FileInformation* file = new FileInformation(); |
| 43 file->path = path; |
| 44 file->type = type; |
| 45 file->prefs_key = prefs_key.as_string(); |
| 46 |
| 47 if (pref_service_ && !prefs_key.empty()) { |
| 48 file->last_seen = base::Time::FromInternalValue( |
| 49 pref_service_->GetInt64(metrics::prefs::kMetricsLastSeenPrefix + |
| 50 prefs_key.as_string())); |
| 51 } |
| 52 |
| 53 files_to_check_.push_back(make_scoped_ptr(file)); |
| 54 } |
| 55 |
| 56 // static |
| 57 void FileMetricsProvider::RegisterPrefs(PrefRegistrySimple* prefs, |
| 58 const base::StringPiece& key) { |
| 59 prefs->RegisterInt64Pref(metrics::prefs::kMetricsLastSeenPrefix + |
| 60 key.as_string(), 0); |
| 61 } |
| 62 |
| 63 // static |
| 64 void FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner( |
| 65 FileMetricsProvider::FileInformationList* files) { |
| 66 for (auto& file : *files) |
| 67 CheckAndMapNewMetrics(file.get()); |
| 68 } |
| 69 |
| 70 // static |
| 71 bool FileMetricsProvider::CheckAndMapNewMetrics( |
| 72 FileMetricsProvider::FileInformation* file) { |
| 73 DCHECK(!file->mapped); |
| 74 |
| 75 base::File::Info info; |
| 76 if (!base::GetFileInfo(file->path, &info) || |
| 77 info.is_directory || info.size == 0) { |
| 78 return false; |
| 79 } |
| 80 |
| 81 if (file->last_seen >= info.last_modified) |
| 82 return false; |
| 83 |
| 84 // A new file of metrics has been found. Map it into memory. |
| 85 file->mapped.reset(new base::MemoryMappedFile()); |
| 86 if (!file->mapped->Initialize(file->path)) { |
| 87 NOTREACHED(); |
| 88 file->mapped.reset(); |
| 89 return false; |
| 90 } |
| 91 return true; |
| 92 } |
| 93 |
| 94 void FileMetricsProvider::ScheduleFilesCheck() { |
| 95 FileInformationList* check_list = new FileInformationList(); |
| 96 std::swap(files_to_check_, *check_list); |
| 97 bool posted = task_runner_->PostTaskAndReply( |
| 98 FROM_HERE, |
| 99 base::Bind(&FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner, |
| 100 base::Unretained(check_list)), |
| 101 base::Bind(&FileMetricsProvider::RecordFilesChecked, |
| 102 base::Unretained(this), base::Owned(check_list))); |
| 103 DCHECK(posted); |
| 104 } |
| 105 |
| 106 void FileMetricsProvider::RecordFilesChecked(FileInformationList* checked) { |
| 107 // Move each processed file to either the "to-read" list (for processing) or |
| 108 // the "to-check" list (for future checking). |
| 109 for (auto iter = checked->begin(); iter != checked->end();) { |
| 110 auto temp = iter++; |
| 111 const FileInformation* file = temp->get(); |
| 112 if (file->mapped) |
| 113 files_to_read_.splice(files_to_read_.end(), *checked, temp); |
| 114 else |
| 115 files_to_check_.splice(files_to_check_.end(), *checked, temp); |
| 116 } |
| 117 } |
| 118 |
| 119 void FileMetricsProvider::RecordFileAsSeen(FileInformation* file) { |
| 120 file->last_seen = base::Time::Now(); |
| 121 if (pref_service_ && !file->prefs_key.empty()) { |
| 122 pref_service_->SetInt64(metrics::prefs::kMetricsLastSeenPrefix + |
| 123 file->prefs_key, |
| 124 file->last_seen.ToInternalValue()); |
| 125 } |
| 126 } |
| 127 |
| 128 void FileMetricsProvider::OnDidCreateMetricsLog() { |
| 129 // Move finished metric files back to list of monitored files. |
| 130 for (auto iter = files_to_read_.begin(); iter != files_to_read_.end();) { |
| 131 auto temp = iter++; |
| 132 const FileInformation* file = temp->get(); |
| 133 if (!file->allocator && !file->mapped) |
| 134 files_to_check_.splice(files_to_check_.end(), files_to_read_, temp); |
| 135 } |
| 136 |
| 137 // Schedule a check to see if there are new metrics to load. If so, they |
| 138 // will be reported during the next collection run after this one. The |
| 139 // check is run off of the worker-pool so as to not cause delays on the |
| 140 // main UI thread (which is currently where metric collection is done). |
| 141 ScheduleFilesCheck(); |
| 142 } |
| 143 |
| 144 void FileMetricsProvider::RecordHistogramSnapshots( |
| 145 base::HistogramSnapshotManager* hsm) { |
| 146 for (auto& file : files_to_read_) { |
| 147 if (!file->allocator) { |
| 148 DCHECK(file->mapped); |
| 149 if (!base::FilePersistentMemoryAllocator::IsFileAcceptable( |
| 150 *file->mapped)) { |
| 151 // Something is fundamentally wrong with the file. Ignore it. |
| 152 LOG(ERROR) << "Metrics file \"" << file->path.value() |
| 153 << "\" is not valid -- ignored."; |
| 154 file->mapped.reset(); |
| 155 RecordFileAsSeen(file.get()); |
| 156 NOTREACHED(); |
| 157 continue; |
| 158 } |
| 159 file->allocator.reset(new base::FilePersistentMemoryAllocator( |
| 160 file->mapped.release(), 0, std::string())); |
| 161 } |
| 162 |
| 163 base::PersistentMemoryAllocator::Iterator hist_iter; |
| 164 file->allocator->CreateIterator(&hist_iter); |
| 165 for (;;) { |
| 166 scoped_ptr<base::HistogramBase> histogram( |
| 167 base::GetNextPersistentHistogram(file->allocator.get(), &hist_iter)); |
| 168 if (!histogram) |
| 169 break; |
| 170 if (file->type == FILE_HISTOGRAMS_ATOMIC) |
| 171 hsm->PrepareOnceTakingOwnership(std::move(histogram)); |
| 172 else |
| 173 hsm->PrepareDeltaTakingOwnership(std::move(histogram)); |
| 174 } |
| 175 |
| 176 if (file->type == FILE_HISTOGRAMS_ATOMIC) { |
| 177 DCHECK(!file->mapped); // Ownership should have moved to allocator. |
| 178 file->allocator.reset(); |
| 179 RecordFileAsSeen(file.get()); |
| 180 } |
| 181 } |
| 182 } |
| 183 |
| 184 } // namespace metrics |
OLD | NEW |